package com.huawei.hms.common.internal; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.util.SparseArray; import com.huawei.hms.api.HuaweiApiClient; /* loaded from: classes2.dex */ public class AutoLifecycleFragment extends Fragment { private final SparseArray a = new SparseArray<>(); private boolean b; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class a { public final HuaweiApiClient a; protected final int b; public a(int i, HuaweiApiClient huaweiApiClient) { this.a = huaweiApiClient; this.b = i; } public void a() { this.a.disconnect(); } } public static AutoLifecycleFragment getInstance(Activity activity) { Preconditions.checkMainThread("Must be called on the main thread"); try { AutoLifecycleFragment autoLifecycleFragment = (AutoLifecycleFragment) activity.getFragmentManager().findFragmentByTag("HmsAutoLifecycleFrag"); FragmentManager fragmentManager = activity.getFragmentManager(); if (autoLifecycleFragment != null) { return autoLifecycleFragment; } AutoLifecycleFragment autoLifecycleFragment2 = new AutoLifecycleFragment(); fragmentManager.beginTransaction().add(autoLifecycleFragment2, "HmsAutoLifecycleFrag").commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); return autoLifecycleFragment2; } catch (ClassCastException e) { throw new IllegalStateException("Fragment with tag HmsAutoLifecycleFrag is not a AutoLifecycleFragment", e); } } @Override // android.app.Fragment public void onCreate(Bundle bundle) { super.onCreate(bundle); } @Override // android.app.Fragment public void onStart() { super.onStart(); this.b = true; for (int i = 0; i < this.a.size(); i++) { this.a.valueAt(i).a.connect((Activity) null); } } @Override // android.app.Fragment public void onStop() { super.onStop(); this.b = false; for (int i = 0; i < this.a.size(); i++) { this.a.valueAt(i).a.disconnect(); } } public void startAutoMange(int i, HuaweiApiClient huaweiApiClient) { Preconditions.checkNotNull(huaweiApiClient, "HuaweiApiClient instance cannot be null"); Preconditions.checkState(this.a.indexOfKey(i) < 0, "Already managing a HuaweiApiClient with this clientId: ".concat(String.valueOf(i))); this.a.put(i, new a(i, huaweiApiClient)); if (this.b) { huaweiApiClient.connect((Activity) null); } } public void stopAutoManage(int i) { a aVar = this.a.get(i); this.a.remove(i); if (aVar != null) { aVar.a(); } } }