Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import com.android.internal.widget.PasswordEntryKeyboardHelper; |
| 20 | import com.android.internal.widget.PasswordEntryKeyboardView; |
| 21 | |
| 22 | import android.app.Activity; |
| 23 | import android.app.StatusBarManager; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 26 | import android.content.Intent; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
| 28 | import android.inputmethodservice.KeyboardView; |
| 29 | import android.os.Bundle; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 30 | import android.os.Handler; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 31 | import android.os.IBinder; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 32 | import android.os.Message; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 33 | import android.os.ServiceManager; |
| 34 | import android.os.SystemProperties; |
| 35 | import android.os.storage.IMountService; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 36 | import android.text.TextUtils; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 37 | import android.text.format.DateFormat; |
| 38 | import android.util.Log; |
| 39 | import android.view.KeyEvent; |
| 40 | import android.view.inputmethod.EditorInfo; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 41 | import android.widget.EditText; |
| 42 | import android.widget.ProgressBar; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 43 | import android.widget.TextView; |
| 44 | |
| 45 | import java.util.Date; |
| 46 | |
| 47 | public class CryptKeeper extends Activity implements TextView.OnEditorActionListener { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 48 | private static final String TAG = "CryptKeeper"; |
| 49 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 50 | private static final String DECRYPT_STATE = "trigger_restart_framework"; |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 51 | |
| 52 | private static final int UPDATE_PROGRESS = 1; |
| 53 | private static final int COOLDOWN = 2; |
| 54 | |
| 55 | private static final int MAX_FAILED_ATTEMPTS = 30; |
| 56 | private static final int COOL_DOWN_ATTEMPTS = 10; |
| 57 | private static final int COOL_DOWN_INTERVAL = 30; // 30 seconds |
| 58 | |
| 59 | |
| 60 | private Handler mHandler = new Handler() { |
| 61 | @Override |
| 62 | public void handleMessage(Message msg) { |
| 63 | |
| 64 | switch (msg.what) { |
| 65 | |
| 66 | case UPDATE_PROGRESS: |
| 67 | String state = SystemProperties.get("vold.encrypt_progress"); |
| 68 | |
| 69 | ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); |
| 70 | progressBar.setProgress(0); |
| 71 | |
| 72 | try { |
| 73 | int progress = Integer.parseInt(state); |
| 74 | progressBar.setProgress(progress); |
| 75 | } catch (Exception e) { |
| 76 | Log.w(TAG, "Error parsing progress: " + e.toString()); |
| 77 | } |
Jason parks | 39f1e04 | 2011-01-20 23:29:28 -0600 | [diff] [blame^] | 78 | |
| 79 | // Check the status every 5 second |
| 80 | sendEmptyMessageDelayed(UPDATE_PROGRESS, 5000); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 81 | break; |
| 82 | |
| 83 | case COOLDOWN: |
| 84 | TextView tv = (TextView) findViewById(R.id.status); |
| 85 | if (mCooldown <= 0) { |
| 86 | // Re-enable the password entry |
| 87 | EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry); |
| 88 | passwordEntry.setEnabled(true); |
| 89 | |
| 90 | tv.setText(R.string.try_again); |
| 91 | |
| 92 | } else { |
| 93 | |
| 94 | CharSequence tempalte = getText(R.string.crypt_keeper_cooldown); |
| 95 | tv.setText(TextUtils.expandTemplate(tempalte, Integer.toString(mCooldown))); |
| 96 | |
| 97 | mCooldown--; |
| 98 | mHandler.sendEmptyMessageDelayed(COOLDOWN, 1000); // Tick every second |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | private int mFailedAttempts = 0; |
| 106 | private int mCooldown; |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public void onCreate(Bundle savedInstanceState) { |
| 110 | super.onCreate(savedInstanceState); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 111 | |
| 112 | String state = SystemProperties.get("vold.decrypt"); |
| 113 | if ("".equals(state) || DECRYPT_STATE.equals(state)) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 114 | // Disable the crypt keeper. |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 115 | PackageManager pm = getPackageManager(); |
| 116 | ComponentName name = new ComponentName(this, CryptKeeper.class); |
| 117 | pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0); |
| 118 | return; |
| 119 | } |
| 120 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 121 | // Check to see why we were started. |
| 122 | String progress = SystemProperties.get("vold.encrypt_progress"); |
Jason parks | dbf4322 | 2011-01-20 18:49:58 -0600 | [diff] [blame] | 123 | |
| 124 | if (!"".equals(progress)) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 125 | setContentView(R.layout.crypt_keeper_progress); |
| 126 | encryptionProgressInit(); |
| 127 | } else { |
| 128 | setContentView(R.layout.crypt_keeper_password_entry); |
| 129 | passwordEntryInit(); |
| 130 | } |
Jason parks | 39f1e04 | 2011-01-20 23:29:28 -0600 | [diff] [blame^] | 131 | |
| 132 | // Disable the status bar |
| 133 | StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE); |
| 134 | sbm.disable(StatusBarManager.DISABLE_EXPAND | StatusBarManager.DISABLE_NOTIFICATION_ICONS |
| 135 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS |
| 136 | | StatusBarManager.DISABLE_SYSTEM_INFO | StatusBarManager.DISABLE_NAVIGATION); |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private void encryptionProgressInit() { |
| 140 | mHandler.sendEmptyMessage(UPDATE_PROGRESS); |
| 141 | } |
| 142 | |
| 143 | private void passwordEntryInit() { |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 144 | TextView passwordEntry = (TextView) findViewById(R.id.passwordEntry); |
| 145 | passwordEntry.setOnEditorActionListener(this); |
| 146 | |
| 147 | KeyboardView keyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard); |
| 148 | |
| 149 | PasswordEntryKeyboardHelper keyboardHelper = new PasswordEntryKeyboardHelper(this, |
| 150 | keyboardView, passwordEntry, false); |
| 151 | keyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA); |
| 152 | |
| 153 | |
| 154 | passwordEntry.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_idle_lock, |
| 155 | 0, 0, 0); |
| 156 | |
| 157 | String dateFormatString = getString(com.android.internal.R.string.full_wday_month_day_no_year); |
| 158 | TextView date = (TextView) findViewById(R.id.date); |
| 159 | date.setText(DateFormat.format(dateFormatString, new Date())); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private IMountService getMountService() { |
| 163 | IBinder service = ServiceManager.getService("mount"); |
| 164 | if (service != null) { |
| 165 | return IMountService.Stub.asInterface(service); |
| 166 | } |
| 167 | return null; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 172 | if (actionId == EditorInfo.IME_NULL) { |
| 173 | // Get the password |
| 174 | String password = v.getText().toString(); |
| 175 | |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 176 | if (TextUtils.isEmpty(password)) { |
| 177 | return true; |
| 178 | } |
| 179 | |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 180 | // Now that we have the password clear the password field. |
| 181 | v.setText(null); |
| 182 | |
| 183 | IMountService service = getMountService(); |
| 184 | try { |
| 185 | service.decryptStorage(password); |
| 186 | |
Jason parks | 39f1e04 | 2011-01-20 23:29:28 -0600 | [diff] [blame^] | 187 | if (mFailedAttempts == 0) { |
| 188 | // Success. Do something here within 2 seconds |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 189 | |
Jason parks | 39f1e04 | 2011-01-20 23:29:28 -0600 | [diff] [blame^] | 190 | } else if (mFailedAttempts == MAX_FAILED_ATTEMPTS) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 191 | // Factory reset the device. |
| 192 | sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); |
| 193 | } else if ((mFailedAttempts % COOL_DOWN_ATTEMPTS) == 0) { |
| 194 | mCooldown = COOL_DOWN_INTERVAL; |
| 195 | EditText passwordEntry = (EditText) findViewById(R.id.passwordEntry); |
| 196 | passwordEntry.setEnabled(false); |
| 197 | mHandler.sendEmptyMessage(COOLDOWN); |
| 198 | } else { |
| 199 | TextView tv = (TextView) findViewById(R.id.status); |
| 200 | tv.setText(R.string.try_again); |
| 201 | } |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 202 | } catch (Exception e) { |
Jason parks | ec5a45e | 2011-01-18 15:28:36 -0600 | [diff] [blame] | 203 | Log.e(TAG, "Error while decrypting...", e); |
Jason parks | 8fd5bc9 | 2011-01-12 16:03:31 -0600 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | } |