Skip to content

Commit

Permalink
Merge pull request remobile#1 from rupalpatel0008/develop
Browse files Browse the repository at this point in the history
Added isCharging
  • Loading branch information
rupalpatel0008 authored May 9, 2018
2 parents 06783df + 02ed6a3 commit 136bc8b
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import com.remobile.cordova.*;
import com.facebook.react.bridge.*;
Expand Down Expand Up @@ -126,10 +127,14 @@ private void removeBatteryListener() {
* @return a JSONObject containing the battery status information
*/
private JSONObject getBatteryInfo(Intent batteryIntent) {
// Are we charging / charged?
int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
JSONObject obj = new JSONObject();
try {
obj.put("level", batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, 0));
obj.put("isPlugged", batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_PLUGGED, -1) > 0 ? true : false);
obj.put("isCharging", isCharging);
} catch (JSONException e) {
LOG.e(LOG_TAG, e.getMessage(), e);
}
Expand All @@ -144,6 +149,7 @@ private JSONObject getBatteryInfo(Intent batteryIntent) {
*/
private void updateBatteryInfo(Intent batteryIntent) {
JSONObject info = this.getBatteryInfo(batteryIntent);
LOG.v(LOG_TAG, info.toString());
try {
this.sendJSEvent("BATTERY_STATUS_EVENT", JsonConvert.jsonToReact(info));
} catch (JSONException e) {
Expand Down

0 comments on commit 136bc8b

Please sign in to comment.