package androidx.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import androidx.lifecycle.g;
import androidx.lifecycle.k;
import java.lang.reflect.Field;

/* JADX INFO: loaded from: classes.dex */
final class ImmLeaksCleaner implements androidx.lifecycle.i {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static int f43b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static Field f44c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static Field f45d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static Field f46e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private Activity f47a;

    ImmLeaksCleaner(Activity activity) {
        this.f47a = activity;
    }

    @SuppressLint({"SoonBlockedPrivateApi"})
    private static void h() {
        try {
            f43b = 2;
            Field declaredField = InputMethodManager.class.getDeclaredField("mServedView");
            f45d = declaredField;
            declaredField.setAccessible(true);
            Field declaredField2 = InputMethodManager.class.getDeclaredField("mNextServedView");
            f46e = declaredField2;
            declaredField2.setAccessible(true);
            Field declaredField3 = InputMethodManager.class.getDeclaredField("mH");
            f44c = declaredField3;
            declaredField3.setAccessible(true);
            f43b = 1;
        } catch (NoSuchFieldException unused) {
        }
    }

    @Override // androidx.lifecycle.i
    public void g(k kVar, g.b bVar) {
        if (bVar != g.b.ON_DESTROY) {
            return;
        }
        if (f43b == 0) {
            h();
        }
        if (f43b == 1) {
            InputMethodManager inputMethodManager = (InputMethodManager) this.f47a.getSystemService("input_method");
            try {
                Object obj = f44c.get(inputMethodManager);
                if (obj == null) {
                    return;
                }
                synchronized (obj) {
                    try {
                        try {
                            View view = (View) f45d.get(inputMethodManager);
                            if (view == null) {
                                return;
                            }
                            if (view.isAttachedToWindow()) {
                                return;
                            }
                            try {
                                f46e.set(inputMethodManager, null);
                                inputMethodManager.isActive();
                            } catch (IllegalAccessException unused) {
                            }
                        } catch (Throwable th) {
                            throw th;
                        }
                    } catch (ClassCastException unused2) {
                    } catch (IllegalAccessException unused3) {
                    }
                }
            } catch (IllegalAccessException unused4) {
            }
        }
    }
}
