Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ask for Location Perms enabled #271

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -46,51 +52,84 @@
* @author John Taylor
*/
public class StardroidApplication extends Application {
private static final String TAG = MiscUtil.getTag(StardroidApplication.class);
private static final String PREVIOUS_APP_VERSION_PREF = "previous_app_version";
private static final String NONE = "Clean install";
private static final String UNKNOWN = "Unknown previous version";

@Inject SharedPreferences preferences;
// We keep a reference to this just to start it initializing.
@Inject LayerManager layerManager;
@Inject Analytics analytics;
@Inject SensorManager sensorManager;

// We need to maintain references to this object to keep it from
// getting gc'd.
@Inject PreferenceChangeAnalyticsTracker preferenceChangeAnalyticsTracker;
private ApplicationComponent component;

@Override
public void onCreate() {
Log.d(TAG, "StardroidApplication: onCreate");
super.onCreate();

component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
component.inject(this);

Log.i(TAG, "OS Version: " + android.os.Build.VERSION.RELEASE
+ "(" + android.os.Build.VERSION.SDK_INT + ")");
String versionName = getVersionName();
Log.i(TAG, "Sky Map version " + versionName + " build " + getVersion());

// This populates the default values from the preferences XML file. See
// {@link DefaultValues} for more details.
PreferenceManager.setDefaultValues(this, R.xml.preference_screen, false);

setUpAnalytics(versionName);

performFeatureCheck();
private static final String TAG = MiscUtil.getTag(StardroidApplication.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - looks like you've done a big reformatting here. Please don't do that - it makes the change history harder to understand and can make merges more difficult. Doing a one off refactor to reformat is sometimes OK if a file is badly formatted, but it's best not to mix it up with functionality changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaydeetay i have only reformatted the code using ctrl+alt+L. Nothing much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaydeetay I understood what you were talking about. Changes made 👍

private static final String PREVIOUS_APP_VERSION_PREF = "previous_app_version";
private static final String NONE = "Clean install";
private static final String UNKNOWN = "Unknown previous version";

@Inject
SharedPreferences preferences;
// We keep a reference to this just to start it initializing.
@Inject
LayerManager layerManager;
@Inject
Analytics analytics;
@Inject
SensorManager sensorManager;

// We need to maintain references to this object to keep it from
// getting gc'd.
@Inject
PreferenceChangeAnalyticsTracker preferenceChangeAnalyticsTracker;
private ApplicationComponent component;

@Override
public void onCreate() {
Log.d(TAG, "StardroidApplication: onCreate");
super.onCreate();

component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
component.inject(this);

Log.i(TAG, "OS Version: " + android.os.Build.VERSION.RELEASE
+ "(" + android.os.Build.VERSION.SDK_INT + ")");
String versionName = getVersionName();
Log.i(TAG, "Sky Map version " + versionName + " build " + getVersion());

// This populates the default values from the preferences XML file. See
// {@link DefaultValues} for more details.
PreferenceManager.setDefaultValues(this, R.xml.preference_screen, false);

setUpAnalytics(versionName);

checkLocationPerms();

performFeatureCheck();

Log.d(TAG, "StardroidApplication: -onCreate");
}

Log.d(TAG, "StardroidApplication: -onCreate");
}
public ApplicationComponent getApplicationComponent() {
return component;
}

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));
Expand Down