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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*
* Copyright (C) 2013 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;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.Handler;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;
import android.view.IWindowManager;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.IWindowManager;
import android.view.WindowManagerGlobal;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import org.cyanogenmod.hardware.KeyDisabler;
public class ButtonSettings extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener {
private static final String TAG = "SystemSettings";
private static final String KEY_HOME_LONG_PRESS = "hardware_keys_home_long_press";
private static final String KEY_HOME_DOUBLE_TAP = "hardware_keys_home_double_tap";
private static final String KEY_MENU_PRESS = "hardware_keys_menu_press";
private static final String KEY_MENU_LONG_PRESS = "hardware_keys_menu_long_press";
private static final String KEY_VOLUME_KEY_CURSOR_CONTROL = "volume_key_cursor_control";
private static final String DISABLE_NAV_KEYS = "disable_nav_keys";
private static final String CATEGORY_POWER = "power_key";
private static final String CATEGORY_HOME = "home_key";
private static final String CATEGORY_MENU = "menu_key";
private static final String CATEGORY_ASSIST = "assist_key";
private static final String CATEGORY_APPSWITCH = "app_switch_key";
private static final String CATEGORY_CAMERA = "camera_key";
private static final String CATEGORY_VOLUME = "volume_keys";
private static final String CATEGORY_BACKLIGHT = "key_backlight";
private static final String CATEGORY_NAVBAR = "navigation_bar";
// 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;
private static final int ACTION_LAUNCH_CAMERA = 6;
private static final int ACTION_LAST_APP = 7;
private static final int ACTION_SLEEP = 8;
// Masks for checking presence of hardware keys.
// Must match values in frameworks/base/core/res/res/values/config.xml
public static final int KEY_MASK_HOME = 0x01;
public static final int KEY_MASK_BACK = 0x02;
public static final int KEY_MASK_MENU = 0x04;
public static final int KEY_MASK_ASSIST = 0x08;
public static final int KEY_MASK_APP_SWITCH = 0x10;
public static final int KEY_MASK_CAMERA = 0x20;
private ListPreference mHomeLongPressAction;
private ListPreference mHomeDoubleTapAction;
private ListPreference mMenuPressAction;
private ListPreference mMenuLongPressAction;
private ListPreference mVolumeKeyCursorControl;
private PreferenceCategory mNavigationPreferencesCat;
private Handler mHandler;
private CheckBoxPreference mDisableNavigationKeys;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.button_settings);
final Resources res = getResources();
final ContentResolver resolver = getActivity().getContentResolver();
final PreferenceScreen prefScreen = getPreferenceScreen();
final int deviceKeys = getResources().getInteger(
com.android.internal.R.integer.config_deviceHardwareKeys);
final boolean hasPowerKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER);
final boolean hasHomeKey = (deviceKeys & KEY_MASK_HOME) != 0;
final boolean hasMenuKey = (deviceKeys & KEY_MASK_MENU) != 0;
final boolean hasAssistKey = (deviceKeys & KEY_MASK_ASSIST) != 0;
boolean hasAnyBindableKey = false;
final PreferenceCategory homeCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_HOME);
final PreferenceCategory menuCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_MENU);
final PreferenceCategory volumeCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_VOLUME);
mHandler = new Handler();
// Force Navigation bar related options
mDisableNavigationKeys = (CheckBoxPreference) findPreference(DISABLE_NAV_KEYS);
// Only visible on devices that does not have a navigation bar already,
// and don't even try unless the existing keys can be disabled
boolean needsNavigationBar = false;
if (KeyDisabler.isSupported()) {
try {
IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
needsNavigationBar = wm.needsNavigationBar();
} catch (RemoteException e) {
}
if (needsNavigationBar) {
prefScreen.removePreference(mDisableNavigationKeys);
} else {
// Remove keys that can be provided by the navbar
updateDisableNavkeysOption();
}
} else {
prefScreen.removePreference(mDisableNavigationKeys);
}
if (hasHomeKey) {
int defaultLongPressAction = res.getInteger(
com.android.internal.R.integer.config_longPressOnHomeBehavior);
if (defaultLongPressAction < ACTION_NOTHING ||
defaultLongPressAction > ACTION_IN_APP_SEARCH) {
defaultLongPressAction = ACTION_NOTHING;
}
int defaultDoubleTapAction = res.getInteger(
com.android.internal.R.integer.config_doubleTapOnHomeBehavior);
if (defaultDoubleTapAction < ACTION_NOTHING ||
defaultDoubleTapAction > ACTION_IN_APP_SEARCH) {
defaultDoubleTapAction = ACTION_NOTHING;
}
int longPressAction = Settings.System.getInt(resolver,
Settings.System.KEY_HOME_LONG_PRESS_ACTION,
defaultLongPressAction);
mHomeLongPressAction = initActionList(KEY_HOME_LONG_PRESS, longPressAction);
int doubleTapAction = Settings.System.getInt(resolver,
Settings.System.KEY_HOME_DOUBLE_TAP_ACTION,
defaultDoubleTapAction);
mHomeDoubleTapAction = initActionList(KEY_HOME_DOUBLE_TAP, doubleTapAction);
hasAnyBindableKey = true;
} else {
prefScreen.removePreference(homeCategory);
}
if (hasMenuKey) {
int pressAction = Settings.System.getInt(resolver,
Settings.System.KEY_MENU_ACTION, ACTION_MENU);
mMenuPressAction = initActionList(KEY_MENU_PRESS, pressAction);
int longPressAction = Settings.System.getInt(resolver,
Settings.System.KEY_MENU_LONG_PRESS_ACTION,
hasAssistKey ? ACTION_NOTHING : ACTION_SEARCH);
mMenuLongPressAction = initActionList(KEY_MENU_LONG_PRESS, longPressAction);
hasAnyBindableKey = true;
} else {
prefScreen.removePreference(menuCategory);
}
if (Utils.hasVolumeRocker(getActivity())) {
int cursorControlAction = Settings.System.getInt(resolver,
Settings.System.VOLUME_KEY_CURSOR_CONTROL, 0);
mVolumeKeyCursorControl = initActionList(KEY_VOLUME_KEY_CURSOR_CONTROL,
cursorControlAction);
} else {
prefScreen.removePreference(volumeCategory);
}
}
private ListPreference initActionList(String key, int value) {
ListPreference list = (ListPreference) getPreferenceScreen().findPreference(key);
list.setValue(Integer.toString(value));
list.setSummary(list.getEntry());
list.setOnPreferenceChangeListener(this);
return list;
}
private void handleActionListChange(ListPreference pref, Object newValue, String setting) {
String value = (String) newValue;
int index = pref.findIndexOfValue(value);
pref.setSummary(pref.getEntries()[index]);
Settings.System.putInt(getContentResolver(), setting, Integer.valueOf(value));
}
@Override
protected int getMetricsCategory() {
return MetricsLogger.DONT_TRACK_ME_BRO;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mHomeLongPressAction) {
handleActionListChange(mHomeLongPressAction, newValue,
Settings.System.KEY_HOME_LONG_PRESS_ACTION);
return true;
} else if (preference == mHomeDoubleTapAction) {
handleActionListChange(mHomeDoubleTapAction, newValue,
Settings.System.KEY_HOME_DOUBLE_TAP_ACTION);
return true;
} else if (preference == mMenuPressAction) {
handleActionListChange(mMenuPressAction, newValue,
Settings.System.KEY_MENU_ACTION);
return true;
} else if (preference == mMenuLongPressAction) {
handleActionListChange(mMenuLongPressAction, newValue,
Settings.System.KEY_MENU_LONG_PRESS_ACTION);
return true;
} else if (preference == mVolumeKeyCursorControl) {
handleActionListChange(mVolumeKeyCursorControl, newValue,
Settings.System.VOLUME_KEY_CURSOR_CONTROL);
return true;
}
return false;
}
private static void writeDisableNavkeysOption(Context context, boolean enabled) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final int defaultBrightness = context.getResources().getInteger(
com.android.internal.R.integer.config_buttonBrightnessSettingDefault);
Settings.System.putInt(context.getContentResolver(),
Settings.System.DEV_FORCE_SHOW_NAVBAR, enabled ? 1 : 0);
KeyDisabler.setActive(enabled);
/* Save/restore button timeouts to disable them in softkey mode */
Editor editor = prefs.edit();
if (enabled) {
int currentBrightness = Settings.System.getInt(context.getContentResolver(),
Settings.System.BUTTON_BRIGHTNESS, defaultBrightness);
if (!prefs.contains("pre_navbar_button_backlight")) {
editor.putInt("pre_navbar_button_backlight", currentBrightness);
}
Settings.System.putInt(context.getContentResolver(),
Settings.System.BUTTON_BRIGHTNESS, 0);
} else {
Settings.System.putInt(context.getContentResolver(),
Settings.System.BUTTON_BRIGHTNESS,
prefs.getInt("pre_navbar_button_backlight", defaultBrightness));
editor.remove("pre_navbar_button_backlight");
}
editor.commit();
}
private void updateDisableNavkeysOption() {
boolean enabled = Settings.System.getInt(getActivity().getContentResolver(),
Settings.System.DEV_FORCE_SHOW_NAVBAR, 0) != 0;
mDisableNavigationKeys.setChecked(enabled);
final PreferenceScreen prefScreen = getPreferenceScreen();
/* Disable hw-key options if they're disabled */
final PreferenceCategory homeCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_HOME);
final PreferenceCategory menuCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_MENU);
final PreferenceCategory assistCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_ASSIST);
final PreferenceCategory appSwitchCategory =
(PreferenceCategory) prefScreen.findPreference(CATEGORY_APPSWITCH);
/* Toggle hardkey control availability depending on navbar state */
if (homeCategory != null) {
homeCategory.setEnabled(!enabled);
}
if (menuCategory != null) {
menuCategory.setEnabled(!enabled);
}
if (assistCategory != null) {
assistCategory.setEnabled(!enabled);
}
if (appSwitchCategory != null) {
appSwitchCategory.setEnabled(!enabled);
}
}
public static void restoreKeyDisabler(Context context) {
if (!KeyDisabler.isSupported()) {
return;
}
writeDisableNavkeysOption(context, Settings.System.getInt(context.getContentResolver(),
Settings.System.DEV_FORCE_SHOW_NAVBAR, 0) != 0);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mDisableNavigationKeys) {
writeDisableNavkeysOption(getActivity(), mDisableNavigationKeys.isChecked());
updateDisableNavkeysOption();
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
|