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