package B;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Message;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.Log;
import b.AbstractBinderC0522b;
import b.C0521a;
import b.InterfaceC0523c;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final class m implements Handler.Callback, ServiceConnection {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final Context f496q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final Handler f497r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final HashMap f498s = new HashMap();

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public HashSet f499t = new HashSet();

    public m(Context context) {
        this.f496q = context;
        HandlerThread handlerThread = new HandlerThread("NotificationManagerCompat");
        handlerThread.start();
        this.f497r = new Handler(handlerThread.getLooper(), this);
    }

    public final void a(l lVar) {
        boolean z8;
        ArrayDeque arrayDeque;
        boolean zIsLoggable = Log.isLoggable("NotifManCompat", 3);
        ComponentName componentName = lVar.f491a;
        if (zIsLoggable) {
            Log.d("NotifManCompat", "Processing component " + componentName + ", " + lVar.f494d.size() + " queued tasks");
        }
        if (lVar.f494d.isEmpty()) {
            return;
        }
        if (lVar.f492b) {
            z8 = true;
        } else {
            Intent component = new Intent("android.support.BIND_NOTIFICATION_SIDE_CHANNEL").setComponent(componentName);
            Context context = this.f496q;
            boolean zBindService = context.bindService(component, this, 33);
            lVar.f492b = zBindService;
            if (zBindService) {
                lVar.f495e = 0;
            } else {
                Log.w("NotifManCompat", "Unable to bind to listener " + componentName);
                context.unbindService(this);
            }
            z8 = lVar.f492b;
        }
        if (!z8 || lVar.f493c == null) {
            b(lVar);
            return;
        }
        while (true) {
            arrayDeque = lVar.f494d;
            j jVar = (j) arrayDeque.peek();
            if (jVar == null) {
                break;
            }
            try {
                if (Log.isLoggable("NotifManCompat", 3)) {
                    Log.d("NotifManCompat", "Sending task " + jVar);
                }
                jVar.a(lVar.f493c);
                arrayDeque.remove();
            } catch (DeadObjectException unused) {
                if (Log.isLoggable("NotifManCompat", 3)) {
                    Log.d("NotifManCompat", "Remote service has died: " + componentName);
                }
            } catch (RemoteException e8) {
                Log.w("NotifManCompat", "RemoteException communicating with " + componentName, e8);
            }
        }
        if (arrayDeque.isEmpty()) {
            return;
        }
        b(lVar);
    }

    public final void b(l lVar) {
        Handler handler = this.f497r;
        ComponentName componentName = lVar.f491a;
        if (handler.hasMessages(3, componentName)) {
            return;
        }
        int i = lVar.f495e;
        int i5 = i + 1;
        lVar.f495e = i5;
        if (i5 <= 6) {
            int i8 = (1 << i) * 1000;
            if (Log.isLoggable("NotifManCompat", 3)) {
                Log.d("NotifManCompat", "Scheduling retry for " + i8 + " ms");
            }
            handler.sendMessageDelayed(handler.obtainMessage(3, componentName), i8);
            return;
        }
        StringBuilder sb = new StringBuilder("Giving up on delivering ");
        ArrayDeque arrayDeque = lVar.f494d;
        sb.append(arrayDeque.size());
        sb.append(" tasks to ");
        sb.append(componentName);
        sb.append(" after ");
        sb.append(lVar.f495e);
        sb.append(" retries");
        Log.w("NotifManCompat", sb.toString());
        arrayDeque.clear();
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        HashSet hashSet;
        int i = message.what;
        InterfaceC0523c interfaceC0523c = null;
        if (i == 0) {
            j jVar = (j) message.obj;
            String string = Settings.Secure.getString(this.f496q.getContentResolver(), "enabled_notification_listeners");
            synchronized (n.f500c) {
                if (string != null) {
                    try {
                        if (!string.equals(n.f501d)) {
                            String[] strArrSplit = string.split(":", -1);
                            HashSet hashSet2 = new HashSet(strArrSplit.length);
                            for (String str : strArrSplit) {
                                ComponentName componentNameUnflattenFromString = ComponentName.unflattenFromString(str);
                                if (componentNameUnflattenFromString != null) {
                                    hashSet2.add(componentNameUnflattenFromString.getPackageName());
                                }
                            }
                            n.f502e = hashSet2;
                            n.f501d = string;
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                hashSet = n.f502e;
            }
            if (!hashSet.equals(this.f499t)) {
                this.f499t = hashSet;
                List<ResolveInfo> listQueryIntentServices = this.f496q.getPackageManager().queryIntentServices(new Intent().setAction("android.support.BIND_NOTIFICATION_SIDE_CHANNEL"), 0);
                HashSet<ComponentName> hashSet3 = new HashSet();
                for (ResolveInfo resolveInfo : listQueryIntentServices) {
                    if (hashSet.contains(resolveInfo.serviceInfo.packageName)) {
                        ServiceInfo serviceInfo = resolveInfo.serviceInfo;
                        ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
                        if (resolveInfo.serviceInfo.permission != null) {
                            Log.w("NotifManCompat", "Permission present on component " + componentName + ", not adding listener record.");
                        } else {
                            hashSet3.add(componentName);
                        }
                    }
                }
                for (ComponentName componentName2 : hashSet3) {
                    if (!this.f498s.containsKey(componentName2)) {
                        if (Log.isLoggable("NotifManCompat", 3)) {
                            Log.d("NotifManCompat", "Adding listener record for " + componentName2);
                        }
                        this.f498s.put(componentName2, new l(componentName2));
                    }
                }
                Iterator it = this.f498s.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    if (!hashSet3.contains(entry.getKey())) {
                        if (Log.isLoggable("NotifManCompat", 3)) {
                            Log.d("NotifManCompat", "Removing listener record for " + entry.getKey());
                        }
                        l lVar = (l) entry.getValue();
                        if (lVar.f492b) {
                            this.f496q.unbindService(this);
                            lVar.f492b = false;
                        }
                        lVar.f493c = null;
                        it.remove();
                    }
                }
            }
            for (l lVar2 : this.f498s.values()) {
                lVar2.f494d.add(jVar);
                a(lVar2);
            }
        } else if (i == 1) {
            k kVar = (k) message.obj;
            ComponentName componentName3 = kVar.f489a;
            IBinder iBinder = kVar.f490b;
            l lVar3 = (l) this.f498s.get(componentName3);
            if (lVar3 != null) {
                int i5 = AbstractBinderC0522b.f9130e;
                if (iBinder != null) {
                    IInterface iInterfaceQueryLocalInterface = iBinder.queryLocalInterface(InterfaceC0523c.f9131c);
                    if (iInterfaceQueryLocalInterface == null || !(iInterfaceQueryLocalInterface instanceof InterfaceC0523c)) {
                        C0521a c0521a = new C0521a();
                        c0521a.f9129e = iBinder;
                        interfaceC0523c = c0521a;
                    } else {
                        interfaceC0523c = (InterfaceC0523c) iInterfaceQueryLocalInterface;
                    }
                }
                lVar3.f493c = interfaceC0523c;
                lVar3.f495e = 0;
                a(lVar3);
                return true;
            }
        } else if (i == 2) {
            l lVar4 = (l) this.f498s.get((ComponentName) message.obj);
            if (lVar4 != null) {
                if (lVar4.f492b) {
                    this.f496q.unbindService(this);
                    lVar4.f492b = false;
                }
                lVar4.f493c = null;
                return true;
            }
        } else {
            if (i != 3) {
                return false;
            }
            l lVar5 = (l) this.f498s.get((ComponentName) message.obj);
            if (lVar5 != null) {
                a(lVar5);
                return true;
            }
        }
        return true;
    }

    @Override // android.content.ServiceConnection
    public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        if (Log.isLoggable("NotifManCompat", 3)) {
            Log.d("NotifManCompat", "Connected to service " + componentName);
        }
        this.f497r.obtainMessage(1, new k(componentName, iBinder)).sendToTarget();
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        if (Log.isLoggable("NotifManCompat", 3)) {
            Log.d("NotifManCompat", "Disconnected from service " + componentName);
        }
        this.f497r.obtainMessage(2, componentName).sendToTarget();
    }
}
