Skip to content

Commit

Permalink
Merge pull request #11 from ajwhite/android-updates
Browse files Browse the repository at this point in the history
Android updates catching up to iOS
  • Loading branch information
atticoos committed Jan 3, 2018
2 parents 9c53a1b + d009ade commit d5dda74
Showing 1 changed file with 19 additions and 3 deletions.
Expand Up @@ -5,6 +5,7 @@
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.os.BatteryManager;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -13,9 +14,13 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
import com.facebook.react.bridge.LifecycleEventListener;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;

// @link http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
Expand Down Expand Up @@ -62,12 +67,23 @@ public void notifyBatteryStateChanged(Intent intent) {
// only emit an event if the Catalyst instance is avialable
if (getReactApplicationContext().hasActiveCatalystInstance()) {
WritableNativeMap params = getJSMap(intent);
getReactApplicationContext()
.getJSModule(RCTNativeAppEventEmitter.class)
.emit(EVENT_NAME, params);
try {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(EVENT_NAME, params);
} catch (Exception e) {
Log.e(getName(), "notifyBatteryStateChanged called before bundle loaded");
}
}
}

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("BATTERY_CHANGE_EVENT", EVENT_NAME);
return constants;
}

@ReactMethod
public void getBatteryLevel(Promise promise) {
if (batteryStatus != null) {
Expand Down

0 comments on commit d5dda74

Please sign in to comment.