• 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.view.View;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.SettingsActivity;
29 import com.android.settings.SettingsPreferenceFragment;
30 import com.android.settings.widget.SwitchBar;
31 import com.android.settings.widget.ToggleSwitch;
32 
33 public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment {
34 
35     protected SwitchBar mSwitchBar;
36     protected ToggleSwitch mToggleSwitch;
37 
38     protected String mPreferenceKey;
39 
40     protected CharSequence mSettingsTitle;
41     protected Intent mSettingsIntent;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         final int resId = getPreferenceScreenResId();
47         if (resId <= 0) {
48             PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
49                     getActivity());
50             setPreferenceScreen(preferenceScreen);
51         }
52     }
53 
54     @Override
onViewCreated(View view, Bundle savedInstanceState)55     public void onViewCreated(View view, Bundle savedInstanceState) {
56         super.onViewCreated(view, savedInstanceState);
57 
58         SettingsActivity activity = (SettingsActivity) getActivity();
59         mSwitchBar = activity.getSwitchBar();
60         updateSwitchBarText(mSwitchBar);
61         mToggleSwitch = mSwitchBar.getSwitch();
62 
63         onProcessArguments(getArguments());
64 
65         // Show the "Settings" menu as if it were a preference screen
66         if (mSettingsTitle != null && mSettingsIntent != null) {
67             PreferenceScreen preferenceScreen = getPreferenceScreen();
68             Preference settingsPref = new Preference(preferenceScreen.getContext());
69             settingsPref.setTitle(mSettingsTitle);
70             settingsPref.setIconSpaceReserved(true);
71             settingsPref.setIntent(mSettingsIntent);
72             preferenceScreen.addPreference(settingsPref);
73         }
74     }
75 
76     @Override
onActivityCreated(Bundle savedInstanceState)77     public void onActivityCreated(Bundle savedInstanceState) {
78         super.onActivityCreated(savedInstanceState);
79         installActionBarToggleSwitch();
80     }
81 
82     @Override
onDestroyView()83     public void onDestroyView() {
84         super.onDestroyView();
85 
86         removeActionBarToggleSwitch();
87     }
88 
updateSwitchBarText(SwitchBar switchBar)89     protected void updateSwitchBarText(SwitchBar switchBar) {
90         // Implement this to provide meaningful text in switch bar
91         switchBar.setSwitchBarText(R.string.accessibility_service_master_switch_title,
92                 R.string.accessibility_service_master_switch_title);
93     }
94 
onPreferenceToggled(String preferenceKey, boolean enabled)95     protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled);
96 
onInstallSwitchBarToggleSwitch()97     protected void onInstallSwitchBarToggleSwitch() {
98         // Implement this to set a checked listener.
99     }
100 
onRemoveSwitchBarToggleSwitch()101     protected void onRemoveSwitchBarToggleSwitch() {
102         // Implement this to reset a checked listener.
103     }
104 
installActionBarToggleSwitch()105     private void installActionBarToggleSwitch() {
106         mSwitchBar.show();
107         onInstallSwitchBarToggleSwitch();
108     }
109 
removeActionBarToggleSwitch()110     private void removeActionBarToggleSwitch() {
111         mToggleSwitch.setOnBeforeCheckedChangeListener(null);
112         onRemoveSwitchBarToggleSwitch();
113         mSwitchBar.hide();
114     }
115 
setTitle(String title)116     public void setTitle(String title) {
117         getActivity().setTitle(title);
118     }
119 
onProcessArguments(Bundle arguments)120     protected void onProcessArguments(Bundle arguments) {
121         // Key.
122         mPreferenceKey = arguments.getString(AccessibilitySettings.EXTRA_PREFERENCE_KEY);
123 
124         // Enabled.
125         if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
126             final boolean enabled = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED);
127             mSwitchBar.setCheckedInternal(enabled);
128         }
129 
130         // Title.
131         if (arguments.containsKey(AccessibilitySettings.EXTRA_RESOLVE_INFO)) {
132             ResolveInfo info = arguments.getParcelable(AccessibilitySettings.EXTRA_RESOLVE_INFO);
133             getActivity().setTitle(info.loadLabel(getPackageManager()).toString());
134         } else if (arguments.containsKey(AccessibilitySettings.EXTRA_TITLE)) {
135             setTitle(arguments.getString(AccessibilitySettings.EXTRA_TITLE));
136         }
137 
138         // Summary.
139         if (arguments.containsKey(AccessibilitySettings.EXTRA_SUMMARY_RES)) {
140             final int summary = arguments.getInt(AccessibilitySettings.EXTRA_SUMMARY_RES);
141             mFooterPreferenceMixin.createFooterPreference().setTitle(summary);
142         } else if (arguments.containsKey(AccessibilitySettings.EXTRA_SUMMARY)) {
143             final CharSequence summary = arguments.getCharSequence(
144                     AccessibilitySettings.EXTRA_SUMMARY);
145             mFooterPreferenceMixin.createFooterPreference().setTitle(summary);
146         }
147     }
148 }
149