j.java 源代码


package com.xiaomi.miui.pushads.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import com.baidu.mobads.sdk.internal.bx;
import com.xiaomi.push.ce;
import java.io.File;
import java.lang.reflect.Method;
import org.json.JSONException;
import org.json.JSONObject;

public class j extends AsyncTask<String, Integer, Integer> {
    private int a;

    private Context f104a;

    private SharedPreferences f105a;

    private c f106a;

    private ce f107a;

    private String f108a;
    private String b;

    public j(Context context, SharedPreferences sharedPreferences, String str, int i, String str2, c cVar) {
        this.f104a = context;
        this.f106a = cVar;
        this.f108a = str;
        this.f105a = sharedPreferences;
        this.b = str2;
    }

    private int a(File file) {
        String str;
        h hVar = (h) this.f107a;
        String str2 = hVar.b;
        if (str2 == null) {
            return -1;
        }
        int a = b.a(this.f104a, file, str2, hVar);
        d.a("下载广告 imgUrl: " + str2 + " 结果: " + a);
        if (isCancelled() || a != 0) {
            if (isCancelled()) {
                str = "asynctask 被cancel";
            } else {
                str = "网络类型改变,中断下载: " + f.a(this.f104a) + " " + a;
            }
            d.a(str);
        }
        return a;
    }

    private int a(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            int a = a(jSONObject);
            k.a("解析参数并检查, 返回结果: " + m113a(a));
            if (a != 0) {
                return a;
            }
            int c = c(jSONObject);
            if (this.f107a != null) {
                k.a("广告获取最终结果: " + c + " 类型: " + this.f107a.a);
            }
            return c;
        } catch (JSONException unused) {
            return -1;
        }
    }

    private int a(JSONObject jSONObject) {
        if (jSONObject == null || !jSONObject.optString("status").equals(bx.o)) {
            return -1;
        }
        int optInt = jSONObject.optInt("nonsense");
        if (optInt != 0) {
            Log.e("MIUIADSPUSH", "广告无效标志设置: " + optInt);
            d.a("广告无效");
            return -2;
        }
        long optLong = jSONObject.optLong("lastShowTime", 0L);
        l.a("expireTime: " + optLong + " currentTime: " + System.currentTimeMillis());
        if (optLong == 0 || optLong >= System.currentTimeMillis()) {
            return 0;
        }
        d.a("广告已经过期 lastShow: " + optLong + " current: " + System.currentTimeMillis());
        return -4;
    }

    private ce a(int i) {
        ce ceVar = new ce();
        if (i == 1) {
            ceVar = new a();
        } else if (i == 2) {
            ceVar = new h();
        }
        ceVar.d = this.a;
        ceVar.h = this.f108a;
        return ceVar;
    }

    private String m113a(int i) {
        return i != -5 ? i != -4 ? i != -3 ? i != -2 ? i != -1 ? i != 0 ? "" : "成功" : "未知原因" : "广告失效" : "到达上限" : "过期" : "消息不匹配";
    }

    private boolean a(int i, int i2) {
        synchronized (this.f105a) {
            long currentTimeMillis = System.currentTimeMillis();
            long j = this.f105a.getLong("starttime", 0L);
            if (j == 0) {
                this.f105a.edit().putLong("starttime", currentTimeMillis).commit();
                return true;
            }
            if (currentTimeMillis - j > 86400000) {
                this.f105a.edit().putLong("starttime", 0L).commit();
                this.f105a.edit().putInt("notifycount", 0).commit();
                this.f105a.edit().putInt("bubblecount", 0).commit();
                return true;
            }
            if (i2 == 2) {
                if (this.f105a.getInt("notifycount", 0) < i) {
                    return true;
                }
            } else if (i2 == 1 && this.f105a.getInt("bubblecount", 0) < i * 4) {
                return true;
            }
            d.b("超过了每天接受广告的上限");
            return false;
        }
    }

    private int b(JSONObject jSONObject) {
        return jSONObject.optInt("showType");
    }

    private int c(JSONObject jSONObject) {
        boolean z;
        int b = b(jSONObject);
        try {
            Method method = Class.forName("miui.util.NotificationFilterHelper").getMethod("canSendNotifications", Context.class, String.class);
            k.a(this.b);
            z = !((Boolean) method.invoke(null, this.f104a, this.b)).booleanValue();
        } catch (Exception e) {
            Log.d("NotifyAdsDownloader", "reflect errors!");
            e.printStackTrace();
            z = false;
        }
        k.a("是否禁用了通知栏广告 " + z);
        int optInt = jSONObject.optInt("receiveUpperBound");
        boolean a = optInt > 0 ? true ^ a(optInt, b) : false;
        k.a("是否达到上限 " + a);
        try {
            if (a || (b == 2 && z)) {
                k.a("使用候选广告 ");
                if (jSONObject.optLong("subAdId") <= 0) {
                    k.a("没有候选广告 ");
                    return -5;
                }
                JSONObject jSONObject2 = new JSONObject(jSONObject.optString("subAdInfo"));
                int b2 = b(jSONObject2);
                if (b2 == 2 && z) {
                    return -6;
                }
                int a2 = a(jSONObject2);
                k.a("候选广告解析参数并检查: " + a2);
                if (a2 != 0) {
                    return a2;
                }
                ce a3 = a(b2);
                this.f107a = a3;
                a3.a(jSONObject2);
            } else {
                k.a("使用主广告 ");
                ce a4 = a(b);
                this.f107a = a4;
                a4.a(jSONObject);
            }
            return 0;
        } catch (JSONException unused) {
            return -1;
        }
    }

    @Override
    public Integer doInBackground(String... strArr) {
        int a = a(this.f108a);
        if (a == 0) {
            if (this.f107a.a == 2) {
                a = a(this.f104a.getDir("comxiaomimiuipushadssdk", 0));
            }
            return Integer.valueOf(a);
        }
        d.a("广告解析失败 " + a);
        return Integer.valueOf(a);
    }

    @Override
    public void onPostExecute(Integer num) {
        super.onPostExecute(num);
        if (this.f106a != null) {
            d.a("下载 post 的结果是: " + num);
            this.f106a.a(num.intValue(), this.f107a, this);
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        Log.d("ADS_DOWNLOAD", "onCancelled");
    }
}