From 66dba76ed4a6042cb3c2aa43c1e5966139e2eda8 Mon Sep 17 00:00:00 2001 From: Adam Domagalski Date: Tue, 27 Nov 2018 16:12:02 +0100 Subject: [PATCH 1/3] istutor bugifx map --- .../wmi/findmytutor/activity/MapActivity.java | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) 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 17242f2..4d9e06e 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 @@ -95,11 +95,12 @@ public class MapActivity extends BaseActivity private int bearingParam = 180; private int tiltParam = 30; private String myId; + private boolean isTutor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + isTutor = PrefUtils.getIsTutor(this); myId = PrefUtils.getUserId(getApplicationContext()); coordinateService = ApiClient.getClient(getApplicationContext()) @@ -136,8 +137,9 @@ public class MapActivity extends BaseActivity mapboxMap.setOnMarkerClickListener(marker -> { String id = markerUserHash.get(marker.getId()); + String locationLevel = PrefUtils.getLocationLevel(getApplicationContext()); - if (id.equals(myId)) { + if (id.equals(myId) && (locationLevel.equals(SharingLevel.MANUAL.toString()) || locationLevel.equals(SharingLevel.PREDEFINED.toString()))) { selectLocationButton.setVisibility(View.GONE); removeLocationButton.setVisibility(View.VISIBLE); @@ -157,8 +159,7 @@ public class MapActivity extends BaseActivity }); setToggleMapBoundsArea(); - - if (PrefUtils.getIsTutor(this)) { + if (isTutor) { setOnMapLongClickListener(); } } @@ -280,7 +281,7 @@ public class MapActivity extends BaseActivity // PrefUtils.getUserId(getApplicationContext()), // PrefUtils.getLocationLevel(getApplicationContext()) // ); - manualLocationUtils.showLocationDialog("Name the location",latLng); + manualLocationUtils.showLocationDialog("Name the location", latLng); handleBackgroundTaskLifeCycle(); } catch (IllegalArgumentException e) { @@ -364,7 +365,7 @@ public class MapActivity extends BaseActivity Marker marker = markerHash.get(id).getMarker(); Boolean showModalAfterAnimation = false; - if(marker.isInfoWindowShown()){ + if (marker.isInfoWindowShown()) { marker.hideInfoWindow(); showModalAfterAnimation = true; } @@ -380,12 +381,11 @@ public class MapActivity extends BaseActivity markerAnimator.start(); - coordsMap.put(id, element); marker.setPosition(toDestination); - if(showModalAfterAnimation){ + if (showModalAfterAnimation) { mapboxMap.selectMarker(marker); } @@ -393,8 +393,6 @@ public class MapActivity extends BaseActivity } - - } else { //Add new marker coordsMap.put(id, element); @@ -435,18 +433,6 @@ public class MapActivity extends BaseActivity // For next fetch previousCoordsIds.clear(); previousCoordsIds.addAll(currentCoordsIds); - - - for (Coordinate coordinate : coordsMap.values()) { - // 300000 = 5mins - if ((System.currentTimeMillis() - coordinate.getTimeStamp()) > (long) 300000) { - String id = coordinate.getUserId(); - Marker markerToRemove = markerHash.get(id).getMarker(); - markerHash.remove(id); - coordsMap.remove(id); - mapboxMap.removeMarker(markerToRemove); - } - } } @Override @@ -463,7 +449,7 @@ public class MapActivity extends BaseActivity if (sharingLevel.equals(SharingLevel.APPROXIMATED.toString())) { defaultIcon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.approximate_localization_marker); } else if (sharingLevel.equals(SharingLevel.MANUAL.toString()) || - sharingLevel.equals(SharingLevel.PREDEFINED.toString()) ) { + sharingLevel.equals(SharingLevel.PREDEFINED.toString())) { defaultIcon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.manual_localization_marker); } else if (id.equals(myId)) { defaultIcon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.my_marker); @@ -649,7 +635,7 @@ public class MapActivity extends BaseActivity for (MapMarker marker : markersToSet) { marker.getMarker().setIcon(markedMarker); - if(markersToSet.size() == 1) + if (markersToSet.size() == 1) mapboxMap.selectMarker(marker.getMarker()); } From ab74f0b7e14dd6a4353d36259c34afda90a6f458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Tue, 27 Nov 2018 19:52:36 +0100 Subject: [PATCH 2/3] Fix manual pop up windows --- .../findmytutor/activity/BaseActivity.java | 6 +- .../wmi/findmytutor/activity/MapActivity.java | 188 +++++++++++++++--- .../findmytutor/activity/SharingFragment.java | 66 ++---- .../utils/ManualLocationUtils.java | 109 +++------- app/src/main/res/layout/location_modal.xml | 3 +- app/src/main/res/layout/pref_sharing.xml | 1 - app/src/main/res/values-pl/strings.xml | 2 +- app/src/main/res/values/strings.xml | 3 + 8 files changed, 202 insertions(+), 176 deletions(-) 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 b8d5ea3..823a869 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 @@ -202,13 +202,11 @@ public abstract class BaseActivity } public void handleBackgroundTaskLifeCycle() { - Boolean shouldServiceRun = PrefUtils.isEnableSharingLocalization(getApplicationContext()) - && !PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext()); + Boolean shouldServiceRun = PrefUtils.isEnableSharingLocalization(getApplicationContext()); if (shouldServiceRun) { startBackgroundLocalizationTask(); - } else if (PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext()) && - !PrefUtils.isEnableSharingLocalization(getApplicationContext())) { + } else { stopBackgroundLocalizationTask(); } } 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 4d9e06e..4a8a7e2 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 @@ -10,11 +10,13 @@ import android.os.Bundle; import android.os.Handler; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; +import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.Button; +import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; @@ -42,6 +44,7 @@ import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel; import com.uam.wmi.findmytutor.model.User; import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.service.CoordinateService; +import com.uam.wmi.findmytutor.service.PredefinedStatusesService; import com.uam.wmi.findmytutor.service.UserService; import com.uam.wmi.findmytutor.utils.FeedbackUtils; import com.uam.wmi.findmytutor.utils.ManualLocationUtils; @@ -115,7 +118,7 @@ public class MapActivity extends BaseActivity } finally { mHandler.postDelayed(mStatusChecker, mInterval); } - manualLocationUtils = new ManualLocationUtils(MapActivity.this); + }; selectLocationButton = findViewById(R.id.select_location_button); @@ -127,6 +130,7 @@ public class MapActivity extends BaseActivity //start background task handleBackgroundTaskLifeCycle(); + manualLocationUtils = new ManualLocationUtils(MapActivity.this); } @Override @@ -144,7 +148,6 @@ public class MapActivity extends BaseActivity removeLocationButton.setVisibility(View.VISIBLE); removeLocationButton.setOnClickListener(view -> { - Log.e(tag + "Manual", "manual coords sending stopped"); stopBackgroundLocalizationTask(); removeLocationButton.setVisibility(View.GONE); @@ -159,6 +162,7 @@ public class MapActivity extends BaseActivity }); setToggleMapBoundsArea(); + if (isTutor) { setOnMapLongClickListener(); } @@ -245,20 +249,26 @@ public class MapActivity extends BaseActivity } - private void setOnMapLongClickListener() { + /* private void setOnMapLongClickListener() { mapboxMap.addOnMapLongClickListener((LatLng latLng) -> { selectLocationButton.setVisibility(View.VISIBLE); removeLocationButton.setVisibility(View.GONE); - Icon icon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.my_marker); if (tmpLocalMarker == null) { - tmpLocalMarker = mapboxMap.addMarker(new MarkerOptions() - .position(latLng) - .icon(icon) - .title("My Loc") - .setSnippet("Snipecik")); + String sharingLevel = SharingLevel.MANUAL.toString(); + Icon defaultIcon = getMapIcon(sharingLevel, myId); + + MarkerOptions markerOptions = new MarkerOptions() + .setIcon(defaultIcon) + .position(latLng); + + tmpLocalMarker = mapboxMap.addMarker(markerOptions); + MapMarker marker = new MapMarker(tmpLocalMarker, markerOptions, defaultIcon, sharingLevel); + + markerHash.put(myId, marker); + updateUserHashMap(myId, marker.getMarker().getId()); } else { ValueAnimator markerAnimator = ObjectAnimator.ofObject(tmpLocalMarker, "position", @@ -269,36 +279,13 @@ public class MapActivity extends BaseActivity selectLocationButton.setOnClickListener((View view) -> { if (tmpLocalMarker != null) { - // Toast instructing user to tap on the mapboxMap - // TODO PUT MANUAL CORD try { -// droppedMarkercoordinate = new Coordinate( -// latLng.getLatitude(), -// latLng.getLongitude(), -// latLng.getAltitude(), -// "approx", -// PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()), -// PrefUtils.getUserId(getApplicationContext()), -// PrefUtils.getLocationLevel(getApplicationContext()) -// ); manualLocationUtils.showLocationDialog("Name the location", latLng); - handleBackgroundTaskLifeCycle(); - + startBackgroundLocalizationTask(); } catch (IllegalArgumentException e) { Timber.e(String.valueOf(e)); } -// Toast.makeText( -// MapActivity.this, -// "Manual Locations selected!" + latLng, -// Toast.LENGTH_LONG -// ).show(); -// LayoutInflater layoutInflaterAndroid = LayoutInflater.from(MapActivity.this); -// View dialogView = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null); -// AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(MapActivity.this ); -// alertDialogBuilderUserInput.setView(dialogView).setPositiveButton("dupa", null); -// final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); -// alertDialog.show(); selectLocationButton.setVisibility(View.GONE); mapboxMap.removeMarker(tmpLocalMarker); tmpLocalMarker = null; @@ -306,6 +293,141 @@ public class MapActivity extends BaseActivity }); }); + }*/ + + private void setOnMapLongClickListener() { + + mapboxMap.addOnMapLongClickListener((LatLng latLng) -> { + selectLocationButton.setVisibility(View.VISIBLE); + removeLocationButton.setVisibility(View.GONE); + + if (tmpLocalMarker == null) { + String sharingLevel = SharingLevel.MANUAL.toString(); + Icon defaultIcon = getMapIcon(sharingLevel, myId); + + MarkerOptions markerOptions = new MarkerOptions() + .setIcon(defaultIcon) + .position(latLng); + + tmpLocalMarker = mapboxMap.addMarker(markerOptions); + MapMarker marker = new MapMarker(tmpLocalMarker, markerOptions, defaultIcon, sharingLevel); + + updateUserHashMap(myId, marker.getMarker().getId()); + + + } else { + ValueAnimator markerAnimator = ObjectAnimator.ofObject(tmpLocalMarker, "position", + new mapUtils.LatLngEvaluator(), tmpLocalMarker.getPosition(), latLng); + markerAnimator.setDuration(2000); + markerAnimator.start(); + } + + selectLocationButton.setOnClickListener((View view) -> { + if (tmpLocalMarker != null) { + + + try { + droppedMarkercoordinate = new Coordinate( + latLng.getLatitude(), + latLng.getLongitude(), + latLng.getAltitude(), + "approx", + PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()), + PrefUtils.getUserId(getApplicationContext()), + PrefUtils.getLocationLevel(getApplicationContext()) + ); + + PrefUtils.putManualLocation(this, latLng); + handleBackgroundTaskLifeCycle(); + + + } catch (IllegalArgumentException e) { + Timber.e(String.valueOf(e)); + } + + Toast.makeText( + MapActivity.this, + "Manual Locations selected!" + latLng, + Toast.LENGTH_LONG + ).show(); + + selectLocationButton.setVisibility(View.GONE); + mapboxMap.removeMarker(tmpLocalMarker); + tmpLocalMarker = null; + + showLocationDialog(latLng); + } + }); + }); + + } + + public void showLocationDialog(LatLng latLng) { + + LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext()); + @SuppressLint("InflateParams") View view = layoutInflaterAndroid.inflate(R.layout.location_modal, null); + AlertDialog.Builder alertDialogBuilderUserInput = new android.support.v7.app.AlertDialog.Builder(this); + + alertDialogBuilderUserInput.setView(view).setPositiveButton(getApplicationContext().getString(R.string.modal_location_send), null); + + alertDialogBuilderUserInput + .setPositiveButton(R.string.lbl_ok, null) + .setNegativeButton(R.string.lbl_cancel, null); + + final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); + + EditText modalUserInput = view.findViewById(R.id.feedback_input); + + alertDialog.setOnShowListener(dialogInterface -> { + Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); + + Button dismissButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); + + dismissButton.setOnClickListener(view1 -> alertDialog.dismiss()); + + sendButton.setOnClickListener(view1 -> { + String body = modalUserInput.getText().toString(); + + if (TextUtils.isEmpty(body)) { + Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.modal_location_hint), Toast.LENGTH_SHORT).show(); + modalUserInput.requestFocus(); + } else { + PrefUtils.putManualLocation(getApplicationContext(), latLng); + sendLocation(body,latLng); + alertDialog.dismiss(); + } + }); + }); + + + + + + alertDialog.show(); + } + + + private void sendLocation(String body, LatLng latLng) { + PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel( + latLng.getLatitude(), + latLng.getLongitude(), + latLng.getAltitude(), + PrefUtils.getUserId(getApplicationContext()), + "string", + "predefined", + body + ); + + PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class); + CompositeDisposable disposable = new CompositeDisposable(); + disposable.add(predefinedStatusesService.postUserPredefinedCoord(PrefUtils.getUserId(getApplicationContext()), droppedMarkercoordinate) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::handleResponse, this::handleError)); + } + + private void handleResponse(List resp) { + Toast.makeText(getApplicationContext(), "Location saved!", Toast.LENGTH_SHORT).show(); } private void fetchTopCoords() { 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 dcab8ae..e9c521c 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 @@ -72,6 +72,7 @@ public class SharingFragment extends PreferenceFragment { protected List predefinedCoordsList= new ArrayList<>(); void getStatuses(CompositeDisposable disposable) { + disposable.add(statusesService.getUserPredefinedStatuses(PrefUtils.getUserId(getApplicationContext())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -80,14 +81,11 @@ public class SharingFragment extends PreferenceFragment { public void onSuccess(List strings) { String[] statusesArray = strings.toArray(new String[strings.size()]); setListPreferenceData(statusList.getKey(), statusesArray); - Log.d("GETSTATUSES", Arrays.toString(statusesArray)); - } @Override public void onError(Throwable e) { Toast.makeText(getApplicationContext(), "Error handling status fetch", Toast.LENGTH_SHORT).show(); - } })); @@ -101,15 +99,11 @@ public class SharingFragment extends PreferenceFragment { @Override public void onSuccess(List coords) { - PredefinedCoordViewModel [] statusesArray = (PredefinedCoordViewModel []) coords.toArray(new PredefinedCoordViewModel[0]); -// setListPreferenceData(statusList.getKey(), statusesArray); - List names = Stream.of(coords).map( p -> p.getName()).collect(com.annimon.stream.Collectors.toList()); + List names = Stream.of(coords).map(PredefinedCoordViewModel::getName).collect(com.annimon.stream.Collectors.toList()); predefinedCoordsList.addAll(coords); String [] stringnames = names.toArray(new String[0]); - Log.d("GETLOCATIONS", predefinedCoordsList.toString()); setListPreferenceData(manualLocationList.getKey(),stringnames); - Log.d("GETLOCATIONS", Arrays.toString(stringnames)); } @@ -118,7 +112,6 @@ public class SharingFragment extends PreferenceFragment { Toast.makeText(getApplicationContext(), "Error handling status fetch", Toast.LENGTH_SHORT).show(); } - })); } @@ -138,10 +131,11 @@ public class SharingFragment extends PreferenceFragment { statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class); disposable = new CompositeDisposable(); getStatuses(disposable); + if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) { getLocations(disposable); - } + locationLevelMapping = new HashMap(); locationLevelMapping.put(0, SharingLevel.PRESENCE.toString()); locationLevelMapping.put(1, SharingLevel.APPROXIMATED.toString()); @@ -156,11 +150,11 @@ public class SharingFragment extends PreferenceFragment { /** Main sharing switch**/ locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> { PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) newValue); + ((MapActivity) getActivity()).handleBackgroundTaskLifeCycle(); return true; }); /** Sharing level list **/ -// locationMode.setSummary(PrefUtils.getLocationLevel(getApplicationContext())); locationMode.setOnPreferenceChangeListener((preference, newValue) -> { PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue))); @@ -185,19 +179,17 @@ public class SharingFragment extends PreferenceFragment { /** Custom manual location list change listener **/ manualLocationList.setOnPreferenceChangeListener((preference, newValue) -> { ListPreference lp = (ListPreference) preference; - Log.d("SELECTEDLOCATION",(lp.getEntries()[Integer.parseInt((String) newValue)].toString())); PredefinedCoordViewModel temp = Stream.of(predefinedCoordsList).filter( p -> p.getName().equals(lp.getEntries()[Integer.parseInt((String) newValue)].toString())).single(); - Log.d("SELECTEDLOCATION",temp.toString()); -// LatLng latLng = new LatLng(latitude, longitude, altitude); + PrefUtils.putManualLocation(getApplicationContext(),temp.getGeoData()); // ((MapActivity) getActivity()).handleBackgroundTaskLifeCycle(); return true; }); + /** Button 'choose from map' button listener **/ manualLocationButton.setOnPreferenceChangeListener((preference, o) -> { - //ToDO wywołanie wybierania lokalizacji z mapy FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.hide(SharingFragment.this); fragmentTransaction.commit(); @@ -209,25 +201,16 @@ public class SharingFragment extends PreferenceFragment { ListPreference lp = (ListPreference) preference; CharSequence [] entries = lp.getEntries(); PrefUtils.storeStatus(getApplicationContext(), (String) entries[Integer.parseInt((String) newValue)]); -// PrefUtils.storeStatus(getApplicationContext(),statusMapping.get(Integer.parseInt((String) newValue))); return true; }); /** Custom status list change listener **/ manualStatus.setOnPreferenceChangeListener((preference, newValue) -> { -// ListPreference lp = (ListPreference) findPreference("key_status_value"); -// updateListPreference(lp, newValue, "manual_statuses"); -// PrefUtils.storeStatus(getApplicationContext(),(String) newValue); -// statusList.setValue((String) newValue); + disposable.add(statusesService.postUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()), (String) newValue) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(this::handleResponse, this::handleError)); -// Log.d("GETSTATUSES", "statusy po dodaniu nowego"); -// getStatuses(disposable); -// CharSequence[] entriesIndexes = statusList.getEntries(); -//// Log.d("GETENTRIES", Arrays.toString(entriesIndexes)); -// Log.d("GETENTRIES ostatni w handle response", (String) entriesIndexes[entriesIndexes.length - 1]); return true; @@ -248,29 +231,6 @@ public class SharingFragment extends PreferenceFragment { return view; } - public String getListPreferenceValue(String key) { - ListPreference lp = (ListPreference) findPreference(key); - return (String) lp.getEntry(); - - } - - protected void updateListPreference(ListPreference lp, Object newValue, String storageKey) { - CharSequence[] entries = lp.getEntries(); - Set defaultEntries = new HashSet(Arrays.asList(entries)); - SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); - - Set manualStatusSet = sharedPref.getStringSet(storageKey, defaultEntries); - manualStatusSet.add((String) newValue); - String[] manualStatusArr = manualStatusSet.toArray(new String[0]); - //Arrays.sort(manualStatusArr); - setListPreferenceData(lp.getKey(), manualStatusArr); -// lp.setValue((String) newValue); - - SharedPreferences.Editor editor = sharedPref.edit(); - editor.putStringSet(storageKey, manualStatusSet); - editor.apply(); - } - protected void setListPreferenceData(String lp_name, String [] entries) { //todo bug z pustym statusem ListPreference lp = (ListPreference) findPreference(lp_name); @@ -281,23 +241,21 @@ public class SharingFragment extends PreferenceFragment { entryValues[i] = Integer.toString(i); } - lp.setDefaultValue("1"); + lp.setDefaultValue("0"); lp.setEntryValues(entryValues); } private void handleResponse(List resp) { - String newStatus = resp.toArray(new String[resp.size()])[resp.size() - 1]; - Log.d("GETSTATUSES new status", newStatus); String[] statusesArray = resp.toArray(new String[resp.size()]); setListPreferenceData(statusList.getKey(), statusesArray); -// Log.d("GETSTATUSES nowe lokalne", Arrays.toString(statusList.getEntries())); -// statusList.setValue(newStatus); + statusList.setValueIndex(resp.size() - 1); + PrefUtils.storeStatus(getApplicationContext(), resp.get(resp.size() -1 )); + } private void handleError (Throwable error){ if (error instanceof HttpException) { - ResponseBody responseBody = ((HttpException) error).response().errorBody(); Toast.makeText(getApplicationContext(), RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/ManualLocationUtils.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/ManualLocationUtils.java index e88f7d7..6de0208 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/ManualLocationUtils.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/ManualLocationUtils.java @@ -1,16 +1,18 @@ package com.uam.wmi.findmytutor.utils; +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.Fragment; import android.content.Context; import android.content.DialogInterface; -import android.content.pm.PackageManager; -import android.os.Build; +import android.preference.PreferenceFragment; import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; -import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; @@ -19,98 +21,41 @@ import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.mapbox.mapboxsdk.geometry.LatLng; import com.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.activity.BaseActivity; -import com.uam.wmi.findmytutor.model.Coordinate; -import com.uam.wmi.findmytutor.model.Feedback; +import com.uam.wmi.findmytutor.activity.MapActivity; import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel; import com.uam.wmi.findmytutor.network.ApiClient; -import com.uam.wmi.findmytutor.service.FeedbackService; import com.uam.wmi.findmytutor.service.PredefinedStatusesService; import java.util.List; -import io.reactivex.Single; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; -import retrofit2.Response; -public class ManualLocationUtils { - private Context activityContext; +import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; +import static java.security.AccessController.getContext; - public ManualLocationUtils(Context context) { - activityContext = context; - } +@SuppressLint("ValidFragment") +public class ManualLocationUtils { + private Context activityContext; - public void showLocationDialog(String subject, LatLng latLng) { - - LayoutInflater layoutInflaterAndroid = LayoutInflater.from(activityContext); - View view = layoutInflaterAndroid.inflate(R.layout.location_modal, null); - AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(activityContext); - alertDialogBuilderUserInput.setView(view).setPositiveButton(activityContext.getString(R.string.modal_location_send), null); - final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); - - EditText modalUserInput = view.findViewById(R.id.feedback_input); - TextView modalTitle = view.findViewById(R.id.feedback_modal_title); - modalTitle.setText(subject); - alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialogInterface) { - Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); - sendButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - String body = modalUserInput.getText().toString(); - if (TextUtils.isEmpty(body)) { - Toast.makeText(activityContext, activityContext.getString(R.string.modal_location_hint), Toast.LENGTH_SHORT).show(); - modalUserInput.requestFocus(); - } else { - PrefUtils.putManualLocation(activityContext, latLng); - sendLocation(body,latLng); - alertDialog.dismiss(); - } - } - }); - } - }); - alertDialog.show(); - } - - private void sendLocation(String body, LatLng latLng) { - PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel( - latLng.getLatitude(), - latLng.getLongitude(), - latLng.getAltitude(), - PrefUtils.getUserId(activityContext), - "string", - "predefined", - body - ); - PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(activityContext).create(PredefinedStatusesService.class); - CompositeDisposable disposable = new CompositeDisposable(); - disposable.add(predefinedStatusesService.postUserPredefinedCoord(PrefUtils.getUserId(activityContext),droppedMarkercoordinate) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(this::handleResponse, this::handleError)); - } - - private void handleResponse(List resp) { - Toast.makeText(activityContext, "Location saved", Toast.LENGTH_SHORT).show(); - } - - private void handleError(Throwable error) { - if (error instanceof HttpException) { - - ResponseBody responseBody = ((HttpException) error).response().errorBody(); - Toast.makeText(activityContext, - RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); - Log.d("POSTCOORDINATE", error.getMessage()); + public ManualLocationUtils(Context context) { + activityContext = context; + } - } else { - Toast.makeText(activityContext, - "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); - Log.d("POSTCOORDINATE", error.getMessage()); - } - } + + private void handleError(Throwable error) { + if (error instanceof HttpException) { + + ResponseBody responseBody = ((HttpException) error).response().errorBody(); + Toast.makeText(activityContext, + RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); + + } else { + Toast.makeText(activityContext, + "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); + } + } } diff --git a/app/src/main/res/layout/location_modal.xml b/app/src/main/res/layout/location_modal.xml index cd566f4..5aaf83e 100644 --- a/app/src/main/res/layout/location_modal.xml +++ b/app/src/main/res/layout/location_modal.xml @@ -18,7 +18,7 @@ android:paddingTop="@dimen/activity_vertical_margin"> android:layout_marginBottom="@dimen/dimen_10" android:fontFamily="sans-serif-medium" android:lineSpacingExtra="8sp" - android:text="placeholder" + android:text="@string/manual_modal_title" android:textColor="@color/colorAccent" android:textSize="@dimen/lbl_new_note_title" android:textStyle="normal" /> @@ -34,6 +34,7 @@ android:paddingTop="@dimen/activity_vertical_margin"> android:hint="@string/modal_feedback_hint" android:maxLength="30" android:maxLines="1" + android:textColor="@color/note_list_text" android:requiresFadingEdge="vertical" android:scrollbars="vertical" /> diff --git a/app/src/main/res/layout/pref_sharing.xml b/app/src/main/res/layout/pref_sharing.xml index 73f957b..740abf4 100644 --- a/app/src/main/res/layout/pref_sharing.xml +++ b/app/src/main/res/layout/pref_sharing.xml @@ -24,7 +24,6 @@ android:summary="%s" android:title="@string/title_location_level" /> Angielski Polski Wybierz język - + \"\" O aplikacji Jesteśmy grupą studentów, która chce pomoć w rozwoju naszego wydziału.\nDziękujemy za używanie naszej aplikacji.\nZespół FMT.! diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fe1b0b8..bc71283 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -219,6 +219,9 @@ User profile Remove Manual location TutorTab + Do you want to save this localization? + Save! + Cancel! From c452e5dbf5557b7d72df1b654dd162dbfe5c9dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Tue, 27 Nov 2018 20:53:01 +0100 Subject: [PATCH 3/3] Fix BG & UI --- .../findmytutor/activity/BaseActivity.java | 3 +- .../findmytutor/activity/LoginActivity.java | 2 +- .../findmytutor/activity/SharingFragment.java | 116 +++++++----------- .../BackgroundLocalizationService.java | 28 +++-- app/src/main/res/layout/feedback_modal.xml | 25 ++-- .../main/res/menu/activity_main_drawer.xml | 4 +- 6 files changed, 81 insertions(+), 97 deletions(-) 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 823a869..b442f6d 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 @@ -100,7 +100,6 @@ public abstract class BaseActivity } else if (itemName.equals(getResources().getString(R.string.navigation_item_logout))) { if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) { - stopBackgroundLocalizationTask(); } @@ -194,7 +193,7 @@ public abstract class BaseActivity Intent startIntent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { startForegroundService(startIntent); } else { startService(startIntent); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java index b1ebc98..1054754 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java @@ -154,7 +154,7 @@ public class LoginActivity extends AppCompatActivity { private void loginProcess(String email, String password) { ValidateUser user = new ValidateUser(email, password); - LdapUser fuser = new LdapUser(email,password,"wmi","tutor","marek","nocny","szmare@wmi.pl"); + LdapUser fuser = new LdapUser(email,password,"tutor","tutor","mieszko","nocny","szmsdsdsdsdare@wmi.pl"); // disposable.add(ldapService.validate(user) disposable.add(ldapService.fakeValidate(fuser) .subscribeOn(Schedulers.io()) 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 e9c521c..56d342e 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 @@ -1,12 +1,7 @@ package com.uam.wmi.findmytutor.activity; import android.annotation.SuppressLint; -import android.app.Activity; import android.app.FragmentTransaction; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Build; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; @@ -16,52 +11,34 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; import android.widget.Toast; -import com.annimon.stream.Collector; import com.annimon.stream.Stream; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; -import com.mapbox.mapboxsdk.geometry.LatLng; import com.uam.wmi.findmytutor.R; -import com.uam.wmi.findmytutor.model.Feedback; import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel; -import com.uam.wmi.findmytutor.model.User; import com.uam.wmi.findmytutor.network.ApiClient; -import com.uam.wmi.findmytutor.service.BackgroundLocalizationService; -import com.uam.wmi.findmytutor.service.FeedbackService; import com.uam.wmi.findmytutor.service.PredefinedStatusesService; import com.uam.wmi.findmytutor.utils.PrefUtils; - import com.uam.wmi.findmytutor.utils.RestApiHelper; import com.uam.wmi.findmytutor.utils.RightButtonPreference; import com.uam.wmi.findmytutor.utils.SharingLevel; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; -import retrofit2.Response; import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; public class SharingFragment extends PreferenceFragment { - private HashMap locationLevelMapping; - private HashMap statusMapping; - private PredefinedStatusesService statusesService; - private CompositeDisposable disposable; protected Preference locationSharing; protected Preference locationMode; protected Preference manualLocationList; @@ -69,7 +46,15 @@ public class SharingFragment extends PreferenceFragment { protected RightButtonPreference manualLocationButton; protected Preference manualStatus; protected ListPreference statusList; - protected List predefinedCoordsList= new ArrayList<>(); + protected List predefinedCoordsList = new ArrayList<>(); + private HashMap locationLevelMapping; + private HashMap statusMapping; + private PredefinedStatusesService statusesService; + private CompositeDisposable disposable; + + public static SharingFragment newInstance() { + return new SharingFragment(); + } void getStatuses(CompositeDisposable disposable) { @@ -91,29 +76,29 @@ public class SharingFragment extends PreferenceFragment { })); } - void getLocations(CompositeDisposable disposable) { - disposable.add(statusesService.getUserPredefinedCoords(PrefUtils.getUserId(getApplicationContext())) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeWith(new DisposableSingleObserver>() { + void getLocations(CompositeDisposable disposable) { + disposable.add(statusesService.getUserPredefinedCoords(PrefUtils.getUserId(getApplicationContext())) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeWith(new DisposableSingleObserver>() { - @Override - public void onSuccess(List coords) { - List names = Stream.of(coords).map(PredefinedCoordViewModel::getName).collect(com.annimon.stream.Collectors.toList()); - predefinedCoordsList.addAll(coords); - String [] stringnames = names.toArray(new String[0]); + @Override + public void onSuccess(List coords) { + List names = Stream.of(coords).map(PredefinedCoordViewModel::getName).collect(com.annimon.stream.Collectors.toList()); + predefinedCoordsList.addAll(coords); + String[] stringnames = names.toArray(new String[0]); - setListPreferenceData(manualLocationList.getKey(),stringnames); + setListPreferenceData(manualLocationList.getKey(), stringnames); - } + } - @Override - public void onError(Throwable e) { - Toast.makeText(getApplicationContext(), "Error handling status fetch", Toast.LENGTH_SHORT).show(); + @Override + public void onError(Throwable e) { + Toast.makeText(getApplicationContext(), "Error handling status fetch", Toast.LENGTH_SHORT).show(); - } - })); - } + } + })); + } @SuppressLint("ResourceType") @Override @@ -132,9 +117,9 @@ public class SharingFragment extends PreferenceFragment { disposable = new CompositeDisposable(); getStatuses(disposable); - if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) { - getLocations(disposable); - } + if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) { + getLocations(disposable); + } locationLevelMapping = new HashMap(); locationLevelMapping.put(0, SharingLevel.PRESENCE.toString()); @@ -158,11 +143,11 @@ public class SharingFragment extends PreferenceFragment { locationMode.setOnPreferenceChangeListener((preference, newValue) -> { PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue))); - if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) { + if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) { preferenceCategory.addPreference(manualLocationList); - getLocations(disposable); - preferenceCategory.addPreference(manualLocationButton); + getLocations(disposable); + preferenceCategory.addPreference(manualLocationButton); } else { preferenceCategory.removePreference(manualLocationList); @@ -180,10 +165,9 @@ public class SharingFragment extends PreferenceFragment { manualLocationList.setOnPreferenceChangeListener((preference, newValue) -> { ListPreference lp = (ListPreference) preference; - PredefinedCoordViewModel temp = Stream.of(predefinedCoordsList).filter( p -> p.getName().equals(lp.getEntries()[Integer.parseInt((String) newValue)].toString())).single(); + PredefinedCoordViewModel temp = Stream.of(predefinedCoordsList).filter(p -> p.getName().equals(lp.getEntries()[Integer.parseInt((String) newValue)].toString())).single(); - PrefUtils.putManualLocation(getApplicationContext(),temp.getGeoData()); -// ((MapActivity) getActivity()).handleBackgroundTaskLifeCycle(); + PrefUtils.putManualLocation(getApplicationContext(), temp.getGeoData()); return true; }); @@ -199,7 +183,7 @@ public class SharingFragment extends PreferenceFragment { /** Status list change listener **/ statusList.setOnPreferenceChangeListener((preference, newValue) -> { ListPreference lp = (ListPreference) preference; - CharSequence [] entries = lp.getEntries(); + CharSequence[] entries = lp.getEntries(); PrefUtils.storeStatus(getApplicationContext(), (String) entries[Integer.parseInt((String) newValue)]); return true; @@ -218,11 +202,6 @@ public class SharingFragment extends PreferenceFragment { } - public static SharingFragment newInstance() { - return new SharingFragment(); - } - - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = super.onCreateView(inflater, container, savedInstanceState); @@ -231,7 +210,7 @@ public class SharingFragment extends PreferenceFragment { return view; } - protected void setListPreferenceData(String lp_name, String [] entries) { + protected void setListPreferenceData(String lp_name, String[] entries) { //todo bug z pustym statusem ListPreference lp = (ListPreference) findPreference(lp_name); lp.setEntries(entries); @@ -250,21 +229,20 @@ public class SharingFragment extends PreferenceFragment { setListPreferenceData(statusList.getKey(), statusesArray); statusList.setValueIndex(resp.size() - 1); - PrefUtils.storeStatus(getApplicationContext(), resp.get(resp.size() -1 )); + PrefUtils.storeStatus(getApplicationContext(), resp.get(resp.size() - 1)); } - private void handleError (Throwable error){ - if (error instanceof HttpException) { - ResponseBody responseBody = ((HttpException) error).response().errorBody(); - Toast.makeText(getApplicationContext(), - RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); + private void handleError(Throwable error) { + if (error instanceof HttpException) { + ResponseBody responseBody = ((HttpException) error).response().errorBody(); + Toast.makeText(getApplicationContext(), + RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText(getApplicationContext(), - "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); - Log.d("FEEDBACK", error.getMessage()); - } + } else { + Toast.makeText(getApplicationContext(), + "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); } + } } 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 d901db4..a510713 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 @@ -25,14 +25,12 @@ import android.util.Log; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; -import com.google.android.gms.tasks.OnSuccessListener; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.mapbox.geojson.Point; import com.mapbox.mapboxsdk.geometry.LatLng; import com.uam.wmi.findmytutor.model.Coordinate; import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.utils.ApproximatedLocalization; -import com.uam.wmi.findmytutor.utils.Consts; import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.RestApiHelper; import com.uam.wmi.findmytutor.utils.SharingLevel; @@ -40,6 +38,8 @@ import com.uam.wmi.findmytutor.utils.mapUtils; import java.util.ArrayList; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -61,7 +61,8 @@ public class BackgroundLocalizationService extends Service { private static long notify_interval = 10000; Location mLastLocation; Boolean stopService = false; - + Intent intent; + public static String str_receiver = "background.location.broadcast"; ArrayList providers = new ArrayList(); LocationListener[] mLocationListeners; @@ -146,17 +147,22 @@ public class BackgroundLocalizationService extends Service { } if (!stopService) { - mStatusChecker = () -> { - try { - fn_getlocation(); - } finally { - mHandler.postDelayed(mStatusChecker, notify_interval); - } - }; - AsyncTask.execute(mStatusChecker); + Timer mTimer = new Timer(); + mTimer.schedule(new TimerTaskToGetLocation(), 20, notify_interval); + intent = new Intent(str_receiver); + + fn_getlocation(); } + + } + + private class TimerTaskToGetLocation extends TimerTask { + @Override + public void run() { + mHandler.post(BackgroundLocalizationService.this::fn_getlocation); + } } @RequiresApi(api = Build.VERSION_CODES.O) diff --git a/app/src/main/res/layout/feedback_modal.xml b/app/src/main/res/layout/feedback_modal.xml index 375a923..591746d 100644 --- a/app/src/main/res/layout/feedback_modal.xml +++ b/app/src/main/res/layout/feedback_modal.xml @@ -15,21 +15,30 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/feedback_input" - android:layout_marginBottom="@dimen/dimen_10" + android:layout_marginBottom="15dp" android:fontFamily="sans-serif-medium" - android:lineSpacingExtra="8sp" + android:lineSpacingExtra="10sp" android:text="placeholder" android:textColor="@color/colorAccent" android:textSize="@dimen/lbl_new_note_title" android:textStyle="normal" /> + + - + \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index 90a85ef..c6e0307 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -1,14 +1,14 @@ - + android:title="@string/navigation_item_blacklist" />-->