summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/view/BatteryMeterView.java
blob: 4b772fa943cc83354604ea400a20d553d04dc342 (plain)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * Copyright (C) 2013 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 org.cyanogenmod.themes.provider.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Handler;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import static org.cyanogenmod.themes.provider.util.SystemUiPreviewGenerator.SYSTEMUI_PACKAGE;

public class BatteryMeterView extends View {
    private static final String TAG = BatteryMeterView.class.getSimpleName();
    private static final boolean ENABLE_PERCENT = true;

    private static final String BATTERYMETER_COLOR_LEVELS = "batterymeter_color_levels";
    private static final String BATTERYMETER_COLOR_VALUES = "batterymeter_color_values";
    private static final String BATTERYMETER_FRAME_COLOR = "batterymeter_frame_color";
    private static final String BATTERYMETER_BOLT_COLOR = "batterymeter_bolt_color";
    private static final String BATTERYMETER_BOLT_POINTS = "batterymeter_bolt_points";
    private static final String STATUS_BAR_CLOCK_COLOR = "status_bar_clock_color";

    public static enum BatteryMeterMode {
        BATTERY_METER_GONE,
        BATTERY_METER_ICON_PORTRAIT,
        BATTERY_METER_ICON_LANDSCAPE,
        BATTERY_METER_CIRCLE,
        BATTERY_METER_TEXT
    }

    private final Runnable mInvalidate = new Runnable() {
        public void run() {
            invalidateIfVisible();
        }
    };

    private final Handler mHandler;

    protected BatteryMeterMode mMeterMode = null;
    protected boolean mShowPercent = false;
    protected boolean mAttached;

    private int mHeight;
    private int mWidth;

    final int[] mColors;
    final int[] mBoltPoints;
    final int mFrameColor;
    final int mBoltColor;
    final int mTextColor;

    private BatteryMeterDrawable mBatteryMeterDrawable;
    private final Object mLock = new Object();

    private Resources mResources;

    public BatteryMeterView(Context context, Resources res) {
        this(context, null, 0, res);
    }

    public BatteryMeterView(Context context, AttributeSet attrs, Resources res) {
        this(context, attrs, 0, res);
    }

