FaceUnlock.java revision dff307697d7a7482efe4c10cb3b07b9249524a42
19431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna/* 29431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * Copyright (C) 2011 The Android Open Source Project 39431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * 49431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * Licensed under the Apache License, Version 2.0 (the "License"); 59431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * you may not use this file except in compliance with the License. 69431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * You may obtain a copy of the License at 79431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * 89431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * http://www.apache.org/licenses/LICENSE-2.0 99431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * 109431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * Unless required by applicable law or agreed to in writing, software 119431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * distributed under the License is distributed on an "AS IS" BASIS, 129431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * See the License for the specific language governing permissions and 149431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna * limitations under the License. 159431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna */ 169431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 179431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnapackage com.android.internal.policy.impl; 189431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 199431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport com.android.internal.R; 209431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport com.android.internal.policy.IFaceLockCallback; 219431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport com.android.internal.policy.IFaceLockInterface; 229431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport com.android.internal.widget.LockPatternUtils; 239431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 243223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonnaimport android.app.admin.DevicePolicyManager; 259431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.content.ComponentName; 269431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.content.Context; 279431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.content.Intent; 289431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.content.ServiceConnection; 299431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.os.Handler; 309431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.os.IBinder; 3122001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonnaimport android.os.Looper; 329431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.os.Message; 339431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.os.RemoteException; 349431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.telephony.TelephonyManager; 359431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.util.Log; 369431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonnaimport android.view.View; 379431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 383223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonnapublic class FaceUnlock implements BiometricSensorUnlock, Handler.Callback { 399431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 409431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private static final boolean DEBUG = false; 419431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private static final String TAG = "FULLockscreen"; 429431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 439431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private final Context mContext; 44ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna private final LockPatternUtils mLockPatternUtils; 459431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private final KeyguardUpdateMonitor mUpdateMonitor; 469431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 47ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // TODO: is mServiceRunning needed or can we just use mIsRunning or check if mService is null? 48ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna private boolean mServiceRunning = false; 49257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // TODO: now that the code has been restructure to do almost all operations from a handler, this 50257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // lock may no longer be necessary. 51ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna private final Object mServiceRunningLock = new Object(); 529431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private IFaceLockInterface mService; 539431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private boolean mBoundToService = false; 54ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna private View mFaceUnlockView; 559431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 569431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private Handler mHandler; 57257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_SHOW_FACE_UNLOCK_VIEW = 0; 58257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_HIDE_FACE_UNLOCK_VIEW = 1; 59257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_SERVICE_CONNECTED = 2; 60257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_SERVICE_DISCONNECTED = 3; 61257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_UNLOCK = 4; 62257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_CANCEL = 5; 63257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_REPORT_FAILED_ATTEMPT = 6; 64257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_EXPOSE_FALLBACK = 7; 65257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int MSG_POKE_WAKELOCK = 8; 669431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 67ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // TODO: This was added for the purpose of adhering to what the biometric interface expects 68ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // the isRunning() function to return. However, it is probably not necessary to have both 69ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // mRunning and mServiceRunning. I'd just rather wait to change that logic. 70257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private volatile boolean mIsRunning = false; 719431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 729431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna // Long enough to stay visible while the service starts 739431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna // Short enough to not have to wait long for backup if service fails to start or crashes 749431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna // The service can take a couple of seconds to start on the first try after boot 75257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private final int SERVICE_STARTUP_VIEW_TIMEOUT = 3000; 769431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 77ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // So the user has a consistent amount of time when brought to the backup method from Face 78ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // Unlock 799431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private final int BACKUP_LOCK_TIMEOUT = 5000; 809431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 819431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna KeyguardScreenCallback mKeyguardScreenCallback; 829431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 83257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 84257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Stores some of the structures that Face Unlock will need to access and creates the handler 85257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * will be used to execute messages on the UI thread. 86257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 879431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public FaceUnlock(Context context, KeyguardUpdateMonitor updateMonitor, 889431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna LockPatternUtils lockPatternUtils, KeyguardScreenCallback keyguardScreenCallback) { 899431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna mContext = context; 909431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna mUpdateMonitor = updateMonitor; 919431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna mLockPatternUtils = lockPatternUtils; 929431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna mKeyguardScreenCallback = keyguardScreenCallback; 939431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna mHandler = new Handler(this); 949431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 959431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 96ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 97ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Stores and displays the view that Face Unlock is allowed to draw within. 98ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * TODO: since the layout object will eventually be shared by multiple biometric unlock 99ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * methods, we will have to add our other views (background, cancel button) here. 100ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 101ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna public void initializeView(View biometricUnlockView) { 102257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.d(TAG, "initializeView()"); 103ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mFaceUnlockView = biometricUnlockView; 104ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna show(0); 105ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } 106ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna 107ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 108ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Indicates whether Face Unlock is currently running. 109ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 1103223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna public boolean isRunning() { 111ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna return mIsRunning; 1123223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } 1133223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna 114ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 115ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Sets the Face Unlock view to visible, hiding it after the specified amount of time. If 11622001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna * timeoutMillis is 0, no hide is performed. Called on the UI thread. 117ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 1183223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna public void show(long timeoutMillis) { 119257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "show()"); 12022001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna if (mHandler.getLooper() != Looper.myLooper()) { 12122001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna Log.e(TAG, "show() called off of the UI thread"); 12222001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna } 12322001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna 124257f2ecc97d294e95b069547466d2054926d960fBrian Colonna removeDisplayMessages(); 125ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (mFaceUnlockView != null) { 126ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mFaceUnlockView.setVisibility(View.VISIBLE); 127ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } 128ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (timeoutMillis > 0) { 129257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessageDelayed(MSG_HIDE_FACE_UNLOCK_VIEW, timeoutMillis); 130ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } 1313223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } 1323223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna 133ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 134ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Hides the Face Unlock view. 135ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 1363223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna public void hide() { 137257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "hide()"); 1383223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna // Remove messages to prevent a delayed show message from undo-ing the hide 139257f2ecc97d294e95b069547466d2054926d960fBrian Colonna removeDisplayMessages(); 140257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_HIDE_FACE_UNLOCK_VIEW); 1413223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } 1423223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna 143ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 144ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Binds to the Face Unlock service. Face Unlock will be started when the bind completes. The 145257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Face Unlock view is displayed to hide the backup lock while the service is starting up. 14622001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna * Called on the UI thread. 147ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 148ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna public boolean start() { 149257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "start()"); 15022001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna if (mHandler.getLooper() != Looper.myLooper()) { 15122001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna Log.e(TAG, "start() called off of the UI thread"); 15222001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna } 15322001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna 154ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (mIsRunning) { 155ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna Log.w(TAG, "start() called when already running"); 156ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } 157ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna 158ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // Show Face Unlock view, but only for a little bit so lockpattern will become visible if 159ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // Face Unlock fails to start or crashes 160ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // This must show before bind to guarantee that Face Unlock has a place to display 161257f2ecc97d294e95b069547466d2054926d960fBrian Colonna show(SERVICE_STARTUP_VIEW_TIMEOUT); 162ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (!mBoundToService) { 163257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.d(TAG, "Binding to Face Unlock service"); 164ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mContext.bindService(new Intent(IFaceLockInterface.class.getName()), 165ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mConnection, 166ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna Context.BIND_AUTO_CREATE, 167ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mLockPatternUtils.getCurrentUser()); 168ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mBoundToService = true; 169ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } else { 170ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna Log.w(TAG, "Attempt to bind to Face Unlock when already bound"); 171ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } 172ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna 173ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mIsRunning = true; 174ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna return true; 175ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna } 176ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna 177ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 17822001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna * Stops Face Unlock and unbinds from the service. Called on the UI thread. 179ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 1803223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna public boolean stop() { 181257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "stop()"); 18222001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna if (mHandler.getLooper() != Looper.myLooper()) { 18322001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna Log.e(TAG, "stop() called off of the UI thread"); 18422001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna } 18522001c1f96984655f7205ecbc93dd982c47b4e97Brian Colonna 186ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna boolean mWasRunning = mIsRunning; 187c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna stopUi(); 188c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna 189c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna if (mBoundToService) { 190c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna if (mService != null) { 191c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna try { 192ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mService.unregisterCallback(mFaceUnlockCallback); 193c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna } catch (RemoteException e) { 194c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna // Not much we can do 1953223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } 1969431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 197257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.d(TAG, "Unbinding from Face Unlock service"); 198c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mContext.unbindService(mConnection); 199c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mBoundToService = false; 200c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna } else { 201c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna // This is usually not an error when this happens. Sometimes we will tell it to 202c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna // unbind multiple times because it's called from both onWindowFocusChanged and 203c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna // onDetachedFromWindow. 204ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (DEBUG) Log.d(TAG, "Attempt to unbind from Face Unlock when not bound"); 2059431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 206ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mIsRunning = false; 207ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna return mWasRunning; 2089431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 2099431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 2103223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna /** 211ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Frees up resources used by Face Unlock and stops it if it is still running. 2123223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna */ 2133223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna public void cleanUp() { 214257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "cleanUp()"); 2153223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna if (mService != null) { 2163223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna try { 217ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna mService.unregisterCallback(mFaceUnlockCallback); 2183223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } catch (RemoteException e) { 2193223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna // Not much we can do 2203223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } 2213223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna stopUi(); 2223223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna mService = null; 2239431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 2243223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna } 2253223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna 226ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 227ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna * Returns the Device Policy Manager quality for Face Unlock, which is BIOMETRIC_WEAK. 228ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 2293223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna public int getQuality() { 2303223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna return DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK; 2319431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 2329431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 233ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 234257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Handles messages such that everything happens on the UI thread in a deterministic order. 235257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Calls from the Face Unlock service come from binder threads. Calls from lockscreen typically 236257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * come from the UI thread. This makes sure there are no race conditions between those calls. 237ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 2389431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 2399431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public boolean handleMessage(Message msg) { 2409431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna switch (msg.what) { 241257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_SHOW_FACE_UNLOCK_VIEW: 242257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleShowFaceUnlockView(); 243257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 244257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_HIDE_FACE_UNLOCK_VIEW: 245257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleHideFaceUnlockView(); 246257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 247257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_SERVICE_CONNECTED: 248257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleServiceConnected(); 249257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 250257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_SERVICE_DISCONNECTED: 251257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleServiceDisconnected(); 252257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 253257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_UNLOCK: 254257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleUnlock(); 255257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 256257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_CANCEL: 257257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleCancel(); 258257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 259257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_REPORT_FAILED_ATTEMPT: 260257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleReportFailedAttempt(); 261257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 262257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_EXPOSE_FALLBACK: 263257f2ecc97d294e95b069547466d2054926d960fBrian Colonna handleExposeFallback(); 264257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 265257f2ecc97d294e95b069547466d2054926d960fBrian Colonna case MSG_POKE_WAKELOCK: 266dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez handlePokeWakelock(msg.arg1); 267257f2ecc97d294e95b069547466d2054926d960fBrian Colonna break; 268257f2ecc97d294e95b069547466d2054926d960fBrian Colonna default: 269257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "Unhandled message"); 270257f2ecc97d294e95b069547466d2054926d960fBrian Colonna return false; 2719431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 2729431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna return true; 2739431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 2749431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 275ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 276257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Sets the Face Unlock view to visible, thus covering the backup lock. 277257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 278257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleShowFaceUnlockView() { 279257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleShowFaceUnlockView()"); 280257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 281257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.setVisibility(View.VISIBLE); 282257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 283257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "mFaceUnlockView is null in handleShowFaceUnlockView()"); 284257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 285257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 286257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 287257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 288257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Sets the Face Unlock view to invisible, thus exposing the backup lock. 289257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 290257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleHideFaceUnlockView() { 291257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleHideFaceUnlockView()"); 292257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 293257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.setVisibility(View.INVISIBLE); 294257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 295257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "mFaceUnlockView is null in handleHideFaceUnlockView()"); 296257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 297257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 298257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 299257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 300257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Tells the service to start its UI via an AIDL interface. Called when the 301257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * onServiceConnected() callback is received. 302257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 303257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleServiceConnected() { 304257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleServiceConnected()"); 305257f2ecc97d294e95b069547466d2054926d960fBrian Colonna try { 306257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mService.registerCallback(mFaceUnlockCallback); 307257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } catch (RemoteException e) { 308257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "Caught exception connecting to Face Unlock: " + e.toString()); 309257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mService = null; 310257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mBoundToService = false; 311257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mIsRunning = false; 312257f2ecc97d294e95b069547466d2054926d960fBrian Colonna return; 313257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 314257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 315257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 316257f2ecc97d294e95b069547466d2054926d960fBrian Colonna IBinder windowToken = mFaceUnlockView.getWindowToken(); 317257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (windowToken != null) { 318dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez // When switching between portrait and landscape view while Face Unlock is running, 319dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez // the screen will eventually go dark unless we poke the wakelock when Face Unlock 320dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez // is restarted. 321dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez mKeyguardScreenCallback.pokeWakelock(); 322dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez 323257f2ecc97d294e95b069547466d2054926d960fBrian Colonna int[] position; 324257f2ecc97d294e95b069547466d2054926d960fBrian Colonna position = new int[2]; 325257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.getLocationInWindow(position); 326257f2ecc97d294e95b069547466d2054926d960fBrian Colonna startUi(windowToken, position[0], position[1], mFaceUnlockView.getWidth(), 327257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.getHeight()); 328257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 329257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "windowToken is null in handleServiceConnected()"); 330257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 331257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 332257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 333257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 334257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 335257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called when the onServiceDisconnected() callback is received. This should not happen during 336257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * normal operation. It indicates an error has occurred. 337257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 338257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleServiceDisconnected() { 339257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "handleServiceDisconnected()"); 340257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // TODO: this lock may no longer be needed now that everything is being called from a 341257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // handler 342257f2ecc97d294e95b069547466d2054926d960fBrian Colonna synchronized (mServiceRunningLock) { 343257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mService = null; 344257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mServiceRunning = false; 345257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 346257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mBoundToService = false; 347257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mIsRunning = false; 348257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 349257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 350257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 351257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Stops the Face Unlock service and tells the device to grant access to the user. Shows the 352257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Face Unlock view to keep the backup lock covered while the device unlocks. 353257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 354257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleUnlock() { 355257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleUnlock()"); 356257f2ecc97d294e95b069547466d2054926d960fBrian Colonna removeDisplayMessages(); 357257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 358257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.setVisibility(View.VISIBLE); 359257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 360257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "mFaceUnlockView is null in handleUnlock()"); 361257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 362257f2ecc97d294e95b069547466d2054926d960fBrian Colonna stop(); 363257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mKeyguardScreenCallback.keyguardDone(true); 364257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mKeyguardScreenCallback.reportSuccessfulUnlockAttempt(); 365257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 366257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 367257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 368257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Stops the Face Unlock service and exposes the backup lock. Called when the user presses the 369dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez * cancel button to skip Face Unlock, no face is detected or there is an error. 370257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 371257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleCancel() { 372257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleCancel()"); 373257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 374257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.setVisibility(View.INVISIBLE); 375257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 376257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "mFaceUnlockView is null in handleCancel()"); 377257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 378257f2ecc97d294e95b069547466d2054926d960fBrian Colonna stop(); 379257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mKeyguardScreenCallback.pokeWakelock(BACKUP_LOCK_TIMEOUT); 380257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 381257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 382257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 383257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Stops the Face Unlock service and exposes the backup lock, reporting a failed unlock attempt. 384257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called when Face Unlock denies access to the user. 385257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 386257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleReportFailedAttempt() { 387257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleReportFailedAttempt()"); 388257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mUpdateMonitor.reportFailedBiometricUnlockAttempt(); 389257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 390257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.setVisibility(View.INVISIBLE); 391257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 392257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "mFaceUnlockView is null in handleReportFailedAttempt()"); 393257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 394257f2ecc97d294e95b069547466d2054926d960fBrian Colonna stop(); 395257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mKeyguardScreenCallback.pokeWakelock(BACKUP_LOCK_TIMEOUT); 396257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 397257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 398257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 399257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Hides the Face Unlock view to expose the backup lock. Called when the Face Unlock service UI 400257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * is started, indicating there is no need to continue displaying the underlying view because 401257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * the service UI is now covering the backup lock. 402257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 403257f2ecc97d294e95b069547466d2054926d960fBrian Colonna void handleExposeFallback() { 404257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "handleExposeFallback()"); 405257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (mFaceUnlockView != null) { 406257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mFaceUnlockView.setVisibility(View.INVISIBLE); 407257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 408257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "mFaceUnlockView is null in handleExposeFallback()"); 409257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 410257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 411257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 412257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 413dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez * Pokes the wakelock to keep the screen alive and active for a specific amount of time. 414257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 415dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez void handlePokeWakelock(int millis) { 416dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez mKeyguardScreenCallback.pokeWakelock(millis); 417257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } 418257f2ecc97d294e95b069547466d2054926d960fBrian Colonna 419257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 420257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Removes show and hide messages from the message queue. Called to prevent delayed show/hide 421257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * messages from undoing a new message. 422ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 423257f2ecc97d294e95b069547466d2054926d960fBrian Colonna private void removeDisplayMessages() { 424257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.removeMessages(MSG_SHOW_FACE_UNLOCK_VIEW); 425257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.removeMessages(MSG_HIDE_FACE_UNLOCK_VIEW); 4269431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4279431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 428257f2ecc97d294e95b069547466d2054926d960fBrian Colonna /** 429257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Implements service connection methods. 430257f2ecc97d294e95b069547466d2054926d960fBrian Colonna */ 4319431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna private ServiceConnection mConnection = new ServiceConnection() { 432ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 433257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called when the Face Unlock service connects after calling bind(). 434ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 4359431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 4369431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public void onServiceConnected(ComponentName className, IBinder iservice) { 437257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.d(TAG, "Connected to Face Unlock service"); 4389431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna mService = IFaceLockInterface.Stub.asInterface(iservice); 439257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_SERVICE_CONNECTED); 4409431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4419431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 442ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 443257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called if the Face Unlock service unexpectedly disconnects. This indicates an error. 444ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 4459431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 4469431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public void onServiceDisconnected(ComponentName className) { 447257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.e(TAG, "Unexpected disconnect from Face Unlock service"); 448257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_SERVICE_DISCONNECTED); 4499431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4509431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna }; 4519431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 452ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 453257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Tells the Face Unlock service to start displaying its UI and start processing. 454ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 4553223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna private void startUi(IBinder windowToken, int x, int y, int w, int h) { 456257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "startUi()"); 457c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna synchronized (mServiceRunningLock) { 458c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna if (!mServiceRunning) { 459ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (DEBUG) Log.d(TAG, "Starting Face Unlock"); 460c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna try { 461c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mService.startUi(windowToken, x, y, w, h, 462c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mLockPatternUtils.isBiometricWeakLivelinessEnabled()); 463c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna } catch (RemoteException e) { 464ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna Log.e(TAG, "Caught exception starting Face Unlock: " + e.toString()); 465c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna return; 4669431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 467c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mServiceRunning = true; 468c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna } else { 469257f2ecc97d294e95b069547466d2054926d960fBrian Colonna Log.w(TAG, "startUi() attempted while running"); 4709431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4719431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4729431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4739431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 474ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 475257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Tells the Face Unlock service to stop displaying its UI and stop processing. 476ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 4773223e2537d5f4e2eceeb321405dbd6da50df66b6Brian Colonna private void stopUi() { 478257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "stopUi()"); 479ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // Note that attempting to stop Face Unlock when it's not running is not an issue. 480ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna // Face Unlock can return, which stops it and then we try to stop it when the 481c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna // screen is turned off. That's why we check. 482c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna synchronized (mServiceRunningLock) { 483c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna if (mServiceRunning) { 484257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "Stopping Face Unlock"); 485c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna try { 486c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mService.stopUi(); 487c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna } catch (RemoteException e) { 488ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna Log.e(TAG, "Caught exception stopping Face Unlock: " + e.toString()); 4899431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 490c169366f2cc44fc395dba76c763505ac2bd61640Brian Colonna mServiceRunning = false; 491257f2ecc97d294e95b069547466d2054926d960fBrian Colonna } else { 492257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // This is usually not an error when this happens. Sometimes we will tell it to 493257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // stop multiple times because it's called from both onWindowFocusChanged and 494257f2ecc97d294e95b069547466d2054926d960fBrian Colonna // onDetachedFromWindow. 495257f2ecc97d294e95b069547466d2054926d960fBrian Colonna if (DEBUG) Log.d(TAG, "stopUi() attempted while not running"); 4969431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4979431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4989431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 4999431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 500ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 501257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Implements the AIDL biometric unlock service callback interface. 502ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 503ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna private final IFaceLockCallback mFaceUnlockCallback = new IFaceLockCallback.Stub() { 504ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 505257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called when Face Unlock wants to grant access to the user. 506ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 5079431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 5089431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public void unlock() { 509ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (DEBUG) Log.d(TAG, "unlock()"); 510257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_UNLOCK); 5119431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 5129431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 513ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 514dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez * Called when the user presses cancel to skip Face Unlock, a face cannot be found or 515dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez * there is an error. 516ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 5179431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 5189431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public void cancel() { 519ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (DEBUG) Log.d(TAG, "cancel()"); 520257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_CANCEL); 5219431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 5229431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 523ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 524257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called when Face Unlock denies access to the user. 525ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 5269431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 5279431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public void reportFailedAttempt() { 528ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (DEBUG) Log.d(TAG, "reportFailedAttempt()"); 529257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_REPORT_FAILED_ATTEMPT); 5309431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 5319431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 532ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 533257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * Called when the Face Unlock service starts displaying the UI, indicating that the backup 534257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * unlock can be exposed because the Face Unlock service is now covering the backup with its 535257f2ecc97d294e95b069547466d2054926d960fBrian Colonna * UI. 536ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna **/ 5379431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna @Override 5389431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna public void exposeFallback() { 539ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna if (DEBUG) Log.d(TAG, "exposeFallback()"); 540257f2ecc97d294e95b069547466d2054926d960fBrian Colonna mHandler.sendEmptyMessage(MSG_EXPOSE_FALLBACK); 5419431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 5429431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna 543ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna /** 544dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez * Called when Face Unlock wants to keep the screen alive and active for a specific amount 545dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez * of time. 546ea8441e22a4316cb6e78dd8bf461d3e658545b64Brian Colonna */ 547dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez public void pokeWakelock(int millis) { 548dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez if (DEBUG) Log.d(TAG, "pokeWakelock() for " + millis + "ms"); 549dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez Message message = mHandler.obtainMessage(MSG_POKE_WAKELOCK, millis, -1); 550dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez mHandler.sendMessage(message); 5519431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna } 552dff307697d7a7482efe4c10cb3b07b9249524a42Uriel Rodriguez 5539431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna }; 5549431366ecb4b6e4a87c0047c36548aa0bc23f2b1Brian Colonna} 555