Example usage for android.os BatteryManager BATTERY_STATUS_DISCHARGING

List of usage examples for android.os BatteryManager BATTERY_STATUS_DISCHARGING

Introduction

In this page you can find the example usage for android.os BatteryManager BATTERY_STATUS_DISCHARGING.

Prototype

int BATTERY_STATUS_DISCHARGING

To view the source code for android.os BatteryManager BATTERY_STATUS_DISCHARGING.

Click Source Link

Usage

From source file:org.wso2.emm.agent.api.DeviceState.java

/**
 * Conversion from charging status int to String can be done through this method.
 *
 * @param status integer representing the charging status.
 * @return String representing the charging status.
 */// w ww  . j  a  v  a  2 s .c o  m
private String getStatus(int status) {
    String statusString = UNKNOWN;
    switch (status) {
    case BatteryManager.BATTERY_STATUS_CHARGING:
        statusString = CHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_DISCHARGING:
        statusString = DISCHARGING;
        break;
    case BatteryManager.BATTERY_STATUS_FULL:
        statusString = FULL;
        break;
    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
        statusString = NOT_CHARGING;
        break;
    }
    return statusString;
}

From source file:com.google.code.twisty.Twisty.java

private void appendBatteryState(StringBuffer sb) {
    IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent intent = registerReceiver(null, battFilter);

    int rawlevel = intent.getIntExtra("level", -1);
    int scale = intent.getIntExtra("scale", -1);
    int status = intent.getIntExtra("status", -1);
    int health = intent.getIntExtra("health", -1);
    int level = -1; // percentage, or -1 for unknown
    if (rawlevel >= 0 && scale > 0) {
        level = (rawlevel * 100) / scale;
    }//  w w w  .  j av  a  2 s  .  c o m
    sb.append("The device");
    if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) {
        sb.append("'s battery feels very hot!");
    } else {
        switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            // old emulator; maybe also when plugged in with no battery
            sb.append(" has no battery.");
            break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
            sb.append("'s battery");
            if (level <= 33)
                sb.append(" is charging, and really ought to " + "remain that way for the time being.");
            else if (level <= 84)
                sb.append(" charges merrily.");
            else
                sb.append(" will soon be fully charged.");
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            if (level == 0)
                sb.append(" needs charging right away.");
            else if (level > 0 && level <= 33)
                sb.append(" is about ready to be recharged.");
            else
                sb.append("'s battery discharges merrily.");
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            sb.append(" is fully charged up and ready to go on " + "an adventure of some sort.");
            break;
        default:
            sb.append("'s battery is indescribable!");
            break;
        }
    }
    sb.append(" ");
}

From source file:com.rareventure.gps2.GpsTrailerService.java

