Add package verification to settings app

Adds a dialog/toggle in security settings to turn package verification on and
off.

Bug: 7082362
Change-Id: I9985437ae28e4a999992c91a7e88547495ccbda2
diff --git a/res/values/strings.xml b/res/values/strings.xml
index fd00184..ba3f386 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2385,6 +2385,10 @@
  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. -->
+    <string name="verify_applications">App check</string>
+    <!-- Applications settings screen, setting check box summary. This is the summary for "App Check" checkbox. -->
+    <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>
     <!-- Applications settings screen, setting check box summary.  This is the summary for "Advanced settings" checkbox  -->
diff --git a/res/xml/security_settings_misc.xml b/res/xml/security_settings_misc.xml
index 9f78820..6f9311a 100644
--- a/res/xml/security_settings_misc.xml
+++ b/res/xml/security_settings_misc.xml
@@ -52,6 +52,12 @@
                 android:summaryOn="@string/install_unknown_applications"
                 android:persistent="false" />
 
+        <CheckBoxPreference
+                android:key="toggle_verify_applications"
+                android:title="@string/verify_applications"
+                android:summaryOff="@string/verify_applications_summary"
+                android:summaryOn="@string/verify_applications_summary"
+                android:persistent="false" />
     </PreferenceCategory>
 
     <PreferenceCategory android:title="@string/credentials_title"
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index e1e4a09..248dcad 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -69,6 +69,7 @@
     private static final String KEY_SHOW_PASSWORD = "show_password";
     private static final String KEY_RESET_CREDENTIALS = "reset_credentials";
     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";
 
     DevicePolicyManager mDPM;
@@ -87,6 +88,7 @@
 
     private CheckBoxPreference mToggleAppInstallation;
     private DialogInterface mWarnInstallApps;
+    private CheckBoxPreference mToggleVerifyApps;
     private CheckBoxPreference mPowerButtonInstantlyLocks;
 
     @Override
@@ -225,6 +227,11 @@
                 KEY_TOGGLE_INSTALL_APPLICATIONS);
         mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
 
+        // Package verification
+        mToggleVerifyApps = (CheckBoxPreference) findPreference(
+            KEY_TOGGLE_VERIFY_APPLICATIONS);
+        mToggleVerifyApps.setChecked(isVerifyAppsEnabled());
+
         return root;
     }
 
@@ -239,6 +246,11 @@
                                 enabled ? 1 : 0);
     }
 
+    private boolean isVerifyAppsEnabled() {
+        return Settings.Global.getInt(getContentResolver(),
+                                      Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) > 0;
+    }
+
     private void warnAppInstallation() {
         // TODO: DialogFragment?
         mWarnInstallApps = new AlertDialog.Builder(getActivity()).setTitle(
@@ -420,6 +432,9 @@
             } else {
                 setNonMarketAppsAllowed(false);
             }
+        } else if (KEY_TOGGLE_VERIFY_APPLICATIONS.equals(key)) {
+            Settings.Global.putInt(getContentResolver(), Settings.Global.PACKAGE_VERIFIER_ENABLE,
+                    mToggleVerifyApps.isChecked() ? 1 : 0);
         } else {
             // If we didn't handle it, let preferences handle it.
             return super.onPreferenceTreeClick(preferenceScreen, preference);