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