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

Commit cc3fcccd authored by Haijie Hong's avatar Haijie Hong
Browse files

Add shared models for device settings

BUG: 343317785
Test: atest DeviceSettingRepositoryTest
Flag: com.android.settings.flags.enable_bluetooth_device_details_polish
Change-Id: I9dce0527713c11048b57b38ebc6fb46e6ba3e473
parent 6a496f11
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.settingslib.bluetooth.devicesettings.shared.model

import android.content.Intent
import android.graphics.Bitmap
import com.android.settingslib.bluetooth.CachedBluetoothDevice
import com.android.settingslib.bluetooth.devicesettings.DeviceSettingId

/** Models a device setting. */
sealed interface DeviceSettingModel {
    val cachedDevice: CachedBluetoothDevice
    @DeviceSettingId val id: Int

    /** Models a device setting which should be displayed as an action/switch preference. */
    data class ActionSwitchPreference(
        override val cachedDevice: CachedBluetoothDevice,
        @DeviceSettingId override val id: Int,
        val title: String,
        val summary: String? = null,
        val icon: Bitmap? = null,
        val intent: Intent? = null,
        val switchState: DeviceSettingStateModel.ActionSwitchPreferenceState? = null,
        val isAllowedChangingState: Boolean = true,
        val updateState: ((DeviceSettingStateModel.ActionSwitchPreferenceState) -> Unit)? = null,
    ) : DeviceSettingModel

    /** Models a device setting which should be displayed as a multi-toggle preference. */
    data class MultiTogglePreference(
        override val cachedDevice: CachedBluetoothDevice,
        @DeviceSettingId override val id: Int,
        val title: String,
        val toggles: List<ToggleModel>,
        val isActive: Boolean,
        val state: DeviceSettingStateModel.MultiTogglePreferenceState,
        val isAllowedChangingState: Boolean,
        val updateState: (DeviceSettingStateModel.MultiTogglePreferenceState) -> Unit
    ) : DeviceSettingModel

    /** Models an unknown preference. */
    data class Unknown(
        override val cachedDevice: CachedBluetoothDevice,
        @DeviceSettingId override val id: Int
    ) : DeviceSettingModel
}

/** Models a toggle in [DeviceSettingModel.MultiTogglePreference]. */
data class ToggleModel(val label: String, val icon: Bitmap)
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.settingslib.bluetooth.devicesettings.shared.model

import com.android.settingslib.bluetooth.devicesettings.DeviceSettingPreferenceState

/** Models a device setting state. */
sealed interface DeviceSettingStateModel {
    fun toParcelable(): DeviceSettingPreferenceState

    /** Models a device setting state for action/switch preference. */
    data class ActionSwitchPreferenceState(val checked: Boolean) : DeviceSettingStateModel {
        override fun toParcelable() =
            com.android.settingslib.bluetooth.devicesettings.ActionSwitchPreferenceState.Builder()
                .setChecked(checked)
                .build()
    }

    /** Models a device setting state for multi-toggle preference. */
    data class MultiTogglePreferenceState(val selectedIndex: Int) : DeviceSettingStateModel {
        override fun toParcelable() =
            com.android.settingslib.bluetooth.devicesettings.MultiTogglePreferenceState.Builder()
                .setState(selectedIndex)
                .build()
    }
}