1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.privatespace.delete; 18 19 import static com.android.settings.system.ResetDashboardFragment.PRIVATE_SPACE_DELETE_CREDENTIAL_REQUEST; 20 21 import android.app.Activity; 22 import android.app.Dialog; 23 import android.app.settings.SettingsEnums; 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.content.Intent; 27 import android.os.Bundle; 28 import android.text.TextUtils; 29 30 import androidx.annotation.NonNull; 31 import androidx.annotation.Nullable; 32 import androidx.annotation.VisibleForTesting; 33 import androidx.appcompat.app.AlertDialog; 34 import androidx.fragment.app.FragmentManager; 35 import androidx.preference.Preference; 36 37 import com.android.internal.annotations.Initializer; 38 import com.android.settings.R; 39 import com.android.settings.core.BasePreferenceController; 40 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 41 import com.android.settings.password.ChooseLockSettingsHelper; 42 import com.android.settings.privatespace.PrivateSpaceMaintainer; 43 import com.android.settings.system.ResetDashboardFragment; 44 45 /** Controller to delete private space from Settings Reset options after authentication. */ 46 public class ResetOptionsDeletePrivateSpaceController extends BasePreferenceController { 47 private static final String TAG = "PrivateSpaceResetCtrl"; 48 private ResetDashboardFragment mHostFragment; 49 ResetOptionsDeletePrivateSpaceController( @onNull Context context, @NonNull String preferenceKey)50 public ResetOptionsDeletePrivateSpaceController( 51 @NonNull Context context, @NonNull String preferenceKey) { 52 super(context, preferenceKey); 53 } 54 55 @Initializer setFragment(@onNull ResetDashboardFragment hostFragment)56 public void setFragment(@NonNull ResetDashboardFragment hostFragment) { 57 mHostFragment = hostFragment; 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 return android.multiuser.Flags.enablePrivateSpaceFeatures() 63 && android.multiuser.Flags.deletePrivateSpaceFromReset() 64 && isPrivateSpaceEntryPointEnabled() 65 ? AVAILABLE 66 : UNSUPPORTED_ON_DEVICE; 67 } 68 69 @Override handlePreferenceTreeClick(@onNull Preference preference)70 public boolean handlePreferenceTreeClick(@NonNull Preference preference) { 71 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { 72 return false; 73 } 74 startAuthenticationForDelete(); 75 return true; 76 } 77 78 @VisibleForTesting startAuthenticationForDelete()79 boolean startAuthenticationForDelete() { 80 final ChooseLockSettingsHelper.Builder builder = 81 new ChooseLockSettingsHelper.Builder(mHostFragment.getActivity(), mHostFragment); 82 builder.setRequestCode(PRIVATE_SPACE_DELETE_CREDENTIAL_REQUEST).show(); 83 return true; 84 } 85 86 /** Method to handle onActivityResult */ handleActivityResult(int requestCode, int resultCode, @Nullable Intent data)87 public boolean handleActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 88 if (requestCode == PRIVATE_SPACE_DELETE_CREDENTIAL_REQUEST 89 && resultCode == Activity.RESULT_OK 90 && data != null) { 91 DeletePrivateSpaceDialogFragment dialogFragment = getDeleteDialogFragment(); 92 dialogFragment.show( 93 getFragmentManager(), DeletePrivateSpaceDialogFragment.class.getName()); 94 return true; 95 } 96 return false; 97 } 98 99 @VisibleForTesting getDeleteDialogFragment()100 DeletePrivateSpaceDialogFragment getDeleteDialogFragment() { 101 return new DeletePrivateSpaceDialogFragment(); 102 } 103 104 @VisibleForTesting getFragmentManager()105 FragmentManager getFragmentManager() { 106 return mHostFragment.getFragmentManager(); 107 } 108 109 @VisibleForTesting isPrivateSpaceEntryPointEnabled()110 boolean isPrivateSpaceEntryPointEnabled() { 111 return PrivateSpaceMaintainer.getInstance(mContext).isPrivateSpaceEntryPointEnabled(); 112 } 113 114 /* Dialog shown when deleting private space from Reset Options. */ 115 public static class DeletePrivateSpaceDialogFragment extends InstrumentedDialogFragment { 116 private static final String TAG = "PrivateSpaceResetFrag"; 117 118 @Override getMetricsCategory()119 public int getMetricsCategory() { 120 return SettingsEnums.RESET_DELETE_PRIVATE_SPACE_DIALOG; 121 } 122 123 @NonNull 124 @Override onCreateDialog(@ullable Bundle savedInstanceState)125 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 126 Context context = getContext(); 127 return new AlertDialog.Builder(context) 128 .setTitle(R.string.private_space_delete_header) 129 .setMessage(R.string.reset_private_space_delete_dialog) 130 .setPositiveButton( 131 R.string.private_space_delete_button_label, 132 (DialogInterface dialog, int which) -> { 133 mMetricsFeatureProvider.action( 134 context, SettingsEnums.RESET_DELETE_PRIVATE_SPACE_CONFIRM); 135 PrivateSpaceMaintainer privateSpaceMaintainer = 136 PrivateSpaceMaintainer.getInstance(context); 137 privateSpaceMaintainer.deletePrivateSpace(); 138 dialog.dismiss(); 139 }) 140 .setNegativeButton( 141 R.string.private_space_cancel_label, 142 (DialogInterface dialog, int which) -> { 143 mMetricsFeatureProvider.action( 144 context, SettingsEnums.RESET_DELETE_PRIVATE_SPACE_CANCEL); 145 dialog.cancel(); 146 }) 147 .create(); 148 } 149 } 150 } 151