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.events.ui.view 18 19 import android.annotation.SuppressLint 20 import android.content.Context 21 import android.util.AttributeSet 22 import android.view.View 23 import android.widget.FrameLayout 24 import android.widget.LinearLayout 25 import androidx.compose.foundation.layout.height 26 import androidx.compose.foundation.layout.width 27 import androidx.compose.ui.Modifier 28 import androidx.compose.ui.platform.ComposeView 29 import com.android.systemui.res.R 30 import com.android.systemui.statusbar.core.NewStatusBarIcons 31 import com.android.systemui.statusbar.events.BackgroundAnimatableView 32 import com.android.systemui.statusbar.pipeline.battery.domain.interactor.BatteryInteractor 33 import com.android.systemui.statusbar.pipeline.battery.shared.ui.BatteryColors.LightThemeChargingColors 34 import com.android.systemui.statusbar.pipeline.battery.shared.ui.BatteryFrame 35 import com.android.systemui.statusbar.pipeline.battery.shared.ui.BatteryGlyph 36 import com.android.systemui.statusbar.pipeline.battery.ui.composable.BatteryCanvas 37 import com.android.systemui.statusbar.pipeline.battery.ui.viewmodel.BatteryViewModel 38 import com.android.systemui.statusbar.pipeline.battery.ui.viewmodel.BatteryViewModel.Companion.glyphRepresentation 39 40 /** 41 * [StatusEvent] chip for the battery plugged in status event. Shows the current battery level and 42 * charging state in the status bar via the system event animation. 43 * 44 * This chip will fully replace [BatteryStatusChip] when [NewStatusBarIcons] is rolled out 45 */ 46 @SuppressLint("ViewConstructor") 47 class BatteryStatusEventComposeChip 48 @JvmOverloads 49 constructor(level: Int, context: Context, attrs: AttributeSet? = null) : 50 FrameLayout(context, attrs), BackgroundAnimatableView { 51 private val roundedContainer: LinearLayout 52 private val composeInner: ComposeView 53 override val contentView: View 54 get() = composeInner 55 56 init { 57 NewStatusBarIcons.unsafeAssertInNewMode() 58 59 inflate(context, R.layout.status_bar_event_chip_compose, this) 60 roundedContainer = requireViewById(R.id.rounded_container) 61 composeInner = requireViewById(R.id.compose_view) <lambda>null62 composeInner.apply { 63 setContent { 64 val isFull = BatteryInteractor.isBatteryFull(level) 65 BatteryCanvas( 66 modifier = 67 Modifier.width(BatteryViewModel.STATUS_BAR_BATTERY_WIDTH) 68 .height(BatteryViewModel.STATUS_BAR_BATTERY_HEIGHT), 69 path = BatteryFrame.pathSpec, 70 // TODO(b/394659067): get a content description for this chip 71 contentDescription = "", 72 innerWidth = BatteryFrame.innerWidth, 73 innerHeight = BatteryFrame.innerHeight, 74 // This event only happens when plugged in, so we always show it as charging 75 glyphs = 76 if (isFull) listOf(BatteryGlyph.Bolt) 77 else level.glyphRepresentation() + BatteryGlyph.Bolt, 78 level = level, 79 isFull = isFull, 80 colorsProvider = { LightThemeChargingColors }, 81 ) 82 } 83 } 84 updateResources() 85 } 86 87 /** 88 * When animating as a chip in the status bar, we want to animate the width for the rounded 89 * container. We have to subtract our own top and left offset because the bounds come to us as 90 * absolute on-screen bounds. 91 */ setBoundsForAnimationnull92 override fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int) { 93 roundedContainer.setLeftTopRightBottom(l - left, t - top, r - left, b - top) 94 } 95 96 @SuppressLint("UseCompatLoadingForDrawables") updateResourcesnull97 private fun updateResources() { 98 roundedContainer.background = mContext.getDrawable(R.drawable.statusbar_chip_bg) 99 } 100 } 101