Skip to content

Commit

Permalink
Fix follow mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Pygmalion69 committed Jul 9, 2023
1 parent 7844671 commit cb20a6b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "org.nitri.opentopo"
minSdkVersion 21
targetSdkVersion 33
versionCode 37
versionName "1.13.2"
versionCode 38
versionName "1.13.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
60 changes: 51 additions & 9 deletions app/src/main/java/org/nitri/opentopo/MapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import org.nitri.opentopo.model.LocationViewModel;
import org.nitri.opentopo.nearby.entity.NearbyItem;
import org.nitri.opentopo.overlay.GestureOverlay;
import org.nitri.opentopo.overlay.OverlayHelper;
import org.osmdroid.config.Configuration;
import org.osmdroid.config.IConfigurationProvider;
Expand All @@ -67,7 +68,8 @@
import io.ticofab.androidgpxparser.parser.domain.Gpx;


public class MapFragment extends Fragment implements LocationListener, PopupMenu.OnMenuItemClickListener {
public class MapFragment extends Fragment implements LocationListener, PopupMenu.OnMenuItemClickListener,
GestureOverlay.GestureCallback {

private MapView mMapView;
private MyLocationNewOverlay mLocationOverlay;
Expand All @@ -88,18 +90,27 @@ public void run() {
}
};

private final Runnable mEnableFollowRunnable = new Runnable() {

@Override
public void run() {
if (mLocationOverlay != null) {
mLocationOverlay.enableFollowLocation();
mLocationOverlay.setEnableAutoStop(true);
}
mMapHandler.postDelayed(this, 5000);
}
};

private final MapListener mDragListener = new MapListener() {
@Override
public boolean onScroll(ScrollEvent event) {
if (mFollow && mMapHandler != null) {
mMapHandler.removeCallbacks(mCenterRunnable);
mMapHandler.postDelayed(mCenterRunnable, 6000);
}
return true;
return false;
}

@Override
public boolean onZoom(ZoomEvent event) {
onUserMapInteraction();
return false;
}
};
Expand Down Expand Up @@ -140,6 +151,7 @@ public boolean onZoom(ZoomEvent event) {
private int mLastNearbyAnimateToId;

private LocationViewModel mLocationViewModel;
private GestureOverlay mGestureOverlay;

public MapFragment() {
}
Expand Down Expand Up @@ -242,6 +254,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mRotationGestureOverlay = new RotationGestureOverlay(mMapView);
mRotationGestureOverlay.setEnabled(true);

mGestureOverlay = new GestureOverlay(this);
mMapView.getOverlays().add(mGestureOverlay);

mMapView.setMaxZoomLevel(17d);
mMapView.setTilesScaledToDpi(true);
if (mListener != null && mListener.isFullscreen()) {
Expand Down Expand Up @@ -269,6 +284,21 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mOverlayHelper = new OverlayHelper(getActivity(), mMapView);

setTilesOverlay();

mLocationOverlay.runOnFirstFix(new Runnable() {
@Override
public void run() {
final GeoPoint location = mLocationOverlay.getMyLocation();
if (location != null) {
try {
requireActivity().runOnUiThread(() -> mMapView.getController().animateTo(location));
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
;
}
});
}

return view;
Expand Down Expand Up @@ -424,8 +454,9 @@ private void enableFollow() {
if (getActivity() != null)
((AppCompatActivity) getActivity()).supportInvalidateOptionsMenu();
mLocationOverlay.enableFollowLocation();
mLocationOverlay.setEnableAutoStop(true);
mMapHandler.removeCallbacks(mCenterRunnable);
mMapHandler.post(mCenterRunnable);
//mMapHandler.post(mCenterRunnable);
mPrefs.edit().putBoolean(PREF_FOLLOW, true).apply();
}

Expand All @@ -434,7 +465,7 @@ private void disableFollow() {
if (getActivity() != null)
((AppCompatActivity) getActivity()).supportInvalidateOptionsMenu();
mLocationOverlay.disableFollowLocation();
mMapHandler.removeCallbacks(mCenterRunnable);
mMapHandler.removeCallbacksAndMessages(null);
mPrefs.edit().putBoolean(PREF_FOLLOW, false).apply();
}

Expand Down Expand Up @@ -771,7 +802,8 @@ public boolean onMenuItemClick(MenuItem menuItem) {

@Override
public void onLocationChanged(Location location) {
if (BuildConfig.DEBUG && location != null) Log.d(TAG, String.format("Location: %f, %f", location.getLatitude(), location.getLongitude()));
if (BuildConfig.DEBUG && location != null)
Log.d(TAG, String.format("Location: %f, %f", location.getLatitude(), location.getLongitude()));
mLocationViewModel.getCurrentLocation().setValue(location);
}

Expand Down Expand Up @@ -811,11 +843,21 @@ public void onDestroy() {
mCompassOverlay = null;
mScaleBarOverlay = null;
mRotationGestureOverlay = null;
mGestureOverlay = null;
if (mOverlayHelper != null)
mOverlayHelper.destroy();
mMapView = null;
}

@Override
public void onUserMapInteraction() {
if (mFollow) {
// follow disabled by gesture -> re-enable with delay
mMapHandler.removeCallbacksAndMessages(null);
mMapHandler.postDelayed(mEnableFollowRunnable, 5000);
}
}

public interface OnFragmentInteractionListener {

/**
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/nitri/opentopo/overlay/GestureOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.nitri.opentopo.overlay;

import android.view.MotionEvent;

import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay;

public class GestureOverlay extends Overlay {

GestureCallback gestureCallback;

public GestureOverlay(GestureCallback gestureCallback) {
this.gestureCallback = gestureCallback;
}

@Override
public boolean onFling(MotionEvent pEvent1, MotionEvent pEvent2, float pVelocityX, float pVelocityY, MapView pMapView) {
gestureCallback.onUserMapInteraction();
return super.onFling(pEvent1, pEvent2, pVelocityX, pVelocityY, pMapView);
}

@Override
public boolean onScroll(MotionEvent pEvent1, MotionEvent pEvent2, float pDistanceX, float pDistanceY, MapView pMapView) {
gestureCallback.onUserMapInteraction();
return super.onScroll(pEvent1, pEvent2, pDistanceX, pDistanceY, pMapView);
}

public interface GestureCallback {
void onUserMapInteraction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;

import androidx.core.content.ContextCompat;

import org.nitri.opentopo.R;
Expand All @@ -19,7 +20,6 @@
import org.osmdroid.views.overlay.infowindow.BasicInfoWindow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -29,8 +29,8 @@

public class OverlayHelper {

private Context mContext;
private MapView mMapView;
private final Context mContext;
private final MapView mMapView;

private ItemizedIconInfoOverlay mWayPointOverlay;
private ItemizedIconInfoOverlay mNearbyItemOverlay;
Expand Down Expand Up @@ -153,7 +153,6 @@ public void clearGpx() {
}
}


public void setNearby(NearbyItem item) {
clearNearby();
GeoPoint geoPoint = new GeoPoint(item.getLat(), item.getLon());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@

public class TrackOverlay extends Overlay {

private Track mTrack;
private Context mContext;

private List<List<Point>> mPointsSegments = new ArrayList<>();
private final Track mTrack;
private final Context mContext;
private final List<List<Point>> mPointsSegments = new ArrayList<>();

private static final String TAG = TrackOverlay.class.getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.nitri.opentopo.overlay;

import android.annotation.SuppressLint;
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
import android.text.Spanned;
Expand All @@ -21,9 +20,9 @@
public class WayPointInfoWindow extends BasicInfoWindow {

private final String mSubDescription;
private int mTitleId;
private int mDescriptionId;
private int mSubDescriptionId;
private final int mTitleId;
private final int mDescriptionId;
private final int mSubDescriptionId;

public WayPointInfoWindow(int layoutResId, int titleId, int descriptionId, int subDescriptionId,
String subDescription, MapView mapView) {
Expand Down

0 comments on commit cb20a6b

Please sign in to comment.