| | |
| | | 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 { |
| | | |
| | |
| | | String config = share.getString("config", ""); |
| | | if (!StringUtils.isEmpty(config)) {//万一没设置起就用广点通 |
| | | try { |
| | | Type type = new TypeToken<List<TypeProbability>>() { |
| | | }.getType(); |
| | | |
| | | JSONObject object = new JSONObject(config); |
| | | return AD_TYPE.valueOf(object.optJSONObject(positionName).optString("type")); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 设置个性化推荐广告 |
| | | * |
| | | * @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; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |