dwemthy
dwemthy

Reputation: 471

WebChromeClient.onGeolocationPermissionsShowPrompt() never called if locations are turned off

I've got a WebView that loads a page with location based functionality and a WebChromeClient with onGeolocationPermissionsShowPrompt() overriden. Enabling location for the page works fine when location services are on.

The issue is that if the user's location is turned off I want to prompt them to turn it on, but if it's turned off I never hit onGeolocationPermissionsShowPrompt(). Is this function only called if a location is available?

Upvotes: 2

Views: 2739

Answers (3)

Ucdemir
Ucdemir

Reputation: 3098

You need second and third paramater to be false

callback.invoke(origin, false, false);

Upvotes: 0

petter
petter

Reputation: 1842

One solution is not to retain the permission (use false as the third parameter in the callback). This can lead to excessive permission asking, so storing the result locally solves that:

class MyWebChromeClient : WebChromeClient() {

    private var givenPermissions: Set<String> = emptySet()

    override fun onGeolocationPermissionsShowPrompt(origin: String, callback: GeolocationPermissions.Callback) {
        val onResult = { allow: Boolean -> callback(origin, allow, false) }
        when {
            origin in givenPermissions -> checkSystemPermission { granted ->
                if (!granted) givenPermissions -= origin
                onResult(granted)
            }
            else -> askForPermission { allow ->
                when (allow) {
                    false -> onResult(false)
                    true -> checkSystemPermission { granted ->
                        if (granted) givenPermissions += origin
                        onResult(granted)
                    }
                }
            }
        }
    }

    private fun askForPermission(callback: (Boolean) -> Unit) {
        // ask for permission
    }

    private fun checkSystemPermission(callback: (Boolean) -> Unit) {
        // check/ask for system permission
    }

}

If the system permission is revoked, the WebChromeClient should be recreated automatically.

Upvotes: 1

ZeroSkittles
ZeroSkittles

Reputation: 164

Not sure if this is exactly what you are looking for but I just call a method CheckEnableGPS()

//Method for turning GPS on if it isn't
    private void CheckEnableGPS(){    
    String provider = Settings.Secure.getString(getContentResolver(),      
    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);       
    if(!provider.equals(""))
    {           
      //GPS Enabled        
    Toast.makeText(ShowMapActivity.this, "GPS Enabled: " + provider,          Toast.LENGTH_LONG).show();       
    }
    else
    {        
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);           startActivity(intent);      
       }   
    }

Upvotes: 0

Related Questions