First working version
This commit is contained in:
parent
891edcc0a3
commit
58f2e79ef0
2 changed files with 40 additions and 17 deletions
|
@ -25,7 +25,9 @@ public class GeolocationBridge {
|
||||||
|
|
||||||
Databridge.addResponder("location:watch", pluginCall -> {
|
Databridge.addResponder("location:watch", pluginCall -> {
|
||||||
pluginCall.setKeepAlive(true);
|
pluginCall.setKeepAlive(true);
|
||||||
new LocationUpdateListener(pluginCall, context).requestLocationUpdates(true);
|
System.out.println("Got a request to start watching the location");
|
||||||
|
new LocationUpdateListener(pluginCall, context)
|
||||||
|
.requestLocationUpdates(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,12 +111,14 @@ class LocationUpdateListener implements LocationListener {
|
||||||
this.error("Location unavailable: no providers defined. Note: this is a Google Play Services free implementation");
|
this.error("Location unavailable: no providers defined. Note: this is a Google Play Services free implementation");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
System.out.println("Starting 'lm.requestLocationUpdates'");
|
||||||
lm.requestLocationUpdates(provider, 1000, 10, this);
|
lm.requestLocationUpdates(provider, 1000, 10, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
|
System.out.println("Got a new location: "+ location.getLatitude()+"; "+location.getLongitude());
|
||||||
JSObject loc = new JSObject();
|
JSObject loc = new JSObject();
|
||||||
loc.put("latitude", location.getLatitude());
|
loc.put("latitude", location.getLatitude());
|
||||||
loc.put("longitude", location.getLongitude());
|
loc.put("longitude", location.getLongitude());
|
||||||
|
@ -129,6 +133,11 @@ class LocationUpdateListener implements LocationListener {
|
||||||
// getBearing returns essentially the same
|
// getBearing returns essentially the same
|
||||||
loc.put("heading", location.getBearing());
|
loc.put("heading", location.getBearing());
|
||||||
}
|
}
|
||||||
|
if (location.hasSpeed()) {
|
||||||
|
// Expected for heading: 0 is north, 90 is east, up till 359° ; see https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates
|
||||||
|
// getBearing returns essentially the same
|
||||||
|
loc.put("speed", location.getSpeed());
|
||||||
|
}
|
||||||
|
|
||||||
JSObject ret = new JSObject();
|
JSObject ret = new JSObject();
|
||||||
ret.put("value", loc);
|
ret.put("value", loc);
|
||||||
|
|
|
@ -32,7 +32,18 @@ public class MainActivity extends BridgeActivity {
|
||||||
new GeolocationBridge(getApplicationContext(), this);
|
new GeolocationBridge(getApplicationContext(), this);
|
||||||
Databridge.addResponder("location:request-permission", pluginCall -> {
|
Databridge.addResponder("location:request-permission", pluginCall -> {
|
||||||
this.locationRequest = pluginCall;
|
this.locationRequest = pluginCall;
|
||||||
this.requestPermission();
|
if(this.hasGeolocationPermission()){
|
||||||
|
Databridge.sendAnswerTo(pluginCall, ""+true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.requestGeolocationPermission();
|
||||||
|
// note: this will call back via:
|
||||||
|
// this.onRequestPermissionsResult();
|
||||||
|
});
|
||||||
|
Databridge.addResponder("location:has-permission", pluginCall -> {
|
||||||
|
var permission = this.hasGeolocationPermission();
|
||||||
|
System.out.println("Android: Geolocation permission is: "+ permission);
|
||||||
|
Databridge.sendAnswerTo(pluginCall, "" + permission);
|
||||||
});
|
});
|
||||||
Databridge.addResponder("request:login", pluginCall -> {
|
Databridge.addResponder("request:login", pluginCall -> {
|
||||||
this.authRequest = pluginCall;
|
this.authRequest = pluginCall;
|
||||||
|
@ -42,7 +53,6 @@ public class MainActivity extends BridgeActivity {
|
||||||
});
|
});
|
||||||
|
|
||||||
Databridge.addResponder("backbutton", responder -> {
|
Databridge.addResponder("backbutton", responder -> {
|
||||||
System.out.println("Received back button responder");
|
|
||||||
this.backbutton = responder;
|
this.backbutton = responder;
|
||||||
responder.setKeepAlive(true);
|
responder.setKeepAlive(true);
|
||||||
});
|
});
|
||||||
|
@ -53,27 +63,31 @@ public class MainActivity extends BridgeActivity {
|
||||||
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_OVERLAY,
|
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_OVERLAY,
|
||||||
() -> {
|
() -> {
|
||||||
System.out.println("Back button pressed");
|
System.out.println("Back button pressed");
|
||||||
if(this.backbutton != null){
|
if (this.backbutton != null) {
|
||||||
Databridge.sendAnswerTo(this.backbutton, "backbutton pressed");
|
Databridge.sendAnswerTo(this.backbutton, "backbutton pressed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestPermission() {
|
private boolean hasGeolocationPermission() {
|
||||||
|
return ContextCompat.checkSelfPermission(
|
||||||
if (ContextCompat.checkSelfPermission(
|
|
||||||
getApplicationContext(),
|
getApplicationContext(),
|
||||||
android.Manifest.permission.ACCESS_FINE_LOCATION
|
android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
) != PackageManager.PERMISSION_GRANTED) {
|
) == PackageManager.PERMISSION_GRANTED;
|
||||||
// Permission is not granted, request it
|
}
|
||||||
ActivityCompat.requestPermissions(
|
|
||||||
this,
|
|
||||||
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
|
|
||||||
GeolocationBridge.requestCode
|
|
||||||
);
|
|
||||||
|
|
||||||
|
private void requestGeolocationPermission() {
|
||||||
|
if (this.hasGeolocationPermission()) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
// Permission is not granted, request it
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this,
|
||||||
|
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
|
||||||
|
GeolocationBridge.requestCode
|
||||||
|
);
|
||||||
|
// The request will call `this.onRequestPermissionsResult` when done!
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -87,7 +101,7 @@ public class MainActivity extends BridgeActivity {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("urlPath: " + url.getPath()+";"+ String.join(", ",url.getQueryParameterNames()));
|
System.out.println("urlPath: " + url.getPath() + ";" + String.join(", ", url.getQueryParameterNames()));
|
||||||
if (url.getQueryParameterNames().contains("oauth_token")) {
|
if (url.getQueryParameterNames().contains("oauth_token")) {
|
||||||
var token = url.getQueryParameter("oauth_token");
|
var token = url.getQueryParameter("oauth_token");
|
||||||
JSObject obj = new JSObject();
|
JSObject obj = new JSObject();
|
||||||
|
@ -123,8 +137,8 @@ public class MainActivity extends BridgeActivity {
|
||||||
if (this.locationRequest != null) {
|
if (this.locationRequest != null) {
|
||||||
// We've only requested "FINE_LOCATION"
|
// We've only requested "FINE_LOCATION"
|
||||||
var granted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
var granted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
||||||
Log.i("Geolocation", "Got fine location request: " + granted);
|
Log.i("Geolocation", "Got fine location permission: " + granted);
|
||||||
Databridge.sendAnswerTo(this.locationRequest, granted ? "granted" : "denied");
|
Databridge.sendAnswerTo(this.locationRequest, ""+granted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue