智能工厂.apk(点击下载) / c.java


package cn.jiguang.dy;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Process;
import android.text.TextUtils;
import cn.jiguang.am.b;
import cn.jiguang.ao.f;
import cn.jiguang.ao.g;
import cn.jiguang.ao.j;
import cn.jiguang.internal.JConstants;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

public class c {
    private static c a;
    private Boolean b;
    private Boolean c;
    private Boolean d = Boolean.TRUE;
    private SharedPreferences e;
    private boolean f;

    /* access modifiers changed from: package-private */
    public class a implements Runnable {
        private Context b;
        private b c;
        private int d;

        public a(Context context, b bVar, int i) {
            this.b = context;
            this.c = bVar;
            this.d = i;
        }

        private void a(Context context) {
            try {
                cn.jiguang.an.b.b("PluginAnalytics", "[reportNow]:" + System.currentTimeMillis());
                File file = new File(cn.jiguang.ao.c.a(context), "dy_analysis");
                if (!file.exists()) {
                    cn.jiguang.an.b.b("PluginAnalytics", "no event file to report");
                    return;
                }
                File[] listFiles = file.listFiles();
                if (listFiles != null) {
                    if (listFiles.length != 0) {
                        for (File file2 : listFiles) {
                            try {
                                if (!file2.getName().endsWith(".tmp")) {
                                    cn.jiguang.an.b.b("PluginAnalytics", "read event from file:" + file2.getAbsolutePath());
                                    String a2 = cn.jiguang.ao.c.a(file2);
                                    if (!TextUtils.isEmpty(a2)) {
                                        JSONObject jSONObject = new JSONObject();
                                        jSONObject.put("itime", System.currentTimeMillis());
                                        jSONObject.put("type", "sdk_stat");
                                        jSONObject.put("events", new JSONArray(a2));
                                        f.a(context, JConstants.SDK_TYPE, 14, null, null, jSONObject.toString());
                                        cn.jiguang.ao.c.a(file2, "");
                                    }
                                }
                            } catch (Throwable th) {
                                cn.jiguang.an.b.b("PluginAnalytics", "report file failed:" + th.getMessage());
                            }
                        }
                        synchronized (c.this.d) {
                            for (File file3 : listFiles) {
                                try {
                                    String name = file3.getName();
                                    if (name.endsWith(".tmp")) {
                                        file3.renameTo(new File(file3.getParentFile(), name.substring(0, name.length() - 4)));
                                    }
                                } catch (Throwable th2) {
                                    cn.jiguang.an.b.b("PluginAnalytics", "report file failed:" + th2.getMessage());
                                }
                            }
                            c.this.b(context);
                        }
                        return;
                    }
                }
                cn.jiguang.an.b.b("PluginAnalytics", "not found child event file to report");
            } catch (Throwable th3) {
                cn.jiguang.an.b.g("PluginAnalytics", "report analysis error:" + th3.getMessage());
            }
        }

        private void a(Context context, List<b> list) {
            String g;
            String jSONArray;
            try {
                JSONArray jSONArray2 = new JSONArray();
                for (int i = 0; i < list.size(); i++) {
                    jSONArray2.put(new JSONObject(list.get(i).e()));
                }
                if (!c.this.c(context) || c.this.f) {
                    g = c.this.g(context);
                    jSONArray = jSONArray2.toString();
                } else {
                    g = c.this.g(context) + ".tmp";
                    jSONArray = jSONArray2.toString();
                }
                cn.jiguang.ao.c.a(context, g, jSONArray);
                cn.jiguang.an.b.b("PluginAnalytics", "saveObjects finished");
            } catch (Throwable th) {
                cn.jiguang.an.b.e("PluginAnalytics", "[saveObjects] failed:" + th.getMessage());
            }
        }

