manual location connected to sharing tab and uses bg task for sending cors

This commit is contained in:
Adam Domagalski 2018-11-26 00:46:36 +01:00
parent fca6cd936c
commit da944c37d5
4 changed files with 88 additions and 53 deletions

View File

@ -140,8 +140,10 @@ public class MapActivity extends BaseActivity
removeLocationButton.setOnClickListener(view -> { removeLocationButton.setOnClickListener(view -> {
Log.e(tag + "Manual", "manual coords sending stopped"); Log.e(tag + "Manual", "manual coords sending stopped");
stopBackgroundLocalizationTask();
// TODO to remove after BGserv // TODO to remove after BGserv
manualLocHandler.removeCallbacks(manualLocStatusChecker); // manualLocHandler.removeCallbacks(manualLocStatusChecker);
removeLocationButton.setVisibility(View.GONE); removeLocationButton.setVisibility(View.GONE);
Toast.makeText(MapActivity.this, "Your marker will disappear in next couple minutes", Toast.LENGTH_SHORT).show(); Toast.makeText(MapActivity.this, "Your marker will disappear in next couple minutes", Toast.LENGTH_SHORT).show();
@ -156,7 +158,9 @@ public class MapActivity extends BaseActivity
setToggleMapBoundsArea(); setToggleMapBoundsArea();
setOnMapLongClickListener(); if (PrefUtils.getIsTutor(this)){
setOnMapLongClickListener();
}
// addStaticLayer(); // addStaticLayer();
} }
@ -264,43 +268,44 @@ public class MapActivity extends BaseActivity
PrefUtils.getUserId(getApplicationContext()), PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext()) PrefUtils.getLocationLevel(getApplicationContext())
); );
PrefUtils.putManualLocation(this, latLng);
// TODO remove after BG sending // // TODO remove after BG sending
manualLocStatusChecker = () -> { // manualLocStatusChecker = () -> {
try { // try {
Log.e(tag + "Manual", "sending manual coords"); // Log.e(tag + "Manual", "sending manual coords");
// TODO ^^^ wrapper to removve // // TODO ^^^ wrapper to removve
disposable.add( // disposable.add(
coordinateService // coordinateService
.postCoordinate(droppedMarkercoordinate) // .postCoordinate(droppedMarkercoordinate)
.subscribeOn(Schedulers.io()) // .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) // .observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() { // .subscribeWith(new DisposableSingleObserver<Coordinate>() {
@SuppressLint("LongLogTag") // @SuppressLint("LongLogTag")
@Override // @Override
public void onSuccess(Coordinate coord) { // public void onSuccess(Coordinate coord) {
Log.e(tag + "POST", String.valueOf(coord)); // Log.e(tag + "POST", String.valueOf(coord));
} // }
//
@SuppressLint("LongLogTag") // @SuppressLint("LongLogTag")
@Override // @Override
public void onError(Throwable e) { // public void onError(Throwable e) {
//
Log.e(tag + "onError", e.getMessage()); // Log.e(tag + "onError", e.getMessage());
//
if (e instanceof HttpException) { // if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody(); // ResponseBody responseBody = ((HttpException) e).response().errorBody();
Log.e(tag + "onError", RestApiHelper.getErrorMessage(responseBody)); // Log.e(tag + "onError", RestApiHelper.getErrorMessage(responseBody));
//
} // }
} // }
})); // }));
// TODO \/\/\/\/\/ wrapper to removve // // TODO \/\/\/\/\/ wrapper to removve
} finally { // } finally {
manualLocHandler.postDelayed(manualLocStatusChecker, mInterval); // manualLocHandler.postDelayed(manualLocStatusChecker, mInterval);
} // }
}; // };
manualLocStatusChecker.run(); // manualLocStatusChecker.run();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {

View File

@ -105,7 +105,6 @@ public class SharingFragment extends PreferenceFragment {
locationLevelMapping.put(2, SharingLevel.EXACT.toString()); locationLevelMapping.put(2, SharingLevel.EXACT.toString());
locationLevelMapping.put(3, SharingLevel.MANUAL.toString()); locationLevelMapping.put(3, SharingLevel.MANUAL.toString());
statusMapping = new HashMap<Integer, String>(); statusMapping = new HashMap<Integer, String>();
statusMapping.put(0,"available"); statusMapping.put(0,"available");
statusMapping.put(1,"consultation"); statusMapping.put(1,"consultation");
@ -119,6 +118,7 @@ public class SharingFragment extends PreferenceFragment {
}); });
/** Sharing level list **/ /** Sharing level list **/
locationMode.setSummary(PrefUtils.getLocationLevel(getApplicationContext()));
locationMode.setOnPreferenceChangeListener((preference, newValue) -> { locationMode.setOnPreferenceChangeListener((preference, newValue) -> {
PrefUtils.storeLocationMode(getApplicationContext(),locationLevelMapping.get(Integer.parseInt((String) newValue))); PrefUtils.storeLocationMode(getApplicationContext(),locationLevelMapping.get(Integer.parseInt((String) newValue)));
if(PrefUtils.getLocationLevel(getApplicationContext()) == "manual"){ if(PrefUtils.getLocationLevel(getApplicationContext()) == "manual"){

View File

@ -28,6 +28,7 @@ import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.OnSuccessListener;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mapbox.geojson.Point; import com.mapbox.geojson.Point;
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;
@ -304,15 +305,20 @@ public class BackgroundLocalizationService extends Service {
@Override @Override
protected Object doInBackground(Object[] objects) { protected Object doInBackground(Object[] objects) {
if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.PRESENCE.toString())) { String locationLevel = PrefUtils.getLocationLevel(getApplicationContext());
if (locationLevel.equals(SharingLevel.PRESENCE.toString())) {
latitude = presenceLatitude; latitude = presenceLatitude;
longitude = presencelongitude; longitude = presencelongitude;
approximatedBuildingPart = presenceApproximatedName; approximatedBuildingPart = presenceApproximatedName;
} else if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.APPROXIMATED.toString())) { } else if (locationLevel.equals(SharingLevel.APPROXIMATED.toString())) {
List<Double> points = approximatedLocalization.getMiddlePointOfBuildingPart(approximatedBuildingPart); List<Double> points = approximatedLocalization.getMiddlePointOfBuildingPart(approximatedBuildingPart);
latitude = points.get(0); latitude = points.get(0);
longitude = points.get(1); longitude = points.get(1);
} else if (locationLevel.equals(SharingLevel.MANUAL.toString())) {
LatLng latLng = PrefUtils.getManualLocation(getApplicationContext());
latitude = latLng.getLatitude();
longitude = latLng.getLongitude();
} }
try { try {
@ -323,7 +329,7 @@ public class BackgroundLocalizationService extends Service {
approximatedBuildingPart, approximatedBuildingPart,
(PrefUtils.isStatusEnabled(getApplicationContext())) ? PrefUtils.getUserStatus(getApplicationContext()) : "", (PrefUtils.isStatusEnabled(getApplicationContext())) ? PrefUtils.getUserStatus(getApplicationContext()) : "",
PrefUtils.getUserId(getApplicationContext()), PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext()) locationLevel
); );
disposable.add( disposable.add(

View File

@ -5,6 +5,8 @@ import android.content.SharedPreferences;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.util.Log; import android.util.Log;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.activity.SharingFragment; import com.uam.wmi.findmytutor.activity.SharingFragment;
import com.auth0.android.jwt.Claim; import com.auth0.android.jwt.Claim;
import com.auth0.android.jwt.JWT; import com.auth0.android.jwt.JWT;
@ -20,11 +22,11 @@ public class PrefUtils {
return context.getSharedPreferences("com.uam.wmi.findmytutor_preferences", Context.MODE_PRIVATE); return context.getSharedPreferences("com.uam.wmi.findmytutor_preferences", Context.MODE_PRIVATE);
} }
public static void getAllKeys(Context context){ public static void getAllKeys(Context context) {
Map<String,?> keys = getSharedPreferences(context).getAll(); Map<String, ?> keys = getSharedPreferences(context).getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){ for (Map.Entry<String, ?> entry : keys.entrySet()) {
Log.d("map values",entry.getKey() + ": " + entry.getValue().toString()); Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
} }
} }
@ -54,14 +56,16 @@ public class PrefUtils {
return getSharedPreferences(context).getString("USER_ID", null); return getSharedPreferences(context).getString("USER_ID", null);
} }
public static boolean isStatusEnabled(Context context){ public static boolean isStatusEnabled(Context context) {
return getSharedPreferences(context).getBoolean("key_status_enabled",false); return getSharedPreferences(context).getBoolean("key_status_enabled", false);
} }
public static String getUserStatus(Context context) { public static String getUserStatus(Context context) {
return getSharedPreferences(context).getString("status_entry", "Available"); return getSharedPreferences(context).getString("status_entry", "Available");
} }
public static void storeStatus(Context context, String status){
public static void storeStatus(Context context, String status) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit(); SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("status_entry", status); editor.putString("status_entry", status);
editor.apply(); editor.apply();
@ -91,15 +95,17 @@ public class PrefUtils {
return getSharedPreferences(context).getBoolean("key_sharing_enabled", false); return getSharedPreferences(context).getBoolean("key_sharing_enabled", false);
} }
public static void storeEnableSharingLocalization(Context context,Boolean isChecked) { public static void storeEnableSharingLocalization(Context context, Boolean isChecked) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit(); SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("key_sharing_enabled", isChecked); editor.putBoolean("key_sharing_enabled", isChecked);
editor.apply(); editor.apply();
} }
public static String getLocationLevel(Context context){
public static String getLocationLevel(Context context) {
return getSharedPreferences(context).getString("location_mode", "exact"); return getSharedPreferences(context).getString("location_mode", "exact");
} }
public static void storeLocationMode(Context context, String mode){
public static void storeLocationMode(Context context, String mode) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit(); SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("location_mode", mode); editor.putString("location_mode", mode);
editor.apply(); editor.apply();
@ -150,4 +156,22 @@ public class PrefUtils {
editor.putBoolean("BACKGROUND_SERVICE_STATUS", status); editor.putBoolean("BACKGROUND_SERVICE_STATUS", status);
editor.apply(); editor.apply();
} }
public static void putManualLocation(Context context, LatLng latLng) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("location_mode", "manual");
editor.putBoolean("key_sharing_enabled", true);
editor.putLong("longitude_manual_location", Double.doubleToRawLongBits(latLng.getLongitude()));
editor.putLong("latitude_manual_location", Double.doubleToRawLongBits(latLng.getLatitude()));
editor.apply();
}
public static LatLng getManualLocation(Context context) {
return new LatLng(
Double.longBitsToDouble(getSharedPreferences(context).getLong("latitude_manual_location", 0)),
Double.longBitsToDouble(getSharedPreferences(context).getLong("longitude_manual_location", 0))
);
}
} }