OSDN Git Service

d8d57e470ddb7e6a5e9f07d0311fcf55a3c9d523
[android-x86/packages-apps-Settings.git] / src / com / android / settings / MediaFormat.java
1 /*
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
17 package com.android.settings;
18
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;
25
26 import com.android.internal.os.storage.ExternalStorageFormatter;
27
28 /**
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.
35  */
36 public class MediaFormat extends Activity {
37
38     private static final int KEYGUARD_REQUEST = 55;
39
40     private LayoutInflater mInflater;
41
42     private View mInitialView;
43     private Button mInitiateButton;
44
45     private View mFinalView;
46     private Button mFinalButton;
47
48     /**
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.
51      */
52     private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
53             public void onClick(View v) {
54
55                 if (Utils.isMonkeyRunning()) {
56                     return;
57                 }
58                 Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
59                 intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
60                 startService(intent);
61                 finish();
62             }
63         };
64
65     /**
66      *  Keyguard validation is run using the standard {@link ConfirmLockPattern}
67      * component as a subactivity
68      */
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));
74     }
75
76     @Override
77     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
78         super.onActivityResult(requestCode, resultCode, data);
79
80         if (requestCode != KEYGUARD_REQUEST) {
81             return;
82         }
83
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) {
89             finish();
90         } else {
91             establishInitialState();
92         }
93     }
94
95     /**
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.
99      */
100     private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
101             public void onClick(View v) {
102                 if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
103                     establishFinalConfirmationState();
104                 }
105             }
106         };
107
108     /**
109      * Configure the UI for the final confirmation interaction
110      */
111     private void establishFinalConfirmationState() {
112         if (mFinalView == null) {
113             mFinalView = mInflater.inflate(R.layout.media_format_final, null);
114             mFinalButton =
115                     (Button) mFinalView.findViewById(R.id.execute_media_format);
116             mFinalButton.setOnClickListener(mFinalClickListener);
117         }
118
119         setContentView(mFinalView);
120     }
121
122     /**
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
126      * this base state.
127      *
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.
133      */
134     private void establishInitialState() {
135         if (mInitialView == null) {
136             mInitialView = mInflater.inflate(R.layout.media_format_primary, null);
137             mInitiateButton =
138                     (Button) mInitialView.findViewById(R.id.initiate_media_format);
139             mInitiateButton.setOnClickListener(mInitiateListener);
140         }
141
142         setContentView(mInitialView);
143     }
144
145     @Override
146     protected void onCreate(Bundle savedState) {
147         super.onCreate(savedState);
148
149         mInitialView = null;
150         mFinalView = null;
151         mInflater = LayoutInflater.from(this);
152
153         establishInitialState();
154     }
155
156     /** Abandon all progress through the confirmation sequence by returning
157      * to the initial view any time the activity is interrupted (e.g. by
158      * idle timeout).
159      */
160     @Override
161     public void onPause() {
162         super.onPause();
163
164         if (!isFinishing()) {
165             establishInitialState();
166         }
167     }
168 }