1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.cyanogenmod;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
public class HardwareKeys extends SettingsPreferenceFragment implements OnPreferenceChangeListener {
private static final String HARDWARE_KEYS_CATEGORY_BINDINGS = "hardware_keys_bindings";
private static final String HARDWARE_KEYS_ENABLE_CUSTOM = "hardware_keys_enable_custom";
private static final String HARDWARE_KEYS_HOME_LONG_PRESS = "hardware_keys_home_long_press";
private static final String HARDWARE_KEYS_MENU_PRESS = "hardware_keys_menu_press";
private static final String HARDWARE_KEYS_MENU_LONG_PRESS = "hardware_keys_menu_long_press";
private static final String HARDWARE_KEYS_ASSIST_PRESS = "hardware_keys_assist_press";
private static final String HARDWARE_KEYS_ASSIST_LONG_PRESS = "hardware_keys_assist_long_press";
private static final String HARDWARE_KEYS_APP_SWITCH_PRESS = "hardware_keys_app_switch_press";
private static final String HARDWARE_KEYS_APP_SWITCH_LONG_PRESS = "hardware_keys_app_switch_long_press";
private static final String HARDWARE_KEYS_SHOW_OVERFLOW = "hardware_keys_show_overflow";
// Available custom actions to perform on a key press.
// Must match values for KEY_HOME_LONG_PRESS_ACTION in:
// frameworks/base/core/java/android/provider/Settings.java
private static final int ACTION_NOTHING = 0;
private static final int ACTION_MENU = 1;
private static final int ACTION_APP_SWITCH = 2;
private static final int ACTION_SEARCH = 3;
private static final int ACTION_VOICE_SEARCH = 4;
private static final int ACTION_IN_APP_SEARCH = 5;
// Masks for checking presence of hardware keys.
// Must match values in frameworks/base/core/res/res/values/config.xml
private static final int KEY_MASK_HOME = 0x01;
private static final int KEY_MASK_BACK = 0x02;
private static final int KEY_MASK_MENU = 0x04;
private static final int KEY_MASK_ASSIST = 0x08;
private static final int KEY_MASK_APP_SWITCH = 0x10;
private CheckBoxPreference mEnableCustomBindings;
private ListPreference mHomeLongPressAction;
private ListPreference mMenuPressAction;
private ListPreference mMenuLongPressAction;
private ListPreference mAssistPressAction;
private ListPreference mAssistLongPressAction;
private ListPreference mAppSwitchPressAction;
private ListPreference mAppSwitchLongPressAction;
private CheckBoxPreference mShowActionOverflow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int deviceKeys = getResources().getInteger(
com.android.internal.R.integer.config_deviceHardwareKeys);
final boolean hasHomeKey = (deviceKeys & KEY_MASK_HOME) != 0;
final boolean hasMenuKey = (deviceKeys & KEY_MASK_MENU) != 0;
final boolean hasAssistKey = (deviceKeys & KEY_MASK_ASSIST) != 0;
final boolean hasAppSwitchKey = (deviceKeys & KEY_MASK_APP_SWITCH) != 0;
addPreferencesFromResource(R.xml.hardware_keys);
PreferenceScreen prefSet = getPreferenceScreen();
mEnableCustomBindings = (CheckBoxPreference) prefSet.findPreference(
HARDWARE_KEYS_ENABLE_CUSTOM);
mHomeLongPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_HOME_LONG_PRESS);
mMenuPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_MENU_PRESS);
mMenuLongPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_MENU_LONG_PRESS);
mAssistPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_ASSIST_PRESS);
mAssistLongPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_ASSIST_LONG_PRESS);
mAppSwitchPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_APP_SWITCH_PRESS);
mAppSwitchLongPressAction = (ListPreference) prefSet.findPreference(
HARDWARE_KEYS_APP_SWITCH_LONG_PRESS);
mShowActionOverflow = (CheckBoxPreference) prefSet.findPreference(
HARDWARE_KEYS_SHOW_OVERFLOW);
PreferenceCategory bindingsCategory = (PreferenceCategory) prefSet.findPreference(
HARDWARE_KEYS_CATEGORY_BINDINGS);
if (hasHomeKey) {
int homeLongPressAction;
if (hasAppSwitchKey) {
homeLongPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_HOME_LONG_PRESS_ACTION, ACTION_NOTHING);
} else {
homeLongPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_HOME_LONG_PRESS_ACTION, ACTION_APP_SWITCH);
}
mHomeLongPressAction.setValue(Integer.toString(homeLongPressAction));
mHomeLongPressAction.setSummary(mHomeLongPressAction.getEntry());
mHomeLongPressAction.setOnPreferenceChangeListener(this);
} else {
bindingsCategory.removePreference(mHomeLongPressAction);
}
if (hasMenuKey) {
int menuPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_MENU_ACTION, ACTION_MENU);
mMenuPressAction.setValue(Integer.toString(menuPressAction));
mMenuPressAction.setSummary(mMenuPressAction.getEntry());
mMenuPressAction.setOnPreferenceChangeListener(this);
int menuLongPressAction;
if (hasAssistKey) {
menuLongPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_MENU_LONG_PRESS_ACTION, ACTION_NOTHING);
} else {
menuLongPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_MENU_LONG_PRESS_ACTION, ACTION_SEARCH);
}
mMenuLongPressAction.setValue(Integer.toString(menuLongPressAction));
mMenuLongPressAction.setSummary(mMenuLongPressAction.getEntry());
mMenuLongPressAction.setOnPreferenceChangeListener(this);
} else {
bindingsCategory.removePreference(mMenuPressAction);
bindingsCategory.removePreference(mMenuLongPressAction);
}
if (hasAssistKey) {
int assistPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_ASSIST_ACTION, ACTION_SEARCH);
mAssistPressAction.setValue(Integer.toString(assistPressAction));
mAssistPressAction.setSummary(mAssistPressAction.getEntry());
mAssistPressAction.setOnPreferenceChangeListener(this);
int assistLongPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_ASSIST_LONG_PRESS_ACTION, ACTION_VOICE_SEARCH);
mAssistLongPressAction.setValue(Integer.toString(assistLongPressAction));
mAssistLongPressAction.setSummary(mAssistLongPressAction.getEntry());
mAssistLongPressAction.setOnPreferenceChangeListener(this);
} else {
bindingsCategory.removePreference(mAssistPressAction);
bindingsCategory.removePreference(mAssistLongPressAction);
}
if (hasAppSwitchKey) {
int appSwitchPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_APP_SWITCH_ACTION, ACTION_APP_SWITCH);
mAppSwitchPressAction.setValue(Integer.toString(appSwitchPressAction));
mAppSwitchPressAction.setSummary(mAppSwitchPressAction.getEntry());
mAppSwitchPressAction.setOnPreferenceChangeListener(this);
int appSwitchLongPressAction = Settings.System.getInt(getContentResolver(),
Settings.System.KEY_APP_SWITCH_LONG_PRESS_ACTION, ACTION_NOTHING);
mAppSwitchLongPressAction.setValue(Integer.toString(appSwitchLongPressAction));
mAppSwitchLongPressAction.setSummary(mAppSwitchLongPressAction.getEntry());
mAppSwitchLongPressAction.setOnPreferenceChangeListener(this);
} else {
bindingsCategory.removePreference(mAppSwitchPressAction);
bindingsCategory.removePreference(mAppSwitchLongPressAction);
}
mEnableCustomBindings.setChecked((Settings.System.getInt(getActivity().
getApplicationContext().getContentResolver(),
Settings.System.HARDWARE_KEY_REBINDING, 0) == 1));
mShowActionOverflow.setChecked((Settings.System.getInt(getActivity().
getApplicationContext().getContentResolver(),
Settings.System.UI_FORCE_OVERFLOW_BUTTON, 0) == 1));
}
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mHomeLongPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mHomeLongPressAction.findIndexOfValue((String) newValue);
mHomeLongPressAction.setSummary(
mHomeLongPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_HOME_LONG_PRESS_ACTION, value);
return true;
} else if (preference == mMenuPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mMenuPressAction.findIndexOfValue((String) newValue);
mMenuPressAction.setSummary(
mMenuPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_MENU_ACTION, value);
return true;
} else if (preference == mMenuLongPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mMenuLongPressAction.findIndexOfValue((String) newValue);
mMenuLongPressAction.setSummary(
mMenuLongPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_MENU_LONG_PRESS_ACTION, value);
return true;
} else if (preference == mAssistPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mAssistPressAction.findIndexOfValue((String) newValue);
mAssistPressAction.setSummary(
mAssistPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_ASSIST_ACTION, value);
return true;
} else if (preference == mAssistLongPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mAssistLongPressAction.findIndexOfValue((String) newValue);
mAssistLongPressAction.setSummary(
mAssistLongPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_ASSIST_LONG_PRESS_ACTION, value);
return true;
} else if (preference == mAppSwitchPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mAppSwitchPressAction.findIndexOfValue((String) newValue);
mAppSwitchPressAction.setSummary(
mAppSwitchPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_APP_SWITCH_ACTION, value);
return true;
} else if (preference == mAppSwitchLongPressAction) {
int value = Integer.valueOf((String) newValue);
int index = mAppSwitchLongPressAction.findIndexOfValue((String) newValue);
mAppSwitchLongPressAction.setSummary(
mAppSwitchLongPressAction.getEntries()[index]);
Settings.System.putInt(getContentResolver(),
Settings.System.KEY_APP_SWITCH_LONG_PRESS_ACTION, value);
return true;
}
return false;
}
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mEnableCustomBindings) {
Settings.System.putInt(getContentResolver(), Settings.System.HARDWARE_KEY_REBINDING,
mEnableCustomBindings.isChecked() ? 1 : 0);
return true;
} else if (preference == mShowActionOverflow) {
boolean enabled = mShowActionOverflow.isChecked();
Settings.System.putInt(getContentResolver(), Settings.System.UI_FORCE_OVERFLOW_BUTTON,
enabled ? 1 : 0);
// Show appropriate
if (enabled) {
Toast.makeText(getActivity(), R.string.hardware_keys_show_overflow_toast_enable,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), R.string.hardware_keys_show_overflow_toast_disable,
Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
}
|