Accessibility service specific settings.

This is the settings part of the change for adding
support for accessibility service specific settings.

1. Added the first cut of a SettingsCheckBoxPreference
   that is a CheckBoxPreference that optionally takes
   an intent to be lauched as settings of the item
   represented by the preference.

2. Updated the AccessibilitySettigns to use the
   SettingsCheckBoxPreference.

Change-Id: If44262b8965c65620c6b8c252f8ef356eb0d95cf
diff --git a/res/drawable-hdpi/ic_sysbar_quicksettings.png b/res/drawable-hdpi/ic_sysbar_quicksettings.png
new file mode 100644
index 0000000..47b4ba2
--- /dev/null
+++ b/res/drawable-hdpi/ic_sysbar_quicksettings.png
Binary files differ
diff --git a/res/drawable-mdpi/ic_sysbar_quicksettings.png b/res/drawable-mdpi/ic_sysbar_quicksettings.png
new file mode 100644
index 0000000..7928104
--- /dev/null
+++ b/res/drawable-mdpi/ic_sysbar_quicksettings.png
Binary files differ
diff --git a/res/layout/preference_settings_checkbox_widget.xml b/res/layout/preference_settings_checkbox_widget.xml
new file mode 100644
index 0000000..a65e56f
--- /dev/null
+++ b/res/layout/preference_settings_checkbox_widget.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 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.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:gravity="center_vertical"
+    android:paddingRight="?android:attr/scrollbarSize">
+
+    <!-- Settings button -->
+    <ImageView
+        android:id="@+id/settings_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:padding="8dip"
+        android:background="?android:attr/selectableItemBackground"
+        android:src="@drawable/ic_sysbar_quicksettings"
+        android:contentDescription="@string/settings_button" />
+
+    <!-- Divider -->
+    <ImageView
+        android:id="@+id/divider"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:src="@drawable/nav_divider" />
+
+    <!-- CheckBox -->
+    <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+android:id/checkbox"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:focusable="false"
+        android:clickable="false" />
+
+</LinearLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 064700f..3a09685 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2609,6 +2609,9 @@
     <!-- Accessibility settings: power button behavior summary text -->
     <string name="accessibility_power_button_ends_call_summary">During a call, pressing Power ends call instead of turning off screen</string>
 
+    <!-- Accessibility settings: button for lauching settings for an accessibility service -->
+    <string name="settings_button">Settings</string>
+
     <!-- Setting interaction category [CHAR LIMIT=35] -->
     <string name="touchscreen_gestures_category">Touchscreen gestures</string>
     <!-- Title for setting the long-press timeout [CHAR LIMIT=35] -->
diff --git a/src/com/android/settings/AccessibilitySettings.java b/src/com/android/settings/AccessibilitySettings.java
index 826410d..9a0db5d 100644
--- a/src/com/android/settings/AccessibilitySettings.java
+++ b/src/com/android/settings/AccessibilitySettings.java
@@ -16,6 +16,7 @@
 
 package com.android.settings;
 
+import android.accessibilityservice.AccessibilityServiceInfo;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.Service;
@@ -78,7 +79,7 @@
 
     private CheckBoxPreference mToggleAccessibilityCheckBox;
     private CheckBoxPreference mToggleScriptInjectionCheckBox;
-    private CheckBoxPreference mToggleAccessibilityServiceCheckBox;
+    private SettingsCheckBoxPreference mToggleAccessibilityServiceCheckBox;
 
     private PreferenceCategory mPowerButtonCategory;
     private CheckBoxPreference mPowerButtonEndsCallCheckBox;
@@ -87,8 +88,8 @@
 
     private ListPreference mLongPressTimeoutListPreference;
 
-    private Map<String, ServiceInfo> mAccessibilityServices =
-        new LinkedHashMap<String, ServiceInfo>();
+    private Map<String, AccessibilityServiceInfo> mAccessibilityServices =
+        new LinkedHashMap<String, AccessibilityServiceInfo>();
 
     private TextUtils.SimpleStringSplitter mStringColonSplitter =
         new TextUtils.SimpleStringSplitter(':');
@@ -157,7 +158,7 @@
             }
         }
 
-        Map<String, ServiceInfo> accessibilityServices = mAccessibilityServices;
+        Map<String, AccessibilityServiceInfo> accessibilityServices = mAccessibilityServices;
 
         for (String key : accessibilityServices.keySet()) {
             CheckBoxPreference preference = (CheckBoxPreference) findPreference(key);
@@ -230,9 +231,9 @@
                 throw new IllegalArgumentException(
                         KEY_TOGGLE_ACCESSIBILITY_SERVICE_CHECKBOX
                                 + " must be mapped to an instance of a "
-                                + CheckBoxPreference.class.getName());
+                                + SettingsCheckBoxPreference.class.getName());
             }
-            mToggleAccessibilityServiceCheckBox = (CheckBoxPreference) preference;
+            mToggleAccessibilityServiceCheckBox = (SettingsCheckBoxPreference) preference;
         }
     }
 
@@ -274,7 +275,7 @@
         } else if (TOGGLE_ACCESSIBILITY_SCRIPT_INJECTION_CHECKBOX.equals(key)) {
             handleToggleAccessibilityScriptInjection((CheckBoxPreference) preference);
         } else if (preference instanceof CheckBoxPreference) {
-            handleEnableAccessibilityServiceStateChange((CheckBoxPreference) preference);
+            handleEnableAccessibilityServiceStateChange((SettingsCheckBoxPreference) preference);
         }
 
         return super.onPreferenceTreeClick(preferenceScreen, preference);
@@ -318,7 +319,8 @@
      *
      * @param preference The preference.
      */
