Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 274d2fc1 authored by Hai Zhang's avatar Hai Zhang
Browse files

Fix crash when role data isn't loaded after restoring state.

In the case where we have a saved state but view model is destroyed,
ListView.getCheckedItemPosition() may return a valid position when we
haven't got the item loaded yet. This change ensures we don't enable
the positive button before role data is loaded, and updates the UI
again when we have the data available.

Fixes: 141367460
Test: presubmit
Change-Id: Ia24cb6396f9038cac4f9bd490705ebca2c4b4a4a
(cherry picked from commit 43d3eec9)
parent 1189512b
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -226,7 +226,7 @@ public class RequestRoleFragment extends DialogFragment {
        // Postponed to onStart() so that the list view in dialog is created.
        mViewModel = ViewModelProviders.of(this, new RequestRoleViewModel.Factory(mRole,
                requireActivity().getApplication())).get(RequestRoleViewModel.class);
        mViewModel.getRoleLiveData().observe(this, mAdapter::replace);
        mViewModel.getRoleLiveData().observe(this, this::onRoleDataChanged);
        mViewModel.getManageRoleHolderStateLiveData().observe(this,
                this::onManageRoleHolderStateChanged);
    }
@@ -262,6 +262,12 @@ public class RequestRoleFragment extends DialogFragment {
        setDeniedOnceAndFinish();
    }

    private void onRoleDataChanged(
            @NonNull List<Pair<ApplicationInfo, Boolean>> qualifyingApplications) {
        mAdapter.replace(qualifyingApplications);
        updateUi();
    }

    private void onItemClicked(int position) {
        mAdapter.onItemClicked(position);
        updateUi();
@@ -344,8 +350,9 @@ public class RequestRoleFragment extends DialogFragment {
        boolean dontAskAgain = mDontAskAgainCheck != null && mDontAskAgainCheck.isChecked();
        mAdapter.setDontAskAgain(dontAskAgain);
        AlertDialog dialog = getDialog();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled && (dontAskAgain
                || !mAdapter.isHolderApplicationChecked()));
        boolean hasRoleData = mViewModel.getRoleLiveData().getValue() != null;
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled && hasRoleData
                && (dontAskAgain || !mAdapter.isHolderApplicationChecked()));
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(enabled);
    }