admin
2021-07-20 27bd1f81221b8c8e8047118a64c2beb7bc214bbb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.yeshi.base.utils.ad;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.lcjian.library.util.common.StringUtils;
import com.yeshi.base.entity.ad.AdPositionEnum;
 
import org.json.JSONException;
import org.json.JSONObject;
 
//广告帮助
public class AdUtil {
 
    public enum AD_TYPE {
        gdt("广点通"), csj("穿山甲"), gdt2("广点通2.0");
 
        private String name;
 
        private AD_TYPE(String name) {
            this.name = name;
        }
 
    }
 
    /**
     * 保存广告配置
     *
     * @param context
     * @param json
     */
    public static void saveAdConfig(Context context, JSONObject json) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();
        editor.putString("config", json.toString());
        editor.commit();
    }
 
    /**
     * 获取广告类型
     *
     * @param context
     * @param position
     * @return
     */
    public static AdTypeEnum getAdType(Context context, AdPositionEnum position) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        String config = share.getString("config", "");
        if (!StringUtils.isEmpty(config)) {//万一没设置起就用广点通
            try {
                JSONObject object = new JSONObject(config);
                object = object.optJSONObject(position.getPositionName());
                if (object != null)
                    return AdTypeEnum.valueOf(object.optString("type"));
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e1) {
                return null;
            }
        } else {
            return AdTypeEnum.gdt;
        }
        return null;
    }
 
    public static String getAdPid(Context context, AdPositionEnum position) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        String config = share.getString("config", "");
        if (!StringUtils.isEmpty(config)) {//万一没设置起就用广点通
            try {
                JSONObject object = new JSONObject(config);
                object = object.optJSONObject(position.getPositionName());
                if (object != null)
                    return object.optString("pid");
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e1) {
                return null;
            }
        } else {
            return null;
        }
        return null;
    }
 
}