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

Commit 3d66a583 authored by Bruno Martins's avatar Bruno Martins
Browse files

SystemUI: Re-implement clock auto-hiding feature



Co-authored-by: default avatarCédric Bellegarde <cedric.bellegarde@adishatz.org>
Change-Id: Idb0501b3d6903eac8e3768b25fd9b80852ba8fb7
parent 5ee8b444
Loading
Loading
Loading
Loading
+59 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.statusbar.pipeline.shared.ui.binder

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.app.WindowConfiguration
import android.content.ContentResolver
import android.database.ContentObserver
import android.net.Uri
@@ -35,6 +36,9 @@ import com.android.systemui.display.dagger.SystemUIDisplaySubcomponent.PerDispla
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.res.R
import com.android.systemui.scene.shared.flag.SceneContainerFlag
import com.android.systemui.shared.system.ActivityManagerWrapper
import com.android.systemui.shared.system.TaskStackChangeListener
import com.android.systemui.shared.system.TaskStackChangeListeners
import com.android.systemui.statusbar.chips.mediaprojection.domain.model.MediaProjectionStopDialogModel
import com.android.systemui.statusbar.chips.ui.binder.OngoingActivityChipBinder
import com.android.systemui.statusbar.chips.ui.binder.OngoingActivityChipViewBinding
@@ -100,6 +104,7 @@ constructor(
    }

    private data class ClockState(
        val autoHide: Boolean,
        val denyListed: Boolean,
        val hideForHun: Boolean,
        val position: Int,
@@ -140,6 +145,7 @@ constructor(
                val clockState =
                    MutableStateFlow(
                        ClockState(
                            autoHide = false,
                            denyListed = false,
                            hideForHun = false,
                            position = context.contentResolver.readClockPosition(),
@@ -147,16 +153,48 @@ constructor(
                        )
                    )

                val clockAutoHideUri: Uri =
                    LineageSettings.System.getUriFor(
                        LineageSettings.System.STATUS_BAR_CLOCK_AUTO_HIDE
                    )
                val iconHideListUri: Uri =
                    Settings.Secure.getUriFor(StatusBarIconController.ICON_HIDE_LIST)
                val statusBarClockUri: Uri =
                    LineageSettings.System.getUriFor(LineageSettings.System.STATUS_BAR_CLOCK)

                val taskStackListener =
                    object : TaskStackChangeListener {
                        override fun onTaskStackChanged() {
                            val autoHide = shouldClockAutoHideForCurrentTask()

                            if (clockState.value.autoHide != autoHide) {
                                clockState.update { it.copy(autoHide = autoHide) }
                            }
                        }
                    }

                val contentObserver =
                    object : ContentObserver(Handler(Looper.getMainLooper())) {
                        override fun onChange(selfChange: Boolean, uri: Uri?) {
                            clockState.update { current ->
                                when (uri) {
                                    clockAutoHideUri -> {
                                        val enabled =
                                            context.contentResolver.readClockAutoHide() != 0

                                        if (enabled) {
                                            TaskStackChangeListeners.getInstance()
                                                .registerTaskStackListener(taskStackListener)
                                        } else {
                                            TaskStackChangeListeners.getInstance()
                                                .unregisterTaskStackListener(taskStackListener)
                                        }

                                        current.copy(
                                            autoHide =
                                                enabled && shouldClockAutoHideForCurrentTask()
                                        )
                                    }
                                    iconHideListUri ->
                                        current.copy(
                                            denyListed =
@@ -179,7 +217,7 @@ constructor(
                        }
                    }

                val urisToObserve = listOf(iconHideListUri, statusBarClockUri)
                val urisToObserve = listOf(clockAutoHideUri, iconHideListUri, statusBarClockUri)
                urisToObserve.forEach { uri ->
                    context.contentResolver.registerContentObserver(
                        uri,
@@ -195,6 +233,8 @@ constructor(
                job?.invokeOnCompletion {
                    runCatching {
                        context.contentResolver.unregisterContentObserver(contentObserver)
                        TaskStackChangeListeners.getInstance()
                            .unregisterTaskStackListener(taskStackListener)
                    }
                }

@@ -405,6 +445,7 @@ constructor(
                                if (
                                    state.visibilityModel.visibility == View.VISIBLE &&
                                        !hunBlocksClock &&
                                        !state.autoHide &&
                                        !state.denyListed
                                ) {
                                    state.visibilityModel
@@ -509,6 +550,15 @@ constructor(
        }
    }

    private fun ContentResolver.readClockAutoHide(): Int {
        return LineageSettings.System.getIntForUser(
            this,
            LineageSettings.System.STATUS_BAR_CLOCK_AUTO_HIDE,
            0,
            UserHandle.USER_CURRENT,
        )
    }

    private fun ContentResolver.readClockPosition(): Int {
        return LineageSettings.System.getIntForUser(
            this,
@@ -518,6 +568,14 @@ constructor(
        )
    }

    private fun shouldClockAutoHideForCurrentTask(): Boolean {
        return ActivityManagerWrapper.getInstance()
            .runningTask
            ?.configuration
            ?.windowConfiguration
            ?.activityType == WindowConfiguration.ACTIVITY_TYPE_HOME
    }

    private fun SystemEventAnimationState.isAnimatingChip() =
        when (this) {
            AnimatingIn,