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

Android doesn't seem to be updating when increasing permissions #2223

Closed
CoryWritesCode opened this issue Dec 4, 2024 · 17 comments
Closed

Comments

@CoryWritesCode
Copy link

CoryWritesCode commented Dec 4, 2024

Your Environment

  • Plugin version: 4.17.6
  • Platform: Android
  • OS version: 13
  • Device manufacturer / model: Pixel 7 emulator
  • React Native version (react-native -v): 0.74.5
  • Plugin config
const geoLocationConfig: Config = {
  // Geolocation Config
  desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
  // This can get really annoying and we already check for permissions at the gig level.
  disableLocationAuthorizationAlert: true,
  // Activity Recognition
  stopTimeout: 5,
  // Application config
  debug: false, // IS_DEV, // <-- enable this hear sounds for background-geolocation life-cycle.
  logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
  stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
  startOnBoot: true, // <-- Auto start tracking when device is powered-up.
  // HTTP / SQLite config
  autoSync: false, // <-- [Default: true] Set true to sync each location to server as it arrives.
  enableHeadless: true,
  locationAuthorizationAlert: {
    titleWhenNotEnabled: "Location services are disabled",
    titleWhenOff: "Location services are turned off",
    instructions:
      "Without location access, you won't see your proximity to nearby Gigs or" +
      " be able to verify your attendance at Gigs which may delay or complicate your payment.",
    cancelButton: "Cancel",
    settingsButton: "Settings",
  },
  backgroundPermissionRationale: {
    title: "Allow {applicationName} to access to this device's location in the background?",
    message:
      "{applicationName} Gigpro accesses your location to display nearby Gigs," +
      " verify your work attendance, and ensure accurate payment. " +
      "You can turn off location access at any time from your device settings. " +
      "We value your privacy and are committed to handling your data responsibly.",
    positiveAction: "Change to {backgroundPermissionOptionLabel}",
    negativeAction: "Cancel",
  },
};

Expected Behavior

onProvider subscription is called when a user updates their permissions.

Actual Behavior

no log is sent until the app is killed and reloaded.

Steps to Reproduce

Code I'm using

Steps:

  1. Have lower permissions (i.e. "when in use", "not allowed")
  2. open app
  3. when prompted upgrade permissions.
export const useIsMinimalLocationAllowed = () => {
  const [isMinimalAllowed, setIsMinimalAllowed] = useState(false);

  useEffect(() => {
    const providerSub = BackgroundGeolocation.onProviderChange((event) => {
      console.log("EVENT CHANGE: ", event.status); // <= does not log
      setIsMinimalAllowed(event.status === BackgroundGeolocation.AUTHORIZATION_STATUS_ALWAYS);
    });

    return () => providerSub.remove();
  }, [flag]);

  return isMinimalAllowed;
};

Context

Sending a user to update their permissions and listening for that change.

Debug logs

Screenshot 2024-12-04 at 10 30 48 AM

@CoryWritesCode

This comment was marked as resolved.

@christocracy
Copy link
Member

Android does not broadcast events when the user changes permissions in your App settings.

@CoryWritesCode
Copy link
Author

How do I recognize changes? Just request the provider state each time I need it?

@christocracy
Copy link
Member

yes

@CoryWritesCode
Copy link
Author

Also, the com....android.permissioncontroller happened when I changed permissions in the app settings. Does the plugin not look for that event?

@christocracy
Copy link
Member

I don't know what you mean.

@CoryWritesCode
Copy link
Author

image
second line down. That is triggered by the android system when I change permissions in the App settings. Does the plugin not look for that change event?

@mikehardy
Copy link
Contributor

I can't read that at all, really blurry. Could maybe just paste as text in between triple-backticks so github continues to work well as a text medium ?

@CoryWritesCode
Copy link
Author

2024-12-04 10:28:30.897 20222-20222 AppPermissionViewModel com....android.permissioncontroller V Permission changed via UI with sessionId=5852813097973706028 changeId=2822267427890129468 vid=10168

@CoryWritesCode
Copy link
Author

Here's a video

Screen.Recording.2024-12-04.at.3.08.00.PM.mov

@christocracy
Copy link
Member

Google "Android BroadcastReceiver app permission change". There is no known BroadcastReceiver to register on Android to listen to permission changes from your App in Settings.

https://stackoverflow.com/questions/32718933/broadcast-action-on-permission-change-in-android-m

@mikehardy
Copy link
Contributor

From that answer this was the thing that was most surprising to me when I first experienced it:

If the user revokes a permission that was previously granted to you, your process will be terminated. When you next run, you will wind up finding out about the permission change as part of checking for permissions.

But as the answers say, it doesn't really matter. In practice all you can do is check permission every single time you go to use it, no way around it

@CoryWritesCode
Copy link
Author

CoryWritesCode commented Dec 4, 2024

That makes sense but also makes me question why even use "onProviderChange" if on Android I need to BackgroundGeolocation.getProviderState anyway.

I say this knowing there are other reasons for having that information but a listener for permissions seems like it should have that sort of capability. Or at least somewhere in the docs noting these things. Maybe I missed it.

@CoryWritesCode
Copy link
Author

I think the thing that will trip people up is that the activity is terminated when a permission is revoked but not when a permission is granted. Something to keep in mind.

@christocracy
Copy link
Member

That makes sense but also makes me question why even use "onProviderChange" if on Android

  • wifi toggled
  • Airplane mode toggled
  • settings->location (global one) toggled

@christocracy
Copy link
Member

but a listener for permissions seems like it should have that sort of capability

This is not a new issue. People have been asking about it for years. Android doesn’t feel it’s important to provide a BroadcastReceiver for this.

@CoryWritesCode
Copy link
Author

For others who also run into this issue, I found utilizing React query was a smooth experience. The results are cached but refetched on window focus so the cached response is always "up to date". Plus you get benefits like isLoading and other such states from async api's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants