MediaFormat.java revision 1feaa85791b3b5cc66a16142afc2259a2356bc9e
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 17package com.android.settings; 18 19import com.android.internal.widget.LockPatternUtils; 20 21import android.app.Activity; 22import android.app.AlertDialog; 23import android.content.Context; 24import android.content.Intent; 25import android.os.Bundle; 26import android.os.IMountService; 27import android.os.ServiceManager; 28import android.os.SystemProperties; 29import android.os.Environment; 30import android.text.TextUtils; 31import android.util.Log; 32import android.view.LayoutInflater; 33import android.view.View; 34import android.widget.Button; 35 36/** 37 * Confirm and execute a format of the sdcard. 38 * Multiple confirmations are required: first, a general "are you sure 39 * you want to do this?" prompt, followed by a keyguard pattern trace if the user 40 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING 41 * ON THE SD CARD" prompt. If at any time the phone is allowed to go to sleep, is 42 * locked, et cetera, then the confirmation sequence is abandoned. 43 */ 44public class MediaFormat extends Activity { 45 46 private static final int KEYGUARD_REQUEST = 55; 47 48 private LayoutInflater mInflater; 49 private LockPatternUtils mLockUtils; 50 51 private View mInitialView; 52 private Button mInitiateButton; 53 54 private View mFinalView; 55 private Button mFinalButton; 56 57 /** 58 * The user has gone through the multiple confirmation, so now we go ahead 59 * and invoke the Mount Service to format the SD card. 60 */ 61 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() { 62 public void onClick(View v) { 63 64 // Those monkeys kept committing suicide, so we add this property 65 // to disable going through with the format 66 if (!TextUtils.isEmpty(SystemProperties.get("ro.monkey"))) { 67 return; 68 } 69 IMountService service = 70 IMountService.Stub.asInterface(ServiceManager.getService("mount")); 71 if (service != null) { 72 try { 73 service.formatMedia(Environment.getExternalStorageDirectory().toString()); 74 } catch (android.os.RemoteException e) { 75 // Intentionally blank - there's nothing we can do here 76 Log.w("MediaFormat", "Unable to invoke IMountService.formatMedia()"); 77 } 78 } else { 79 Log.w("MediaFormat", "Unable to locate IMountService"); 80 } 81 finish(); 82 } 83 }; 84 85 /** 86 * Keyguard validation is run using the standard {@link ConfirmLockPattern} 87 * component as a subactivity 88 */ 89 private void runKeyguardConfirmation() { 90 final Intent intent = new Intent(); 91 intent.setClassName("com.android.settings", 92 "com.android.settings.ConfirmLockPattern"); 93 // supply header and footer text in the intent 94 intent.putExtra(ConfirmLockPattern.HEADER_TEXT, 95 getText(R.string.media_format_gesture_prompt)); 96 intent.putExtra(ConfirmLockPattern.FOOTER_TEXT, 97 getText(R.string.media_format_gesture_explanation)); 98 startActivityForResult(intent, KEYGUARD_REQUEST); 99 } 100 101 @Override 102 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 103 super.onActivityResult(requestCode, resultCode, data); 104 105 if (requestCode != KEYGUARD_REQUEST) { 106 return; 107 } 108 109 // If the user entered a valid keyguard trace, present the final 110 // confirmation prompt; otherwise, go back to the initial state. 111 if (resultCode == Activity.RESULT_OK) { 112 establishFinalConfirmationState(); 113 } else { 114 establishInitialState(); 115 } 116 } 117 118 /** 119 * If the user clicks to begin the reset sequence, we next require a 120 * keyguard confirmation if the user has currently enabled one. If there 121 * is no keyguard available, we simply go to the final confirmation prompt. 122 */ 123 private Button.OnClickListener mInitiateListener = new Button.OnClickListener() { 124 public void onClick(View v) { 125 if (mLockUtils.isLockPatternEnabled()) { 126 runKeyguardConfirmation(); 127 } else { 128 establishFinalConfirmationState(); 129 } 130 } 131 }; 132 133 /** 134 * Configure the UI for the final confirmation interaction 135 */ 136 private void establishFinalConfirmationState() { 137 if (mFinalView == null) { 138 mFinalView = mInflater.inflate(R.layout.media_format_final, null); 139 mFinalButton = 140 (Button) mFinalView.findViewById(R.id.execute_media_format); 141 mFinalButton.setOnClickListener(mFinalClickListener); 142 } 143 144 setContentView(mFinalView); 145 } 146 147 /** 148 * In its initial state, the activity presents a button for the user to 149 * click in order to initiate a confirmation sequence. This method is 150 * called from various other points in the code to reset the activity to 151 * this base state. 152 * 153 * <p>Reinflating views from resources is expensive and prevents us from 154 * caching widget pointers, so we use a single-inflate pattern: we lazy- 155 * inflate each view, caching all of the widget pointers we'll need at the 156 * time, then simply reuse the inflated views directly whenever we need 157 * to change contents. 158 */ 159 private void establishInitialState() { 160 if (mInitialView == null) { 161 mInitialView = mInflater.inflate(R.layout.media_format_primary, null); 162 mInitiateButton = 163 (Button) mInitialView.findViewById(R.id.initiate_media_format); 164 mInitiateButton.setOnClickListener(mInitiateListener); 165 } 166 167 setContentView(mInitialView); 168 } 169 170 @Override 171 protected void onCreate(Bundle savedState) { 172 super.onCreate(savedState); 173 174 mInitialView = null; 175 mFinalView = null; 176 mInflater = LayoutInflater.from(this); 177 mLockUtils = new LockPatternUtils(getContentResolver()); 178 179 establishInitialState(); 180 } 181 182 /** Abandon all progress through the confirmation sequence by returning 183 * to the initial view any time the activity is interrupted (e.g. by 184 * idle timeout). 185 */ 186 @Override 187 public void onPause() { 188 super.onPause(); 189 190 establishInitialState(); 191 } 192 193} 194