Add screen for turning TalkBack on and off and remove intent to launch TB
tutorial from SUW. Also fix how Fragments are displayed (Fragment Views
were being inflated incorrectly causing bugs like the one where TB would
still see the hub elements on child screens) and  remove ripple from 
non-selectable text.
Bug: 27203716
Bug: 27218687
Bug: 27066012
Bug: 27587193

Change-Id: I4c6724b3ff6d6a7fca3d0f419a5e1c82ee1071d5
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 2bce5b6..9f86646 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -372,6 +372,7 @@
     private boolean mIsShowingDashboard;
     private boolean mIsShortcut;
 
+    private int mMainContentId = R.id.main_content;
     private ViewGroup mContent;
 
     private SearchView mSearchView;
@@ -546,7 +547,7 @@
         setContentView(mIsShowingDashboard ?
                 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
 
-        mContent = (ViewGroup) findViewById(R.id.main_content);
+        mContent = (ViewGroup) findViewById(mMainContentId);
 
         getFragmentManager().addOnBackStackChangedListener(this);
 
@@ -680,6 +681,14 @@
                 + " ms");
     }
 
+    /**
+     * Sets the id of the view continaing the main content. Should be called before calling super's
+     * onCreate.
+     */
+    protected void setMainContentId(int contentId) {
+        mMainContentId = contentId;
+    }
+
     private void setTitleFromIntent(Intent intent) {
         final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
         if (initialTitleResId > 0) {
@@ -962,7 +971,7 @@
      */
     public void startPreferenceFragment(Fragment fragment, boolean push) {
         FragmentTransaction transaction = getFragmentManager().beginTransaction();
-        transaction.replace(R.id.main_content, fragment);
+        transaction.replace(mMainContentId, fragment);
         if (push) {
             transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
             transaction.addToBackStack(BACK_STACK_PREFS);
@@ -983,7 +992,7 @@
         }
         Fragment f = Fragment.instantiate(this, fragmentName, args);
         FragmentTransaction transaction = getFragmentManager().beginTransaction();
-        transaction.replace(R.id.main_content, f);
+        transaction.replace(mMainContentId, f);
         if (withTransition) {
             TransitionManager.beginDelayedTransition(mContent);
         }
@@ -1179,7 +1188,7 @@
         if (mSearchResultsFragment != null) {
             return;
         }
-        Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
+        Fragment current = getFragmentManager().findFragmentById(mMainContentId);
         if (current != null && current instanceof SearchResultsSummary) {
             mSearchResultsFragment = (SearchResultsSummary) current;
         } else {
diff --git a/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizard.java b/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizard.java
index 35bb83b..12e58c4 100644
--- a/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizard.java
+++ b/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizard.java
@@ -17,18 +17,14 @@
 package com.android.settings.accessibility;
 
 import android.accessibilityservice.AccessibilityServiceInfo;
-import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.Intent;
 import android.content.pm.ServiceInfo;
-import android.content.res.Resources;
 import android.os.Bundle;
-import android.os.Handler;
 import android.provider.Settings;
 import android.support.v7.preference.Preference;
-import android.util.Log;
+import android.text.TextUtils;
 import android.view.accessibility.AccessibilityManager;
 
 import com.android.internal.logging.MetricsProto;
@@ -49,24 +45,12 @@
     // Preferences.
     private static final String DISPLAY_MAGNIFICATION_PREFERENCE =
             "screen_magnification_preference";
-    private static final String SCREEN_READER_PREFERENCE = "talkback_preference";
+    private static final String SCREEN_READER_PREFERENCE = "screen_reader_preference";
     private static final String FONT_SIZE_PREFERENCE = "font_size_preference";
 
-    // Time needed to let Talkback initialize its self before launching the tutorial.
-    private static final long SCREEN_READER_INITIALIZATION_DELAY_MS = 3000;
-
-    private String mTalkbackPackage;
-
     // Preference controls.
     private Preference mDisplayMagnificationPreference;
-    private Preference mTalkbackPreference;
-
-    private Runnable mStartTalkbackRunnable = new Runnable() {
-        @Override
-        public void run() {
-            launchTalkbackTutorial();
-        }
-    };
+    private Preference mScreenReaderPreference;
 
     @Override
     protected int getMetricsCategory() {
@@ -79,7 +63,13 @@
         addPreferencesFromResource(R.xml.accessibility_settings_for_setup_wizard);
 
         mDisplayMagnificationPreference = findPreference(DISPLAY_MAGNIFICATION_PREFERENCE);
-        mTalkbackPreference = findPreference(SCREEN_READER_PREFERENCE);
+        mScreenReaderPreference = findPreference(SCREEN_READER_PREFERENCE);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        updateScreenReaderPreference();
     }
 
     @Override
@@ -93,18 +83,6 @@
         return false;
     }
 
-    private void launchTalkbackTutorial() {
-        try {
-            Intent intent = new Intent(Settings.ACTION_SCREEN_READER_TUTORIAL);
-            intent.setPackage(mTalkbackPackage);
-            startActivity(intent);
-        } catch (ActivityNotFoundException e) {
-            // This can happen if either the build is misconfigued or an OEM removes the intent
-            // filter for the Talkback tutorial from their implementation of Talkback.
-            Log.e(TAG, "Can't find Talkback Tutorial: " + Settings.ACTION_SCREEN_READER_TUTORIAL);
-        }
-    }
-
     @Override
     public boolean onPreferenceTreeClick(Preference preference) {
         if (mDisplayMagnificationPreference == preference) {
@@ -116,58 +94,53 @@
             extras.putBoolean(AccessibilitySettings.EXTRA_CHECKED,
                     Settings.Secure.getInt(getContentResolver(),
                     Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, 0) == 1);
-        } else if (mTalkbackPreference == preference) {
-            // Enable Talkback if disabled. The tutorial will automatically start when Talkback is
-            // first activated.
-            final ContentResolver resolver = getContentResolver();
-            final int accessibilityEnabled =
-                    Settings.Secure.getInt(resolver, Settings.Secure.ACCESSIBILITY_ENABLED, 0);
-            if (accessibilityEnabled == 0) {
-                // Find the first installed screen reader.
-                String serviceToEnable = null;
-                final AccessibilityManager manager =
-                        getActivity().getSystemService(AccessibilityManager.class);
-                final List<AccessibilityServiceInfo> accessibilityServices =
-                        manager.getInstalledAccessibilityServiceList();
-                for (AccessibilityServiceInfo accessibilityService : accessibilityServices) {
-                    if ((accessibilityService.feedbackType
-                            & AccessibilityServiceInfo.FEEDBACK_SPOKEN) != 0) {
-                        final ServiceInfo serviceInfo =
-                                accessibilityService.getResolveInfo().serviceInfo;
-                        mTalkbackPackage = serviceInfo.packageName;
-                        final ComponentName componentName =
-                                new ComponentName(serviceInfo.packageName, serviceInfo.name);
-
-                        serviceToEnable = componentName.flattenToString();
-                        break;
-                    }
-                }
-
-                // Enable all accessibility services with spoken feedback type.
-                Settings.Secure.putString(resolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
-                        serviceToEnable);
-
-                // Allow the services we just enabled to toggle touch exploration.
-                Settings.Secure.putString(resolver,
-                        Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
-                        serviceToEnable);
-
-                // Enable touch exploration.
-                Settings.Secure.putInt(resolver, Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1);
-
-                // Turn on accessibility mode last, since enabling accessibility with no
-                // services has no effect.
-                Settings.Secure.putInt(resolver, Settings.Secure.ACCESSIBILITY_ENABLED, 1);
-
-                // Since Talkback will display an error message if it's not active when the Tutorial
-                // is launched, launch Talkbck Tutorial with a delay.
-                new Handler().postDelayed(mStartTalkbackRunnable,
-                        SCREEN_READER_INITIALIZATION_DELAY_MS);
-            } else {
-                launchTalkbackTutorial();
-            }
         }
 
         return super.onPreferenceTreeClick(preference);
     }
+
+    private AccessibilityServiceInfo findFirstServiceWithSpokenFeedback() {
+        final AccessibilityManager manager =
+                getActivity().getSystemService(AccessibilityManager.class);
+        final List<AccessibilityServiceInfo> accessibilityServices =
+                manager.getInstalledAccessibilityServiceList();
+        for (AccessibilityServiceInfo info : accessibilityServices) {
+            if ((info.feedbackType & AccessibilityServiceInfo.FEEDBACK_SPOKEN) != 0) {
+                return info;
+            }
+        }
+
+        return null;
+    }
+
+    private void updateScreenReaderPreference() {
+        // Find a screen reader.
+        AccessibilityServiceInfo info = findFirstServiceWithSpokenFeedback();
+        if (info == null) {
+            mScreenReaderPreference.setEnabled(false);
+        } else {
+            mScreenReaderPreference.setEnabled(true);
+        }
+
+        ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
+        String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();
+        mScreenReaderPreference.setTitle(title);
+
+        ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
+        mScreenReaderPreference.setKey(componentName.flattenToString());
+
+        // Update the extras.
+        Bundle extras = mScreenReaderPreference.getExtras();
+        extras.putParcelable(AccessibilitySettings.EXTRA_COMPONENT_NAME, componentName);
+
+        extras.putString(AccessibilitySettings.EXTRA_PREFERENCE_KEY,
+                mScreenReaderPreference.getKey());
+        extras.putString(AccessibilitySettings.EXTRA_TITLE, title);
+
+        String description = info.loadDescription(getPackageManager());
+        if (TextUtils.isEmpty(description)) {
+            description = getString(R.string.accessibility_service_default_description);
+        }
+        extras.putString(AccessibilitySettings.EXTRA_SUMMARY, description);
+    }
 }
diff --git a/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizardActivity.java b/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizardActivity.java
index 790d99b..ff96ec6 100644
--- a/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizardActivity.java
+++ b/src/com/android/settings/accessibility/AccessibilitySettingsForSetupWizardActivity.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2016 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.
@@ -21,8 +21,10 @@
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.view.accessibility.AccessibilityEvent;
+import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.View;
+import android.widget.FrameLayout;
 
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
@@ -38,16 +40,23 @@
 
     @Override
     protected void onCreate(Bundle savedState) {
+        // Main content frame id should be set before calling super as that is where the first
+        // Fragment is inflated.
+        setMainContentId(R.id.suw_main_content);
         super.onCreate(savedState);
 
+        // Finish configuring the content view.
+        FrameLayout parentLayout = (FrameLayout) findViewById(R.id.main_content);
+        LayoutInflater.from(this)
+                .inflate(R.layout.accessibility_settings_for_suw, parentLayout);
+
         getActionBar().setDisplayHomeAsUpEnabled(true);
         setIsDrawerPresent(false);
 
-        // Hide System Nav Bar
+        // Hide System Nav Bar.
         SystemBarHelper.hideSystemBars(getWindow());
 
-        // Show SUW Nav Bar
-        setContentView(R.layout.accessibility_settings_for_suw);
+        // Show SUW Nav Bar.
         NavigationBar navigationBar = (NavigationBar) findViewById(R.id.suw_navigation_bar);
         navigationBar.getNextButton().setVisibility(View.GONE);
         navigationBar.setNavigationBarListener(new NavigationBar.NavigationBarListener() {
@@ -122,24 +131,4 @@
                     .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
         }
     }
-
-    /**
-     * Start a new fragment.
-     *
-     * @param fragment The fragment to start
-     * @param push If true, the current fragment will be pushed onto the back stack.  If false,
-     * the current fragment will be replaced.
-     */
-    @Override
-    public void startPreferenceFragment(Fragment fragment, boolean push) {
-        FragmentTransaction transaction = getFragmentManager().beginTransaction();
-        transaction.replace(R.id.suw_main_content, fragment);
-        if (push) {
-            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
-            transaction.addToBackStack(BACK_STACK_PREFS);
-        } else {
-            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
-        }
-        transaction.commitAllowingStateLoss();
-    }
 }