package androidx.loader.app; import android.os.Bundle; import android.os.Looper; import android.util.Log; import androidx.collection.SparseArrayCompat; import androidx.core.util.DebugUtils; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelStore; import androidx.loader.app.LoaderManager; import androidx.loader.content.Loader; import java.io.FileDescriptor; import java.io.PrintWriter; import java.lang.reflect.Modifier; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class LoaderManagerImpl extends LoaderManager { static boolean DEBUG = false; static final String TAG = "LoaderManager"; private final LifecycleOwner mLifecycleOwner; private final LoaderViewModel mLoaderViewModel; /* loaded from: classes2.dex */ public static class LoaderInfo extends MutableLiveData implements Loader.OnLoadCompleteListener { private final Bundle mArgs; private final int mId; private LifecycleOwner mLifecycleOwner; private final Loader mLoader; private LoaderObserver mObserver; private Loader mPriorLoader; Loader getLoader() { return this.mLoader; } LoaderInfo(int i, Bundle bundle, Loader loader, Loader loader2) { this.mId = i; this.mArgs = bundle; this.mLoader = loader; this.mPriorLoader = loader2; loader.registerListener(i, this); } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.lifecycle.LiveData public void onActive() { if (LoaderManagerImpl.DEBUG) { Log.v(LoaderManagerImpl.TAG, " Starting: " + this); } this.mLoader.startLoading(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.lifecycle.LiveData public void onInactive() { if (LoaderManagerImpl.DEBUG) { Log.v(LoaderManagerImpl.TAG, " Stopping: " + this); } this.mLoader.stopLoading(); } Loader setCallback(LifecycleOwner lifecycleOwner, LoaderManager.LoaderCallbacks loaderCallbacks) { LoaderObserver loaderObserver = new LoaderObserver<>(this.mLoader, loaderCallbacks); observe(lifecycleOwner, loaderObserver); LoaderObserver loaderObserver2 = this.mObserver; if (loaderObserver2 != null) { removeObserver(loaderObserver2); } this.mLifecycleOwner = lifecycleOwner; this.mObserver = loaderObserver; return this.mLoader; } void markForRedelivery() { LifecycleOwner lifecycleOwner = this.mLifecycleOwner; LoaderObserver loaderObserver = this.mObserver; if (lifecycleOwner == null || loaderObserver == null) { return; } super.removeObserver(loaderObserver); observe(lifecycleOwner, loaderObserver); } boolean isCallbackWaitingForData() { LoaderObserver loaderObserver; return (!hasActiveObservers() || (loaderObserver = this.mObserver) == null || loaderObserver.hasDeliveredData()) ? false : true; } /* JADX WARN: Multi-variable type inference failed */ @Override // androidx.lifecycle.LiveData public void removeObserver(Observer observer) { super.removeObserver(observer); this.mLifecycleOwner = null; this.mObserver = null; } Loader destroy(boolean z) { if (LoaderManagerImpl.DEBUG) { Log.v(LoaderManagerImpl.TAG, " Destroying: " + this); } this.mLoader.cancelLoad(); this.mLoader.abandon(); LoaderObserver loaderObserver = this.mObserver; if (loaderObserver != null) { removeObserver(loaderObserver); if (z) { loaderObserver.reset(); } } this.mLoader.unregisterListener(this); if ((loaderObserver == null || loaderObserver.hasDeliveredData()) && !z) { return this.mLoader; } this.mLoader.reset(); return this.mPriorLoader; } @Override // androidx.loader.content.Loader.OnLoadCompleteListener public void onLoadComplete(Loader loader, D d) { if (LoaderManagerImpl.DEBUG) { Log.v(LoaderManagerImpl.TAG, "onLoadComplete: " + this); } if (Looper.myLooper() == Looper.getMainLooper()) { setValue(d); return; } if (LoaderManagerImpl.DEBUG) { Log.w(LoaderManagerImpl.TAG, "onLoadComplete was incorrectly called on a background thread"); } postValue(d); } @Override // androidx.lifecycle.MutableLiveData, androidx.lifecycle.LiveData public void setValue(D d) { super.setValue(d); Loader loader = this.mPriorLoader; if (loader != null) { loader.reset(); this.mPriorLoader = null; } } public String toString() { StringBuilder sb = new StringBuilder(64); sb.append("LoaderInfo{"); sb.append(Integer.toHexString(System.identityHashCode(this))); sb.append(" #"); sb.append(this.mId); sb.append(" : "); DebugUtils.buildShortClassTag(this.mLoader, sb); sb.append("}}"); return sb.toString(); } public void dump(String str, FileDescriptor fileDescriptor, PrintWriter printWriter, String[] strArr) { printWriter.print(str); printWriter.print("mId="); printWriter.print(this.mId); printWriter.print(" mArgs="); printWriter.println(this.mArgs); printWriter.print(str); printWriter.print("mLoader="); printWriter.println(this.mLoader); this.mLoader.dump(str + " ", fileDescriptor, printWriter, strArr); if (this.mObserver != null) { printWriter.print(str); printWriter.print("mCallbacks="); printWriter.println(this.mObserver); this.mObserver.dump(str + " ", printWriter); } printWriter.print(str); printWriter.print("mData="); printWriter.println(getLoader().dataToString(getValue())); printWriter.print(str); printWriter.print("mStarted="); printWriter.println(hasActiveObservers()); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class LoaderObserver implements Observer { private final LoaderManager.LoaderCallbacks mCallback; private boolean mDeliveredData = false; private final Loader mLoader; boolean hasDeliveredData() { return this.mDeliveredData; } LoaderObserver(Loader loader, LoaderManager.LoaderCallbacks loaderCallbacks) { this.mLoader = loader; this.mCallback = loaderCallbacks; } @Override // androidx.lifecycle.Observer public void onChanged(D d) { if (LoaderManagerImpl.DEBUG) { Log.v(LoaderManagerImpl.TAG, " onLoadFinished in " + this.mLoader + ": " + this.mLoader.dataToString(d)); } this.mCallback.onLoadFinished(this.mLoader, d); this.mDeliveredData = true; } void reset() { if (this.mDeliveredData) { if (LoaderManagerImpl.DEBUG) { Log.v(LoaderManagerImpl.TAG, " Resetting: " + this.mLoader); } this.mCallback.onLoaderReset(this.mLoader); } } public String toString() { return this.mCallback.toString(); } public void dump(String str, PrintWriter printWriter) { printWriter.print(str); printWriter.print("mDeliveredData="); printWriter.println(this.mDeliveredData); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class LoaderViewModel extends ViewModel { private static final ViewModelProvider.Factory FACTORY = new ViewModelProvider.Factory() { // from class: androidx.loader.app.LoaderManagerImpl.LoaderViewModel.1 @Override // androidx.lifecycle.ViewModelProvider.Factory public T create(Class cls) { return new LoaderViewModel(); } }; private SparseArrayCompat mLoaders = new SparseArrayCompat<>(); private boolean mCreatingLoader = false; void finishCreatingLoader() { this.mCreatingLoader = false; } boolean isCreatingLoader() { return this.mCreatingLoader; } void startCreatingLoader() { this.mCreatingLoader = true; } LoaderViewModel() { } static LoaderViewModel getInstance(ViewModelStore viewModelStore) { return (LoaderViewModel) new ViewModelProvider(viewModelStore, FACTORY).get(LoaderViewModel.class); } void putLoader(int i, LoaderInfo loaderInfo) { this.mLoaders.put(i, loaderInfo); } LoaderInfo getLoader(int i) { return this.mLoaders.get(i); } void removeLoader(int i) { this.mLoaders.remove(i); } boolean hasRunningLoaders() { int size = this.mLoaders.size(); for (int i = 0; i < size; i++) { if (this.mLoaders.valueAt(i).isCallbackWaitingForData()) { return true; } } return false; } void markForRedelivery() { int size = this.mLoaders.size(); for (int i = 0; i < size; i++) { this.mLoaders.valueAt(i).markForRedelivery(); } } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.lifecycle.ViewModel public void onCleared() { super.onCleared(); int size = this.mLoaders.size(); for (int i = 0; i < size; i++) { this.mLoaders.valueAt(i).destroy(true); } this.mLoaders.clear(); } public void dump(String str, FileDescriptor fileDescriptor, PrintWriter printWriter, String[] strArr) { if (this.mLoaders.size() > 0) { printWriter.print(str); printWriter.println("Loaders:"); String str2 = str + " "; for (int i = 0; i < this.mLoaders.size(); i++) { LoaderInfo valueAt = this.mLoaders.valueAt(i); printWriter.print(str); printWriter.print(" #"); printWriter.print(this.mLoaders.keyAt(i)); printWriter.print(": "); printWriter.println(valueAt.toString()); valueAt.dump(str2, fileDescriptor, printWriter, strArr); } } } } /* JADX INFO: Access modifiers changed from: package-private */ public LoaderManagerImpl(LifecycleOwner lifecycleOwner, ViewModelStore viewModelStore) { this.mLifecycleOwner = lifecycleOwner; this.mLoaderViewModel = LoaderViewModel.getInstance(viewModelStore); } private Loader createAndInstallLoader(int i, Bundle bundle, LoaderManager.LoaderCallbacks loaderCallbacks, Loader loader) { try { this.mLoaderViewModel.startCreatingLoader(); Loader onCreateLoader = loaderCallbacks.onCreateLoader(i, bundle); if (onCreateLoader == null) { throw new IllegalArgumentException("Object returned from onCreateLoader must not be null"); } if (onCreateLoader.getClass().isMemberClass() && !Modifier.isStatic(onCreateLoader.getClass().getModifiers())) { throw new IllegalArgumentException("Object returned from onCreateLoader must not be a non-static inner member class: " + onCreateLoader); } LoaderInfo loaderInfo = new LoaderInfo(i, bundle, onCreateLoader, loader); if (DEBUG) { Log.v(TAG, " Created new loader " + loaderInfo); } this.mLoaderViewModel.putLoader(i, loaderInfo); this.mLoaderViewModel.finishCreatingLoader(); return loaderInfo.setCallback(this.mLifecycleOwner, loaderCallbacks); } catch (Throwable th) { this.mLoaderViewModel.finishCreatingLoader(); throw th; } } @Override // androidx.loader.app.LoaderManager public Loader initLoader(int i, Bundle bundle, LoaderManager.LoaderCallbacks loaderCallbacks) { if (this.mLoaderViewModel.isCreatingLoader()) { throw new IllegalStateException("Called while creating a loader"); } if (Looper.getMainLooper() != Looper.myLooper()) { throw new IllegalStateException("initLoader must be called on the main thread"); } LoaderInfo loader = this.mLoaderViewModel.getLoader(i); if (DEBUG) { Log.v(TAG, "initLoader in " + this + ": args=" + bundle); } if (loader == null) { return createAndInstallLoader(i, bundle, loaderCallbacks, null); } if (DEBUG) { Log.v(TAG, " Re-using existing loader " + loader); } return loader.setCallback(this.mLifecycleOwner, loaderCallbacks); } @Override // androidx.loader.app.LoaderManager public Loader restartLoader(int i, Bundle bundle, LoaderManager.LoaderCallbacks loaderCallbacks) { if (this.mLoaderViewModel.isCreatingLoader()) { throw new IllegalStateException("Called while creating a loader"); } if (Looper.getMainLooper() != Looper.myLooper()) { throw new IllegalStateException("restartLoader must be called on the main thread"); } if (DEBUG) { Log.v(TAG, "restartLoader in " + this + ": args=" + bundle); } LoaderInfo loader = this.mLoaderViewModel.getLoader(i); return createAndInstallLoader(i, bundle, loaderCallbacks, loader != null ? loader.destroy(false) : null); } @Override // androidx.loader.app.LoaderManager public void destroyLoader(int i) { if (this.mLoaderViewModel.isCreatingLoader()) { throw new IllegalStateException("Called while creating a loader"); } if (Looper.getMainLooper() != Looper.myLooper()) { throw new IllegalStateException("destroyLoader must be called on the main thread"); } if (DEBUG) { Log.v(TAG, "destroyLoader in " + this + " of " + i); } LoaderInfo loader = this.mLoaderViewModel.getLoader(i); if (loader != null) { loader.destroy(true); this.mLoaderViewModel.removeLoader(i); } } @Override // androidx.loader.app.LoaderManager public Loader getLoader(int i) { if (this.mLoaderViewModel.isCreatingLoader()) { throw new IllegalStateException("Called while creating a loader"); } LoaderInfo loader = this.mLoaderViewModel.getLoader(i); if (loader != null) { return loader.getLoader(); } return null; } @Override // androidx.loader.app.LoaderManager public void markForRedelivery() { this.mLoaderViewModel.markForRedelivery(); } public String toString() { StringBuilder sb = new StringBuilder(128); sb.append("LoaderManager{"); sb.append(Integer.toHexString(System.identityHashCode(this))); sb.append(" in "); DebugUtils.buildShortClassTag(this.mLifecycleOwner, sb); sb.append("}}"); return sb.toString(); } @Override // androidx.loader.app.LoaderManager @Deprecated public void dump(String str, FileDescriptor fileDescriptor, PrintWriter printWriter, String[] strArr) { this.mLoaderViewModel.dump(str, fileDescriptor, printWriter, strArr); } @Override // androidx.loader.app.LoaderManager public boolean hasRunningLoaders() { return this.mLoaderViewModel.hasRunningLoaders(); } }