        /* JADX WARNING: Can't wrap try/catch for region: R(2:18|19) */
        /* JADX WARNING: Code restructure failed: missing block: B:19:?, code lost:
            cn.jiguang.ao.c.a(r6, r5.a.g(r6), "");
         */
        /* JADX WARNING: Code restructure failed: missing block: B:21:0x0071, code lost:
            r1 = th;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:22:0x0072, code lost:
            r0 = r2;
         */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:18:0x0064 */
        private List<b> b(Context context) {
            ArrayList arrayList = null;
            try {
                cn.jiguang.an.b.b("PluginAnalytics", "loadObjects start");
                String a2 = cn.jiguang.ao.c.a(context, (!c.this.c(context) || c.this.f) ? c.this.g(context) : c.this.g(context) + ".tmp");
                if (TextUtils.isEmpty(a2)) {
                    return null;
                }
                ArrayList arrayList2 = new ArrayList();
                JSONArray jSONArray = new JSONArray(a2);
                for (int i = 0; i < jSONArray.length(); i++) {
                    JSONObject jSONObject = jSONArray.getJSONObject(i);
                    b bVar = new b();
                    bVar.a(jSONObject);
                    arrayList2.add(bVar);
                }
                return arrayList2;
            } catch (Throwable th) {
                Throwable th2 = th;
                cn.jiguang.an.b.e("PluginAnalytics", "[loadObjects] failed:" + th2.getMessage());
                cn.jiguang.ao.c.a(context, c.this.g(context), "");
                return arrayList;
            }
        }

        public void run() {
            try {
                if (this.b != null) {
                    if (this.d == 0 && this.c == null) {
                        cn.jiguang.an.b.e("PluginAnalytics", "save event info is null");
                    } else if (this.d != 1) {
                        synchronized (c.this.d) {
                            List<b> b2 = b(this.b);
                            if (b2 == null) {
                                b2 = new ArrayList<>();
                            }
                            cn.jiguang.an.b.b("PluginAnalytics", "will save event info:" + this.c.e());
                            if (b2.contains(this.c)) {
                                b bVar = b2.get(b2.indexOf(this.c));
                                bVar.a(this.c);
                                cn.jiguang.an.b.b("PluginAnalytics", "just update,need not add:" + bVar.e());
                            } else {
                                b2.add(this.c);
                            }
                            if (b2.size() >= 200) {
                                b2.remove(0);
                            }
                            a(this.b, b2);
                        }
                    } else if (c.this.f) {
                        cn.jiguang.an.b.b("PluginAnalytics", "first need not report");
                    } else if (c.this.c(this.b)) {
                        a(this.b);
                    }
                }
            } catch (Throwable th) {
                cn.jiguang.an.b.e("PluginAnalytics", "saveAction failed:" + th.getMessage());
            }
        }
    }

    private c() {
        try {
            if (Class.forName("cn.jiguang.dy.FileLocker") != null) {
                this.d = FileLocker.fileLock;
            }
        } catch (Throwable unused) {
            cn.jiguang.an.b.e("PluginAnalytics", "FileLocker not exist");
        }
    }

    public static c a() {
        if (a == null) {
            synchronized (c.class) {
                if (a == null) {
                    a = new c();
                }
            }
        }
        return a;
    }

    private boolean c(Context context, boolean z) {
        SharedPreferences f2 = f(context);
        boolean z2 = f2.getBoolean("first_" + z, true);
        if (z2) {
            this.f = true;
            SharedPreferences.Editor edit = f(context).edit();
            edit.putBoolean("first_" + z, false).apply();
        }
        return z2;
    }

    private boolean d(Context context) {
        c(context, g.b(context));
        boolean z = f(context).getBoolean("plugin_report_switch", false);
        if (z) {
            return z;
        }
        Boolean bool = this.b;
        if (bool != null) {
            return bool.booleanValue();
        }
        this.b = Boolean.valueOf(f(context).getBoolean("need_report", false));
        return this.b.booleanValue();
    }

