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

Commit 9fa6b6b7 authored by brycelee's avatar brycelee
Browse files

Make doze sensors available for use outside doze module.

This changelist refactors dagger dependencies to allow doze
brightness sensors to be accessed outside of doze components. These
sensors are also used for low-light detection in dreams now.

Bug: 432103856
Test: manual verified build
Flag: EXEMPT refactor
Change-Id: I4f29d2bc31dd5c7e5eb56c56f9f898c98ade38e6
parent 6368174d
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -68,6 +68,7 @@ import com.android.systemui.desktop.dagger.DesktopModule;
import com.android.systemui.deviceentry.DeviceEntryModule;
import com.android.systemui.deviceentry.DeviceEntryModule;
import com.android.systemui.display.DisplayModule;
import com.android.systemui.display.DisplayModule;
import com.android.systemui.doze.dagger.DozeComponent;
import com.android.systemui.doze.dagger.DozeComponent;
import com.android.systemui.doze.dagger.RootDozeModule;
import com.android.systemui.dreams.dagger.DreamModule;
import com.android.systemui.dreams.dagger.DreamModule;
import com.android.systemui.flags.FeatureFlags;
import com.android.systemui.flags.FeatureFlags;
import com.android.systemui.flags.FlagDependenciesModule;
import com.android.systemui.flags.FlagDependenciesModule;
@@ -247,6 +248,7 @@ import javax.inject.Named;
        DeviceEntryModule.class,
        DeviceEntryModule.class,
        DisableFlagsModule.class,
        DisableFlagsModule.class,
        DisplayModule.class,
        DisplayModule.class,
        RootDozeModule.class,
        DreamModule.class,
        DreamModule.class,
        EventLogModule.class,
        EventLogModule.class,
        FalsingModule.class,
        FalsingModule.class,
@@ -352,6 +354,7 @@ public abstract class SystemUIModule {
    public abstract NotificationRowBinder bindNotificationRowBinder(
    public abstract NotificationRowBinder bindNotificationRowBinder(
            NotificationRowBinderImpl notificationRowBinder);
            NotificationRowBinderImpl notificationRowBinder);



    @SysUISingleton
    @SysUISingleton
    @Provides
    @Provides
    static SysUiState provideSysUiState(
    static SysUiState provideSysUiState(
+3 −56
Original line number Original line Diff line number Diff line
@@ -16,9 +16,7 @@


package com.android.systemui.doze.dagger;
package com.android.systemui.doze.dagger;


import android.content.Context;
import com.android.systemui.Flags;
import android.hardware.Sensor;

import com.android.systemui.doze.DozeAuthRemover;
import com.android.systemui.doze.DozeAuthRemover;
import com.android.systemui.doze.DozeBrightnessHostForwarder;
import com.android.systemui.doze.DozeBrightnessHostForwarder;
import com.android.systemui.doze.DozeDockHandler;
import com.android.systemui.doze.DozeDockHandler;
@@ -30,30 +28,21 @@ import com.android.systemui.doze.DozePauser;
import com.android.systemui.doze.DozeScreenBrightness;
import com.android.systemui.doze.DozeScreenBrightness;
import com.android.systemui.doze.DozeScreenState;
import com.android.systemui.doze.DozeScreenState;
import com.android.systemui.doze.DozeScreenStatePreventingAdapter;
import com.android.systemui.doze.DozeScreenStatePreventingAdapter;
import com.android.systemui.doze.DozeSensors;
import com.android.systemui.doze.DozeSuppressor;
import com.android.systemui.doze.DozeSuppressor;
import com.android.systemui.doze.DozeSuspendScreenStatePreventingAdapter;
import com.android.systemui.doze.DozeSuspendScreenStatePreventingAdapter;
import com.android.systemui.doze.DozeTransitionListener;
import com.android.systemui.doze.DozeTransitionListener;
import com.android.systemui.doze.DozeTriggers;
import com.android.systemui.doze.DozeTriggers;
import com.android.systemui.doze.DozeUi;
import com.android.systemui.doze.DozeUi;
import com.android.systemui.doze.DozeWallpaperState;
import com.android.systemui.doze.DozeWallpaperState;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.statusbar.policy.DevicePostureController;
import com.android.systemui.util.sensors.AsyncSensorManager;
import com.android.systemui.util.wakelock.DelayedWakeLock;
import com.android.systemui.util.wakelock.DelayedWakeLock;
import com.android.systemui.util.wakelock.WakeLock;
import com.android.systemui.util.wakelock.WakeLock;
import java.util.List;
import java.util.ArrayList;
import com.android.systemui.Flags;


import dagger.Module;
import dagger.Module;
import dagger.Provides;
import dagger.Provides;


import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/** Dagger module for use with {@link com.android.systemui.doze.dagger.DozeComponent}. */
/** Dagger module for use with {@link com.android.systemui.doze.dagger.DozeComponent}. */
@Module
@Module
@@ -106,46 +95,4 @@ public abstract class DozeModule {


        return parts.toArray(new DozeMachine.Part[0]);
        return parts.toArray(new DozeMachine.Part[0]);
    }
    }

    @Provides
    @BrightnessSensor
    static Optional<Sensor>[] providesBrightnessSensors(
            AsyncSensorManager sensorManager,
            Context context,
            DozeParameters dozeParameters) {
        String[] sensorNames = dozeParameters.brightnessNames();
        if (sensorNames.length == 0 || sensorNames == null) {
            // if no brightness names are specified, just use the brightness sensor type
            return new Optional[]{
                    Optional.ofNullable(DozeSensors.findSensor(
                            sensorManager,
                            context.getString(R.string.doze_brightness_sensor_type),
                            null
                    ))
            };
        }

        // length and index of brightnessMap correspond to DevicePostureController.DevicePostureInt:
        final Optional<Sensor>[] brightnessSensorMap =
                new Optional[DevicePostureController.SUPPORTED_POSTURES_SIZE];
        Arrays.fill(brightnessSensorMap, Optional.empty());

        // Map of sensorName => Sensor, so we reuse the same sensor if it's the same between
        // postures
        Map<String, Optional<Sensor>> nameToSensorMap = new HashMap<>();
        for (int i = 0; i < sensorNames.length; i++) {
            final String sensorName = sensorNames[i];
            if (!nameToSensorMap.containsKey(sensorName)) {
                nameToSensorMap.put(sensorName,
                        Optional.ofNullable(
                                DozeSensors.findSensor(
                                        sensorManager,
                                        context.getString(R.string.doze_brightness_sensor_type),
                                        sensorNames[i]
                                )));
            }
            brightnessSensorMap[i] = nameToSensorMap.get(sensorName);
        }
        return brightnessSensorMap;
    }
}
}
+89 −0
Original line number Original line 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.systemui.doze.dagger

import android.content.Context
import android.hardware.Sensor
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.doze.DozeSensors
import com.android.systemui.res.R
import com.android.systemui.statusbar.phone.DozeParameters
import com.android.systemui.statusbar.policy.DevicePostureController
import com.android.systemui.util.sensors.AsyncSensorManager
import dagger.Module
import dagger.Provides
import java.util.HashMap
import java.util.Optional

/**
 * A module for allowing components outside the doze package to depend on doze-related dependencies
 * such as the doze brightness sensors.
 */
@Module
abstract class RootDozeModule {
    companion object {
        @SysUISingleton
        @Provides
        @BrightnessSensor
        fun providesBrightnessSensors(
            sensorManager: AsyncSensorManager?,
            context: Context,
            dozeParameters: DozeParameters,
        ): Array<Optional<Sensor>> {
            val sensorNames: Array<String> = dozeParameters.brightnessNames() ?: emptyArray()
            if (sensorNames.isEmpty()) {
                // if no brightness names are specified, just use the brightness sensor type
                return arrayOf(
                    Optional.ofNullable<Sensor>(
                        DozeSensors.findSensor(
                            sensorManager,
                            context.getString(R.string.doze_brightness_sensor_type),
                            null,
                        )
                    )
                )
            }

            // length and index of brightnessMap correspond to
            // DevicePostureController.DevicePostureInt:
            val brightnessSensorMap: Array<Optional<Sensor>> =
                Array<Optional<Sensor>>(DevicePostureController.SUPPORTED_POSTURES_SIZE) {
                    Optional.empty<Sensor>()
                }

            // Map of sensorName => Sensor, so we reuse the same sensor if it's the same between
            // postures
            val nameToSensorMap: MutableMap<String, Optional<Sensor>> =
                HashMap<String, Optional<Sensor>>()
            for (i in sensorNames.indices) {
                val sensorName = sensorNames[i]
                if (!nameToSensorMap.containsKey(sensorName)) {
                    nameToSensorMap[sensorName] =
                        Optional.ofNullable<android.hardware.Sensor>(
                            DozeSensors.findSensor(
                                sensorManager,
                                context.getString(R.string.doze_brightness_sensor_type),
                                sensorNames[i],
                            )
                        )
                }
                nameToSensorMap[sensorName]?.let { brightnessSensorMap[i] = it }
            }
            return brightnessSensorMap
        }
    }
}
+10 −0
Original line number Original line Diff line number Diff line
@@ -26,6 +26,16 @@ enum class DevicePosture {
    OPENED,
    OPENED,
    FLIPPED;
    FLIPPED;


    fun toDevicePostureInt(): Int {
        return when (this) {
            CLOSED -> DevicePostureController.DEVICE_POSTURE_CLOSED
            HALF_OPENED -> DevicePostureController.DEVICE_POSTURE_HALF_OPENED
            OPENED -> DevicePostureController.DEVICE_POSTURE_OPENED
            FLIPPED -> DevicePostureController.DEVICE_POSTURE_FLIPPED
            UNKNOWN -> DevicePostureController.DEVICE_POSTURE_UNKNOWN
        }
    }

    companion object {
    companion object {
        fun toPosture(@DevicePostureController.DevicePostureInt posture: Int): DevicePosture {
        fun toPosture(@DevicePostureController.DevicePostureInt posture: Int): DevicePosture {
            return when (posture) {
            return when (posture) {