admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.weikou.beibeivideo.util.ad;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
 
import com.lcjian.library.util.common.AndroidManifestUtil;
import com.weikou.beibeivideo.entity.ad.AdPositionEnum;
import com.weikou.beibeivideo.entity.ad.AdTypeVO;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.TimeUtil;
import com.weikou.beibeivideo.util.TimeUtils;
import com.weikou.beibeivideo.util.downutil.StringUtils;
 
import org.json.JSONException;
import org.json.JSONObject;
 
//广告帮助
public class AdUtil {
 
    public enum AD_TYPE {
        gdt("广点通"), csj("穿山甲"), gdt2("广点通2.0"), hw("华为"), vivo("vivo");
 
        private String name;
 
        private AD_TYPE(String name) {
            this.name = name;
        }
 
    }
 
    public static AD_TYPE getSmallExpressAdType() {
        if (BeibeiConstant.AD_TYPE != null && AdTypeVO.TYPE_CSJ.equalsIgnoreCase(BeibeiConstant.AD_TYPE.getExpressSmallType())) {
            return AD_TYPE.csj;
        } else {
            return AD_TYPE.gdt;
        }
    }
 
    public static AD_TYPE getBigExpressAdType() {
        if (BeibeiConstant.AD_TYPE != null && AdTypeVO.TYPE_CSJ.equalsIgnoreCase(BeibeiConstant.AD_TYPE.getExpressBigType())) {
            return AD_TYPE.csj;
        } else {
            return AD_TYPE.gdt2;
        }
    }
 
    /**
     * 保存广告配置
     *
     * @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 AD_TYPE getAdType(Context context, AdPositionEnum position) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        String config = share.getString("config", "");
        if (!StringUtils.isNullOrEmpty(config)) {//万一没设置起就用广点通
            try {
                JSONObject object = new JSONObject(config);
                object = object.optJSONObject(position.getPositionName());
                if (object != null)
                    if (AndroidManifestUtil.isHuaWeiChannel(context) && false) {
                        return AD_TYPE.hw;
                    } else {
                        return AD_TYPE.valueOf(object.optString("type"));
                    }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e1) {
                return null;
            }
        } else {
            if (AndroidManifestUtil.isHuaWeiChannel(context) && false) {
                return AD_TYPE.hw;
            } else {
                return AD_TYPE.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.isNullOrEmpty(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;
    }
 
    /**
     * 是否显示PPTV免广告权益
     *
     * @return
     */
    public static boolean showPPTVNoAdReward(Context context) {
        return getAdType(context, AdPositionEnum.pptvNoAdReward) != null;
    }
 
    //是否已经获得pptv视频免广告权益
    public static boolean isGetPPTVNoAdReward(Context context) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        long expireTime = share.getLong("pptvNoAdReward", 0);
        if (System.currentTimeMillis() > expireTime) {
            return false;
        }
        return true;
    }
 
    public static int getPPTVNoAdRewardLeftTimeWithMinute(Context context) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        long expireTime = share.getLong("pptvNoAdReward", 0);
        if (System.currentTimeMillis() > expireTime) {
            return 0;
        } else {
            return (int) ((expireTime - System.currentTimeMillis()) / (1000 * 60));
        }
    }
 
    public static void setPPTVNoAdRewardSuccess(Context context) {
        int hour = getPPTVNoAdRewardHour(context);
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();
        editor.putLong("pptvNoAdReward", System.currentTimeMillis() + 1000 * 60 * 60L * hour);
        editor.commit();
    }
 
    //获取pptv免广告权益的时间(小时记)
    public static int getPPTVNoAdRewardHour(Context context) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        return share.getInt("pptvNoAdRewardHour", 3);
    }
 
    /**
     * 设置pptv无广告权益的时间(小时记)
     *
     * @param context
     * @param hour
     */
    public static void setPPTVNoAdRewardHour(Context context, int hour) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();
        editor.putInt("pptvNoAdRewardHour", hour);
        editor.commit();
    }
 
 
    /**
     * 穿山甲广告是否需要初始化
     *
     * @return
     */
    public static boolean isCanInitCSJ() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
//        return true;
    }
 
 
    // 是否可以显示华为的广告
    public static boolean canShowAdHuaWei(Context context, AdPositionEnum position) {
        return canShowAd(context, "huawei", position);
    }
 
    public static void setAdHuaWeiShown(Context context, AdPositionEnum position) {
        setAdShown(context, "huawei", position);
    }
 
 
    // 是否可以显示华为的广告
    public static boolean canShowAd(Context context, String channel, AdPositionEnum position) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        long lastTime = share.getLong(channel + "_show_time_" + position.name(), 0);
        if (TimeUtils.millisToStringDate(System.currentTimeMillis(), "yyyyMMdd").equalsIgnoreCase(TimeUtils.millisToStringDate(lastTime, "yyyyMMdd"))) {
            //同一天
            return false;
        }
        return true;
    }
 
    public static void setAdShown(Context context, String channel, AdPositionEnum position) {
        SharedPreferences share = context.getSharedPreferences("adConfig", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();
        editor.putLong(channel + "_show_time_" + position.name(), System.currentTimeMillis());
        editor.commit();
    }
 
 
}