diff options
| author | SpiritCroc <dev@spiritcroc.de> | 2019-02-02 20:21:00 +0100 |
|---|---|---|
| committer | Ali B <abittin@gmail.com> | 2019-03-18 13:29:58 +0300 |
| commit | 9f041dc2b2dd9881a693104f480a5f0f8d2ed531 (patch) | |
| tree | e98089d3c16e42a74006e0f2427fb0019a8565d6 | |
| parent | 20f9a39059d612a9bfe9c61308326988beb96232 (diff) | |
Notification slider: kernel independent actions
Don't change nodes/keycodes, but actions.
This allows us to add additional actions without patching the kernel
(which consequently yields less compatibility issues with custom kernels).
Works with any kernel that sends 601/602/603 keycodes for
top/middle/bottom actions by default.
[Pie/GZOSP-base version]
Change-Id: I3cc63e363ccec19b96a76ee4369d3692cbf98402
| -rw-r--r-- | DeviceHandler/proguard.flags | 11 | ||||
| -rw-r--r-- | DeviceHandler/res/values/arrays.xml | 2 | ||||
| -rw-r--r-- | DeviceHandler/res/values/strings.xml | 1 | ||||
| -rw-r--r-- | DeviceHandler/res/xml/slider_panel.xml | 21 | ||||
| -rw-r--r-- | DeviceHandler/src/com/aicp/device/preference/ListPreference.java | 55 | ||||
| -rw-r--r-- | DeviceHandler/src/com/aicp/device/preference/SystemSettingListPreference.java | 49 | ||||
| -rw-r--r-- | DeviceHandler/src/com/aicp/device/preference/SystemSettingsStore.java | 72 | ||||
| -rw-r--r-- | DeviceHandler/src/com/slim/device/BootReceiver.java | 14 | ||||
| -rw-r--r-- | DeviceHandler/src/com/slim/device/KeyHandler.java | 111 | ||||
| -rw-r--r-- | DeviceHandler/src/com/slim/device/settings/SliderSettings.java | 20 |
10 files changed, 323 insertions, 33 deletions
diff --git a/DeviceHandler/proguard.flags b/DeviceHandler/proguard.flags index 9e6791b..58ef4d7 100644 --- a/DeviceHandler/proguard.flags +++ b/DeviceHandler/proguard.flags @@ -3,3 +3,14 @@ -keepclassmembers public class com.slim.device.KeyHandler { *; } + +# Keep classes that may be inflated from XML. +-keepclasseswithmembers class * { + public <init>(android.content.Context, android.util.AttributeSet); +} +-keepclasseswithmembers class * { + public <init>(android.content.Context, android.util.AttributeSet, int); +} +-keepclasseswithmembers class * { + public <init>(android.content.Context, android.util.AttributeSet, int, int); +} diff --git a/DeviceHandler/res/values/arrays.xml b/DeviceHandler/res/values/arrays.xml index 8b234ab..82bde98 100644 --- a/DeviceHandler/res/values/arrays.xml +++ b/DeviceHandler/res/values/arrays.xml @@ -18,6 +18,7 @@ <item>@string/alarms_only</item> <item>@string/priority_only</item> <item>@string/none</item> + <item>@string/silent</item> <item>@string/vibrate</item> <item>@string/ring</item> </string-array> @@ -27,6 +28,7 @@ <item>601</item> <item>602</item> <item>603</item> + <item>620</item> <item>604</item> <item>605</item> </string-array> diff --git a/DeviceHandler/res/values/strings.xml b/DeviceHandler/res/values/strings.xml index 1565cd8..ede626c 100644 --- a/DeviceHandler/res/values/strings.xml +++ b/DeviceHandler/res/values/strings.xml @@ -50,6 +50,7 @@ <string name="alarms_only">Alarms only</string> <string name="priority_only">Priority only</string> <string name="none">All notifications</string> + <string name="silent">Mute</string> <string name="vibrate">Vibrate</string> <string name="ring">Ring</string> diff --git a/DeviceHandler/res/xml/slider_panel.xml b/DeviceHandler/res/xml/slider_panel.xml index 80302ba..9757b0a 100644 --- a/DeviceHandler/res/xml/slider_panel.xml +++ b/DeviceHandler/res/xml/slider_panel.xml @@ -19,26 +19,29 @@ <PreferenceCategory android:title="@string/notification_slider"> - <ListPreference - android:key="keycode_top_position" + <com.aicp.device.preference.SystemSettingListPreference + android:key="device_oppo_common_notification_slider_up1" android:dialogTitle="@string/action" android:title="@string/top_position" android:entries="@array/notification_slider_action_entries" - android:entryValues="@array/notification_slider_action_entry_values" /> + android:entryValues="@array/notification_slider_action_entry_values" + android:defaultValue="601" /> - <ListPreference - android:key="keycode_middle_position" + <com.aicp.device.preference.SystemSettingListPreference + android:key="device_oppo_common_notification_slider_middle1" android:dialogTitle="@string/action" android:title="@string/middle_position" android:entries="@array/notification_slider_action_entries" - android:entryValues="@array/notification_slider_action_entry_values" /> + android:entryValues="@array/notification_slider_action_entry_values" + android:defaultValue="602" /> - <ListPreference - android:key="keycode_bottom_position" + <com.aicp.device.preference.SystemSettingListPreference + android:key="device_oppo_common_notification_slider_bottom1" android:dialogTitle="@string/action" android:title="@string/bottom_position" android:entries="@array/notification_slider_action_entries" - android:entryValues="@array/notification_slider_action_entry_values" /> + android:entryValues="@array/notification_slider_action_entry_values" + android:defaultValue="603" /> </PreferenceCategory> diff --git a/DeviceHandler/src/com/aicp/device/preference/ListPreference.java b/DeviceHandler/src/com/aicp/device/preference/ListPreference.java new file mode 100644 index 0000000..3c113ab --- /dev/null +++ b/DeviceHandler/src/com/aicp/device/preference/ListPreference.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2017-2018 Android Ice Cold 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.aicp.device.preference; + +import android.content.Context; +import android.text.TextUtils; +import android.util.AttributeSet; + +public class ListPreference extends android.preference.ListPreference { + private boolean mAutoSummary = false; + + public ListPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public ListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public ListPreference(Context context) { + super(context); + } + + @Override + public void setValue(String value) { + super.setValue(value); + if (mAutoSummary || TextUtils.isEmpty(getSummary())) { + setSummary(getEntry(), true); + } + } + + @Override + public void setSummary(CharSequence summary) { + setSummary(summary, false); + } + + private void setSummary(CharSequence summary, boolean autoSummary) { + mAutoSummary = autoSummary; + super.setSummary(summary); + } +} diff --git a/DeviceHandler/src/com/aicp/device/preference/SystemSettingListPreference.java b/DeviceHandler/src/com/aicp/device/preference/SystemSettingListPreference.java new file mode 100644 index 0000000..fd8a67a --- /dev/null +++ b/DeviceHandler/src/com/aicp/device/preference/SystemSettingListPreference.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2017-2018 Android Ice Cold 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.aicp.device.preference; + +import android.content.Context; +import android.text.TextUtils; +import android.util.AttributeSet; + +public class SystemSettingListPreference extends ListPreference { + + public SystemSettingListPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver())); + } + + public SystemSettingListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver())); + } + + public SystemSettingListPreference(Context context) { + super(context); + setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver())); + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + // This is what default ListPreference implementation is doing without respecting + // real default value: + //setValue(restoreValue ? getPersistedString(mValue) : (String) defaultValue); + // Instead, we better do + setValue(restoreValue ? getPersistedString((String) defaultValue) : (String) defaultValue); + } + +} diff --git a/DeviceHandler/src/com/aicp/device/preference/SystemSettingsStore.java b/DeviceHandler/src/com/aicp/device/preference/SystemSettingsStore.java new file mode 100644 index 0000000..77aa8f5 --- /dev/null +++ b/DeviceHandler/src/com/aicp/device/preference/SystemSettingsStore.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 Android Ice Cold 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.aicp.device.preference; + +import android.content.ContentResolver; +import android.preference.PreferenceDataStore; +import android.provider.Settings; + +public class SystemSettingsStore implements PreferenceDataStore { + + private ContentResolver mContentResolver; + + public SystemSettingsStore(ContentResolver contentResolver) { + mContentResolver = contentResolver; + } + + public boolean getBoolean(String key, boolean defValue) { + return getInt(key, defValue ? 1 : 0) != 0; + } + + public float getFloat(String key, float defValue) { + return Settings.System.getFloat(mContentResolver, key, defValue); + } + + public int getInt(String key, int defValue) { + return Settings.System.getInt(mContentResolver, key, defValue); + } + + public long getLong(String key, long defValue) { + return Settings.System.getLong(mContentResolver, key, defValue); + } + + public String getString(String key, String defValue) { + String result = Settings.System.getString(mContentResolver, key); + return result == null ? defValue : result; + } + + public void putBoolean(String key, boolean value) { + putInt(key, value ? 1 : 0); + } + + public void putFloat(String key, float value) { + Settings.System.putFloat(mContentResolver, key, value); + } + + public void putInt(String key, int value) { + Settings.System.putInt(mContentResolver, key, value); + } + + public void putLong(String key, long value) { + Settings.System.putLong(mContentResolver, key, value); + } + + public void putString(String key, String value) { + Settings.System.putString(mContentResolver, key, value); + } + +} diff --git a/DeviceHandler/src/com/slim/device/BootReceiver.java b/DeviceHandler/src/com/slim/device/BootReceiver.java index 8035938..0821251 100644 --- a/DeviceHandler/src/com/slim/device/BootReceiver.java +++ b/DeviceHandler/src/com/slim/device/BootReceiver.java @@ -29,6 +29,10 @@ import com.slim.device.settings.ScreenOffGesture; import com.slim.device.settings.SliderSettings; import com.slim.device.util.FileUtils; +import static com.slim.device.KeyHandler.SLIDER_TOP; +import static com.slim.device.KeyHandler.SLIDER_MIDDLE; +import static com.slim.device.KeyHandler.SLIDER_BOTTOM; + import java.io.File; @@ -50,13 +54,17 @@ public class BootReceiver extends BroadcastReceiver { } else { enableComponent(context, SliderSettings.class.getName()); - String sliderTop = getPreferenceString(context, "keycode_top_position", "601"); - String sliderMiddle = getPreferenceString(context, "keycode_middle_position", "602"); - String sliderBottom = getPreferenceString(context, "keycode_bottom_position", "603"); + // Set keycodes as expected by our keyhandler for kernels + // that control keycodes using these files + /* EDIT: don't, can lead to wrong value sent after boot + String sliderTop = "" + SLIDER_TOP; + String sliderMiddle = "" + SLIDER_MIDDLE; + String sliderBottom = "" + SLIDER_BOTTOM; FileUtils.writeLine(KernelControl.KEYCODE_SLIDER_TOP, sliderTop); FileUtils.writeLine(KernelControl.KEYCODE_SLIDER_MIDDLE, sliderMiddle); FileUtils.writeLine(KernelControl.KEYCODE_SLIDER_BOTTOM, sliderBottom); + */ } } diff --git a/DeviceHandler/src/com/slim/device/KeyHandler.java b/DeviceHandler/src/com/slim/device/KeyHandler.java index deb36ee..e1227c8 100644 --- a/DeviceHandler/src/com/slim/device/KeyHandler.java +++ b/DeviceHandler/src/com/slim/device/KeyHandler.java @@ -1,5 +1,6 @@ /* * Copyright (C) 2014 Slimroms + * Copyright (C) 2019 Android Ice Cold Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +18,11 @@ package com.slim.device; import android.app.Activity; import android.app.NotificationManager; +import android.content.ContentResolver; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.content.SharedPreferences; +import android.database.ContentObserver; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; @@ -30,6 +33,7 @@ import android.os.Message; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.os.SystemProperties; +import android.os.UserHandle; import android.os.Vibrator; import android.provider.Settings; import android.util.Log; @@ -44,11 +48,20 @@ import com.android.internal.util.ArrayUtils; import com.android.internal.util.gzosp.ActionConstants; import com.android.internal.util.gzosp.Action; +import java.util.HashSet; + public class KeyHandler implements DeviceKeyHandler { private static final String TAG = KeyHandler.class.getSimpleName(); private static final int GESTURE_REQUEST = 1; + public static final String SETTING_NOTIF_SLIDER_UP = + "device_oppo_common_notification_slider_up1"; + public static final String SETTING_NOTIF_SLIDER_MIDDLE = + "device_oppo_common_notification_slider_middle1"; + public static final String SETTING_NOTIF_SLIDER_BOTTOM = + "device_oppo_common_notification_slider_bottom1"; + // Supported scancodes private static final int GESTURE_CIRCLE_SCANCODE = 250; private static final int GESTURE_SWIPE_DOWN_SCANCODE = 251; @@ -63,6 +76,13 @@ public class KeyHandler implements DeviceKeyHandler { private static final int MODE_NONE = 603; private static final int MODE_VIBRATE = 604; private static final int MODE_RING = 605; + // AICP additions: arbitrary value which hopefully doesn't conflict with upstream anytime soon + private static final int MODE_SILENT = 620; + + // Keycodes from kernel found in drivers/input/misc/tri_state_key.c + public static final int SLIDER_TOP = 601; + public static final int SLIDER_MIDDLE = 602; + public static final int SLIDER_BOTTOM = 603; private static final int[] sSupportedGestures = new int[]{ GESTURE_CIRCLE_SCANCODE, @@ -76,9 +96,17 @@ public class KeyHandler implements DeviceKeyHandler { MODE_PRIORITY_ONLY, MODE_NONE, MODE_VIBRATE, - MODE_RING + MODE_RING, + MODE_SILENT, }; + private static final HashSet<Integer> sSupportedSliderModes = new HashSet<>(); + static { + sSupportedSliderModes.add(SLIDER_TOP); + sSupportedSliderModes.add(SLIDER_MIDDLE); + sSupportedSliderModes.add(SLIDER_BOTTOM); + } + private final Context mContext; private final AudioManager mAudioManager; private final PowerManager mPowerManager; @@ -90,6 +118,10 @@ public class KeyHandler implements DeviceKeyHandler { private Vibrator mVibrator; WakeLock mProximityWakeLock; + private int mSliderUpAction; + private int mSliderMiddleAction; + private int mSliderBottomAction; + public KeyHandler(Context context) { mContext = context; mEventHandler = new EventHandler(); @@ -112,14 +144,24 @@ public class KeyHandler implements DeviceKeyHandler { "com.slim.device", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { } + + new SettingsObserver(new Handler()).observe(); } private class EventHandler extends Handler { @Override public void handleMessage(Message msg) { - KeyEvent event = (KeyEvent) msg.obj; + int scanCode; + if (msg.obj instanceof KeyEvent) { + KeyEvent event = (KeyEvent) msg.obj; + scanCode = event.getScanCode(); + } else if (msg.obj instanceof Integer) { + scanCode = (Integer) msg.obj; + } else { + return; + } String action = null; - switch(event.getScanCode()) { + switch(scanCode) { case GESTURE_CIRCLE_SCANCODE: action = getGestureSharedPreferences() .getString(ScreenOffGesture.PREF_GESTURE_CIRCLE, @@ -176,6 +218,9 @@ public class KeyHandler implements DeviceKeyHandler { case MODE_RING: setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL); break; + case MODE_SILENT: + setRingerModeInternal(AudioManager.RINGER_MODE_SILENT); + break; } if (action == null || action != null && action.equals(ActionConstants.ACTION_NULL)) { @@ -218,6 +263,17 @@ public class KeyHandler implements DeviceKeyHandler { public KeyEvent handleKeyEvent(KeyEvent event) { int scanCode = event.getScanCode(); + boolean isSliderModeSupported = sSupportedSliderModes.contains(scanCode); + if (isSliderModeSupported) { + // Remap slider actions + if (scanCode == SLIDER_TOP) { + scanCode = mSliderUpAction; + } else if (scanCode == SLIDER_MIDDLE) { + scanCode = mSliderMiddleAction; + } else if (scanCode == SLIDER_BOTTOM) { + scanCode = mSliderBottomAction; + } + } boolean isKeySupported = ArrayUtils.contains(sSupportedGestures, scanCode); if (!isKeySupported) { return event; @@ -226,7 +282,13 @@ public class KeyHandler implements DeviceKeyHandler { return null; } if (isKeySupported && !mEventHandler.hasMessages(GESTURE_REQUEST)) { - Message msg = getMessageForKeyEvent(event); + Message msg; + if (scanCode != event.getScanCode()) { + // Overwritten action + msg = getMessageForScanCode(scanCode); + } else { + msg = getMessageForKeyEvent(event); + } if (scanCode < MODE_TOTAL_SILENCE && mProximitySensor != null) { mEventHandler.sendMessageDelayed(msg, 200); processEvent(event); @@ -243,6 +305,12 @@ public class KeyHandler implements DeviceKeyHandler { return msg; } + private Message getMessageForScanCode(int scanCode) { + Message msg = mEventHandler.obtainMessage(GESTURE_REQUEST); + msg.obj = new Integer(scanCode); + return msg; + } + private void processEvent(final KeyEvent keyEvent) { mProximityWakeLock.acquire(); mSensorManager.registerListener(new SensorEventListener() { @@ -268,4 +336,39 @@ public class KeyHandler implements DeviceKeyHandler { }, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST); } + private class SettingsObserver extends ContentObserver { + SettingsObserver(Handler handler) { + super(handler); + } + + void observe() { + ContentResolver resolver = mContext.getContentResolver(); + + resolver.registerContentObserver(Settings.System.getUriFor(SETTING_NOTIF_SLIDER_UP), + false, this, UserHandle.USER_ALL); + resolver.registerContentObserver( + Settings.System.getUriFor(SETTING_NOTIF_SLIDER_MIDDLE), false, this, + UserHandle.USER_ALL); + resolver.registerContentObserver( + Settings.System.getUriFor(SETTING_NOTIF_SLIDER_BOTTOM), false, this, + UserHandle.USER_ALL); + + update(); + } + + @Override + public void onChange(boolean selfChange) { + update(); + } + + void update() { + mSliderUpAction = Settings.System.getInt(mContext.getContentResolver(), + SETTING_NOTIF_SLIDER_UP, 601); + mSliderMiddleAction = Settings.System.getInt(mContext.getContentResolver(), + SETTING_NOTIF_SLIDER_MIDDLE, 602); + mSliderBottomAction = Settings.System.getInt(mContext.getContentResolver(), + SETTING_NOTIF_SLIDER_BOTTOM, 603); + } + } + } diff --git a/DeviceHandler/src/com/slim/device/settings/SliderSettings.java b/DeviceHandler/src/com/slim/device/settings/SliderSettings.java index dfe3de0..be6d1b8 100644 --- a/DeviceHandler/src/com/slim/device/settings/SliderSettings.java +++ b/DeviceHandler/src/com/slim/device/settings/SliderSettings.java @@ -30,23 +30,10 @@ import com.slim.device.util.FileUtils; public class SliderSettings extends PreferenceActivity implements OnPreferenceChangeListener { - private ListPreference mSliderTop; - private ListPreference mSliderMiddle; - private ListPreference mSliderBottom; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.slider_panel); - - mSliderTop = (ListPreference) findPreference("keycode_top_position"); - mSliderTop.setOnPreferenceChangeListener(this); - - mSliderMiddle = (ListPreference) findPreference("keycode_middle_position"); - mSliderMiddle.setOnPreferenceChangeListener(this); - - mSliderBottom = (ListPreference) findPreference("keycode_bottom_position"); - mSliderBottom.setOnPreferenceChangeListener(this); } private void setSummary(ListPreference preference, String file) { @@ -59,6 +46,8 @@ public class SliderSettings extends PreferenceActivity @Override public boolean onPreferenceChange(Preference preference, Object newValue) { + return false; + /* final String file; if (preference == mSliderTop) { file = KernelControl.KEYCODE_SLIDER_TOP; @@ -74,6 +63,7 @@ public class SliderSettings extends PreferenceActivity setSummary((ListPreference) preference, file); return true; + */ } @Override @@ -82,9 +72,5 @@ public class SliderSettings extends PreferenceActivity // Remove padding around the listview getListView().setPadding(0, 0, 0, 0); - - setSummary(mSliderTop, KernelControl.KEYCODE_SLIDER_TOP); - setSummary(mSliderMiddle, KernelControl.KEYCODE_SLIDER_MIDDLE); - setSummary(mSliderBottom, KernelControl.KEYCODE_SLIDER_BOTTOM); } } |
