package com.kwad.sdk.collector.a; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.Process; import android.os.RemoteException; import android.webkit.WebView; import com.baidu.idl.face.api.manager.LogicConst; import com.kwad.sdk.api.proxy.app.ServiceProxyRemote; import com.kwad.sdk.collector.AppStatusRules; import com.kwad.sdk.collector.d; import com.kwad.sdk.core.e.b; import com.kwad.sdk.utils.SystemUtil; import com.kwad.sdk.utils.ao; import com.kwad.sdk.utils.f; import com.kwad.sdk.utils.s; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import org.json.JSONArray; public class a extends com.kwad.sdk.g.a { private static AtomicBoolean aSb = new AtomicBoolean(false); public static volatile Message aSc; private HandlerC0140a aRZ = new HandlerC0140a((byte) 0); private Messenger aSa = new Messenger(this.aRZ); /* access modifiers changed from: package-private */ /* renamed from: com.kwad.sdk.collector.a.a$a reason: collision with other inner class name */ public static class HandlerC0140a extends Handler { private WeakReference<Service> aSe; private HandlerC0140a() { } /* synthetic */ HandlerC0140a(byte b) { this(); } public final void a(Service service) { if (service != null) { this.aSe = new WeakReference<>(service); } else { this.aSe = null; } } public final void handleMessage(Message message) { super.handleMessage(message); b.d("RemoteService", "handleMessage"); WeakReference<Service> weakReference = this.aSe; Service service = weakReference != null ? weakReference.get() : null; if (service != null) { if (!a.Kn().get()) { b.d("RemoteService", "save buffered message"); a.aSc = Message.obtain(message); return; } final Messenger messenger = message.replyTo; int i = message.what; b.d("RemoteService", "handleMessage what: " + i); if (i == 100) { final Bundle bundle = new Bundle(); f.a(service, new f.b() { /* class com.kwad.sdk.collector.a.a.HandlerC0140a.AnonymousClass1 */ @Override // com.kwad.sdk.utils.f.b public final void O(List<com.kwad.sdk.collector.model.b> list) { b.d("RemoteService", "RemoteService: onAppStatusResult list: " + list); if (list != null && !list.isEmpty()) { b.d("RemoteService", "RemoteService: onAppStatusResult: " + list.size()); JSONArray ak = f.a.ak(list); String str = null; String jSONArray = ak != null ? ak.toString() : null; b.d("RemoteService", "resultJson :" + jSONArray); if (jSONArray != null) { AppStatusRules XQ = f.XQ(); ArrayList<AppStatusRules.Strategy> allStrategy = XQ != null ? XQ.getAllStrategy() : null; if (allStrategy != null) { str = s.an(allStrategy).toString(); } bundle.putString(LogicConst.RESULT_JSON, jSONArray); bundle.putString("allStrategyJson", str); } } try { Message obtain = Message.obtain(); obtain.what = 101; obtain.setData(bundle); messenger.send(obtain); } catch (RemoteException unused) { } if (!(list == null || list.isEmpty())) { for (com.kwad.sdk.collector.model.b bVar : list) { bVar.destroy(); } } } }); } } } } public static AtomicBoolean Kn() { return aSb; } public static void a(Context context, ServiceConnection serviceConnection) { b.d("RemoteService", "bindASService"); context.bindService(new Intent(context, ServiceProxyRemote.class), serviceConnection, 1); } public static void b(Context context, ServiceConnection serviceConnection) { b.d("RemoteService", "unbindASService"); try { context.unbindService(serviceConnection); } catch (Exception e) { b.printStackTrace(e); } } private static boolean be(Context context) { String processName = ao.getProcessName(context); return processName != null && !context.getPackageName().equals(processName); } public static void register() { try { com.kwad.sdk.service.a.g(Class.forName("com.kwad.sdk.api.proxy.app.ServiceProxyRemote"), a.class); } catch (Throwable unused) { } } @Override // com.kwad.sdk.g.a, com.kwad.sdk.api.proxy.IServiceProxy public IBinder onBind(Service service, Intent intent) { return this.aSa.getBinder(); } @Override // com.kwad.sdk.g.a, com.kwad.sdk.api.proxy.IServiceProxy public void onCreate(Service service) { super.onCreate(service); if (Build.VERSION.SDK_INT >= 28) { try { WebView.setDataDirectorySuffix(ao.getProcessName(service.getApplicationContext())); } catch (Exception e) { b.d("RemoteService", "WebView has already been initialized " + e.getMessage()); } } b.d("RemoteService", "onCreate processName:" + ao.getProcessName(service)); if (!SystemUtil.isInMainProcess(service)) { d.a(service, new d.a() { /* class com.kwad.sdk.collector.a.a.AnonymousClass1 */ @Override // com.kwad.sdk.collector.d.a public final void cy(String str) { b.e("RemoteService", "onLoadError: " + str); a.aSb.set(false); } @Override // com.kwad.sdk.collector.d.a public final void onLoaded() { b.d("RemoteService", "onLoaded"); a.aSb.set(true); if (a.aSc != null) { a.this.aRZ.handleMessage(a.aSc); a.aSc = null; } } }); } else { aSb.set(true); } this.aRZ.a(service); } @Override // com.kwad.sdk.g.a, com.kwad.sdk.api.proxy.IServiceProxy public void onDestroy(Service service) { super.onDestroy(service); b.d("RemoteService", "onDestroy"); this.aRZ.a(null); if (be(service)) { b.d("RemoteService", "goto kill myself"); Process.killProcess(Process.myPid()); } } }