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