Add save battery mode

This commit is contained in:
Mieszko Wrzeszczyński 2019-01-03 19:34:55 +01:00
parent 88f263217a
commit ea0d6e1e96
8 changed files with 54 additions and 58 deletions

View File

@ -53,6 +53,7 @@ import io.reactivex.schedulers.Schedulers;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
import static com.uam.wmi.findmytutor.utils.Const.defaultMapZoom; import static com.uam.wmi.findmytutor.utils.Const.defaultMapZoom;
import static com.uam.wmi.findmytutor.utils.Const.onlineBackgroundLocationInterval;
import static com.uam.wmi.findmytutor.utils.Const.searchMapZoom; import static com.uam.wmi.findmytutor.utils.Const.searchMapZoom;
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus; import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
@ -200,7 +201,6 @@ public abstract class BaseActivity
public void stopBackgroundLocalizationTask() { public void stopBackgroundLocalizationTask() {
Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
stopIntent.putExtra("request_stop", true); stopIntent.putExtra("request_stop", true);
Log.e("Localization", "JEstem w stop BG");
stopService(stopIntent); stopService(stopIntent);
@ -210,6 +210,7 @@ public abstract class BaseActivity
checkPermissions(); checkPermissions();
Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startIntent.putExtra("notify_interval", onlineBackgroundLocationInterval);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
startForegroundService(startIntent); startForegroundService(startIntent);

View File

@ -333,7 +333,7 @@ public class MapActivity extends BaseActivity
final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
EditText modalUserInput = view.findViewById(R.id.feedback_input); EditText modalUserInput = view.findViewById(R.id.manual_input);
alertDialog.setOnShowListener(dialogInterface -> { alertDialog.setOnShowListener(dialogInterface -> {
Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);

View File

@ -153,6 +153,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
/** Sharing level list **/ /** Sharing level list **/
locationMode.setOnPreferenceChangeListener((preference, newValue) -> { locationMode.setOnPreferenceChangeListener((preference, newValue) -> {
((MapActivity) getActivity()).stopBackgroundLocalizationTask();
((MapActivity) getActivity()).startBackgroundLocalizationTask();
PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue))); PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue)));

View File

@ -29,13 +29,11 @@ import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult; import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationServices;
import com.mapbox.geojson.Point; import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.model.Coordinate; import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.utils.ApproximatedLocalization; import com.uam.wmi.findmytutor.utils.ApproximatedLocalization;
import com.uam.wmi.findmytutor.utils.Const;
import com.uam.wmi.findmytutor.utils.MapUtils; import com.uam.wmi.findmytutor.utils.MapUtils;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.SharingLevel; import com.uam.wmi.findmytutor.utils.SharingLevel;
@ -61,11 +59,11 @@ import static java.lang.String.valueOf;
public class BackgroundLocalizationService extends Service { public class BackgroundLocalizationService extends Service {
private static final String TAG = "MyLocationService"; private static final String TAG = "MyLocationService";
private static long notify_interval; private static Integer notify_interval = onlineBackgroundLocationInterval;
private Boolean highAccuracyMode; private static Integer notify_interval_inside_building = onlineBackgroundLocationInterval;
private static long notify_interval_inside_building = onlineBackgroundLocationInterval; private static Integer notify_interval_outside_building = offlineBackgroundLocationInterval;
private static long notify_interval_outside_building = offlineBackgroundLocationInterval;
private static int coordinatesHistoryLength = 5; private static int coordinatesHistoryLength = 5;
private Boolean highAccuracyMode;
private Location mLastLocation; private Location mLastLocation;
private Boolean stopService = false; private Boolean stopService = false;
private ArrayList<String> providers = new ArrayList<String>(); private ArrayList<String> providers = new ArrayList<String>();
@ -77,7 +75,6 @@ public class BackgroundLocalizationService extends Service {
private LocationRequest mLocationRequest; private LocationRequest mLocationRequest;
private Location mCurrentLocation; private Location mCurrentLocation;
private LocationCallback mLocationCallback; private LocationCallback mLocationCallback;
private Looper myLooper;
private LocationListener mLocationListener; private LocationListener mLocationListener;
public BackgroundLocalizationService() { public BackgroundLocalizationService() {
@ -103,7 +100,7 @@ public class BackgroundLocalizationService extends Service {
if (intent != null) { if (intent != null) {
stopService = intent.getBooleanExtra("request_stop", false); stopService = intent.getBooleanExtra("request_stop", false);
notify_interval = intent.getLongExtra("notify_interval", onlineBackgroundLocationInterval); notify_interval = intent.getIntExtra("notify_interval", onlineBackgroundLocationInterval);
} }
if (stopService) { if (stopService) {
@ -124,8 +121,7 @@ public class BackgroundLocalizationService extends Service {
if (locationResult != null) { if (locationResult != null) {
mCurrentLocation = locationResult.getLastLocation(); mCurrentLocation = locationResult.getLastLocation();
sendCoordinateToBackend(mCurrentLocation); sendCoordinateToBackend(mCurrentLocation);
coordinatesHistory.add(mCurrentLocation); changeBackgroundMode();
changeBackgroundMode();
} }
} }
}; };
@ -144,12 +140,24 @@ public class BackgroundLocalizationService extends Service {
startForeground(1001, notification); startForeground(1001, notification);
} }
createFusedLocationClient(); if (!stopService && !PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) {
createFusedLocationClient();
}else if (!stopService &&
PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) {
mStatusChecker = () -> {
try {
sendCoordinateToBackend(fakeLoc);
} finally {
mHandler.postDelayed(mStatusChecker, notify_interval);
}
};
AsyncTask.execute(mStatusChecker);
}
} }
private void createFusedLocationClient(){ private void createFusedLocationClient() {
Integer saveMode = Long.valueOf(notify_interval).compareTo(Long.valueOf(offlineBackgroundLocationInterval)); Integer saveMode = Long.valueOf(notify_interval).compareTo(Long.valueOf(offlineBackgroundLocationInterval));
myLooper = Looper.myLooper();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return; return;
@ -157,9 +165,9 @@ public class BackgroundLocalizationService extends Service {
mLocationRequest = new LocationRequest(); mLocationRequest = new LocationRequest();
if(saveMode != 0){ if (saveMode != 0) {
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}else{ } else {
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
} }
@ -171,13 +179,13 @@ public class BackgroundLocalizationService extends Service {
if (!stopService) { if (!stopService) {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, myLooper); mLocationCallback, Looper.getMainLooper());
} }
} }
private void changeBackgroundMode() { private void changeBackgroundMode() {
if (coordinatesHistory.size() > 4) { if (coordinatesHistory.size() > 4) {
Long prevInterval = notify_interval; Integer prevInterval = notify_interval;
Boolean shouldExtendTimeInterval = Stream.of(coordinatesHistory) Boolean shouldExtendTimeInterval = Stream.of(coordinatesHistory)
.map(MapUtils::checkIfCoordinateIsValid).takeWhile(s -> !s).toList().size() == coordinatesHistory.size(); .map(MapUtils::checkIfCoordinateIsValid).takeWhile(s -> !s).toList().size() == coordinatesHistory.size();
@ -191,8 +199,7 @@ public class BackgroundLocalizationService extends Service {
} }
Integer changedMode = Long.valueOf(prevInterval).compareTo(Long.valueOf(notify_interval)); Integer changedMode = Long.valueOf(prevInterval).compareTo(Long.valueOf(notify_interval));
if (changedMode != 0) {
if(changedMode != 0){
updateListeners(); updateListeners();
} }
} }
@ -218,28 +225,6 @@ public class BackgroundLocalizationService extends Service {
startForeground(2, notification); startForeground(2, notification);
} }
private void getLocalizationFromListeners() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mFusedLocationClient.getLastLocation().addOnSuccessListener(
location -> {
if (location != null) {
mLastLocation = location;
coordinatesHistory.add(location);
sendCoordinateToBackend(location);
}
});
}
private void sendCoordinateToBackend(Location location) { private void sendCoordinateToBackend(Location location) {
new Task(location).execute(); new Task(location).execute();
} }
@ -274,21 +259,21 @@ public class BackgroundLocalizationService extends Service {
} }
} }
private void restartService(){ private void restartService() {
Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
stopIntent.putExtra("request_stop", true); stopIntent.putExtra("request_stop", true);
stopService(stopIntent); stopService(stopIntent);
Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startIntent.putExtra("notify_interval", notify_interval); startIntent.putExtra("notify_interval", notify_interval);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
startForegroundService(startIntent); startForegroundService(startIntent);
} else { } else {
startService(startIntent); startService(startIntent);
} }
} }
@ -337,6 +322,12 @@ public class BackgroundLocalizationService extends Service {
approximatedBuildingPart = PrefUtils.getManualLocationApproximation(getApplicationContext()); approximatedBuildingPart = PrefUtils.getManualLocationApproximation(getApplicationContext());
} }
Location fakeLoc = new Location("");
fakeLoc.setLatitude(latitude);
fakeLoc.setLongitude(longitude);
coordinatesHistory.add(fakeLoc);
try { try {
Coordinate coordinate = new Coordinate( Coordinate coordinate = new Coordinate(
latitude, latitude,

View File

@ -7,8 +7,8 @@ import java.util.List;
public class Const { public class Const {
public final static String BASE_URL = "https://s416084.projektstudencki.pl/master/"; public final static String BASE_URL = "https://s416084.projektstudencki.pl/master/";
public final static Integer onlineBackgroundLocationInterval = 7000; public final static Integer onlineBackgroundLocationInterval = 7000;
public final static Integer offlineBackgroundLocationInterval = 14000; public final static Integer offlineBackgroundLocationInterval = 360000;
public final static Integer defaultMapZoom = 17; public final static Integer defaultMapZoom = 17;
public final static Integer searchMapZoom = 13; public final static Integer searchMapZoom = 13;
public final static Double presenceLatitude = 52.466365; public final static Double presenceLatitude = 52.466365;

View File

@ -24,14 +24,14 @@
android:textStyle="normal" /> android:textStyle="normal" />
<EditText <EditText
android:id="@+id/feedback_input" android:id="@+id/manual_input"
android:layout_width="315dp" android:layout_width="315dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" android:layout_marginTop="50dp"
android:ems="10" android:ems="10"
android:hint="@string/modal_feedback_hint" android:hint="@string/modal_manual_hint"
android:maxLength="30" android:maxLength="30"
android:maxLines="1" android:maxLines="1"
android:requiresFadingEdge="vertical" android:requiresFadingEdge="vertical"

View File

@ -259,6 +259,7 @@
<string name="network_err">Błąd sieci!</string> <string name="network_err">Błąd sieci!</string>
<string name="search_null">Brak wyników!</string> <string name="search_null">Brak wyników!</string>
<string name="modal_manual_hint">Nadaj nazwę tej lokalizacji.</string>
</resources> </resources>

View File

@ -340,5 +340,6 @@
<string name="only_online_users">Only online users</string> <string name="only_online_users">Only online users</string>
<string name="network_err">Network Error !</string> <string name="network_err">Network Error !</string>
<string name="search_null">Search response is empty!</string> <string name="search_null">Search response is empty!</string>
<string name="modal_manual_hint">Insert a name for this localization.</string>
</resources> </resources>