Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 81b9e383 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Catalyst] Migrate ring vibration switch preference" into main

parents 567da75b 833bccc6
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -27,10 +27,11 @@

    <PreferenceCategory
        android:key="vibration_category_call"
        android:title="@string/accessibility_call_vibration_category_title">
        android:title="@string/accessibility_call_vibration_category_title"
        android:persistent="false">

        <SwitchPreferenceCompat
            android:key="vibration_preference_ring"
            android:key="ring_vibration_intensity"
            android:title="@string/accessibility_ring_vibration_title"
            app:keywords="@string/keywords_ring_vibration"
            app:controller="com.android.settings.accessibility.RingVibrationTogglePreferenceController" />
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.accessibility

import android.os.VibrationAttributes
import android.provider.Settings
import com.android.settings.R

/** Accessibility settings for ring vibration. */
// LINT.IfChange
class RingVibrationIntensitySwitchPreference :
    VibrationIntensitySwitchPreference(
        key = KEY,
        vibrationUsage = VibrationAttributes.USAGE_RINGTONE,
        title = R.string.accessibility_ring_vibration_title,
    ) {
    override val keywords: Int
        get() = R.string.keywords_ring_vibration

    companion object {
        const val KEY = Settings.System.RING_VIBRATION_INTENSITY
    }
}
// LINT.ThenChange(RingVibrationTogglePreferenceController.java)
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.accessibility;
import android.content.Context;

/** Preference controller for ringtone vibration with only a toggle for on/off states. */
// LINT.IfChange
public class RingVibrationTogglePreferenceController extends VibrationTogglePreferenceController {

    public RingVibrationTogglePreferenceController(Context context, String preferenceKey) {
@@ -30,3 +31,4 @@ public class RingVibrationTogglePreferenceController extends VibrationTogglePref
        return AVAILABLE;
    }
}
// LINT.ThenChange(RingVibrationIntensitySwitchPreference.java)
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.accessibility

import android.content.Context
import android.os.VibrationAttributes.Usage
import android.os.Vibrator
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.KeyValueStoreDelegate
import com.android.settingslib.datastore.SettingsSystemStore

/** SettingsStore for vibration intensity preferences with custom default value. */
class VibrationIntensitySettingsStore(
    context: Context,
    @Usage vibrationUsage: Int,
    override val keyValueStoreDelegate: KeyValueStore = SettingsSystemStore.get(context),
    private val defaultIntensity: Int = context.getDefaultVibrationIntensity(vibrationUsage),
) : KeyValueStoreDelegate {

    override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) =
        intensityToValue(valueType, defaultIntensity)

    override fun <T : Any> getValue(key: String, valueType: Class<T>) =
        intensityToValue(valueType, keyValueStoreDelegate.getInt(key) ?: defaultIntensity)

    override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) =
        keyValueStoreDelegate.setInt(key, value?.let { valueToIntensity(valueType, it) })

    @Suppress("UNCHECKED_CAST")
    private fun <T: Any> intensityToValue(valueType: Class<T>, intensity: Int): T? =
        when (valueType) {
            Boolean::class.javaObjectType -> intensity != Vibrator.VIBRATION_INTENSITY_OFF
            Int::class.javaObjectType -> intensity
            else -> null
        } as T?

    private fun <T: Any> valueToIntensity(valueType: Class<T>, value: T): Int? =
        when (valueType) {
            Boolean::class.javaObjectType ->
                if (value as Boolean) defaultIntensity else Vibrator.VIBRATION_INTENSITY_OFF
            Int::class.javaObjectType -> value as Int
            else -> null
        }
}

/** Returns the device default vibration intensity for given usage. */
private fun Context.getDefaultVibrationIntensity(@Usage vibrationUsage: Int): Int =
    getSystemService(Vibrator::class.java).getDefaultVibrationIntensity(vibrationUsage)
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.accessibility

import android.content.Context
import android.os.VibrationAttributes.Usage
import androidx.annotation.CallSuper
import androidx.annotation.StringRes
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.SwitchPreference
import com.android.settingslib.preference.SwitchPreferenceBinding

/**
 * SwitchPreference for vibration intensity.
 *
 * This implementation uses VibrationIntensitySettingsStore to save the device default vibration
 * intensity value when the switch is turned on, also playing a haptic preview.
 */
// LINT.IfChange
open class VibrationIntensitySwitchPreference(
    key: String,
    @Usage val vibrationUsage: Int,
    @StringRes title: Int = 0,
    @StringRes summary: Int = 0,
) : SwitchPreference(key, title, summary),
    OnPreferenceChangeListener,
    SwitchPreferenceBinding {

    override fun storage(context: Context): KeyValueStore =
        VibrationIntensitySettingsStore(context, vibrationUsage)

    @CallSuper
    override fun bind(preference: Preference, metadata: PreferenceMetadata) {
        super.bind(preference, metadata)
        preference.onPreferenceChangeListener = this
    }

    override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
        if (newValue as Boolean) {
            preference.context.playVibrationSettingsPreview(vibrationUsage)
        }
        return true
    }
}
// LINT.ThenChange(VibrationTogglePreferenceController.java)
Loading