blob: 3060d3e1893979749e2a06ede47586832dc76514 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2008 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
17package com.android.settings;
18
Amith Yamasanib14e1e02010-11-02 09:52:29 -070019import com.android.settings.R;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080020
Joe Onoratob51886d2010-11-08 18:25:51 -080021import android.accounts.Account;
22import android.accounts.AccountManager;
23import android.accounts.AuthenticatorDescription;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.app.Activity;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070025import android.app.Fragment;
Joe Onoratob51886d2010-11-08 18:25:51 -080026import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080028import android.content.pm.PackageManager;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070029import android.content.res.Resources;
Joe Onoratob51886d2010-11-08 18:25:51 -080030import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.os.Bundle;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070032import android.preference.Preference;
33import android.preference.PreferenceActivity;
Joe Onoratob51886d2010-11-08 18:25:51 -080034import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035import android.view.LayoutInflater;
36import android.view.View;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070037import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070039import android.widget.CheckBox;
Joe Onoratob51886d2010-11-08 18:25:51 -080040import android.widget.LinearLayout;
41import android.widget.TextView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042
43/**
44 * Confirm and execute a reset of the device to a clean "just out of the box"
45 * state. Multiple confirmations are required: first, a general "are you sure
46 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
47 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
48 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
49 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070050 *
51 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052 */
Amith Yamasanib14e1e02010-11-02 09:52:29 -070053public class MasterClear extends Fragment {
Joe Onoratob51886d2010-11-08 18:25:51 -080054 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055
56 private static final int KEYGUARD_REQUEST = 55;
57
Amith Yamasanib14e1e02010-11-02 09:52:29 -070058 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080059
Amith Yamasanib14e1e02010-11-02 09:52:29 -070060 private View mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080061 private Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070062 private View mExternalStorageContainer;
63 private CheckBox mExternalStorage;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065 /**
Jim Miller2deec7e2010-04-13 17:43:36 -070066 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -070068 * @param request the request code to be returned once confirmation finishes
69 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070 */
Jim Miller2deec7e2010-04-13 17:43:36 -070071 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070072 Resources res = getActivity().getResources();
73 return new ChooseLockSettingsHelper(getActivity(), this)
Jim Miller2deec7e2010-04-13 17:43:36 -070074 .launchConfirmationActivity(request,
Amith Yamasanib14e1e02010-11-02 09:52:29 -070075 res.getText(R.string.master_clear_gesture_prompt),
76 res.getText(R.string.master_clear_gesture_explanation));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 }
78
79 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -070080 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 super.onActivityResult(requestCode, resultCode, data);
82
83 if (requestCode != KEYGUARD_REQUEST) {
84 return;
85 }
86
87 // If the user entered a valid keyguard trace, present the final
88 // confirmation prompt; otherwise, go back to the initial state.
89 if (resultCode == Activity.RESULT_OK) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -070090 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 } else {
92 establishInitialState();
93 }
94 }
95
Amith Yamasanib14e1e02010-11-02 09:52:29 -070096 private void showFinalConfirmation() {
97 Preference preference = new Preference(getActivity());
98 preference.setFragment(MasterClearConfirm.class.getName());
99 preference.setTitle(R.string.master_clear_confirm_title);
100 preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
101 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(null, preference);
102 }
103
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800104 /**
105 * If the user clicks to begin the reset sequence, we next require a
106 * keyguard confirmation if the user has currently enabled one. If there
107 * is no keyguard available, we simply go to the final confirmation prompt.
108 */
109 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700110
111 public void onClick(View v) {
112 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
113 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700116 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117
118 /**
119 * In its initial state, the activity presents a button for the user to
120 * click in order to initiate a confirmation sequence. This method is
121 * called from various other points in the code to reset the activity to
122 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800123 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800124 * <p>Reinflating views from resources is expensive and prevents us from
125 * caching widget pointers, so we use a single-inflate pattern: we lazy-
126 * inflate each view, caching all of the widget pointers we'll need at the
127 * time, then simply reuse the inflated views directly whenever we need
128 * to change contents.
129 */
130 private void establishInitialState() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700131 mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
132 mInitiateButton.setOnClickListener(mInitiateListener);
133 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
134 mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700136 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
137
138 @Override
139 public void onClick(View v) {
140 mExternalStorage.toggle();
141 }
142 });
Joe Onoratob51886d2010-11-08 18:25:51 -0800143
144 loadAccountList();
145 }
146
147 private void loadAccountList() {
148 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
149 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
150
151 Context context = getActivity();
152
153 AccountManager mgr = AccountManager.get(context);
154 Account[] accounts = mgr.getAccounts();
155 final int N = accounts.length;
156 if (N == 0) {
157 accountsLabel.setVisibility(View.GONE);
158 contents.setVisibility(View.GONE);
159 return;
160 }
161
162 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
163 Context.LAYOUT_INFLATER_SERVICE);
164
165 AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypes();
166 final int M = descs.length;
167
168 for (int i=0; i<N; i++) {
169 Account account = accounts[i];
170 AuthenticatorDescription desc = null;
171 for (int j=0; j<M; j++) {
172 if (account.type.equals(descs[j].type)) {
173 desc = descs[j];
174 break;
175 }
176 }
177 if (desc == null) {
178 Log.w(TAG, "No descriptor for account name=" + account.name
179 + " type=" + account.type);
180 continue;
181 }
182 Drawable icon;
183 try {
184 Context authContext = context.createPackageContext(desc.packageName, 0);
185 icon = authContext.getResources().getDrawable(desc.iconId);
186 } catch (PackageManager.NameNotFoundException e) {
187 Log.w(TAG, "No icon for account type " + desc.type);
188 icon = null;
189 }
190
191 TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
192 contents, false);
193 child.setText(account.name);
194 if (icon != null) {
195 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
196 }
197 contents.addView(child);
198 }
199
200 accountsLabel.setVisibility(View.VISIBLE);
201 contents.setVisibility(View.VISIBLE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800202 }
203
204 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700205 public View onCreateView(LayoutInflater inflater, ViewGroup container,
206 Bundle savedInstanceState) {
207 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800208
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800209 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700210 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800211 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800212}