    private boolean e(Context context) {
        boolean z = f(context).getBoolean("plugin_multi_switch", false);
        if (z) {
            return z;
        }
        Boolean bool = this.c;
        if (bool != null) {
            return bool.booleanValue();
        }
        this.c = Boolean.valueOf(f(context).getBoolean("multi_process", false));
        return this.c.booleanValue();
    }

    private SharedPreferences f(Context context) {
        if (this.e == null) {
            this.e = context.getSharedPreferences("cn.jiguang.prefs", 0);
        }
        return this.e;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String g(Context context) {
        String d2 = j.d(g.a(context));
        if (TextUtils.isEmpty(d2)) {
            return "dy_analysis" + File.separator + Process.myPid();
        }
        return "dy_analysis" + File.separator + d2;
    }

    public void a(Context context) {
        try {
            cn.jiguang.an.b.b("PluginAnalytics", "will [report]");
            if (!d(context)) {
                cn.jiguang.an.b.b("PluginAnalytics", "need not report");
                return;
            }
            f.a(context, JConstants.SDK_TYPE, 76, null, null, new a(context, null, 1));
        } catch (Throwable th) {
            cn.jiguang.an.b.b("PluginAnalytics", "[report] failed:" + th.getMessage());
        }
    }

    public void a(Context context, int i, int i2, cn.jiguang.am.a aVar, int i3, String str) {
        String str2;
        String str3;
        try {
            if (d(context)) {
                String a2 = cn.jiguang.al.a.a(i, 4);
                int i4 = Integer.MAX_VALUE;
                if (!TextUtils.isEmpty(a2)) {
                    try {
                        i4 = Integer.parseInt(a2);
                    } catch (Throwable unused) {
                    }
                }
                if (i2 <= i4 && aVar.k / 1000 == 1) {
                    cn.jiguang.an.b.b("PluginAnalytics", "no plugin ,need not save event");
                    return;
                } else if (aVar != null) {
                    b bVar = new b(context, aVar, i, i2);
                    if (!bVar.b() || e(context)) {
                        if (i3 == 0) {
                            bVar.c();
                        } else if (i3 == 1) {
                            bVar.d();
                        } else if (i3 == 2) {
                            bVar.a(str);
                        }
                        f.a(context, JConstants.SDK_TYPE, 76, null, null, new a(context, bVar, 0));
                        return;
                    }
                    str2 = "PluginAnalytics";
                    str3 = "need not save event in sub process:" + bVar.f();
                } else {
                    return;
                }
            } else {
                str2 = "PluginAnalytics";
                str3 = "need not save event";
            }
            cn.jiguang.an.b.b(str2, str3);
        } catch (Throwable th) {
            cn.jiguang.an.b.b("PluginAnalytics", "onEvent:" + th.getMessage());
        }
    }

    public void a(Context context, boolean z) {
        cn.jiguang.an.b.b("PluginAnalytics", "[setNeedReport] enable:" + z);
        this.b = Boolean.valueOf(z);
        f(context).edit().putBoolean("need_report", z).apply();
    }

    public void b(Context context) {
        f(context).edit().putLong("plugin_report_time_", System.currentTimeMillis()).apply();
    }

    public void b(Context context, boolean z) {
        cn.jiguang.an.b.b("PluginAnalytics", "[setMultiProcessOpen] enable:" + z);
        this.c = Boolean.valueOf(z);
        f(context).edit().putBoolean("multi_process", z).apply();
    }

    public boolean c(Context context) {
        long j = f(context).getLong("plugin_report_time_", 0);
        boolean z = j <= 0 || System.currentTimeMillis() > f(context).getLong("plugin_report_interval_", JConstants.DAY) + j;
        if (!z) {
            cn.jiguang.an.b.b("PluginAnalytics", "no need report, last is " + cn.jiguang.ao.b.a(j));
        }
        return z;
    }
}