Skip to content

Commit

Permalink
listener for location & airplane
Browse files Browse the repository at this point in the history
  • Loading branch information
c19354837 committed May 7, 2018
1 parent bfaa7af commit 3fe65e6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 21 deletions.
46 changes: 27 additions & 19 deletions SystemSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ export default class SystemSetting {
return false;
}

static async addBluetoothListener(callback) {
return await SystemSetting._addListener(false, 'bluetooth', 'EventBluetoothChange', callback)
}

static async addWifiListener(callback) {
return await SystemSetting._addListener(true, 'wifi', 'EventWifiChange', callback)
}

static async addLocationListener(callback) {
return await SystemSetting._addListener(true, 'location', 'EventLocationChange', callback)
}

static async addAirplaneListener(callback) {
return await SystemSetting._addListener(true, 'airplane', 'EventAirplaneChange', callback)
}

static async _addListener(androidOnly, type, eventName, callback) {
if (!androidOnly || Utils.isAndroid) {
const result = await SystemSetting._activeListener(type)
if (result) {
return eventEmitter.addListener(eventName, callback)
}
}
return null
}

static async _activeListener(name) {
try {
await SystemSettingNative.activeListener(name)
Expand All @@ -216,25 +242,7 @@ export default class SystemSetting {
return true;
}

static async addBluetoothListener(callback) {
const result = await SystemSetting._activeListener('bluetooth')
if (result) {
return eventEmitter.addListener('EventBluetoothChange', callback)
}
return null;
}

static async addWifiListener(callback) {
if(Utils.isAndroid){
const result = await SystemSetting._activeListener('wifi')
if (result) {
return eventEmitter.addListener('EventWifiChange', callback)
}
}
return null;
}

static removeListener(listener){
static removeListener(listener) {
listener && listener.remove()
}

Expand Down
73 changes: 71 additions & 2 deletions android/src/main/java/com/ninty/system/setting/SystemSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class SystemSetting extends ReactContextBaseJavaModule implements Activit
private BroadcastReceiver volumeBR;
private volatile BroadcastReceiver wifiBR;
private volatile BroadcastReceiver bluetoothBR;
private volatile BroadcastReceiver locationBR;
private volatile BroadcastReceiver airplaneBR;
private IntentFilter filter;

public SystemSetting(ReactApplicationContext reactContext) {
Expand Down Expand Up @@ -134,6 +136,53 @@ public void onReceive(Context context, Intent intent) {
}
}

private void listenLocationState() {
if (locationBR == null) {
synchronized (this) {
if (locationBR == null) {
locationBR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
mContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("EventLocationChange", isLocationEnable());
}
}
};
IntentFilter locationFilter = new IntentFilter();
locationFilter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);

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

private void listenAirplaneState() {
if (airplaneBR == null) {
synchronized (this) {
if (airplaneBR == null) {
airplaneBR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
int val = Settings.System.getInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
mContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("EventAirplaneChange", val == 1);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
};
IntentFilter locationFilter = new IntentFilter();
locationFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);

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

@Override
public String getName() {
return SystemSetting.class.getSimpleName();
Expand Down Expand Up @@ -313,13 +362,17 @@ public void switchWifi() {
@ReactMethod
public void isLocationEnabled(Promise promise) {
if (lm != null) {
promise.resolve(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
promise.resolve(isLocationEnable());
} else {
promise.reject("-1", "get location manager fail");
}
}

private boolean isLocationEnable() {
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

@ReactMethod
public void switchLocation() {
switchSetting(SysSettings.LOCATION);
Expand Down Expand Up @@ -360,6 +413,14 @@ public void activeListener(String type, Promise promise) {
listenBluetoothState();
promise.resolve(null);
return;
case "location":
listenLocationState();
promise.resolve(null);
return;
case "airplane":
listenAirplaneState();
promise.resolve(null);
return;
}
promise.reject("-1", "unsupported listener type: " + type);
}
Expand Down Expand Up @@ -426,6 +487,14 @@ public void onHostDestroy() {
mContext.unregisterReceiver(bluetoothBR);
bluetoothBR = null;
}
if (locationBR != null) {
mContext.unregisterReceiver(locationBR);
locationBR = null;
}
if (airplaneBR != null) {
mContext.unregisterReceiver(airplaneBR);
airplaneBR = null;
}

mContext.removeLifecycleEventListener(this);
}
Expand Down

0 comments on commit 3fe65e6

Please sign in to comment.