Update TrustAgentService API after review

Add test for DPM.setTrustAgentConfiguration()

Also fixes bug where DPM flags were set in onPreferenceChanged()
which happened before being set on the checkbox preference.

Fixes bug 17008504

Change-Id: I165cd73d11c0a68ee6667cb1fd88892a39267903
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 58daf39..65bfba3 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -790,6 +790,10 @@
     <string name="require_encryption">Require encryption</string>
     <string name="activate_encryption">Activate encryption</string>
 
+    <string name="trust_agent_category">Trust Agent Features</string>
+    <string name="set_trust_agent_component_name">Enabled Component Name</string>
+    <string name="set_trust_agent_feature_list">Enabled Features (comma-separated)</string>
+
     <!-- Strings used by DeviceAdminSample controller code -->
     <string name="password_sufficient">Current password meets policy requirements</string>
     <string name="password_insufficient">Current password does not meet policy requirements</string>
diff --git a/samples/ApiDemos/res/xml/device_admin_general.xml b/samples/ApiDemos/res/xml/device_admin_general.xml
index cfd0048..1d0084e 100644
--- a/samples/ApiDemos/res/xml/device_admin_general.xml
+++ b/samples/ApiDemos/res/xml/device_admin_general.xml
@@ -57,4 +57,19 @@
 
     </PreferenceCategory>
 
+    <PreferenceCategory
+        android:title="@string/trust_agent_category" >
+
+        <EditTextPreference
+            android:key="key_trust_agent_component"
+            android:title="@string/set_trust_agent_component_name"
+            android:dialogTitle="@string/set_trust_agent_component_name" />
+
+        <EditTextPreference
+            android:key="key_trust_agent_features"
+            android:title="@string/set_trust_agent_feature_list"
+            android:dialogTitle="@string/set_trust_agent_feature_list" />
+
+    </PreferenceCategory>
+
 </PreferenceScreen>
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java b/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
index 82df903..71badcd 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
@@ -26,7 +26,9 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.os.PersistableBundle;
 import android.preference.CheckBoxPreference;
 import android.preference.EditTextPreference;
 import android.preference.ListPreference;
@@ -41,6 +43,8 @@
 import android.util.Log;
 import android.widget.Toast;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -71,6 +75,8 @@
     private static final String KEY_DISABLE_NOTIFICATIONS = "key_disable_notifications";
     private static final String KEY_DISABLE_UNREDACTED = "key_disable_unredacted";
     private static final String KEY_DISABLE_TRUST_AGENTS = "key_disable_trust_agents";
+    private static final String KEY_TRUST_AGENT_COMPONENT = "key_trust_agent_component";
+    private static final String KEY_TRUST_AGENT_FEATURES = "key_trust_agent_features";
     private static final String KEY_DISABLE_KEYGUARD_WIDGETS = "key_disable_keyguard_widgets";
     private static final String KEY_DISABLE_KEYGUARD_SECURE_CAMERA
             = "key_disable_keyguard_secure_camera";
@@ -274,6 +280,8 @@
         private CheckBoxPreference mDisableKeyguardNotificationCheckbox;
         private CheckBoxPreference mDisableKeyguardTrustAgentCheckbox;
         private CheckBoxPreference mDisableKeyguardUnredactedCheckbox;
+        private EditTextPreference mTrustAgentComponent;
+        private EditTextPreference mTrustAgentFeatures;
 
         @Override
         public void onCreate(Bundle savedInstanceState) {
@@ -304,6 +312,14 @@
             mDisableKeyguardTrustAgentCheckbox =
                     (CheckBoxPreference) findPreference(KEY_DISABLE_TRUST_AGENTS);
             mDisableKeyguardTrustAgentCheckbox.setOnPreferenceChangeListener(this);
+
+            mTrustAgentComponent =
+                    (EditTextPreference) findPreference(KEY_TRUST_AGENT_COMPONENT);
+            mTrustAgentComponent.setOnPreferenceChangeListener(this);
+
+            mTrustAgentFeatures =
+                    (EditTextPreference) findPreference(KEY_TRUST_AGENT_FEATURES);
+            mTrustAgentFeatures.setOnPreferenceChangeListener(this);
         }
 
         // At onResume time, reload UI with current values as required
