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

Unverified Commit ad39fa1e authored by rmp22's avatar rmp22 Committed by Michael Bestas
Browse files

SystemUI: Add auto brightness toggle in brightness slider in new compose QS



Drawables are from Paranoid Android

Co-authored-by: default avatarAbhay Singh Gill <abhaygill017@gmail.com>
Co-authored-by: default avatarAndrew Fluck <andrew@aospa.co>
Co-authored-by: default avatarIdo Ben-Hur <idoybh2@gmail.com>
Co-authored-by: default avatarLuca Stefani <luca.stefani.ge1@gmail.com>
Co-authored-by: default avatarMichael Bestas <mkbestas@lineageos.org>
Co-authored-by: default avatarPranav Vashi <neobuddy89@gmail.com>
Change-Id: I8fb0a1692a3431754c02bf237da9bfa05f25e44d
parent 002a97c9
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.settings.SystemSettings
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
@@ -63,6 +64,7 @@ class ScreenBrightnessDisplayManagerRepositoryTest : SysuiTestCase() {

    @Mock private lateinit var displayManager: DisplayManager
    @Mock private lateinit var display: Display
    @Mock private lateinit var systemSettings: SystemSettings

    private val displayId = 0

@@ -78,6 +80,7 @@ class ScreenBrightnessDisplayManagerRepositoryTest : SysuiTestCase() {
                logcatTableLogBuffer(kosmos, "screenBrightness"),
                kosmos.applicationCoroutineScope,
                kosmos.testDispatcher,
                systemSettings,
            )

        whenever(displayManager.getDisplay(displayId)).thenReturn(display)
+55 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 Paranoid Android

     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.
-->
<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:name="sun"
        android:pathData="M 20 8.69 L 20 4 L 15.31 4 L 12 0.69 L 8.69 4 L 4 4 L 4 8.69 L 0.69 12 L 4 15.31 L 4 20 L 8.69 20 L 12 23.31 L 15.31 20 L 20 20 L 20 15.31 L 23.31 12 L 20 8.69 Z M 18 14.48 L 18 18 L 14.48 18 L 12 20.48 L 9.52 18 L 6 18 L 6 14.48 L 3.52 12 L 6 9.52 L 6 6 L 9.52 6 L 12 3.52 L 14.48 6 L 18 6 L 18 9.52 L 20.48 12 L 18 14.48 Z"
        android:fillColor="#ffffff" />
    <group android:name="mask_group">
        <clip-path
            android:name="mask"
            android:pathData="M 18 14.48 L 18 18 L 14.48 18 L 12 20.48 L 9.52 18 L 6 18 L 6 14.48 L 3.52 12 L 6 9.52 L 6 6 L 9.52 6 L 12 3.52 L 14.48 6 L 18 6 L 18 9.52 L 20.48 12 L 18 14.48 Z"/>
        <group
            android:name="auto_transform"
            android:pivotX="12"
            android:pivotY="12"
            android:translateX="-24"
            android:scaleX="0.5"
            android:scaleY="0.5">
            <path
                android:name="auto"
                android:pathData="M 11 7 L 7.8 16 L 9.7 16 L 10.4 14 L 13.6 14 L 14.3 16 L 16.2 16 L 13 7 L 11 7 Z M 12 9 L 13.15 12.65 L 10.85 12.65 L 12 9 Z"
                android:fillColor="#ffffff" />
        </group>
        <group
            android:name="manual_transform"
            android:pivotX="12"
            android:pivotY="12"
            android:translateX="0"
            android:scaleX="1"
            android:scaleY="1">
            <path
                android:name="manual"
                android:pathData="M 11.994 13.159 L 10.159 7.007 L 7.929 7.007 L 7.929 16 L 9.678 16 L 9.678 12.936 L 9.56 9.809 L 11.314 16 L 12.667 16 L 14.427 9.809 L 14.31 12.936 L 14.31 16 L 16.052 16 L 16.052 7.007 L 13.822 7.007 L 11.994 13.159 Z"
                android:fillColor="#ffffff" />
        </group>
    </group>
</vector>
+56 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 Paranoid Android

     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.
-->
<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:name="sun"
        android:pathData="M 20 8.69 L 20 4 L 15.31 4 L 12 0.69 L 8.69 4 L 4 4 L 4 8.69 L 0.69 12 L 4 15.31 L 4 20 L 8.69 20 L 12 23.31 L 15.31 20 L 20 20 L 20 15.31 L 23.31 12 L 20 8.69 Z M 18 14.48 L 18 18 L 14.48 18 L 12 20.48 L 9.52 18 L 6 18 L 6 14.48 L 3.52 12 L 6 9.52 L 6 6 L 9.52 6 L 12 3.52 L 14.48 6 L 18 6 L 18 9.52 L 20.48 12 L 18 14.48 Z"
        android:fillColor="#ffffff" />
    <group android:name="mask_group">
        <clip-path
            android:name="mask"
            android:pathData="M 18 14.48 L 18 18 L 14.48 18 L 12 20.48 L 9.52 18 L 6 18 L 6 14.48 L 3.52 12 L 6 9.52 L 6 6 L 9.52 6 L 12 3.52 L 14.48 6 L 18 6 L 18 9.52 L 20.48 12 L 18 14.48 Z"/>
        <group
            android:name="auto_transform"
            android:pivotX="12"
            android:pivotY="12"
            android:translateX="0"
            android:scaleX="1"
            android:scaleY="1">
            <path
                android:name="auto"
                android:pathData="M 11 7 L 7.8 16 L 9.7 16 L 10.4 14 L 13.6 14 L 14.3 16 L 16.2 16 L 13 7 L 11 7 Z M 12 9 L 13.15 12.65 L 10.85 12.65 L 12 9 Z"
                android:fillColor="#ffffff" />
        </group>
        <group
            android:name="manual_transform"
            android:pivotX="12"
            android:pivotY="12"
            android:translateX="-24"
            android:scaleX="0.5"
            android:scaleY="0.5">
            <path
                android:name="manual"
                android:pathData="M 11.994 13.159 L 10.159 7.007 L 7.929 7.007 L 7.929 16 L 9.678 16 L 9.678 12.936 L 9.56 9.809 L 11.314 16 L 12.667 16 L 14.427 9.809 L 14.31 12.936 L 14.31 16 L 16.052 16 L 16.052 7.007 L 13.822 7.007 L 11.994 13.159 Z"
                android:fillColor="#ffffff" />
        </group>
    </group>
</vector>
+3 −0
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@
    <string name="vpn_credentials_password">Password</string>
    <string name="vpn_credentials_dialog_connect">Connect</string>

    <!-- QS button to enable adaptive brightness -->
    <string name="accessibility_adaptive_brightness">Adaptive brightness</string>

    <!-- Name of the firewall status bar icon. -->
    <string name="status_bar_firewall">Firewall</string>
    <string name="status_bar_firewall_slot" translatable="false">firewall</string>
+41 −0
Original line number Diff line number Diff line
@@ -17,13 +17,16 @@
package com.android.systemui.brightness.data.repository

import android.annotation.SuppressLint
import android.database.ContentObserver
import android.hardware.display.BrightnessInfo
import android.hardware.display.DisplayManager
import android.provider.Settings
import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.systemui.brightness.shared.model.BrightnessLog
import com.android.systemui.brightness.shared.model.LinearBrightness
import com.android.systemui.brightness.shared.model.formatBrightness
import com.android.systemui.brightness.shared.model.logDiffForTable
import com.android.systemui.util.settings.SystemSettings
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
@@ -78,6 +81,10 @@ interface ScreenBrightnessRepository {

    /** Sets the brightness definitively. */
    fun setBrightness(value: LinearBrightness)

    val isAutoBrightnessEnabledFlow: StateFlow<Boolean>

    fun toggleBrightnessMode()
}

@SuppressLint("MissingPermission")
@@ -91,6 +98,7 @@ constructor(
    @BrightnessLog private val tableBuffer: TableLogBuffer,
    @Application private val applicationScope: CoroutineScope,
    @Background private val backgroundContext: CoroutineContext,
    private val systemSettings: SystemSettings,
) : ScreenBrightnessRepository {

    private val apiQueue = Channel<SetBrightnessMethod>(capacity = UNLIMITED)
@@ -194,6 +202,39 @@ constructor(
        apiQueue.trySend(SetBrightnessMethod.Permanent(value))
    }

    override val isAutoBrightnessEnabledFlow: StateFlow<Boolean> =
        conflatedCallbackFlow {
            val uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE)
            val observer = object : ContentObserver(null) {
                override fun onChange(selfChange: Boolean) {
                    trySend(isAutoBrightnessEnabled())
                }
            }
            systemSettings.registerContentObserverAsync(uri, false, observer)
            trySend(isAutoBrightnessEnabled())
            awaitClose {
                systemSettings.unregisterContentObserverAsync(observer)
            }
        }
        .flowOn(backgroundContext)
        .stateIn(applicationScope, SharingStarted.WhileSubscribed(), isAutoBrightnessEnabled())

    fun isAutoBrightnessEnabled(): Boolean {
        return systemSettings.getInt(
            Settings.System.SCREEN_BRIGHTNESS_MODE,
            Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
        ) != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
    }

    override fun toggleBrightnessMode() {
        val enabled = isAutoBrightnessEnabled()
        systemSettings.putInt(
            Settings.System.SCREEN_BRIGHTNESS_MODE,
            if (enabled) Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
            else Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
        )
    }

    private sealed interface SetBrightnessMethod {
        val value: LinearBrightness

Loading