package androidx.core.app;

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.AbstractBinderC1177b;
import b.C1176a;
import b.InterfaceC1178c;
import com.revenuecat.purchases.common.Constants;
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 K implements Handler.Callback, ServiceConnection {

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

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

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

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

    public K(Context context) {
        this.f11206a = context;
        HandlerThread handlerThread = new HandlerThread("NotificationManagerCompat");
        handlerThread.start();
        this.f11207b = new Handler(handlerThread.getLooper(), this);
    }

    public final void a(J j4) {
        boolean z6;
        ArrayDeque arrayDeque;
        boolean zIsLoggable = Log.isLoggable("NotifManCompat", 3);
        ComponentName componentName = j4.f11201a;
        if (zIsLoggable) {
            Log.d("NotifManCompat", "Processing component " + componentName + ", " + j4.f11204d.size() + " queued tasks");
        }
        if (j4.f11204d.isEmpty()) {
            return;
        }
        if (j4.f11202b) {
            z6 = true;
        } else {
            Intent component = new Intent("android.support.BIND_NOTIFICATION_SIDE_CHANNEL").setComponent(componentName);
            Context context = this.f11206a;
            boolean zBindService = context.bindService(component, this, 33);
            j4.f11202b = zBindService;
            if (zBindService) {
                j4.f11205e = 0;
            } else {
                Log.w("NotifManCompat", "Unable to bind to listener " + componentName);
                context.unbindService(this);
            }
            z6 = j4.f11202b;
        }
        if (!z6 || j4.f11203c == null) {
            b(j4);
            return;
        }
        while (true) {
            arrayDeque = j4.f11204d;
            H h5 = (H) arrayDeque.peek();
            if (h5 == null) {
                break;
            }
            try {
                if (Log.isLoggable("NotifManCompat", 3)) {
                    Log.d("NotifManCompat", "Sending task " + h5);
                }
                h5.a(j4.f11203c);
                arrayDeque.remove();
            } catch (DeadObjectException unused) {
                if (Log.isLoggable("NotifManCompat", 3)) {
                    Log.d("NotifManCompat", "Remote service has died: " + componentName);
                }
            } catch (RemoteException e6) {
                Log.w("NotifManCompat", "RemoteException communicating with " + componentName, e6);
            }
        }
        if (arrayDeque.isEmpty()) {
            return;
        }
        b(j4);
    }

    public final void b(J j4) {
        Handler handler = this.f11207b;
        ComponentName componentName = j4.f11201a;
        if (handler.hasMessages(3, componentName)) {
            return;
        }
        int i6 = j4.f11205e;
        int i7 = i6 + 1;
        j4.f11205e = i7;
        if (i7 <= 6) {
            int i8 = (1 << i6) * 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 = j4.f11204d;
        sb.append(arrayDeque.size());
        sb.append(" tasks to ");
        sb.append(componentName);
        sb.append(" after ");
        sb.append(j4.f11205e);
        sb.append(" retries");
        Log.w("NotifManCompat", sb.toString());
        arrayDeque.clear();
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        HashSet hashSet;
        int i6 = message.what;
        InterfaceC1178c interfaceC1178c = null;
        if (i6 == 0) {
            H h5 = (H) message.obj;
            String string = Settings.Secure.getString(this.f11206a.getContentResolver(), "enabled_notification_listeners");
            synchronized (L.f11210c) {
                if (string != null) {
                    try {
                        if (!string.equals(L.f11211d)) {
                            String[] strArrSplit = string.split(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, -1);
                            HashSet hashSet2 = new HashSet(strArrSplit.length);
                            for (String str : strArrSplit) {
                                ComponentName componentNameUnflattenFromString = ComponentName.unflattenFromString(str);
                                if (componentNameUnflattenFromString != null) {
                                    hashSet2.add(componentNameUnflattenFromString.getPackageName());
                                }
                            }
                            L.f11212e = hashSet2;
                            L.f11211d = string;
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                hashSet = L.f11212e;
            }
            if (!hashSet.equals(this.f11209d)) {
                this.f11209d = hashSet;
                List<ResolveInfo> listQueryIntentServices = this.f11206a.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.f11208c.containsKey(componentName2)) {
                        if (Log.isLoggable("NotifManCompat", 3)) {
                            Log.d("NotifManCompat", "Adding listener record for " + componentName2);
                        }
                        this.f11208c.put(componentName2, new J(componentName2));
                    }
                }
                Iterator it = this.f11208c.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());
                        }
                        J j4 = (J) entry.getValue();
                        if (j4.f11202b) {
                            this.f11206a.unbindService(this);
                            j4.f11202b = false;
                        }
                        j4.f11203c = null;
                        it.remove();
                    }
                }
            }
            for (J j6 : this.f11208c.values()) {
                j6.f11204d.add(h5);
                a(j6);
            }
        } else if (i6 == 1) {
            I i7 = (I) message.obj;
            ComponentName componentName3 = i7.f11199a;
            IBinder iBinder = i7.f11200b;
            J j7 = (J) this.f11208c.get(componentName3);
            if (j7 != null) {
                int i8 = AbstractBinderC1177b.f13207a;
                if (iBinder != null) {
                    IInterface iInterfaceQueryLocalInterface = iBinder.queryLocalInterface(InterfaceC1178c.f13208g);
                    if (iInterfaceQueryLocalInterface == null || !(iInterfaceQueryLocalInterface instanceof InterfaceC1178c)) {
                        C1176a c1176a = new C1176a();
                        c1176a.f13206a = iBinder;
                        interfaceC1178c = c1176a;
                    } else {
                        interfaceC1178c = (InterfaceC1178c) iInterfaceQueryLocalInterface;
                    }
                }
                j7.f11203c = interfaceC1178c;
                j7.f11205e = 0;
                a(j7);
                return true;
            }
        } else if (i6 == 2) {
            J j8 = (J) this.f11208c.get((ComponentName) message.obj);
            if (j8 != null) {
                if (j8.f11202b) {
                    this.f11206a.unbindService(this);
                    j8.f11202b = false;
                }
                j8.f11203c = null;
                return true;
            }
        } else {
            if (i6 != 3) {
                return false;
            }
            J j9 = (J) this.f11208c.get((ComponentName) message.obj);
            if (j9 != null) {
                a(j9);
                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.f11207b.obtainMessage(1, new I(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.f11207b.obtainMessage(2, componentName).sendToTarget();
    }
}
