package D;

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.AbstractBinderC0320b;
import b.C0319a;
import b.InterfaceC0321c;
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 U implements Handler.Callback, ServiceConnection {

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

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

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

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

    public U(Context context) {
        this.f337a = context;
        HandlerThread handlerThread = new HandlerThread("NotificationManagerCompat");
        handlerThread.start();
        this.f338b = new Handler(handlerThread.getLooper(), this);
    }

    public final void a(T t4) {
        boolean z5;
        ArrayDeque arrayDeque;
        boolean zIsLoggable = Log.isLoggable("NotifManCompat", 3);
        ComponentName componentName = t4.f332a;
        if (zIsLoggable) {
            Log.d("NotifManCompat", "Processing component " + componentName + ", " + t4.f335d.size() + " queued tasks");
        }
        if (t4.f335d.isEmpty()) {
            return;
        }
        if (t4.f333b) {
            z5 = true;
        } else {
            Intent component = new Intent("android.support.BIND_NOTIFICATION_SIDE_CHANNEL").setComponent(componentName);
            Context context = this.f337a;
            boolean zBindService = context.bindService(component, this, 33);
            t4.f333b = zBindService;
            if (zBindService) {
                t4.f336e = 0;
            } else {
                Log.w("NotifManCompat", "Unable to bind to listener " + componentName);
                context.unbindService(this);
            }
            z5 = t4.f333b;
        }
        if (!z5 || t4.f334c == null) {
            b(t4);
            return;
        }
        while (true) {
            arrayDeque = t4.f335d;
            Q q = (Q) arrayDeque.peek();
            if (q == null) {
                break;
            }
            try {
                if (Log.isLoggable("NotifManCompat", 3)) {
                    Log.d("NotifManCompat", "Sending task " + q);
                }
                q.a(t4.f334c);
                arrayDeque.remove();
            } catch (DeadObjectException unused) {
                if (Log.isLoggable("NotifManCompat", 3)) {
                    Log.d("NotifManCompat", "Remote service has died: " + componentName);
                }
            } catch (RemoteException e2) {
                Log.w("NotifManCompat", "RemoteException communicating with " + componentName, e2);
            }
        }
        if (arrayDeque.isEmpty()) {
            return;
        }
        b(t4);
    }

    public final void b(T t4) {
        Handler handler = this.f338b;
        ComponentName componentName = t4.f332a;
        if (handler.hasMessages(3, componentName)) {
            return;
        }
        int i = t4.f336e;
        int i5 = i + 1;
        t4.f336e = i5;
        if (i5 <= 6) {
            int i6 = (1 << i) * 1000;
            if (Log.isLoggable("NotifManCompat", 3)) {
                Log.d("NotifManCompat", "Scheduling retry for " + i6 + " ms");
            }
            handler.sendMessageDelayed(handler.obtainMessage(3, componentName), i6);
            return;
        }
        StringBuilder sb = new StringBuilder("Giving up on delivering ");
        ArrayDeque arrayDeque = t4.f335d;
        sb.append(arrayDeque.size());
        sb.append(" tasks to ");
        sb.append(componentName);
        sb.append(" after ");
        sb.append(t4.f336e);
        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;
        InterfaceC0321c interfaceC0321c = null;
        if (i == 0) {
            Q q = (Q) message.obj;
            String string = Settings.Secure.getString(this.f337a.getContentResolver(), "enabled_notification_listeners");
            synchronized (V.f341c) {
                if (string != null) {
                    try {
                        if (!string.equals(V.f342d)) {
                            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());
                                }
                            }
                            V.f343e = hashSet2;
                            V.f342d = string;
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                hashSet = V.f343e;
            }
            if (!hashSet.equals(this.f340d)) {
                this.f340d = hashSet;
                List<ResolveInfo> listQueryIntentServices = this.f337a.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.f339c.containsKey(componentName2)) {
                        if (Log.isLoggable("NotifManCompat", 3)) {
                            Log.d("NotifManCompat", "Adding listener record for " + componentName2);
                        }
                        this.f339c.put(componentName2, new T(componentName2));
                    }
                }
                Iterator it = this.f339c.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());
                        }
                        T t4 = (T) entry.getValue();
                        if (t4.f333b) {
                            this.f337a.unbindService(this);
                            t4.f333b = false;
                        }
                        t4.f334c = null;
                        it.remove();
                    }
                }
            }
            for (T t5 : this.f339c.values()) {
                t5.f335d.add(q);
                a(t5);
            }
        } else if (i == 1) {
            S s5 = (S) message.obj;
            ComponentName componentName3 = s5.f330a;
            IBinder iBinder = s5.f331b;
            T t6 = (T) this.f339c.get(componentName3);
            if (t6 != null) {
                int i5 = AbstractBinderC0320b.f4623c;
                if (iBinder != null) {
                    IInterface iInterfaceQueryLocalInterface = iBinder.queryLocalInterface(InterfaceC0321c.f4624a);
                    if (iInterfaceQueryLocalInterface == null || !(iInterfaceQueryLocalInterface instanceof InterfaceC0321c)) {
                        C0319a c0319a = new C0319a();
                        c0319a.f4622c = iBinder;
                        interfaceC0321c = c0319a;
                    } else {
                        interfaceC0321c = (InterfaceC0321c) iInterfaceQueryLocalInterface;
                    }
                }
                t6.f334c = interfaceC0321c;
                t6.f336e = 0;
                a(t6);
                return true;
            }
        } else if (i == 2) {
            T t7 = (T) this.f339c.get((ComponentName) message.obj);
            if (t7 != null) {
                if (t7.f333b) {
                    this.f337a.unbindService(this);
                    t7.f333b = false;
                }
                t7.f334c = null;
                return true;
            }
        } else {
            if (i != 3) {
                return false;
            }
            T t8 = (T) this.f339c.get((ComponentName) message.obj);
            if (t8 != null) {
                a(t8);
                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.f338b.obtainMessage(1, new S(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.f338b.obtainMessage(2, componentName).sendToTarget();
    }
}
