blob: 74c0dcb233cb0548afce668f9c2f5343f7602f0d [file]
/*
* Copyright (C) 2026 The Android Open Source 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.inputmethod
import android.Manifest.permission.WRITE_SECURE_SETTINGS
import android.hardware.input.InputSettings
import android.provider.Settings
import com.android.internal.accessibility.AccessibilityShortcutController.MOUSE_KEYS_COMPONENT_NAME
import com.android.settings.R
import com.android.settings.accessibility.AccessibilityUtil
import com.android.settings.flags.Flags as SettingsFlags
import com.android.settingslib.catalyst.flags.Flags as CatalystFlags
import com.android.settingslib.metadata.IntroducedVersion.API_CINNAMON_BUN
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.SensitivityLevel
import com.android.settingslib.metadata.preferencesapi.PreferencesApiScreen
import com.android.settingslib.metadata.preferencesapi.PreferenceContainer
import com.android.settingslib.metadata.preferencesapi.Stability
import com.android.settingslib.metadata.preferencesapi.category.Category
import com.android.settingslib.metadata.preferencesapi.preconditions.Allowed
import com.android.settingslib.metadata.preferencesapi.preconditions.Custom
import com.android.settingslib.metadata.preferencesapi.types.AnyBoolean
import com.android.settingslib.metadata.preferencesapi.types.IntInRange
import com.android.settingslib.metadata.preferencesapi.preconditions.preferenceValue
// LINT.IfChange
@ProvidePreferenceScreen(MouseKeysApiScreen.KEY)
class MouseKeysApiScreen :
PreferencesApiScreen(
key = KEY,
topLevelSettingsCategory = Category.ACCESSIBILITY,
fragment = MouseKeysMainPageFragment::class,
purpose = R.string.mouse_keys_screen_purpose,
) {
init {
flag { SettingsFlags.catalystMigration26q3() }
if (CatalystFlags.settingsCatalystApi27q2()) {
precondition(
R.string.mouse_keys_precondition,
preferenceValue(MouseKeysApi.KEY_PHYSICAL_KEYBOARD_ATTACHED).isEqualTo(true)
)
} else {
precondition(R.string.mouse_keys_precondition) {
if (PhysicalKeyboardFragment.getHardKeyboards(context).isNotEmpty()) {
Allowed
} else {
Custom(R.string.mouse_keys_unsupported, Stability.UNSTABLE)
}
}
}
// Use mouse keys (Toggle)
preference(
key = KEY_MOUSE_KEYS_MAIN_SWITCH,
type = AnyBoolean,
purpose = R.string.mouse_keys_main_switch_purpose,
introducedVersion = API_CINNAMON_BUN,
) {
sensitivityLevel(SensitivityLevel.NO_SENSITIVITY)
get { execute { InputSettings.isAccessibilityMouseKeysEnabled(context) } }
set {
permissions(WRITE_SECURE_SETTINGS)
execute { value -> InputSettings.setAccessibilityMouseKeysEnabled(context, value) }
}
}
// Mouse keys shortcut (Toggle)
preference(
key = KEY_MOUSE_KEYS_SHORTCUT_PREFERENCE,
type = AnyBoolean,
purpose = R.string.mouse_keys_shortcut_preference_purpose,
introducedVersion = API_CINNAMON_BUN,
) {
sensitivityLevel(SensitivityLevel.NO_SENSITIVITY)
get {
execute {
Settings.Secure.getString(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE,
)
?.contains(MOUSE_KEYS_COMPONENT_NAME.flattenToString()) == true
}
}
set {
permissions(WRITE_SECURE_SETTINGS)
execute { value ->
val current =
Settings.Secure.getString(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE,
) ?: ""
val targets = current.split(":").filter { it.isNotEmpty() }.toMutableSet()
if (value) {
targets.add(MOUSE_KEYS_COMPONENT_NAME.flattenToString())
} else {
targets.remove(MOUSE_KEYS_COMPONENT_NAME.flattenToString())
}
Settings.Secure.putString(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE,
targets.joinToString(":"),
)
}
}
}
// Acceleration (Slider)
preference(
key = KEY_MOUSE_KEYS_ACCELERATION_SLIDER,
type = IntInRange(0, 10),
purpose = R.string.mouse_keys_acceleration_slider_purpose,
introducedVersion = API_CINNAMON_BUN,
) {
flag { AccessibilityUtil.isMouseKeyEnhancementEnabled() }
sensitivityLevel(SensitivityLevel.NO_SENSITIVITY)
get {
execute {
val acceleration =
Settings.Secure.getFloat(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_MOUSE_KEYS_ACCELERATION,
InputSettings.DEFAULT_MOUSE_KEYS_ACCELERATION,
)
Math.round(acceleration / 0.1f)
}
}
set {
permissions(WRITE_SECURE_SETTINGS)
execute { value ->
Settings.Secure.putFloat(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_MOUSE_KEYS_ACCELERATION,
value * 0.1f,
)
}
}
}
// Speed (Slider)
preference(
key = KEY_MOUSE_KEYS_MAX_SPEED_SLIDER,
type = IntInRange(1, 10),
purpose = R.string.mouse_keys_max_speed_slider_purpose,
introducedVersion = API_CINNAMON_BUN,
) {
flag { AccessibilityUtil.isMouseKeyEnhancementEnabled() }
sensitivityLevel(SensitivityLevel.NO_SENSITIVITY)
get {
execute {
Settings.Secure.getInt(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_MOUSE_KEYS_MAX_SPEED,
InputSettings.DEFAULT_MOUSE_KEYS_MAX_SPEED,
)
}
}
set {
permissions(WRITE_SECURE_SETTINGS)
execute { value ->
Settings.Secure.putInt(
context.contentResolver,
Settings.Secure.ACCESSIBILITY_MOUSE_KEYS_MAX_SPEED,
value,
)
}
}
}
// Use primary keys (Toggle)
preference(
key = KEY_MOUSE_KEYS_USE_PRIMARY_KEY_PREFERENCE,
type = AnyBoolean,
purpose = R.string.mouse_keys_use_primary_keys_preference_purpose,
introducedVersion = API_CINNAMON_BUN,
) {
flag { AccessibilityUtil.isMouseKeyEnhancementEnabled() }
sensitivityLevel(SensitivityLevel.NO_SENSITIVITY)
get { execute { InputSettings.isPrimaryKeysForMouseKeysEnabled(context) } }
set {
permissions(WRITE_SECURE_SETTINGS)
execute { value -> InputSettings.setPrimaryKeysForMouseKeysEnabled(context, value) }
}
}
}
companion object {
const val KEY = "mouse_keys_main_page"
const val KEY_MOUSE_KEYS_MAIN_SWITCH = "prf_mouse_keys_enabled"
const val KEY_MOUSE_KEYS_SHORTCUT_PREFERENCE = "prf_mouse_keys_shortcut_enabled"
const val KEY_MOUSE_KEYS_ACCELERATION_SLIDER = "prf_mouse_keys_acceleration"
const val KEY_MOUSE_KEYS_MAX_SPEED_SLIDER = "prf_mouse_keys_max_speed"
const val KEY_MOUSE_KEYS_USE_PRIMARY_KEY_PREFERENCE =
"prf_mouse_keys_use_primary_keys_enabled"
}
}
// LINT.ThenChange(MouseKeysMainPageFragment.java)
@ProvidePreferenceScreen(MouseKeysApi.KEY)
class MouseKeysApi :
PreferenceContainer(key = KEY) {
init {
flag { CatalystFlags.settingsCatalystApi27q2() }
preference(
key = KEY_PHYSICAL_KEYBOARD_ATTACHED,
type = AnyBoolean,
purpose = R.string.physical_keyboard_attached_purpose,
introducedVersion = API_CINNAMON_BUN,
) {
sensitivityLevel(SensitivityLevel.NO_SENSITIVITY)
get { execute { PhysicalKeyboardFragment.getHardKeyboards(context).isNotEmpty() } }
}
}
companion object {
const val KEY = "mouse_keys"
const val KEY_PHYSICAL_KEYBOARD_ATTACHED = "physical_keyboard_attached"
}
}