@Override
public void onCreate() {
    Log.d(TAG, "GPS Service Startup");

    //android.os.Debug.waitForDebugger(); //FODO 1 IXME DISABLE!

    Nammu.init(this);

    //reset all flags
    notificationFlags = 0;//from w  w  w . j  a  va  2s .  com

    updateNotification(FLAG_GPS_ENABLED, true);

    GTG.addGTGEventListener(gtgEventListener);

    //read isCollectData first from shared prefs and if its false, quit immediately
    //we don't want to notify the user that we ran for any reason if this is false
    // (for example the db was corrupted)
    GTG.prefSet.loadAndroidPreferencesFromSharedPrefs(this);

    //co: we initially don't want to do anything if the user is unlicensed,
    // just find out how much piracy is a problem
    if (!GTG.prefs.isCollectData) //|| GTGEvent.ERROR_UNLICENSED.isOn)
    {
        turnOffNotification();
        stopSelf();
        return;
    }

    GTG.initRwtm.registerWritingThread();
    try {

        try {
            GTG.requireInitialSetup(this, true);

            GTG.requirePrefsLoaded(this);

            if (!GTG.requireNotInRestore()) {
                updateNotification(FLAG_IN_RESTORE, true);
                stopSelf();

                return;
            }

            Intent i = GTG.requireNotTrialWhenPremiumIsAvailable(this);

            if (i != null) {
                turnOffNotification();
                stopSelf();

                return;
            }

            if (!GTG.requireNotTrialExpired()) {
                updateNotification(FLAG_TRIAL_EXPIRED, true);
                stopSelf();

                return;
            }

            if (!GTG.requireSdcardPresent(this)) {
                updateNotification(FLAG_ERROR_SDCARD_NOT_MOUNTED, true);
                stopSelf();

                return;
            }

            if (!GTG.requireSystemInstalled(this)) {
                stopSelf();
                return;
            }

            if (GTG.requireDbReady() != GTG.REQUIRE_DB_READY_OK) {
                updateNotification(FLAG_ERROR_DB_PROBLEM, true);
                stopSelf();

                return;
            }

            GTG.requireEncrypt();
        } finally {
            GTG.initRwtm.unregisterWritingThread();
        }

        updateNotification(FLAG_COLLECT_ENABLED, true);

        //co because we don't want gps2data files in the root directory of sdcard. they take up too much space
        //                 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //                 File dataFile = new File(Environment.getExternalStorageDirectory(), "gps2data"+sdf.format(new Date())+".txt");
        File dataFile = null;
        gpsManager = new GpsTrailerManager(dataFile, this, TAG, this.getMainLooper());
        //         gpsManager = new GpsTrailerManager(null, this, TAG,
        //               this.getMainLooper());

        //note that there is no way to receive the current status of the battery
        //However, the battery receiver will get a notification as soon as its registered
        // to the current status of the battery
        batteryReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                float batteryLeft = ((float) intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1))
                        / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

                /* ttt_installer:remove_line */Log.d(GTG.TAG,
                        "Received battery info, batteryLeft: " + batteryLeft);

                int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);

                if (batteryLeft < GTG.prefs.minBatteryPerc
                        && (status == BatteryManager.BATTERY_STATUS_DISCHARGING
                                || status == BatteryManager.BATTERY_STATUS_UNKNOWN)) {
                    updateNotification(FLAG_BATTERY_LOW, true);
                    GTG.alert(GTGEvent.ERROR_LOW_BATTERY);
                } else
                    updateNotification(FLAG_BATTERY_LOW, false);

                //we wait until now to finish startup 
                //because otherwise if the battery is low
                //and we are awoken for some other event, we would otherwise show our icon for
                //a brief period before we get the battery is low event
                updateNotification(FLAG_FINISHED_STARTUP, true);

                //just a hack, a reasonable place to check if the trial version has expired
                checkTrialExpired();
            }
        };

        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, batteryLevelFilter);

        gpsManager.start();

    } catch (Exception e) {
        shutdownWithException(e);
        ACRA.getErrorReporter().handleException(e);
    }
}

From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java

private void monitorBatteryState() {
    battReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            StringBuilder sb = new StringBuilder();

            int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); // charge level from 0 to scale inclusive
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // Max value for charge level
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
            boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); //0 if the device is not plugged in; 1 if plugged into an AC power adapter; 2 if plugged in via USB.
            //                int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // voltage in millivolts
            nBatteryTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); // current battery temperature in tenths of a degree Centigrade
            //                String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);

            nChargeLevel = -1; // percentage, or -1 for unknown
            if (rawlevel >= 0 && scale > 0) {
                nChargeLevel = (rawlevel * 100) / scale;
            }// ww w .j  a va  2  s.  c  om

            if (plugged > 0)
                sACStatus = "ONLINE";
            else
                sACStatus = "OFFLINE";

            if (present == false)
                sb.append("NO BATTERY");
            else {
                if (nChargeLevel < 10)
                    sb.append("Critical");
                else if (nChargeLevel < 33)
                    sb.append("LOW");
                else if (nChargeLevel > 80)
                    sb.append("HIGH");
            }

            if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) {
                sb.append("Overheated ");
                sb.append((((float) (nBatteryTemp)) / 10));
                sb.append("(C)");
            } else {
                switch (status) {
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    // old emulator; maybe also when plugged in with no battery
                    if (present == true)
                        sb.append(" UNKNOWN");
                    break;
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    sb.append(" CHARGING");
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    sb.append(" DISCHARGING");
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    sb.append(" NOTCHARGING");
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    sb.append(" FULL");
                    break;
                default:
                    if (present == true)
                        sb.append("Unknown");
                    break;
                }
            }

            sPowerStatus = sb.toString();
        }
    };

    IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(battReceiver, battFilter);
}