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