1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.android.settingslib.graph
import android.content.Context
import android.graphics.BlendMode
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.util.PathParser
import android.util.TypedValue
import com.android.settingslib.R
import com.android.settingslib.Utils
/**
* A battery meter drawable that respects paths configured in
* frameworks/base/core/res/res/values/config.xml to allow for an easily overrideable battery icon
*/
open class ThemedBatteryDrawable(private val context: Context, frameColor: Int) : Drawable() {
// Need to load:
// 1. perimeter shape
// 2. fill mask (if smaller than perimeter, this would create a fill that
// doesn't touch the walls
private val perimeterPath = Path()
private val scaledPerimeter = Path()
private val errorPerimeterPath = Path()
private val scaledErrorPerimeter = Path()
// Fill will cover the whole bounding rect of the fillMask, and be masked by the path
private val fillMask = Path()
private val scaledFill = Path()
// Based off of the mask, the fill will interpolate across this space
private val fillRect = RectF()
// Top of this rect changes based on level, 100% == fillRect
private val levelRect = RectF()
private val levelPath = Path()
// Updates the transform of the paths when our bounds change
private val scaleMatrix = Matrix()
private val padding = Rect()
// The net result of fill + perimeter paths
private val unifiedPath = Path()
// Bolt path (used while charging)
private val boltPath = Path()
private val scaledBolt = Path()
// Plus sign (used for power save mode)
private val plusPath = Path()
private val scaledPlus = Path()
private var intrinsicHeight: Int
private var intrinsicWidth: Int
// To implement hysteresis, keep track of the need to invert the interior icon of the battery
private var invertFillIcon = false
// Colors can be configured based on battery level (see res/values/arrays.xml)
private var colorLevels: IntArray
private var fillColor: Int = Color.MAGENTA
private var backgroundColor: Int = Color.MAGENTA
// updated whenever level changes
private var levelColor: Int = Color.MAGENTA
// Dual tone implies that battery level is a clipped overlay over top of the whole shape
private var dualTone = false
private var batteryLevel = 0
private val invalidateRunnable: () -> Unit = {
invalidateSelf()
}
open var criticalLevel: Int = context.resources.getInteger(
com.android.internal.R.integer.config_criticalBatteryWarningLevel)
var charging = false
set(value) {
field = value
postInvalidate()
}
var powerSaveEnabled = false
set(value) {
field = value
postInvalidate()
}
private val fillColorStrokePaint = Paint(Paint.ANTI_ALIAS_FLAG).also { p ->
p.color = frameColor
p.alpha = 255
p.isDither = true
p.strokeWidth = 5f
p.style = Paint.Style.STROKE
p.blendMode = BlendMode.SRC
p.strokeMiter = 5f
p.strokeJoin = Paint.Join.ROUND
}
private val fillColorStrokeProtection = Paint(Paint.ANTI_ALIAS_FLAG).also { p ->
p.isDither = true
p.strokeWidth = 5f
p.style = Paint.Style.STROKE
p.blendMode = BlendMode.CLEAR
p.strokeMiter = 5f
p.strokeJoin = Paint.Join.ROUND
}
private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG).also { p ->
p.color = frameColor
p.alpha = 255
p.isDither = true
p.strokeWidth = 0f
p.style = Paint.Style.FILL_AND_STROKE
}
private val errorPaint = Paint(Paint.ANTI_ALIAS_FLAG).also { p ->
p.color = Utils.getColorStateListDefaultColor(context, R.color.batterymeter_plus_color)
p.alpha = 255
p.isDither = true
p.strokeWidth = 0f
p.style = Paint.Style.FILL_AND_STROKE
p.blendMode = BlendMode.SRC
}
// Only used if dualTone is set to true
private val dualToneBackgroundFill = Paint(Paint.ANTI_ALIAS_FLAG).also { p ->
p.color = frameColor
p.alpha = 85 // ~0.3 alpha by default
p.isDither = true
p.strokeWidth = 0f
p.style = Paint.Style.FILL_AND_STROKE
}
init {
val density = context.resources.displayMetrics.density
intrinsicHeight = (Companion.HEIGHT * density).toInt()
intrinsicWidth = (Companion.WIDTH * density).toInt()
val res = context.resources
val levels = res.obtainTypedArray(R.array.batterymeter_color_levels)
val colors = res.obtainTypedArray(R.array.batterymeter_color_values)
val N = levels.length()
colorLevels = IntArray(2 * N)
for (i in 0 until N) {
colorLevels[2 * i] = levels.getInt(i, 0)
if (colors.getType(i) == TypedValue.TYPE_ATTRIBUTE) {
colorLevels[2 * i + 1] = Utils.getColorAttrDefaultColor(context,
colors.getThemeAttributeId(i, 0))
} else {
colorLevels[2 * i + 1] = colors.getColor(i, 0)
}
}
levels.recycle()
colors.recycle()
loadPaths()
}
override fun draw(c: Canvas) {
c.saveLayer(null, null)
unifiedPath.reset()
levelPath.reset()
levelRect.set(fillRect)
val fillFraction = batteryLevel / 100f
val fillTop =
if (batteryLevel >= 95)
fillRect.top
else
fillRect.top + (fillRect.height() * (1 - fillFraction))
levelRect.top = Math.floor(fillTop.toDouble()).toFloat()
levelPath.addRect(levelRect, Path.Direction.CCW)
// The perimeter should never change
unifiedPath.addPath(scaledPerimeter)
// If drawing dual tone, the level is used only to clip the whole drawable path
if (!dualTone) {
unifiedPath.op(levelPath, Path.Op.UNION)
}
fillPaint.color = levelColor
// Deal with unifiedPath clipping before it draws
if (charging) {
// Clip out the bolt shape
unifiedPath.op(scaledBolt, Path.Op.DIFFERENCE)
if (!invertFillIcon) {
c.drawPath(scaledBolt, fillPaint)
}
}
if (dualTone) {
// Dual tone means we draw the shape again, clipped to the charge level
c.drawPath(unifiedPath, dualToneBackgroundFill)
c.save()
c.clipRect(0f,
bounds.bottom - bounds.height() * fillFraction,
bounds.right.toFloat(),
bounds.bottom.toFloat())
c.drawPath(unifiedPath, fillPaint)
c.restore()
} else {
// Non dual-tone means we draw the perimeter (with the level fill), and potentially
// draw the fill again with a critical color
fillPaint.color = fillColor
c.drawPath(unifiedPath, fillPaint)
fillPaint.color = levelColor
// Show colorError below this level
if (batteryLevel <= Companion.CRITICAL_LEVEL && !charging) {
c.save()
c.clipPath(scaledFill)
c.drawPath(levelPath, fillPaint)
c.restore()
}
}
if (charging) {
c.clipOutPath(scaledBolt)
if (invertFillIcon) {
c.drawPath(scaledBolt, fillColorStrokePaint)
} else {
c.drawPath(scaledBolt, fillColorStrokeProtection)
}
} else if (powerSaveEnabled) {
// If power save is enabled draw the perimeter path with colorError
c.drawPath(scaledErrorPerimeter, errorPaint)
// And draw the plus sign on top of the fill
c.drawPath(scaledPlus, errorPaint)
}
c.restore()
}
private fun batteryColorForLevel(level: Int): Int {
return when {
charging || powerSaveEnabled -> fillColor
else -> getColorForLevel(level)
}
}
private fun getColorForLevel(level: Int): Int {
var thresh: Int
var color = 0
var i = 0
while (i < colorLevels.size) {
thresh = colorLevels[i]
color = colorLevels[i + 1]
if (level <= thresh) {
// Respect tinting for "normal" level
return if (i == colorLevels.size - 2) {
fillColor
} else {
color
}
}
i += 2
}
return color
}
/**
* Alpha is unused internally, and should be defined in the colors passed to {@link setColors}.
* Further, setting an alpha for a dual tone battery meter doesn't make sense without bounds
* defining the minimum background fill alpha. This is because fill + background must be equal
* to the net alpha passed in here.
*/
override fun setAlpha(alpha: Int) {
}
override fun setColorFilter(colorFilter: ColorFilter?) {
fillPaint.colorFilter = colorFilter
fillColorStrokePaint.colorFilter = colorFilter
dualToneBackgroundFill.colorFilter = colorFilter
}
/**
* Deprecated, but required by Drawable
*/
override fun getOpacity(): Int {
return PixelFormat.OPAQUE
}
override fun getIntrinsicHeight(): Int {
return intrinsicHeight
}
override fun getIntrinsicWidth(): Int {
return intrinsicWidth
}
/**
* Set the fill level
*/
public open fun setBatteryLevel(l: Int) {
invertFillIcon = if (l >= 67) true else if (l <= 33) false else invertFillIcon
batteryLevel = l
levelColor = batteryColorForLevel(batteryLevel)
invalidateSelf()
}
public fun getBatteryLevel(): Int {
return batteryLevel
}
override fun onBoundsChange(bounds: Rect?) {
super.onBoundsChange(bounds)
updateSize()
}
fun setPadding(left: Int, top: Int, right: Int, bottom: Int) {
padding.left = left
padding.top = top
padding.right = right
padding.bottom = bottom
updateSize()
}
fun setColors(fgColor: Int, bgColor: Int, singleToneColor: Int) {
fillColor = if (dualTone) fgColor else singleToneColor
fillPaint.color = fillColor
fillColorStrokePaint.color = fillColor
backgroundColor = bgColor
dualToneBackgroundFill.color = bgColor
// Also update the level color, since fillColor may have changed
levelColor = batteryColorForLevel(batteryLevel)
invalidateSelf()
}
private fun postInvalidate() {
unscheduleSelf(invalidateRunnable)
scheduleSelf(invalidateRunnable, 0)
}
private fun updateSize() {
val b = bounds
if (b.isEmpty) {
scaleMatrix.setScale(1f, 1f)
} else {
scaleMatrix.setScale((b.right / WIDTH), (b.bottom / HEIGHT))
}
perimeterPath.transform(scaleMatrix, scaledPerimeter)
errorPerimeterPath.transform(scaleMatrix, scaledErrorPerimeter)
fillMask.transform(scaleMatrix, scaledFill)
scaledFill.computeBounds(fillRect, true)
boltPath.transform(scaleMatrix, scaledBolt)
plusPath.transform(scaleMatrix, scaledPlus)
// It is expected that this view only ever scale by the same factor in each dimension, so
// just pick one to scale the strokeWidths
val scaledStrokeWidth =
Math.max(b.right / WIDTH * PROTECTION_STROKE_WIDTH, PROTECTION_MIN_STROKE_WIDTH)
fillColorStrokePaint.strokeWidth = scaledStrokeWidth
fillColorStrokeProtection.strokeWidth = scaledStrokeWidth
}
private fun loadPaths() {
val pathString = context.resources.getString(
com.android.internal.R.string.config_batterymeterPerimeterPath)
perimeterPath.set(PathParser.createPathFromPathData(pathString))
perimeterPath.computeBounds(RectF(), true)
val errorPathString = context.resources.getString(
com.android.internal.R.string.config_batterymeterErrorPerimeterPath)
errorPerimeterPath.set(PathParser.createPathFromPathData(errorPathString))
errorPerimeterPath.computeBounds(RectF(), true)
val fillMaskString = context.resources.getString(
com.android.internal.R.string.config_batterymeterFillMask)
fillMask.set(PathParser.createPathFromPathData(fillMaskString))
// Set the fill rect so we can calculate the fill properly
fillMask.computeBounds(fillRect, true)
val boltPathString = context.resources.getString(
com.android.internal.R.string.config_batterymeterBoltPath)
boltPath.set(PathParser.createPathFromPathData(boltPathString))
val plusPathString = context.resources.getString(
com.android.internal.R.string.config_batterymeterPowersavePath)
plusPath.set(PathParser.createPathFromPathData(plusPathString))
dualTone = context.resources.getBoolean(
com.android.internal.R.bool.config_batterymeterDualTone)
}
companion object {
private const val TAG = "ThemedBatteryDrawable"
private const val WIDTH = 12f
private const val HEIGHT = 20f
private const val CRITICAL_LEVEL = 15
// On a 12x20 grid, how wide to make the fill protection stroke.
// Scales when our size changes
private const val PROTECTION_STROKE_WIDTH = 3f
// Arbitrarily chosen for visibility at small sizes
private const val PROTECTION_MIN_STROKE_WIDTH = 6f
}
}
|