|
|
|
@@ -17,18 +17,32 @@ package com.android.settings.system;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.content.pm.ResolveInfo;
|
|
|
|
|
import android.os.UserManager;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import androidx.activity.result.ActivityResultLauncher;
|
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts;
|
|
|
|
|
import androidx.preference.Preference;
|
|
|
|
|
|
|
|
|
|
import com.android.settings.R;
|
|
|
|
|
import com.android.settings.Settings;
|
|
|
|
|
import com.android.settings.Utils;
|
|
|
|
|
import com.android.settings.core.BasePreferenceController;
|
|
|
|
|
import com.android.settings.factory_reset.Flags;
|
|
|
|
|
|
|
|
|
|
public class FactoryResetPreferenceController extends BasePreferenceController {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "FactoryResetPreference";
|
|
|
|
|
|
|
|
|
|
private static final String ACTION_PREPARE_FACTORY_RESET =
|
|
|
|
|
"com.android.settings.ACTION_PREPARE_FACTORY_RESET";
|
|
|
|
|
|
|
|
|
|
private static final String PREPARE_FACTORY_RESET_PERMISSION =
|
|
|
|
|
"com.android.settings.permissions.PREPARE_FACTORY_RESET";
|
|
|
|
|
|
|
|
|
|
private final UserManager mUm;
|
|
|
|
|
private ActivityResultLauncher<Intent> mFactoryResetPreparationLauncher;
|
|
|
|
|
|
|
|
|
|
public FactoryResetPreferenceController(Context context, String preferenceKey) {
|
|
|
|
|
super(context, preferenceKey);
|
|
|
|
@@ -44,10 +58,73 @@ public class FactoryResetPreferenceController extends BasePreferenceController {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
|
|
|
|
if (mPreferenceKey.equals(preference.getKey())) {
|
|
|
|
|
final Intent intent = new Intent(mContext, Settings.FactoryResetActivity.class);
|
|
|
|
|
mContext.startActivity(intent);
|
|
|
|
|
if (Flags.enableFactoryResetWizard()) {
|
|
|
|
|
startFactoryResetPreparationActivity();
|
|
|
|
|
} else {
|
|
|
|
|
startFactoryResetActivity();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startFactoryResetPreparationActivity() {
|
|
|
|
|
Intent prepareFactoryResetIntent = getPrepareFactoryResetIntent();
|
|
|
|
|
if (prepareFactoryResetIntent != null && mFactoryResetPreparationLauncher != null) {
|
|
|
|
|
mFactoryResetPreparationLauncher.launch(prepareFactoryResetIntent);
|
|
|
|
|
} else {
|
|
|
|
|
startFactoryResetActivity();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We check that the activity that can handle the factory reset preparation action is indeed
|
|
|
|
|
// a system app with proper permissions.
|
|
|
|
|
private Intent getPrepareFactoryResetIntent() {
|
|
|
|
|
final Intent prepareFactoryResetWizardRequest = new Intent(ACTION_PREPARE_FACTORY_RESET);
|
|
|
|
|
final PackageManager pm = mContext.getPackageManager();
|
|
|
|
|
final ResolveInfo resolution = pm.resolveActivity(prepareFactoryResetWizardRequest, 0);
|
|
|
|
|
if (resolution != null
|
|
|
|
|
&& resolution.activityInfo != null) {
|
|
|
|
|
String packageName = resolution.activityInfo.packageName;
|
|
|
|
|
PackageInfo factoryResetWizardPackageInfo;
|
|
|
|
|
try {
|
|
|
|
|
factoryResetWizardPackageInfo = pm.getPackageInfo(packageName, 0);
|
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
|
Log.e(TAG, "Unable to resolve a Factory Reset Handler Application");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (factoryResetWizardPackageInfo.requestedPermissions == null
|
|
|
|
|
|| factoryResetWizardPackageInfo.requestedPermissions.length == 0) {
|
|
|
|
|
Log.e(TAG, "Factory Reset Handler has no permissions requested.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < factoryResetWizardPackageInfo.requestedPermissions.length; i++) {
|
|
|
|
|
String permission = factoryResetWizardPackageInfo.requestedPermissions[i];
|
|
|
|
|
boolean isGranted =
|
|
|
|
|
(factoryResetWizardPackageInfo.requestedPermissionsFlags[i]
|
|
|
|
|
& PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0;
|
|
|
|
|
if (permission.equals(PREPARE_FACTORY_RESET_PERMISSION) && isGranted) {
|
|
|
|
|
return prepareFactoryResetWizardRequest;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return prepareFactoryResetWizardRequest;
|
|
|
|
|
}
|
|
|
|
|
Log.i(TAG, "Unable to resolve a Factory Reset Handler Activity");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setFragment(ResetDashboardFragment fragment) {
|
|
|
|
|
if (Flags.enableFactoryResetWizard()) {
|
|
|
|
|
mFactoryResetPreparationLauncher = fragment.registerForActivityResult(
|
|
|
|
|
new ActivityResultContracts.StartActivityForResult(),
|
|
|
|
|
result -> {
|
|
|
|
|
startFactoryResetActivity();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startFactoryResetActivity() {
|
|
|
|
|
final Intent intent = new Intent(mContext, Settings.FactoryResetActivity.class);
|
|
|
|
|
mContext.startActivity(intent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|