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

Commit 6a449c62 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

Adds power repository and interactor.

UserSwitcherActivity refactor: CL 3/7

Adds a repository and an interactor to expose screen on/off state to the
rest of the app. This is needed by the user switcher as it finishes the
activity if the screen turns off.

Bug: 243844359
Test: Included unit tests.
Change-Id: If428cae7be2c25639d43a56717b7e6e9f0b86bc5
parent b1205215
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -20,13 +20,18 @@ import com.android.systemui.power.EnhancedEstimates;
import com.android.systemui.power.EnhancedEstimatesImpl;
import com.android.systemui.power.PowerNotificationWarnings;
import com.android.systemui.power.PowerUI;
import com.android.systemui.power.data.repository.PowerRepositoryModule;

import dagger.Binds;
import dagger.Module;


/** Dagger Module for code in the power package. */
@Module
@Module(
        includes = {
                PowerRepositoryModule.class,
        }
)
public interface PowerModule {
    /** */
    @Binds
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.power.data.repository

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.PowerManager
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import javax.inject.Inject
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow

/** Defines interface for classes that act as source of truth for power-related data. */
interface PowerRepository {
    /** Whether the device is interactive. Starts with the current state. */
    val isInteractive: Flow<Boolean>
}

@SysUISingleton
class PowerRepositoryImpl
@Inject
constructor(
    manager: PowerManager,
    dispatcher: BroadcastDispatcher,
) : PowerRepository {

    override val isInteractive: Flow<Boolean> = conflatedCallbackFlow {
        fun send() {
            trySendWithFailureLogging(manager.isInteractive, TAG)
        }

        val receiver =
            object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    send()
                }
            }

        dispatcher.registerReceiver(
            receiver,
            IntentFilter().apply {
                addAction(Intent.ACTION_SCREEN_ON)
                addAction(Intent.ACTION_SCREEN_OFF)
            },
        )
        send()

        awaitClose { dispatcher.unregisterReceiver(receiver) }
    }

    companion object {
        private const val TAG = "PowerRepository"
    }
}
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.power.data.repository

import dagger.Binds
import dagger.Module

@Module
interface PowerRepositoryModule {
    @Binds fun bindRepository(impl: PowerRepositoryImpl): PowerRepository
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.power.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.power.data.repository.PowerRepository
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow

/** Hosts business logic for interacting with the power system. */
@SysUISingleton
class PowerInteractor
@Inject
constructor(
    repository: PowerRepository,
) {
    /** Whether the screen is on or off. */
    val isInteractive: Flow<Boolean> = repository.isInteractive
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.power.data.repository

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class FakePowerRepository(
    initialInteractive: Boolean = true,
) : PowerRepository {

    private val _isInteractive = MutableStateFlow(initialInteractive)
    override val isInteractive: Flow<Boolean> = _isInteractive.asStateFlow()

    fun setInteractive(value: Boolean) {
        _isInteractive.value = value
    }
}
Loading