KeyHandler: Add customization options

Change-Id: I5f50f5d442342b2082bf47e11b1351e578fa0144
This commit is contained in:
Timi Rautamäki
2021-10-06 13:17:27 +00:00
committed by LuK1337
parent 94f6a33159
commit a9f61a216e
9 changed files with 261 additions and 12 deletions

View File

@@ -2,11 +2,17 @@ android_app {
name: "KeyHandler",
srcs: ["src/**/*.kt"],
resource_dirs: ["res"],
certificate: "platform",
platform_apis: true,
system_ext_specific: true,
static_libs: [
"org.lineageos.platform.internal",
"org.lineageos.settings.resources",
],
optimize: {
proguard_flags_files: ["proguard.flags"],
},

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 The LineageOS Project
Copyright (C) 2018, 2021 The LineageOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -18,4 +18,37 @@
android:sharedUserId="android.uid.system"
package="org.lineageos.settings.device">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/device_settings_app_name"
android:theme="@style/Theme.Main"
android:defaultToDeviceProtectedStorage="true"
android:directBootAware="true">
<provider
android:name=".ConfigPanelSearchIndexablesProvider"
android:authorities="org.lineageos.settings.device"
android:multiprocess="false"
android:grantUriPermissions="true"
android:permission="android.permission.READ_SEARCH_INDEXABLES"
android:exported="true">
<intent-filter>
<action android:name="android.content.action.SEARCH_INDEXABLES_PROVIDER" />
</intent-filter>
</provider>
<!-- Additional button settings (Button settings) -->
<activity
android:name=".ButtonSettingsActivity"
android:label="@string/button_panel_title"
android:exported="false">
<intent-filter>
<action android:name="org.lineageos.settings.device.ADDITIONAL_BUTTONS_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".KeyHandler" />
</application>
</manifest>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<resources>
<string-array name="alert_slider_action_entries" translatable="false">
<item>@string/alert_slider_mode_normal</item>
<item>@string/alert_slider_mode_vibration</item>
<item>@string/alert_slider_mode_silent</item>
</string-array>
<string-array name="alert_slider_action_entry_values" translatable="false">
<item>2</item>
<item>1</item>
<item>0</item>
</string-array>
</resources>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<resources>
<!-- Alert slider -->
<string name="alert_slider_category_title">Alert slider</string>
<string name="alert_slider_selection_dialog_title">Action</string>
<string name="alert_slider_top_position">Top position</string>
<string name="alert_slider_middle_position">Middle position</string>
<string name="alert_slider_bottom_position">Bottom position</string>
<string name="alert_slider_mode_silent">Silent</string>
<string name="alert_slider_mode_normal">Normal</string>
<string name="alert_slider_mode_vibration">Vibration</string>
</resources>

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/alert_slider_category_title">
<ListPreference
android:key="config_top_position"
android:dialogTitle="@string/alert_slider_selection_dialog_title"
android:title="@string/alert_slider_top_position"
android:summary="%s"
android:entries="@array/alert_slider_action_entries"
android:entryValues="@array/alert_slider_action_entry_values"
android:defaultValue="0" />
<ListPreference
android:key="config_middle_position"
android:dialogTitle="@string/alert_slider_selection_dialog_title"
android:title="@string/alert_slider_middle_position"
android:summary="%s"
android:entries="@array/alert_slider_action_entries"
android:entryValues="@array/alert_slider_action_entry_values"
android:defaultValue="1" />
<ListPreference
android:key="config_bottom_position"
android:dialogTitle="@string/alert_slider_selection_dialog_title"
android:title="@string/alert_slider_bottom_position"
android:summary="%s"
android:entries="@array/alert_slider_action_entries"
android:entryValues="@array/alert_slider_action_entry_values"
android:defaultValue="2" />
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2021 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.settings.device
import android.R
import android.preference.PreferenceActivity
import android.os.Bundle
class ButtonSettingsActivity : PreferenceActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fragmentManager.beginTransaction().replace(
R.id.content,
ButtonSettingsFragment()
).commit()
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.settings.device
import android.os.Bundle
import android.view.MenuItem
import androidx.preference.PreferenceFragment
class ButtonSettingsFragment : PreferenceFragment() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.button_panel)
activity.actionBar!!.setDisplayHomeAsUpEnabled(true)
}
override fun addPreferencesFromResource(preferencesResId: Int) {
super.addPreferencesFromResource(preferencesResId)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.home -> {
activity.finish()
return true
}
}
return super.onOptionsItemSelected(item)
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.settings.device
import android.database.Cursor
import android.database.MatrixCursor
import android.provider.SearchIndexableResource
import android.provider.SearchIndexablesProvider
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_CLASS_NAME
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_ICON_RESID
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_ACTION
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RANK
import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RESID
import android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS
import android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS
import android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS
class ConfigPanelSearchIndexablesProvider : SearchIndexablesProvider() {
override fun onCreate(): Boolean = true
override fun queryXmlResources(projection: Array<String?>?): Cursor {
val cursor = MatrixCursor(INDEXABLES_XML_RES_COLUMNS)
INDEXABLE_RES.forEach {
cursor.addRow(generateResourceRef(it))
}
return cursor
}
override fun queryRawData(projection: Array<String?>?): Cursor {
return MatrixCursor(INDEXABLES_RAW_COLUMNS)
}
override fun queryNonIndexableKeys(projection: Array<String?>?): Cursor {
return MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS)
}
private fun generateResourceRef(sir: SearchIndexableResource): Array<Any?> {
val ref = arrayOfNulls<Any>(7)
ref[COLUMN_INDEX_XML_RES_RANK] = sir.rank
ref[COLUMN_INDEX_XML_RES_RESID] = sir.xmlResId
ref[COLUMN_INDEX_XML_RES_CLASS_NAME] = null
ref[COLUMN_INDEX_XML_RES_ICON_RESID] = sir.iconResId
ref[COLUMN_INDEX_XML_RES_INTENT_ACTION] = "com.android.settings.action.EXTRA_SETTINGS"
ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = "org.lineageos.settings.device"
ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS] = sir.className
return ref
}
companion object {
private const val TAG = "ConfigPanelSearchIndexablesProvider"
private val INDEXABLE_RES = arrayOf<SearchIndexableResource>(
SearchIndexableResource(
1, R.xml.button_panel, ButtonSettingsActivity::class.java.name, 0
)
)
}
}

View File

@@ -15,33 +15,57 @@ import com.android.internal.os.DeviceKeyHandler
class KeyHandler(context: Context) : DeviceKeyHandler {
private val audioManager = context.getSystemService(AudioManager::class.java)
private val vibrator = context.getSystemService(Vibrator::class.java)
private val packageContext = context.createPackageContext(
KeyHandler::class.java.getPackage()!!.name, 0
)
private val sharedPreferences
get() = packageContext.getSharedPreferences(
packageContext.packageName + "_preferences",
Context.MODE_PRIVATE or Context.MODE_MULTI_PROCESS
)
override fun handleKeyEvent(event: KeyEvent): KeyEvent {
if (event.action == KeyEvent.ACTION_DOWN) {
when (event.scanCode) {
MODE_NORMAL -> {
audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL)
vibrator.vibrate(MODE_NORMAL_EFFECT)
POSITION_TOP -> {
val mode = sharedPreferences.getString(ALERT_SLIDER_TOP_KEY, "0")!!.toInt()
audioManager.setRingerModeInternal(mode)
vibrateIfNeeded(mode)
}
MODE_VIBRATION -> {
audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_VIBRATE)
vibrator.vibrate(MODE_VIBRATION_EFFECT)
POSITION_MIDDLE -> {
val mode = sharedPreferences.getString(ALERT_SLIDER_MIDDLE_KEY, "1")!!.toInt()
audioManager.setRingerModeInternal(mode)
vibrateIfNeeded(mode)
}
MODE_SILENCE -> {
audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_SILENT)
POSITION_BOTTOM -> {
val mode = sharedPreferences.getString(ALERT_SLIDER_BOTTOM_KEY, "2")!!.toInt()
audioManager.setRingerModeInternal(mode)
vibrateIfNeeded(mode)
}
}
}
return event
}
private fun vibrateIfNeeded(mode: Int) {
when (mode) {
AudioManager.RINGER_MODE_VIBRATE -> vibrator.vibrate(MODE_VIBRATION_EFFECT)
AudioManager.RINGER_MODE_NORMAL -> vibrator.vibrate(MODE_NORMAL_EFFECT)
}
}
companion object {
private const val TAG = "KeyHandler"
// Slider key codes
private const val MODE_NORMAL = 601
private const val MODE_VIBRATION = 602
private const val MODE_SILENCE = 603
private const val POSITION_TOP = 601
private const val POSITION_MIDDLE = 602
private const val POSITION_BOTTOM = 603
// Preference keys
private const val ALERT_SLIDER_TOP_KEY = "config_top_position"
private const val ALERT_SLIDER_MIDDLE_KEY = "config_middle_position"
private const val ALERT_SLIDER_BOTTOM_KEY = "config_bottom_position"
// Vibration effects
private val MODE_NORMAL_EFFECT = VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK)