Skip to content

Commit

Permalink
Refactored GeocodeProviderBaseFactory and associated test
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Aug 23, 2020
1 parent ac976ba commit 9388e24
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2013-2020 Federico Iosue (federico@iosue.it)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package it.feio.android.omninotes.helpers;


import static org.junit.Assert.assertTrue;

import it.feio.android.omninotes.BaseAndroidTestCase;
import org.junit.Test;

public class GeocodeProviderBaseFactoryTest extends BaseAndroidTestCase {

@Test
public void checkHighAccuracyLocationProvider () {
boolean highAccuracyLocationProviderEnabled= GeocodeProviderBaseFactory.checkHighAccuracyLocationProvider(testContext);
assertTrue(highAccuracyLocationProviderEnabled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,36 @@

package it.feio.android.omninotes.helpers;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

import io.nlopez.smartlocation.location.LocationProvider;
import io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesWithFallbackProvider;
import it.feio.android.omninotes.R;
import it.feio.android.omninotes.utils.GeocodeHelper;

public class GeocodeProviderBaseFactory {

protected GeocodeProviderBaseFactory() {
protected GeocodeProviderBaseFactory () {
// hides public constructor
}

public static LocationProvider getProvider (Context context) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT&&android.os.Build.VERSION.SDK_INT <Build.VERSION_CODES.P)
{
if(getLocationMode(context)!=3)
{
Toast.makeText(context, "Please set Location mode to high accuracy to continue", Toast.LENGTH_SHORT).show();
context.startActivity((new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)));
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.P
&& checkHighAccuracyLocationProvider(context)) {
Toast.makeText(context, R.string.location_set_high_accuracy, Toast.LENGTH_SHORT).show();
context.startActivity((new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)));
}

return new LocationGooglePlayServicesWithFallbackProvider(context);
}
public static int getLocationMode(Context context) {
ContentResolver contentResolver=(ContentResolver)context.getContentResolver();
try {
return Settings.Secure.getInt(contentResolver, Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return -1;

public static boolean checkHighAccuracyLocationProvider (Context context) {
return GeocodeHelper.checkLocationProviderEnabled(context, LocationManager.GPS_PROVIDER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.TextUtils;
import io.nlopez.smartlocation.SmartLocation;
Expand Down Expand Up @@ -237,4 +238,13 @@ public static boolean areCoordinates (String string) {
Matcher m = p.matcher(string);
return m.matches();
}

/**
* Checks for location provider between {@link android.location.LocationManager#GPS_PROVIDER} etc...
*/
public static boolean checkLocationProviderEnabled(Context context, String provider) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(provider);
}

}
1 change: 1 addition & 0 deletions omniNotes/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<string name="delete_all_attachments">Delete all the attachments?</string>
<string name="gallery">Gallery</string>
<string name="location">Location</string>
<string name="location_set_high_accuracy">Please set Location mode to high accuracy to continue</string>
<string name="timestamp">Timestamp</string>
<string name="pushbullet">Pushbullet</string>
<string name="remove_location">Remove location?</string>
Expand Down

This file was deleted.

0 comments on commit 9388e24

Please sign in to comment.