KeyHandler: Add more modes
* Add DND modes * alarms only * important interruptions * total silence * Add option to mute media when going to silent mode Change-Id: Ia59e06f0a0dd3047b3771bb3ac8d5e338d0a75c3
This commit is contained in:
@@ -8,11 +8,17 @@
|
||||
<item>@string/alert_slider_mode_normal</item>
|
||||
<item>@string/alert_slider_mode_vibration</item>
|
||||
<item>@string/alert_slider_mode_silent</item>
|
||||
<item>@string/alert_slider_mode_dnd_priority_only</item>
|
||||
<item>@string/alert_slider_mode_dnd_total_silence</item>
|
||||
<item>@string/alert_slider_mode_dnd_alarms_only</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="alert_slider_action_entry_values" translatable="false">
|
||||
<item>2</item>
|
||||
<item>1</item>
|
||||
<item>0</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
<item>5</item>
|
||||
</string-array>
|
||||
</resources>
|
||||
|
||||
@@ -13,4 +13,9 @@
|
||||
<string name="alert_slider_mode_silent">Silent</string>
|
||||
<string name="alert_slider_mode_normal">Normal</string>
|
||||
<string name="alert_slider_mode_vibration">Vibration</string>
|
||||
<string name="alert_slider_mode_dnd_priority_only">Priority only</string>
|
||||
<string name="alert_slider_mode_dnd_total_silence">Total silence</string>
|
||||
<string name="alert_slider_mode_dnd_alarms_only">Alarms only</string>
|
||||
<string name="alert_slider_mute_media_title">Mute media</string>
|
||||
<string name="alert_slider_mute_media_summary">Mute media when switching to silent</string>
|
||||
</resources>
|
||||
|
||||
@@ -7,6 +7,12 @@
|
||||
<PreferenceCategory
|
||||
android:title="@string/alert_slider_category_title">
|
||||
|
||||
<SwitchPreference
|
||||
android:key="config_mute_media"
|
||||
android:title="@string/alert_slider_mute_media_title"
|
||||
android:summary="@string/alert_slider_mute_media_summary"
|
||||
android:defaultValue="0" />
|
||||
|
||||
<ListPreference
|
||||
android:key="config_top_position"
|
||||
android:dialogTitle="@string/alert_slider_selection_dialog_title"
|
||||
|
||||
@@ -5,15 +5,22 @@
|
||||
|
||||
package org.lineageos.settings.device
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.media.AudioManager
|
||||
import android.media.AudioSystem
|
||||
import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import android.provider.Settings
|
||||
import android.view.KeyEvent
|
||||
import com.android.internal.os.DeviceKeyHandler
|
||||
|
||||
class KeyHandler(context: Context) : DeviceKeyHandler {
|
||||
private val audioManager = context.getSystemService(AudioManager::class.java)
|
||||
private val notificationManager = context.getSystemService(NotificationManager::class.java)
|
||||
private val vibrator = context.getSystemService(Vibrator::class.java)
|
||||
private val packageContext = context.createPackageContext(
|
||||
KeyHandler::class.java.getPackage()!!.name, 0
|
||||
@@ -24,23 +31,35 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
|
||||
Context.MODE_PRIVATE or Context.MODE_MULTI_PROCESS
|
||||
)
|
||||
|
||||
private var wasMuted = false
|
||||
private val broadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val stream = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1)
|
||||
val state = intent.getBooleanExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, false)
|
||||
if (stream == AudioSystem.STREAM_MUSIC && state == false) {
|
||||
wasMuted = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
context.registerReceiver(
|
||||
broadcastReceiver,
|
||||
IntentFilter(AudioManager.STREAM_MUTE_CHANGED_ACTION)
|
||||
)
|
||||
}
|
||||
|
||||
override fun handleKeyEvent(event: KeyEvent): KeyEvent {
|
||||
if (event.action == KeyEvent.ACTION_DOWN) {
|
||||
when (event.scanCode) {
|
||||
POSITION_TOP -> {
|
||||
val mode = sharedPreferences.getString(ALERT_SLIDER_TOP_KEY, "0")!!.toInt()
|
||||
audioManager.setRingerModeInternal(mode)
|
||||
vibrateIfNeeded(mode)
|
||||
handleMode(sharedPreferences.getString(ALERT_SLIDER_TOP_KEY, "0")!!.toInt())
|
||||
}
|
||||
POSITION_MIDDLE -> {
|
||||
val mode = sharedPreferences.getString(ALERT_SLIDER_MIDDLE_KEY, "1")!!.toInt()
|
||||
audioManager.setRingerModeInternal(mode)
|
||||
vibrateIfNeeded(mode)
|
||||
handleMode(sharedPreferences.getString(ALERT_SLIDER_MIDDLE_KEY, "1")!!.toInt())
|
||||
}
|
||||
POSITION_BOTTOM -> {
|
||||
val mode = sharedPreferences.getString(ALERT_SLIDER_BOTTOM_KEY, "2")!!.toInt()
|
||||
audioManager.setRingerModeInternal(mode)
|
||||
vibrateIfNeeded(mode)
|
||||
handleMode(sharedPreferences.getString(ALERT_SLIDER_BOTTOM_KEY, "2")!!.toInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,6 +73,36 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleMode(mode: Int) {
|
||||
val muteMedia = sharedPreferences.getBoolean(MUTE_MEDIA_WITH_SILENT, false)
|
||||
|
||||
when (mode) {
|
||||
AudioManager.RINGER_MODE_SILENT -> {
|
||||
notificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG)
|
||||
audioManager.setRingerModeInternal(mode)
|
||||
if (muteMedia) {
|
||||
audioManager.adjustVolume(AudioManager.ADJUST_MUTE, 0)
|
||||
wasMuted = true
|
||||
}
|
||||
}
|
||||
AudioManager.RINGER_MODE_VIBRATE, AudioManager.RINGER_MODE_NORMAL -> {
|
||||
notificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG)
|
||||
audioManager.setRingerModeInternal(mode)
|
||||
if (muteMedia && wasMuted) {
|
||||
audioManager.adjustVolume(AudioManager.ADJUST_UNMUTE, 0)
|
||||
}
|
||||
}
|
||||
ZEN_PRIORITY_ONLY, ZEN_TOTAL_SILENCE, ZEN_ALARMS_ONLY -> {
|
||||
audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL)
|
||||
notificationManager.setZenMode(mode - ZEN_OFFSET, null, TAG)
|
||||
if (muteMedia && wasMuted) {
|
||||
audioManager.adjustVolume(AudioManager.ADJUST_UNMUTE, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
vibrateIfNeeded(mode)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "KeyHandler"
|
||||
|
||||
@@ -66,6 +115,13 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
|
||||
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"
|
||||
private const val MUTE_MEDIA_WITH_SILENT = "config_mute_media"
|
||||
|
||||
// ZEN constants
|
||||
private const val ZEN_OFFSET = 2
|
||||
private const val ZEN_PRIORITY_ONLY = 3
|
||||
private const val ZEN_TOTAL_SILENCE = 4
|
||||
private const val ZEN_ALARMS_ONLY = 5
|
||||
|
||||
// Vibration effects
|
||||
private val MODE_NORMAL_EFFECT = VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK)
|
||||
|
||||
Reference in New Issue
Block a user