capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
185 stars 57 forks source link

Add method to check background location permission (iOS & Android) #118

Closed LukasGrubis closed 1 month ago

diachedelic commented 1 month ago

I appreciate the work that went into this, but I don't think it is the ideal solution. Because this plugin does not request the ACCESS_BACKGROUND_LOCATION permission, it makes no sense to have a method that checks it. I also appreciate that you provided an iOS equivalent, but in this case the two permissions are not symmetrical.

I intend to properly fix the bug you are experiencing, but the 'checkBackgroundLocationPermission' method seems more like a stopgap measure. I recommend you move it into an ad hoc Android plugin within your application. It is very easy:

// BackgroundLocationPermissionsPlugin.java

package ...;

...

@NativePlugin
public class BackgroundLocationPermissionsPlugin extends Plugin {
    @PluginMethod()
    public void checkBackgroundLocationPermission(PluginCall call) {
        Boolean hasPermission = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;
        JSObject obj = new JSObject();
        obj.put("hasPermission", hasPermission);
        call.resolve(obj);
    }
}

Place BackgroundLocationPermissionsPlugin.java in the same directory as MainActivity.java. Then modify MainActivity.java like so:

package ...;

import android.os.Bundle;
import com.getcapacitor.BridgeActivity;

public class MainActivity extends BridgeActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        registerPlugin(BackgroundLocationPermissionsPlugin.class);
        super.onCreate(savedInstanceState);
    }
}