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

Commit 3cc5b540 authored by Chris Poultney's avatar Chris Poultney
Browse files

Creates an interface, implementation, and fake for UiModeManager

Also updates tests to use the fake

Bug: 331594637
Flag: NA
Test: enabled contrast and tested manually
Change-Id: Idab5e6ebab91b49d718eb4783f8693092f06f818
parent c6150e37
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import static android.Manifest.permission.MODIFY_DAY_NIGHT_MODE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.PowerManager.ACTION_POWER_SAVE_MODE_CHANGED;

import android.app.UiModeManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -43,6 +42,7 @@ import com.android.customization.module.logging.ThemesUserEventLogger;
import com.android.customization.picker.mode.DarkModeSectionView;
import com.android.themepicker.R;
import com.android.wallpaper.model.CustomizationSectionController;
import com.android.wallpaper.system.UiModeManagerWrapper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -61,18 +61,21 @@ public class DarkModeSectionController implements
    private Context mContext;
    private DarkModeSectionView mDarkModeSectionView;
    private final DarkModeSnapshotRestorer mSnapshotRestorer;
    private final UiModeManagerWrapper mUiModeManager;
    private final ThemesUserEventLogger mThemesUserEventLogger;

    public DarkModeSectionController(
            Context context,
            Lifecycle lifecycle,
            DarkModeSnapshotRestorer snapshotRestorer,
            UiModeManagerWrapper uiModeManager,
            ThemesUserEventLogger themesUserEventLogger) {
        mContext = context;
        mLifecycle = lifecycle;
        mPowerManager = context.getSystemService(PowerManager.class);
        mLifecycle.addObserver(this);
        mSnapshotRestorer = snapshotRestorer;
        mUiModeManager = uiModeManager;
        mThemesUserEventLogger = themesUserEventLogger;
    }

