blob: 3917b76e6f8a154b3811cb0769ee29d420afdf72 [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.text.format.DateFormat;
39import android.util.Log;
40import android.view.KeyEvent;
41import android.view.inputmethod.EditorInfo;
Jason parksec5a45e2011-01-18 15:28:36 -060042import android.widget.EditText;
43import android.widget.ProgressBar;
Jason parks8fd5bc92011-01-12 16:03:31 -060044import android.widget.TextView;
45
46import java.util.Date;
47
48public 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
60
61 private Handler mHandler = new Handler() {
62 @Override
63 public void handleMessage(Message msg) {
Jason parks35933812011-01-21 15:48:20 -060064
Jason parksec5a45e2011-01-18 15:28:36 -060065 switch (msg.what) {
Jason parks35933812011-01-21 15:48:20 -060066
Jason parksec5a45e2011-01-18 15:28:36 -060067 case UPDATE_PROGRESS:
68 String state = SystemProperties.get("vold.encrypt_progress");
Jason parks35933812011-01-21 15:48:20 -060069
Jason parksec5a45e2011-01-18 15:28:36 -060070 ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
71 progressBar.setProgress(0);
Jason parks35933812011-01-21 15:48:20 -060072
Jason parksec5a45e2011-01-18 15:28:36 -060073 try {
74 int progress = Integer.parseInt(state);
75 progressBar.setProgress(progress);
76 } catch (Exception e) {
77 Log.w(TAG, "Error parsing progress: " + e.toString());
78 }
Jason parks39f1e042011-01-20 23:29:28 -060079
80 // Check the status every 5 second
81 sendEmptyMessageDelayed(UPDATE_PROGRESS, 5000);
Jason parksec5a45e2011-01-18 15:28:36 -060082 break;
Jason parks35933812011-01-21 15:48:20 -060083
Jason parksec5a45e2011-01-18 15:28:36 -060084 case COOLDOWN:
85 TextView tv = (TextView) findViewById(R.id.status);
86 if (mCooldown <= 0) {
87 // Re-enable the password entry
88 EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry);
89 passwordEntry.setEnabled(true);
Jason parks35933812011-01-21 15:48:20 -060090
Jason parksec5a45e2011-01-18 15:28:36 -060091 tv.setText(R.string.try_again);
Jason parks35933812011-01-21 15:48:20 -060092
Jason parksec5a45e2011-01-18 15:28:36 -060093 } else {
Jason parks35933812011-01-21 15:48:20 -060094
Jason parksec5a45e2011-01-18 15:28:36 -060095 CharSequence tempalte = getText(R.string.crypt_keeper_cooldown);
96 tv.setText(TextUtils.expandTemplate(tempalte, Integer.toString(mCooldown)));
Jason parks35933812011-01-21 15:48:20 -060097
Jason parksec5a45e2011-01-18 15:28:36 -060098 mCooldown--;
99 mHandler.sendEmptyMessageDelayed(COOLDOWN, 1000); // Tick every second
100 }
101 break;
102 }
103 }
104 };
Jason parks35933812011-01-21 15:48:20 -0600105
Jason parksec5a45e2011-01-18 15:28:36 -0600106 private int mFailedAttempts = 0;
107 private int mCooldown;
Jason parks35933812011-01-21 15:48:20 -0600108
Jason parks8fd5bc92011-01-12 16:03:31 -0600109 @Override
110 public void onCreate(Bundle savedInstanceState) {
111 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600112
Jason parks8fd5bc92011-01-12 16:03:31 -0600113 String state = SystemProperties.get("vold.decrypt");
114 if ("".equals(state) || DECRYPT_STATE.equals(state)) {
Jason parks35933812011-01-21 15:48:20 -0600115 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600116 PackageManager pm = getPackageManager();
117 ComponentName name = new ComponentName(this, CryptKeeper.class);
118 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
119 return;
120 }
Jason parks35933812011-01-21 15:48:20 -0600121
Jason parksec5a45e2011-01-18 15:28:36 -0600122 // Check to see why we were started.
123 String progress = SystemProperties.get("vold.encrypt_progress");
Jason parksdbf43222011-01-20 18:49:58 -0600124
125 if (!"".equals(progress)) {
Jason parksec5a45e2011-01-18 15:28:36 -0600126 setContentView(R.layout.crypt_keeper_progress);
127 encryptionProgressInit();
128 } else {
129 setContentView(R.layout.crypt_keeper_password_entry);
130 passwordEntryInit();
131 }
Jason parks39f1e042011-01-20 23:29:28 -0600132
133 // Disable the status bar
134 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
135 sbm.disable(StatusBarManager.DISABLE_EXPAND | StatusBarManager.DISABLE_NOTIFICATION_ICONS
136 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
137 | StatusBarManager.DISABLE_SYSTEM_INFO | StatusBarManager.DISABLE_NAVIGATION);
Jason parksec5a45e2011-01-18 15:28:36 -0600138 }
Jason parks35933812011-01-21 15:48:20 -0600139
Jason parksec5a45e2011-01-18 15:28:36 -0600140 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600141 // Accquire a partial wakelock to prevent the device from sleeping. Note
142 // we never release this wakelock as we will be restarted after the device
143 // is encrypted.
144
145 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
146 PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
147
148 wakeLock.acquire();
149
Jason parksec5a45e2011-01-18 15:28:36 -0600150 mHandler.sendEmptyMessage(UPDATE_PROGRESS);
151 }
Jason parks35933812011-01-21 15:48:20 -0600152
Jason parksec5a45e2011-01-18 15:28:36 -0600153 private void passwordEntryInit() {
Jason parks8fd5bc92011-01-12 16:03:31 -0600154 TextView passwordEntry = (TextView) findViewById(R.id.passwordEntry);
155 passwordEntry.setOnEditorActionListener(this);
Jason parks35933812011-01-21 15:48:20 -0600156
Jason parks8fd5bc92011-01-12 16:03:31 -0600157 KeyboardView keyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard);
Jason parks35933812011-01-21 15:48:20 -0600158
Jason parks8fd5bc92011-01-12 16:03:31 -0600159 PasswordEntryKeyboardHelper keyboardHelper = new PasswordEntryKeyboardHelper(this,
160 keyboardView, passwordEntry, false);
161 keyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA);
162
163
164 passwordEntry.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_idle_lock,
165 0, 0, 0);
166
167 String dateFormatString = getString(com.android.internal.R.string.full_wday_month_day_no_year);
168 TextView date = (TextView) findViewById(R.id.date);
169 date.setText(DateFormat.format(dateFormatString, new Date()));
Jason parks8fd5bc92011-01-12 16:03:31 -0600170 }
171
172 private IMountService getMountService() {
173 IBinder service = ServiceManager.getService("mount");
174 if (service != null) {
175 return IMountService.Stub.asInterface(service);
176 }
177 return null;
178 }
179
180 @Override
181 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
182 if (actionId == EditorInfo.IME_NULL) {
183 // Get the password
184 String password = v.getText().toString();
185
Jason parksec5a45e2011-01-18 15:28:36 -0600186 if (TextUtils.isEmpty(password)) {
187 return true;
188 }
Jason parks35933812011-01-21 15:48:20 -0600189
Jason parks8fd5bc92011-01-12 16:03:31 -0600190 // Now that we have the password clear the password field.
191 v.setText(null);
192
193 IMountService service = getMountService();
194 try {
195 service.decryptStorage(password);
196
Jason parks39f1e042011-01-20 23:29:28 -0600197 if (mFailedAttempts == 0) {
198 // Success. Do something here within 2 seconds
Jason parks8fd5bc92011-01-12 16:03:31 -0600199
Jason parks39f1e042011-01-20 23:29:28 -0600200 } else if (mFailedAttempts == MAX_FAILED_ATTEMPTS) {
Jason parksec5a45e2011-01-18 15:28:36 -0600201 // Factory reset the device.
202 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
203 } else if ((mFailedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
204 mCooldown = COOL_DOWN_INTERVAL;
205 EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry);
206 passwordEntry.setEnabled(false);
207 mHandler.sendEmptyMessage(COOLDOWN);
208 } else {
209 TextView tv = (TextView) findViewById(R.id.status);
210 tv.setText(R.string.try_again);
211 }
Jason parks8fd5bc92011-01-12 16:03:31 -0600212 } catch (Exception e) {
Jason parksec5a45e2011-01-18 15:28:36 -0600213 Log.e(TAG, "Error while decrypting...", e);
Jason parks8fd5bc92011-01-12 16:03:31 -0600214 }
Jason parks35933812011-01-21 15:48:20 -0600215
Jason parks8fd5bc92011-01-12 16:03:31 -0600216 return true;
217 }
218 return false;
219 }
220}