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 17package com.android.settings.accessibility; 18 19import android.content.Intent; 20import android.graphics.Color; 21import android.graphics.drawable.ColorDrawable; 22import android.os.Bundle; 23import android.preference.Preference; 24import android.preference.PreferenceScreen; 25import android.view.Menu; 26import android.view.MenuInflater; 27import android.view.MenuItem; 28import android.view.View; 29import android.view.accessibility.AccessibilityEvent; 30import android.view.accessibility.AccessibilityManager; 31import android.widget.TextView; 32 33import com.android.settings.R; 34import com.android.settings.SettingsActivity; 35import com.android.settings.SettingsPreferenceFragment; 36import com.android.settings.widget.SwitchBar; 37import com.android.settings.widget.ToggleSwitch; 38 39public abstract class ToggleFeaturePreferenceFragment 40 extends SettingsPreferenceFragment { 41 42 protected SwitchBar mSwitchBar; 43 protected ToggleSwitch mToggleSwitch; 44 45 protected String mPreferenceKey; 46 protected Preference mSummaryPreference; 47 48 protected CharSequence mSettingsTitle; 49 protected Intent mSettingsIntent; 50 51 @Override 52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen( 55 getActivity()); 56 setPreferenceScreen(preferenceScreen); 57 mSummaryPreference = new Preference(getActivity()) { 58 @Override 59 protected void onBindView(View view) { 60 super.onBindView(view); 61 final TextView summaryView = (TextView) view.findViewById(android.R.id.summary); 62 summaryView.setText(getSummary()); 63 sendAccessibilityEvent(summaryView); 64 } 65 66 private void sendAccessibilityEvent(View view) { 67 // Since the view is still not attached we create, populate, 68 // and send the event directly since we do not know when it 69 // will be attached and posting commands is not as clean. 70 AccessibilityManager accessibilityManager = 71 AccessibilityManager.getInstance(getActivity()); 72 if (accessibilityManager.isEnabled()) { 73 AccessibilityEvent event = AccessibilityEvent.obtain(); 74 event.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED); 75 view.onInitializeAccessibilityEvent(event); 76 view.dispatchPopulateAccessibilityEvent(event); 77 accessibilityManager.sendAccessibilityEvent(event); 78 } 79 } 80 }; 81 mSummaryPreference.setPersistent(false); 82 mSummaryPreference.setLayoutResource(R.layout.text_description_preference); 83 preferenceScreen.addPreference(mSummaryPreference); 84 } 85 86 @Override 87 public void onViewCreated(View view, Bundle savedInstanceState) { 88 super.onViewCreated(view, savedInstanceState); 89 90 SettingsActivity activity = (SettingsActivity) getActivity(); 91 mSwitchBar = activity.getSwitchBar(); 92 mToggleSwitch = mSwitchBar.getSwitch(); 93 94 onProcessArguments(getArguments()); 95 } 96 97 @Override 98 public void onActivityCreated(Bundle savedInstanceState) { 99 super.onActivityCreated(savedInstanceState); 100 101 installActionBarToggleSwitch(); 102 } 103 104 @Override 105 public void onDestroyView() { 106 super.onDestroyView(); 107 108 removeActionBarToggleSwitch(); 109 } 110 111 protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled); 112 113 @Override 114 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 115 super.onCreateOptionsMenu(menu, inflater); 116 MenuItem menuItem = menu.add(mSettingsTitle); 117 menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 118 menuItem.setIntent(mSettingsIntent); 119 } 120 121 protected void onInstallSwitchBarToggleSwitch() { 122 // Implement this to set a checked listener. 123 } 124 125 protected void onRemoveSwitchBarToggleSwitch() { 126 // Implement this to reset a checked listener. 127 } 128 129 private void installActionBarToggleSwitch() { 130 mSwitchBar.show(); 131 onInstallSwitchBarToggleSwitch(); 132 } 133 134 private void removeActionBarToggleSwitch() { 135 mToggleSwitch.setOnBeforeCheckedChangeListener(null); 136 onRemoveSwitchBarToggleSwitch(); 137 mSwitchBar.hide(); 138 } 139 140 public void setTitle(String title) { 141 getActivity().setTitle(title); 142 } 143 144 protected void onProcessArguments(Bundle arguments) { 145 if (arguments == null) { 146 getPreferenceScreen().removePreference(mSummaryPreference); 147 return; 148 } 149 150 // Key. 151 mPreferenceKey = arguments.getString(AccessibilitySettings.EXTRA_PREFERENCE_KEY); 152 153 // Enabled. 154 if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) { 155 final boolean enabled = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED); 156 mSwitchBar.setCheckedInternal(enabled); 157 } 158 159 // Title. 160 if (arguments.containsKey(AccessibilitySettings.EXTRA_TITLE)) { 161 setTitle(arguments.getString(AccessibilitySettings.EXTRA_TITLE)); 162 } 163 164 // Summary. 165 if (arguments.containsKey(AccessibilitySettings.EXTRA_SUMMARY)) { 166 final CharSequence summary = arguments.getCharSequence( 167 AccessibilitySettings.EXTRA_SUMMARY); 168 mSummaryPreference.setSummary(summary); 169 170 // Set a transparent drawable to prevent use of the default one. 171 getListView().setSelector(new ColorDrawable(Color.TRANSPARENT)); 172 getListView().setDivider(null); 173 } else { 174 getPreferenceScreen().removePreference(mSummaryPreference); 175 } 176 } 177} 178