Skip to content

Commit

Permalink
#20, #28 - check network location settings just for system location p…
Browse files Browse the repository at this point in the history
…rovider, don't check permissions and settings when entering main application, fix of stopping update circle when GPS is requested and not found, fix starting of periodic weather check when location added or removed
  • Loading branch information
thuryn committed Mar 15, 2018
1 parent 19c782a commit 783690e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "org.thosp.yourlocalweather"
minSdkVersion 14
targetSdkVersion 25
versionCode 27
versionName "3.0.1"
versionCode 28
versionName "3.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
playAccountConfig = playAccountConfigs.defaultAccountConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ private void deleteLocation(int position) {
CurrentWeatherDbHelper currentWeatherDbHelper = CurrentWeatherDbHelper.getInstance(org.thosp.yourlocalweather.LocationsActivity.this);
WeatherForecastDbHelper weatherForecastDbHelper = WeatherForecastDbHelper.getInstance(org.thosp.yourlocalweather.LocationsActivity.this);
Location location = locationsAdapter.locations.get(position);
int locatonOrder = location.getOrderId();
currentWeatherDbHelper.deleteRecordByLocation(location);
weatherForecastDbHelper.deleteRecordByLocation(location);
locationsDbHelper.deleteRecordFromTable(location);

if (locatonOrder == 1) {
Intent intentToStartUpdate = new Intent("org.thosp.yourlocalweather.action.RESTART_ALARM_SERVICE");
intentToStartUpdate.setPackage("org.thosp.yourlocalweather");
this.startService(intentToStartUpdate);
}

locationsAdapter.locations.remove(position);
locationsAdapter.notifyItemRemoved(position);
locationsAdapter.notifyItemRangeChanged(position, locationsAdapter.getItemCount());
Expand Down
27 changes: 13 additions & 14 deletions app/src/main/java/org/thosp/yourlocalweather/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,12 @@ public boolean checkPermissionsSettingsAndShowAlert() {
&& locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)
&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
String geocoder = AppPreference.getLocationGeocoderSource(getBaseContext());

if (!isGPSEnabled && !isNetworkEnabled) {
boolean gpsNotEnabled = !isGPSEnabled && AppPreference.isGpsEnabledByPreferences(getBaseContext());
boolean networkNotEnabled = !isNetworkEnabled && "location_geocoder_system".equals(geocoder);

if (gpsNotEnabled || networkNotEnabled) {
settingsAlert.setMessage(R.string.alertDialog_location_permission_message_location_phone_settings);
settingsAlert.setPositiveButton(R.string.alertDialog_location_permission_positiveButton_settings,
new DialogInterface.OnClickListener() {
Expand All @@ -531,7 +535,6 @@ public void onClick(DialogInterface dialog, int which) {
}
});
} else {
String geocoder = AppPreference.getLocationGeocoderSource(getBaseContext());
List<String> permissions = new ArrayList<>();
StringBuilder notificationMessage = new StringBuilder();
if (AppPreference.isGpsEnabledByPreferences(getBaseContext()) &&
Expand All @@ -540,14 +543,12 @@ public void onClick(DialogInterface dialog, int which) {
notificationMessage.append(getString(R.string.alertDialog_location_permission_message_location_phone_settings) + "\n\n");
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (isNetworkEnabled) {
if ("location_geocoder_local".equals(geocoder) && ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
notificationMessage.append(getString(R.string.alertDialog_location_permission_message_location_phone_permission));
permissions.add(Manifest.permission.READ_PHONE_STATE);
} else if ("location_geocoder_system".equals(geocoder) && ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
notificationMessage.append(getString(R.string.alertDialog_location_permission_message_location_network_permission));
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if ("location_geocoder_local".equals(geocoder) && ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
notificationMessage.append(getString(R.string.alertDialog_location_permission_message_location_phone_permission));
permissions.add(Manifest.permission.READ_PHONE_STATE);
} else if (isNetworkEnabled && "location_geocoder_system".equals(geocoder) && ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
notificationMessage.append(getString(R.string.alertDialog_location_permission_message_location_network_permission));
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (permissions.isEmpty()) {
return true;
Expand Down Expand Up @@ -587,19 +588,17 @@ public void onClick(DialogInterface dialog, int which) {
private int selectedCacheLocationStrategy;

private void checkSettingsAndPermisions() {
if (!initialGuideCompleted) {
checkAndShowInitialGuide();
if (initialGuideCompleted) {
return;
}
checkPermissionsSettingsAndShowAlert();
checkAndShowInitialGuide();
}

private void checkAndShowInitialGuide() {
int initialGuideVersion = PreferenceManager.getDefaultSharedPreferences(getBaseContext())
.getInt(Constants.APP_INITIAL_GUIDE_VERSION, 0);
if (initialGuideVersion > 0) {
initialGuideCompleted = true;
checkPermissionsSettingsAndShowAlert();
return;
}
if (initialGuidePage > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,11 @@ private void storeLocation() {
values.put(LocationsContract.Locations.COLUMN_NAME_ADDRESS_FOUND, 1);

long newLocationRowId = db.insert(LocationsContract.Locations.TABLE_NAME, null, values);

if (currentMaxOrderId == 0) {
Intent intentToStartUpdate = new Intent("org.thosp.yourlocalweather.action.RESTART_ALARM_SERVICE");
intentToStartUpdate.setPackage("org.thosp.yourlocalweather");
this.startService(intentToStartUpdate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ public int onStartCommand(Intent intent, int flags, final int startId) {
isGPSEnabled +
", isNetworkEnabled=" +
isNetworkEnabled);
if (isUpdateOfLocationEnabled && (isGPSEnabled || isNetworkEnabled)) {
String geocoder = AppPreference.getLocationGeocoderSource(this);
String geocoder = AppPreference.getLocationGeocoderSource(this);
if (isUpdateOfLocationEnabled && (isGPSEnabled || isNetworkEnabled || !"location_geocoder_system".equals(geocoder))) {
appendLog(getBaseContext(), TAG, "Widget calls to update location, geocoder = " + geocoder);
if ("location_geocoder_unifiednlp".equals(geocoder) || "location_geocoder_local".equals(geocoder)) {
updateNetworkLocation(false);
Expand Down Expand Up @@ -380,6 +380,7 @@ public void run() {
public void run() {
locationManager.removeUpdates(gpsLocationListener);
setNoLocationFound();
stopRefreshRotation();
}
};

Expand Down Expand Up @@ -464,7 +465,10 @@ private boolean updateNetworkLocation(boolean bylastLocationOnly) {
boolean isGPSEnabled = AppPreference.isGpsEnabledByPreferences(getBaseContext()) &&
locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)
&& locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isNetworkEnabled && isGPSEnabled && !bylastLocationOnly) {
String geocoder = AppPreference.getLocationGeocoderSource(getBaseContext());
boolean networkNotEnabled = !isNetworkEnabled && "location_geocoder_system".equals(geocoder);

if (networkNotEnabled && isGPSEnabled && !bylastLocationOnly) {
startRefreshRotation();
gpsRequestLocation();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,19 @@ public static List<String> getAllPermissions(Context context) {
boolean isNetworkEnabled = locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)
&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

boolean gsmWifiBasedNetworkLocationProvider = false;
String geocoder = AppPreference.getLocationGeocoderSource(context);
if ("location_geocoder_system".equals(geocoder) || "location_geocoder_local".equals(geocoder)) {
gsmWifiBasedNetworkLocationProvider = true;
}

if (AppPreference.isGpsEnabledByPreferences(context) &&
isGPSEnabled &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (isNetworkEnabled) {
if (gsmWifiBasedNetworkLocationProvider && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_PHONE_STATE);
} else if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if ("location_geocoder_local".equals(geocoder) &&
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_PHONE_STATE);
} else if ("location_geocoder_system".equals(geocoder) &&
isNetworkEnabled &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
return permissions;
}
Expand All @@ -85,24 +81,25 @@ public static boolean checkPermissionsAndSettings(Context context) {
boolean isNetworkEnabled = locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)
&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

String geocoder = AppPreference.getLocationGeocoderSource(context);
boolean gpsNotSet = AppPreference.isGpsEnabledByPreferences(context) && !isGPSEnabled;
boolean networkNotSet = "location_geocoder_system".equals(geocoder) && !isNetworkEnabled;

appendLog(context, TAG_CHECK_PERMISSIONS_AND_SETTINGS, "isGPSEnabled=" + isGPSEnabled + ", isNetworkEnabled=" + isNetworkEnabled);
if (!isGPSEnabled && !isNetworkEnabled) {
if (gpsNotSet && networkNotSet) {
appendLog(context, TAG_CHECK_PERMISSIONS_AND_SETTINGS, "isGPSEnabled and isNetworkEnabled is not set, returning false");
return false;
} else {
String geocoder = AppPreference.getLocationGeocoderSource(context);
List<String> permissions = new ArrayList<>();
if (AppPreference.isGpsEnabledByPreferences(context) &&
isGPSEnabled &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (isNetworkEnabled) {
if ("location_geocoder_local".equals(geocoder) && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_PHONE_STATE);
} else if ("location_geocoder_system".equals(geocoder) && ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if ("location_geocoder_local".equals(geocoder) && ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_PHONE_STATE);
} else if ("location_geocoder_system".equals(geocoder) && isNetworkEnabled && ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
appendLog(context, TAG_CHECK_PERMISSIONS_AND_SETTINGS, "permissions is empty = " + permissions.isEmpty());
if (permissions.isEmpty()) {
Expand Down

0 comments on commit 783690e

Please sign in to comment.