Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Created September 3, 2019 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunusemredilber/e42c5b9f6e996240fe7fbc0137b5dc3a to your computer and use it in GitHub Desktop.
Save yunusemredilber/e42c5b9f6e996240fe7fbc0137b5dc3a to your computer and use it in GitHub Desktop.
Android Turbolinks Geolocation Handling
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
public class BaseActivity extends AppCompatActivity implements TurbolinksAdapter {
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// Enable geolocation, javascript & other things
TurbolinksSession.getDefault(this).getWebView().getSettings().setGeolocationEnabled(true);
TurbolinksSession.getDefault(this).getWebView().getSettings().setJavaScriptEnabled(true);
TurbolinksSession.getDefault(this).getWebView().getSettings().setDatabaseEnabled(true);
TurbolinksSession.getDefault(this).getWebView().getSettings().setDomStorageEnabled(true);
// Set location prompt
TurbolinksSession.getDefault(this).getWebView().setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
// Set GeolocationDatabasePath
TurbolinksSession.getDefault(this).getWebView().getSettings().setGeolocationDatabasePath( this.getFilesDir().getPath() );
// ... (Make visit)
checkLocationPermission();
}
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(BaseActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Request location updates:
//locationManager.requestLocationUpdates(provider, 400, 1, this);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment