First attempt

This commit is contained in:
Mieszko Wrzeszczyński 2018-12-05 00:45:16 +01:00
parent 5fc5140866
commit 5e51be5d0b
3 changed files with 62 additions and 26 deletions

View File

@ -77,6 +77,7 @@ public abstract class BaseActivity
private Fragment userListFragment; private Fragment userListFragment;
private ActiveFragment activeFragment = ActiveFragment.NONE; private ActiveFragment activeFragment = ActiveFragment.NONE;
private Fragment activeBottomMenu = null;
private SearchView searchView; private SearchView searchView;
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
@ -351,7 +352,7 @@ public abstract class BaseActivity
} else if (itemId == R.id.nav_user_list) { } else if (itemId == R.id.nav_user_list) {
loadUserListFragment(); loadUserListFragment();
} }
selectBottomNavigationBarItem(itemId);
}, 300); }, 300);
return true; return true;
@ -363,6 +364,7 @@ public abstract class BaseActivity
FragmentTransaction ft = getFragmentManager().beginTransaction(); FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.activity_content, sharingFragment); ft.replace(R.id.activity_content, sharingFragment);
ft.commit(); ft.commit();
} }
private void loadUserListFragment() { private void loadUserListFragment() {
@ -372,8 +374,8 @@ public abstract class BaseActivity
FragmentTransaction ft = getFragmentManager().beginTransaction(); FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.activity_content, userListFragment); ft.replace(R.id.activity_content, userListFragment);
ft.commit(); ft.commit();
}
}
private void updateNavigationBarState() { private void updateNavigationBarState() {
int actionId = getNavigationMenuItemId(); int actionId = getNavigationMenuItemId();
@ -385,9 +387,6 @@ public abstract class BaseActivity
item.setChecked(true); item.setChecked(true);
} }
abstract int getNavigationMenuItemId(); abstract int getNavigationMenuItemId();
abstract int getContentViewId(); abstract int getContentViewId();
} }

View File

@ -23,6 +23,7 @@ import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import com.annimon.stream.Stream;
import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationServices;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
@ -36,16 +37,21 @@ import com.uam.wmi.findmytutor.utils.RestApiHelper;
import com.uam.wmi.findmytutor.utils.SharingLevel; import com.uam.wmi.findmytutor.utils.SharingLevel;
import com.uam.wmi.findmytutor.utils.MapUtils; import com.uam.wmi.findmytutor.utils.MapUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.queue.CircularFifoQueue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import timber.log.Timber;
import static com.uam.wmi.findmytutor.utils.Consts.presenceApproximatedName; import static com.uam.wmi.findmytutor.utils.Consts.presenceApproximatedName;
import static com.uam.wmi.findmytutor.utils.Consts.presenceLatitude; import static com.uam.wmi.findmytutor.utils.Consts.presenceLatitude;
@ -59,6 +65,10 @@ public class BackgroundLocalizationService extends Service {
private static final float LOCATION_DISTANCE = 5f; private static final float LOCATION_DISTANCE = 5f;
public static String str_receiver = "background.location.broadcast"; public static String str_receiver = "background.location.broadcast";
private static long notify_interval = 10000; private static long notify_interval = 10000;
private static long notify_interval_inside_building = 10000;
private static long notify_interval_outside_building = 20000;
//private static long notify_interval_outside_building = 900000;
private CircularFifoQueue<Location> coordinatesHistory = new CircularFifoQueue<Location>(5);
Location mLastLocation; Location mLastLocation;
Boolean stopService = false; Boolean stopService = false;
Intent intent; Intent intent;
@ -82,7 +92,6 @@ public class BackgroundLocalizationService extends Service {
new LocationListener(LocationManager.NETWORK_PROVIDER), new LocationListener(LocationManager.NETWORK_PROVIDER),
new LocationListener(LocationManager.PASSIVE_PROVIDER) new LocationListener(LocationManager.PASSIVE_PROVIDER)
}; };
} }
@Override @Override
@ -146,22 +155,41 @@ public class BackgroundLocalizationService extends Service {
} }
if (!stopService) { if (!stopService) {
mStatusChecker = () -> { mStatusChecker = () -> {
try { try {
fn_getlocation(); fn_getlocation();
changeBackgroundMode();
} finally { } finally {
mHandler.postDelayed(mStatusChecker, notify_interval); mHandler.postDelayed(mStatusChecker, notify_interval);
} }
}; };
AsyncTask.execute(mStatusChecker); AsyncTask.execute(mStatusChecker);
}
} }
private void changeBackgroundMode() {
Predicate<Boolean> p2 = v -> !v;
if(coordinatesHistory.size() > 4){
Boolean shouldExtendTimeInterval = Stream.of(coordinatesHistory)
.map(MapUtils::checkIfCoordinateIsValid).takeWhile( s-> !s).toList().size() == 5;
if(shouldExtendTimeInterval){
notify_interval = notify_interval_outside_building;
}else{
notify_interval = notify_interval_inside_building;
} }
}
Log.e("COORD_HISTORY", String.valueOf(Stream.of(coordinatesHistory)
.map(MapUtils::checkIfCoordinateIsValid).toList()));
Log.e("COORD_HISTORY", String.valueOf(coordinatesHistory));
Log.e("NEW INTERVAL", String.valueOf(notify_interval));
}
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() { private void startMyOwnForeground() {
@ -198,8 +226,20 @@ public class BackgroundLocalizationService extends Service {
List<String> providers1 = mLocationManager.getProviders(true); List<String> providers1 = mLocationManager.getProviders(true);
Location bestLocation = null; Location bestLocation = null;
AtomicReference<Boolean> triggerAnotherLocationListener = new AtomicReference<>(false);
mFusedLocationClient.getLastLocation().addOnSuccessListener(
location -> {
if (location != null) {
mLastLocation = location;
coordinatesHistory.add(location);
fn_update(location);
}
triggerAnotherLocationListener.set(true);
});
if(triggerAnotherLocationListener.get()){
for (String provider : providers1) { for (String provider : providers1) {
Location location = mLocationManager.getLastKnownLocation(provider); Location location = mLocationManager.getLastKnownLocation(provider);
@ -209,18 +249,15 @@ public class BackgroundLocalizationService extends Service {
if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) { if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = location; bestLocation = location;
} }
} }
coordinatesHistory.add(bestLocation);
Log.e("Best localization:", String.valueOf(bestLocation)); Log.e("Best localization:", String.valueOf(bestLocation));
mFusedLocationClient.getLastLocation().addOnSuccessListener(
location -> {
if (location != null) {
mLastLocation = location;
fn_update(location);
} }
});
} }
private void fn_update(Location location) { private void fn_update(Location location) {
@ -333,6 +370,8 @@ public class BackgroundLocalizationService extends Service {
approximatedBuildingPart = PrefUtils.getManualLocationApproximation(getApplicationContext()); approximatedBuildingPart = PrefUtils.getManualLocationApproximation(getApplicationContext());
} }
try { try {
Coordinate coordinate = new Coordinate( Coordinate coordinate = new Coordinate(
latitude, latitude,

View File

@ -44,8 +44,6 @@ public class ManualLocationUtils {
activityContext = context; activityContext = context;
} }
private void handleError(Throwable error) { private void handleError(Throwable error) {
if (error instanceof HttpException) { if (error instanceof HttpException) {