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.app.ActionBar; 20 import android.app.Activity; 21 import android.content.Intent; 22 import android.graphics.Color; 23 import android.graphics.drawable.ColorDrawable; 24 import android.os.Bundle; 25 import android.preference.Preference; 26 import android.preference.PreferenceActivity; 27 import android.preference.PreferenceScreen; 28 import android.view.Gravity; 29 import android.view.Menu; 30 import android.view.MenuInflater; 31 import android.view.MenuItem; 32 import android.view.View; 33 import android.view.accessibility.AccessibilityEvent; 34 import android.view.accessibility.AccessibilityManager; 35 import android.widget.TextView; 36 37 import com.android.settings.R; 38 import com.android.settings.SettingsPreferenceFragment; 39 40 public abstract class ToggleFeaturePreferenceFragment 41 extends SettingsPreferenceFragment { 42 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 TextView summaryView = (TextView) view.findViewById(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 onInstallActionBarToggleSwitch(); 90 onProcessArguments(getArguments()); 91 // Set a transparent drawable to prevent use of the default one. 92 getListView().setSelector(new ColorDrawable(Color.TRANSPARENT)); 93 getListView().setDivider(null); 94 } 95 96 @Override 97 public void onDestroyView() { 98 getActivity().getActionBar().setCustomView(null); 99 mToggleSwitch.setOnBeforeCheckedChangeListener(null); 100 super.onDestroyView(); 101 } 102 103 protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled); 104 105 @Override 106 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 107 super.onCreateOptionsMenu(menu, inflater); 108 MenuItem menuItem = menu.add(mSettingsTitle); 109 menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 110 menuItem.setIntent(mSettingsIntent); 111 } 112 113 protected void onInstallActionBarToggleSwitch() { 114 mToggleSwitch = createAndAddActionBarToggleSwitch(getActivity()); 115 } 116 117 private ToggleSwitch createAndAddActionBarToggleSwitch(Activity activity) { 118 ToggleSwitch toggleSwitch = new ToggleSwitch(activity); 119 final int padding = activity.getResources().getDimensionPixelSize( 120 R.dimen.action_bar_switch_padding); 121 toggleSwitch.setPaddingRelative(0, 0, padding, 0); 122 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, 123 ActionBar.DISPLAY_SHOW_CUSTOM); 124 activity.getActionBar().setCustomView(toggleSwitch, 125 new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 126 ActionBar.LayoutParams.WRAP_CONTENT, 127 Gravity.CENTER_VERTICAL | Gravity.END)); 128 return toggleSwitch; 129 } 130 131 protected void onProcessArguments(Bundle arguments) { 132 // Key. 133 mPreferenceKey = arguments.getString(AccessibilitySettings.EXTRA_PREFERENCE_KEY); 134 // Enabled. 135 final boolean enabled = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED); 136 mToggleSwitch.setCheckedInternal(enabled); 137 // Title. 138 PreferenceActivity activity = (PreferenceActivity) getActivity(); 139 if (!activity.onIsMultiPane() || activity.onIsHidingHeaders()) { 140 String title = arguments.getString(AccessibilitySettings.EXTRA_TITLE); 141 getActivity().setTitle(title); 142 } 143 // Summary. 144 CharSequence summary = arguments.getCharSequence(AccessibilitySettings.EXTRA_SUMMARY); 145 mSummaryPreference.setSummary(summary); 146 } 147 } 148