package io.flutter.plugins.webviewflutter;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import io.flutter.plugins.webviewflutter.WebViewProxyApi;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.WeakHashMap;
import r4.AbstractC0980e;
import r4.AbstractC0983h;
import r4.AbstractC0995t;

/* JADX INFO: loaded from: classes.dex */
public final class AndroidWebkitLibraryPigeonInstanceManager {
    public static final Companion Companion = new Companion(null);
    private static final long minHostCreatedIdentifier = 65536;
    private static final String tag = "PigeonInstanceManager";
    private long clearFinalizedWeakReferencesInterval;
    private final PigeonFinalizationListener finalizationListener;
    private final Handler handler;
    private boolean hasFinalizationListenerStopped;
    private final WeakHashMap<IdentityWeakReference<Object>, Long> identifiers;
    private long nextIdentifier;
    private final ReferenceQueue<Object> referenceQueue;
    private final Runnable releaseAllFinalizedInstancesRunnable;
    private final HashMap<Long, Object> strongInstances;
    private final HashMap<Long, IdentityWeakReference<Object>> weakInstances;
    private final HashMap<IdentityWeakReference<Object>, Long> weakReferencesToIdentifiers;

    public static final class Companion {
        public /* synthetic */ Companion(AbstractC0980e abstractC0980e) {
            this();
        }

        public final AndroidWebkitLibraryPigeonInstanceManager create(PigeonFinalizationListener pigeonFinalizationListener) {
            AbstractC0983h.e("finalizationListener", pigeonFinalizationListener);
            return new AndroidWebkitLibraryPigeonInstanceManager(pigeonFinalizationListener);
        }

        private Companion() {
        }
    }

    public static final class IdentityWeakReference<T> extends WeakReference<T> {
        private final int savedHashCode;

        /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
        public IdentityWeakReference(T t4) {
            this(t4, null);
            AbstractC0983h.e("instance", t4);
        }

        public boolean equals(Object obj) {
            T t4 = get();
            return t4 != null ? (obj instanceof IdentityWeakReference) && ((IdentityWeakReference) obj).get() == t4 : obj == this;
        }

        public int hashCode() {
            return this.savedHashCode;
        }

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public IdentityWeakReference(T t4, ReferenceQueue<T> referenceQueue) {
            super(t4, referenceQueue);
            AbstractC0983h.e("instance", t4);
            this.savedHashCode = System.identityHashCode(t4);
        }
    }

    public interface PigeonFinalizationListener {
        void onFinalize(long j5);
    }

    public AndroidWebkitLibraryPigeonInstanceManager(PigeonFinalizationListener pigeonFinalizationListener) {
        AbstractC0983h.e("finalizationListener", pigeonFinalizationListener);
        this.finalizationListener = pigeonFinalizationListener;
        this.identifiers = new WeakHashMap<>();
        this.weakInstances = new HashMap<>();
        this.strongInstances = new HashMap<>();
        this.referenceQueue = new ReferenceQueue<>();
        this.weakReferencesToIdentifiers = new HashMap<>();
        Handler handler = new Handler(Looper.getMainLooper());
        this.handler = handler;
        Runnable runnable = new Runnable() { // from class: io.flutter.plugins.webviewflutter.a
            @Override // java.lang.Runnable
            public final void run() {
                this.f6806k.releaseAllFinalizedInstances();
            }
        };
        this.releaseAllFinalizedInstancesRunnable = runnable;
        this.nextIdentifier = minHostCreatedIdentifier;
        this.clearFinalizedWeakReferencesInterval = 3000L;
        handler.postDelayed(runnable, 3000L);
    }

    private final void addInstance(Object obj, long j5) {
        if (j5 < 0) {
            throw new IllegalArgumentException(("Identifier must be >= 0: " + j5).toString());
        }
        if (this.weakInstances.containsKey(Long.valueOf(j5))) {
            throw new IllegalArgumentException(("Identifier has already been added: " + j5).toString());
        }
        IdentityWeakReference<Object> identityWeakReference = new IdentityWeakReference<>(obj, this.referenceQueue);
        this.identifiers.put(identityWeakReference, Long.valueOf(j5));
        this.weakInstances.put(Long.valueOf(j5), identityWeakReference);
        this.weakReferencesToIdentifiers.put(identityWeakReference, Long.valueOf(j5));
        this.strongInstances.put(Long.valueOf(j5), obj);
    }

