Skip to content

Commit

Permalink
fix: Android geofence permissions 29+ (#3854)
Browse files Browse the repository at this point in the history
* fix: Android geofence permissions 29+

* hard-coded FLAG_MUTABLE because not available in older versions
  • Loading branch information
shannah authored Nov 24, 2024
1 parent b4185aa commit 329216c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6662,18 +6662,25 @@ public void printStackTraceToStream(Throwable t, Writer o) {
* @return LocationControl Object
*/
public LocationManager getLocationManager() {
boolean permissionGranted = false;
if (Build.VERSION.SDK_INT >= 29 && "true".equals(Display.getInstance().getProperty("android.requiresBackgroundLocationPermissionForAPI29", "false"))) {

if (checkForPermission("android.permission.ACCESS_BACKGROUND_LOCATION", "This is required to get the location")) {
permissionGranted = true;
}

}
if (!permissionGranted && !checkForPermission( Manifest.permission.ACCESS_FINE_LOCATION, "This is required to get the location")) {
String permissionMessage = "This is required to get the location";
if (
!checkForPermission( Manifest.permission.ACCESS_FINE_LOCATION, permissionMessage)
) {
return null;
}

if (
Build.VERSION.SDK_INT >= 29
&& "true".equals(Display.getInstance().getProperty("android.requiresBackgroundLocationPermissionForAPI29", "false"))
) {
if (
!checkForPermission(
"android.permission.ACCESS_BACKGROUND_LOCATION",
permissionMessage
)
) {
com.codename1.io.Log.e(new RuntimeException("Background location permission denied"));
}
}

boolean includesPlayServices = Display.getInstance().getProperty("IncludeGPlayServices", "false").equals("true");
if (includesPlayServices && hasAndroidMarket()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class AndroidLocationPlayServiceManager extends com.codename1.location.Lo
LifecycleListener {



private static final int FLAG_MUTABLE = 33554432;
static class ParcelableUtil {
public static byte[] marshall(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
Expand Down Expand Up @@ -477,12 +477,12 @@ private PendingIntent createGeofencePendingIntent(
}
Intent intent = new Intent(context, BackgroundLocationBroadcastReceiver.class);
intent.setAction(BackgroundLocationBroadcastReceiver.ACTION_PROCESS_GEOFENCE_TRANSITIONS);
intent.setData(Uri.parse("http://codenameone.com/a?" + geofenceListenerClass.getName()));
intent.putExtra("geofenceListenerClass", geofenceListenerClass.getName());
geofencePendingIntent = PendingIntent.getBroadcast(
AndroidNativeUtil.getContext().getApplicationContext(),
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
PendingIntent.FLAG_UPDATE_CURRENT | FLAG_MUTABLE
);
return geofencePendingIntent;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public void onReceive(Context context, Intent intent) {

String dataString = intent.getDataString();
if (dataString == null) {
return;
if (intent.getExtras() != null && intent.getExtras().get("geofenceListenerClass") != null) {
dataString = "-?" + intent.getExtras().get("geofenceListenerClass");
} else {
return;
}
}
String[] params = dataString.split("[?]");
if (params.length < 2) {
Expand Down

0 comments on commit 329216c

Please sign in to comment.