package defpackage;

import android.app.Notification;
import android.app.NotificationManager;
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.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.Log;
import defpackage.uf4;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class dq6 {
    public static String d;
    public static m g;
    public final Context a;
    public final NotificationManager b;
    public static final Object c = new Object();
    public static HashSet e = new HashSet();
    public static final Object f = new Object();

    @l18
    public static class a {
    }

    @l18
    public static class b {
    }

    @l18
    public static class c {
    }

    @l18
    public static class d {
    }

    @l18
    public static class e {
    }

    @l18
    public static class f {
    }

    @l18
    public static class g {
    }

    public static class h implements n {
        @Override // dq6.n
        public final void a(uf4 uf4Var) {
            throw null;
        }

        public final String toString() {
            throw null;
        }
    }

    @m38
    @Retention(RetentionPolicy.SOURCE)
    public @interface i {
    }

    public static class j {
    }

    public static class k implements n {
        public final String a;
        public final int b;
        public final Notification c;

        public k(String str, int i, Notification notification) {
            this.a = str;
            this.b = i;
            this.c = notification;
        }

        @Override // dq6.n
        public final void a(uf4 uf4Var) {
            uf4Var.cs(this.a, this.b, null, this.c);
        }

        public final String toString() {
            StringBuilder sb = new StringBuilder("NotifyTask[packageName:");
            sb.append(this.a);
            sb.append(", id:");
            return n41.q(sb, this.b, ", tag:null]");
        }
    }

    public static class l {
        public final ComponentName a;
        public final IBinder b;

        public l(ComponentName componentName, IBinder iBinder) {
            this.a = componentName;
            this.b = iBinder;
        }
    }

    public static class m implements Handler.Callback, ServiceConnection {
        public final Context a;
        public final Handler b;
        public final HashMap c = new HashMap();
        public HashSet d = new HashSet();

        public static class a {
            public final ComponentName a;
            public uf4 c;
            public boolean b = false;
            public final ArrayDeque d = new ArrayDeque();
            public int e = 0;

            public a(ComponentName componentName) {
                this.a = componentName;
            }
        }

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

        public final void a(a aVar) {
            boolean z;
            ArrayDeque arrayDeque = aVar.d;
            ComponentName componentName = aVar.a;
            if (Log.isLoggable("NotifManCompat", 3)) {
                Objects.toString(componentName);
                arrayDeque.size();
            }
            if (arrayDeque.isEmpty()) {
                return;
            }
            if (aVar.b) {
                z = true;
            } else {
                Intent component = new Intent("android.support.BIND_NOTIFICATION_SIDE_CHANNEL").setComponent(componentName);
                Context context = this.a;
                boolean zBindService = context.bindService(component, this, 33);
                aVar.b = zBindService;
                if (zBindService) {
                    aVar.e = 0;
                } else {
                    Log.w("NotifManCompat", "Unable to bind to listener " + componentName);
                    context.unbindService(this);
                }
                z = aVar.b;
            }
            if (!z || aVar.c == null) {
                b(aVar);
                return;
            }
            while (true) {
                n nVar = (n) arrayDeque.peek();
                if (nVar == null) {
                    break;
                }
                try {
                    if (Log.isLoggable("NotifManCompat", 3)) {
                        nVar.toString();
                    }
                    nVar.a(aVar.c);
                    arrayDeque.remove();
                } catch (DeadObjectException unused) {
                    if (Log.isLoggable("NotifManCompat", 3)) {
                        Objects.toString(componentName);
                    }
                } catch (RemoteException e) {
                    Log.w("NotifManCompat", "RemoteException communicating with " + componentName, e);
                }
            }
            if (arrayDeque.isEmpty()) {
                return;
            }
            b(aVar);
        }

        public final void b(a aVar) {
            ComponentName componentName = aVar.a;
            ArrayDeque arrayDeque = aVar.d;
            Handler handler = this.b;
            if (handler.hasMessages(3, componentName)) {
                return;
            }
            int i = aVar.e + 1;
            aVar.e = i;
            if (i <= 6) {
                handler.sendMessageDelayed(handler.obtainMessage(3, componentName), (1 << r4) * 1000);
                return;
            }
            Log.w("NotifManCompat", "Giving up on delivering " + arrayDeque.size() + " tasks to " + componentName + " after " + aVar.e + " retries");
            arrayDeque.clear();
        }

        @Override // android.os.Handler.Callback
        public final boolean handleMessage(Message message) {
            HashSet hashSet;
            int i = message.what;
            if (i == 0) {
                n nVar = (n) message.obj;
                Context context = this.a;
                Object obj = dq6.c;
                String string = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
                synchronized (dq6.c) {
                    if (string != null) {
                        try {
                            if (!string.equals(dq6.d)) {
                                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());
                                    }
                                }
                                dq6.e = hashSet2;
                                dq6.d = string;
                            }
                        } catch (Throwable th) {
                            throw th;
                        }
                    }
                    hashSet = dq6.e;
                }
                if (!hashSet.equals(this.d)) {
                    this.d = hashSet;
                    List<ResolveInfo> listQueryIntentServices = this.a.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.c.containsKey(componentName2)) {
                            if (Log.isLoggable("NotifManCompat", 3)) {
                                Objects.toString(componentName2);
                            }
                            this.c.put(componentName2, new a(componentName2));
                        }
                    }
                    Iterator it = this.c.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry entry = (Map.Entry) it.next();
                        if (!hashSet3.contains(entry.getKey())) {
                            if (Log.isLoggable("NotifManCompat", 3)) {
                                Objects.toString(entry.getKey());
                            }
                            a aVar = (a) entry.getValue();
                            if (aVar.b) {
                                this.a.unbindService(this);
                                aVar.b = false;
                            }
                            aVar.c = null;
                            it.remove();
                        }
                    }
                }
                for (a aVar2 : this.c.values()) {
                    aVar2.d.add(nVar);
                    a(aVar2);
                }
            } else if (i == 1) {
                l lVar = (l) message.obj;
                ComponentName componentName3 = lVar.a;
                IBinder iBinder = lVar.b;
                a aVar3 = (a) this.c.get(componentName3);
                if (aVar3 != null) {
                    aVar3.c = uf4.b.cw(iBinder);
                    aVar3.e = 0;
                    a(aVar3);
                    return true;
                }
            } else if (i == 2) {
                a aVar4 = (a) this.c.get((ComponentName) message.obj);
                if (aVar4 != null) {
                    if (aVar4.b) {
                        this.a.unbindService(this);
                        aVar4.b = false;
                    }
                    aVar4.c = null;
                    return true;
                }
            } else {
                if (i != 3) {
                    return false;
                }
                a aVar5 = (a) this.c.get((ComponentName) message.obj);
                if (aVar5 != null) {
                    a(aVar5);
                    return true;
                }
            }
            return true;
        }

        @Override // android.content.ServiceConnection
        public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            if (Log.isLoggable("NotifManCompat", 3)) {
                Objects.toString(componentName);
            }
            this.b.obtainMessage(1, new l(componentName, iBinder)).sendToTarget();
        }

        @Override // android.content.ServiceConnection
        public final void onServiceDisconnected(ComponentName componentName) {
            if (Log.isLoggable("NotifManCompat", 3)) {
                Objects.toString(componentName);
            }
            this.b.obtainMessage(2, componentName).sendToTarget();
        }
    }

    public interface n {
        void a(uf4 uf4Var);
    }

    public dq6(Context context) {
        this.a = context;
        this.b = (NotificationManager) context.getSystemService("notification");
    }

    public final void a(int i2, Notification notification) {
        NotificationManager notificationManager = this.b;
        Bundle bundle = notification.extras;
        if (bundle == null || !bundle.getBoolean("android.support.useSideChannel")) {
            notificationManager.notify(null, i2, notification);
            return;
        }
        k kVar = new k(this.a.getPackageName(), i2, notification);
        synchronized (f) {
            try {
                if (g == null) {
                    g = new m(this.a.getApplicationContext());
                }
                g.b.obtainMessage(0, kVar).sendToTarget();
            } catch (Throwable th) {
                throw th;
            }
        }
        notificationManager.cancel(null, i2);
    }
}
