Add a preference in reset options to delete private space

This takses care to add an entry point in Settings->System->Reset
options->Delete private space when private space feature is supported on
the device. Entry point appears both when private space exists and also
when private is not created.
On selecting asks for user authentication and on successful
authentication using device lock shows a dialog asking for
confirmation and calls for deletion private space.

Based on this entrypoint it would not be possible to guess if private
space exists on the device.

Recording link : b/329041740#comment3

Bug: 329041740
Test: atest ResetOptionsDeletePrivateSpaceControllerTest and verified
this doesn't give away existence of private space

Change-Id: I9a5b908e7a3f9edaf609cf7e6a87f9842d689ce6
This commit is contained in:
josephpv
2024-03-18 12:52:13 +00:00
parent d47736635b
commit 43c8d3a3f8
5 changed files with 356 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privatespace.delete;
import static com.android.settings.system.ResetDashboardFragment.PRIVATE_SPACE_DELETE_CREDENTIAL_REQUEST;
import android.app.Activity;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import com.android.internal.annotations.Initializer;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.password.ChooseLockSettingsHelper;
import com.android.settings.privatespace.PrivateSpaceMaintainer;
import com.android.settings.system.ResetDashboardFragment;
/** Controller to delete private space from Settings Reset options after authentication. */
public class ResetOptionsDeletePrivateSpaceController extends BasePreferenceController {
private static final String TAG = "PrivateSpaceResetCtrl";
private ResetDashboardFragment mHostFragment;
public ResetOptionsDeletePrivateSpaceController(
@NonNull Context context, @NonNull String preferenceKey) {
super(context, preferenceKey);
}
@Initializer
public void setFragment(@NonNull ResetDashboardFragment hostFragment) {
mHostFragment = hostFragment;
}
@Override
public int getAvailabilityStatus() {
// TODO(b/330396315) : use canAddPrivateProfile() to check if private space is supported
// on the device
return android.multiuser.Flags.enablePrivateSpaceFeatures()
&& android.multiuser.Flags.deletePrivateSpaceFromReset()
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public boolean handlePreferenceTreeClick(@NonNull Preference preference) {
if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
return false;
}
startAuthenticationForDelete();
return true;
}
@VisibleForTesting
boolean startAuthenticationForDelete() {
final ChooseLockSettingsHelper.Builder builder =
new ChooseLockSettingsHelper.Builder(mHostFragment.getActivity(), mHostFragment);
builder.setRequestCode(PRIVATE_SPACE_DELETE_CREDENTIAL_REQUEST).show();
return true;
}
/** Method to handle onActivityResult */
public boolean handleActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == PRIVATE_SPACE_DELETE_CREDENTIAL_REQUEST
&& resultCode == Activity.RESULT_OK
&& data != null) {
DeletePrivateSpaceDialogFragment dialogFragment = getDeleteDialogFragment();
dialogFragment.show(
getFragmentManager(), DeletePrivateSpaceDialogFragment.class.getName());
return true;
}
return false;
}
@VisibleForTesting
DeletePrivateSpaceDialogFragment getDeleteDialogFragment() {
return new DeletePrivateSpaceDialogFragment();
}
@VisibleForTesting
FragmentManager getFragmentManager() {
return mHostFragment.getFragmentManager();
}
/* Dialog shown when deleting private space from Reset Options. */
public static class DeletePrivateSpaceDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "PrivateSpaceResetFrag";
@Override
public int getMetricsCategory() {
return SettingsEnums.RESET_DELETE_PRIVATE_SPACE_DIALOG;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Context context = getContext();
return new AlertDialog.Builder(context)
.setTitle(R.string.reset_private_space_delete_title)
.setMessage(R.string.reset_private_space_delete_dialog)
.setPositiveButton(
R.string.private_space_delete_button_label,
(DialogInterface dialog, int which) -> {
mMetricsFeatureProvider.action(
context, SettingsEnums.RESET_DELETE_PRIVATE_SPACE_CONFIRM);
PrivateSpaceMaintainer privateSpaceMaintainer =
PrivateSpaceMaintainer.getInstance(context);
privateSpaceMaintainer.deletePrivateSpace();
dialog.dismiss();
})
.setNegativeButton(
R.string.private_space_cancel_label,
(DialogInterface dialog, int which) -> {
mMetricsFeatureProvider.action(
context, SettingsEnums.RESET_DELETE_PRIVATE_SPACE_CANCEL);
dialog.cancel();
})
.create();
}
}
}