AccessibilitySettings.java revision 9b539cc584821761690d1065b309557265582bbf
1a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov/*
2a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * Copyright (C) 2009 The Android Open Source Project
3a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov *
4a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * Licensed under the Apache License, Version 2.0 (the "License");
5a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * you may not use this file except in compliance with the License.
6a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * You may obtain a copy of the License at
7a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov *
8a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov *      http://www.apache.org/licenses/LICENSE-2.0
9a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov *
10a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * Unless required by applicable law or agreed to in writing, software
11a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * distributed under the License is distributed on an "AS IS" BASIS,
12a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * See the License for the specific language governing permissions and
14a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * limitations under the License.
15a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov */
16a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
17a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovpackage com.android.settings;
18a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
196f0b4d8143858e41cede1f935230e2a430b9fd2dSvetoslav Ganovimport android.accessibilityservice.AccessibilityServiceInfo;
20672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.app.ActionBar;
21672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.app.Activity;
2200453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackbornimport android.app.ActivityManagerNative;
23a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.app.AlertDialog;
24cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganovimport android.app.Dialog;
25672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.content.ComponentName;
26672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.content.Context;
27a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.content.DialogInterface;
2805b08a50f86a91ab2db6b7e47febadc593972609Charles Chenimport android.content.Intent;
291a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganovimport android.content.SharedPreferences;
30a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.content.pm.ServiceInfo;
3100453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackbornimport android.content.res.Configuration;
3205b08a50f86a91ab2db6b7e47febadc593972609Charles Chenimport android.net.Uri;
33a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.os.Bundle;
34672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.os.Handler;
35672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.os.Message;
3600453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackbornimport android.os.RemoteException;
3705b08a50f86a91ab2db6b7e47febadc593972609Charles Chenimport android.os.SystemProperties;
38a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.preference.CheckBoxPreference;
3982623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganovimport android.preference.ListPreference;
40a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.preference.Preference;
411a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganovimport android.preference.PreferenceActivity;
426e101d125a9b874ff0337b04c94564ccf95ac7c6David Brownimport android.preference.PreferenceCategory;
43a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.preference.PreferenceScreen;
44a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.provider.Settings;
45a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.text.TextUtils;
46672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.text.TextUtils.SimpleStringSplitter;
47672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.view.Gravity;
486e101d125a9b874ff0337b04c94564ccf95ac7c6David Brownimport android.view.KeyCharacterMap;
496e101d125a9b874ff0337b04c94564ccf95ac7c6David Brownimport android.view.KeyEvent;
50672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.view.Menu;
51672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.view.MenuInflater;
52672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.view.MenuItem;
53672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.view.View;
541a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganovimport android.view.accessibility.AccessibilityEvent;
55a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport android.view.accessibility.AccessibilityManager;
56ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganovimport android.widget.LinearLayout;
57672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.widget.Switch;
58672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport android.widget.TextView;
59a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
60672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport com.android.internal.content.PackageMonitor;
61672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport com.android.settings.AccessibilitySettings.ToggleSwitch.OnBeforeCheckedChangeListener;
62672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
63672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport java.util.HashMap;
64a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport java.util.HashSet;
65a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport java.util.List;
66a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganovimport java.util.Map;
67672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolacheimport java.util.Set;
68a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
69a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov/**
70a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov * Activity with the accessibility settings.
71a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov */
7282623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganovpublic class AccessibilitySettings extends SettingsPreferenceFragment implements DialogCreatable,
7382623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov        Preference.OnPreferenceChangeListener {
74abc95ee82295b82ad0475a8b4c55a5196a5f014aSvetoslav Ganov
7505b08a50f86a91ab2db6b7e47febadc593972609Charles Chen    private static final String DEFAULT_SCREENREADER_MARKET_LINK =
7605b08a50f86a91ab2db6b7e47febadc593972609Charles Chen        "market://search?q=pname:com.google.android.marvin.talkback";
7705b08a50f86a91ab2db6b7e47febadc593972609Charles Chen
7800453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn    private static final float LARGE_FONT_SCALE = 1.3f;
7900453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn
801a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String SYSTEM_PROPERTY_MARKET_URL = "ro.screenreader.market";
811a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
82672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Timeout before we update the services if packages are added/removed since
83672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // the AccessibilityManagerService has to do that processing first to generate
84672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // the AccessibilityServiceInfo we need for proper presentation.
85672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final long DELAY_UPDATE_SERVICES_PREFERENCES_MILLIS = 1000;
86672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
87672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final char ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR = ':';
88672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
891a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String KEY_ACCESSIBILITY_TUTORIAL_LAUNCHED_ONCE =
901a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        "key_accessibility_tutorial_launched_once";
911a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
921a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String KEY_INSTALL_ACCESSIBILITY_SERVICE_OFFERED_ONCE =
931a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        "key_install_accessibility_service_offered_once";
941a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
95672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Preference categories
96672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final String SERVICES_CATEGORY = "services_category";
971a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String SYSTEM_CATEGORY = "system_category";
98672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
99672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Preferences
100672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final String TOGGLE_LARGE_TEXT_PREFERENCE = "toggle_large_text_preference";
101672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final String TOGGLE_POWER_BUTTON_ENDS_CALL_PREFERENCE =
102672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        "toggle_power_button_ends_call_preference";
103ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private static final String TOGGLE_AUTO_ROTATE_SCREEN_PREFERENCE =
104ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        "toggle_auto_rotate_screen_preference";
105672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final String TOGGLE_TOUCH_EXPLORATION_PREFERENCE =
106672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        "toggle_touch_exploration_preference";
107672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final String SELECT_LONG_PRESS_TIMEOUT_PREFERENCE =
108672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        "select_long_press_timeout_preference";
109672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private static final String TOGGLE_SCRIPT_INJECTION_PREFERENCE =
110672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        "toggle_script_injection_preference";
111672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
112672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Extras passed to sub-fragments.
1131a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String EXTRA_PREFERENCE_KEY = "preference_key";
1141a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String EXTRA_CHECKED = "checked";
1151a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String EXTRA_TITLE = "title";
1161a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String EXTRA_SUMMARY = "summary";
117ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private static final String EXTRA_ENABLE_WARNING_TITLE = "enable_warning_title";
118ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private static final String EXTRA_ENABLE_WARNING_MESSAGE = "enable_warning_message";
119ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private static final String EXTRA_DISABLE_WARNING_TITLE = "disable_warning_title";
120ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private static final String EXTRA_DISABLE_WARNING_MESSAGE = "disable_warning_message";
1211a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String EXTRA_SETTINGS_TITLE = "settings_title";
1221a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static final String EXTRA_SETTINGS_COMPONENT_NAME = "settings_component_name";
123672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
124672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Dialog IDs.
125ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private static final int DIALOG_ID_NO_ACCESSIBILITY_SERVICES = 1;
126a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
127672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Auxiliary members.
128672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private final SimpleStringSplitter mStringColonSplitter =
129672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        new SimpleStringSplitter(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR);
1303d64653d1f8dd58fb05ff845706eda5c7f344d0bSvetoslav Ganov
131672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private final Map<String, String> mLongPressTimeoutValuetoTitleMap =
132672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        new HashMap<String, String>();
1336e101d125a9b874ff0337b04c94564ccf95ac7c6David Brown
134672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private final Configuration mCurConfig = new Configuration();
135cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov
136672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private final PackageMonitor mSettingsPackageMonitor = new SettingsPackageMonitor();
13782623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov
138672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private final Handler mHandler = new Handler() {
139672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
140672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void dispatchMessage(Message msg) {
141672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            super.dispatchMessage(msg);
142ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            updateServicesPreferences();
143672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
144672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    };
14500453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn
146672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    // Preference controls.
147672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private PreferenceCategory mServicesCategory;
1481a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private PreferenceCategory mSystemsCategory;
149672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
150672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private CheckBoxPreference mToggleLargeTextPreference;
151672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private CheckBoxPreference mTogglePowerButtonEndsCallPreference;
152ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private CheckBoxPreference mToggleAutoRotateScreenPreference;
1531a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private Preference mToggleTouchExplorationPreference;
154672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private ListPreference mSelectLongPressTimeoutPreference;
1551a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private AccessibilityEnableScriptInjectionPreference mToggleScriptInjectionPreference;
1569b539cc584821761690d1065b309557265582bbfSvetoslav Ganov    private Preference mNoServicesMessagePreference;
1571a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
1581a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private int mLongPressTimeoutDefault;
159a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
160a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    @Override
161d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    public void onCreate(Bundle icicle) {
162a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        super.onCreate(icicle);
163a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        addPreferencesFromResource(R.xml.accessibility_settings);
1641a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        initializeAllPreferences();
165cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov    }
166cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov
167cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov    @Override
168672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    public void onResume() {
169672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        super.onResume();
170ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        updateAllPreferences();
171ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        if (mServicesCategory.getPreference(0) == mNoServicesMessagePreference) {
1721a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            offerInstallAccessibilitySerivceOnce();
1731a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        }
174672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        mSettingsPackageMonitor.register(getActivity(), false);
175749ba652279bb4131a1a9dbe3f5d2cc4302c596dSvetoslav Ganov    }
176749ba652279bb4131a1a9dbe3f5d2cc4302c596dSvetoslav Ganov
177749ba652279bb4131a1a9dbe3f5d2cc4302c596dSvetoslav Ganov    @Override
178749ba652279bb4131a1a9dbe3f5d2cc4302c596dSvetoslav Ganov    public void onPause() {
179672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        mSettingsPackageMonitor.unregister();
1801a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        super.onPause();
1811a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    }
1821a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
18382623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov    public boolean onPreferenceChange(Preference preference, Object newValue) {
184672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        if (preference == mSelectLongPressTimeoutPreference) {
1851a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            String stringValue = (String) newValue;
18682623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov            Settings.Secure.putInt(getContentResolver(),
1871a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    Settings.Secure.LONG_PRESS_TIMEOUT, Integer.parseInt(stringValue));
188672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mSelectLongPressTimeoutPreference.setSummary(
1891a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    mLongPressTimeoutValuetoTitleMap.get(stringValue));
19082623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov            return true;
19182623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov        }
19282623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov        return false;
19382623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov    }
19482623019d21ceb36aaa9711a4267147883442ce3Svetoslav Ganov
195a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    @Override
196a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
197672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        if (mToggleLargeTextPreference == preference) {
1981a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            handleToggleLargeTextPreferenceClick();
199672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            return true;
200672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        } else if (mTogglePowerButtonEndsCallPreference == preference) {
2011a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            handleTogglePowerButtonEndsCallPreferenceClick();
202672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            return true;
203ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        } else if (mToggleAutoRotateScreenPreference == preference) {
204ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            handleToggleAutoRotateScreenPreferenceClick();
205ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            return true;
206a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        }
207a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        return super.onPreferenceTreeClick(preferenceScreen, preference);
208a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    }
209a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
2101a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private void handleToggleLargeTextPreferenceClick() {
211672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        try {
2121a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            mCurConfig.fontScale = mToggleLargeTextPreference.isChecked() ? LARGE_FONT_SCALE : 1;
213672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
2141a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        } catch (RemoteException re) {
215672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            /* ignore */
2163d64653d1f8dd58fb05ff845706eda5c7f344d0bSvetoslav Ganov        }
2173d64653d1f8dd58fb05ff845706eda5c7f344d0bSvetoslav Ganov    }
2183d64653d1f8dd58fb05ff845706eda5c7f344d0bSvetoslav Ganov
2191a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private void handleTogglePowerButtonEndsCallPreferenceClick() {
2201a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        Settings.Secure.putInt(getContentResolver(),
2211a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
2221a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                (mTogglePowerButtonEndsCallPreference.isChecked()
2231a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        ? Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP
2241a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        : Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_SCREEN_OFF));
225a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    }
226a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
227ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private void handleToggleAutoRotateScreenPreferenceClick() {
228ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        Settings.System.putInt(getContentResolver(),
229ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                Settings.System.ACCELEROMETER_ROTATION,
230ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                (mToggleAutoRotateScreenPreference.isChecked() ? 1 : 0));
2311a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    }
2321a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
2331a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private void initializeAllPreferences() {
234672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        mServicesCategory = (PreferenceCategory) findPreference(SERVICES_CATEGORY);
2351a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mSystemsCategory = (PreferenceCategory) findPreference(SYSTEM_CATEGORY);
2361a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
2371a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Large text.
2381a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mToggleLargeTextPreference =
2391a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            (CheckBoxPreference) findPreference(TOGGLE_LARGE_TEXT_PREFERENCE);
2401a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
2411a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Power button ends calls.
2421a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mTogglePowerButtonEndsCallPreference =
2431a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            (CheckBoxPreference) findPreference(TOGGLE_POWER_BUTTON_ENDS_CALL_PREFERENCE);
244ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        if (!KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER)
245ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                || !Utils.isVoiceCapable(getActivity())) {
2461a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            mSystemsCategory.removePreference(mTogglePowerButtonEndsCallPreference);
2471a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        }
2481a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
249ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        // Auto-rotate screen
250ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        mToggleAutoRotateScreenPreference =
251ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            (CheckBoxPreference) findPreference(TOGGLE_AUTO_ROTATE_SCREEN_PREFERENCE);
252ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
2531a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Touch exploration enabled.
2541a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mToggleTouchExplorationPreference = findPreference(TOGGLE_TOUCH_EXPLORATION_PREFERENCE);
255672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
2561a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Long press timeout.
2571a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mSelectLongPressTimeoutPreference =
2581a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            (ListPreference) findPreference(SELECT_LONG_PRESS_TIMEOUT_PREFERENCE);
259672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        mSelectLongPressTimeoutPreference.setOnPreferenceChangeListener(this);
2601a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        if (mLongPressTimeoutValuetoTitleMap.size() == 0) {
2611a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            String[] timeoutValues = getResources().getStringArray(
2621a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    R.array.long_press_timeout_selector_values);
2631a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            mLongPressTimeoutDefault = Integer.parseInt(timeoutValues[0]);
2641a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            String[] timeoutTitles = getResources().getStringArray(
2651a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    R.array.long_press_timeout_selector_titles);
2661a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            final int timeoutValueCount = timeoutValues.length;
2671a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            for (int i = 0; i < timeoutValueCount; i++) {
2681a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov               mLongPressTimeoutValuetoTitleMap.put(timeoutValues[i], timeoutTitles[i]);
2691a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            }
2701a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        }
2711a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
2721a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Script injection.
2731a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mToggleScriptInjectionPreference = (AccessibilityEnableScriptInjectionPreference)
2741a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            findPreference(TOGGLE_SCRIPT_INJECTION_PREFERENCE);
275a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    }
276a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
277ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private void updateAllPreferences() {
278ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        updateServicesPreferences();
279ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        updateSystemPreferences();
2801a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    }
2811a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
282ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private void updateServicesPreferences() {
2831a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Since services category is auto generated we have to do a pass
2841a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // to generate it since services can come and go and then based on
2851a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // the global accessibility state to decided whether it is enabled.
2861a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
2871a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Generate.
288672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        mServicesCategory.removeAll();
289672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
290672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(getActivity());
291a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
2926f0b4d8143858e41cede1f935230e2a430b9fd2dSvetoslav Ganov        List<AccessibilityServiceInfo> installedServices =
2936f0b4d8143858e41cede1f935230e2a430b9fd2dSvetoslav Ganov            accessibilityManager.getInstalledAccessibilityServiceList();
294a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
295672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        Set<ComponentName> enabledComponentNames = new HashSet<ComponentName>();
296672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        String settingValue = Settings.Secure.getString(getContentResolver(),
297672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
298672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        if (settingValue != null) {
299672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            SimpleStringSplitter splitter = mStringColonSplitter;
300672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            splitter.setString(settingValue);
301672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            while (splitter.hasNext()) {
302672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                enabledComponentNames.add(ComponentName.unflattenFromString(splitter.next()));
30300453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn            }
304a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        }
305a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov
306a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        for (int i = 0, count = installedServices.size(); i < count; ++i) {
307672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            AccessibilityServiceInfo info = installedServices.get(i);
308672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
309672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(
310672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    getActivity());
311672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();
312672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
313672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
314672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            ComponentName componentName = new ComponentName(serviceInfo.packageName,
315672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    serviceInfo.name);
316abc95ee82295b82ad0475a8b4c55a5196a5f014aSvetoslav Ganov
317672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            preference.setKey(componentName.flattenToString());
318672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
319672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            preference.setTitle(title);
320672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            final boolean enabled = enabledComponentNames.contains(componentName);
321672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            if (enabled) {
322672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                preference.setSummary(getString(R.string.accessibility_service_state_on));
323672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            } else {
324672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                preference.setSummary(getString(R.string.accessibility_service_state_off));
325672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
326672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
327672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            preference.setOrder(i);
328672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            preference.setFragment(ToggleAccessibilityServiceFragment.class.getName());
3291a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            preference.setPersistent(true);
330672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
331672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Bundle extras = preference.getExtras();
332672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            extras.putString(EXTRA_PREFERENCE_KEY, preference.getKey());
333672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            extras.putBoolean(EXTRA_CHECKED, enabled);
334672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            extras.putString(EXTRA_TITLE, title);
3351a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
3361a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            String description = info.getDescription();
3371a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            if (TextUtils.isEmpty(description)) {
3381a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                description = getString(R.string.accessibility_service_default_description);
3391a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            }
3401a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            extras.putString(EXTRA_SUMMARY, description);
3411a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
342ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            CharSequence applicationLabel = info.getResolveInfo().loadLabel(getPackageManager());
343ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
344ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            extras.putString(EXTRA_ENABLE_WARNING_TITLE, getString(
345ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    R.string.accessibility_service_security_warning_title, applicationLabel));
346ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            extras.putString(EXTRA_ENABLE_WARNING_MESSAGE, getString(
347ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    R.string.accessibility_service_security_warning_summary, applicationLabel));
348ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
349ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            extras.putString(EXTRA_DISABLE_WARNING_TITLE, getString(
350ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    R.string.accessibility_service_disable_warning_title,
351ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    applicationLabel));
352ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            extras.putString(EXTRA_DISABLE_WARNING_MESSAGE, getString(
353ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    R.string.accessibility_service_disable_warning_summary,
354ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    applicationLabel));
355672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
356672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            String settingsClassName = info.getSettingsActivityName();
357672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            if (!TextUtils.isEmpty(settingsClassName)) {
358672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                extras.putString(EXTRA_SETTINGS_TITLE,
359672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        getString(R.string.accessibility_menu_item_settings));
360672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                extras.putString(EXTRA_SETTINGS_COMPONENT_NAME,
361672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        new ComponentName(info.getResolveInfo().serviceInfo.packageName,
362672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                                settingsClassName).flattenToString());
363cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov            }
364672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
365672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mServicesCategory.addPreference(preference);
366a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov        }
3671a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
368ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        if (mServicesCategory.getPreferenceCount() == 0) {
369ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            if (mNoServicesMessagePreference == null) {
370ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                mNoServicesMessagePreference = new Preference(getActivity()) {
371ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    @Override
372ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    protected void onBindView(View view) {
373ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        super.onBindView(view);
374ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
375ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        LinearLayout containerView =
376ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            (LinearLayout) view.findViewById(R.id.message_container);
377ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        containerView.setGravity(Gravity.CENTER);
378ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
379ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        TextView summaryView = (TextView) view.findViewById(R.id.summary);
3809b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                        String title = getString(R.string.accessibility_no_services_installed);
381ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        summaryView.setText(title);
382ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    }
383ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                };
384ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                mNoServicesMessagePreference.setPersistent(false);
385ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                mNoServicesMessagePreference.setLayoutResource(
386ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        R.layout.text_description_preference);
387ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                mNoServicesMessagePreference.setSelectable(false);
388ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            }
389ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mServicesCategory.addPreference(mNoServicesMessagePreference);
390ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        }
391a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov    }
39205b08a50f86a91ab2db6b7e47febadc593972609Charles Chen
393ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov    private void updateSystemPreferences() {
394672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        // Large text.
39500453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn        try {
396ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
397672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        } catch (RemoteException re) {
398672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            /* ignore */
39900453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn        }
400ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        mToggleLargeTextPreference.setChecked(mCurConfig.fontScale == LARGE_FONT_SCALE);
401672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
4021a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // Power button ends calls.
403ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        if (KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER)
404ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                && Utils.isVoiceCapable(getActivity())) {
405ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            final int incallPowerBehavior = Settings.Secure.getInt(getContentResolver(),
4061a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
407ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_DEFAULT);
408ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            final boolean powerButtonEndsCall =
409ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                (incallPowerBehavior == Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP);
410ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mTogglePowerButtonEndsCallPreference.setChecked(powerButtonEndsCall);
411672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
412672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
413ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        // Auto-rotate screen
414ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        final boolean autoRotationEnabled = Settings.System.getInt(getContentResolver(),
415ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                Settings.System.ACCELEROMETER_ROTATION, 0) != 0;
416ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        mToggleAutoRotateScreenPreference.setChecked(autoRotationEnabled);
417ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
418672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        // Touch exploration enabled.
4199b539cc584821761690d1065b309557265582bbfSvetoslav Ganov        if (AccessibilityManager.getInstance(getActivity()).isEnabled()) {
4209b539cc584821761690d1065b309557265582bbfSvetoslav Ganov            mSystemsCategory.addPreference(mToggleTouchExplorationPreference);
4219b539cc584821761690d1065b309557265582bbfSvetoslav Ganov            final boolean touchExplorationEnabled = (Settings.Secure.getInt(getContentResolver(),
4229b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                    Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0) == 1);
4239b539cc584821761690d1065b309557265582bbfSvetoslav Ganov            if (touchExplorationEnabled) {
4249b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                mToggleTouchExplorationPreference.setSummary(
4259b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                        getString(R.string.accessibility_service_state_on));
4269b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                mToggleTouchExplorationPreference.getExtras().putBoolean(EXTRA_CHECKED, true);
4279b539cc584821761690d1065b309557265582bbfSvetoslav Ganov            } else {
4289b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                mToggleTouchExplorationPreference.setSummary(
4299b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                        getString(R.string.accessibility_service_state_off));
4309b539cc584821761690d1065b309557265582bbfSvetoslav Ganov                mToggleTouchExplorationPreference.getExtras().putBoolean(EXTRA_CHECKED, false);
4319b539cc584821761690d1065b309557265582bbfSvetoslav Ganov            }
4329b539cc584821761690d1065b309557265582bbfSvetoslav Ganov
433672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        } else {
4349b539cc584821761690d1065b309557265582bbfSvetoslav Ganov            mSystemsCategory.removePreference(mToggleTouchExplorationPreference);
435672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
436672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
437672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        // Long press timeout.
438ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        final int longPressTimeout = Settings.Secure.getInt(getContentResolver(),
439ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                Settings.Secure.LONG_PRESS_TIMEOUT, mLongPressTimeoutDefault);
440ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        String value = String.valueOf(longPressTimeout);
441ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        mSelectLongPressTimeoutPreference.setValue(value);
4421a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        mSelectLongPressTimeoutPreference.setSummary(mLongPressTimeoutValuetoTitleMap.get(value));
443672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
444672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        // Script injection.
445ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        final boolean  scriptInjectionAllowed = (Settings.Secure.getInt(getContentResolver(),
446ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION, 0) == 1);
447ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        mToggleScriptInjectionPreference.setInjectionAllowed(scriptInjectionAllowed);
4481a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    }
4491a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
4501a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private void offerInstallAccessibilitySerivceOnce() {
4519b539cc584821761690d1065b309557265582bbfSvetoslav Ganov        // There is always one preference - if no services it is just a message.
4529b539cc584821761690d1065b309557265582bbfSvetoslav Ganov        if (mServicesCategory.getPreference(0) != mNoServicesMessagePreference) {
4531a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            return;
4541a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        }
4551a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        SharedPreferences preferences = getActivity().getPreferences(Context.MODE_PRIVATE);
4561a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        final boolean offerInstallService = !preferences.getBoolean(
4571a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                KEY_INSTALL_ACCESSIBILITY_SERVICE_OFFERED_ONCE, false);
4581a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        if (offerInstallService) {
4591a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            preferences.edit().putBoolean(KEY_INSTALL_ACCESSIBILITY_SERVICE_OFFERED_ONCE,
4601a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    true).commit();
4611a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            // Notify user that they do not have any accessibility
4621a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            // services installed and direct them to Market to get TalkBack.
4631a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            showDialog(DIALOG_ID_NO_ACCESSIBILITY_SERVICES);
464672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
46500453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn    }
46600453157ae62d3ba63fe42eccf5113d89e15cab7Dianne Hackborn
467cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov    @Override
468cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov    public Dialog onCreateDialog(int dialogId) {
469cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov        switch (dialogId) {
470cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov            case DIALOG_ID_NO_ACCESSIBILITY_SERVICES:
471cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                return new AlertDialog.Builder(getActivity())
472cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                    .setTitle(R.string.accessibility_service_no_apps_title)
473cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                    .setMessage(R.string.accessibility_service_no_apps_message)
474cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                    .setPositiveButton(android.R.string.ok,
475cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                        new DialogInterface.OnClickListener() {
476cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                            public void onClick(DialogInterface dialog, int which) {
477cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                // dismiss the dialog before launching the activity otherwise
478cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                // the dialog removal occurs after onSaveInstanceState which
479cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                // triggers an exception
4801a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                                removeDialog(DIALOG_ID_NO_ACCESSIBILITY_SERVICES);
481cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                String screenreaderMarketLink = SystemProperties.get(
4821a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                                        SYSTEM_PROPERTY_MARKET_URL,
4831a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                                        DEFAULT_SCREENREADER_MARKET_LINK);
484cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                Uri marketUri = Uri.parse(screenreaderMarketLink);
485cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
486cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                                startActivity(marketIntent);
487cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                            }
488cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                    })
489cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                    .setNegativeButton(android.R.string.cancel, null)
490cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                    .create();
491cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov            default:
492cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov                return null;
493cdd8e34b4fd2a751490b5adfae048563fa14d9d5Svetoslav Ganov        }
49405b08a50f86a91ab2db6b7e47febadc593972609Charles Chen    }
495672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
496672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private class SettingsPackageMonitor extends PackageMonitor {
497672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
498672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
499672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onPackageAdded(String packageName, int uid) {
500672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Message message = mHandler.obtainMessage();
501672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mHandler.sendMessageDelayed(message, DELAY_UPDATE_SERVICES_PREFERENCES_MILLIS);
502672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
503672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
504672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
505672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onPackageAppeared(String packageName, int reason) {
506672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Message message = mHandler.obtainMessage();
507672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mHandler.sendMessageDelayed(message, DELAY_UPDATE_SERVICES_PREFERENCES_MILLIS);
508672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
509672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
510672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
511672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onPackageDisappeared(String packageName, int reason) {
512672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Message message = mHandler.obtainMessage();
513672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mHandler.sendMessageDelayed(message, DELAY_UPDATE_SERVICES_PREFERENCES_MILLIS);
514672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
515672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
516672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
517672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onPackageRemoved(String packageName, int uid) {
518672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Message message = mHandler.obtainMessage();
519672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mHandler.sendMessageDelayed(message, DELAY_UPDATE_SERVICES_PREFERENCES_MILLIS);
520672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
521672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    }
522672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
5231a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov    private static ToggleSwitch createAndAddActionBarToggleSwitch(Activity activity) {
524672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        ToggleSwitch toggleSwitch = new ToggleSwitch(activity);
525672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        final int padding = activity.getResources().getDimensionPixelSize(
526672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                R.dimen.action_bar_switch_padding);
527672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        toggleSwitch.setPadding(0, 0, padding, 0);
528672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
529672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                ActionBar.DISPLAY_SHOW_CUSTOM);
530672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        activity.getActionBar().setCustomView(toggleSwitch,
531672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
532672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        ActionBar.LayoutParams.WRAP_CONTENT,
533672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        Gravity.CENTER_VERTICAL | Gravity.RIGHT));
534672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        return toggleSwitch;
535672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    }
536672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
537672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    public static class ToggleSwitch extends Switch {
538672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
539672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private OnBeforeCheckedChangeListener mOnBeforeListener;
540672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
541672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public static interface OnBeforeCheckedChangeListener {
542672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked);
543672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
544672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
545672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public ToggleSwitch(Context context) {
546672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            super(context);
547672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
548672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
549672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void setOnBeforeCheckedChangeListener(OnBeforeCheckedChangeListener listener) {
550672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mOnBeforeListener = listener;
551672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
552672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
553672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
554672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void setChecked(boolean checked) {
555672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            if (mOnBeforeListener != null
556672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    && mOnBeforeListener.onBeforeCheckedChanged(this, checked)) {
557672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                return;
558672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
559672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            super.setChecked(checked);
560672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
561672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
5621a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        public void setCheckedInternal(boolean checked) {
563672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            super.setChecked(checked);
564672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
565672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    }
566672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
567672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    public static class ToggleAccessibilityServiceFragment extends TogglePreferenceFragment {
568672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
569672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onPreferenceToggled(String preferenceKey, boolean enabled) {
570672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            String enabledServices = Settings.Secure.getString(getContentResolver(),
571672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
572672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            if (enabledServices == null) {
573672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                enabledServices = "";
574672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
575672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            final int length = enabledServices.length();
576672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            if (enabled) {
577672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                if (enabledServices.contains(preferenceKey)) {
578672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    return;
579672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                }
580672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                if (length == 0) {
581672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    enabledServices += preferenceKey;
582672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    Settings.Secure.putString(getContentResolver(),
583672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServices);
584ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    // Enabling the first service enables accessibility.
585ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    Settings.Secure.putInt(getContentResolver(),
586ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            Settings.Secure.ACCESSIBILITY_ENABLED, 1);
587672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                } else if (length > 0) {
588672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    enabledServices += ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR + preferenceKey;
589672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    Settings.Secure.putString(getContentResolver(),
590672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServices);
591672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                }
592672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            } else {
593672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                final int index = enabledServices.indexOf(preferenceKey);
594672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                if (index == 0) {
595672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    enabledServices = enabledServices.replace(preferenceKey, "");
596672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    Settings.Secure.putString(getContentResolver(),
597672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServices);
598ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    // Disabling the last service disables accessibility).
599ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    Settings.Secure.putInt(getContentResolver(),
600ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            Settings.Secure.ACCESSIBILITY_ENABLED, 0);
601672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                } else if (index > 0) {
602672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    enabledServices = enabledServices.replace(
603672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                            ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR + preferenceKey, "");
604672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    Settings.Secure.putString(getContentResolver(),
605672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServices);
606672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                }
607672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
608672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
609672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    }
610672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
611672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    public static class ToggleTouchExplorationFragment extends TogglePreferenceFragment {
612672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
613672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onPreferenceToggled(String preferenceKey, boolean enabled) {
614672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Settings.Secure.putInt(getContentResolver(),
615672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    Settings.Secure.TOUCH_EXPLORATION_ENABLED, enabled ? 1 : 0);
6161a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            if (enabled) {
6171a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                SharedPreferences preferences = getActivity().getPreferences(Context.MODE_PRIVATE);
6181a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                final boolean launchAccessibilityTutorial = !preferences.getBoolean(
6191a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        KEY_ACCESSIBILITY_TUTORIAL_LAUNCHED_ONCE, false);
6201a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                if (launchAccessibilityTutorial) {
6211a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    preferences.edit().putBoolean(KEY_ACCESSIBILITY_TUTORIAL_LAUNCHED_ONCE,
6221a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                            true).commit();
6231a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    Intent intent = new Intent(AccessibilityTutorialActivity.ACTION);
6241a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    getActivity().startActivity(intent);
6251a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                }
6261a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            }
627672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
628672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    }
629672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
630672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    private abstract static class TogglePreferenceFragment extends SettingsPreferenceFragment
631672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            implements DialogInterface.OnClickListener {
632672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
633ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private static final int DIALOG_ID_ENABLE_WARNING = 1;
634ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private static final int DIALOG_ID_DISABLE_WARNING = 2;
635672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
636672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private String mPreferenceKey;
637672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
638672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private ToggleSwitch mToggleSwitch;
639672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
640ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private CharSequence mEnableWarningTitle;
641ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private CharSequence mEnableWarningMessage;
642ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private CharSequence mDisableWarningTitle;
643ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private CharSequence mDisableWarningMessage;
644672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private Preference mSummaryPreference;
645672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
646672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private CharSequence mSettingsTitle;
647672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private Intent mSettingsIntent;
648672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
649ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov        private int mShownDialogId;
650ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
6511a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        // TODO: Showing sub-sub fragment does not handle the activity title
6521a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        //       so we do it but this is wrong. Do a real fix when there is time.
6531a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        private CharSequence mOldActivityTitle;
654672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
655672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
656672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onCreate(Bundle savedInstanceState) {
657672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            super.onCreate(savedInstanceState);
658672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
659672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    getActivity());
660672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            setPreferenceScreen(preferenceScreen);
661672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mSummaryPreference = new Preference(getActivity()) {
662672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                @Override
663672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                protected void onBindView(View view) {
664672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    super.onBindView(view);
665672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    TextView summaryView = (TextView) view.findViewById(R.id.summary);
666672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    summaryView.setText(getSummary());
6671a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    sendAccessibilityEvent(summaryView);
6681a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                }
6691a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
6701a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                private void sendAccessibilityEvent(View view) {
6711a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    // Since the view is still not attached we create, populate,
6721a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    // and send the event directly since we do not know when it
6731a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    // will be attached and posting commands is not as clean.
6741a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    AccessibilityManager accessibilityManager =
6751a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        AccessibilityManager.getInstance(getActivity());
6761a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    if (accessibilityManager.isEnabled()) {
6771a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        AccessibilityEvent event = AccessibilityEvent.obtain();
6781a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        event.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED);
6791a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        view.onInitializeAccessibilityEvent(event);
6801a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        view.dispatchPopulateAccessibilityEvent(event);
6811a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                        accessibilityManager.sendAccessibilityEvent(event);
6821a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                    }
683672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                }
684672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            };
685672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mSummaryPreference.setPersistent(false);
686672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mSummaryPreference.setLayoutResource(R.layout.text_description_preference);
687672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            preferenceScreen.addPreference(mSummaryPreference);
688672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
689672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
6901a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        @Override
6911a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        public void onViewCreated(View view, Bundle savedInstanceState) {
6921a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            super.onViewCreated(view, savedInstanceState);
6931a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            installActionBarToggleSwitch();
6941a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            processArguments();
6951a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            getListView().setDivider(null);
6961a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            getListView().setEnabled(false);
6971a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        }
6981a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
6991a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        @Override
7001a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        public void onDestroyView() {
7011a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            getActivity().getActionBar().setCustomView(null);
7021a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            if (mOldActivityTitle != null) {
7031a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                getActivity().getActionBar().setTitle(mOldActivityTitle);
7041a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            }
7051b52e0d0240040478ebbe9ac2244816c39ddcd4eAmith Yamasani            mToggleSwitch.setOnBeforeCheckedChangeListener(null);
7061a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            super.onDestroyView();
7071a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov        }
7081a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov
709672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public abstract void onPreferenceToggled(String preferenceKey, boolean value);
710672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
711672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
712672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
713672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            super.onCreateOptionsMenu(menu, inflater);
714672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            MenuItem menuItem = menu.add(mSettingsTitle);
715ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
716672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            menuItem.setIntent(mSettingsIntent);
717672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
718672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
719672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
720672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public Dialog onCreateDialog(int dialogId) {
721ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            CharSequence title = null;
722ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            CharSequence message = null;
723672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            switch (dialogId) {
724ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                case DIALOG_ID_ENABLE_WARNING:
725ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    mShownDialogId = DIALOG_ID_ENABLE_WARNING;
726ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    title = mEnableWarningTitle;
727ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    message = mEnableWarningMessage;
728ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    break;
729ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                case DIALOG_ID_DISABLE_WARNING:
730ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    mShownDialogId = DIALOG_ID_DISABLE_WARNING;
731ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    title = mDisableWarningTitle;
732ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    message = mDisableWarningMessage;
733ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    break;
734672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                default:
735672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    throw new IllegalArgumentException();
736672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
737ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            return new AlertDialog.Builder(getActivity())
738ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .setTitle(title)
739ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .setIcon(android.R.drawable.ic_dialog_alert)
740ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .setMessage(message)
741ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .setCancelable(true)
742ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .setPositiveButton(android.R.string.ok, this)
743ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .setNegativeButton(android.R.string.cancel, this)
744ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                .create();
745672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
746672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
747672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        @Override
748672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        public void onClick(DialogInterface dialog, int which) {
749ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            final boolean checked;
750672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            switch (which) {
751672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                case DialogInterface.BUTTON_POSITIVE:
752ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    checked = (mShownDialogId == DIALOG_ID_ENABLE_WARNING);
753ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    mToggleSwitch.setCheckedInternal(checked);
754ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    onPreferenceToggled(mPreferenceKey, checked);
755672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    break;
756672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                case DialogInterface.BUTTON_NEGATIVE:
757ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    checked = (mShownDialogId == DIALOG_ID_DISABLE_WARNING);
758ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    mToggleSwitch.setCheckedInternal(checked);
759ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    onPreferenceToggled(mPreferenceKey, checked);
760672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    break;
761672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                default:
762672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    throw new IllegalArgumentException();
763672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
764672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
765672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
766672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private void installActionBarToggleSwitch() {
7671a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            mToggleSwitch = createAndAddActionBarToggleSwitch(getActivity());
768672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
769672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                @Override
770672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
771672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    if (checked) {
772ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        if (!TextUtils.isEmpty(mEnableWarningMessage)) {
7731a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                            toggleSwitch.setCheckedInternal(false);
774ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            showDialog(DIALOG_ID_ENABLE_WARNING);
775672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                            return true;
776672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        }
777672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        onPreferenceToggled(mPreferenceKey, true);
778672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    } else {
779ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        if (!TextUtils.isEmpty(mDisableWarningMessage)) {
780ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            toggleSwitch.setCheckedInternal(true);
781ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            showDialog(DIALOG_ID_DISABLE_WARNING);
782ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                            return true;
783ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                        }
784672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        onPreferenceToggled(mPreferenceKey, false);
785672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    }
786672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    return false;
787672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                }
788672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            });
789672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
790672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
791672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        private void processArguments() {
792672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            Bundle arguments = getArguments();
793672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
794672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            // Key.
795672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mPreferenceKey = arguments.getString(EXTRA_PREFERENCE_KEY);
796672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
797672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            // Enabled.
798672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            final boolean enabled = arguments.getBoolean(EXTRA_CHECKED);
7991a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            mToggleSwitch.setCheckedInternal(enabled);
800672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
801672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            // Title.
8021a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            PreferenceActivity activity = (PreferenceActivity) getActivity();
8031a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            if (!activity.onIsMultiPane() || activity.onIsHidingHeaders()) {
8041a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                mOldActivityTitle = getActivity().getTitle();
8051a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov                String title = arguments.getString(EXTRA_TITLE);
806ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                getActivity().getActionBar().setTitle(title);
8071a97fa13d03d0966ccaa1480c33d8bedce734407Svetoslav Ganov            }
808672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
809672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            // Summary.
810672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            String summary = arguments.getString(EXTRA_SUMMARY);
811672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            mSummaryPreference.setSummary(summary);
812672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
813672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            // Settings title and intent.
814672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            String settingsTitle = arguments.getString(EXTRA_SETTINGS_TITLE);
815672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            String settingsComponentName = arguments.getString(EXTRA_SETTINGS_COMPONENT_NAME);
816672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            if (!TextUtils.isEmpty(settingsTitle) && !TextUtils.isEmpty(settingsComponentName)) {
817672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                Intent settingsIntent = new Intent(Intent.ACTION_MAIN).setComponent(
818672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                        ComponentName.unflattenFromString(settingsComponentName.toString()));
819672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                if (!getPackageManager().queryIntentActivities(settingsIntent, 0).isEmpty()) {
820672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    mSettingsTitle = settingsTitle;
821672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    mSettingsIntent = settingsIntent;
822672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                    setHasOptionsMenu(true);
823672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache                }
824672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache            }
825672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache
826ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            // Enable warning title.
827ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mEnableWarningTitle = arguments.getCharSequence(
828ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    AccessibilitySettings.EXTRA_ENABLE_WARNING_TITLE);
829ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
830ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            // Enable warning message.
831ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mEnableWarningMessage = arguments.getCharSequence(
832ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    AccessibilitySettings.EXTRA_ENABLE_WARNING_MESSAGE);
833ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
834ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            // Disable warning title.
835ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mDisableWarningTitle = arguments.getString(
836ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    AccessibilitySettings.EXTRA_DISABLE_WARNING_TITLE);
837ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov
838ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            // Disable warning message.
839ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov            mDisableWarningMessage = arguments.getString(
840ea5a50a708389b8ff0d898ffe080149d14377b07Svetoslav Ganov                    AccessibilitySettings.EXTRA_DISABLE_WARNING_MESSAGE);
841672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache        }
842672b3c6576efa9e8e4ad0ebbea7f0c36ecf77d9bCostin Manolache    }
843a05d0dc67eb6aae169308dda25fe235226fa395bsvetoslavganov}
844