Search in sources :

Example 1 with OnBackPressedDispatcherOwner

use of androidx.activity.OnBackPressedDispatcherOwner in project sesl by OneUIProject.

the class FragmentManager method attachController.

@SuppressWarnings("deprecation")
@SuppressLint("SyntheticAccessor")
void attachController(@NonNull FragmentHostCallback<?> host, @NonNull FragmentContainer container, @Nullable final Fragment parent) {
    if (mHost != null)
        throw new IllegalStateException("Already attached");
    mHost = host;
    mContainer = container;
    mParent = parent;
    // backward compatibility with the deprecated onAttachFragment() APIs
    if (mParent != null) {
        addFragmentOnAttachListener(new FragmentOnAttachListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onAttachFragment(@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment) {
                parent.onAttachFragment(fragment);
            }
        });
    } else if (host instanceof FragmentOnAttachListener) {
        addFragmentOnAttachListener((FragmentOnAttachListener) host);
    }
    if (mParent != null) {
        // Since the callback depends on us being the primary navigation fragment,
        // update our callback now that we have a parent so that we have the correct
        // state by default
        updateOnBackPressedCallbackEnabled();
    }
    // Set up the OnBackPressedCallback
    if (host instanceof OnBackPressedDispatcherOwner) {
        OnBackPressedDispatcherOwner dispatcherOwner = ((OnBackPressedDispatcherOwner) host);
        mOnBackPressedDispatcher = dispatcherOwner.getOnBackPressedDispatcher();
        LifecycleOwner owner = parent != null ? parent : dispatcherOwner;
        mOnBackPressedDispatcher.addCallback(owner, mOnBackPressedCallback);
    }
    // Get the FragmentManagerViewModel
    if (parent != null) {
        mNonConfig = parent.mFragmentManager.getChildNonConfig(parent);
    } else if (host instanceof ViewModelStoreOwner) {
        ViewModelStore viewModelStore = ((ViewModelStoreOwner) host).getViewModelStore();
        mNonConfig = FragmentManagerViewModel.getInstance(viewModelStore);
    } else {
        mNonConfig = new FragmentManagerViewModel(false);
    }
    // Ensure that the state is in sync with FragmentManager
    mNonConfig.setIsStateSaved(isStateSaved());
    mFragmentStore.setNonConfig(mNonConfig);
    if (mHost instanceof ActivityResultRegistryOwner) {
        ActivityResultRegistry registry = ((ActivityResultRegistryOwner) mHost).getActivityResultRegistry();
        String parentId = parent != null ? parent.mWho + ":" : "";
        String keyPrefix = "FragmentManager:" + parentId;
        mStartActivityForResult = registry.register(keyPrefix + "StartActivityForResult", new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {

            @Override
            public void onActivityResult(ActivityResult result) {
                LaunchedFragmentInfo requestInfo = mLaunchedFragments.pollFirst();
                if (requestInfo == null) {
                    Log.w(TAG, "No Activities were started for result for " + this);
                    return;
                }
                String fragmentWho = requestInfo.mWho;
                int requestCode = requestInfo.mRequestCode;
                Fragment fragment = mFragmentStore.findFragmentByWho(fragmentWho);
                // result call
                if (fragment == null) {
                    Log.w(TAG, "Activity result delivered for unknown Fragment " + fragmentWho);
                    return;
                }
                fragment.onActivityResult(requestCode, result.getResultCode(), result.getData());
            }
        });
        mStartIntentSenderForResult = registry.register(keyPrefix + "StartIntentSenderForResult", new FragmentManager.FragmentIntentSenderContract(), new ActivityResultCallback<ActivityResult>() {

            @Override
            public void onActivityResult(ActivityResult result) {
                LaunchedFragmentInfo requestInfo = mLaunchedFragments.pollFirst();
                if (requestInfo == null) {
                    Log.w(TAG, "No IntentSenders were started for " + this);
                    return;
                }
                String fragmentWho = requestInfo.mWho;
                int requestCode = requestInfo.mRequestCode;
                Fragment fragment = mFragmentStore.findFragmentByWho(fragmentWho);
                // result call
                if (fragment == null) {
                    Log.w(TAG, "Intent Sender result delivered for unknown Fragment " + fragmentWho);
                    return;
                }
                fragment.onActivityResult(requestCode, result.getResultCode(), result.getData());
            }
        });
        mRequestPermissions = registry.register(keyPrefix + "RequestPermissions", new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {

            @SuppressLint("SyntheticAccessor")
            @Override
            public void onActivityResult(Map<String, Boolean> result) {
                String[] permissions = result.keySet().toArray(new String[0]);
                ArrayList<Boolean> resultValues = new ArrayList<>(result.values());
                int[] grantResults = new int[resultValues.size()];
                for (int i = 0; i < resultValues.size(); i++) {
                    grantResults[i] = resultValues.get(i) ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED;
                }
                LaunchedFragmentInfo requestInfo = mLaunchedFragments.pollFirst();
                if (requestInfo == null) {
                    Log.w(TAG, "No permissions were requested for " + this);
                    return;
                }
                String fragmentWho = requestInfo.mWho;
                int requestCode = requestInfo.mRequestCode;
                Fragment fragment = mFragmentStore.findFragmentByWho(fragmentWho);
                // permissions call
                if (fragment == null) {
                    Log.w(TAG, "Permission request result delivered for unknown " + "Fragment " + fragmentWho);
                    return;
                }
                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        });
    }
}
Also used : OnBackPressedDispatcherOwner(androidx.activity.OnBackPressedDispatcherOwner) ActivityResultRegistry(androidx.activity.result.ActivityResultRegistry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ViewModelStore(androidx.lifecycle.ViewModelStore) ViewModelStoreOwner(androidx.lifecycle.ViewModelStoreOwner) ActivityResult(androidx.activity.result.ActivityResult) SuppressLint(android.annotation.SuppressLint) ActivityResultRegistryOwner(androidx.activity.result.ActivityResultRegistryOwner) Map(java.util.Map) HashMap(java.util.HashMap) LifecycleOwner(androidx.lifecycle.LifecycleOwner) ActivityResultCallback(androidx.activity.result.ActivityResultCallback) SuppressLint(android.annotation.SuppressLint)

Example 2 with OnBackPressedDispatcherOwner

use of androidx.activity.OnBackPressedDispatcherOwner in project GreenHouse by utsanjan.

the class FragmentManagerImpl method attachController.

/* JADX WARN: Multi-variable type inference failed */
public void attachController(FragmentHostCallback host, FragmentContainer container, Fragment parent) {
    if (this.mHost == null) {
        this.mHost = host;
        this.mContainer = container;
        this.mParent = parent;
        if (parent != null) {
            updateOnBackPressedCallbackEnabled();
        }
        if (host instanceof OnBackPressedDispatcherOwner) {
            OnBackPressedDispatcherOwner dispatcherOwner = (OnBackPressedDispatcherOwner) host;
            this.mOnBackPressedDispatcher = dispatcherOwner.getOnBackPressedDispatcher();
            LifecycleOwner owner = parent != null ? parent : dispatcherOwner;
            this.mOnBackPressedDispatcher.addCallback(owner, this.mOnBackPressedCallback);
        }
        if (parent != null) {
            this.mNonConfig = parent.mFragmentManager.getChildNonConfig(parent);
        } else if (host instanceof ViewModelStoreOwner) {
            ViewModelStore viewModelStore = ((ViewModelStoreOwner) host).getViewModelStore();
            this.mNonConfig = FragmentManagerViewModel.getInstance(viewModelStore);
        } else {
            this.mNonConfig = new FragmentManagerViewModel(false);
        }
    } else {
        throw new IllegalStateException("Already attached");
    }
}
Also used : ViewModelStoreOwner(androidx.lifecycle.ViewModelStoreOwner) OnBackPressedDispatcherOwner(androidx.activity.OnBackPressedDispatcherOwner) ViewModelStore(androidx.lifecycle.ViewModelStore) LifecycleOwner(androidx.lifecycle.LifecycleOwner)

Example 3 with OnBackPressedDispatcherOwner

use of androidx.activity.OnBackPressedDispatcherOwner in project sesl-appcompat by OneUIProject.

the class FragmentManager method attachController.

@SuppressWarnings("deprecation")
@SuppressLint("SyntheticAccessor")
void attachController(@NonNull FragmentHostCallback<?> host, @NonNull FragmentContainer container, @Nullable final Fragment parent) {
    if (mHost != null)
        throw new IllegalStateException("Already attached");
    mHost = host;
    mContainer = container;
    mParent = parent;
    // backward compatibility with the deprecated onAttachFragment() APIs
    if (mParent != null) {
        addFragmentOnAttachListener(new FragmentOnAttachListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onAttachFragment(@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment) {
                parent.onAttachFragment(fragment);
            }
        });
    } else if (host instanceof FragmentOnAttachListener) {
        addFragmentOnAttachListener((FragmentOnAttachListener) host);
    }
    if (mParent != null) {
        // Since the callback depends on us being the primary navigation fragment,
        // update our callback now that we have a parent so that we have the correct
        // state by default
        updateOnBackPressedCallbackEnabled();
    }
    // Set up the OnBackPressedCallback
    if (host instanceof OnBackPressedDispatcherOwner) {
        OnBackPressedDispatcherOwner dispatcherOwner = ((OnBackPressedDispatcherOwner) host);
        mOnBackPressedDispatcher = dispatcherOwner.getOnBackPressedDispatcher();
        LifecycleOwner owner = parent != null ? parent : dispatcherOwner;
        mOnBackPressedDispatcher.addCallback(owner, mOnBackPressedCallback);
    }
    // Get the FragmentManagerViewModel
    if (parent != null) {
        mNonConfig = parent.mFragmentManager.getChildNonConfig(parent);
    } else if (host instanceof ViewModelStoreOwner) {
        ViewModelStore viewModelStore = ((ViewModelStoreOwner) host).getViewModelStore();
        mNonConfig = FragmentManagerViewModel.getInstance(viewModelStore);
    } else {
        mNonConfig = new FragmentManagerViewModel(false);
    }
    // Ensure that the state is in sync with FragmentManager
    mNonConfig.setIsStateSaved(isStateSaved());
    mFragmentStore.setNonConfig(mNonConfig);
    if (mHost instanceof ActivityResultRegistryOwner) {
        ActivityResultRegistry registry = ((ActivityResultRegistryOwner) mHost).getActivityResultRegistry();
        String parentId = parent != null ? parent.mWho + ":" : "";
        String keyPrefix = "FragmentManager:" + parentId;
        mStartActivityForResult = registry.register(keyPrefix + "StartActivityForResult", new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {

            @Override
            public void onActivityResult(ActivityResult result) {
                LaunchedFragmentInfo requestInfo = mLaunchedFragments.pollFirst();
                if (requestInfo == null) {
                    Log.w(TAG, "No Activities were started for result for " + this);
                    return;
                }
                String fragmentWho = requestInfo.mWho;
                int requestCode = requestInfo.mRequestCode;
                Fragment fragment = mFragmentStore.findFragmentByWho(fragmentWho);
                // result call
                if (fragment == null) {
                    Log.w(TAG, "Activity result delivered for unknown Fragment " + fragmentWho);
                    return;
                }
                fragment.onActivityResult(requestCode, result.getResultCode(), result.getData());
            }
        });
        mStartIntentSenderForResult = registry.register(keyPrefix + "StartIntentSenderForResult", new FragmentManager.FragmentIntentSenderContract(), new ActivityResultCallback<ActivityResult>() {

            @Override
            public void onActivityResult(ActivityResult result) {
                LaunchedFragmentInfo requestInfo = mLaunchedFragments.pollFirst();
                if (requestInfo == null) {
                    Log.w(TAG, "No IntentSenders were started for " + this);
                    return;
                }
                String fragmentWho = requestInfo.mWho;
                int requestCode = requestInfo.mRequestCode;
                Fragment fragment = mFragmentStore.findFragmentByWho(fragmentWho);
                // result call
                if (fragment == null) {
                    Log.w(TAG, "Intent Sender result delivered for unknown Fragment " + fragmentWho);
                    return;
                }
                fragment.onActivityResult(requestCode, result.getResultCode(), result.getData());
            }
        });
        mRequestPermissions = registry.register(keyPrefix + "RequestPermissions", new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {

            @SuppressLint("SyntheticAccessor")
            @Override
            public void onActivityResult(Map<String, Boolean> result) {
                String[] permissions = result.keySet().toArray(new String[0]);
                ArrayList<Boolean> resultValues = new ArrayList<>(result.values());
                int[] grantResults = new int[resultValues.size()];
                for (int i = 0; i < resultValues.size(); i++) {
                    grantResults[i] = resultValues.get(i) ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED;
                }
                LaunchedFragmentInfo requestInfo = mLaunchedFragments.pollFirst();
                if (requestInfo == null) {
                    Log.w(TAG, "No permissions were requested for " + this);
                    return;
                }
                String fragmentWho = requestInfo.mWho;
                int requestCode = requestInfo.mRequestCode;
                Fragment fragment = mFragmentStore.findFragmentByWho(fragmentWho);
                // permissions call
                if (fragment == null) {
                    Log.w(TAG, "Permission request result delivered for unknown " + "Fragment " + fragmentWho);
                    return;
                }
                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        });
    }
}
Also used : OnBackPressedDispatcherOwner(androidx.activity.OnBackPressedDispatcherOwner) ActivityResultRegistry(androidx.activity.result.ActivityResultRegistry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ViewModelStore(androidx.lifecycle.ViewModelStore) ViewModelStoreOwner(androidx.lifecycle.ViewModelStoreOwner) ActivityResult(androidx.activity.result.ActivityResult) SuppressLint(android.annotation.SuppressLint) ActivityResultRegistryOwner(androidx.activity.result.ActivityResultRegistryOwner) Map(java.util.Map) HashMap(java.util.HashMap) LifecycleOwner(androidx.lifecycle.LifecycleOwner) ActivityResultCallback(androidx.activity.result.ActivityResultCallback) SuppressLint(android.annotation.SuppressLint)

Aggregations

OnBackPressedDispatcherOwner (androidx.activity.OnBackPressedDispatcherOwner)3 LifecycleOwner (androidx.lifecycle.LifecycleOwner)3 ViewModelStore (androidx.lifecycle.ViewModelStore)3 ViewModelStoreOwner (androidx.lifecycle.ViewModelStoreOwner)3 SuppressLint (android.annotation.SuppressLint)2 ActivityResult (androidx.activity.result.ActivityResult)2 ActivityResultCallback (androidx.activity.result.ActivityResultCallback)2 ActivityResultRegistry (androidx.activity.result.ActivityResultRegistry)2 ActivityResultRegistryOwner (androidx.activity.result.ActivityResultRegistryOwner)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2