Android Open Source - E34Clock Battery Info






From Project

Back to project page E34Clock.

License

The source code is released under:

Apache License

If you think the Android project E34Clock 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

/*
 * (c) Copyright 2013 Stefan Langer//  w  w  w .j  a va2s.  c om
 *
 *    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 de.element34.e34clock;

import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;

public class BatteryInfo {
    private static final String TAG = "e34clock.BatteryInfo";

    public String getBatteryLevel(Intent batteryStatus) {
        String retVal = "---%";
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        if (scale > 0) {
            int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

            int extra = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
            Log.v(TAG, "Retrieved EXTRA_STATUS " + extra);
            switch (extra) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    retVal = String.valueOf((int) Math.ceil(level / (float) scale * 100)) + "%";
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    retVal = "Full";
                    break;
                default:
                    Log.w(TAG, String.format("Unable to determine level of battery scale %d, level %d!", scale, level));
            }
        }
        Log.v(TAG, "Battery status " + retVal + " determined!");
        return retVal;
    }

    public int getBatteryImg(Intent batteryStatus) {
        int extra = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        Log.v(TAG, "Retrieved EXTRA_PLUGGED " + extra);
        switch (extra) {
            case BatteryManager.BATTERY_PLUGGED_AC:
            case BatteryManager.BATTERY_PLUGGED_WIRELESS:
            case BatteryManager.BATTERY_PLUGGED_USB:
                Log.v(TAG, "Using plugged battery image!");
                return R.drawable.battery_load;
            default:
                Log.v(TAG, "Using battery image!");
                return R.drawable.battery;
        }
    }
}




Java Source Code List

de.element34.e34clock.AlarmInfo.java
de.element34.e34clock.BatteryInfo.java
de.element34.e34clock.ClockService.java
de.element34.e34clock.ClockSettingsActivity.java
de.element34.e34clock.ClockUtil.java
de.element34.e34clock.ClockWidget.java
de.element34.e34clock.Constants.java
de.element34.e34clock.FullClockWidget.java
de.element34.e34clock.SoloClockWidget.java
de.element34.e34clock.SoloDateWidget.java