-    private void handleEnableAccessibilityServiceStateChange(CheckBoxPreference preference) {
+    private void handleEnableAccessibilityServiceStateChange(
+            SettingsCheckBoxPreference preference) {
         if (preference.isChecked()) {
             mToggleAccessibilityServiceCheckBox = preference;
             // set right enabled state since the user may press back
@@ -357,7 +359,8 @@
         AccessibilityManager accessibilityManager =
             (AccessibilityManager) getSystemService(Service.ACCESSIBILITY_SERVICE);
 
-        List<ServiceInfo> installedServices = accessibilityManager.getAccessibilityServiceList();
+        List<AccessibilityServiceInfo> installedServices =
+            accessibilityManager.getInstalledAccessibilityServiceList();
 
         if (installedServices.isEmpty()) {
             getPreferenceScreen().removePreference(mAccessibilityServicesCategory);
@@ -367,12 +370,22 @@
         getPreferenceScreen().addPreference(mAccessibilityServicesCategory);
 
         for (int i = 0, count = installedServices.size(); i < count; ++i) {
-            ServiceInfo serviceInfo = installedServices.get(i);
-            String key = serviceInfo.packageName + "/" + serviceInfo.name;
+            AccessibilityServiceInfo accessibilityServiceInfo = installedServices.get(i);
+            String key = accessibilityServiceInfo.getId();
 
-            if (mAccessibilityServices.put(key, serviceInfo) == null) {
-                CheckBoxPreference preference = new CheckBoxPreference(getActivity());
+            if (mAccessibilityServices.put(key, accessibilityServiceInfo) == null) {
+                String settingsActivityName = accessibilityServiceInfo.getSettingsActivityName();
+                Intent settingsIntent = null;
+                if (!TextUtils.isEmpty(settingsActivityName)) {
+                    String packageName = accessibilityServiceInfo.getResolveInfo()
+                        .serviceInfo.packageName;
+                    settingsIntent = new Intent(Intent.ACTION_MAIN);
+                    settingsIntent.setClassName(packageName, settingsActivityName);
+                }
+                SettingsCheckBoxPreference preference = new SettingsCheckBoxPreference(
+                        getActivity(), settingsIntent);
                 preference.setKey(key);
+                ServiceInfo serviceInfo = accessibilityServiceInfo.getResolveInfo().serviceInfo;
                 preference.setTitle(serviceInfo.loadLabel(getActivity().getPackageManager()));
                 mAccessibilityServicesCategory.addPreference(preference);
             }
@@ -424,7 +437,8 @@
                     .setMessage(getResources().getString(
                             R.string.accessibility_service_security_warning,
                             mAccessibilityServices.get(mToggleAccessibilityServiceCheckBox.getKey())
-                            .applicationInfo.loadLabel(getActivity().getPackageManager())))
+                            .getResolveInfo().serviceInfo.applicationInfo.loadLabel(
+                                    getActivity().getPackageManager())))
                     .setCancelable(true)
                     .setPositiveButton(android.R.string.ok,
                             new DialogInterface.OnClickListener() {
diff --git a/src/com/android/settings/SettingsCheckBoxPreference.java b/src/com/android/settings/SettingsCheckBoxPreference.java
new file mode 100644
index 0000000..6acdfe8
--- /dev/null
+++ b/src/com/android/settings/SettingsCheckBoxPreference.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2011 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.Context;
+import android.content.Intent;
+import android.preference.CheckBoxPreference;
+import android.util.TypedValue;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.CheckBox;
+import android.widget.ImageView;
+
+/**
+ * CheckBox preference that optionally shows an icon for launching a settings
+ * {@link android.app.Activity}. The settings activity, if intent for launching
+ * it was provided, can be stared only if the CheckBox in is checked.
+ */
+public class SettingsCheckBoxPreference extends CheckBoxPreference {
+
+    // Integer.MIN_VALUE means not initalized
+    private static int sDimAlpha = Integer.MIN_VALUE;
+
+    private final Intent mSettingsIntent;
+
+    /**
+     * Creates a new instance.
+     *
+     * @param context Context for accessing resources.
+     * @param settingsIntent Intent to use as settings for the item represented by
+     *        this preference. Pass <code>null</code> if there is no associated 
+     *        settings activity.
+     */
+    public SettingsCheckBoxPreference(Context context, Intent settingsIntent) {
+        super(context);
+
+        if (sDimAlpha == Integer.MIN_VALUE) {
+            TypedValue outValue = new TypedValue();
+            context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
+            sDimAlpha = (int) (outValue.getFloat() * 255);
+        }
+
+        mSettingsIntent = settingsIntent;
+        setWidgetLayoutResource(R.layout.preference_settings_checkbox_widget);
+    }
+
+    @Override
+    protected void onBindView(View view) {
+        super.onBindView(view);
+
+        ImageView settingsButton = (ImageView) view.findViewById(R.id.settings_button);
+        if (settingsButton == null) {
+            return;
+        }
+        if (mSettingsIntent != null) {
+            CheckBox checkbox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox);
+            if (checkbox == null) {
+                return;
+            }
+            if (checkbox.isChecked()) {
+                settingsButton.setOnClickListener(new OnClickListener() {
+                    public void onClick(View view) {
+                        getContext().startActivity(mSettingsIntent);
+                    }
+                });
+            }
+            settingsButton.setVisibility(View.VISIBLE);
+            if (checkbox.isChecked() && isEnabled()) {
+                settingsButton.setAlpha(255);
+            } else {
+                settingsButton.setAlpha(sDimAlpha);
+            }
+        } else {
+            settingsButton.setVisibility(View.GONE);
+        }
+    }
+}