blob: 770ee122db5e7eb6b4c71eeedca9ee65159edea9 [file] [log] [blame]
Jason parks8fd5bc92011-01-12 16:03:31 -06001/*
2 * Copyright (C) 2011 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.PasswordEntryKeyboardHelper;
20import com.android.internal.widget.PasswordEntryKeyboardView;
21
22import android.app.Activity;
23import android.app.StatusBarManager;
24import android.content.ComponentName;
25import android.content.Context;
Jason parksec5a45e2011-01-18 15:28:36 -060026import android.content.Intent;
Jason parks8fd5bc92011-01-12 16:03:31 -060027import android.content.pm.PackageManager;
28import android.inputmethodservice.KeyboardView;
29import android.os.Bundle;
Jason parksec5a45e2011-01-18 15:28:36 -060030import android.os.Handler;
Jason parks8fd5bc92011-01-12 16:03:31 -060031import android.os.IBinder;
Jason parksec5a45e2011-01-18 15:28:36 -060032import android.os.Message;
Jason parks35933812011-01-21 15:48:20 -060033import android.os.PowerManager;
Jason parks8fd5bc92011-01-12 16:03:31 -060034import android.os.ServiceManager;
35import android.os.SystemProperties;
36import android.os.storage.IMountService;
Jason parksec5a45e2011-01-18 15:28:36 -060037import android.text.TextUtils;
Jason parks8fd5bc92011-01-12 16:03:31 -060038import android.util.Log;
39import android.view.KeyEvent;
Andy Stadler13d62042011-01-31 19:21:37 -080040import android.view.View;
41import android.view.View.OnClickListener;
Jason parks8fd5bc92011-01-12 16:03:31 -060042import android.view.inputmethod.EditorInfo;
Andy Stadler13d62042011-01-31 19:21:37 -080043import android.widget.Button;
Jason parksec5a45e2011-01-18 15:28:36 -060044import android.widget.EditText;
45import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060046import android.widget.TextView;
47
Jason parks8fd5bc92011-01-12 16:03:31 -060048public class CryptKeeper extends Activity implements TextView.OnEditorActionListener {
Jason parksec5a45e2011-01-18 15:28:36 -060049 private static final String TAG = "CryptKeeper";
Jason parks35933812011-01-21 15:48:20 -060050
Jason parks8fd5bc92011-01-12 16:03:31 -060051 private static final String DECRYPT_STATE = "trigger_restart_framework";
Jason parksec5a45e2011-01-18 15:28:36 -060052
53 private static final int UPDATE_PROGRESS = 1;
54 private static final int COOLDOWN = 2;
55
56 private static final int MAX_FAILED_ATTEMPTS = 30;
57 private static final int COOL_DOWN_ATTEMPTS = 10;
58 private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds
59
Andy Stadler14997402011-02-01 15:34:59 -080060 private int mCooldown;
61 PowerManager.WakeLock mWakeLock;
62
63 /**
64 * Used to propagate state through configuration changes (e.g. screen rotation)
65 */
66 private static class NonConfigurationInstanceState {
67 final PowerManager.WakeLock wakelock;
68
69 NonConfigurationInstanceState(PowerManager.WakeLock _wakelock) {
70 wakelock = _wakelock;
71 }
72 }
73
Jason parksf1dbf552011-01-24 16:19:28 -060074 // This activity is used to fade the screen to black after the password is entered.
75 public static class Blank extends Activity {
Andy Stadler13d62042011-01-31 19:21:37 -080076 @Override
77 public void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79 setContentView(R.layout.crypt_keeper_blank);
80 }
Jason parksf1dbf552011-01-24 16:19:28 -060081 }
Jason parksec5a45e2011-01-18 15:28:36 -060082
83 private Handler mHandler = new Handler() {
84 @Override
85 public void handleMessage(Message msg) {
Jason parksec5a45e2011-01-18 15:28:36 -060086 switch (msg.what) {
Jason parksec5a45e2011-01-18 15:28:36 -060087 case UPDATE_PROGRESS:
Jason parksf8217302011-01-26 13:11:42 -060088 updateProgress();
Jason parksec5a45e2011-01-18 15:28:36 -060089 break;
Jason parks35933812011-01-21 15:48:20 -060090
Jason parksec5a45e2011-01-18 15:28:36 -060091 case COOLDOWN:
Jason parksf8217302011-01-26 13:11:42 -060092 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -060093 break;
94 }
95 }
96 };
Jason parks35933812011-01-21 15:48:20 -060097
Jason parks8fd5bc92011-01-12 16:03:31 -060098 @Override
99 public void onCreate(Bundle savedInstanceState) {
100 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600101
Jason parks8fd5bc92011-01-12 16:03:31 -0600102 String state = SystemProperties.get("vold.decrypt");
103 if ("".equals(state) || DECRYPT_STATE.equals(state)) {
Jason parks35933812011-01-21 15:48:20 -0600104 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600105 PackageManager pm = getPackageManager();
106 ComponentName name = new ComponentName(this, CryptKeeper.class);
107 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
108 return;
109 }
Jason parks35933812011-01-21 15:48:20 -0600110
Jason parksec5a45e2011-01-18 15:28:36 -0600111 // Check to see why we were started.
112 String progress = SystemProperties.get("vold.encrypt_progress");
Jason parksdbf43222011-01-20 18:49:58 -0600113
114 if (!"".equals(progress)) {
Jason parksec5a45e2011-01-18 15:28:36 -0600115 setContentView(R.layout.crypt_keeper_progress);
116 encryptionProgressInit();
117 } else {
118 setContentView(R.layout.crypt_keeper_password_entry);
119 passwordEntryInit();
120 }
Jason parks39f1e042011-01-20 23:29:28 -0600121
122 // Disable the status bar
123 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
Andy Stadler13d62042011-01-31 19:21:37 -0800124 sbm.disable(StatusBarManager.DISABLE_EXPAND
125 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
Jason parks39f1e042011-01-20 23:29:28 -0600126 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
Andy Stadler13d62042011-01-31 19:21:37 -0800127 | StatusBarManager.DISABLE_SYSTEM_INFO
128 | StatusBarManager.DISABLE_NAVIGATION
129 | StatusBarManager.DISABLE_BACK);
Andy Stadler14997402011-02-01 15:34:59 -0800130
131 // Check for (and recover) retained instance data
132 Object lastInstance = getLastNonConfigurationInstance();
133 if (lastInstance instanceof NonConfigurationInstanceState) {
134 NonConfigurationInstanceState retained = (NonConfigurationInstanceState) lastInstance;
135 mWakeLock = retained.wakelock;
136 }
Jason parksec5a45e2011-01-18 15:28:36 -0600137 }
Jason parks35933812011-01-21 15:48:20 -0600138
Jason parksf8217302011-01-26 13:11:42 -0600139 @Override
140 public void onStop() {
141 super.onStop();
142
143 mHandler.removeMessages(COOLDOWN);
144 mHandler.removeMessages(UPDATE_PROGRESS);
Andy Stadler14997402011-02-01 15:34:59 -0800145 }
146
147 /**
148 * Reconfiguring, so propagate the wakelock to the next instance. This runs between onStop()
149 * and onDestroy() and only if we are changing configuration (e.g. rotation). Also clears
150 * mWakeLock so the subsequent call to onDestroy does not release it.
151 */
152 @Override
153 public Object onRetainNonConfigurationInstance() {
154 NonConfigurationInstanceState state = new NonConfigurationInstanceState(mWakeLock);
155 mWakeLock = null;
156 return state;
157 }
158
159 @Override
160 public void onDestroy() {
161 super.onDestroy();
Jason parksf8217302011-01-26 13:11:42 -0600162
163 if (mWakeLock != null) {
164 mWakeLock.release();
165 mWakeLock = null;
166 }
167 }
168
Jason parksec5a45e2011-01-18 15:28:36 -0600169 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600170 // Accquire a partial wakelock to prevent the device from sleeping. Note
171 // we never release this wakelock as we will be restarted after the device
172 // is encrypted.
173
174 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Jason parksf8217302011-01-26 13:11:42 -0600175 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Jason parks35933812011-01-21 15:48:20 -0600176
Jason parksf8217302011-01-26 13:11:42 -0600177 mWakeLock.acquire();
Jason parks35933812011-01-21 15:48:20 -0600178
Jason parksf8217302011-01-26 13:11:42 -0600179 ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
180 progressBar.setIndeterminate(true);
181
182 updateProgress();
183 }
184
Andy Stadler13d62042011-01-31 19:21:37 -0800185 private void showFactoryReset() {
186 // Hide the encryption-bot to make room for the "factory reset" button
187 findViewById(R.id.encroid).setVisibility(View.GONE);
188
189 // Show the reset button, failure text, and a divider
190 Button button = (Button) findViewById(R.id.factory_reset);
191 button.setVisibility(View.VISIBLE);
192 button.setOnClickListener(new OnClickListener() {
193 public void onClick(View v) {
194 // Factory reset the device.
195 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
196 }
197 });
198
199 TextView tv = (TextView) findViewById(R.id.title);
200 tv.setText(R.string.crypt_keeper_failed_title);
201
202 tv = (TextView) findViewById(R.id.status);
203 tv.setText(R.string.crypt_keeper_failed_summary);
204
205 View view = findViewById(R.id.bottom_divider);
206 view.setVisibility(View.VISIBLE);
207 }
208
Jason parksf8217302011-01-26 13:11:42 -0600209 private void updateProgress() {
210 String state = SystemProperties.get("vold.encrypt_progress");
211
Andy Stadler13d62042011-01-31 19:21:37 -0800212 if ("error_partially_encrypted".equals(state)) {
213 showFactoryReset();
214 return;
215 }
216
Jason parksf8217302011-01-26 13:11:42 -0600217 int progress = 0;
218 try {
219 progress = Integer.parseInt(state);
220 } catch (Exception e) {
221 Log.w(TAG, "Error parsing progress: " + e.toString());
222 }
223
224 CharSequence status = getText(R.string.crypt_keeper_setup_description);
225 TextView tv = (TextView) findViewById(R.id.status);
226 tv.setText(TextUtils.expandTemplate(status, Integer.toString(progress)));
227
228 // Check the progress every 5 seconds
229 mHandler.removeMessages(UPDATE_PROGRESS);
230 mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS, 5000);
231 }
232
233 private void cooldown() {
234 TextView tv = (TextView) findViewById(R.id.status);
Andy Stadler13d62042011-01-31 19:21:37 -0800235
Jason parksf8217302011-01-26 13:11:42 -0600236 if (mCooldown <= 0) {
237 // Re-enable the password entry
238 EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry);
239 passwordEntry.setEnabled(true);
240
Andy Stadler13d62042011-01-31 19:21:37 -0800241 tv.setVisibility(View.GONE);
Jason parksf8217302011-01-26 13:11:42 -0600242 } else {
Andy Stadler13d62042011-01-31 19:21:37 -0800243 CharSequence template = getText(R.string.crypt_keeper_cooldown);
244 tv.setText(TextUtils.expandTemplate(template, Integer.toString(mCooldown)));
245
246 tv.setVisibility(View.VISIBLE);
Jason parksf8217302011-01-26 13:11:42 -0600247
248 mCooldown--;
249 mHandler.removeMessages(COOLDOWN);
250 mHandler.sendEmptyMessageDelayed(COOLDOWN, 1000); // Tick every second
251 }
Jason parksec5a45e2011-01-18 15:28:36 -0600252 }
Jason parks35933812011-01-21 15:48:20 -0600253
Jason parksec5a45e2011-01-18 15:28:36 -0600254 private void passwordEntryInit() {
Jason parks8fd5bc92011-01-12 16:03:31 -0600255 TextView passwordEntry = (TextView) findViewById(R.id.passwordEntry);
256 passwordEntry.setOnEditorActionListener(this);
Jason parks35933812011-01-21 15:48:20 -0600257
Jason parks8fd5bc92011-01-12 16:03:31 -0600258 KeyboardView keyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard);
Jason parks35933812011-01-21 15:48:20 -0600259
Jason parks8fd5bc92011-01-12 16:03:31 -0600260 PasswordEntryKeyboardHelper keyboardHelper = new PasswordEntryKeyboardHelper(this,
261 keyboardView, passwordEntry, false);
262 keyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA);
Jason parks8fd5bc92011-01-12 16:03:31 -0600263 }
264
265 private IMountService getMountService() {
266 IBinder service = ServiceManager.getService("mount");
267 if (service != null) {
268 return IMountService.Stub.asInterface(service);
269 }
270 return null;
271 }
272
273 @Override
274 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
275 if (actionId == EditorInfo.IME_NULL) {
276 // Get the password
277 String password = v.getText().toString();
278
Jason parksec5a45e2011-01-18 15:28:36 -0600279 if (TextUtils.isEmpty(password)) {
280 return true;
281 }
Jason parks35933812011-01-21 15:48:20 -0600282
Jason parks8fd5bc92011-01-12 16:03:31 -0600283 // Now that we have the password clear the password field.
284 v.setText(null);
285
286 IMountService service = getMountService();
287 try {
Jason parksf1dbf552011-01-24 16:19:28 -0600288 int failedAttempts = service.decryptStorage(password);
Jason parks8fd5bc92011-01-12 16:03:31 -0600289
Jason parksf1dbf552011-01-24 16:19:28 -0600290 if (failedAttempts == 0) {
291 // The password was entered successfully. Start the Blank activity
292 // so this activity animates to black before the devices starts. Note
293 // It has 1 second to complete the animation or it will be frozen
294 // until the boot animation comes back up.
295 Intent intent = new Intent(this, Blank.class);
296 finish();
297 startActivity(intent);
298 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
Jason parksec5a45e2011-01-18 15:28:36 -0600299 // Factory reset the device.
300 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parksf1dbf552011-01-24 16:19:28 -0600301 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Jason parksec5a45e2011-01-18 15:28:36 -0600302 mCooldown = COOL_DOWN_INTERVAL;
303 EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry);
304 passwordEntry.setEnabled(false);
Jason parksf8217302011-01-26 13:11:42 -0600305 cooldown();
Jason parksec5a45e2011-01-18 15:28:36 -0600306 } else {
307 TextView tv = (TextView) findViewById(R.id.status);
308 tv.setText(R.string.try_again);
Andy Stadler13d62042011-01-31 19:21:37 -0800309 tv.setVisibility(View.VISIBLE);
Jason parksec5a45e2011-01-18 15:28:36 -0600310 }
Jason parks8fd5bc92011-01-12 16:03:31 -0600311 } catch (Exception e) {
Jason parksec5a45e2011-01-18 15:28:36 -0600312 Log.e(TAG, "Error while decrypting...", e);
Jason parks8fd5bc92011-01-12 16:03:31 -0600313 }
Jason parks35933812011-01-21 15:48:20 -0600314
Jason parks8fd5bc92011-01-12 16:03:31 -0600315 return true;
316 }
317 return false;
318 }
319}