1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.statusbar.pipeline.airplane.data.repository 18 19 import android.os.Handler 20 import android.os.UserHandle 21 import android.provider.Settings.Global 22 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow 23 import com.android.systemui.dagger.SysUISingleton 24 import com.android.systemui.dagger.qualifiers.Application 25 import com.android.systemui.dagger.qualifiers.Background 26 import com.android.systemui.log.table.TableLogBuffer 27 import com.android.systemui.log.table.logDiffsForTable 28 import com.android.systemui.qs.SettingObserver 29 import com.android.systemui.statusbar.pipeline.dagger.AirplaneTableLog 30 import com.android.systemui.util.settings.GlobalSettings 31 import javax.inject.Inject 32 import kotlinx.coroutines.CoroutineScope 33 import kotlinx.coroutines.ExperimentalCoroutinesApi 34 import kotlinx.coroutines.channels.awaitClose 35 import kotlinx.coroutines.flow.SharingStarted 36 import kotlinx.coroutines.flow.StateFlow 37 import kotlinx.coroutines.flow.distinctUntilChanged 38 import kotlinx.coroutines.flow.stateIn 39 40 /** 41 * Provides data related to airplane mode. 42 * 43 * IMPORTANT: This is currently *not* used to render any airplane mode information anywhere. It is 44 * only used to help [com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.WifiViewModel] 45 * determine what parts of the wifi icon view should be shown. 46 * 47 * TODO(b/238425913): Consider migrating the status bar airplane mode icon to use this repo. 48 */ 49 interface AirplaneModeRepository { 50 /** Observable for whether the device is currently in airplane mode. */ 51 val isAirplaneMode: StateFlow<Boolean> 52 } 53 54 @SysUISingleton 55 @Suppress("EXPERIMENTAL_IS_NOT_ENABLED") 56 @OptIn(ExperimentalCoroutinesApi::class) 57 class AirplaneModeRepositoryImpl 58 @Inject 59 constructor( 60 @Background private val bgHandler: Handler, 61 private val globalSettings: GlobalSettings, 62 @AirplaneTableLog logger: TableLogBuffer, 63 @Application scope: CoroutineScope, 64 ) : AirplaneModeRepository { 65 // TODO(b/254848912): Replace this with a generic SettingObserver coroutine once we have it. 66 override val isAirplaneMode: StateFlow<Boolean> = 67 conflatedCallbackFlow { 68 val observer = 69 object : 70 SettingObserver( 71 globalSettings, 72 bgHandler, 73 Global.AIRPLANE_MODE_ON, 74 UserHandle.USER_ALL 75 ) { 76 override fun handleValueChanged(value: Int, observedChange: Boolean) { 77 trySend(value == 1) 78 } 79 } 80 81 observer.isListening = true 82 trySend(observer.value == 1) 83 awaitClose { observer.isListening = false } 84 } 85 .distinctUntilChanged() 86 .logDiffsForTable( 87 logger, 88 columnPrefix = "", 89 columnName = "isAirplaneMode", 90 initialValue = false 91 ) 92 .stateIn( 93 scope, 94 started = SharingStarted.WhileSubscribed(), 95 // When the observer starts listening, the flow will emit the current value so the 96 // initialValue here is irrelevant. 97 initialValue = false, 98 ) 99 } 100