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