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.Utils
26 import com.android.systemui.R
27 import com.android.systemui.battery.BatteryMeterView
28 import com.android.systemui.statusbar.events.BackgroundAnimatableView
29 
30 class BatteryStatusChip @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
31     FrameLayout(context, attrs), BackgroundAnimatableView {
32 
33     private val roundedContainer: LinearLayout
34     private val batteryMeterView: BatteryMeterView
35     override val contentView: View
36         get() = batteryMeterView
37 
38     init {
39         inflate(context, R.layout.battery_status_chip, this)
40         roundedContainer = requireViewById(R.id.rounded_container)
41         batteryMeterView = requireViewById(R.id.battery_meter_view)
42         updateResources()
43     }
44 
45     /**
46      * When animating as a chip in the status bar, we want to animate the width for the rounded
47      * container. We have to subtract our own top and left offset because the bounds come to us as
48      * absolute on-screen bounds.
49      */
50     override fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int) {
51         roundedContainer.setLeftTopRightBottom(l - left, t - top, r - left, b - top)
52     }
53 
54     fun setBatteryLevel(@IntRange(from = 0, to = 100) batteryLevel: Int) {
55         batteryMeterView.setForceShowPercent(true)
56         batteryMeterView.onBatteryLevelChanged(batteryLevel, true)
57     }
58 
59     override fun onConfigurationChanged(newConfig: Configuration) {
60         super.onConfigurationChanged(newConfig)
61         updateResources()
62     }
63 
64     @SuppressLint("UseCompatLoadingForDrawables")
65     private fun updateResources() {
66         val primaryColor =
67             Utils.getColorAttrDefaultColor(context, com.android.internal.R.attr.colorPrimary)
68         val textColorSecondary =
69             Utils.getColorAttrDefaultColor(mContext, android.R.attr.textColorSecondary)
70         batteryMeterView.updateColors(primaryColor, textColorSecondary, primaryColor)
71         roundedContainer.background = mContext.getDrawable(R.drawable.statusbar_chip_bg)
72     }
73 }
74