Android Open Source - Battery-Indicator Battery Level






From Project

Back to project page Battery-Indicator.

License

The source code is released under:

GNU General Public License

If you think the Android project Battery-Indicator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
    Copyright (c) 2013 Darshan-Josiah Barber
//from w  w  w .j a v a 2  s.  c om
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

package com.darshancomputing.BatteryIndicatorPro;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;

class BatteryLevel {
    private int width, top_h, body_h, bottom_h;
    private Canvas canvas;
    private Paint fill_paint, bitmap_paint;
    private Bitmap battery_top, battery_body, battery_bottom, battery;

    public static final int SIZE_LARGE = 1;
    public static final int SIZE_NOTIFICATION = 4;

    public BatteryLevel(Context context) {
        this(context, SIZE_LARGE);
    }

    public BatteryLevel(Context context, int inSampleSize) {
        Resources res = context.getResources();
        //?context = null;

        BitmapFactory bf = new BitmapFactory();
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inDensity = DisplayMetrics.DENSITY_DEFAULT;
        bfo.inScaled = false;
        bfo.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
        bfo.inSampleSize = inSampleSize;

        battery_top    = bf.decodeResource(res, R.drawable.empty_battery_top   , bfo);
        battery_body   = bf.decodeResource(res, R.drawable.empty_battery_body  , bfo);
        battery_bottom = bf.decodeResource(res, R.drawable.empty_battery_bottom, bfo);
        //?res = null;

           width = battery_top.getWidth();
           top_h = battery_top.getHeight();
          body_h = battery_body.getHeight();
        bottom_h = battery_bottom.getHeight();

        canvas = new Canvas();

        battery = Bitmap.createBitmap(width, top_h + body_h + bottom_h, Bitmap.Config.ARGB_8888);
        battery.setDensity(DisplayMetrics.DENSITY_DEFAULT);
        canvas.setBitmap(battery);

        fill_paint = new Paint();
        fill_paint.setColor(0xaa33b5e5);
        fill_paint.setAntiAlias(true);
        fill_paint.setStrokeCap(Paint.Cap.ROUND);
        fill_paint.setStrokeJoin(Paint.Join.ROUND);
        fill_paint.setStyle(Paint.Style.FILL);
        fill_paint.setDither(true);

        bitmap_paint = new Paint();
        bitmap_paint.setAntiAlias(true);
        bitmap_paint.setDither(true);

        //setLevel(0); // TODO: Does it make sense to show an empty battery at first, in case it take a moment to get level?
    }

    public void setLevel(int level) {
        if (level < 0) level = 0; // I suspect we might get called with -1 in certain circumstances

        int rect_top = top_h + (body_h * (100 - level) / 100);
        int rect_bottom = top_h + body_h;

        RectF body_rect = new RectF(0, rect_top, width, rect_bottom);

        canvas.drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);

        canvas.drawRoundRect(body_rect, 7.5f, 7.5f, fill_paint);

        canvas.drawBitmap(battery_top   , 0, 0             , bitmap_paint);
        canvas.drawBitmap(battery_body  , 0, top_h         , bitmap_paint);
        canvas.drawBitmap(battery_bottom, 0, top_h + body_h, bitmap_paint);
    }

    public Bitmap getBitmap() {
        return battery;
    }

    public void recycle() {
        battery_top.recycle();
        battery_body.recycle();
        battery_bottom.recycle();
        battery.recycle();
    }
}




Java Source Code List

com.darshancomputing.BatteryIndicatorPro.AlarmDatabase.java
com.darshancomputing.BatteryIndicatorPro.AlarmEditActivity.java
com.darshancomputing.BatteryIndicatorPro.AlarmRingtonePreference.java
com.darshancomputing.BatteryIndicatorPro.AlarmsActivity.java
com.darshancomputing.BatteryIndicatorPro.BatteryInfoActivity.java
com.darshancomputing.BatteryIndicatorPro.BatteryInfoAppWidgetProvider.java
com.darshancomputing.BatteryIndicatorPro.BatteryInfoService.java
com.darshancomputing.BatteryIndicatorPro.BatteryInfo.java
com.darshancomputing.BatteryIndicatorPro.BatteryLevel.java
com.darshancomputing.BatteryIndicatorPro.BootCompletedReceiver.java
com.darshancomputing.BatteryIndicatorPro.CircleWidgetBackground.java
com.darshancomputing.BatteryIndicatorPro.ColorPickerDialog.java
com.darshancomputing.BatteryIndicatorPro.ColorPickerPanelView.java
com.darshancomputing.BatteryIndicatorPro.ColorPickerPreference.java
com.darshancomputing.BatteryIndicatorPro.ColorPickerView.java
com.darshancomputing.BatteryIndicatorPro.ColorPreviewPreference.java
com.darshancomputing.BatteryIndicatorPro.CurrentInfoFragment.java
com.darshancomputing.BatteryIndicatorPro.FullAppWidgetProvider.java
com.darshancomputing.BatteryIndicatorPro.HelpActivity.java
com.darshancomputing.BatteryIndicatorPro.LogDatabase.java
com.darshancomputing.BatteryIndicatorPro.LogViewFragment.java
com.darshancomputing.BatteryIndicatorPro.Logger.java
com.darshancomputing.BatteryIndicatorPro.PluginServiceConnection.java
com.darshancomputing.BatteryIndicatorPro.PredictorCore.java
com.darshancomputing.BatteryIndicatorPro.Predictor.java
com.darshancomputing.BatteryIndicatorPro.SettingsActivity.java
com.darshancomputing.BatteryIndicatorPro.SettingsHelpActivity.java
com.darshancomputing.BatteryIndicatorPro.Str.java