Moving the trust agents settings in the security settings menu and adding
personal unlock.

Change-Id: I0eeb3a32c55450af2435112a98aa44475159dc4f
diff --git a/res/layout/trust_agent_settings.xml b/res/layout/advanced_security_settings.xml
similarity index 100%
rename from res/layout/trust_agent_settings.xml
rename to res/layout/advanced_security_settings.xml
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5e90be5..6bc1ff9 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3979,6 +3979,8 @@
     <string name="trusted_credentials">Trusted credentials</string>
     <!-- Summary of preference to display trusted credentials (aka CA certificates) [CHAR LIMIT=NONE] -->
     <string name="trusted_credentials_summary">Display trusted CA certificates</string>
+    <!-- Title of preference group for advance security settings [CHAR LIMIT=30] -->
+    <string name="advanced_security_title">Advanced</string>
     <!-- Title of preference of what type of credential storage this device has: hardware or software [CHAR LIMIT=30] -->
     <string name="credential_storage_type">Storage type</string>
     <!-- Summary text for preference showing what type of credential storage this device has when it is stored in a hardware-backed storage (as opposed to "software only") [CHAR LIMIT=NONE] -->
diff --git a/res/xml/security_settings_misc.xml b/res/xml/security_settings_misc.xml
index 84ce96c..c8f3e02 100644
--- a/res/xml/security_settings_misc.xml
+++ b/res/xml/security_settings_misc.xml
@@ -47,12 +47,6 @@
                 android:persistent="false"
                 android:fragment="com.android.settings.DeviceAdminSettings"/>
 
-        <Preference android:key="manage_trust_agents"
-                    android:title="@string/manage_trust_agents"
-                    android:summary="@string/manage_trust_agents_summary"
-                    android:persistent="false"
-                    android:fragment="com.android.settings.TrustAgentSettings"/>
-
         <CheckBoxPreference android:key="toggle_install_applications"
                 android:title="@string/install_applications"
                 android:summaryOff="@string/install_unknown_applications"
@@ -64,7 +58,6 @@
                 android:summaryOff="@string/verify_applications_summary"
                 android:summaryOn="@string/verify_applications_summary"
                 android:persistent="false" />
-
     </PreferenceCategory>
 
     <PreferenceCategory android:key="credentials_management"
@@ -99,4 +92,14 @@
                     android:targetClass="com.android.settings.CredentialStorage"/>
         </Preference>
     </PreferenceCategory>
+
+    <PreferenceCategory android:key="advanced_security"
+            android:title="@string/advanced_security_title"
+            android:persistent="false">
+        <Preference android:key="manage_trust_agents"
+                android:title="@string/manage_trust_agents"
+                android:summary="@string/manage_trust_agents_summary"
+                android:persistent="false"
+                android:fragment="com.android.settings.AdvancedSecuritySettings"/>
+    </PreferenceCategory>
 </PreferenceScreen>
diff --git a/src/com/android/settings/TrustAgentSettings.java b/src/com/android/settings/AdvancedSecuritySettings.java
similarity index 69%
rename from src/com/android/settings/TrustAgentSettings.java
rename to src/com/android/settings/AdvancedSecuritySettings.java
index d31436d..c497ac4 100644
--- a/src/com/android/settings/TrustAgentSettings.java
+++ b/src/com/android/settings/AdvancedSecuritySettings.java
@@ -18,26 +18,17 @@
 
 import com.android.internal.widget.LockPatternUtils;
 
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-
 import android.app.ListFragment;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
-import android.content.res.Resources;
-import android.content.res.TypedArray;
-import android.content.res.XmlResourceParser;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.service.trust.TrustAgentService;
 import android.util.ArrayMap;
 import android.util.ArraySet;
-import android.util.AttributeSet;
-import android.util.Slog;
-import android.util.Xml;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -46,15 +37,12 @@
 import android.widget.ImageView;
 import android.widget.TextView;
 
-import java.io.IOException;
 import java.util.List;
 