@@ -340,8 +356,8 @@
             if (super.onPreferenceChange(preference, newValue)) {
                 return true;
             }
-            boolean value = (Boolean) newValue;
             if (preference == mEnableCheckbox) {
+                boolean value = (Boolean) newValue;
                 if (value != mAdminActive) {
                     if (value) {
                         // Launch the activity to have the user enable our admin.
@@ -359,6 +375,7 @@
                     }
                 }
             } else if (preference == mDisableCameraCheckbox) {
+                boolean value = (Boolean) newValue;
                 mDPM.setCameraDisabled(mDeviceAdminSample, value);
                 // Delay update because the change is only applied after exiting this method.
                 postReloadSummaries();
@@ -366,20 +383,39 @@
                     || preference == mDisableKeyguardSecureCameraCheckbox
                     || preference == mDisableKeyguardNotificationCheckbox
                     || preference == mDisableKeyguardUnredactedCheckbox
-                    || preference == mDisableKeyguardTrustAgentCheckbox) {
-                // Delay update because the change is only applied after exiting this method.
-                getView().post(new Runnable() {
-                    @Override
-                    public void run() {
-                        mDPM.setKeyguardDisabledFeatures(mDeviceAdminSample,
-                                createKeyguardDisabledFlag());
-                    }
-                });
+                    || preference == mDisableKeyguardTrustAgentCheckbox
+                    || preference == mTrustAgentComponent
+                    || preference == mTrustAgentFeatures) {
+                postUpdateDpmDisableFeatures();
                 postReloadSummaries();
             }
             return true;
         }
 
+        private void postUpdateDpmDisableFeatures() {
+            getView().post(new Runnable() {
+                @Override
+                public void run() {
+                    mDPM.setKeyguardDisabledFeatures(mDeviceAdminSample,
+                            createKeyguardDisabledFlag());
+                    String component = mTrustAgentComponent.getText();
+                    if (component != null) {
+                        ComponentName agent = ComponentName.unflattenFromString(component);
+                        if (agent != null) {
+                            String featureString = mTrustAgentFeatures.getText();
+                            if (featureString != null) {
+                                PersistableBundle bundle = new PersistableBundle();
+                                bundle.putStringArray("features", featureString.split(","));
+                                mDPM.setTrustAgentConfiguration(mDeviceAdminSample, agent, bundle);
+                            }
+                        } else {
+                            Log.w(TAG, "Invalid component: " + component);
+                        }
+                    }
+                }
+            });
+        }
+
         @Override
         protected void reloadSummaries() {
             super.reloadSummaries();
@@ -416,6 +452,17 @@
                         R.string.keyguard_trust_agents_disabled
                         : R.string.keyguard_trust_agents_enabled);
             mDisableKeyguardTrustAgentCheckbox.setSummary(keyguardEnableTrustAgentSummary);
+
+            final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
+            final boolean trustDisabled =
+                    (disabled & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0;
+            String component = prefs.getString(mTrustAgentComponent.getKey(), null);
+            mTrustAgentComponent.setSummary(component);
+            mTrustAgentComponent.setEnabled(trustDisabled);
+
+            String features = prefs.getString(mTrustAgentFeatures.getKey(), null);
+            mTrustAgentFeatures.setSummary(features);
+            mTrustAgentFeatures.setEnabled(trustDisabled);
         }
 
         /** Updates the device capabilities area (dis/enabling) as the admin is (de)activated */
@@ -426,6 +473,8 @@
             mDisableKeyguardNotificationCheckbox.setEnabled(enabled);
             mDisableKeyguardUnredactedCheckbox.setEnabled(enabled);
             mDisableKeyguardTrustAgentCheckbox.setEnabled(enabled);
+            mTrustAgentComponent.setEnabled(enabled);
+            mTrustAgentFeatures.setEnabled(enabled);
         }
     }