diff --git a/app/src/main/java/com/google/android/stardroid/LocationManagerCheck.java b/app/src/main/java/com/google/android/stardroid/LocationManagerCheck.java new file mode 100644 index 000000000..5048e8c17 --- /dev/null +++ b/app/src/main/java/com/google/android/stardroid/LocationManagerCheck.java @@ -0,0 +1,75 @@ +package com.google.android.stardroid; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.location.LocationManager; +import android.provider.Settings; + +public class LocationManagerCheck { + + LocationManager locationManager; + Boolean locationServiceBoolean = false; + int providerType = 0; + static AlertDialog alert; + + public LocationManagerCheck(Context context) { + locationManager = (LocationManager) context + .getSystemService(Context.LOCATION_SERVICE); + boolean gpsIsEnabled = locationManager + .isProviderEnabled(LocationManager.GPS_PROVIDER); + boolean networkIsEnabled = locationManager + .isProviderEnabled(LocationManager.NETWORK_PROVIDER); + + if (networkIsEnabled == true && gpsIsEnabled == true) { + locationServiceBoolean = true; + providerType = 1; + + } else if (networkIsEnabled != true && gpsIsEnabled == true) { + locationServiceBoolean = true; + providerType = 2; + + } else if (networkIsEnabled == true && gpsIsEnabled != true) { + locationServiceBoolean = true; + providerType = 1; + } + } + + public Boolean isLocationServiceAvailable() { + return locationServiceBoolean; + } + + public int getProviderType() { + return providerType; + } + + public void createLocationServiceError(final Context activityObj) { + + // show alert dialog if Internet is not connected + AlertDialog.Builder builder = new AlertDialog.Builder(activityObj); + + builder.setMessage( + "You need to activate location service to use this feature. Please turn on network or GPS mode in location settings") + .setTitle("Enable Location") + .setCancelable(false) + .setPositiveButton("Settings", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Intent intent = new Intent( + Settings.ACTION_LOCATION_SOURCE_SETTINGS); + activityObj.startActivity(intent); + dialog.dismiss(); + } + }) + .setNegativeButton("Cancel", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + alert = builder.create(); + alert.show(); + } +} diff --git a/app/src/main/java/com/google/android/stardroid/StardroidApplication.java b/app/src/main/java/com/google/android/stardroid/StardroidApplication.java index 182c35029..aa25a354c 100644 --- a/app/src/main/java/com/google/android/stardroid/StardroidApplication.java +++ b/app/src/main/java/com/google/android/stardroid/StardroidApplication.java @@ -13,7 +13,10 @@ // limitations under the License. package com.google.android.stardroid; +import android.Manifest; +import android.app.Activity; import android.app.Application; +import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; @@ -22,8 +25,11 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.location.Location; +import android.location.LocationManager; import android.os.Build; import android.preference.PreferenceManager; +import android.support.v4.app.ActivityCompat; import android.util.Log; import com.google.android.stardroid.layers.LayerManager; @@ -92,6 +98,32 @@ public ApplicationComponent getApplicationComponent() { return component; } + private void checkLocationPerms() { + LocationManagerCheck locationManagerCheck = new LocationManagerCheck(this); + Location location = null; + LocationManager locationManager = + (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + + if (locationManagerCheck.isLocationServiceAvailable()) { + + if (locationManagerCheck.getProviderType() == 1) { + if (ActivityCompat.checkSelfPermission(this, + Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager + .PERMISSION_GRANTED + && ActivityCompat.checkSelfPermission(this, + Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager + .PERMISSION_GRANTED) { + location = locationManager + .getLastKnownLocation(LocationManager.GPS_PROVIDER); + return; + }else if (locationManagerCheck.getProviderType() == 2) + location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + } + } else { + locationManagerCheck.createLocationServiceError(getApplicationContext()); + } + } + private void setUpAnalytics(String versionName) { analytics.setCustomVar(Slice.ANDROID_OS, Integer.toString(Build.VERSION.SDK_INT)); analytics.setCustomVar(Slice.SKYMAP_VERSION, versionName);