-public class TrustAgentSettings extends ListFragment implements View.OnClickListener {
-    static final String TAG = "TrustAgentSettings";
+public class AdvancedSecuritySettings extends ListFragment implements View.OnClickListener {
+    static final String TAG = "AdvancedSecuritySettings";
 
     private static final String SERVICE_INTERFACE = TrustAgentService.SERVICE_INTERFACE;
-    private static final String TRUST_AGENT_META_DATA = TrustAgentService.TRUST_AGENT_META_DATA;
-
 
     private final ArraySet<ComponentName> mActiveAgents = new ArraySet<ComponentName>();
     private final ArrayMap<ComponentName, AgentInfo> mAvailableAgents
@@ -94,7 +82,7 @@
                     container.getContext().getApplicationContext());
         }
         setListAdapter(new AgentListAdapter());
-        return inflater.inflate(R.layout.trust_agent_settings, container, false);
+        return inflater.inflate(R.layout.advanced_security_settings, container, false);
     }
 
     @Override
@@ -121,70 +109,21 @@
 
         for (ResolveInfo resolveInfo : resolveInfos) {
             if (resolveInfo.serviceInfo == null) continue;
-            ComponentName name = getComponentName(resolveInfo);
+            ComponentName name = TrustAgentUtils.getComponentName(resolveInfo);
             if (!mAvailableAgents.containsKey(name)) {
                 AgentInfo agentInfo = new AgentInfo();
                 agentInfo.label = resolveInfo.loadLabel(pm);
                 agentInfo.icon = resolveInfo.loadIcon(pm);
                 agentInfo.component = name;
-                agentInfo.settings = getSettingsComponentName(pm, resolveInfo);
+                TrustAgentUtils.TrustAgentComponentInfo trustAgentComponentInfo =
+                        TrustAgentUtils.getSettingsComponent(pm, resolveInfo);
+                agentInfo.settings = trustAgentComponentInfo.componentName;
                 mAvailableAgents.put(name, agentInfo);
             }
         }
         ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
     }
 
