Created
September 3, 2019 13:54
-
-
Save yunusemredilber/e42c5b9f6e996240fe7fbc0137b5dc3a to your computer and use it in GitHub Desktop.
Android Turbolinks Geolocation Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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