1 /* 2 * Copyright (C) 2025 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.battery.ui.binder 18 19 import androidx.compose.foundation.layout.height 20 import androidx.compose.foundation.layout.width 21 import androidx.compose.runtime.getValue 22 import androidx.compose.ui.Modifier 23 import androidx.compose.ui.platform.ComposeView 24 import androidx.compose.ui.platform.ViewCompositionStrategy 25 import androidx.core.view.isVisible 26 import androidx.lifecycle.Lifecycle 27 import androidx.lifecycle.compose.collectAsStateWithLifecycle 28 import androidx.lifecycle.repeatOnLifecycle 29 import com.android.systemui.lifecycle.repeatWhenAttached 30 import com.android.systemui.statusbar.phone.domain.interactor.IsAreaDark 31 import com.android.systemui.statusbar.pipeline.battery.ui.composable.UnifiedBattery 32 import com.android.systemui.statusbar.pipeline.battery.ui.viewmodel.BatteryViewModel 33 import com.android.systemui.statusbar.pipeline.battery.ui.viewmodel.BatteryViewModel.Companion.STATUS_BAR_BATTERY_HEIGHT 34 import com.android.systemui.statusbar.pipeline.battery.ui.viewmodel.BatteryViewModel.Companion.STATUS_BAR_BATTERY_WIDTH 35 import kotlinx.coroutines.flow.Flow 36 37 /** In cases where the battery needs to be bound to an existing android view */ 38 object UnifiedBatteryViewBinder { 39 /** Seats the [UnifiedBattery] into the given [ComposeView] root. */ 40 @JvmStatic bindnull41 fun bind( 42 view: ComposeView, 43 viewModelFactory: BatteryViewModel.Factory, 44 isAreaDark: Flow<IsAreaDark>, 45 ) { 46 view.repeatWhenAttached { 47 repeatOnLifecycle(Lifecycle.State.STARTED) { 48 view.apply { 49 isVisible = true 50 setViewCompositionStrategy( 51 ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed 52 ) 53 setContent { 54 val isDark by isAreaDark.collectAsStateWithLifecycle(IsAreaDark { true }) 55 UnifiedBattery( 56 modifier = 57 Modifier.height(STATUS_BAR_BATTERY_HEIGHT) 58 .width(STATUS_BAR_BATTERY_WIDTH), 59 viewModelFactory = viewModelFactory, 60 isDark = isDark, 61 ) 62 } 63 } 64 } 65 } 66 } 67 } 68