Grey out App Check setting when no verifiers exist

Bug: 7082362
Change-Id: I6e8a6032b58145fe6e2918f364ffd92cbb97ed67
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ba3f386..42f4198 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2385,9 +2385,9 @@
  from unknown sources. You agree that you are solely responsible for any
  damage to your phone or loss of data that may result from using
  these apps.</string>
-    <!-- Applications settings screen, setting check box title. If checked, the system will send package verification requests to package verifiers on the device who will ultimately allow or reject the installation of applications. -->
+    <!-- Applications settings screen, setting check box title. If checked, the system will send package verification requests to package verifiers on the device who will ultimately allow or reject the installation of applications. [CHAR LIMIT=30] -->
     <string name="verify_applications">App check</string>
-    <!-- Applications settings screen, setting check box summary. This is the summary for "App Check" checkbox. -->
+    <!-- Applications settings screen, setting check box summary. This is the summary for "App Check" checkbox. [CHAR LIMIT=none] -->
     <string name="verify_applications_summary">Disallow or warn before installation of apps that may cause harm</string>
     <!-- Applications settings screen, setting check box title. If checked, applications show more settings options. -->
     <string name="advanced_settings">Advanced settings</string>
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index 248dcad..e309c37 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -25,6 +25,9 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.net.Uri;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.Vibrator;
@@ -43,6 +46,7 @@
 import com.android.internal.widget.LockPatternUtils;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Gesture lock pattern settings.
@@ -71,6 +75,7 @@
     private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications";
     private static final String KEY_TOGGLE_VERIFY_APPLICATIONS = "toggle_verify_applications";
     private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
+    private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
 
     DevicePolicyManager mDPM;
 
@@ -228,9 +233,13 @@
         mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
 
         // Package verification
-        mToggleVerifyApps = (CheckBoxPreference) findPreference(
-            KEY_TOGGLE_VERIFY_APPLICATIONS);
-        mToggleVerifyApps.setChecked(isVerifyAppsEnabled());
+        mToggleVerifyApps = (CheckBoxPreference) findPreference(KEY_TOGGLE_VERIFY_APPLICATIONS);
+        if (isVerifierInstalled()) {
+            mToggleVerifyApps.setChecked(isVerifyAppsEnabled());
+        } else {
+            mToggleVerifyApps.setChecked(false);
+            mToggleVerifyApps.setEnabled(false);
+        }
 
         return root;
     }
@@ -251,6 +260,15 @@
                                       Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) > 0;
     }
 
+    private boolean isVerifierInstalled() {
+        final PackageManager pm = getPackageManager();
+        final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
+        verification.setType(PACKAGE_MIME_TYPE);
+        verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+        final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(verification, 0);
+        return (receivers.size() > 0) ? true : false;
+    }
+
     private void warnAppInstallation() {
         // TODO: DialogFragment?
         mWarnInstallApps = new AlertDialog.Builder(getActivity()).setTitle(