blob: 5afc3f614e2e4784a27aeac5761bbc9606ee46f3 [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
Jason parksf1dbf552011-01-24 16:19:28 -060060 // This activity is used to fade the screen to black after the password is entered.
61 public static class Blank extends Activity {
62 }
Jason parksec5a45e2011-01-18 15:28:36 -060063
64 private Handler mHandler = new Handler() {
65 @Override
66 public void handleMessage(Message msg) {
Jason parks35933812011-01-21 15:48:20 -060067
Jason parksec5a45e2011-01-18 15:28:36 -060068 switch (msg.what) {
Jason parks35933812011-01-21 15:48:20 -060069
Jason parksec5a45e2011-01-18 15:28:36 -060070 case UPDATE_PROGRESS:
71 String state = SystemProperties.get("vold.encrypt_progress");
Jason parks35933812011-01-21 15:48:20 -060072
Jason parksec5a45e2011-01-18 15:28:36 -060073 ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
74 progressBar.setProgress(0);
Jason parks35933812011-01-21 15:48:20 -060075
Jason parksec5a45e2011-01-18 15:28:36 -060076 try {
77 int progress = Integer.parseInt(state);
78 progressBar.setProgress(progress);
79 } catch (Exception e) {
80 Log.w(TAG, "Error parsing progress: " + e.toString());
81 }
Jason parks39f1e042011-01-20 23:29:28 -060082
83 // Check the status every 5 second
84 sendEmptyMessageDelayed(UPDATE_PROGRESS, 5000);
Jason parksec5a45e2011-01-18 15:28:36 -060085 break;
Jason parks35933812011-01-21 15:48:20 -060086
Jason parksec5a45e2011-01-18 15:28:36 -060087 case COOLDOWN:
88 TextView tv = (TextView) findViewById(R.id.status);
89 if (mCooldown <= 0) {
90 // Re-enable the password entry
91 EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry);
92 passwordEntry.setEnabled(true);
Jason parks35933812011-01-21 15:48:20 -060093
Jason parksec5a45e2011-01-18 15:28:36 -060094 tv.setText(R.string.try_again);
Jason parks35933812011-01-21 15:48:20 -060095
Jason parksec5a45e2011-01-18 15:28:36 -060096 } else {
Jason parks35933812011-01-21 15:48:20 -060097
Jason parksec5a45e2011-01-18 15:28:36 -060098 CharSequence tempalte = getText(R.string.crypt_keeper_cooldown);
99 tv.setText(TextUtils.expandTemplate(tempalte, Integer.toString(mCooldown)));
Jason parks35933812011-01-21 15:48:20 -0600100
Jason parksec5a45e2011-01-18 15:28:36 -0600101 mCooldown--;
102 mHandler.sendEmptyMessageDelayed(COOLDOWN, 1000); // Tick every second
103 }
104 break;
105 }
106 }
107 };
Jason parks35933812011-01-21 15:48:20 -0600108
Jason parksec5a45e2011-01-18 15:28:36 -0600109 private int mCooldown;
Jason parks35933812011-01-21 15:48:20 -0600110
Jason parks8fd5bc92011-01-12 16:03:31 -0600111 @Override
112 public void onCreate(Bundle savedInstanceState) {
113 super.onCreate(savedInstanceState);
Jason parks35933812011-01-21 15:48:20 -0600114
Jason parks8fd5bc92011-01-12 16:03:31 -0600115 String state = SystemProperties.get("vold.decrypt");
116 if ("".equals(state) || DECRYPT_STATE.equals(state)) {
Jason parks35933812011-01-21 15:48:20 -0600117 // Disable the crypt keeper.
Jason parks8fd5bc92011-01-12 16:03:31 -0600118 PackageManager pm = getPackageManager();
119 ComponentName name = new ComponentName(this, CryptKeeper.class);
120 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
121 return;
122 }
Jason parks35933812011-01-21 15:48:20 -0600123
Jason parksec5a45e2011-01-18 15:28:36 -0600124 // Check to see why we were started.
125 String progress = SystemProperties.get("vold.encrypt_progress");
Jason parksdbf43222011-01-20 18:49:58 -0600126
127 if (!"".equals(progress)) {
Jason parksec5a45e2011-01-18 15:28:36 -0600128 setContentView(R.layout.crypt_keeper_progress);
129 encryptionProgressInit();
130 } else {
131 setContentView(R.layout.crypt_keeper_password_entry);
132 passwordEntryInit();
133 }
Jason parks39f1e042011-01-20 23:29:28 -0600134
135 // Disable the status bar
136 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
137 sbm.disable(StatusBarManager.DISABLE_EXPAND | StatusBarManager.DISABLE_NOTIFICATION_ICONS
138 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
139 | StatusBarManager.DISABLE_SYSTEM_INFO | StatusBarManager.DISABLE_NAVIGATION);
Jason parksec5a45e2011-01-18 15:28:36 -0600140 }
Jason parks35933812011-01-21 15:48:20 -0600141
Jason parksec5a45e2011-01-18 15:28:36 -0600142 private void encryptionProgressInit() {
Jason parks35933812011-01-21 15:48:20 -0600143 // Accquire a partial wakelock to prevent the device from sleeping. Note
144 // we never release this wakelock as we will be restarted after the device
145 // is encrypted.
146
147 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Jason parksf1dbf552011-01-24 16:19:28 -0600148 PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG);
Jason parks35933812011-01-21 15:48:20 -0600149
150 wakeLock.acquire();
151
Jason parksec5a45e2011-01-18 15:28:36 -0600152 mHandler.sendEmptyMessage(UPDATE_PROGRESS);
153 }
Jason parks35933812011-01-21 15:48:20 -0600154
Jason parksec5a45e2011-01-18 15:28:36 -0600155 private void passwordEntryInit() {
Jason parks8fd5bc92011-01-12 16:03:31 -0600156 TextView passwordEntry = (TextView) findViewById(R.id.passwordEntry);
157 passwordEntry.setOnEditorActionListener(this);
Jason parks35933812011-01-21 15:48:20 -0600158
Jason parks8fd5bc92011-01-12 16:03:31 -0600159 KeyboardView keyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard);
Jason parks35933812011-01-21 15:48:20 -0600160
Jason parks8fd5bc92011-01-12 16:03:31 -0600161 PasswordEntryKeyboardHelper keyboardHelper = new PasswordEntryKeyboardHelper(this,
162 keyboardView, passwordEntry, false);
163 keyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA);
164
165
166 passwordEntry.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_idle_lock,
167 0, 0, 0);
168
169 String dateFormatString = getString(com.android.internal.R.string.full_wday_month_day_no_year);
170 TextView date = (TextView) findViewById(R.id.date);
171 date.setText(DateFormat.format(dateFormatString, new Date()));
Jason parks8fd5bc92011-01-12 16:03:31 -0600172 }
173
174 private IMountService getMountService() {
175 IBinder service = ServiceManager.getService("mount");
176 if (service != null) {
177 return IMountService.Stub.asInterface(service);
178 }
179 return null;
180 }
181
182 @Override
183 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
184 if (actionId == EditorInfo.IME_NULL) {
185 // Get the password
186 String password = v.getText().toString();
187
Jason parksec5a45e2011-01-18 15:28:36 -0600188 if (TextUtils.isEmpty(password)) {
189 return true;
190 }
Jason parks35933812011-01-21 15:48:20 -0600191
Jason parks8fd5bc92011-01-12 16:03:31 -0600192 // Now that we have the password clear the password field.
193 v.setText(null);
194
195 IMountService service = getMountService();
196 try {
Jason parksf1dbf552011-01-24 16:19:28 -0600197 int failedAttempts = service.decryptStorage(password);
Jason parks8fd5bc92011-01-12 16:03:31 -0600198
Jason parksf1dbf552011-01-24 16:19:28 -0600199 if (failedAttempts == 0) {
200 // The password was entered successfully. Start the Blank activity
201 // so this activity animates to black before the devices starts. Note
202 // It has 1 second to complete the animation or it will be frozen
203 // until the boot animation comes back up.
204 Intent intent = new Intent(this, Blank.class);
205 finish();
206 startActivity(intent);
207 } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
Jason parksec5a45e2011-01-18 15:28:36 -0600208 // Factory reset the device.
209 sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
Jason parksf1dbf552011-01-24 16:19:28 -0600210 } else if ((failedAttempts % COOL_DOWN_ATTEMPTS) == 0) {
Jason parksec5a45e2011-01-18 15:28:36 -0600211 mCooldown = COOL_DOWN_INTERVAL;
212 EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry);
213 passwordEntry.setEnabled(false);
214 mHandler.sendEmptyMessage(COOLDOWN);
215 } else {
216 TextView tv = (TextView) findViewById(R.id.status);
217 tv.setText(R.string.try_again);
218 }
Jason parks8fd5bc92011-01-12 16:03:31 -0600219 } catch (Exception e) {
Jason parksec5a45e2011-01-18 15:28:36 -0600220 Log.e(TAG, "Error while decrypting...", e);
Jason parks8fd5bc92011-01-12 16:03:31 -0600221 }
Jason parks35933812011-01-21 15:48:20 -0600222
Jason parks8fd5bc92011-01-12 16:03:31 -0600223 return true;
224 }
225 return false;
226 }
227}