1 /* 2 * Copyright (C) 2015 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.tv.settings.device.storage; 18 19 import android.annotation.Nullable; 20 import android.app.Activity; 21 import android.app.ActivityManager; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.util.Log; 25 26 import androidx.annotation.NonNull; 27 import androidx.leanback.app.GuidedStepFragment; 28 import androidx.leanback.widget.GuidanceStylist; 29 import androidx.leanback.widget.GuidedAction; 30 31 import com.android.tv.settings.R; 32 33 import java.util.List; 34 35 public class ResetActivity extends Activity { 36 37 private static final String TAG = "ResetActivity"; 38 39 /** 40 * Support for shutdown-after-reset. If our launch intent has a true value for 41 * the boolean extra under the following key, then include it in the intent we 42 * use to trigger a factory reset. This will cause us to shut down instead of 43 * restart after the reset. 44 */ 45 private static final String SHUTDOWN_INTENT_EXTRA = "shutdown"; 46 47 @Override onCreate(@ullable Bundle savedInstanceState)48 protected void onCreate(@Nullable Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 if (savedInstanceState == null) { 51 GuidedStepFragment.addAsRoot(this, ResetFragment.newInstance(), android.R.id.content); 52 } 53 } 54 55 public static class ResetFragment extends GuidedStepFragment { 56 newInstance()57 public static ResetFragment newInstance() { 58 59 Bundle args = new Bundle(); 60 61 ResetFragment fragment = new ResetFragment(); 62 fragment.setArguments(args); 63 return fragment; 64 } 65 66 @NonNull 67 @Override onCreateGuidance(Bundle savedInstanceState)68 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 69 return new GuidanceStylist.Guidance( 70 getString(R.string.device_reset), 71 getString(R.string.factory_reset_description), 72 null, 73 getContext().getDrawable(R.drawable.ic_settings_backup_restore_132dp)); 74 } 75 76 @Override onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)77 public void onCreateActions(@NonNull List<GuidedAction> actions, 78 Bundle savedInstanceState) { 79 actions.add(new GuidedAction.Builder(getContext()) 80 .clickAction(GuidedAction.ACTION_ID_CANCEL) 81 .build()); 82 actions.add(new GuidedAction.Builder(getContext()) 83 .clickAction(GuidedAction.ACTION_ID_OK) 84 .title(getString(R.string.device_reset)) 85 .build()); 86 } 87 88 @Override onGuidedActionClicked(GuidedAction action)89 public void onGuidedActionClicked(GuidedAction action) { 90 if (action.getId() == GuidedAction.ACTION_ID_OK) { 91 add(getFragmentManager(), ResetConfirmFragment.newInstance()); 92 } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL) { 93 getActivity().finish(); 94 } else { 95 Log.wtf(TAG, "Unknown action clicked"); 96 } 97 } 98 } 99 100 public static class ResetConfirmFragment extends GuidedStepFragment { 101 newInstance()102 public static ResetConfirmFragment newInstance() { 103 104 Bundle args = new Bundle(); 105 106 ResetConfirmFragment fragment = new ResetConfirmFragment(); 107 fragment.setArguments(args); 108 return fragment; 109 } 110 111 @NonNull 112 @Override onCreateGuidance(Bundle savedInstanceState)113 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 114 return new GuidanceStylist.Guidance( 115 getString(R.string.device_reset), 116 getString(R.string.confirm_factory_reset_description), 117 null, 118 getContext().getDrawable(R.drawable.ic_settings_backup_restore_132dp)); 119 } 120 121 @Override onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)122 public void onCreateActions(@NonNull List<GuidedAction> actions, 123 Bundle savedInstanceState) { 124 actions.add(new GuidedAction.Builder(getContext()) 125 .clickAction(GuidedAction.ACTION_ID_CANCEL) 126 .build()); 127 actions.add(new GuidedAction.Builder(getContext()) 128 .clickAction(GuidedAction.ACTION_ID_OK) 129 .title(getString(R.string.confirm_factory_reset_device)) 130 .build()); 131 } 132 133 @Override onGuidedActionClicked(GuidedAction action)134 public void onGuidedActionClicked(GuidedAction action) { 135 if (action.getId() == GuidedAction.ACTION_ID_OK) { 136 if (ActivityManager.isUserAMonkey()) { 137 Log.v(TAG, "Monkey tried to erase the device. Bad monkey, bad!"); 138 getActivity().finish(); 139 } else { 140 Intent resetIntent = new Intent(Intent.ACTION_FACTORY_RESET); 141 resetIntent.setPackage("android"); 142 resetIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); 143 resetIntent.putExtra(Intent.EXTRA_REASON, "ResetConfirmFragment"); 144 if (getActivity().getIntent().getBooleanExtra(SHUTDOWN_INTENT_EXTRA, false)) { 145 resetIntent.putExtra(SHUTDOWN_INTENT_EXTRA, true); 146 } 147 getContext().sendBroadcast(resetIntent); 148 } 149 } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL) { 150 getActivity().finish(); 151 } else { 152 Log.wtf(TAG, "Unknown action clicked"); 153 } 154 } 155 } 156 } 157