@@ -134,13 +137,12 @@ public class DarkModeSectionController implements
            disableToast.show();
            return;
        }
        UiModeManager uiModeManager = context.getSystemService(UiModeManager.class);
        int shortDelay = context.getResources().getInteger(android.R.integer.config_shortAnimTime);
        new Handler(Looper.getMainLooper()).postDelayed(
                () -> {
                    mDarkModeSectionView.announceForAccessibility(
                            context.getString(R.string.mode_changed));
                    uiModeManager.setNightModeActivated(viewActivated);
                    mUiModeManager.setNightModeActivated(viewActivated);
                    mThemesUserEventLogger.logDarkThemeApplied(viewActivated);
                    mSnapshotRestorer.store(viewActivated);
                },
+2 −2
Original line number Diff line number Diff line
@@ -17,13 +17,13 @@

package com.android.customization.model.mode

import android.app.UiModeManager
import android.content.Context
import android.content.res.Configuration
import androidx.annotation.VisibleForTesting
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore
import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot
import com.android.wallpaper.system.UiModeManagerWrapper
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext

@@ -37,7 +37,7 @@ class DarkModeSnapshotRestorer : SnapshotRestorer {

    constructor(
        context: Context,
        manager: UiModeManager,
        manager: UiModeManagerWrapper,
        backgroundDispatcher: CoroutineDispatcher,
    ) : this(
        backgroundDispatcher = backgroundDispatcher,
+10 −16
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@
package com.android.customization.module

import android.app.Activity
import android.app.UiModeManager
import android.app.WallpaperColors
import android.app.WallpaperManager
import android.content.Context
@@ -144,7 +143,7 @@ constructor(
                        wallpaperColorsRepository = getWallpaperColorsRepository(),
                    ),
                    getKeyguardQuickAffordancePickerViewModelFactory(appContext),
                    getColorContrastSectionViewModelFactory(appContext),
                    getColorContrastSectionViewModelFactory(),
                    getNotificationSectionViewModelFactory(appContext),
                    getFlags(),
                    getClockCarouselViewModelFactory(
@@ -236,27 +235,22 @@ constructor(
                .also { wallpaperInteractor = it }
    }

    private fun getColorContrastSectionInteractorImpl(
        context: Context
    ): ColorContrastSectionInteractor {
    private fun getColorContrastSectionInteractorImpl(): ColorContrastSectionInteractor {
        return ColorContrastSectionInteractor(
            ColorContrastSectionRepository(context, bgDispatcher),
            ColorContrastSectionRepository(uiModeManager, bgDispatcher),
        )
    }

    fun getColorContrastSectionInteractor(context: Context): ColorContrastSectionInteractor {
    fun getColorContrastSectionInteractor(): ColorContrastSectionInteractor {
        return colorContrastSectionInteractor
            ?: getColorContrastSectionInteractorImpl(context).also {
                colorContrastSectionInteractor = it
            }
            ?: getColorContrastSectionInteractorImpl().also { colorContrastSectionInteractor = it }
    }

    fun getColorContrastSectionViewModelFactory(
        context: Context
    ): ColorContrastSectionViewModel.Factory {
    fun getColorContrastSectionViewModelFactory(): ColorContrastSectionViewModel.Factory {
        return colorContrastSectionViewModelFactory
            ?: ColorContrastSectionViewModel.Factory(getColorContrastSectionInteractor(context))
                .also { colorContrastSectionViewModelFactory = it }
            ?: ColorContrastSectionViewModel.Factory(getColorContrastSectionInteractor()).also {
                colorContrastSectionViewModelFactory = it
            }
    }

    override fun getKeyguardQuickAffordancePickerInteractor(
@@ -495,7 +489,7 @@ constructor(
        return darkModeSnapshotRestorer
            ?: DarkModeSnapshotRestorer(
                    context = appContext,
                    manager = appContext.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager,
                    manager = uiModeManager,
                    backgroundDispatcher = bgDispatcher,
                )
                .also { darkModeSnapshotRestorer = it }
+1 −0
Original line number Diff line number Diff line
@@ -204,6 +204,7 @@ class ColorPickerFragment : AppbarFragment() {
                    context,
                    lifecycle,
                    injector.getDarkModeSnapshotRestorer(requireContext()),
                    injector.uiModeManager,
                    injector.getUserEventLogger(),
                )
                .createView(requireContext())
+7 −10
Original line number Diff line number Diff line
@@ -17,8 +17,7 @@
package com.android.customization.picker.settings.data.repository

import android.app.UiModeManager
import android.content.Context
import android.content.Context.UI_MODE_SERVICE
import com.android.wallpaper.system.UiModeManagerWrapper
import java.util.concurrent.Executor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asExecutor
@@ -26,13 +25,11 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow

open class ColorContrastSectionRepository(
    private val context: Context,
class ColorContrastSectionRepository(
    uiModeManager: UiModeManagerWrapper,
    private val bgDispatcher: CoroutineDispatcher
) {
    var uiModeManager =
        context.applicationContext.getSystemService(UI_MODE_SERVICE) as UiModeManager?
    open var contrast: Flow<Float> = callbackFlow {
    var contrast: Flow<Float> = callbackFlow {
        val executor: Executor = bgDispatcher.asExecutor()
        val listener =
            UiModeManager.ContrastChangeListener { contrast ->
@@ -41,13 +38,13 @@ open class ColorContrastSectionRepository(
            }

        // Emit the current contrast value immediately
        uiModeManager?.contrast?.let { currentContrast -> trySend(currentContrast) }
        uiModeManager.getContrast()?.let { currentContrast -> trySend(currentContrast) }

        uiModeManager?.addContrastChangeListener(executor, listener)
        uiModeManager.addContrastChangeListener(executor, listener)

        awaitClose {
            // Unregister the listener when the flow collection is cancelled or no longer in use
            uiModeManager?.removeContrastChangeListener(listener)
            uiModeManager.removeContrastChangeListener(listener)
        }
    }
}
Loading