package com.bytedance.pangle.service.a; import android.content.ComponentName; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.RemoteException; import com.bytedance.pangle.Zeus; import com.bytedance.pangle.d; import com.bytedance.pangle.f; import com.bytedance.pangle.log.ZeusLogger; import com.bytedance.pangle.plugin.Plugin; import com.bytedance.pangle.plugin.PluginManager; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; public class a extends d.a { private static volatile a b; private final Handler a = new Handler(Looper.getMainLooper()); private final HashMap<ComponentName, IBinder> c = new HashMap<>(); private final HashMap<ComponentName, b> d = new HashMap<>(); private final C0023a<Intent> e = new C0023a<>(); private final HashMap<ComponentName, com.bytedance.pangle.service.a> f = new HashMap<>(); private final HashSet<ComponentName> g = new HashSet<>(); private final HashSet<ComponentName> h = new HashSet<>(); @Override // com.bytedance.pangle.d.a public IBinder asBinder() { return null; } /* access modifiers changed from: package-private */ public class b extends HashSet<f> { b() { } public final boolean contains(Object obj) { if (super.contains(obj)) { return true; } if (!(obj instanceof f)) { return false; } Iterator it = iterator(); while (it.hasNext()) { try { if (((f) it.next()).a() == ((f) obj).a()) { return true; } } catch (RemoteException e) { e.printStackTrace(); } } return false; } public final boolean remove(Object obj) { if (super.remove(obj)) { return true; } f fVar = null; Iterator it = iterator(); while (true) { if (!it.hasNext()) { break; } f fVar2 = (f) it.next(); try { if (fVar2.a() == ((f) obj).a()) { fVar = fVar2; break; } } catch (RemoteException e) { e.printStackTrace(); } } return super.remove(fVar); } } /* access modifiers changed from: package-private */ /* renamed from: com.bytedance.pangle.service.a.a$a reason: collision with other inner class name */ public class C0023a<T> extends HashMap<f, T> { C0023a() { } public final boolean containsKey(Object obj) { if (super.containsKey(obj)) { return true; } if (!(obj instanceof f)) { return false; } for (K k : keySet()) { try { if (k.a() == ((f) obj).a()) { return true; } } catch (RemoteException e) { e.printStackTrace(); } } return false; } @Override // java.util.AbstractMap, java.util.Map, java.util.HashMap public final T remove(Object obj) { K k; T t = (T) super.remove(obj); if (t != null) { return t; } Iterator<K> it = keySet().iterator(); while (true) { if (!it.hasNext()) { k = null; break; } k = it.next(); try { if (k.a() == ((f) obj).a()) { break; } } catch (RemoteException e) { e.printStackTrace(); } } return (T) super.remove(k); } } public static a b() { if (b == null) { synchronized (a.class) { if (b == null) { b = new a(); } } } return b; } private a() { } @Override // com.bytedance.pangle.d public final ComponentName a(final Intent intent, final String str) { if (Looper.myLooper() == Looper.getMainLooper()) { return c(intent, str); } this.a.post(new Runnable() { /* class com.bytedance.pangle.service.a.a.AnonymousClass1 */ public final void run() { a.this.c(intent, str); } }); return intent.getComponent(); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private synchronized ComponentName c(Intent intent, String str) { ComponentName component = intent.getComponent(); if (!this.f.containsKey(component)) { com.bytedance.pangle.service.a d2 = d(intent, str); if (d2 == null) { return component; } this.f.put(component, d2); this.g.add(component); } com.bytedance.pangle.service.a aVar = this.f.get(component); if (aVar != null) { aVar.onStartCommand(intent, 0, 0); } return component; } private static com.bytedance.pangle.service.a d(Intent intent, String str) { com.bytedance.pangle.service.a e2 = e(intent, str); if (e2 != null) { e2.onCreate(); } return e2; } private static com.bytedance.pangle.service.a e(Intent intent, String str) { boolean z; Exception e2; ComponentName component = intent.getComponent(); Plugin plugin = PluginManager.getInstance().getPlugin(str); try { z = Zeus.loadPlugin(str); try { com.bytedance.pangle.service.a aVar = (com.bytedance.pangle.service.a) plugin.mClassLoader.loadClass(component.getClassName()).newInstance(); aVar.attach(plugin); return aVar; } catch (Exception e3) { e2 = e3; ZeusLogger.e(ZeusLogger.TAG_SERVICE, "newServiceInstance failed! loadPlugin = ".concat(String.valueOf(z)), e2); throw new RuntimeException(e2); } } catch (Exception e4) { e2 = e4; z = false; ZeusLogger.e(ZeusLogger.TAG_SERVICE, "newServiceInstance failed! loadPlugin = ".concat(String.valueOf(z)), e2); throw new RuntimeException(e2); } } @Override // com.bytedance.pangle.d public final boolean b(final Intent intent, String str) { if (Looper.myLooper() == Looper.getMainLooper()) { b().a(intent.getComponent()); return true; } this.a.post(new Runnable() { /* class com.bytedance.pangle.service.a.a.AnonymousClass2 */ public final void run() { a.b().a(intent.getComponent()); } }); return true; } public final synchronized boolean a(ComponentName componentName) { if (!this.f.containsKey(componentName)) { return false; } this.h.add(componentName); return b(componentName); } private boolean b(ComponentName componentName) { if (!this.g.contains(componentName)) { if (this.d.get(componentName) != null) { return false; } c(componentName); return true; } else if (!this.h.contains(componentName) || this.d.containsKey(componentName)) { return false; } else { c(componentName); return true; } } private void c(ComponentName componentName) { com.bytedance.pangle.service.a remove = this.f.remove(componentName); this.h.remove(componentName); this.c.remove(componentName); this.g.remove(componentName); if (remove != null) { remove.onDestroy(); } } @Override // com.bytedance.pangle.d public final boolean a(final Intent intent, final f fVar, final int i, final String str) { if (Looper.myLooper() == Looper.getMainLooper()) { return a(intent, fVar, str); } this.a.post(new Runnable() { /* class com.bytedance.pangle.service.a.a.AnonymousClass3 */ public final void run() { try { a.this.a((a) intent, (Intent) fVar, (f) str); } catch (RemoteException e2) { ZeusLogger.e(ZeusLogger.TAG_SERVICE, "bindService failed", e2); } } }); return true; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private synchronized boolean a(Intent intent, f fVar, String str) { ComponentName component = intent.getComponent(); if (!this.f.containsKey(component)) { com.bytedance.pangle.service.a d2 = d(intent, str); if (d2 == null) { return false; } this.f.put(component, d2); } com.bytedance.pangle.service.a aVar = this.f.get(component); if (!this.c.containsKey(component)) { this.c.put(component, aVar.onBind(intent)); } IBinder iBinder = this.c.get(component); if (iBinder != null) { if (!this.d.containsKey(component)) { b bVar = new b(); bVar.add(fVar); this.d.put(component, bVar); this.e.put(fVar, intent); fVar.a(component, iBinder); } else if (!this.d.get(component).contains(fVar)) { this.d.get(component).add(fVar); this.e.put(fVar, intent); fVar.a(component, iBinder); } } return true; } @Override // com.bytedance.pangle.d public final void a(final f fVar) { if (Looper.myLooper() == Looper.getMainLooper()) { b(fVar); } else { this.a.post(new Runnable() { /* class com.bytedance.pangle.service.a.a.AnonymousClass4 */ public final void run() { a.this.b((a) fVar); } }); } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private synchronized void b(f fVar) { for (ComponentName componentName : this.d.keySet()) { b bVar = this.d.get(componentName); if (bVar.contains(fVar)) { bVar.remove(fVar); Intent remove = this.e.remove(fVar); if (bVar.size() == 0) { this.d.remove(componentName); com.bytedance.pangle.service.a aVar = this.f.get(componentName); if (aVar != null) { aVar.onUnbind(remove); } } b(componentName); return; } } } }