285 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			285 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package androidx.activity.result;
 | |
| 
 | |
| import android.content.Intent;
 | |
| import android.os.Bundle;
 | |
| import android.util.Log;
 | |
| import androidx.activity.result.contract.ActivityResultContract;
 | |
| import androidx.core.app.ActivityOptionsCompat;
 | |
| import androidx.lifecycle.Lifecycle;
 | |
| import androidx.lifecycle.LifecycleEventObserver;
 | |
| import androidx.lifecycle.LifecycleOwner;
 | |
| import java.util.ArrayList;
 | |
| import java.util.HashMap;
 | |
| import java.util.Iterator;
 | |
| import java.util.Map;
 | |
| import kotlin.random.Random;
 | |
| 
 | |
| /* loaded from: classes.dex */
 | |
| public abstract class ActivityResultRegistry {
 | |
|     private static final int INITIAL_REQUEST_CODE_VALUE = 65536;
 | |
|     private static final String KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS = "KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS";
 | |
|     private static final String KEY_COMPONENT_ACTIVITY_PENDING_RESULTS = "KEY_COMPONENT_ACTIVITY_PENDING_RESULT";
 | |
|     private static final String KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS = "KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS";
 | |
|     private static final String KEY_COMPONENT_ACTIVITY_REGISTERED_RCS = "KEY_COMPONENT_ACTIVITY_REGISTERED_RCS";
 | |
|     private static final String LOG_TAG = "ActivityResultRegistry";
 | |
|     private final Map<Integer, String> mRcToKey = new HashMap();
 | |
|     final Map<String, Integer> mKeyToRc = new HashMap();
 | |
|     private final Map<String, LifecycleContainer> mKeyToLifecycleContainers = new HashMap();
 | |
|     ArrayList<String> mLaunchedKeys = new ArrayList<>();
 | |
|     final transient Map<String, CallbackAndContract<?>> mKeyToCallback = new HashMap();
 | |
|     final Map<String, Object> mParsedPendingResults = new HashMap();
 | |
|     final Bundle mPendingResults = new Bundle();
 | |
| 
 | |
|     public abstract <I, O> void onLaunch(int i, ActivityResultContract<I, O> activityResultContract, I i2, ActivityOptionsCompat activityOptionsCompat);
 | |
| 
 | |
|     public final <I, O> ActivityResultLauncher<I> register(final String str, LifecycleOwner lifecycleOwner, final ActivityResultContract<I, O> activityResultContract, final ActivityResultCallback<O> activityResultCallback) {
 | |
|         Lifecycle lifecycle = lifecycleOwner.getLifecycle();
 | |
|         if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
 | |
|             throw new IllegalStateException("LifecycleOwner " + lifecycleOwner + " is attempting to register while current state is " + lifecycle.getCurrentState() + ". LifecycleOwners must call register before they are STARTED.");
 | |
|         }
 | |
|         registerKey(str);
 | |
|         LifecycleContainer lifecycleContainer = this.mKeyToLifecycleContainers.get(str);
 | |
|         if (lifecycleContainer == null) {
 | |
|             lifecycleContainer = new LifecycleContainer(lifecycle);
 | |
|         }
 | |
|         lifecycleContainer.addObserver(new LifecycleEventObserver() { // from class: androidx.activity.result.ActivityResultRegistry.1
 | |
|             @Override // androidx.lifecycle.LifecycleEventObserver
 | |
|             public void onStateChanged(LifecycleOwner lifecycleOwner2, Lifecycle.Event event) {
 | |
|                 if (Lifecycle.Event.ON_START.equals(event)) {
 | |
|                     ActivityResultRegistry.this.mKeyToCallback.put(str, new CallbackAndContract<>(activityResultCallback, activityResultContract));
 | |
|                     if (ActivityResultRegistry.this.mParsedPendingResults.containsKey(str)) {
 | |
|                         Object obj = ActivityResultRegistry.this.mParsedPendingResults.get(str);
 | |
|                         ActivityResultRegistry.this.mParsedPendingResults.remove(str);
 | |
|                         activityResultCallback.onActivityResult(obj);
 | |
|                     }
 | |
|                     ActivityResult activityResult = (ActivityResult) ActivityResultRegistry.this.mPendingResults.getParcelable(str);
 | |
|                     if (activityResult != null) {
 | |
|                         ActivityResultRegistry.this.mPendingResults.remove(str);
 | |
|                         activityResultCallback.onActivityResult(activityResultContract.parseResult(activityResult.getResultCode(), activityResult.getData()));
 | |
|                         return;
 | |
|                     }
 | |
|                     return;
 | |
|                 }
 | |
|                 if (Lifecycle.Event.ON_STOP.equals(event)) {
 | |
|                     ActivityResultRegistry.this.mKeyToCallback.remove(str);
 | |
|                 } else if (Lifecycle.Event.ON_DESTROY.equals(event)) {
 | |
|                     ActivityResultRegistry.this.unregister(str);
 | |
|                 }
 | |
|             }
 | |
|         });
 | |
|         this.mKeyToLifecycleContainers.put(str, lifecycleContainer);
 | |
|         return new ActivityResultLauncher<I>() { // from class: androidx.activity.result.ActivityResultRegistry.2
 | |
|             @Override // androidx.activity.result.ActivityResultLauncher
 | |
|             public ActivityResultContract<I, ?> getContract() {
 | |
|                 return activityResultContract;
 | |
|             }
 | |
| 
 | |
|             @Override // androidx.activity.result.ActivityResultLauncher
 | |
|             public void launch(I i, ActivityOptionsCompat activityOptionsCompat) {
 | |
|                 Integer num = ActivityResultRegistry.this.mKeyToRc.get(str);
 | |
|                 if (num == null) {
 | |
|                     throw new IllegalStateException("Attempting to launch an unregistered ActivityResultLauncher with contract " + activityResultContract + " and input " + i + ". You must ensure the ActivityResultLauncher is registered before calling launch().");
 | |
|                 }
 | |
|                 ActivityResultRegistry.this.mLaunchedKeys.add(str);
 | |
|                 try {
 | |
|                     ActivityResultRegistry.this.onLaunch(num.intValue(), activityResultContract, i, activityOptionsCompat);
 | |
|                 } catch (Exception e) {
 | |
|                     ActivityResultRegistry.this.mLaunchedKeys.remove(str);
 | |
|                     throw e;
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             @Override // androidx.activity.result.ActivityResultLauncher
 | |
|             public void unregister() {
 | |
|                 ActivityResultRegistry.this.unregister(str);
 | |
|             }
 | |
|         };
 | |
|     }
 | |
| 
 | |
|     /* JADX WARN: Multi-variable type inference failed */
 | |
|     public final <I, O> ActivityResultLauncher<I> register(final String str, final ActivityResultContract<I, O> activityResultContract, ActivityResultCallback<O> activityResultCallback) {
 | |
|         registerKey(str);
 | |
|         this.mKeyToCallback.put(str, new CallbackAndContract<>(activityResultCallback, activityResultContract));
 | |
|         if (this.mParsedPendingResults.containsKey(str)) {
 | |
|             Object obj = this.mParsedPendingResults.get(str);
 | |
|             this.mParsedPendingResults.remove(str);
 | |
|             activityResultCallback.onActivityResult(obj);
 | |
|         }
 | |
|         ActivityResult activityResult = (ActivityResult) this.mPendingResults.getParcelable(str);
 | |
|         if (activityResult != null) {
 | |
|             this.mPendingResults.remove(str);
 | |
|             activityResultCallback.onActivityResult(activityResultContract.parseResult(activityResult.getResultCode(), activityResult.getData()));
 | |
|         }
 | |
|         return new ActivityResultLauncher<I>() { // from class: androidx.activity.result.ActivityResultRegistry.3
 | |
|             @Override // androidx.activity.result.ActivityResultLauncher
 | |
|             public ActivityResultContract<I, ?> getContract() {
 | |
|                 return activityResultContract;
 | |
|             }
 | |
| 
 | |
|             @Override // androidx.activity.result.ActivityResultLauncher
 | |
|             public void launch(I i, ActivityOptionsCompat activityOptionsCompat) {
 | |
|                 Integer num = ActivityResultRegistry.this.mKeyToRc.get(str);
 | |
|                 if (num == null) {
 | |
|                     throw new IllegalStateException("Attempting to launch an unregistered ActivityResultLauncher with contract " + activityResultContract + " and input " + i + ". You must ensure the ActivityResultLauncher is registered before calling launch().");
 | |
|                 }
 | |
|                 ActivityResultRegistry.this.mLaunchedKeys.add(str);
 | |
|                 try {
 | |
|                     ActivityResultRegistry.this.onLaunch(num.intValue(), activityResultContract, i, activityOptionsCompat);
 | |
|                 } catch (Exception e) {
 | |
|                     ActivityResultRegistry.this.mLaunchedKeys.remove(str);
 | |
|                     throw e;
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             @Override // androidx.activity.result.ActivityResultLauncher
 | |
|             public void unregister() {
 | |
|                 ActivityResultRegistry.this.unregister(str);
 | |
|             }
 | |
|         };
 | |
|     }
 | |
| 
 | |
|     final void unregister(String str) {
 | |
|         Integer remove;
 | |
|         if (!this.mLaunchedKeys.contains(str) && (remove = this.mKeyToRc.remove(str)) != null) {
 | |
|             this.mRcToKey.remove(remove);
 | |
|         }
 | |
|         this.mKeyToCallback.remove(str);
 | |
|         if (this.mParsedPendingResults.containsKey(str)) {
 | |
|             Log.w(LOG_TAG, "Dropping pending result for request " + str + ": " + this.mParsedPendingResults.get(str));
 | |
|             this.mParsedPendingResults.remove(str);
 | |
|         }
 | |
|         if (this.mPendingResults.containsKey(str)) {
 | |
|             Log.w(LOG_TAG, "Dropping pending result for request " + str + ": " + this.mPendingResults.getParcelable(str));
 | |
|             this.mPendingResults.remove(str);
 | |
|         }
 | |
|         LifecycleContainer lifecycleContainer = this.mKeyToLifecycleContainers.get(str);
 | |
|         if (lifecycleContainer != null) {
 | |
|             lifecycleContainer.clearObservers();
 | |
|             this.mKeyToLifecycleContainers.remove(str);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public final void onSaveInstanceState(Bundle bundle) {
 | |
|         bundle.putIntegerArrayList(KEY_COMPONENT_ACTIVITY_REGISTERED_RCS, new ArrayList<>(this.mKeyToRc.values()));
 | |
|         bundle.putStringArrayList(KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS, new ArrayList<>(this.mKeyToRc.keySet()));
 | |
|         bundle.putStringArrayList(KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS, new ArrayList<>(this.mLaunchedKeys));
 | |
|         bundle.putBundle(KEY_COMPONENT_ACTIVITY_PENDING_RESULTS, (Bundle) this.mPendingResults.clone());
 | |
|     }
 | |
| 
 | |
|     public final void onRestoreInstanceState(Bundle bundle) {
 | |
|         if (bundle == null) {
 | |
|             return;
 | |
|         }
 | |
|         ArrayList<Integer> integerArrayList = bundle.getIntegerArrayList(KEY_COMPONENT_ACTIVITY_REGISTERED_RCS);
 | |
|         ArrayList<String> stringArrayList = bundle.getStringArrayList(KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS);
 | |
|         if (stringArrayList == null || integerArrayList == null) {
 | |
|             return;
 | |
|         }
 | |
|         this.mLaunchedKeys = bundle.getStringArrayList(KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS);
 | |
|         this.mPendingResults.putAll(bundle.getBundle(KEY_COMPONENT_ACTIVITY_PENDING_RESULTS));
 | |
|         for (int i = 0; i < stringArrayList.size(); i++) {
 | |
|             String str = stringArrayList.get(i);
 | |
|             if (this.mKeyToRc.containsKey(str)) {
 | |
|                 Integer remove = this.mKeyToRc.remove(str);
 | |
|                 if (!this.mPendingResults.containsKey(str)) {
 | |
|                     this.mRcToKey.remove(remove);
 | |
|                 }
 | |
|             }
 | |
|             bindRcKey(integerArrayList.get(i).intValue(), stringArrayList.get(i));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public final boolean dispatchResult(int i, int i2, Intent intent) {
 | |
|         String str = this.mRcToKey.get(Integer.valueOf(i));
 | |
|         if (str == null) {
 | |
|             return false;
 | |
|         }
 | |
|         doDispatch(str, i2, intent, this.mKeyToCallback.get(str));
 | |
|         return true;
 | |
|     }
 | |
| 
 | |
|     public final <O> boolean dispatchResult(int i, O o) {
 | |
|         String str = this.mRcToKey.get(Integer.valueOf(i));
 | |
|         if (str == null) {
 | |
|             return false;
 | |
|         }
 | |
|         CallbackAndContract<?> callbackAndContract = this.mKeyToCallback.get(str);
 | |
|         if (callbackAndContract == null || callbackAndContract.mCallback == null) {
 | |
|             this.mPendingResults.remove(str);
 | |
|             this.mParsedPendingResults.put(str, o);
 | |
|             return true;
 | |
|         }
 | |
|         ActivityResultCallback<?> activityResultCallback = callbackAndContract.mCallback;
 | |
|         if (!this.mLaunchedKeys.remove(str)) {
 | |
|             return true;
 | |
|         }
 | |
|         activityResultCallback.onActivityResult(o);
 | |
|         return true;
 | |
|     }
 | |
| 
 | |
|     private <O> void doDispatch(String str, int i, Intent intent, CallbackAndContract<O> callbackAndContract) {
 | |
|         if (callbackAndContract != null && callbackAndContract.mCallback != null && this.mLaunchedKeys.contains(str)) {
 | |
|             callbackAndContract.mCallback.onActivityResult(callbackAndContract.mContract.parseResult(i, intent));
 | |
|             this.mLaunchedKeys.remove(str);
 | |
|         } else {
 | |
|             this.mParsedPendingResults.remove(str);
 | |
|             this.mPendingResults.putParcelable(str, new ActivityResult(i, intent));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void registerKey(String str) {
 | |
|         if (this.mKeyToRc.get(str) != null) {
 | |
|             return;
 | |
|         }
 | |
|         bindRcKey(generateRandomNumber(), str);
 | |
|     }
 | |
| 
 | |
|     private int generateRandomNumber() {
 | |
|         int nextInt = Random.INSTANCE.nextInt(2147418112);
 | |
|         while (true) {
 | |
|             int i = nextInt + 65536;
 | |
|             if (!this.mRcToKey.containsKey(Integer.valueOf(i))) {
 | |
|                 return i;
 | |
|             }
 | |
|             nextInt = Random.INSTANCE.nextInt(2147418112);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void bindRcKey(int i, String str) {
 | |
|         this.mRcToKey.put(Integer.valueOf(i), str);
 | |
|         this.mKeyToRc.put(str, Integer.valueOf(i));
 | |
|     }
 | |
| 
 | |
|     private static class CallbackAndContract<O> {
 | |
|         final ActivityResultCallback<O> mCallback;
 | |
|         final ActivityResultContract<?, O> mContract;
 | |
| 
 | |
|         CallbackAndContract(ActivityResultCallback<O> activityResultCallback, ActivityResultContract<?, O> activityResultContract) {
 | |
|             this.mCallback = activityResultCallback;
 | |
|             this.mContract = activityResultContract;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private static class LifecycleContainer {
 | |
|         final Lifecycle mLifecycle;
 | |
|         private final ArrayList<LifecycleEventObserver> mObservers = new ArrayList<>();
 | |
| 
 | |
|         LifecycleContainer(Lifecycle lifecycle) {
 | |
|             this.mLifecycle = lifecycle;
 | |
|         }
 | |
| 
 | |
|         void addObserver(LifecycleEventObserver lifecycleEventObserver) {
 | |
|             this.mLifecycle.addObserver(lifecycleEventObserver);
 | |
|             this.mObservers.add(lifecycleEventObserver);
 | |
|         }
 | |
| 
 | |
|         void clearObservers() {
 | |
|             Iterator<LifecycleEventObserver> it = this.mObservers.iterator();
 | |
|             while (it.hasNext()) {
 | |
|                 this.mLifecycle.removeObserver(it.next());
 | |
|             }
 | |
|             this.mObservers.clear();
 | |
|         }
 | |
|     }
 | |
| }
 |