• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.accessibility;
18 
19 import android.content.Intent;
20 import android.content.pm.ResolveInfo;
21 import android.os.Bundle;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 import android.view.View;
25 
26 import com.android.settings.R;
27 import com.android.settings.SettingsActivity;
28 import com.android.settings.SettingsPreferenceFragment;
29 import com.android.settings.widget.SwitchBar;
30 import com.android.settings.widget.ToggleSwitch;
31 
32 public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment {
33 
34     protected SwitchBar mSwitchBar;
35     protected ToggleSwitch mToggleSwitch;
36 
37     protected String mPreferenceKey;
38 
39     protected CharSequence mSettingsTitle;
40     protected Intent mSettingsIntent;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     public void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         final int resId = getPreferenceScreenResId();
46         if (resId <= 0) {
47             PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
48                     getActivity());
49             setPreferenceScreen(preferenceScreen);
50         }
51     }
52 
53     @Override
onViewCreated(View view, Bundle savedInstanceState)54     public void onViewCreated(View view, Bundle savedInstanceState) {
55         super.onViewCreated(view, savedInstanceState);
56 
57         SettingsActivity activity = (SettingsActivity) getActivity();
58         mSwitchBar = activity.getSwitchBar();
59         updateSwitchBarText(mSwitchBar);
60         mToggleSwitch = mSwitchBar.getSwitch();
61 
62         onProcessArguments(getArguments());
63 
64         // Show the "Settings" menu as if it were a preference screen
65         if (mSettingsTitle != null && mSettingsIntent != null) {
66             PreferenceScreen preferenceScreen = getPreferenceScreen();
67             Preference settingsPref = new Preference(preferenceScreen.getContext());
68             settingsPref.setTitle(mSettingsTitle);
69             settingsPref.setIconSpaceReserved(true);
70             settingsPref.setIntent(mSettingsIntent);
71             preferenceScreen.addPreference(settingsPref);
72         }
73     }
74 
75     @Override
onActivityCreated(Bundle savedInstanceState)76     public void onActivityCreated(Bundle savedInstanceState) {
77         super.onActivityCreated(savedInstanceState);
78         installActionBarToggleSwitch();
79     }
80 
81     @Override
onDestroyView()82     public void onDestroyView() {
83         super.onDestroyView();
84 
85         removeActionBarToggleSwitch();
86     }
87 
updateSwitchBarText(SwitchBar switchBar)88     protected void updateSwitchBarText(SwitchBar switchBar) {
89         // Implement this to provide meaningful text in switch bar
90         switchBar.setSwitchBarText(R.string.accessibility_service_master_switch_title,
91                 R.string.accessibility_service_master_switch_title);
92     }
93 
onPreferenceToggled(String preferenceKey, boolean enabled)94     protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled);
95 
onInstallSwitchBarToggleSwitch()96     protected void onInstallSwitchBarToggleSwitch() {
97         // Implement this to set a checked listener.
98     }
99 
onRemoveSwitchBarToggleSwitch()100     protected void onRemoveSwitchBarToggleSwitch() {
101         // Implement this to reset a checked listener.
102     }
103 
installActionBarToggleSwitch()104     private void installActionBarToggleSwitch() {
105         mSwitchBar.show();
106         onInstallSwitchBarToggleSwitch();
107     }
108 
removeActionBarToggleSwitch()109     private void removeActionBarToggleSwitch() {
110         mToggleSwitch.setOnBeforeCheckedChangeListener(null);
111         onRemoveSwitchBarToggleSwitch();
112         mSwitchBar.hide();
113     }
114 
setTitle(String title)115     public void setTitle(String title) {
116         getActivity().setTitle(title);
117     }
118 
onProcessArguments(Bundle arguments)119     protected void onProcessArguments(Bundle arguments) {
120         // Key.
121         mPreferenceKey = arguments.getString(AccessibilitySettings.EXTRA_PREFERENCE_KEY);
122 
123         // Enabled.
124         if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
125             final boolean enabled = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED);
126             mSwitchBar.setCheckedInternal(enabled);
127         }
128 
129         // Title.
130         if (arguments.containsKey(AccessibilitySettings.EXTRA_RESOLVE_INFO)) {
131             ResolveInfo info = arguments.getParcelable(AccessibilitySettings.EXTRA_RESOLVE_INFO);
132             getActivity().setTitle(info.loadLabel(getPackageManager()).toString());
133         } else if (arguments.containsKey(AccessibilitySettings.EXTRA_TITLE)) {
134             setTitle(arguments.getString(AccessibilitySettings.EXTRA_TITLE));
135         }
136 
137         // Summary.
138         if (arguments.containsKey(AccessibilitySettings.EXTRA_SUMMARY_RES)) {
139             final int summary = arguments.getInt(AccessibilitySettings.EXTRA_SUMMARY_RES);
140             mFooterPreferenceMixin.createFooterPreference().setTitle(summary);
141         } else if (arguments.containsKey(AccessibilitySettings.EXTRA_SUMMARY)) {
142             final CharSequence summary = arguments.getCharSequence(
143                     AccessibilitySettings.EXTRA_SUMMARY);
144             mFooterPreferenceMixin.createFooterPreference().setTitle(summary);
145         }
146     }
147 }
148