1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.statusbar
16 
17 import android.annotation.IntRange
18 import android.annotation.SuppressLint
19 import android.content.Context
20 import android.content.res.Configuration
21 import android.util.AttributeSet
22 import android.view.View
23 import android.widget.FrameLayout
24 import android.widget.LinearLayout
25 import com.android.settingslib.flags.Flags.newStatusBarIcons
26 import com.android.systemui.battery.BatteryMeterView
27 import com.android.systemui.battery.unified.BatteryColors
28 import com.android.systemui.res.R
29 import com.android.systemui.statusbar.events.BackgroundAnimatableView
30 
31 class BatteryStatusChip @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
32     FrameLayout(context, attrs), BackgroundAnimatableView {
33 
34     private val roundedContainer: LinearLayout
35     private val batteryMeterView: BatteryMeterView
36     override val contentView: View
37         get() = batteryMeterView
38 
39     init {
40         inflate(context, R.layout.battery_status_chip, this)
41         roundedContainer = requireViewById(R.id.rounded_container)
42         batteryMeterView = requireViewById(R.id.battery_meter_view)
43         batteryMeterView.setStaticColor(true)
44         if (newStatusBarIcons()) {
45             batteryMeterView.setUnifiedBatteryColors(BatteryColors.LightThemeColors)
46         } else {
47             val primaryColor = context.resources.getColor(android.R.color.black, context.theme)
48             batteryMeterView.updateColors(primaryColor, primaryColor, primaryColor)
49         }
50         updateResources()
51     }
52 
53     /**
54      * When animating as a chip in the status bar, we want to animate the width for the rounded
55      * container. We have to subtract our own top and left offset because the bounds come to us as
56      * absolute on-screen bounds.
57      */
setBoundsForAnimationnull58     override fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int) {
59         roundedContainer.setLeftTopRightBottom(l - left, t - top, r - left, b - top)
60     }
61 
setBatteryLevelnull62     fun setBatteryLevel(@IntRange(from = 0, to = 100) batteryLevel: Int) {
63         batteryMeterView.setForceShowPercent(true)
64         batteryMeterView.onBatteryLevelChanged(batteryLevel, true)
65     }
66 
onConfigurationChangednull67     override fun onConfigurationChanged(newConfig: Configuration) {
68         super.onConfigurationChanged(newConfig)
69         updateResources()
70     }
71 
72     @SuppressLint("UseCompatLoadingForDrawables")
updateResourcesnull73     private fun updateResources() {
74         roundedContainer.background = mContext.getDrawable(R.drawable.statusbar_chip_bg)
75     }
76 }
77