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;
|
}
|
}
|
|
|
}
|