Skip to content

Commit 329216c

Browse files
authored
fix: Android geofence permissions 29+ (#3854)
* fix: Android geofence permissions 29+ * hard-coded FLAG_MUTABLE because not available in older versions
1 parent b4185aa commit 329216c

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6662,18 +6662,25 @@ public void printStackTraceToStream(Throwable t, Writer o) {
66626662
* @return LocationControl Object
66636663
*/
66646664
public LocationManager getLocationManager() {
6665-
boolean permissionGranted = false;
6666-
if (Build.VERSION.SDK_INT >= 29 && "true".equals(Display.getInstance().getProperty("android.requiresBackgroundLocationPermissionForAPI29", "false"))) {
6667-
6668-
if (checkForPermission("android.permission.ACCESS_BACKGROUND_LOCATION", "This is required to get the location")) {
6669-
permissionGranted = true;
6670-
}
6671-
6672-
}
6673-
if (!permissionGranted && !checkForPermission( Manifest.permission.ACCESS_FINE_LOCATION, "This is required to get the location")) {
6665+
String permissionMessage = "This is required to get the location";
6666+
if (
6667+
!checkForPermission( Manifest.permission.ACCESS_FINE_LOCATION, permissionMessage)
6668+
) {
66746669
return null;
66756670
}
6676-
6671+
if (
6672+
Build.VERSION.SDK_INT >= 29
6673+
&& "true".equals(Display.getInstance().getProperty("android.requiresBackgroundLocationPermissionForAPI29", "false"))
6674+
) {
6675+
if (
6676+
!checkForPermission(
6677+
"android.permission.ACCESS_BACKGROUND_LOCATION",
6678+
permissionMessage
6679+
)
6680+
) {
6681+
com.codename1.io.Log.e(new RuntimeException("Background location permission denied"));
6682+
}
6683+
}
66776684

66786685
boolean includesPlayServices = Display.getInstance().getProperty("IncludeGPlayServices", "false").equals("true");
66796686
if (includesPlayServices && hasAndroidMarket()) {

Ports/Android/src/com/codename1/location/AndroidLocationPlayServiceManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class AndroidLocationPlayServiceManager extends com.codename1.location.Lo
6666
LifecycleListener {
6767

6868

69-
69+
private static final int FLAG_MUTABLE = 33554432;
7070
static class ParcelableUtil {
7171
public static byte[] marshall(Parcelable parceable) {
7272
Parcel parcel = Parcel.obtain();
@@ -477,12 +477,12 @@ private PendingIntent createGeofencePendingIntent(
477477
}
478478
Intent intent = new Intent(context, BackgroundLocationBroadcastReceiver.class);
479479
intent.setAction(BackgroundLocationBroadcastReceiver.ACTION_PROCESS_GEOFENCE_TRANSITIONS);
480-
intent.setData(Uri.parse("http://codenameone.com/a?" + geofenceListenerClass.getName()));
480+
intent.putExtra("geofenceListenerClass", geofenceListenerClass.getName());
481481
geofencePendingIntent = PendingIntent.getBroadcast(
482482
AndroidNativeUtil.getContext().getApplicationContext(),
483483
0,
484484
intent,
485-
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
485+
PendingIntent.FLAG_UPDATE_CURRENT | FLAG_MUTABLE
486486
);
487487
return geofencePendingIntent;
488488
} else {

Ports/Android/src/com/codename1/location/BackgroundLocationBroadcastReceiver.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ public void onReceive(Context context, Intent intent) {
105105

106106
String dataString = intent.getDataString();
107107
if (dataString == null) {
108-
return;
108+
if (intent.getExtras() != null && intent.getExtras().get("geofenceListenerClass") != null) {
109+
dataString = "-?" + intent.getExtras().get("geofenceListenerClass");
110+
} else {
111+
return;
112+
}
109113
}
110114
String[] params = dataString.split("[?]");
111115
if (params.length < 2) {

0 commit comments

Comments
 (0)