Reworked biometric weak check functions

Renamed isBiometricEnabled to isBiometricWeakInstalled.  This function
now checks if the system property is set, the facelock package is
installed, and if the phone has a front facing camera.  It no longer
checks if facelock is currently set as the unlock method.
Added isBiometricWeakInstalled checks to all cases where facelock is used
in LockPatternKeyguardView

Change-Id: Ia86a7ad6118101c6aab90ffb2ee9c42bf2548149
diff --git a/core/java/com/android/internal/widget/LockPatternUtils.java b/core/java/com/android/internal/widget/LockPatternUtils.java
index 4a38775..a793abb 100644
--- a/core/java/com/android/internal/widget/LockPatternUtils.java
+++ b/core/java/com/android/internal/widget/LockPatternUtils.java
@@ -24,11 +24,15 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.hardware.Camera;
+import android.hardware.Camera.CameraInfo;
 import android.os.FileObserver;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemClock;
+import android.os.SystemProperties;
 import android.os.storage.IMountService;
 import android.provider.Settings;
 import android.security.KeyStore;
@@ -434,7 +438,7 @@
      * Calls back SetupFaceLock to delete the gallery file when the lock type is changed
     */
     void deleteGallery() {
-        if(isBiometricEnabled()) {
+        if(usingBiometricWeak()) {
             Intent intent = new Intent().setClassName("com.android.facelock",
                     "com.android.facelock.SetupFaceLock");
             intent.putExtra("deleteGallery", true);
@@ -677,6 +681,9 @@
         return quality;
     }
 
+    /**
+     * @return true if the lockscreen method is set to biometric weak
+     */
     public boolean usingBiometricWeak() {
         int quality =
                 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
@@ -810,7 +817,7 @@
                 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
 
         return savedPasswordExists() && (passwordEnabled ||
-                (isBiometricEnabled() && backupEnabled));
+                (usingBiometricWeak() && backupEnabled));
     }
 
     /**
@@ -824,16 +831,36 @@
         return getBoolean(Settings.Secure.LOCK_PATTERN_ENABLED)
                 && (getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
                         == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING ||
-                        (isBiometricEnabled() && backupEnabled));
+                        (usingBiometricWeak() && backupEnabled));
     }
 
     /**
-     * @return Whether biometric weak lock is enabled.
+     * @return Whether biometric weak lock is installed and that the front facing camera exists
      */
-    public boolean isBiometricEnabled() {
-        // TODO: check if it's installed
-        return getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
-                == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK;
+    public boolean isBiometricWeakInstalled() {
+        // Check that the system flag was set
+        if (!SystemProperties.getBoolean("ro.lockscreen.facelock_enabled", false)) {
+            return false;
+        }
+
+        // Check that it's installed
+        PackageManager pm = mContext.getPackageManager();
+        try {
+            pm.getPackageInfo("com.android.facelock", PackageManager.GET_ACTIVITIES);
+        } catch (PackageManager.NameNotFoundException e) {
+            return false;
+        }
+
+        // Check that the camera is enabled
+        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
+            return false;
+        }
+        if (getDevicePolicyManager().getCameraDisabled(null)) {
+            return false;
+        }
+
+
+        return true;
     }
 
     /**
diff --git a/policy/src/com/android/internal/policy/impl/LockPatternKeyguardView.java b/policy/src/com/android/internal/policy/impl/LockPatternKeyguardView.java
index f970ff3..ca5d274 100644
--- a/policy/src/com/android/internal/policy/impl/LockPatternKeyguardView.java
+++ b/policy/src/com/android/internal/policy/impl/LockPatternKeyguardView.java
@@ -531,7 +531,8 @@
             ((KeyguardScreen) mUnlockScreen).onResume();
         }
 
-        if (mLockPatternUtils.usingBiometricWeak()) {
+        if (mLockPatternUtils.usingBiometricWeak() &&
+                mLockPatternUtils.isBiometricWeakInstalled()) {
             mHandler.sendEmptyMessage(MSG_SHOW_FACELOCK_AREA_VIEW);
         } else {
             mHandler.sendEmptyMessage(MSG_HIDE_FACELOCK_AREA_VIEW);
@@ -1014,7 +1015,8 @@
 
     // Binds to FaceLock service, but does not tell it to start
     public void bindToFaceLock() {
-        if (mLockPatternUtils.usingBiometricWeak()) {
+        if (mLockPatternUtils.usingBiometricWeak() &&
+                mLockPatternUtils.isBiometricWeakInstalled()) {
             if (!mBoundToFaceLockService) {
                 if (DEBUG) Log.d(TAG, "before bind to FaceLock service");
                 mContext.bindService(new Intent(IFaceLockInterface.class.getName()),
@@ -1030,7 +1032,8 @@
 
     // Tells FaceLock to stop and then unbinds from the FaceLock service
     public void stopAndUnbindFromFaceLock() {
-        if (mLockPatternUtils.usingBiometricWeak()) {
+        if (mLockPatternUtils.usingBiometricWeak() &&
+                mLockPatternUtils.isBiometricWeakInstalled()) {
             stopFaceLock();
 
             if (mBoundToFaceLockService) {
@@ -1079,7 +1082,8 @@
     // Tells the FaceLock service to start displaying its UI and perform recognition
     public void startFaceLock(IBinder windowToken, int x, int y, int h, int w)
     {
-        if (mLockPatternUtils.usingBiometricWeak()) {
+        if (mLockPatternUtils.usingBiometricWeak() &&
+                mLockPatternUtils.isBiometricWeakInstalled()) {
             synchronized (mFaceLockServiceRunningLock) {
                 if (!mFaceLockServiceRunning) {
                     if (DEBUG) Log.d(TAG, "Starting FaceLock");
@@ -1099,7 +1103,8 @@
     // Tells the FaceLock service to stop displaying its UI and stop recognition
     public void stopFaceLock()
     {
-        if (mLockPatternUtils.usingBiometricWeak()) {
+        if (mLockPatternUtils.usingBiometricWeak() &&
+                mLockPatternUtils.isBiometricWeakInstalled()) {
             // Note that attempting to stop FaceLock when it's not running is not an issue.
             // FaceLock can return, which stops it and then we try to stop it when the
             // screen is turned off.  That's why we check.