2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.Button;
26 import com.android.internal.os.storage.ExternalStorageFormatter;
29 * Confirm and execute a format of the sdcard.
30 * Multiple confirmations are required: first, a general "are you sure
31 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
32 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
33 * ON THE SD CARD" prompt. If at any time the phone is allowed to go to sleep, is
34 * locked, et cetera, then the confirmation sequence is abandoned.
36 public class MediaFormat extends Activity {
38 private static final int KEYGUARD_REQUEST = 55;
40 private LayoutInflater mInflater;
42 private View mInitialView;
43 private Button mInitiateButton;
45 private View mFinalView;
46 private Button mFinalButton;
49 * The user has gone through the multiple confirmation, so now we go ahead
50 * and invoke the Mount Service to format the SD card.
52 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
53 public void onClick(View v) {
55 if (Utils.isMonkeyRunning()) {
58 Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
59 intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
66 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
67 * component as a subactivity
69 private boolean runKeyguardConfirmation(int request) {
70 return new ChooseLockSettingsHelper(this)
71 .launchConfirmationActivity(request,
72 getText(R.string.media_format_gesture_prompt),
73 getText(R.string.media_format_gesture_explanation));
77 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
78 super.onActivityResult(requestCode, resultCode, data);
80 if (requestCode != KEYGUARD_REQUEST) {
84 // If the user entered a valid keyguard trace, present the final
85 // confirmation prompt; otherwise, go back to the initial state.
86 if (resultCode == Activity.RESULT_OK) {
87 establishFinalConfirmationState();
88 } else if (resultCode == Activity.RESULT_CANCELED) {
91 establishInitialState();
96 * If the user clicks to begin the reset sequence, we next require a
97 * keyguard confirmation if the user has currently enabled one. If there
98 * is no keyguard available, we simply go to the final confirmation prompt.
100 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
101 public void onClick(View v) {
102 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
103 establishFinalConfirmationState();
109 * Configure the UI for the final confirmation interaction
111 private void establishFinalConfirmationState() {
112 if (mFinalView == null) {
113 mFinalView = mInflater.inflate(R.layout.media_format_final, null);
115 (Button) mFinalView.findViewById(R.id.execute_media_format);
116 mFinalButton.setOnClickListener(mFinalClickListener);
119 setContentView(mFinalView);
123 * In its initial state, the activity presents a button for the user to
124 * click in order to initiate a confirmation sequence. This method is
125 * called from various other points in the code to reset the activity to
128 * <p>Reinflating views from resources is expensive and prevents us from
129 * caching widget pointers, so we use a single-inflate pattern: we lazy-
130 * inflate each view, caching all of the widget pointers we'll need at the
131 * time, then simply reuse the inflated views directly whenever we need
132 * to change contents.
134 private void establishInitialState() {
135 if (mInitialView == null) {
136 mInitialView = mInflater.inflate(R.layout.media_format_primary, null);
138 (Button) mInitialView.findViewById(R.id.initiate_media_format);
139 mInitiateButton.setOnClickListener(mInitiateListener);
142 setContentView(mInitialView);
146 protected void onCreate(Bundle savedState) {
147 super.onCreate(savedState);
151 mInflater = LayoutInflater.from(this);
153 establishInitialState();
156 /** Abandon all progress through the confirmation sequence by returning
157 * to the initial view any time the activity is interrupted (e.g. by
161 public void onPause() {
164 if (!isFinishing()) {
165 establishInitialState();