BatteryMeterDrawableBase.java revision d866b8aa0341e960399c425b021b4a6aedfa5b9e
1/* 2 * Copyright (C) 2017 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 17package com.android.settingslib.graph; 18 19import android.animation.ArgbEvaluator; 20import android.annotation.Nullable; 21import android.content.Context; 22import android.content.res.Resources; 23import android.content.res.TypedArray; 24import android.graphics.Canvas; 25import android.graphics.Color; 26import android.graphics.ColorFilter; 27import android.graphics.Paint; 28import android.graphics.Path; 29import android.graphics.RectF; 30import android.graphics.Typeface; 31import android.graphics.drawable.Drawable; 32import android.util.TypedValue; 33 34import com.android.settingslib.R; 35import com.android.settingslib.Utils; 36 37public class BatteryMeterDrawableBase extends Drawable { 38 39 private static final float ASPECT_RATIO = 9.5f / 14.5f; 40 public static final String TAG = BatteryMeterDrawableBase.class.getSimpleName(); 41 42 protected final Context mContext; 43 44 private int mLevel = -1; 45 private boolean mPluggedIn; 46 private boolean mPowerSaveEnabled; 47 private boolean mShowPercent; 48 49 private static final boolean SINGLE_DIGIT_PERCENT = false; 50 51 private static final int FULL = 96; 52 53 private static final float BOLT_LEVEL_THRESHOLD = 0.3f; // opaque bolt below this fraction 54 55 private final int[] mColors; 56 private final int mIntrinsicWidth; 57 private final int mIntrinsicHeight; 58 59 private float mButtonHeightFraction; 60 private float mSubpixelSmoothingLeft; 61 private float mSubpixelSmoothingRight; 62 private final Paint mFramePaint, mBatteryPaint, mWarningTextPaint, mTextPaint, mBoltPaint, 63 mPlusPaint; 64 private float mTextHeight, mWarningTextHeight; 65 private int mIconTint = Color.WHITE; 66 private float mOldDarkIntensity = -1f; 67 68 private int mHeight; 69 private int mWidth; 70 private String mWarningString; 71 private final int mCriticalLevel; 72 private int mChargeColor; 73 private final float[] mBoltPoints; 74 private final Path mBoltPath = new Path(); 75 private final float[] mPlusPoints; 76 private final Path mPlusPath = new Path(); 77 78 private final RectF mFrame = new RectF(); 79 private final RectF mButtonFrame = new RectF(); 80 private final RectF mBoltFrame = new RectF(); 81 private final RectF mPlusFrame = new RectF(); 82 83 private final Path mShapePath = new Path(); 84 private final Path mClipPath = new Path(); 85 private final Path mTextPath = new Path(); 86 87 private int mDarkModeBackgroundColor; 88 private int mDarkModeFillColor; 89 90 private int mLightModeBackgroundColor; 91 private int mLightModeFillColor; 92 93 public BatteryMeterDrawableBase(Context context, int frameColor) { 94 mContext = context; 95 final Resources res = context.getResources(); 96 TypedArray levels = res.obtainTypedArray(R.array.batterymeter_color_levels); 97 TypedArray colors = res.obtainTypedArray(R.array.batterymeter_color_values); 98 99 final int N = levels.length(); 100 mColors = new int[2 * N]; 101 for (int i=0; i < N; i++) { 102 mColors[2 * i] = levels.getInt(i, 0); 103 if (colors.getType(i) == TypedValue.TYPE_ATTRIBUTE) { 104 mColors[2 * i + 1] = Utils.getColorAttr(context, colors.getThemeAttributeId(i, 0)); 105 } else { 106 mColors[2 * i + 1] = colors.getColor(i, 0); 107 } 108 } 109 levels.recycle(); 110 colors.recycle(); 111 112 mWarningString = context.getString(R.string.battery_meter_very_low_overlay_symbol); 113 mCriticalLevel = mContext.getResources().getInteger( 114 com.android.internal.R.integer.config_criticalBatteryWarningLevel); 115 mButtonHeightFraction = context.getResources().getFraction( 116 R.fraction.battery_button_height_fraction, 1, 1); 117 mSubpixelSmoothingLeft = context.getResources().getFraction( 118 R.fraction.battery_subpixel_smoothing_left, 1, 1); 119 mSubpixelSmoothingRight = context.getResources().getFraction( 120 R.fraction.battery_subpixel_smoothing_right, 1, 1); 121 122 mFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 123 mFramePaint.setColor(frameColor); 124 mFramePaint.setDither(true); 125 mFramePaint.setStrokeWidth(0); 126 mFramePaint.setStyle(Paint.Style.FILL_AND_STROKE); 127 128 mBatteryPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 129 mBatteryPaint.setDither(true); 130 mBatteryPaint.setStrokeWidth(0); 131 mBatteryPaint.setStyle(Paint.Style.FILL_AND_STROKE); 132 133 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 134 Typeface font = Typeface.create("sans-serif-condensed", Typeface.BOLD); 135 mTextPaint.setTypeface(font); 136 mTextPaint.setTextAlign(Paint.Align.CENTER); 137 138 mWarningTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 139 font = Typeface.create("sans-serif", Typeface.BOLD); 140 mWarningTextPaint.setTypeface(font); 141 mWarningTextPaint.setTextAlign(Paint.Align.CENTER); 142 if (mColors.length > 1) { 143 mWarningTextPaint.setColor(mColors[1]); 144 } 145 146 mChargeColor = Utils.getDefaultColor(mContext, R.color.batterymeter_charge_color); 147 148 mBoltPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 149 mBoltPaint.setColor(Utils.getDefaultColor(mContext, R.color.batterymeter_bolt_color)); 150 mBoltPoints = loadPoints(res, R.array.batterymeter_bolt_points); 151 152 mPlusPaint = new Paint(mBoltPaint); 153 mPlusPoints = loadPoints(res, R.array.batterymeter_plus_points); 154 155 mDarkModeBackgroundColor = 156 Utils.getDefaultColor(mContext, R.color.dark_mode_icon_color_dual_tone_background); 157 mDarkModeFillColor = 158 Utils.getDefaultColor(mContext, R.color.dark_mode_icon_color_dual_tone_fill); 159 mLightModeBackgroundColor = 160 Utils.getDefaultColor(mContext, R.color.light_mode_icon_color_dual_tone_background); 161 mLightModeFillColor = 162 Utils.getDefaultColor(mContext, R.color.light_mode_icon_color_dual_tone_fill); 163 164 mIntrinsicWidth = context.getResources().getDimensionPixelSize(R.dimen.battery_width); 165 mIntrinsicHeight = context.getResources().getDimensionPixelSize(R.dimen.battery_height); 166 } 167 168 @Override 169 public int getIntrinsicHeight() { 170 return mIntrinsicHeight; 171 } 172 173 @Override 174 public int getIntrinsicWidth() { 175 return mIntrinsicWidth; 176 } 177 178 public void setShowPercent(boolean show) { 179 mShowPercent = show; 180 postInvalidate(); 181 } 182 183 public void setPluggedIn(boolean val) { 184 mPluggedIn = val; 185 postInvalidate(); 186 } 187 188 public void setBatteryLevel(int val) { 189 mLevel = val; 190 postInvalidate(); 191 } 192 193 public void setPowerSave(boolean val) { 194 mPowerSaveEnabled = val; 195 postInvalidate(); 196 } 197 198 // an approximation of View.postInvalidate() 199 protected void postInvalidate() { 200 unscheduleSelf(this::invalidateSelf); 201 scheduleSelf(this::invalidateSelf, 0); 202 } 203 204 private static float[] loadPoints(Resources res, int pointArrayRes) { 205 final int[] pts = res.getIntArray(pointArrayRes); 206 int maxX = 0, maxY = 0; 207 for (int i = 0; i < pts.length; i += 2) { 208 maxX = Math.max(maxX, pts[i]); 209 maxY = Math.max(maxY, pts[i + 1]); 210 } 211 final float[] ptsF = new float[pts.length]; 212 for (int i = 0; i < pts.length; i += 2) { 213 ptsF[i] = (float) pts[i] / maxX; 214 ptsF[i + 1] = (float) pts[i + 1] / maxY; 215 } 216 return ptsF; 217 } 218 219 @Override 220 public void setBounds(int left, int top, int right, int bottom) { 221 super.setBounds(left, top, right, bottom); 222 mHeight = bottom - top; 223 mWidth = right - left; 224 mWarningTextPaint.setTextSize(mHeight * 0.75f); 225 mWarningTextHeight = -mWarningTextPaint.getFontMetrics().ascent; 226 } 227 228 private int getColorForLevel(int percent) { 229 // If we are in power save mode, always use the normal color. 230 if (mPowerSaveEnabled) { 231 return mColors[mColors.length - 1]; 232 } 233 int thresh, color = 0; 234 for (int i = 0; i < mColors.length; i += 2) { 235 thresh = mColors[i]; 236 color = mColors[i + 1]; 237 if (percent <= thresh) { 238 239 // Respect tinting for "normal" level 240 if (i == mColors.length - 2) { 241 return mIconTint; 242 } else { 243 return color; 244 } 245 } 246 } 247 return color; 248 } 249 250 public void setDarkIntensity(float darkIntensity) { 251 if (darkIntensity == mOldDarkIntensity) { 252 return; 253 } 254 int backgroundColor = getBackgroundColor(darkIntensity); 255 int fillColor = getFillColor(darkIntensity); 256 setColors(fillColor, backgroundColor); 257 mOldDarkIntensity = darkIntensity; 258 } 259 260 public void setColors(int fillColor, int backgroundColor) { 261 mIconTint = fillColor; 262 mFramePaint.setColor(backgroundColor); 263 mBoltPaint.setColor(fillColor); 264 mChargeColor = fillColor; 265 invalidateSelf(); 266 } 267 268 private int getBackgroundColor(float darkIntensity) { 269 return getColorForDarkIntensity( 270 darkIntensity, mLightModeBackgroundColor, mDarkModeBackgroundColor); 271 } 272 273 private int getFillColor(float darkIntensity) { 274 return getColorForDarkIntensity( 275 darkIntensity, mLightModeFillColor, mDarkModeFillColor); 276 } 277 278 private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) { 279 return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor); 280 } 281 282 @Override 283 public void draw(Canvas c) { 284 final int level = mLevel; 285 286 if (level == -1) return; 287 288 float drawFrac = (float) level / 100f; 289 final int height = mHeight; 290 final int width = (int) (ASPECT_RATIO * mHeight); 291 int px = (mWidth - width) / 2; 292 293 final int buttonHeight = (int) (height * mButtonHeightFraction); 294 295 mFrame.set(0, 0, width, height); 296 mFrame.offset(px, 0); 297 298 // button-frame: area above the battery body 299 mButtonFrame.set( 300 mFrame.left + Math.round(width * 0.25f), 301 mFrame.top, 302 mFrame.right - Math.round(width * 0.25f), 303 mFrame.top + buttonHeight); 304 305 mButtonFrame.top += mSubpixelSmoothingLeft; 306 mButtonFrame.left += mSubpixelSmoothingLeft; 307 mButtonFrame.right -= mSubpixelSmoothingRight; 308 309 // frame: battery body area 310 mFrame.top += buttonHeight; 311 mFrame.left += mSubpixelSmoothingLeft; 312 mFrame.top += mSubpixelSmoothingLeft; 313 mFrame.right -= mSubpixelSmoothingRight; 314 mFrame.bottom -= mSubpixelSmoothingRight; 315 316 // set the battery charging color 317 mBatteryPaint.setColor(mPluggedIn ? mChargeColor : getColorForLevel(level)); 318 319 if (level >= FULL) { 320 drawFrac = 1f; 321 } else if (level <= mCriticalLevel) { 322 drawFrac = 0f; 323 } 324 325 final float levelTop = drawFrac == 1f ? mButtonFrame.top 326 : (mFrame.top + (mFrame.height() * (1f - drawFrac))); 327 328 // define the battery shape 329 mShapePath.reset(); 330 mShapePath.moveTo(mButtonFrame.left, mButtonFrame.top); 331 mShapePath.lineTo(mButtonFrame.right, mButtonFrame.top); 332 mShapePath.lineTo(mButtonFrame.right, mFrame.top); 333 mShapePath.lineTo(mFrame.right, mFrame.top); 334 mShapePath.lineTo(mFrame.right, mFrame.bottom); 335 mShapePath.lineTo(mFrame.left, mFrame.bottom); 336 mShapePath.lineTo(mFrame.left, mFrame.top); 337 mShapePath.lineTo(mButtonFrame.left, mFrame.top); 338 mShapePath.lineTo(mButtonFrame.left, mButtonFrame.top); 339 340 if (mPluggedIn) { 341 // define the bolt shape 342 final float bl = mFrame.left + mFrame.width() / 4f; 343 final float bt = mFrame.top + mFrame.height() / 6f; 344 final float br = mFrame.right - mFrame.width() / 4f; 345 final float bb = mFrame.bottom - mFrame.height() / 10f; 346 if (mBoltFrame.left != bl || mBoltFrame.top != bt 347 || mBoltFrame.right != br || mBoltFrame.bottom != bb) { 348 mBoltFrame.set(bl, bt, br, bb); 349 mBoltPath.reset(); 350 mBoltPath.moveTo( 351 mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(), 352 mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height()); 353 for (int i = 2; i < mBoltPoints.length; i += 2) { 354 mBoltPath.lineTo( 355 mBoltFrame.left + mBoltPoints[i] * mBoltFrame.width(), 356 mBoltFrame.top + mBoltPoints[i + 1] * mBoltFrame.height()); 357 } 358 mBoltPath.lineTo( 359 mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(), 360 mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height()); 361 } 362 363 float boltPct = (mBoltFrame.bottom - levelTop) / (mBoltFrame.bottom - mBoltFrame.top); 364 boltPct = Math.min(Math.max(boltPct, 0), 1); 365 if (boltPct <= BOLT_LEVEL_THRESHOLD) { 366 // draw the bolt if opaque 367 c.drawPath(mBoltPath, mBoltPaint); 368 } else { 369 // otherwise cut the bolt out of the overall shape 370 mShapePath.op(mBoltPath, Path.Op.DIFFERENCE); 371 } 372 } else if (mPowerSaveEnabled) { 373 // define the plus shape 374 final float pw = mFrame.width() * 2 / 3; 375 final float pl = mFrame.left + (mFrame.width() - pw) / 2; 376 final float pt = mFrame.top + (mFrame.height() - pw) / 2; 377 final float pr = mFrame.right - (mFrame.width() - pw) / 2; 378 final float pb = mFrame.bottom - (mFrame.height() - pw) / 2; 379 if (mPlusFrame.left != pl || mPlusFrame.top != pt 380 || mPlusFrame.right != pr || mPlusFrame.bottom != pb) { 381 mPlusFrame.set(pl, pt, pr, pb); 382 mPlusPath.reset(); 383 mPlusPath.moveTo( 384 mPlusFrame.left + mPlusPoints[0] * mPlusFrame.width(), 385 mPlusFrame.top + mPlusPoints[1] * mPlusFrame.height()); 386 for (int i = 2; i < mPlusPoints.length; i += 2) { 387 mPlusPath.lineTo( 388 mPlusFrame.left + mPlusPoints[i] * mPlusFrame.width(), 389 mPlusFrame.top + mPlusPoints[i + 1] * mPlusFrame.height()); 390 } 391 mPlusPath.lineTo( 392 mPlusFrame.left + mPlusPoints[0] * mPlusFrame.width(), 393 mPlusFrame.top + mPlusPoints[1] * mPlusFrame.height()); 394 } 395 396 float boltPct = (mPlusFrame.bottom - levelTop) / (mPlusFrame.bottom - mPlusFrame.top); 397 boltPct = Math.min(Math.max(boltPct, 0), 1); 398 if (boltPct <= BOLT_LEVEL_THRESHOLD) { 399 // draw the bolt if opaque 400 c.drawPath(mPlusPath, mPlusPaint); 401 } else { 402 // otherwise cut the bolt out of the overall shape 403 mShapePath.op(mPlusPath, Path.Op.DIFFERENCE); 404 } 405 } 406 407 // compute percentage text 408 boolean pctOpaque = false; 409 float pctX = 0, pctY = 0; 410 String pctText = null; 411 if (!mPluggedIn && !mPowerSaveEnabled && level > mCriticalLevel && mShowPercent) { 412 mTextPaint.setColor(getColorForLevel(level)); 413 mTextPaint.setTextSize(height * 414 (SINGLE_DIGIT_PERCENT ? 0.75f 415 : (mLevel == 100 ? 0.38f : 0.5f))); 416 mTextHeight = -mTextPaint.getFontMetrics().ascent; 417 pctText = String.valueOf(SINGLE_DIGIT_PERCENT ? (level / 10) : level); 418 pctX = mWidth * 0.5f; 419 pctY = (mHeight + mTextHeight) * 0.47f; 420 pctOpaque = levelTop > pctY; 421 if (!pctOpaque) { 422 mTextPath.reset(); 423 mTextPaint.getTextPath(pctText, 0, pctText.length(), pctX, pctY, mTextPath); 424 // cut the percentage text out of the overall shape 425 mShapePath.op(mTextPath, Path.Op.DIFFERENCE); 426 } 427 } 428 429 // draw the battery shape background 430 c.drawPath(mShapePath, mFramePaint); 431 432 // draw the battery shape, clipped to charging level 433 mFrame.top = levelTop; 434 mClipPath.reset(); 435 mClipPath.addRect(mFrame, Path.Direction.CCW); 436 mShapePath.op(mClipPath, Path.Op.INTERSECT); 437 c.drawPath(mShapePath, mBatteryPaint); 438 439 if (!mPluggedIn && !mPowerSaveEnabled) { 440 if (level <= mCriticalLevel) { 441 // draw the warning text 442 final float x = mWidth * 0.5f; 443 final float y = (mHeight + mWarningTextHeight) * 0.48f; 444 c.drawText(mWarningString, x, y, mWarningTextPaint); 445 } else if (pctOpaque) { 446 // draw the percentage text 447 c.drawText(pctText, pctX, pctY, mTextPaint); 448 } 449 } 450 } 451 452 // Some stuff required by Drawable. 453 @Override 454 public void setAlpha(int alpha) { 455 } 456 457 @Override 458 public void setColorFilter(@Nullable ColorFilter colorFilter) { 459 mFramePaint.setColorFilter(colorFilter); 460 mBatteryPaint.setColorFilter(colorFilter); 461 mWarningTextPaint.setColorFilter(colorFilter); 462 mBoltPaint.setColorFilter(colorFilter); 463 mPlusPaint.setColorFilter(colorFilter); 464 } 465 466 @Override 467 public int getOpacity() { 468 return 0; 469 } 470} 471