    public BatteryMeterView(Context context, AttributeSet attrs, int defStyle, Resources res) {
        super(context, attrs, defStyle);
        mHandler = new Handler();

        TypedArray levels = res.obtainTypedArray(res.getIdentifier(BATTERYMETER_COLOR_LEVELS,
                "array", SYSTEMUI_PACKAGE));
        TypedArray colors = res.obtainTypedArray(res.getIdentifier(BATTERYMETER_COLOR_VALUES,
                "array", SYSTEMUI_PACKAGE));
        TypedArray boltPoints = res.obtainTypedArray(res.getIdentifier(BATTERYMETER_BOLT_POINTS,
                "array", SYSTEMUI_PACKAGE));

        mResources = res;

        int N = levels.length();
        mColors = new int[2*N];
        for (int i=0; i<N; i++) {
            mColors[2*i] = levels.getInt(i, 0);
            mColors[2*i+1] = colors.getColor(i, 0);
        }
        levels.recycle();
        colors.recycle();

        N = boltPoints.length();
        mBoltPoints = new int[N];
        for (int i = 0; i < N; i++) {
            mBoltPoints[i] = boltPoints.getInt(i, 0);
        }
        boltPoints.recycle();

        mShowPercent = ENABLE_PERCENT && 0 != Settings.System.getInt(
                context.getContentResolver(), "status_bar_show_battery_percent", 0);

        mMeterMode = BatteryMeterMode.BATTERY_METER_CIRCLE;
        mBatteryMeterDrawable = createBatteryMeterDrawable(mMeterMode);

        mFrameColor = res.getColor(res.getIdentifier(BATTERYMETER_FRAME_COLOR, "color",
                SYSTEMUI_PACKAGE));
        mBoltColor = res.getColor(res.getIdentifier(BATTERYMETER_BOLT_COLOR, "color",
                SYSTEMUI_PACKAGE));
        mTextColor = res.getColor(res.getIdentifier(STATUS_BAR_CLOCK_COLOR, "color",
                SYSTEMUI_PACKAGE));

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    protected BatteryMeterDrawable createBatteryMeterDrawable(BatteryMeterMode mode) {
        Resources res = mResources;
        switch (mode) {
            case BATTERY_METER_CIRCLE:
                return new CircleBatteryMeterDrawable(res);

            case BATTERY_METER_TEXT:
                return new TextBatteryMeterDrawable(res);

            case BATTERY_METER_ICON_LANDSCAPE:
                return new NormalBatteryMeterDrawable(res, true);

            default:
                return new NormalBatteryMeterDrawable(res, false);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (mMeterMode == BatteryMeterMode.BATTERY_METER_CIRCLE) {
            height += (CircleBatteryMeterDrawable.STROKE_WITH / 3);
            width = height;
        } else if (mMeterMode == BatteryMeterMode.BATTERY_METER_TEXT) {
            width = (int)((TextBatteryMeterDrawable) mBatteryMeterDrawable).calculateMeasureWidth();
            onSizeChanged(width, height, 0, 0); // Force a size changed event
        } else if (mMeterMode.compareTo(BatteryMeterMode.BATTERY_METER_ICON_LANDSCAPE) == 0) {
            width = (int)(height * 1.2f);
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mHeight = h;
        mWidth = w;
        synchronized (mLock) {
            if (mBatteryMeterDrawable != null) {
                mBatteryMeterDrawable.onSizeChanged(w, h, oldw, oldh);
            }
        }
    }

    protected void invalidateIfVisible() {
        if (getVisibility() == View.VISIBLE && mAttached) {
            if (mAttached) {
                postInvalidate();
            } else {
                invalidate();
            }
        }
    }

    public int getColorForLevel(int percent) {
        int thresh, color = 0;
        for (int i=0; i<mColors.length; i+=2) {
            thresh = mColors[i];
            color = mColors[i+1];
            if (percent <= thresh) return color;
        }
        return color;
    }

    public void setShowPercent(boolean show) {
        if (ENABLE_PERCENT) {
            mShowPercent = show;
            invalidateIfVisible();
        }
    }

    public void setMode(BatteryMeterMode mode) {
        if (mMeterMode == mode) {
            return;
        }

        mMeterMode = mode;
        if (mode == BatteryMeterMode.BATTERY_METER_GONE) {
            setVisibility(View.GONE);
            synchronized (mLock) {
                mBatteryMeterDrawable = null;
            }
        } else {
            synchronized (mLock) {
                if (mBatteryMeterDrawable != null) {
                    mBatteryMeterDrawable.onDispose();
                }
                mBatteryMeterDrawable = createBatteryMeterDrawable(mode);
            }
            if (mMeterMode == BatteryMeterMode.BATTERY_METER_ICON_PORTRAIT ||
                    mMeterMode == BatteryMeterMode.BATTERY_METER_ICON_LANDSCAPE) {
                ((NormalBatteryMeterDrawable)mBatteryMeterDrawable).loadBoltPoints(
                        mContext.getResources());
            }
            setVisibility(View.VISIBLE);
            postInvalidate();
            requestLayout();
        }
    }

    @Override
    public void draw(Canvas c) {
        synchronized (mLock) {
            if (mBatteryMeterDrawable != null) {
                mBatteryMeterDrawable.onDraw(c);
            }
        }
    }

    protected interface BatteryMeterDrawable {
        void onDraw(Canvas c);
        void onSizeChanged(int w, int h, int oldw, int oldh);
        void onDispose();
    }

    protected class NormalBatteryMeterDrawable implements BatteryMeterDrawable {

        public static final boolean SINGLE_DIGIT_PERCENT = false;

        public static final int FULL = 96;
        public static final int EMPTY = 4;

        public static final float SUBPIXEL = 0.4f;  // inset rects for softer edges

        private boolean mDisposed;

        protected final boolean mHorizontal;

        private Paint mFramePaint, mBatteryPaint, mWarningTextPaint, mTextPaint, mBoltPaint;
        private int mButtonHeight;
        private float mTextHeight;

        private final float[] mBoltPoints;
        private final Path mBoltPath = new Path();

        private final RectF mFrame = new RectF();
        private final RectF mButtonFrame = new RectF();
        private final RectF mClipFrame = new RectF();
        private final RectF mBoltFrame = new RectF();

        public NormalBatteryMeterDrawable(Resources res, boolean horizontal) {
            super();
            mHorizontal = horizontal;
            mDisposed = false;

            mFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mFramePaint.setColor(mFrameColor);
            mFramePaint.setDither(true);
            mFramePaint.setStrokeWidth(0);
            mFramePaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mFramePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));

            mBatteryPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mBatteryPaint.setDither(true);
            mBatteryPaint.setStrokeWidth(0);
            mBatteryPaint.setStyle(Paint.Style.FILL_AND_STROKE);

            mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mTextPaint.setColor(mBoltColor);
            Typeface font = Typeface.create("sans-serif-condensed", Typeface.BOLD);
            mTextPaint.setTypeface(font);
            mTextPaint.setTextAlign(Paint.Align.CENTER);

            mWarningTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mWarningTextPaint.setColor(mColors[1]);
            font = Typeface.create("sans-serif", Typeface.BOLD);
            mWarningTextPaint.setTypeface(font);
            mWarningTextPaint.setTextAlign(Paint.Align.CENTER);

            mBoltPaint = new Paint();
            mBoltPaint.setAntiAlias(true);
            mBoltPaint.setColor(mFrameColor);
            mBoltPoints = loadBoltPoints(res);
        }

        @Override
        public void onDraw(Canvas c) {
            if (mDisposed) return;

            final int level = 50;

            float drawFrac = (float) level / 100f;
            final int pt = getPaddingTop() + (mHorizontal ? (int)(mHeight * 0.20f) : 0);
            final int pl = getPaddingLeft();
            final int pr = getPaddingRight();
            final int pb = getPaddingBottom();
            int height = mHeight - pt - pb;
            int width = mWidth - pl - pr;

            mButtonHeight = (int) ((mHorizontal ? width : height) * 0.12f);

            mFrame.set(0, 0, width, height);
            mFrame.offset(pl, pt);

            if (mHorizontal) {
                mButtonFrame.set(
                        /*cover frame border of intersecting area*/
                        width - (mButtonHeight + 5) - mFrame.left,
                        mFrame.top + height * 0.25f,
                        mFrame.right,
                        mFrame.bottom - height * 0.25f);

                mButtonFrame.top += SUBPIXEL;
                mButtonFrame.bottom -= SUBPIXEL;
                mButtonFrame.right -= SUBPIXEL;
            } else {
                mButtonFrame.set(
                        mFrame.left + width * 0.25f,
                        mFrame.top,
                        mFrame.right - width * 0.25f,
                        mFrame.top + mButtonHeight + 5 /*cover frame border of intersecting area*/);

                mButtonFrame.top += SUBPIXEL;
                mButtonFrame.left += SUBPIXEL;
                mButtonFrame.right -= SUBPIXEL;
            }

            if (mHorizontal) {
                mFrame.right -= mButtonHeight;
            } else {
                mFrame.top += mButtonHeight;
            }
            mFrame.left += SUBPIXEL;
            mFrame.top += SUBPIXEL;
            mFrame.right -= SUBPIXEL;
            mFrame.bottom -= SUBPIXEL;

            // first, draw the battery shape
            c.drawRect(mFrame, mFramePaint);

            // fill 'er up
            final int color = getColorForLevel(level);
            mBatteryPaint.setColor(color);

            if (level >= FULL) {
                drawFrac = 1f;
            } else if (level <= EMPTY) {
                drawFrac = 0f;
            }

            c.drawRect(mButtonFrame, drawFrac == 1f ? mBatteryPaint : mFramePaint);

            mClipFrame.set(mFrame);
            if (mHorizontal) {
                mClipFrame.right -= (mFrame.width() * (1f - drawFrac));
            } else {
                mClipFrame.top += (mFrame.height() * (1f - drawFrac));
            }

            c.save(Canvas.CLIP_SAVE_FLAG);
            c.clipRect(mClipFrame);
            c.drawRect(mFrame, mBatteryPaint);
            c.restore();

                // draw the bolt
                final float bl = (int)(mFrame.left + mFrame.width() / (mHorizontal ? 9f : 4.5f));
                final float bt = (int)(mFrame.top + mFrame.height() / (mHorizontal ? 4.5f : 6f));
                final float br = (int)(mFrame.right - mFrame.width() / (mHorizontal ? 6f : 7f));
                final float bb = (int)(mFrame.bottom - mFrame.height() / (mHorizontal ? 7f : 10f));
                if (mBoltFrame.left != bl || mBoltFrame.top != bt
                        || mBoltFrame.right != br || mBoltFrame.bottom != bb) {
                    mBoltFrame.set(bl, bt, br, bb);
                    mBoltPath.reset();
                    mBoltPath.moveTo(
                            mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
                            mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
                    for (int i = 2; i < mBoltPoints.length; i += 2) {
                        mBoltPath.lineTo(
                                mBoltFrame.left + mBoltPoints[i] * mBoltFrame.width(),
                                mBoltFrame.top + mBoltPoints[i + 1] * mBoltFrame.height());
                    }
                    mBoltPath.lineTo(
                            mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
                            mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
                }
                c.drawPath(mBoltPath, mBoltPaint);
            if (mShowPercent) {
                final float nofull = mHorizontal ? 0.75f : 0.6f;
                final float single = mHorizontal ? 0.86f : 0.75f;
                mTextPaint.setTextSize(height *
                        (SINGLE_DIGIT_PERCENT ? single
                                : nofull));
                mTextHeight = -mTextPaint.getFontMetrics().ascent;

                final String str = String.valueOf(SINGLE_DIGIT_PERCENT ? (level/10) : level);
                final float x  = mWidth * 0.5f;
                final float y = pt + (height + mTextHeight) * 0.47f;

                c.drawText(str, x, y, mTextPaint);
            }
        }

        @Override
        public void onDispose() {
            mHandler.removeCallbacks(mInvalidate);
            mDisposed = true;
        }

        @Override
        public void onSizeChanged(int w, int h, int oldw, int oldh) {
            mWarningTextPaint.setTextSize(h * 0.75f);
        }

        private float[] loadBoltPoints(Resources res) {
            final int[] pts = BatteryMeterView.this.mBoltPoints;
            int maxX = 0, maxY = 0;
            for (int i = 0; i < pts.length; i += 2) {
                maxX = Math.max(maxX, pts[i]);
                maxY = Math.max(maxY, pts[i + 1]);
            }
            final float[] ptsF = new float[pts.length];
            for (int i = 0; i < pts.length; i += 2) {
                ptsF[i] = (float)pts[i] / maxX;
                ptsF[i + 1] = (float)pts[i + 1] / maxY;
            }
            return ptsF;
        }
    }

    protected class CircleBatteryMeterDrawable implements BatteryMeterDrawable {

        public static final float STROKE_WITH = 6.5f;

        private boolean mDisposed;

        private int     mAnimOffset;
        private boolean mIsAnimating;   // stores charge-animation status to reliably
                                        //remove callbacks

        private int     mCircleSize;    // draw size of circle
        private RectF   mRectLeft;      // contains the precalculated rect used in drawArc(),
                                        // derived from mCircleSize
        private float   mTextX, mTextY; // precalculated position for drawText() to appear centered

        private Paint   mTextPaint;
        private Paint   mFrontPaint;
        private Paint   mBackPaint;
        private Paint   mBoltPaint;

        private final RectF mBoltFrame = new RectF();
        private final float[] mBoltPoints;
        private final Path mBoltPath = new Path();

        public CircleBatteryMeterDrawable(Resources res) {
            super();
            mDisposed = false;

            mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mTextPaint.setColor(mTextColor);
            Typeface font = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
            mTextPaint.setTypeface(font);
            mTextPaint.setTextAlign(Paint.Align.CENTER);

            mFrontPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mFrontPaint.setStrokeCap(Paint.Cap.BUTT);
            mFrontPaint.setDither(true);
            mFrontPaint.setStrokeWidth(0);
            mFrontPaint.setStyle(Paint.Style.STROKE);

            mBackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mBackPaint.setColor(mFrameColor);
            mBackPaint.setStrokeCap(Paint.Cap.BUTT);
            mBackPaint.setDither(true);
            mBackPaint.setStrokeWidth(0);
            mBackPaint.setStyle(Paint.Style.STROKE);
            mBackPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));

            mBoltPaint = new Paint();
            mBoltPaint.setAntiAlias(true);
            mBoltPaint.setColor(getColorForLevel(50));
            mBoltPoints = loadBoltPoints(res);
        }

        @Override
        public void onDraw(Canvas c) {
            if (mDisposed) return;

            if (mRectLeft == null) {
                initSizeBasedStuff();
            }

            drawCircle(c, mTextX, mRectLeft);
        }

        @Override
        public void onDispose() {
            mHandler.removeCallbacks(mInvalidate);
            mDisposed = true;
        }

        @Override
        public void onSizeChanged(int w, int h, int oldw, int oldh) {
            initSizeBasedStuff();
        }

        private float[] loadBoltPoints(Resources res) {
            final int[] pts = BatteryMeterView.this.mBoltPoints;
            int maxX = 0, maxY = 0;
            for (int i = 0; i < pts.length; i += 2) {
                maxX = Math.max(maxX, pts[i]);
                maxY = Math.max(maxY, pts[i + 1]);
            }
            final float[] ptsF = new float[pts.length];
            for (int i = 0; i < pts.length; i += 2) {
                ptsF[i] = (float)pts[i] / maxX;
                ptsF[i + 1] = (float)pts[i + 1] / maxY;
            }
            return ptsF;
        }

        private void drawCircle(Canvas canvas, float textX, RectF drawRect) {
            int level = 50;
            Paint paint;

            paint = mFrontPaint;
            paint.setColor(getColorForLevel(level));

            // draw thin gray ring first
            canvas.drawArc(drawRect, 270, 360, false, mBackPaint);
            // draw colored arc representing charge level
            canvas.drawArc(drawRect, 270 + mAnimOffset, 3.6f * level, false, paint);
            // if chosen by options, draw percentage text in the middle
            // always skip percentage when 100, so layout doesnt break

            // draw the bolt
            final float bl = (int)(drawRect.left + drawRect.width() / 3.2f);
            final float bt = (int)(drawRect.top + drawRect.height() / 4f);
            final float br = (int)(drawRect.right - drawRect.width() / 5.2f);
            final float bb = (int)(drawRect.bottom - drawRect.height() / 8f);
            if (mBoltFrame.left != bl || mBoltFrame.top != bt
                    || mBoltFrame.right != br || mBoltFrame.bottom != bb) {
                mBoltFrame.set(bl, bt, br, bb);
                mBoltPath.reset();
                mBoltPath.moveTo(
                        mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
                        mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
                for (int i = 2; i < mBoltPoints.length; i += 2) {
                    mBoltPath.lineTo(
                            mBoltFrame.left + mBoltPoints[i] * mBoltFrame.width(),
                            mBoltFrame.top + mBoltPoints[i + 1] * mBoltFrame.height());
                }
                mBoltPath.lineTo(
                        mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
                        mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
            }
            canvas.drawPath(mBoltPath, mBoltPaint);
        }

        /**
         * initializes all size dependent variables
         * sets stroke width and text size of all involved paints
         * YES! i think the method name is appropriate
         */
        private void initSizeBasedStuff() {
            mCircleSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
            mTextPaint.setTextSize(mCircleSize / 2f);

            float strokeWidth = mCircleSize / STROKE_WITH;
            mFrontPaint.setStrokeWidth(strokeWidth);
            mBackPaint.setStrokeWidth(strokeWidth);

            // calculate rectangle for drawArc calls
            int pLeft = getPaddingLeft();
            mRectLeft = new RectF(pLeft + strokeWidth / 2.0f, 0 + strokeWidth / 2.0f, mCircleSize
                    - strokeWidth / 2.0f + pLeft, mCircleSize - strokeWidth / 2.0f);

            // calculate Y position for text
            Rect bounds = new Rect();
            mTextPaint.getTextBounds("99", 0, "99".length(), bounds);
            mTextX = mCircleSize / 2.0f + getPaddingLeft();
            // the +1dp at end of formula balances out rounding issues.works out on all resolutions
            mTextY = mCircleSize / 2.0f + (bounds.bottom - bounds.top) / 2.0f
                    - strokeWidth / 2.0f + getResources().getDisplayMetrics().density;
        }
    }

    protected class TextBatteryMeterDrawable implements BatteryMeterDrawable {

        private static final boolean DRAW_LEVEL = false;

        public static final int FULL = 96;
        public static final int EMPTY = 4;

        private boolean mDisposed;

        private float mTextX;
        private float mTextY;

        private boolean mOldPlugged = false;
        private int mOldLevel = -1;

        private boolean mIsAnimating;
        private int mAnimOffset;

        private Paint mBackPaint;
        private Paint mFrontPaint;

        public TextBatteryMeterDrawable(Resources res) {
            super();
            mDisposed = false;
            mIsAnimating = false;

            DisplayMetrics dm = res.getDisplayMetrics();

            mBackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mBackPaint.setTextAlign(Paint.Align.RIGHT);
            mBackPaint.setColor(mFrameColor);
            mBackPaint.setTextSize(16.0f * dm.density);

            mFrontPaint = new Paint(mBackPaint);
        }

        @Override
        public void onDraw(Canvas c) {
            if (mDisposed) return;

            int level = 50;
            boolean plugged = true;

            if (mOldLevel != level || mOldPlugged != plugged) {
                mOldLevel = level;
                mOldPlugged = plugged;

                postInvalidate();
                requestLayout();
                return;
            }

            mFrontPaint.setColor(getColorForLevel(level));

            // Is plugged? Then use the animation status
            drawWithLevel(c, mAnimOffset, getLevel(level));
        }

        private void drawWithLevel(Canvas c, int level, String levelTxt) {
            Rect bounds = getBounds(level);

            // Draw the background
            c.drawText(levelTxt, mTextX, mTextY, mBackPaint);

            // Draw the foreground
            c.save();
            c.clipRect(0.0f, mTextY - ((level * bounds.height()) / 100.0f), mTextX, mTextY);
            c.drawText(levelTxt, mTextX, mTextY, mFrontPaint);
            c.restore();
        }

        private void drawWithoutLevel(Canvas c, String levelTxt) {
            // We need to draw the overlay back paint to get the proper color
            c.drawText(levelTxt, mTextX, mTextY, mBackPaint);
            c.drawText(levelTxt, mTextX, mTextY, mFrontPaint);
        }

        @Override
        public void onDispose() {
            mHandler.removeCallbacks(mInvalidate);
            mDisposed = true;
        }

        @Override
        public void onSizeChanged(int w, int h, int oldw, int oldh) {
            Rect bounds = getBounds(50);
            float onedp = mContext.getResources().getDisplayMetrics().density * 0.5f;
            float height = h - getPaddingBottom() - getPaddingTop();

            mTextX = w;
            mTextY = h - getPaddingBottom() - (height / 2 - bounds.height() /2) + onedp;
        }

        protected float calculateMeasureWidth() {
            Rect bounds = getBounds(50);
            float onedp = mContext.getResources().getDisplayMetrics().density;
            return bounds.width() + getPaddingStart() + getPaddingEnd() + onedp;
        }

        private Rect getBounds(int level) {
            Rect bounds = new Rect();
            String levelTxt = getLevel(level);
            mBackPaint.getTextBounds(levelTxt, 0, levelTxt.length(), bounds);
            return bounds;
        }

        private String getLevel(int level) {
            if (level == -1) {
                return String.format("?", level);
            }
            return String.format("%s%%", level);
        }

        private void resetChargeAnimation() {
            if (mIsAnimating) {
                mIsAnimating = false;
                mAnimOffset = 0;
                mHandler.removeCallbacks(mInvalidate);
            }
        }
    }
}