• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.battery
16 
17 import com.android.settingslib.graph.ThemedBatteryDrawable
18 
19 /** An object storing specs related to the battery icon in the status bar. */
20 object BatterySpecs {
21 
22     /** Width of the main battery icon, not including the shield. */
23     const val BATTERY_WIDTH = ThemedBatteryDrawable.WIDTH
24     /** Height of the main battery icon, not including the shield. */
25     const val BATTERY_HEIGHT = ThemedBatteryDrawable.HEIGHT
26 
27     private const val SHIELD_WIDTH = 10f
28     private const val SHIELD_HEIGHT = 13f
29 
30     /**
31      * Amount that the left side of the shield should be offset from the left side of the battery.
32      */
33     const val SHIELD_LEFT_OFFSET = 8f
34     /** Amount that the top of the shield should be offset from the top of the battery. */
35     const val SHIELD_TOP_OFFSET = 10f
36 
37     const val SHIELD_STROKE = 4f
38 
39     /** The full width of the battery icon, including the main battery icon *and* the shield. */
40     const val BATTERY_WIDTH_WITH_SHIELD = SHIELD_LEFT_OFFSET + SHIELD_WIDTH
41     /** The full height of the battery icon, including the main battery icon *and* the shield. */
42     const val BATTERY_HEIGHT_WITH_SHIELD = SHIELD_TOP_OFFSET + SHIELD_HEIGHT
43 
44     /**
45      * Given the desired height of the main battery icon in pixels, returns the height that the full
46      * battery icon will take up in pixels.
47      *
48      * If there's no shield, this will just return [mainBatteryHeight]. Otherwise, the shield
49      * extends slightly below the bottom of the main battery icon so we need some extra height.
50      */
51     @JvmStatic
getFullBatteryHeightnull52     fun getFullBatteryHeight(mainBatteryHeight: Float, displayShield: Boolean): Float {
53         return if (!displayShield) {
54             mainBatteryHeight
55         } else {
56             val verticalScaleFactor = mainBatteryHeight / BATTERY_HEIGHT
57             verticalScaleFactor * BATTERY_HEIGHT_WITH_SHIELD
58         }
59     }
60 
61     /**
62      * Given the desired width of the main battery icon in pixels, returns the width that the full
63      * battery icon will take up in pixels.
64      *
65      * If there's no shield, this will just return [mainBatteryWidth]. Otherwise, the shield extends
66      * past the right side of the main battery icon so we need some extra width.
67      */
68     @JvmStatic
getFullBatteryWidthnull69     fun getFullBatteryWidth(mainBatteryWidth: Float, displayShield: Boolean): Float {
70         return if (!displayShield) {
71             mainBatteryWidth
72         } else {
73             val horizontalScaleFactor = mainBatteryWidth / BATTERY_WIDTH
74             horizontalScaleFactor * BATTERY_WIDTH_WITH_SHIELD
75         }
76     }
77 
78     /**
79      * Given the height of the full battery icon, return how tall the main battery icon should be.
80      *
81      * If there's no shield, this will just return [fullBatteryHeight]. Otherwise, the shield takes
82      * up some of the view's height so the main battery width will be just a portion of
83      * [fullBatteryHeight].
84      */
85     @JvmStatic
getMainBatteryHeightnull86     fun getMainBatteryHeight(fullBatteryHeight: Float, displayShield: Boolean): Float {
87         return if (!displayShield) {
88             fullBatteryHeight
89         } else {
90             return (BATTERY_HEIGHT / BATTERY_HEIGHT_WITH_SHIELD) * fullBatteryHeight
91         }
92     }
93 
94     /**
95      * Given the width of the full battery icon, return how wide the main battery icon should be.
96      *
97      * If there's no shield, this will just return [fullBatteryWidth]. Otherwise, the shield takes
98      * up some of the view's width so the main battery width will be just a portion of
99      * [fullBatteryWidth].
100      */
101     @JvmStatic
getMainBatteryWidthnull102     fun getMainBatteryWidth(fullBatteryWidth: Float, displayShield: Boolean): Float {
103         return if (!displayShield) {
104             fullBatteryWidth
105         } else {
106             return (BATTERY_WIDTH / BATTERY_WIDTH_WITH_SHIELD) * fullBatteryWidth
107         }
108     }
109 }
110