代码位置(android 9):
packages\apps\PackageInstaller\src\com\android\packageinstaller\PackageInstallerActivity.java
效果:
/**
* Dialog to show when the source of apk can not be identified
*/
public static class AnonymousSourceDialog extends DialogFragment {
static AnonymousSourceDialog newInstance() {
return new AnonymousSourceDialog();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.anonymous_source_warning)
.setPositiveButton(R.string.anonymous_source_continue,
((dialog, which) -> {
PackageInstallerActivity activity = ((PackageInstallerActivity)
getActivity());
activity.mAllowUnknownSources = true;
activity.initiateInstall();
}))
.setNegativeButton(R.string.cancel, ((dialog, which) -> getActivity().finish()))
.create();
}
@Override
public void onCancel(DialogInterface dialog) {
getActivity().finish();
}
}
调用:
case DLG_ANONYMOUS_SOURCE:
return AnonymousSourceDialog.newInstance();
/**
* Replace any dialog shown by the dialog with the one for the given {@link #createDialog id}.
*
* @param id The dialog type to add
*/
private void showDialogInner(int id) {
DialogFragment currentDialog =
(DialogFragment) getFragmentManager().findFragmentByTag("dialog");
if (currentDialog != null) {
currentDialog.dismissAllowingStateLoss();
}
DialogFragment newDialog = createDialog(id);
if (newDialog != null) {
newDialog.showAllowingStateLoss(getFragmentManager(), "dialog");
}
}
/**
* Create a new dialog.
*
* @param id The id of the dialog (determines dialog type)
*
* @return The dialog
*/
private DialogFragment createDialog(int id) {
switch (id) {
case DLG_PACKAGE_ERROR:
return SimpleErrorDialog.newInstance(R.string.Parse_error_dlg_text);
case DLG_OUT_OF_SPACE:
return OutOfSpaceDialog.newInstance(
mPm.getApplicationLabel(mPkgInfo.applicationInfo));
case DLG_INSTALL_ERROR:
return InstallErrorDialog.newInstance(
mPm.getApplicationLabel(mPkgInfo.applicationInfo));
case DLG_NOT_SUPPORTED_ON_WEAR:
return NotSupportedOnWearDialog.newInstance();
case DLG_INSTALL_APPS_RESTRICTED_FOR_USER:
return SimpleErrorDialog.newInstance(
R.string.install_apps_user_restriction_dlg_text);
case DLG_UNKNOWN_SOURCES_RESTRICTED_FOR_USER:
return SimpleErrorDialog.newInstance(
R.string.unknown_apps_user_restriction_dlg_text);
case DLG_EXTERNAL_SOURCE_BLOCKED:
return ExternalSourcesBlockedDialog.newInstance(mOriginatingPackage);
case DLG_ANONYMOUS_SOURCE:
return AnonymousSourceDialog.newInstance();
}
return null;
}