diff --git a/.idea/misc.xml b/.idea/misc.xml index dc44dda..b0c7b20 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java index bed01f5..36f212a 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java @@ -53,6 +53,7 @@ import io.reactivex.schedulers.Schedulers; import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; 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.PrefUtils.storeBackgroundLocationStatus; @@ -200,7 +201,6 @@ public abstract class BaseActivity public void stopBackgroundLocalizationTask() { Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); stopIntent.putExtra("request_stop", true); - Log.e("Localization", "JEstem w stop BG"); stopService(stopIntent); @@ -210,6 +210,7 @@ public abstract class BaseActivity checkPermissions(); Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); + startIntent.putExtra("notify_interval", onlineBackgroundLocationInterval); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { startForegroundService(startIntent); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java index b1e9a32..e0d4309 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java @@ -333,7 +333,7 @@ public class MapActivity extends BaseActivity final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); - EditText modalUserInput = view.findViewById(R.id.feedback_input); + EditText modalUserInput = view.findViewById(R.id.manual_input); alertDialog.setOnShowListener(dialogInterface -> { Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java index 1eee441..5755ab7 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java @@ -153,6 +153,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere /** Sharing level list **/ locationMode.setOnPreferenceChangeListener((preference, newValue) -> { + ((MapActivity) getActivity()).stopBackgroundLocalizationTask(); + ((MapActivity) getActivity()).startBackgroundLocalizationTask(); PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue))); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java index 1323d62..827c589 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java @@ -11,11 +11,13 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Color; import android.location.Location; +import android.location.LocationListener; import android.location.LocationManager; import android.os.AsyncTask; import android.os.Build; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v4.app.NotificationCompat; @@ -23,6 +25,9 @@ import android.util.Log; import com.annimon.stream.Stream; import com.google.android.gms.location.FusedLocationProviderClient; +import com.google.android.gms.location.LocationCallback; +import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationResult; import com.google.android.gms.location.LocationServices; import com.mapbox.geojson.Point; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -54,10 +59,11 @@ import static java.lang.String.valueOf; public class BackgroundLocalizationService extends Service { private static final String TAG = "MyLocationService"; - private static long notify_interval = onlineBackgroundLocationInterval; - private static long notify_interval_inside_building = onlineBackgroundLocationInterval; - private static long notify_interval_outside_building = offlineBackgroundLocationInterval; + private static Integer notify_interval = onlineBackgroundLocationInterval; + private static Integer notify_interval_inside_building = onlineBackgroundLocationInterval; + private static Integer notify_interval_outside_building = offlineBackgroundLocationInterval; private static int coordinatesHistoryLength = 5; + private Boolean highAccuracyMode; private Location mLastLocation; private Boolean stopService = false; private ArrayList providers = new ArrayList(); @@ -66,6 +72,10 @@ public class BackgroundLocalizationService extends Service { private Runnable mStatusChecker; private FusedLocationProviderClient mFusedLocationClient; private Location fakeLoc = null; + private LocationRequest mLocationRequest; + private Location mCurrentLocation; + private LocationCallback mLocationCallback; + private LocationListener mLocationListener; public BackgroundLocalizationService() { providers.add(LocationManager.GPS_PROVIDER); @@ -90,7 +100,9 @@ public class BackgroundLocalizationService extends Service { if (intent != null) { stopService = intent.getBooleanExtra("request_stop", false); + notify_interval = intent.getIntExtra("notify_interval", onlineBackgroundLocationInterval); } + if (stopService) { storeBackgroundLocationStatus(getApplication(), false); stopForeground(true); @@ -101,6 +113,20 @@ public class BackgroundLocalizationService extends Service { return START_STICKY; } + private void createLocationCallback() { + mLocationCallback = new LocationCallback() { + @Override + public void onLocationResult(LocationResult locationResult) { + super.onLocationResult(locationResult); + if (locationResult != null) { + mCurrentLocation = locationResult.getLastLocation(); + sendCoordinateToBackend(mCurrentLocation); + changeBackgroundMode(); + } + } + }; + } + @Override public void onCreate() { Log.e(TAG, "onCreate"); @@ -114,20 +140,14 @@ public class BackgroundLocalizationService extends Service { startForeground(1001, notification); } - - if (!stopService) { + if (!stopService && !PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) { + createFusedLocationClient(); + }else if (!stopService && + PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) { mStatusChecker = () -> { try { - if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) { - sendCoordinateToBackend(fakeLoc); - } else { - mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); - getLocalizationFromListeners(); - changeBackgroundMode(); - } + sendCoordinateToBackend(fakeLoc); } finally { - mFusedLocationClient = null; - destroyLocationListeners(); mHandler.postDelayed(mStatusChecker, notify_interval); } }; @@ -136,8 +156,36 @@ public class BackgroundLocalizationService extends Service { } } + private void createFusedLocationClient() { + Integer saveMode = Long.valueOf(notify_interval).compareTo(Long.valueOf(offlineBackgroundLocationInterval)); + + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + return; + } + + mLocationRequest = new LocationRequest(); + + if (saveMode != 0) { + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + } else { + mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); + } + + mLocationRequest.setFastestInterval(notify_interval); + mLocationRequest.setInterval(notify_interval); + + createLocationCallback(); + + if (!stopService) { + mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); + mFusedLocationClient.requestLocationUpdates(mLocationRequest, + mLocationCallback, Looper.getMainLooper()); + } + } + private void changeBackgroundMode() { if (coordinatesHistory.size() > 4) { + Integer prevInterval = notify_interval; Boolean shouldExtendTimeInterval = Stream.of(coordinatesHistory) .map(MapUtils::checkIfCoordinateIsValid).takeWhile(s -> !s).toList().size() == coordinatesHistory.size(); @@ -150,6 +198,10 @@ public class BackgroundLocalizationService extends Service { notify_interval = notify_interval_inside_building; } + Integer changedMode = Long.valueOf(prevInterval).compareTo(Long.valueOf(notify_interval)); + if (changedMode != 0) { + updateListeners(); + } } } @@ -173,30 +225,6 @@ public class BackgroundLocalizationService extends Service { 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) { new Task(location).execute(); } @@ -211,7 +239,41 @@ public class BackgroundLocalizationService extends Service { } private void destroyLocationListeners() { + if (mFusedLocationClient != null) { + mFusedLocationClient.removeLocationUpdates(mLocationCallback); + mFusedLocationClient = null; + mLocationCallback = null; + mLocationRequest = null; + } + } + private void updateListeners() { + if (mFusedLocationClient != null) { + mFusedLocationClient.removeLocationUpdates(mLocationCallback) + .addOnCompleteListener(task -> { + restartService(); + mFusedLocationClient = null; + mLocationCallback = null; + mLocationRequest = null; + }); + } + } + + private void restartService() { + + Intent stopIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); + stopIntent.putExtra("request_stop", true); + + stopService(stopIntent); + + Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); + startIntent.putExtra("notify_interval", notify_interval); + + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { + startForegroundService(startIntent); + } else { + startService(startIntent); + } } @@ -260,6 +322,12 @@ public class BackgroundLocalizationService extends Service { approximatedBuildingPart = PrefUtils.getManualLocationApproximation(getApplicationContext()); } + Location fakeLoc = new Location(""); + + fakeLoc.setLatitude(latitude); + fakeLoc.setLongitude(longitude); + coordinatesHistory.add(fakeLoc); + try { Coordinate coordinate = new Coordinate( latitude, @@ -277,7 +345,6 @@ public class BackgroundLocalizationService extends Service { .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableCompletableObserver() { - @Override public void onComplete() { Log.e(TAG, "CoordinateSuccess"); @@ -285,9 +352,7 @@ public class BackgroundLocalizationService extends Service { @Override public void onError(Throwable e) { - Log.e(TAG, "onErr" + valueOf(e)); - } })); } catch (IllegalArgumentException e) { diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/Const.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/Const.java index 48fd35f..0590a2f 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/Const.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/Const.java @@ -8,7 +8,7 @@ import java.util.List; public class Const { public final static String BASE_URL = "https://s416084.projektstudencki.pl/master/"; public final static Integer onlineBackgroundLocationInterval = 7000; - public final static Integer offlineBackgroundLocationInterval = 36000; + public final static Integer offlineBackgroundLocationInterval = 360000; public final static Integer defaultMapZoom = 17; public final static Integer searchMapZoom = 13; public final static Double presenceLatitude = 52.466365; diff --git a/app/src/main/res/layout/location_modal.xml b/app/src/main/res/layout/location_modal.xml index 51949bd..a4af220 100644 --- a/app/src/main/res/layout/location_modal.xml +++ b/app/src/main/res/layout/location_modal.xml @@ -24,14 +24,14 @@ android:textStyle="normal" /> Błąd sieci! Brak wyników! + Nadaj nazwę tej lokalizacji. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc7f5bc..6860e0d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -340,5 +340,6 @@ Only online users Network Error ! Search response is empty! + Insert a name for this localization.