-    private ComponentName getComponentName(ResolveInfo resolveInfo) {
-        if (resolveInfo == null || resolveInfo.serviceInfo == null) return null;
-        return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
-    }
-
-    private ComponentName getSettingsComponentName(PackageManager pm, ResolveInfo resolveInfo) {
-        if (resolveInfo == null || resolveInfo.serviceInfo == null
-                || resolveInfo.serviceInfo.metaData == null) return null;
-        String cn = null;
-        XmlResourceParser parser = null;
-        Exception caughtException = null;
-        try {
-            parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA);
-            if (parser == null) {
-                Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data");
-                return null;
-            }
-            Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo);
-            AttributeSet attrs = Xml.asAttributeSet(parser);
-            int type;
-            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
-                    && type != XmlPullParser.START_TAG) {
-            }
-            String nodeName = parser.getName();
-            if (!"trust-agent".equals(nodeName)) {
-                Slog.w(TAG, "Meta-data does not start with trust-agent tag");
-                return null;
-            }
-            TypedArray sa = res
-                    .obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent);
-            cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity);
-            sa.recycle();
-        } catch (PackageManager.NameNotFoundException e) {
-            caughtException = e;
-        } catch (IOException e) {
-            caughtException = e;
-        } catch (XmlPullParserException e) {
-            caughtException = e;
-        } finally {
-            if (parser != null) parser.close();
-        }
-        if (caughtException != null) {
-            Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException);
-            return null;
-        }
-        if (cn != null && cn.indexOf('/') < 0) {
-            cn = resolveInfo.serviceInfo.packageName + "/" + cn;
-        }
-        return cn == null ? null : ComponentName.unflattenFromString(cn);
-    }
-
     @Override
     public void onClick(View view) {
         ViewHolder h = (ViewHolder) view.getTag();
@@ -282,10 +221,10 @@
             h.name = (TextView)v.findViewById(R.id.name);
             h.checkbox = (CheckBox)v.findViewById(R.id.checkbox);
             h.clickable = v.findViewById(R.id.clickable);
-            h.clickable.setOnClickListener(TrustAgentSettings.this);
+            h.clickable.setOnClickListener(AdvancedSecuritySettings.this);
             h.description = (TextView)v.findViewById(R.id.description);
             h.settings = v.findViewById(R.id.settings);
-            h.settings.setOnClickListener(TrustAgentSettings.this);
+            h.settings.setOnClickListener(AdvancedSecuritySettings.this);
             v.setTag(h);
             h.settings.setTag(h);
             h.clickable.setTag(h);
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index 00d1d89..ab03853 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -41,6 +41,7 @@
 import android.provider.SearchIndexableResource;
 import android.provider.Settings;
 import android.security.KeyStore;
+import android.service.trust.TrustAgentService;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 
@@ -58,6 +59,8 @@
 public class SecuritySettings extends RestrictedSettingsFragment
         implements OnPreferenceChangeListener, DialogInterface.OnClickListener, Indexable {
     static final String TAG = "SecuritySettings";
+    private static final Intent TRUST_AGENT_INTENT =
+            new Intent(TrustAgentService.SERVICE_INTERFACE);
 
     // Lock Settings
     private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
@@ -74,6 +77,7 @@
     private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
     private static final int CONFIRM_EXISTING_FOR_BIOMETRIC_WEAK_IMPROVE_REQUEST = 124;
     private static final int CONFIRM_EXISTING_FOR_BIOMETRIC_WEAK_LIVELINESS_OFF = 125;
+    private static final int CHANGE_TRUST_AGENT_SETTINGS = 126;
 
     // Misc Settings
     private static final String KEY_SIM_LOCK = "sim_lock";
@@ -86,6 +90,7 @@
     private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
     private static final String KEY_CREDENTIALS_MANAGER = "credentials_management";
     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
+    private static final String KEY_TRUST_AGENT = "trust_agent";
 
     private DevicePolicyManager mDPM;
 
@@ -161,7 +166,7 @@
     /**
      * Important!
      *
-     * Dont forget to update the SecuritySearchIndexProvider if you are doing any change in the
+     * Don't forget to update the SecuritySearchIndexProvider if you are doing any change in the
      * logic or adding/removing preferences here.
      */
     private PreferenceScreen createPreferenceHierarchy() {
@@ -270,7 +275,7 @@
         }
 
         // Application install
-        PreferenceGroup deviceAdminCategory= (PreferenceGroup)
+        PreferenceGroup deviceAdminCategory = (PreferenceGroup)
                 root.findPreference(KEY_DEVICE_ADMIN_CATEGORY);
         mToggleAppInstallation = (CheckBoxPreference) findPreference(
                 KEY_TOGGLE_INSTALL_APPLICATIONS);
@@ -303,6 +308,36 @@
             protectByRestrictions(root.findPreference(KEY_CREDENTIALS_INSTALL));
         }
 
+        // Trust Agent preferences
+        PreferenceGroup securityCategory = (PreferenceGroup)
+                root.findPreference(KEY_SECURITY_CATEGORY);
+        if (securityCategory != null) {
+            PackageManager pm = getPackageManager();
+            List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
+                    PackageManager.GET_META_DATA);
+            for (ResolveInfo resolveInfo : resolveInfos) {
+                if (resolveInfo.serviceInfo == null) continue;
+                TrustAgentUtils.TrustAgentComponentInfo trustAgentComponentInfo =
+                        TrustAgentUtils.getSettingsComponent(pm, resolveInfo);
+                if (trustAgentComponentInfo.componentName == null ||
+                        trustAgentComponentInfo.title == null ||
+                        trustAgentComponentInfo.title == "") continue;
+                Preference trustAgentPreference =
+                        new Preference(securityCategory.getContext());
+                trustAgentPreference.setKey(KEY_TRUST_AGENT);
+                trustAgentPreference.setTitle(trustAgentComponentInfo.title);
+                trustAgentPreference.setSummary(trustAgentComponentInfo.summary);
+                // Create intent for this preference.
+                Intent intent = new Intent();
+                intent.setComponent(trustAgentComponentInfo.componentName);
+                intent.setAction(Intent.ACTION_MAIN);
+                trustAgentPreference.setIntent(intent);
+                // Add preference to the settings menu.
+                securityCategory.addPreference(trustAgentPreference);
+                break; // Only render the first one.
+            }
+        }
+
         return root;
     }
 
@@ -522,6 +557,13 @@
         } else if (KEY_TOGGLE_VERIFY_APPLICATIONS.equals(key)) {
             Settings.Global.putInt(getContentResolver(), Settings.Global.PACKAGE_VERIFIER_ENABLE,
                     mToggleVerifyApps.isChecked() ? 1 : 0);
+        } else if (KEY_TRUST_AGENT.equals(key)) {
+            ChooseLockSettingsHelper helper =
+                    new ChooseLockSettingsHelper(this.getActivity(), this);
+            if (!helper.launchConfirmationActivity(CHANGE_TRUST_AGENT_SETTINGS, null, null)) {
+                // If this returns false, it means no password confirmation is required.
+                startActivity(preference.getIntent());
+            }
         } else {
             // If we didn't handle it, let preferences handle it.
             return super.onPreferenceTreeClick(preferenceScreen, preference);
@@ -552,6 +594,14 @@
             // is called by grabbing the value from lockPatternUtils.  We can't set it here
             // because mBiometricWeakLiveliness could be null
             return;
+        } else if (requestCode == CHANGE_TRUST_AGENT_SETTINGS && resultCode == Activity.RESULT_OK) {
+            Preference preference = getPreferenceScreen().findPreference(KEY_TRUST_AGENT);
+            if (preference != null) {
+                Intent intent = preference.getIntent();
+                if (intent != null) {
+                    startActivity(intent);
+                }
+            }
         }
         createPreferenceHierarchy();
     }
diff --git a/src/com/android/settings/TrustAgentUtils.java b/src/com/android/settings/TrustAgentUtils.java
new file mode 100644
index 0000000..64829a2
--- /dev/null
+++ b/src/com/android/settings/TrustAgentUtils.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings;
+
+import android.content.ComponentName;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.res.Resources;
+import android.content.res.TypedArray;
+import android.content.res.XmlResourceParser;
+import android.service.trust.TrustAgentService;
+import android.util.AttributeSet;
+import android.util.Slog;
+import android.util.Xml;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+
+public class TrustAgentUtils {
+    static final String TAG = "TrustAgentUtils";
+
+    private static final String TRUST_AGENT_META_DATA = TrustAgentService.TRUST_AGENT_META_DATA;
+
+    public static class TrustAgentComponentInfo {
+        ComponentName componentName;
+        String title;
+        String summary;
+    }
+
+    public static ComponentName getComponentName(ResolveInfo resolveInfo) {
+        if (resolveInfo == null || resolveInfo.serviceInfo == null) return null;
+        return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
+    }
+
+    public static TrustAgentComponentInfo getSettingsComponent(
+            PackageManager pm, ResolveInfo resolveInfo) {
+        if (resolveInfo == null || resolveInfo.serviceInfo == null
+                || resolveInfo.serviceInfo.metaData == null) return null;
+        String cn = null;
+        TrustAgentComponentInfo trustAgentComponentInfo = new TrustAgentComponentInfo();
+        XmlResourceParser parser = null;
+        Exception caughtException = null;
+        try {
+            parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA);
+            if (parser == null) {
+                Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data");
+                return null;
+            }
+            Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo);
+            AttributeSet attrs = Xml.asAttributeSet(parser);
+            int type;
+            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                    && type != XmlPullParser.START_TAG) {
+            }
+            String nodeName = parser.getName();
+            if (!"trust-agent".equals(nodeName)) {
+                Slog.w(TAG, "Meta-data does not start with trust-agent tag");
+                return null;
+            }
+            TypedArray sa =
+                    res.obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent);
+            trustAgentComponentInfo.summary =
+                    sa.getString(com.android.internal.R.styleable.TrustAgent_summary);
+            trustAgentComponentInfo.title =
+                    sa.getString(com.android.internal.R.styleable.TrustAgent_title);
+            cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity);
+            sa.recycle();
+        } catch (PackageManager.NameNotFoundException e) {
+            caughtException = e;
+        } catch (IOException e) {
+            caughtException = e;
+        } catch (XmlPullParserException e) {
+            caughtException = e;
+        } finally {
+            if (parser != null) parser.close();
+        }
+        if (caughtException != null) {
+            Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException);
+            return null;
+        }
+        if (cn != null && cn.indexOf('/') < 0) {
+            cn = resolveInfo.serviceInfo.packageName + "/" + cn;
+        }
+        trustAgentComponentInfo.componentName = (cn == null) ? null : ComponentName.unflattenFromString(cn);
+        return trustAgentComponentInfo;
+    }
+}