    private final void logWarningIfFinalizationListenerHasStopped() {
        if (hasFinalizationListenerStopped()) {
            Log.w(tag, "The manager was used after calls to the PigeonFinalizationListener has been stopped.");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void releaseAllFinalizedInstances() {
        if (hasFinalizationListenerStopped()) {
            return;
        }
        while (true) {
            IdentityWeakReference identityWeakReference = (IdentityWeakReference) this.referenceQueue.poll();
            if (identityWeakReference == null) {
                this.handler.postDelayed(this.releaseAllFinalizedInstancesRunnable, this.clearFinalizedWeakReferencesInterval);
                return;
            }
            Long l5 = (Long) AbstractC0995t.a(this.weakReferencesToIdentifiers).remove(identityWeakReference);
            if (l5 != null) {
                this.weakInstances.remove(l5);
                this.strongInstances.remove(l5);
                this.finalizationListener.onFinalize(l5.longValue());
            }
        }
    }

    public final void addDartCreatedInstance(Object obj, long j5) {
        AbstractC0983h.e("instance", obj);
        logWarningIfFinalizationListenerHasStopped();
        addInstance(obj, j5);
    }

    public final long addHostCreatedInstance(Object obj) {
        AbstractC0983h.e("instance", obj);
        logWarningIfFinalizationListenerHasStopped();
        if (!containsInstance(obj)) {
            long j5 = this.nextIdentifier;
            this.nextIdentifier = 1 + j5;
            addInstance(obj, j5);
            return j5;
        }
        throw new IllegalArgumentException(("Instance of " + obj.getClass() + " has already been added.").toString());
    }

    public final void clear() {
        this.identifiers.clear();
        this.weakInstances.clear();
        this.strongInstances.clear();
        this.weakReferencesToIdentifiers.clear();
    }

    public final boolean containsInstance(Object obj) {
        logWarningIfFinalizationListenerHasStopped();
        return obj != null && this.identifiers.containsKey(new IdentityWeakReference(obj));
    }

    public final long getClearFinalizedWeakReferencesInterval() {
        return this.clearFinalizedWeakReferencesInterval;
    }

    public final Long getIdentifierForStrongReference(Object obj) {
        logWarningIfFinalizationListenerHasStopped();
        if (obj == null) {
            return null;
        }
        Long l5 = this.identifiers.get(new IdentityWeakReference(obj));
        if (l5 != null) {
            this.strongInstances.put(l5, obj);
        }
        return l5;
    }

    public final <T> T getInstance(long j5) {
        logWarningIfFinalizationListenerHasStopped();
        IdentityWeakReference<Object> identityWeakReference = this.weakInstances.get(Long.valueOf(j5));
        if (identityWeakReference != null) {
            return (T) identityWeakReference.get();
        }
        return null;
    }

    public final boolean hasFinalizationListenerStopped() {
        return this.hasFinalizationListenerStopped;
    }

    public final <T> T remove(long j5) {
        logWarningIfFinalizationListenerHasStopped();
        Object androidWebkitLibraryPigeonInstanceManager = getInstance(j5);
        if (androidWebkitLibraryPigeonInstanceManager instanceof WebViewProxyApi.WebViewPlatformView) {
            ((WebViewProxyApi.WebViewPlatformView) androidWebkitLibraryPigeonInstanceManager).destroy();
        }
        return (T) this.strongInstances.remove(Long.valueOf(j5));
    }

    public final void setClearFinalizedWeakReferencesInterval(long j5) {
        this.handler.removeCallbacks(this.releaseAllFinalizedInstancesRunnable);
        this.clearFinalizedWeakReferencesInterval = j5;
        releaseAllFinalizedInstances();
    }

    public final void stopFinalizationListener() {
        this.handler.removeCallbacks(this.releaseAllFinalizedInstancesRunnable);
        this.hasFinalizationListenerStopped = true;
    }
}
