KeyguardAccountView.java revision 6dbf8610e94c1b41e0124577101d6369fd204ad3
1/* 2 * Copyright (C) 2012 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 */ 16package com.android.internal.policy.impl.keyguard; 17 18import android.accounts.Account; 19import android.accounts.AccountManager; 20import android.accounts.AccountManagerCallback; 21import android.accounts.AccountManagerFuture; 22import android.accounts.AuthenticatorException; 23import android.accounts.OperationCanceledException; 24import android.app.Dialog; 25import android.app.ProgressDialog; 26import android.content.Context; 27import android.content.Intent; 28import android.graphics.Rect; 29import android.os.Bundle; 30import android.text.Editable; 31import android.text.InputFilter; 32import android.text.LoginFilter; 33import android.text.TextWatcher; 34import android.util.AttributeSet; 35import android.view.KeyEvent; 36import android.view.View; 37import android.view.WindowManager; 38import android.widget.Button; 39import android.widget.EditText; 40import android.widget.LinearLayout; 41 42import com.android.internal.widget.LockPatternUtils; 43import com.android.internal.R; 44 45import java.io.IOException; 46 47/** 48 * When the user forgets their password a bunch of times, we fall back on their 49 * account's login/password to unlock the phone (and reset their lock pattern). 50 */ 51public class KeyguardAccountView extends LinearLayout implements KeyguardSecurityView, 52 View.OnClickListener, TextWatcher { 53 private static final int AWAKE_POKE_MILLIS = 30000; 54 private static final String LOCK_PATTERN_PACKAGE = "com.android.settings"; 55 private static final String LOCK_PATTERN_CLASS = LOCK_PATTERN_PACKAGE + ".ChooseLockGeneric"; 56 57 private KeyguardSecurityCallback mCallback; 58 private LockPatternUtils mLockPatternUtils; 59 private EditText mLogin; 60 private EditText mPassword; 61 private Button mOk; 62 public boolean mEnableFallback; 63 private SecurityMessageDisplay mSecurityMessageDisplay; 64 65 /** 66 * Shown while making asynchronous check of password. 67 */ 68 private ProgressDialog mCheckingDialog; 69 70 public KeyguardAccountView(Context context) { 71 this(context, null, 0); 72 } 73 74 public KeyguardAccountView(Context context, AttributeSet attrs) { 75 this(context, attrs, 0); 76 } 77 78 public KeyguardAccountView(Context context, AttributeSet attrs, int defStyle) { 79 super(context, attrs, defStyle); 80 mLockPatternUtils = new LockPatternUtils(getContext()); 81 } 82 83 @Override 84 protected void onFinishInflate() { 85 super.onFinishInflate(); 86 87 // We always set a dummy NavigationManager to avoid null checks 88 mSecurityMessageDisplay = new KeyguardNavigationManager(null); 89 90 mLogin = (EditText) findViewById(R.id.login); 91 mLogin.setFilters(new InputFilter[] { new LoginFilter.UsernameFilterGeneric() } ); 92 mLogin.addTextChangedListener(this); 93 94 mPassword = (EditText) findViewById(R.id.password); 95 mPassword.addTextChangedListener(this); 96 97 mOk = (Button) findViewById(R.id.ok); 98 mOk.setOnClickListener(this); 99 reset(); 100 } 101 102 public void setKeyguardCallback(KeyguardSecurityCallback callback) { 103 mCallback = callback; 104 } 105 106 public void setLockPatternUtils(LockPatternUtils utils) { 107 mLockPatternUtils = utils; 108 } 109 110 public KeyguardSecurityCallback getCallback() { 111 return mCallback; 112 } 113 114 115 public void afterTextChanged(Editable s) { 116 } 117 118 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 119 } 120 121 public void onTextChanged(CharSequence s, int start, int before, int count) { 122 if (mCallback != null) { 123 mCallback.userActivity(AWAKE_POKE_MILLIS); 124 } 125 } 126 127 @Override 128 protected boolean onRequestFocusInDescendants(int direction, 129 Rect previouslyFocusedRect) { 130 // send focus to the login field 131 return mLogin.requestFocus(direction, previouslyFocusedRect); 132 } 133 134 public boolean needsInput() { 135 return true; 136 } 137 138 public void reset() { 139 // start fresh 140 mLogin.setText(""); 141 mPassword.setText(""); 142 mLogin.requestFocus(); 143 boolean permLocked = mLockPatternUtils.isPermanentlyLocked(); 144 mSecurityMessageDisplay.setMessage(permLocked ? R.string.kg_login_too_many_attempts : 145 R.string.kg_login_instructions, permLocked ? true : false); 146 } 147 148 /** {@inheritDoc} */ 149 public void cleanUp() { 150 if (mCheckingDialog != null) { 151 mCheckingDialog.hide(); 152 } 153 mCallback = null; 154 mLockPatternUtils = null; 155 } 156 157 public void onClick(View v) { 158 mCallback.userActivity(0); 159 if (v == mOk) { 160 asyncCheckPassword(); 161 } 162 } 163 164 private void postOnCheckPasswordResult(final boolean success) { 165 // ensure this runs on UI thread 166 mLogin.post(new Runnable() { 167 public void run() { 168 if (success) { 169 // clear out forgotten password 170 mLockPatternUtils.setPermanentlyLocked(false); 171 mLockPatternUtils.setLockPatternEnabled(false); 172 mLockPatternUtils.saveLockPattern(null); 173 174 // launch the 'choose lock pattern' activity so 175 // the user can pick a new one if they want to 176 Intent intent = new Intent(); 177 intent.setClassName(LOCK_PATTERN_PACKAGE, LOCK_PATTERN_CLASS); 178 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 179 mContext.startActivity(intent); 180 mCallback.reportSuccessfulUnlockAttempt(); 181 182 // dismiss keyguard 183 mCallback.dismiss(true); 184 } else { 185 mSecurityMessageDisplay.setMessage(R.string.kg_login_invalid_input, true); 186 mPassword.setText(""); 187 mCallback.reportFailedUnlockAttempt(); 188 } 189 } 190 }); 191 } 192 193 @Override 194 public boolean dispatchKeyEvent(KeyEvent event) { 195 if (event.getAction() == KeyEvent.ACTION_DOWN 196 && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { 197 if (mLockPatternUtils.isPermanentlyLocked()) { 198 mCallback.dismiss(false); 199 } else { 200 // TODO: mCallback.forgotPattern(false); 201 } 202 return true; 203 } 204 return super.dispatchKeyEvent(event); 205 } 206 207 /** 208 * Given the string the user entered in the 'username' field, find 209 * the stored account that they probably intended. Prefer, in order: 210 * 211 * - an exact match for what was typed, or 212 * - a case-insensitive match for what was typed, or 213 * - if they didn't include a domain, an exact match of the username, or 214 * - if they didn't include a domain, a case-insensitive 215 * match of the username. 216 * 217 * If there is a tie for the best match, choose neither -- 218 * the user needs to be more specific. 219 * 220 * @return an account name from the database, or null if we can't 221 * find a single best match. 222 */ 223 private Account findIntendedAccount(String username) { 224 Account[] accounts = AccountManager.get(mContext).getAccountsByType("com.google"); 225 226 // Try to figure out which account they meant if they 227 // typed only the username (and not the domain), or got 228 // the case wrong. 229 230 Account bestAccount = null; 231 int bestScore = 0; 232 for (Account a: accounts) { 233 int score = 0; 234 if (username.equals(a.name)) { 235 score = 4; 236 } else if (username.equalsIgnoreCase(a.name)) { 237 score = 3; 238 } else if (username.indexOf('@') < 0) { 239 int i = a.name.indexOf('@'); 240 if (i >= 0) { 241 String aUsername = a.name.substring(0, i); 242 if (username.equals(aUsername)) { 243 score = 2; 244 } else if (username.equalsIgnoreCase(aUsername)) { 245 score = 1; 246 } 247 } 248 } 249 if (score > bestScore) { 250 bestAccount = a; 251 bestScore = score; 252 } else if (score == bestScore) { 253 bestAccount = null; 254 } 255 } 256 return bestAccount; 257 } 258 259 private void asyncCheckPassword() { 260 mCallback.userActivity(AWAKE_POKE_MILLIS); 261 final String login = mLogin.getText().toString(); 262 final String password = mPassword.getText().toString(); 263 Account account = findIntendedAccount(login); 264 if (account == null) { 265 postOnCheckPasswordResult(false); 266 return; 267 } 268 getProgressDialog().show(); 269 Bundle options = new Bundle(); 270 options.putString(AccountManager.KEY_PASSWORD, password); 271 AccountManager.get(mContext).confirmCredentials(account, options, null /* activity */, 272 new AccountManagerCallback<Bundle>() { 273 public void run(AccountManagerFuture<Bundle> future) { 274 try { 275 mCallback.userActivity(AWAKE_POKE_MILLIS); 276 final Bundle result = future.getResult(); 277 final boolean verified = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); 278 postOnCheckPasswordResult(verified); 279 } catch (OperationCanceledException e) { 280 postOnCheckPasswordResult(false); 281 } catch (IOException e) { 282 postOnCheckPasswordResult(false); 283 } catch (AuthenticatorException e) { 284 postOnCheckPasswordResult(false); 285 } finally { 286 mLogin.post(new Runnable() { 287 public void run() { 288 getProgressDialog().hide(); 289 } 290 }); 291 } 292 } 293 }, null /* handler */); 294 } 295 296 private Dialog getProgressDialog() { 297 if (mCheckingDialog == null) { 298 mCheckingDialog = new ProgressDialog(mContext); 299 mCheckingDialog.setMessage( 300 mContext.getString(R.string.kg_login_checking_password)); 301 mCheckingDialog.setIndeterminate(true); 302 mCheckingDialog.setCancelable(false); 303 mCheckingDialog.getWindow().setType( 304 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); 305 } 306 return mCheckingDialog; 307 } 308 309 @Override 310 public void onPause() { 311 312 } 313 314 @Override 315 public void onResume() { 316 reset(); 317 } 318 319 @Override 320 public void setSecurityMessageDisplay(SecurityMessageDisplay display) { 321 mSecurityMessageDisplay = display; 322 reset(); 323 } 324} 325 326