Add dialog for enabling a provider in settings

Test: manual
Bug: 277923170
Merged-In: I404444115f178faf5545dec9686096fcdfdf73de
Change-Id: I404444115f178faf5545dec9686096fcdfdf73de
This commit is contained in:
Becca Hughes
2023-04-12 19:03:17 +00:00
parent e85d588904
commit b69351b06d
3 changed files with 42 additions and 36 deletions

View File

@@ -153,8 +153,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
}
final String action = launchIntent.getAction();
final boolean isCredProviderAction =
TextUtils.equals(action, PRIMARY_INTENT);
final boolean isCredProviderAction = TextUtils.equals(action, PRIMARY_INTENT);
final boolean isExistingAction = TextUtils.equals(action, ALTERNATE_INTENT);
final boolean isValid = isCredProviderAction || isExistingAction;
@@ -226,7 +225,8 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
}
NewProviderConfirmationDialogFragment fragment =
newNewProviderConfirmationDialogFragment(serviceInfo.packageName, appName);
newNewProviderConfirmationDialogFragment(
serviceInfo.packageName, appName, /* setActivityResult= */ true);
if (fragment == null || mFragmentManager == null) {
return;
}
@@ -482,20 +482,17 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
boolean isChecked = pref.isChecked();
if (isChecked) {
// Show the error if too many enabled.
if (!togglePackageNameEnabled(packageName)) {
final DialogFragment fragment = newErrorDialogFragment();
if (fragment == null || mFragmentManager == null) {
return true;
}
fragment.show(mFragmentManager, ErrorDialogFragment.TAG);
// The user set the check to true so we need to set it back.
pref.setChecked(false);
// Since we are enabling it we should confirm the user decision with a
// dialog box.
NewProviderConfirmationDialogFragment fragment =
newNewProviderConfirmationDialogFragment(
packageName, title, /* setActivityResult= */ false);
if (fragment == null || mFragmentManager == null) {
return true;
}
fragment.show(mFragmentManager, NewProviderConfirmationDialogFragment.TAG);
return true;
} else {
// If we are disabling the last enabled provider then show a warning.
@@ -546,12 +543,15 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
/** Create the new provider confirmation dialog. */
private @Nullable NewProviderConfirmationDialogFragment
newNewProviderConfirmationDialogFragment(
@NonNull String packageName, @NonNull CharSequence appName) {
@NonNull String packageName,
@NonNull CharSequence appName,
boolean setActivityResult) {
DialogHost host =
new DialogHost() {
@Override
public void onDialogClick(int whichButton) {
completeEnableProviderDialogBox(whichButton, packageName);
completeEnableProviderDialogBox(
whichButton, packageName, setActivityResult);
}
};
@@ -559,17 +559,19 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
}
@VisibleForTesting
void completeEnableProviderDialogBox(int whichButton, String packageName) {
void completeEnableProviderDialogBox(
int whichButton, String packageName, boolean setActivityResult) {
int activityResult = -1;
if (whichButton == DialogInterface.BUTTON_POSITIVE) {
if (togglePackageNameEnabled(packageName)) {
// Enable all prefs.
if (mPrefs.containsKey(packageName)) {
mPrefs.get(packageName).setChecked(true);
}
setActivityResult(Activity.RESULT_OK);
activityResult = Activity.RESULT_OK;
} else {
// There are too many providers so set the result as cancelled.
setActivityResult(Activity.RESULT_CANCELED);
activityResult = Activity.RESULT_CANCELED;
// Show the error if too many enabled.
final DialogFragment fragment = newErrorDialogFragment();
@@ -582,7 +584,13 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
}
} else {
// The user clicked the cancel button so send that result back.
setActivityResult(Activity.RESULT_CANCELED);
activityResult = Activity.RESULT_CANCELED;
}
// If the dialog is being shown because of the intent we should
// return a result.
if (activityResult == -1 || !setActivityResult) {
setActivityResult(activityResult);
}
}
@@ -735,16 +743,17 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle bundle = getArguments();
final Context context = getContext();
final CharSequence appName =
bundle.getCharSequence(CredentialManagerDialogFragment.APP_NAME_KEY);
final String title =
context.getString(
R.string.credman_enable_confirmation_message_title,
bundle.getCharSequence(CredentialManagerDialogFragment.APP_NAME_KEY));
context.getString(R.string.credman_enable_confirmation_message_title, appName);
final String message =
context.getString(R.string.credman_enable_confirmation_message, appName);
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(context.getString(R.string.credman_enable_confirmation_message))
.setPositiveButton(
R.string.credman_enable_confirmation_message_positive_button, this)
.setMessage(message)
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, this)
.create();
}