Skip to content

Commit

Permalink
Android: rewrite location module using new location manager interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Agontuk committed Mar 15, 2021
1 parent 50e5082 commit 58a0362
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.os.Build;
import android.text.TextUtils;
import android.provider.Settings;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.facebook.react.bridge.Arguments;
Expand Down Expand Up @@ -78,14 +80,17 @@ public static boolean isProviderEnabled(Context context, String provider) {
/**
* Build error response for error callback.
*/
public static WritableMap buildError(int code, String message) {
WritableMap error = Arguments.createMap();
error.putInt("code", code);
public static WritableMap buildError(LocationError locationError, @Nullable String message) {
String msg = message;

if (message != null) {
error.putString("message", message);
if (msg == null) {
msg = getDefaultErrorMessage(locationError);
}

WritableMap error = Arguments.createMap();
error.putInt("code", locationError.getValue());
error.putString("message", msg);

return error;
}

Expand All @@ -108,4 +113,22 @@ public static WritableMap locationToMap(Location location) {

return map;
}

private static String getDefaultErrorMessage(LocationError locationError) {
switch (locationError) {
case PERMISSION_DENIED:
return "Location permission not granted.";
case POSITION_UNAVAILABLE:
return "No location provider available.";
case TIMEOUT:
return "Location request timed out.";
case PLAY_SERVICE_NOT_AVAILABLE:
return "Google play service is not available.";
case SETTINGS_NOT_SATISFIED:
return "Location settings are not satisfied.";
case INTERNAL_ERROR:
default:
return "Internal error occurred";
}
}
}
Loading

0 comments on commit 58a0362

Please sign in to comment.