1 /* 2 * Copyright (C) 2021 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.car.settings.profiles; 18 19 import android.content.Context; 20 import android.content.Intent; 21 22 import com.android.car.settings.common.ConfirmationDialogFragment; 23 import com.android.car.settings.common.FragmentController; 24 import com.android.internal.annotations.VisibleForTesting; 25 26 /** 27 * Consolidates demo profile dialog logic into one handler so we can have 28 * consistent logic across various parts of the Settings app. 29 */ 30 public class DemoProfileDialogHandler { 31 32 private static final String FACTORY_RESET_PACKAGE_NAME = "android"; 33 private static final String FACTORY_RESET_REASON = "ExitRetailModeConfirmed"; 34 @VisibleForTesting 35 static final String CONFIRM_EXIT_RETAIL_MODE_DIALOG_TAG = 36 "com.android.car.settings.profiles.ConfirmExitRetailModeDialog"; 37 38 private final Context mContext; 39 private final FragmentController mFragmentController; 40 41 /** 42 * Will perform a factory reset. Copied from 43 * {@link com.android.settings.MainClearConfirm#doMainClear()} 44 */ 45 private final ConfirmationDialogFragment.ConfirmListener mConfirmExitRetailModeListener; 46 DemoProfileDialogHandler(Context context, FragmentController fragmentController)47 public DemoProfileDialogHandler(Context context, FragmentController fragmentController) { 48 mContext = context; 49 mFragmentController = fragmentController; 50 mConfirmExitRetailModeListener = 51 arguments -> { 52 Intent intent = new Intent(Intent.ACTION_FACTORY_RESET); 53 intent.setPackage(FACTORY_RESET_PACKAGE_NAME); 54 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 55 intent.putExtra(Intent.EXTRA_REASON, FACTORY_RESET_REASON); 56 intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, true); 57 intent.putExtra(Intent.EXTRA_WIPE_ESIMS, true); 58 mContext.sendBroadcast(intent); 59 // Intent handling is asynchronous -- assume it will happen soon. 60 }; 61 } 62 63 /** 64 * Handles operations that should happen in host's onCreateInternal(). 65 * Resets listeners as they can get unregistered with certain configuration changes. 66 */ onCreateInternal()67 public void onCreateInternal() { 68 ConfirmationDialogFragment.resetListeners( 69 (ConfirmationDialogFragment) mFragmentController.findDialogByTag( 70 CONFIRM_EXIT_RETAIL_MODE_DIALOG_TAG), 71 mConfirmExitRetailModeListener, 72 /* rejectListener= */ null, 73 /* neutralListener= */ null); 74 } 75 76 /** 77 * Display dialog to exit retail mode 78 */ showExitRetailDialog()79 public void showExitRetailDialog() { 80 ConfirmationDialogFragment dialogFragment = 81 ProfilesDialogProvider.getConfirmExitRetailModeDialogFragment(mContext, 82 mConfirmExitRetailModeListener, null); 83 84 mFragmentController.showDialog(dialogFragment, CONFIRM_EXIT_RETAIL_MODE_DIALOG_TAG); 85 } 86 } 87