Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add location permission check for getCurrentWifiSSID and reScanAndLoa… #264

Merged
merged 2 commits into from
May 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ public String getName() {
*/
@ReactMethod
public void loadWifiList(final Promise promise) {
final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context);
if (!locationPermissionGranted) {
promise.reject(LoadWifiListErrorCodes.locationPermissionMissing.toString(), "Location permission (ACCESS_FINE_LOCATION) is not granted");
return;
}

final boolean isLocationOn = LocationUtils.isLocationOn(context);
if (!isLocationOn) {
promise.reject(LoadWifiListErrorCodes.locationServicesOff.toString(), "Location service is turned off");
if(!isLocationPermissionGranted(promise)){
return;
}

Expand Down Expand Up @@ -209,15 +201,7 @@ public void setEnabled(final boolean enabled) {
*/
@ReactMethod
public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final String password, final boolean isWep, final Promise promise) {
final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context);
if (!locationPermissionGranted) {
promise.reject(ConnectErrorCodes.locationPermissionMissing.toString(), "Location permission (ACCESS_FINE_LOCATION) is not granted");
return;
}

final boolean isLocationOn = LocationUtils.isLocationOn(context);
if (!isLocationOn) {
promise.reject(ConnectErrorCodes.locationServicesOff.toString(), "Location service is turned off");
if(!isLocationPermissionGranted(promise)) {
return;
}

Expand Down Expand Up @@ -289,6 +273,10 @@ public void failed(@NonNull DisconnectionErrorCode errorCode) {
*/
@ReactMethod
public void getCurrentWifiSSID(final Promise promise) {
if(!isLocationPermissionGranted(promise)){
return;
}

String ssid = getWifiSSID();
if (ssid == null) {
promise.reject(GetCurrentWifiSSIDErrorCodes.couldNotDetectSSID.toString(), "Not connected or connecting.");
Expand Down Expand Up @@ -393,6 +381,10 @@ public void failed(@NonNull RemoveErrorCode errorCode) {
*/
@ReactMethod
public void reScanAndLoadWifiList(final Promise promise) {
if(!isLocationPermissionGranted(promise)) {
return;
}

final WifiScanResultReceiver wifiScanResultReceiver = new WifiScanResultReceiver(wifi, promise);
getReactApplicationContext().registerReceiver(wifiScanResultReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
Expand Down Expand Up @@ -470,7 +462,7 @@ public void onAvailable(Network network) {
@Override
public void onUnavailable() {
super.onUnavailable();
promise.reject(ConnectErrorCodes.userDenied.toString(), "On Android 10, the user cancelled connecting (via System UI).");
promise.reject(ConnectErrorCodes.didNotFindNetwork.toString(), "Network not found or network request cannot be fulfilled.");
}

@Override
Expand Down Expand Up @@ -576,4 +568,20 @@ private void stuffWifiConfigurationWithoutEncryption(final WifiConfiguration wif
private String formatWithBackslashes(final String value) {
return String.format("\"%s\"", value);
}

private boolean isLocationPermissionGranted(final Promise promise) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a 100% fan of the method name, as this method does the check and rejects the promise, which isn't clear now.

Copy link
Collaborator

@eliaslecomte eliaslecomte Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe rejectIfLocationPermissionMissing or assertLocationPermissionGranted?

final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context);
if (!locationPermissionGranted) {
promise.reject(ConnectErrorCodes.locationPermissionMissing.toString(), "Location permission (ACCESS_FINE_LOCATION) is not granted");
return false;
}

final boolean isLocationOn = LocationUtils.isLocationOn(context);
if (!isLocationOn) {
promise.reject(ConnectErrorCodes.locationServicesOff.toString(), "Location service is turned off");
return false;
}

return true;
}
}