admin
2022-05-05 355fe96b2a4c7821256d9e8828d2cb9539904878
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.demo.library_ad;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.demo.lib.common.util.common.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qq.e.comm.managers.GDTAdSdk;
import com.qq.e.comm.managers.setting.GlobalSetting;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
public class AdUtil {
 
    public enum AD_TYPE {
        gdt("广点通"), csj("穿山甲");
        private String name;
 
        AD_TYPE(String name) {
            this.name = name;
        }
    }
 
 
    /**
     * 初始化广点通广告
     *
     * @param context
     * @param appId
     */
    public static void initGDTAd(Context context, String appId) {
        GDTAdSdk.init(context, appId);
    }
 
    /**
     * 穿山甲广告初始化
     *
     * @param context
     * @param appId
     * @param downLoadNotify
     * @param initCallback
     */
    public static void initCSJAd(Context context, String appId, boolean downLoadNotify, TTAdSdk.InitCallback initCallback) {
        TTAdManagerHolder.init(context, appId, downLoadNotify, initCallback);
    }
 
 
    /**
     * 保存广告配置
     *
     * @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 positionName
     * @return
     */
    public static AD_TYPE getAdType(Context context, String positionName) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        String config = share.getString("config", "");
        if (!StringUtils.isEmpty(config)) {//万一没设置起就用广点通
            try {
                Type type = new TypeToken<List<TypeProbability>>() {
                }.getType();
 
                JSONObject object = new JSONObject(config);
                JSONArray array = object.optJSONArray(positionName);
                if (array == null) {
                    return null;
                }
                List<TypeProbability> typeList = new Gson().fromJson(array.toString(), type);
                int count = 0;
                Map<AD_TYPE, int[]> map = new HashMap<>();
                for (TypeProbability probability : typeList) {
                    int olcCount = count;
                    count += probability.getWeight();
                    map.put(probability.getType(), new int[]{olcCount, count});
                }
                int random = (int) (Math.random() * count);
 
                for (Iterator<AD_TYPE> its = map.keySet().iterator(); its.hasNext(); ) {
                    AD_TYPE t = its.next();
                    int[] space = map.get(t);
                    if (space[0] <= random && random < space[1]) {
                        return t;
                    }
                }
                return null;
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e1) {
                return null;
            }
        }
        return null;
    }
 
 
    /**
     * 设置个性化推荐广告
     *
     * @param recommend
     */
    public static void setPersonalRecommend(boolean recommend) {
        setPersonalRecommendCSJ(recommend);
        //广点通个性化广告关闭
        GlobalSetting.setPersonalizedState(recommend ? 0 : 1);
    }
 
    //穿山甲
    private static void setPersonalRecommendCSJ(boolean recommend) {
        String content = "";
        try {
            JSONArray jsonArray = new JSONArray();
            JSONObject personalObject = new JSONObject();
            personalObject.put("name", "personal_ads_type");
            personalObject.put("value", recommend ? "1" : "0");
            jsonArray.put(personalObject);
            content = jsonArray.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        TTAdConfig ttAdConfig = new TTAdConfig.Builder()
                .data(content)
                .build();
        TTAdSdk.updateAdConfig(ttAdConfig);
    }
 
 
    class TypeProbability {
        AD_TYPE type;
        int weight;
 
        public AD_TYPE getType() {
            return type;
        }
 
        public void setType(AD_TYPE type) {
            this.type = type;
        }
 
        public int getWeight() {
            return weight;
        }
 
        public void setWeight(int weight) {
            this.weight = weight;
        }
    }
 
 
}