翰林优商.apk(点击下载) / f.java


package com.kwad.sdk.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.TextUtils;
import com.baidu.idl.face.api.manager.LogicConst;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.collector.AppStatusRules;
import com.kwad.sdk.collector.c;
import com.kwad.sdk.collector.h;
import com.kwad.sdk.collector.i;
import com.kwad.sdk.collector.j;
import com.kwad.sdk.crash.utils.g;
import com.kwad.sdk.utils.InstalledAppInfoManager;
import com.kwai.video.player.KsMediaMeta;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class f {
    private static Handler Ql;
    private static ServiceConnection aXT = new ServiceConnection() {
        /* class com.kwad.sdk.utils.f.AnonymousClass1 */

        public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            try {
                Messenger messenger = new Messenger(iBinder);
                Message obtain = Message.obtain();
                obtain.what = 100;
                if (f.XR()) {
                    obtain.replyTo = f.bAB;
                    try {
                        messenger.send(obtain);
                    } catch (RemoteException unused) {
                    }
                } else {
                    com.kwad.sdk.core.e.b.w("AppStatusHelper", "clientMessenger init error");
                }
            } catch (SecurityException e) {
                com.kwad.sdk.core.e.b.printStackTraceOnly(e);
                com.kwad.sdk.service.b.gatherException(e);
            }
        }

        public final void onServiceDisconnected(ComponentName componentName) {
        }
    };
    private static Messenger bAB;
    private static volatile ExecutorService bAC;
    private static volatile AppStatusRules bAD;
    private static WeakReference<Context> bAE;
    private static h bAF;

    public static class a implements com.kwad.sdk.core.b {
        private String appName;
        private List<Long> bAL = new ArrayList();
        private String packageName;

        public a() {
        }

        private a(String str, String str2) {
            this.appName = str;
            this.packageName = str2;
        }

        public static JSONArray ak(List<com.kwad.sdk.collector.model.b> list) {
            List<a> list2;
            try {
                list2 = al(list);
            } catch (Exception e) {
                com.kwad.sdk.service.b.gatherException(e);
                list2 = null;
            }
            if (list2 == null) {
                return null;
            }
            JSONArray jSONArray = new JSONArray();
            for (a aVar : list2) {
                jSONArray.put(aVar.toJson());
            }
            return jSONArray;
        }

        private static List<a> al(List<com.kwad.sdk.collector.model.b> list) {
            a aVar;
            if (!(list == null || list.size() == 0)) {
                HashMap hashMap = new HashMap();
                try {
                    for (com.kwad.sdk.collector.model.b bVar : list) {
                        String b = com.kwad.sdk.collector.model.c.b(bVar);
                        if (hashMap.containsKey(b)) {
                            aVar = (a) hashMap.get(b);
                        } else {
                            a aVar2 = new a(com.kwad.sdk.collector.model.c.a(bVar), com.kwad.sdk.collector.model.c.b(bVar));
                            hashMap.put(b, aVar2);
                            aVar = aVar2;
                        }
                        long c = com.kwad.sdk.collector.model.c.c(bVar) / 1000;
                        if (aVar != null) {
                            aVar.at(c);
                        }
                    }
                    return new ArrayList(hashMap.values());
                } catch (ClassCastException e) {
                    com.kwad.sdk.core.e.b.printStackTraceOnly(e);
                }
            }
            return null;
        }

        private void at(long j) {
            this.bAL.add(Long.valueOf(j));
        }

        @Override // com.kwad.sdk.core.b
        public final void parseJson(JSONObject jSONObject) {
            if (jSONObject != null) {
                this.appName = jSONObject.optString("appName");
                this.packageName = jSONObject.optString("packageName");
                JSONArray optJSONArray = jSONObject.optJSONArray("runningTimes");
                if (optJSONArray != null) {
                    this.bAL.clear();
                    int length = optJSONArray.length();
                    for (int i = 0; i < length; i++) {
                        try {
                            this.bAL.add(Long.valueOf(optJSONArray.getLong(i)));
                        } catch (JSONException e) {
                            com.kwad.sdk.core.e.b.printStackTraceOnly(e);
                        }
                    }
                }
            }
        }

        @Override // com.kwad.sdk.core.b
        public final JSONObject toJson() {
            JSONObject jSONObject = new JSONObject();
            s.putValue(jSONObject, "appName", this.appName);
            s.putValue(jSONObject, "packageName", this.packageName);
            JSONArray jSONArray = new JSONArray();
            for (Long l : this.bAL) {
                jSONArray.put(l.longValue());
            }
            s.putValue(jSONObject, "runningTimes", jSONArray);
            return jSONObject;
        }
    }

    public interface b {
        void O(List<com.kwad.sdk.collector.model.b> list);
    }

    /* access modifiers changed from: package-private */
    public static class c extends Handler {
        public c(Looper looper) {
            super(looper);
        }

        private static void a(ArrayList<com.kwad.sdk.collector.model.b> arrayList) {
            JSONArray ak;
            if (arrayList != null && (ak = a.ak(arrayList)) != null) {
                f.bAF.c(ak);
            }
        }

        private static void am(List<a> list) {
            if (list != null) {
                f.bAF.c(s.an(list));
            }
        }

        private void b(Message message) {
            List list;
            ArrayList arrayList;
            Bundle data = message.getData();
            List<AppStatusRules.Strategy> list2 = null;
            if (data != null) {
                try {
                    if (data.containsKey(LogicConst.RESULT_JSON)) {
                        list = s.a(data.getString(LogicConst.RESULT_JSON), new com.kwad.sdk.core.c<a>() {
                            /* class com.kwad.sdk.utils.f.c.AnonymousClass1 */

                            private static a Yb() {
                                return new a();
                            }

                            /* Return type fixed from 'com.kwad.sdk.core.b' to match base method */
                            @Override // com.kwad.sdk.core.c
                            public final /* synthetic */ a Fn() {
                                return Yb();
                            }
                        });
                        arrayList = null;
                    } else {
                        arrayList = (ArrayList) data.getSerializable("data");
                        list = null;
                    }
                } catch (Throwable unused) {
                    arrayList = null;
                    list = null;
                }
                if (arrayList != null) {
                    com.kwad.sdk.core.e.b.d("AppStatusHelper", "ClientHandler: handleMessage data size: " + arrayList.size());
                    a(arrayList);
                }
                if (list != null) {
                    am(list);
                }
            }
            if (!(f.bAE == null || f.bAE.get() == null || data == null)) {
                if (data.containsKey("allStrategyJson")) {
                    String string = data.getString("allStrategyJson");
                    if (string != null) {
                        list2 = s.a(string, new com.kwad.sdk.core.c<AppStatusRules.Strategy>() {
                            /* class com.kwad.sdk.utils.f.c.AnonymousClass2 */

                            private static AppStatusRules.Strategy Yc() {
                                return new AppStatusRules.Strategy();
                            }

                            /* Return type fixed from 'com.kwad.sdk.core.b' to match base method */
                            @Override // com.kwad.sdk.core.c
                            public final /* synthetic */ AppStatusRules.Strategy Fn() {
                                return Yc();
                            }
                        });
                    }
                } else {
                    list2 = (ArrayList) data.getSerializable("allStrategy");
                }
                if (list2 != null) {
                    for (AppStatusRules.Strategy strategy : list2) {
                        long needSaveLaunchTime = strategy.getNeedSaveLaunchTime();
                        if (needSaveLaunchTime >= 0) {
                            i.a((Context) f.bAE.get(), strategy, needSaveLaunchTime);
                        }
                    }
                }
            }
            if (!(f.bAE == null || f.aXT == null)) {
                com.kwad.sdk.core.e.b.d("AppStatusHelper", "unbindASService");
                com.kwad.sdk.collector.a.a.b((Context) f.bAE.get(), f.aXT);
            }
        }

        public final void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 101) {
                try {
                    b(message);
                } catch (Throwable th) {
                    com.kwad.sdk.core.e.b.printStackTraceOnly(th);
                }
            }
        }
    }

    /* access modifiers changed from: package-private */
    public static class d implements b {
        private b bAN = null;

        public d(b bVar) {
        }

        @Override // com.kwad.sdk.utils.f.b
        public final void O(List<com.kwad.sdk.collector.model.b> list) {
            JSONArray ak = a.ak(list);
            if (ak != null) {
                f.bAF.c(ak);
            }
            b bVar = this.bAN;
            if (bVar != null) {
                bVar.O(list);
            }
        }
    }

    public static AppStatusRules XQ() {
        return bAD;
    }

    /* access modifiers changed from: private */
    public static boolean XR() {
        if (bAB == null) {
            try {
                bAB = new Messenger(new c(Looper.getMainLooper()));
            } catch (Throwable unused) {
            }
        }
        return bAB != null;
    }

    /* access modifiers changed from: private */
    public static synchronized void XS() {
        synchronized (f.class) {
            if (bAC == null) {
                bAC = com.kwad.sdk.core.threads.b.On();
            }
        }
    }

    private static boolean XT() {
        try {
            Class<?> cls = Class.forName("com.kwad.sdk.api.proxy.app.ServiceProxyRemote");
            if (cls != null) {
                Context context = KsAdSDK.getContext();
                return context.getPackageManager().queryIntentServices(new Intent(context, cls), 65536).size() > 0;
            }
        } catch (ClassNotFoundException e) {
            com.kwad.sdk.core.e.b.printStackTraceOnly(e);
        }
        return false;
    }

    private static List<com.kwad.sdk.collector.model.b> a(AppStatusRules.Strategy strategy, Map<String, InstalledAppInfoManager.AppPackageInfo> map) {
        boolean isNeedLaunch = strategy.isNeedLaunch();
        com.kwad.sdk.core.e.b.d("AppStatusHelper", "analysisByFile, strategy: " + strategy.getName() + ", needLaunch: " + isNeedLaunch);
        return !isNeedLaunch ? new ArrayList() : com.kwad.sdk.collector.b.Ke().a(strategy, map);
    }

    public static void a(final Context context, long j, h hVar) {
        if (!at.Zc() && !com.kwad.sdk.core.config.d.R(KsMediaMeta.AV_CH_TOP_FRONT_CENTER) && context != null && !c.cc(context)) {
            bAF = hVar;
            boolean isInMainProcess = SystemUtil.isInMainProcess(context);
            com.kwad.sdk.core.e.b.d("AppStatusHelper", "isMainProcess: " + isInMainProcess);
            if (isInMainProcess) {
                bAE = new WeakReference<>(context);
                if (Ql == null) {
                    Ql = new Handler(Looper.getMainLooper());
                }
                Ql.postDelayed(new Runnable() {
                    /* class com.kwad.sdk.utils.f.AnonymousClass2 */

                    public final void run() {
                        com.kwad.sdk.collector.c.a(context, new c.a() {
                            /* class com.kwad.sdk.utils.f.AnonymousClass2.AnonymousClass1 */

                            @Override // com.kwad.sdk.collector.c.a
                            public final void b(AppStatusRules appStatusRules) {
                                appStatusRules.initStatus(context);
                                AppStatusRules unused = f.bAD = appStatusRules;
                                f.b(context, f.bAD);
                                f.ce(context);
                                boolean ef = bd.ef(context);
                                boolean isAppStatusTargetNotEmpty = AppStatusRules.isAppStatusTargetNotEmpty(f.bAD);
                                com.kwad.sdk.core.e.b.d("AppStatusHelper", "appStatusTargetNotEmpty: " + isAppStatusTargetNotEmpty + ", permissionGranted: " + ef);
                                if (ef && isAppStatusTargetNotEmpty) {
                                    long obtainDefaultScanInterval = f.bAD.obtainDefaultScanInterval();
                                    if (obtainDefaultScanInterval > 0) {
                                        f.f(context, obtainDefaultScanInterval);
                                    } else {
                                        f.cg(context);
                                    }
                                }
                                boolean isUploadTargetNotEmpty = AppStatusRules.isUploadTargetNotEmpty(f.bAD);
                                boolean z = f.bAD.obtainUploadConfigFileMaxSize() > 0;
                                com.kwad.sdk.core.e.b.d("AppStatusHelper", "uploadTargetNotEmpty: " + isUploadTargetNotEmpty + ", enableUpload: " + z);
                                if (isUploadTargetNotEmpty && z && ef) {
                                    f.XS();
                                    f.bAC.submit(new Runnable() {
                                        /* class com.kwad.sdk.utils.f.AnonymousClass2.AnonymousClass1.AnonymousClass1 */

                                        public final void run() {
                                            try {
                                                j.a(context, f.bAD);
                                            } catch (Throwable th) {
                                                com.kwad.sdk.service.b.gatherException(th);
                                                com.kwad.sdk.core.e.b.printStackTraceOnly(th);
                                            }
                                        }
                                    });
                                }
                            }

                            @Override // com.kwad.sdk.collector.c.a
                            public final void w(int i, String str) {
                                com.kwad.sdk.core.e.b.e("AppStatusHelper", "fetchAppStatusConfig onFetchError: " + str + ", code: " + i);
                            }
                        });
                    }
                }, 30000);
            }
        }
    }

    public static void a(final Context context, final b bVar) {
        if (context != null && !at.Zc() && !com.kwad.sdk.core.config.d.R(KsMediaMeta.AV_CH_TOP_FRONT_CENTER) && !c.cc(context)) {
            XS();
            bAC.submit(new Runnable() {
                /* class com.kwad.sdk.utils.f.AnonymousClass4 */

                public final void run() {
                    List ch;
                    try {
                        HashSet hashSet = new HashSet();
                        if (bd.ef(context) && (ch = f.ch(context)) != null) {
                            Iterator it = ch.iterator();
                            while (it.hasNext()) {
                                com.kwad.sdk.core.e.b.d("AppStatusHelper", "AppRunningInfo: " + ((com.kwad.sdk.collector.model.b) it.next()));
                            }
                            hashSet.addAll(ch);
                            if (bVar != null) {
                                bVar.O(new ArrayList(hashSet));
                            }
                        }
                    } catch (Throwable th) {
                        com.kwad.sdk.core.e.b.printStackTraceOnly(th);
                        com.kwad.sdk.service.b.gatherException(th);
                    }
                }
            });
        }
    }

    private static List<com.kwad.sdk.collector.model.b> aj(List<com.kwad.sdk.collector.model.b> list) {
        return list.isEmpty() ? list : new ArrayList(new LinkedHashSet(list));
    }

    /* access modifiers changed from: private */
    public static void b(Context context, AppStatusRules appStatusRules) {
        File file = new File(context.getFilesDir(), "LOCAL_APP_STATUS_RULES_JSON");
        String jSONObject = appStatusRules.toJson().toString();
        if (!TextUtils.isEmpty(jSONObject)) {
            g.h(file.getAbsolutePath(), com.kwad.sdk.core.a.c.de(jSONObject), false);
        }
    }

    public static void ce(Context context) {
        if (bAD == null) {
            bAD = cf(context);
        }
    }

    private static AppStatusRules cf(Context context) {
        File file = new File(context.getFilesDir(), "LOCAL_APP_STATUS_RULES_JSON");
        if (!file.exists()) {
            return null;
        }
        try {
            String D = g.D(file);
            if (TextUtils.isEmpty(D)) {
                return null;
            }
            if (com.kwad.sdk.core.a.c.dg(D)) {
                D = com.kwad.sdk.core.a.c.df(D);
            }
            JSONObject jSONObject = new JSONObject(D);
            AppStatusRules appStatusRules = new AppStatusRules();
            appStatusRules.parseJson(jSONObject);
            return appStatusRules;
        } catch (Throwable unused) {
            return null;
        }
    }

    /* access modifiers changed from: private */
    public static void cg(Context context) {
        if (context != null) {
            boolean XT = XT();
            com.kwad.sdk.core.e.b.d("AppStatusHelper", "isServiceAvailable: " + XT);
            if (XT) {
                com.kwad.sdk.collector.a.a.a(context, aXT);
            } else {
                a(context, new d(null));
            }
        }
    }

    /* access modifiers changed from: private */
    public static List<com.kwad.sdk.collector.model.b> ch(Context context) {
        if (!bd.ef(context)) {
            return new ArrayList();
        }
        if (bAD == null) {
            bAD = cf(context);
        }
        return ci(context);
    }

    private static List<com.kwad.sdk.collector.model.b> ci(Context context) {
        ArrayList arrayList = new ArrayList();
        if (at.Zc() || com.kwad.sdk.core.config.d.R(KsMediaMeta.AV_CH_TOP_FRONT_CENTER) || c.cc(context)) {
            return arrayList;
        }
        AppStatusRules XQ = XQ();
        Map<String, InstalledAppInfoManager.AppPackageInfo> dz = au.dz(context);
        for (AppStatusRules.Strategy strategy : i.c(XQ)) {
            arrayList.addAll(a(strategy, dz));
            strategy.setNeedSaveLaunchTime(System.currentTimeMillis());
        }
        AppStatusRules.Strategy d2 = i.d(XQ);
        arrayList.addAll(a(d2, dz));
        d2.setNeedSaveLaunchTime(System.currentTimeMillis());
        return aj(arrayList);
    }

    public static void f(final Context context, final long j) {
        if (Ql == null) {
            Ql = new Handler(Looper.getMainLooper());
        }
        Ql.post(new Runnable() {
            /* class com.kwad.sdk.utils.f.AnonymousClass3 */

            public final void run() {
                f.cg(context);
                f.Ql.postDelayed(this, j);
            }
        });
    }
}