Skip to content

Commit

Permalink
new API: addLocationModeListener()
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiulin committed Feb 14, 2019
1 parent f86c55d commit 54a1c98
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
4 changes: 4 additions & 0 deletions SystemSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export default class SystemSetting {
return await SystemSetting._addListener(true, 'location', 'EventLocationChange', callback)
}

static async addLocationModeListener(callback) {
return await SystemSetting._addListener(true, 'locationMode', 'EventLocationModeChange', callback)
}

static async addAirplaneListener(callback) {
return await SystemSetting._addListener(true, 'airplane', 'EventAirplaneChange', callback)
}
Expand Down
51 changes: 43 additions & 8 deletions android/src/main/java/com/ninty/system/setting/SystemSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class SystemSetting extends ReactContextBaseJavaModule implements Activit
private volatile BroadcastReceiver wifiBR;
private volatile BroadcastReceiver bluetoothBR;
private volatile BroadcastReceiver locationBR;
private volatile BroadcastReceiver locationModeBR;
private volatile BroadcastReceiver airplaneBR;

public SystemSetting(ReactApplicationContext reactContext) {
Expand Down Expand Up @@ -150,6 +151,28 @@ public void onReceive(Context context, Intent intent) {
}
}

private void listenLocationModeState() {
if (locationModeBR == null) {
synchronized (this) {
if (locationModeBR == null) {
locationModeBR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(LocationManager.MODE_CHANGED_ACTION)) {
mContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("EventLocationModeChange", getLocationMode());
}
}
};
IntentFilter locationFilter = new IntentFilter();
locationFilter.addAction(LocationManager.MODE_CHANGED_ACTION);

mContext.registerReceiver(locationModeBR, locationFilter);
}
}
}
}

private void listenAirplaneState() {
if (airplaneBR == null) {
synchronized (this) {
Expand Down Expand Up @@ -377,19 +400,23 @@ public void isLocationEnabled(Promise promise) {
@ReactMethod
public void getLocationMode(Promise promise) {
if (lm != null) {
int result = 0;
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
result |= 1;
}
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
result |= 1 << 1;
}
promise.resolve(result);
promise.resolve(getLocationMode());
} else {
promise.reject("-1", "get location manager fail");
}
}

private int getLocationMode() {
int result = 0;
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
result |= 1;
}
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
result |= 1 << 1;
}
return result;
}

private boolean isLocationEnable() {
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Expand Down Expand Up @@ -439,6 +466,10 @@ public void activeListener(String type, Promise promise) {
listenLocationState();
promise.resolve(null);
return;
case "locationMode":
listenLocationModeState();
promise.resolve(null);
return;
case "airplane":
listenAirplaneState();
promise.resolve(null);
Expand Down Expand Up @@ -523,6 +554,10 @@ public void onHostDestroy() {
mContext.unregisterReceiver(locationBR);
locationBR = null;
}
if (locationModeBR != null) {
mContext.unregisterReceiver(locationModeBR);
locationBR = null;
}
if (airplaneBR != null) {
mContext.unregisterReceiver(airplaneBR);
airplaneBR = null;
Expand Down

0 comments on commit 54a1c98

Please sign in to comment.