Skip to content

Commit cb20a6b

Browse files
committed
Fix follow mode
1 parent 7844671 commit cb20a6b

File tree

6 files changed

+93
-23
lines changed

6 files changed

+93
-23
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "org.nitri.opentopo"
77
minSdkVersion 21
88
targetSdkVersion 33
9-
versionCode 37
10-
versionName "1.13.2"
9+
versionCode 38
10+
versionName "1.13.3"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
vectorDrawables.useSupportLibrary = true
1313
}

app/src/main/java/org/nitri/opentopo/MapFragment.java

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import org.nitri.opentopo.model.LocationViewModel;
4343
import org.nitri.opentopo.nearby.entity.NearbyItem;
44+
import org.nitri.opentopo.overlay.GestureOverlay;
4445
import org.nitri.opentopo.overlay.OverlayHelper;
4546
import org.osmdroid.config.Configuration;
4647
import org.osmdroid.config.IConfigurationProvider;
@@ -67,7 +68,8 @@
6768
import io.ticofab.androidgpxparser.parser.domain.Gpx;
6869

6970

70-
public class MapFragment extends Fragment implements LocationListener, PopupMenu.OnMenuItemClickListener {
71+
public class MapFragment extends Fragment implements LocationListener, PopupMenu.OnMenuItemClickListener,
72+
GestureOverlay.GestureCallback {
7173

7274
private MapView mMapView;
7375
private MyLocationNewOverlay mLocationOverlay;
@@ -88,18 +90,27 @@ public void run() {
8890
}
8991
};
9092

93+
private final Runnable mEnableFollowRunnable = new Runnable() {
94+
95+
@Override
96+
public void run() {
97+
if (mLocationOverlay != null) {
98+
mLocationOverlay.enableFollowLocation();
99+
mLocationOverlay.setEnableAutoStop(true);
100+
}
101+
mMapHandler.postDelayed(this, 5000);
102+
}
103+
};
104+
91105
private final MapListener mDragListener = new MapListener() {
92106
@Override
93107
public boolean onScroll(ScrollEvent event) {
94-
if (mFollow && mMapHandler != null) {
95-
mMapHandler.removeCallbacks(mCenterRunnable);
96-
mMapHandler.postDelayed(mCenterRunnable, 6000);
97-
}
98-
return true;
108+
return false;
99109
}
100110

101111
@Override
102112
public boolean onZoom(ZoomEvent event) {
113+
onUserMapInteraction();
103114
return false;
104115
}
105116
};
@@ -140,6 +151,7 @@ public boolean onZoom(ZoomEvent event) {
140151
private int mLastNearbyAnimateToId;
141152

142153
private LocationViewModel mLocationViewModel;
154+
private GestureOverlay mGestureOverlay;
143155

144156
public MapFragment() {
145157
}
@@ -242,6 +254,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
242254
mRotationGestureOverlay = new RotationGestureOverlay(mMapView);
243255
mRotationGestureOverlay.setEnabled(true);
244256

257+
mGestureOverlay = new GestureOverlay(this);
258+
mMapView.getOverlays().add(mGestureOverlay);
259+
245260
mMapView.setMaxZoomLevel(17d);
246261
mMapView.setTilesScaledToDpi(true);
247262
if (mListener != null && mListener.isFullscreen()) {
@@ -269,6 +284,21 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
269284
mOverlayHelper = new OverlayHelper(getActivity(), mMapView);
270285

271286
setTilesOverlay();
287+
288+
mLocationOverlay.runOnFirstFix(new Runnable() {
289+
@Override
290+
public void run() {
291+
final GeoPoint location = mLocationOverlay.getMyLocation();
292+
if (location != null) {
293+
try {
294+
requireActivity().runOnUiThread(() -> mMapView.getController().animateTo(location));
295+
} catch (IllegalStateException e) {
296+
e.printStackTrace();
297+
}
298+
}
299+
;
300+
}
301+
});
272302
}
273303

274304
return view;
@@ -424,8 +454,9 @@ private void enableFollow() {
424454
if (getActivity() != null)
425455
((AppCompatActivity) getActivity()).supportInvalidateOptionsMenu();
426456
mLocationOverlay.enableFollowLocation();
457+
mLocationOverlay.setEnableAutoStop(true);
427458
mMapHandler.removeCallbacks(mCenterRunnable);
428-
mMapHandler.post(mCenterRunnable);
459+
//mMapHandler.post(mCenterRunnable);
429460
mPrefs.edit().putBoolean(PREF_FOLLOW, true).apply();
430461
}
431462

@@ -434,7 +465,7 @@ private void disableFollow() {
434465
if (getActivity() != null)
435466
((AppCompatActivity) getActivity()).supportInvalidateOptionsMenu();
436467
mLocationOverlay.disableFollowLocation();
437-
mMapHandler.removeCallbacks(mCenterRunnable);
468+
mMapHandler.removeCallbacksAndMessages(null);
438469
mPrefs.edit().putBoolean(PREF_FOLLOW, false).apply();
439470
}
440471

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

772803
@Override
773804
public void onLocationChanged(Location location) {
774-
if (BuildConfig.DEBUG && location != null) Log.d(TAG, String.format("Location: %f, %f", location.getLatitude(), location.getLongitude()));
805+
if (BuildConfig.DEBUG && location != null)
806+
Log.d(TAG, String.format("Location: %f, %f", location.getLatitude(), location.getLongitude()));
775807
mLocationViewModel.getCurrentLocation().setValue(location);
776808
}
777809

@@ -811,11 +843,21 @@ public void onDestroy() {
811843
mCompassOverlay = null;
812844
mScaleBarOverlay = null;
813845
mRotationGestureOverlay = null;
846+
mGestureOverlay = null;
814847
if (mOverlayHelper != null)
815848
mOverlayHelper.destroy();
816849
mMapView = null;
817850
}
818851

852+
@Override
853+
public void onUserMapInteraction() {
854+
if (mFollow) {
855+
// follow disabled by gesture -> re-enable with delay
856+
mMapHandler.removeCallbacksAndMessages(null);
857+
mMapHandler.postDelayed(mEnableFollowRunnable, 5000);
858+
}
859+
}
860+
819861
public interface OnFragmentInteractionListener {
820862

821863
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.nitri.opentopo.overlay;
2+
3+
import android.view.MotionEvent;
4+
5+
import org.osmdroid.views.MapView;
6+
import org.osmdroid.views.overlay.Overlay;
7+
8+
public class GestureOverlay extends Overlay {
9+
10+
GestureCallback gestureCallback;
11+
12+
public GestureOverlay(GestureCallback gestureCallback) {
13+
this.gestureCallback = gestureCallback;
14+
}
15+
16+
@Override
17+
public boolean onFling(MotionEvent pEvent1, MotionEvent pEvent2, float pVelocityX, float pVelocityY, MapView pMapView) {
18+
gestureCallback.onUserMapInteraction();
19+
return super.onFling(pEvent1, pEvent2, pVelocityX, pVelocityY, pMapView);
20+
}
21+
22+
@Override
23+
public boolean onScroll(MotionEvent pEvent1, MotionEvent pEvent2, float pDistanceX, float pDistanceY, MapView pMapView) {
24+
gestureCallback.onUserMapInteraction();
25+
return super.onScroll(pEvent1, pEvent2, pDistanceX, pDistanceY, pMapView);
26+
}
27+
28+
public interface GestureCallback {
29+
void onUserMapInteraction();
30+
}
31+
}

app/src/main/java/org/nitri/opentopo/overlay/OverlayHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Color;
55
import android.graphics.ColorMatrix;
66
import android.graphics.ColorMatrixColorFilter;
7+
78
import androidx.core.content.ContextCompat;
89

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

2122
import java.util.ArrayList;
22-
import java.util.Arrays;
2323
import java.util.Collections;
2424
import java.util.List;
2525

@@ -29,8 +29,8 @@
2929

3030
public class OverlayHelper {
3131

32-
private Context mContext;
33-
private MapView mMapView;
32+
private final Context mContext;
33+
private final MapView mMapView;
3434

3535
private ItemizedIconInfoOverlay mWayPointOverlay;
3636
private ItemizedIconInfoOverlay mNearbyItemOverlay;
@@ -153,7 +153,6 @@ public void clearGpx() {
153153
}
154154
}
155155

156-
157156
public void setNearby(NearbyItem item) {
158157
clearNearby();
159158
GeoPoint geoPoint = new GeoPoint(item.getLat(), item.getLon());

app/src/main/java/org/nitri/opentopo/overlay/TrackOverlay.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
public class TrackOverlay extends Overlay {
2424

25-
private Track mTrack;
26-
private Context mContext;
27-
28-
private List<List<Point>> mPointsSegments = new ArrayList<>();
25+
private final Track mTrack;
26+
private final Context mContext;
27+
private final List<List<Point>> mPointsSegments = new ArrayList<>();
2928

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

app/src/main/java/org/nitri/opentopo/overlay/WayPointInfoWindow.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.nitri.opentopo.overlay;
22

33
import android.annotation.SuppressLint;
4-
import android.text.Html;
54
import android.text.Layout;
65
import android.text.Spannable;
76
import android.text.Spanned;
@@ -21,9 +20,9 @@
2120
public class WayPointInfoWindow extends BasicInfoWindow {
2221

2322
private final String mSubDescription;
24-
private int mTitleId;
25-
private int mDescriptionId;
26-
private int mSubDescriptionId;
23+
private final int mTitleId;
24+
private final int mDescriptionId;
25+
private final int mSubDescriptionId;
2726

2827
public WayPointInfoWindow(int layoutResId, int titleId, int descriptionId, int subDescriptionId,
2928
String subDescription, MapView mapView) {

0 commit comments

Comments
 (0)