Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 1 | /* |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 18 | |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 19 | import com.android.internal.policy.IFaceLockCallback; |
| 20 | import com.android.internal.policy.IFaceLockInterface; |
| 21 | import com.android.internal.widget.LockPatternUtils; |
| 22 | |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 23 | import android.app.admin.DevicePolicyManager; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.ServiceConnection; |
| 28 | import android.os.Handler; |
| 29 | import android.os.IBinder; |
Brian Colonna | 22001c1 | 2012-05-09 11:16:04 -0400 | [diff] [blame] | 30 | import android.os.Looper; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 31 | import android.os.Message; |
Steven Ross | acbe41f | 2012-09-27 11:29:37 -0400 | [diff] [blame] | 32 | import android.os.PowerManager; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 33 | import android.os.RemoteException; |
Brian Colonna | 5eb83aa | 2012-11-19 16:05:48 -0500 | [diff] [blame] | 34 | import android.os.UserHandle; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.View; |
| 37 | |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 38 | public class FaceUnlock implements BiometricSensorUnlock, Handler.Callback { |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 39 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 40 | private static final boolean DEBUG = KeyguardConstants.DEBUG; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 41 | private static final String TAG = "FULLockscreen"; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 42 | private static final String FACE_LOCK_PACKAGE = "com.android.facelock"; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 43 | |
| 44 | private final Context mContext; |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 45 | private final LockPatternUtils mLockPatternUtils; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 46 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 47 | // TODO: is mServiceRunning needed or can we just use mIsRunning or check if mService is null? |
| 48 | private boolean mServiceRunning = false; |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 49 | // TODO: now that the code has been restructure to do almost all operations from a handler, this |
| 50 | // lock may no longer be necessary. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 51 | private final Object mServiceRunningLock = new Object(); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 52 | private IFaceLockInterface mService; |
| 53 | private boolean mBoundToService = false; |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 54 | private View mFaceUnlockView; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 55 | |
| 56 | private Handler mHandler; |
Danielle Millett | 61413b5 | 2012-10-09 18:07:02 -0400 | [diff] [blame] | 57 | private final int MSG_SERVICE_CONNECTED = 0; |
| 58 | private final int MSG_SERVICE_DISCONNECTED = 1; |
| 59 | private final int MSG_UNLOCK = 2; |
| 60 | private final int MSG_CANCEL = 3; |
| 61 | private final int MSG_REPORT_FAILED_ATTEMPT = 4; |
Brian Colonna | 667b5d5 | 2012-10-31 23:07:00 -0400 | [diff] [blame] | 62 | private final int MSG_POKE_WAKELOCK = 5; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 63 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 64 | // TODO: This was added for the purpose of adhering to what the biometric interface expects |
| 65 | // the isRunning() function to return. However, it is probably not necessary to have both |
| 66 | // mRunning and mServiceRunning. I'd just rather wait to change that logic. |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 67 | private volatile boolean mIsRunning = false; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 68 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 69 | // So the user has a consistent amount of time when brought to the backup method from Face |
| 70 | // Unlock |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 71 | private final int BACKUP_LOCK_TIMEOUT = 5000; |
| 72 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 73 | KeyguardSecurityCallback mKeyguardScreenCallback; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 74 | |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 75 | /** |
| 76 | * Stores some of the structures that Face Unlock will need to access and creates the handler |
| 77 | * will be used to execute messages on the UI thread. |
| 78 | */ |
Jim Miller | 000464a | 2012-09-04 16:45:06 -0700 | [diff] [blame] | 79 | public FaceUnlock(Context context) { |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 80 | mContext = context; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 81 | mLockPatternUtils = new LockPatternUtils(context); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 82 | mHandler = new Handler(this); |
| 83 | } |
| 84 | |
Jim Miller | 000464a | 2012-09-04 16:45:06 -0700 | [diff] [blame] | 85 | public void setKeyguardCallback(KeyguardSecurityCallback keyguardScreenCallback) { |
| 86 | mKeyguardScreenCallback = keyguardScreenCallback; |
| 87 | } |
| 88 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 89 | /** |
| 90 | * Stores and displays the view that Face Unlock is allowed to draw within. |
| 91 | * TODO: since the layout object will eventually be shared by multiple biometric unlock |
| 92 | * methods, we will have to add our other views (background, cancel button) here. |
| 93 | */ |
| 94 | public void initializeView(View biometricUnlockView) { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 95 | Log.d(TAG, "initializeView()"); |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 96 | mFaceUnlockView = biometricUnlockView; |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 99 | /** |
| 100 | * Indicates whether Face Unlock is currently running. |
| 101 | */ |
| 102 | public boolean isRunning() { |
| 103 | return mIsRunning; |
| 104 | } |
| 105 | |
| 106 | /** |
Danielle Millett | 61413b5 | 2012-10-09 18:07:02 -0400 | [diff] [blame] | 107 | * Dismisses face unlock and goes to the backup lock |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 108 | */ |
Danielle Millett | 61413b5 | 2012-10-09 18:07:02 -0400 | [diff] [blame] | 109 | public void stopAndShowBackup() { |
| 110 | if (DEBUG) Log.d(TAG, "stopAndShowBackup()"); |
| 111 | mHandler.sendEmptyMessage(MSG_CANCEL); |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Binds to the Face Unlock service. Face Unlock will be started when the bind completes. The |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 116 | * Face Unlock view is displayed to hide the backup lock while the service is starting up. |
Brian Colonna | 22001c1 | 2012-05-09 11:16:04 -0400 | [diff] [blame] | 117 | * Called on the UI thread. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 118 | */ |
| 119 | public boolean start() { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 120 | if (DEBUG) Log.d(TAG, "start()"); |
Brian Colonna | 22001c1 | 2012-05-09 11:16:04 -0400 | [diff] [blame] | 121 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 122 | Log.e(TAG, "start() called off of the UI thread"); |
| 123 | } |
| 124 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 125 | if (mIsRunning) { |
| 126 | Log.w(TAG, "start() called when already running"); |
| 127 | } |
| 128 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 129 | if (!mBoundToService) { |
Amith Yamasani | 4b4b954 | 2012-09-14 13:36:29 -0700 | [diff] [blame] | 130 | Log.d(TAG, "Binding to Face Unlock service for user=" |
| 131 | + mLockPatternUtils.getCurrentUser()); |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 132 | mContext.bindServiceAsUser( |
| 133 | new Intent(IFaceLockInterface.class.getName()).setPackage(FACE_LOCK_PACKAGE), |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 134 | mConnection, |
| 135 | Context.BIND_AUTO_CREATE, |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 136 | new UserHandle(mLockPatternUtils.getCurrentUser())); |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 137 | mBoundToService = true; |
| 138 | } else { |
| 139 | Log.w(TAG, "Attempt to bind to Face Unlock when already bound"); |
| 140 | } |
| 141 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 142 | mIsRunning = true; |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | /** |
Brian Colonna | 22001c1 | 2012-05-09 11:16:04 -0400 | [diff] [blame] | 147 | * Stops Face Unlock and unbinds from the service. Called on the UI thread. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 148 | */ |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 149 | public boolean stop() { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 150 | if (DEBUG) Log.d(TAG, "stop()"); |
Brian Colonna | 22001c1 | 2012-05-09 11:16:04 -0400 | [diff] [blame] | 151 | if (mHandler.getLooper() != Looper.myLooper()) { |
Jim Miller | a71984f | 2012-10-24 22:08:49 -0700 | [diff] [blame] | 152 | Log.e(TAG, "stop() called from non-UI thread"); |
Brian Colonna | 22001c1 | 2012-05-09 11:16:04 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Steven Ross | 025fb93 | 2012-11-05 13:49:31 -0500 | [diff] [blame] | 155 | // Clearing any old service connected messages. |
| 156 | mHandler.removeMessages(MSG_SERVICE_CONNECTED); |
| 157 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 158 | boolean mWasRunning = mIsRunning; |
Danielle Millett | 1108a2c | 2012-10-29 22:16:32 -0400 | [diff] [blame] | 159 | |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 160 | stopUi(); |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 161 | |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 162 | if (mBoundToService) { |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 163 | if (mService != null) { |
| 164 | try { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 165 | mService.unregisterCallback(mFaceUnlockCallback); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 166 | } catch (RemoteException e) { |
| 167 | // Not much we can do |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 168 | } |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 169 | } |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 170 | Log.d(TAG, "Unbinding from Face Unlock service"); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 171 | mContext.unbindService(mConnection); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 172 | mBoundToService = false; |
| 173 | } else { |
| 174 | // This is usually not an error when this happens. Sometimes we will tell it to |
| 175 | // unbind multiple times because it's called from both onWindowFocusChanged and |
| 176 | // onDetachedFromWindow. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 177 | if (DEBUG) Log.d(TAG, "Attempt to unbind from Face Unlock when not bound"); |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 178 | } |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 179 | mIsRunning = false; |
| 180 | return mWasRunning; |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 184 | * Frees up resources used by Face Unlock and stops it if it is still running. |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 185 | */ |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 186 | public void cleanUp() { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 187 | if (DEBUG) Log.d(TAG, "cleanUp()"); |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 188 | if (mService != null) { |
| 189 | try { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 190 | mService.unregisterCallback(mFaceUnlockCallback); |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 191 | } catch (RemoteException e) { |
| 192 | // Not much we can do |
| 193 | } |
| 194 | stopUi(); |
| 195 | mService = null; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 196 | } |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 199 | /** |
| 200 | * Returns the Device Policy Manager quality for Face Unlock, which is BIOMETRIC_WEAK. |
| 201 | */ |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 202 | public int getQuality() { |
| 203 | return DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 206 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 207 | * Handles messages such that everything happens on the UI thread in a deterministic order. |
| 208 | * Calls from the Face Unlock service come from binder threads. Calls from lockscreen typically |
| 209 | * come from the UI thread. This makes sure there are no race conditions between those calls. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 210 | */ |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 211 | public boolean handleMessage(Message msg) { |
| 212 | switch (msg.what) { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 213 | case MSG_SERVICE_CONNECTED: |
| 214 | handleServiceConnected(); |
| 215 | break; |
| 216 | case MSG_SERVICE_DISCONNECTED: |
| 217 | handleServiceDisconnected(); |
| 218 | break; |
| 219 | case MSG_UNLOCK: |
Brian Colonna | 5eb83aa | 2012-11-19 16:05:48 -0500 | [diff] [blame] | 220 | handleUnlock(msg.arg1); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 221 | break; |
| 222 | case MSG_CANCEL: |
| 223 | handleCancel(); |
| 224 | break; |
| 225 | case MSG_REPORT_FAILED_ATTEMPT: |
| 226 | handleReportFailedAttempt(); |
| 227 | break; |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 228 | case MSG_POKE_WAKELOCK: |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 229 | handlePokeWakelock(msg.arg1); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 230 | break; |
| 231 | default: |
| 232 | Log.e(TAG, "Unhandled message"); |
| 233 | return false; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 238 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 239 | * Tells the service to start its UI via an AIDL interface. Called when the |
| 240 | * onServiceConnected() callback is received. |
| 241 | */ |
| 242 | void handleServiceConnected() { |
Brian Colonna | c266070 | 2012-05-16 22:11:30 -0400 | [diff] [blame] | 243 | Log.d(TAG, "handleServiceConnected()"); |
| 244 | |
| 245 | // It is possible that an unbind has occurred in the time between the bind and when this |
| 246 | // function is reached. If an unbind has already occurred, proceeding on to call startUi() |
| 247 | // can result in a fatal error. Note that the onServiceConnected() callback is |
| 248 | // asynchronous, so this possibility would still exist if we executed this directly in |
| 249 | // onServiceConnected() rather than using a handler. |
| 250 | if (!mBoundToService) { |
| 251 | Log.d(TAG, "Dropping startUi() in handleServiceConnected() because no longer bound"); |
| 252 | return; |
| 253 | } |
| 254 | |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 255 | try { |
| 256 | mService.registerCallback(mFaceUnlockCallback); |
| 257 | } catch (RemoteException e) { |
| 258 | Log.e(TAG, "Caught exception connecting to Face Unlock: " + e.toString()); |
| 259 | mService = null; |
| 260 | mBoundToService = false; |
| 261 | mIsRunning = false; |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (mFaceUnlockView != null) { |
| 266 | IBinder windowToken = mFaceUnlockView.getWindowToken(); |
| 267 | if (windowToken != null) { |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 268 | // When switching between portrait and landscape view while Face Unlock is running, |
| 269 | // the screen will eventually go dark unless we poke the wakelock when Face Unlock |
| 270 | // is restarted. |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 271 | mKeyguardScreenCallback.userActivity(0); |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 272 | |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 273 | int[] position; |
| 274 | position = new int[2]; |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 275 | mFaceUnlockView.getLocationInWindow(position); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 276 | startUi(windowToken, position[0], position[1], mFaceUnlockView.getWidth(), |
| 277 | mFaceUnlockView.getHeight()); |
| 278 | } else { |
| 279 | Log.e(TAG, "windowToken is null in handleServiceConnected()"); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Called when the onServiceDisconnected() callback is received. This should not happen during |
| 286 | * normal operation. It indicates an error has occurred. |
| 287 | */ |
| 288 | void handleServiceDisconnected() { |
| 289 | Log.e(TAG, "handleServiceDisconnected()"); |
| 290 | // TODO: this lock may no longer be needed now that everything is being called from a |
| 291 | // handler |
| 292 | synchronized (mServiceRunningLock) { |
| 293 | mService = null; |
| 294 | mServiceRunning = false; |
| 295 | } |
| 296 | mBoundToService = false; |
| 297 | mIsRunning = false; |
| 298 | } |
| 299 | |
| 300 | /** |
Danielle Millett | 61413b5 | 2012-10-09 18:07:02 -0400 | [diff] [blame] | 301 | * Stops the Face Unlock service and tells the device to grant access to the user. |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 302 | */ |
Brian Colonna | 5eb83aa | 2012-11-19 16:05:48 -0500 | [diff] [blame] | 303 | void handleUnlock(int authenticatedUserId) { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 304 | if (DEBUG) Log.d(TAG, "handleUnlock()"); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 305 | stop(); |
Brian Colonna | 5eb83aa | 2012-11-19 16:05:48 -0500 | [diff] [blame] | 306 | int currentUserId = mLockPatternUtils.getCurrentUser(); |
| 307 | if (authenticatedUserId == currentUserId) { |
| 308 | if (DEBUG) Log.d(TAG, "Unlocking for user " + authenticatedUserId); |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 309 | mKeyguardScreenCallback.reportUnlockAttempt(true); |
Brian Colonna | 5eb83aa | 2012-11-19 16:05:48 -0500 | [diff] [blame] | 310 | mKeyguardScreenCallback.dismiss(true); |
| 311 | } else { |
| 312 | Log.d(TAG, "Ignoring unlock for authenticated user (" + authenticatedUserId + |
| 313 | ") because the current user is " + currentUserId); |
| 314 | } |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Danielle Millett | 61413b5 | 2012-10-09 18:07:02 -0400 | [diff] [blame] | 318 | * Stops the Face Unlock service and goes to the backup lock. |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 319 | */ |
| 320 | void handleCancel() { |
| 321 | if (DEBUG) Log.d(TAG, "handleCancel()"); |
Brian Colonna | 667b5d5 | 2012-10-31 23:07:00 -0400 | [diff] [blame] | 322 | // We are going to the backup method, so we don't want to see Face Unlock again until the |
| 323 | // next time the user visits keyguard. |
| 324 | KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(false); |
| 325 | |
Danielle Millett | 61413b5 | 2012-10-09 18:07:02 -0400 | [diff] [blame] | 326 | mKeyguardScreenCallback.showBackupSecurity(); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 327 | stop(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 328 | mKeyguardScreenCallback.userActivity(BACKUP_LOCK_TIMEOUT); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
Brian Colonna | 7019325 | 2012-06-04 16:16:46 -0400 | [diff] [blame] | 332 | * Increments the number of failed Face Unlock attempts. |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 333 | */ |
| 334 | void handleReportFailedAttempt() { |
| 335 | if (DEBUG) Log.d(TAG, "handleReportFailedAttempt()"); |
Brian Colonna | 667b5d5 | 2012-10-31 23:07:00 -0400 | [diff] [blame] | 336 | // We are going to the backup method, so we don't want to see Face Unlock again until the |
| 337 | // next time the user visits keyguard. |
| 338 | KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(false); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 339 | |
Jim Miller | 7751ff6 | 2014-01-14 18:57:03 -0800 | [diff] [blame] | 340 | mKeyguardScreenCallback.reportUnlockAttempt(false); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Steven Ross | acbe41f | 2012-09-27 11:29:37 -0400 | [diff] [blame] | 344 | * If the screen is on, pokes the wakelock to keep the screen alive and active for a specific |
| 345 | * amount of time. |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 346 | */ |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 347 | void handlePokeWakelock(int millis) { |
Steven Ross | acbe41f | 2012-09-27 11:29:37 -0400 | [diff] [blame] | 348 | PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 349 | if (powerManager.isScreenOn()) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 350 | mKeyguardScreenCallback.userActivity(millis); |
Steven Ross | acbe41f | 2012-09-27 11:29:37 -0400 | [diff] [blame] | 351 | } |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 355 | * Implements service connection methods. |
| 356 | */ |
| 357 | private ServiceConnection mConnection = new ServiceConnection() { |
| 358 | /** |
| 359 | * Called when the Face Unlock service connects after calling bind(). |
| 360 | */ |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 361 | public void onServiceConnected(ComponentName className, IBinder iservice) { |
| 362 | Log.d(TAG, "Connected to Face Unlock service"); |
| 363 | mService = IFaceLockInterface.Stub.asInterface(iservice); |
| 364 | mHandler.sendEmptyMessage(MSG_SERVICE_CONNECTED); |
| 365 | } |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 366 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 367 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 368 | * Called if the Face Unlock service unexpectedly disconnects. This indicates an error. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 369 | */ |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 370 | public void onServiceDisconnected(ComponentName className) { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 371 | Log.e(TAG, "Unexpected disconnect from Face Unlock service"); |
| 372 | mHandler.sendEmptyMessage(MSG_SERVICE_DISCONNECTED); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 373 | } |
| 374 | }; |
| 375 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 376 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 377 | * Tells the Face Unlock service to start displaying its UI and start processing. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 378 | */ |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 379 | private void startUi(IBinder windowToken, int x, int y, int w, int h) { |
Brian Colonna | c266070 | 2012-05-16 22:11:30 -0400 | [diff] [blame] | 380 | if (DEBUG) Log.d(TAG, "startUi()"); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 381 | synchronized (mServiceRunningLock) { |
| 382 | if (!mServiceRunning) { |
Brian Colonna | c266070 | 2012-05-16 22:11:30 -0400 | [diff] [blame] | 383 | Log.d(TAG, "Starting Face Unlock"); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 384 | try { |
Uriel Rodriguez | 4fa995a | 2012-05-30 20:01:38 -0400 | [diff] [blame] | 385 | mService.startUi(windowToken, x, y, w, h, |
| 386 | mLockPatternUtils.isBiometricWeakLivelinessEnabled()); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 387 | } catch (RemoteException e) { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 388 | Log.e(TAG, "Caught exception starting Face Unlock: " + e.toString()); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 389 | return; |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 390 | } |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 391 | mServiceRunning = true; |
| 392 | } else { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 393 | Log.w(TAG, "startUi() attempted while running"); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 398 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 399 | * Tells the Face Unlock service to stop displaying its UI and stop processing. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 400 | */ |
Brian Colonna | 3223e25 | 2012-04-11 11:12:37 -0400 | [diff] [blame] | 401 | private void stopUi() { |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 402 | if (DEBUG) Log.d(TAG, "stopUi()"); |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 403 | // Note that attempting to stop Face Unlock when it's not running is not an issue. |
| 404 | // Face Unlock can return, which stops it and then we try to stop it when the |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 405 | // screen is turned off. That's why we check. |
| 406 | synchronized (mServiceRunningLock) { |
| 407 | if (mServiceRunning) { |
Brian Colonna | c266070 | 2012-05-16 22:11:30 -0400 | [diff] [blame] | 408 | Log.d(TAG, "Stopping Face Unlock"); |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 409 | try { |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 410 | mService.stopUi(); |
| 411 | } catch (RemoteException e) { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 412 | Log.e(TAG, "Caught exception stopping Face Unlock: " + e.toString()); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 413 | } |
Brian Colonna | c169366 | 2012-04-19 11:34:04 -0400 | [diff] [blame] | 414 | mServiceRunning = false; |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 415 | } else { |
| 416 | // This is usually not an error when this happens. Sometimes we will tell it to |
| 417 | // stop multiple times because it's called from both onWindowFocusChanged and |
| 418 | // onDetachedFromWindow. |
| 419 | if (DEBUG) Log.d(TAG, "stopUi() attempted while not running"); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 424 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 425 | * Implements the AIDL biometric unlock service callback interface. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 426 | */ |
| 427 | private final IFaceLockCallback mFaceUnlockCallback = new IFaceLockCallback.Stub() { |
| 428 | /** |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 429 | * Called when Face Unlock wants to grant access to the user. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 430 | */ |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 431 | public void unlock() { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 432 | if (DEBUG) Log.d(TAG, "unlock()"); |
Brian Colonna | 5eb83aa | 2012-11-19 16:05:48 -0500 | [diff] [blame] | 433 | Message message = mHandler.obtainMessage(MSG_UNLOCK, UserHandle.getCallingUserId(), -1); |
| 434 | mHandler.sendMessage(message); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 437 | /** |
Brian Colonna | 7019325 | 2012-06-04 16:16:46 -0400 | [diff] [blame] | 438 | * Called when Face Unlock wants to go to the backup. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 439 | */ |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 440 | public void cancel() { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 441 | if (DEBUG) Log.d(TAG, "cancel()"); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 442 | mHandler.sendEmptyMessage(MSG_CANCEL); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 445 | /** |
Brian Colonna | 7019325 | 2012-06-04 16:16:46 -0400 | [diff] [blame] | 446 | * Called when Face Unlock wants to increment the number of failed attempts. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 447 | */ |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 448 | public void reportFailedAttempt() { |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 449 | if (DEBUG) Log.d(TAG, "reportFailedAttempt()"); |
Brian Colonna | 257f2ec | 2012-04-27 13:14:22 -0400 | [diff] [blame] | 450 | mHandler.sendEmptyMessage(MSG_REPORT_FAILED_ATTEMPT); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 453 | /** |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 454 | * Called when Face Unlock wants to keep the screen alive and active for a specific amount |
| 455 | * of time. |
Brian Colonna | ea8441e | 2012-04-25 17:51:49 -0400 | [diff] [blame] | 456 | */ |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 457 | public void pokeWakelock(int millis) { |
| 458 | if (DEBUG) Log.d(TAG, "pokeWakelock() for " + millis + "ms"); |
| 459 | Message message = mHandler.obtainMessage(MSG_POKE_WAKELOCK, millis, -1); |
| 460 | mHandler.sendMessage(message); |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 461 | } |
Uriel Rodriguez | dff3076 | 2012-05-09 16:10:41 -0400 | [diff] [blame] | 462 | |
Brian Colonna | 9431366 | 2012-04-06 13:01:29 -0400 | [diff] [blame] | 463 | }; |
| 464 | } |