Skip to content

Commit

Permalink
Merge pull request remobile#2 from rupalpatel0008/develop
Browse files Browse the repository at this point in the history
Updated response of onBatteryStatus()
  • Loading branch information
rupalpatel0008 authored May 9, 2018
2 parents 55fed47 + 3b115c1 commit 05257b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,19 @@ 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("isCharging", isCharging);
obj.put ("isCharging", isCharging);
obj.put ("level", batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, 0)); // a new JSONObject()
} catch (JSONException e) {
LOG.e(LOG_TAG, e.getMessage(), e);
}
System.out.println(obj);
return obj;
}

Expand All @@ -149,9 +151,11 @@ private JSONObject getBatteryInfo(Intent batteryIntent) {
*/
private void updateBatteryInfo(Intent batteryIntent) {
JSONObject info = this.getBatteryInfo(batteryIntent);
LOG.v(LOG_TAG, info.toString());
WritableMap params = Arguments.createMap();
try {
this.sendJSEvent("BATTERY_STATUS_EVENT", JsonConvert.jsonToReact(info));
params.putString("level", info.getString("level"));
params.putBoolean("isCharging", info.getBoolean("isCharging"));
this.sendJSEvent("BATTERY_STATUS_EVENT", params);
} catch (JSONException e) {
LOG.e(LOG_TAG, e.getMessage(), e);
}
Expand Down
10 changes: 5 additions & 5 deletions libs/battery.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ let subscription = null;

exports.register = function (options) {
let _level = null;
let _isPlugged = null;
let _isCharging = null;
const { onBatteryStatus, onBatteryLow, onBatteryCritical } = options;
if (onBatteryStatus || onBatteryLow || onBatteryCritical) {
subscription = EventEmitter.addListener('BATTERY_STATUS_EVENT', (info) => {
subscription = EventEmitter.addListener('BATTERY_STATUS_EVENT', info => {
if (info) {
if (_level !== info.level || _isPlugged !== info.isPlugged) {
if (_level !== info.level || _isCharging !== info.isCharging) {
if (info.level === null && _level !== null) {
return; // special case where callback is called because we stopped listening to the native side.
}

// Something changed. Fire batterystatus event
onBatteryStatus && onBatteryStatus(info);

if (!info.isPlugged) { // do not fire low/critical if we are charging. issue: CB-4520
if (!info.isCharging) { // do not fire low/critical if we are charging. issue: CB-4520
// note the following are NOT exact checks, as we want to catch a transition from
// above the threshold to below. issue: CB-4519
if (_level > STATUS_CRITICAL && info.level <= STATUS_CRITICAL) {
Expand All @@ -58,7 +58,7 @@ exports.register = function (options) {
}
}
_level = info.level;
_isPlugged = info.isPlugged;
_isCharging = info.isCharging;
}
}
});
Expand Down

0 comments on commit 05257b8

Please sign in to comment.