Loading...
Searching...
No Matches
DefaultNavigationView.java
Go to the documentation of this file.
1package com.navigine.view;
2
3import android.content.Context;
4import android.graphics.PointF;
5import android.util.AttributeSet;
6import android.view.Gravity;
7import android.view.ViewGroup;
8import android.widget.FrameLayout;
9
10import com.navigine.idl.java.CameraListener;
11import com.navigine.idl.java.CameraUpdateReason;
12import com.navigine.idl.java.NavigineSdk;
13import com.navigine.idl.java.UserLocationLayer;
14import com.navigine.view.widgets.FloorSelectorViewConfig;
15import com.navigine.view.widgets.FollowMeButton;
16import com.navigine.view.widgets.FollowMeButtonConfig;
17import com.navigine.view.widgets.ZoomControlsConfig;
18
65public class DefaultNavigationView extends DefaultNavigineView {
66 public static final String TAG = DefaultNavigationView.class.getCanonicalName();
67
68 private FollowMeButtonConfig followMeButtonConfig;
69 private FollowMeButton followMeButton;
70 private UserLocationLayer userLocationLayer;
71 private CameraListener cameraListener;
72 private boolean cameraListenerRegistered;
73
83 public DefaultNavigationView(final Context context) {
84 this(context, null, null, null, null, null);
85 }
86
88 public DefaultNavigationView(final Context context, final AttributeSet attrs) {
89 this(context, attrs, null, null, null, null);
90 }
91
101 public DefaultNavigationView(final Context context, final AttributeSet attrs,
102 final DefaultNavigationViewConfig viewConfig) {
103 this(context, attrs, viewConfig, null, null, null);
104 }
105
110 public DefaultNavigationView(final Context context, final AttributeSet attrs,
111 final DefaultNavigationViewConfig viewConfig,
112 final ZoomControlsConfig zoomConfig,
113 final FollowMeButtonConfig followMeConfig,
114 final FloorSelectorViewConfig floorConfig) {
115 super(context, attrs,
116 viewConfig != null ? viewConfig : DefaultNavigationViewConfig.defaultConfig(),
117 zoomConfig,
118 floorConfig);
119 this.followMeButtonConfig = followMeConfig != null ? followMeConfig : FollowMeButtonConfig.defaultConfig();
120 installFollowMeAndUserLocation();
121 }
122
124 private void installFollowMeAndUserLocation() {
125 followMeButton = new FollowMeButton(getContext(),
126 this::followMePressed,
127 followMeButtonConfig);
128 FrameLayout.LayoutParams followLp = new FrameLayout.LayoutParams(
129 ViewGroup.LayoutParams.WRAP_CONTENT,
130 ViewGroup.LayoutParams.WRAP_CONTENT
131 );
132 followLp.gravity = Gravity.END | Gravity.BOTTOM;
133 addView(followMeButton, followLp);
134
137
138 userLocationLayer = NavigineSdk.getInstance().getUserLocationLayer(locationWindow);
139 userLocationLayer.setVisible(true);
140
141 cameraListener = new CameraListener() {
142 @Override
143 public void onCameraPositionChanged(CameraUpdateReason reason, boolean finished) {
144 if (reason == CameraUpdateReason.APPLICATION) {
145 return;
146 }
147
148 if (followMeButton != null && followMeButton.isFollowing()) {
149 userLocationLayer.resetAnchor();
150 followMeButton.updateAppearanceForFollowingState(false);
151 }
152 }
153 };
154 registerCameraListener();
155 }
156
157 private void registerCameraListener() {
158 if (cameraListenerRegistered || locationWindow == null || cameraListener == null) {
159 return;
160 }
161 locationWindow.addCameraListener(cameraListener);
162 cameraListenerRegistered = true;
163 }
164
165 private void unregisterCameraListener() {
166 if (!cameraListenerRegistered || locationWindow == null || cameraListener == null) {
167 return;
168 }
169 locationWindow.removeCameraListener(cameraListener);
170 cameraListenerRegistered = false;
171 }
172
173 @Override
174 protected void applyConfigVisibility() {
175 super.applyConfigVisibility();
176 if (followMeButton != null) {
177 followMeButton.setVisibility(
178 (getViewConfig().getVisibleWidgets() & DefaultNavigationViewConfig.WIDGET_FOLLOW_ME) != 0
179 ? VISIBLE : GONE);
180 }
181 }
182
183 @Override
184 protected void applyWidgetLayoutParams() {
185 super.applyWidgetLayoutParams();
186 if (followMeButton != null) {
187 FrameLayout.LayoutParams followLp = (FrameLayout.LayoutParams) followMeButton.getLayoutParams();
188 if (followLp == null) {
189 followLp = new FrameLayout.LayoutParams(
190 ViewGroup.LayoutParams.WRAP_CONTENT,
191 ViewGroup.LayoutParams.WRAP_CONTENT);
192 }
193 followLp.gravity = Gravity.END | Gravity.BOTTOM;
194 FollowMeButtonConfig fmConfig = followMeButtonConfig;
195 int rightPad = fmConfig.getMarginRight() > 0
196 ? dpToPx(fmConfig.getMarginRight())
197 : dpToPx((int) com.navigine.view.widgets.WidgetStyles.STANDARD_RIGHT_PADDING);
198 int bottomPad = fmConfig.getMarginBottom() > 0
199 ? dpToPx(fmConfig.getMarginBottom())
200 : dpToPx((int) com.navigine.view.widgets.WidgetStyles.FOLLOW_ME_BOTTOM_PADDING);
201 followLp.setMargins(0, 0, rightPad, bottomPad);
202 followMeButton.setLayoutParams(followLp);
203 }
204 }
205
210 private void followMePressed(boolean isFollowing) {
211 if (isFollowing) {
212 // Screen-space pixels (same as iOS/Qt/Flutter): view size × display density.
213 final float density = getResources().getDisplayMetrics().density;
214 float centerX = getWidth() * density / 2f;
215 float centerY = getHeight() * density / 2f;
216 PointF anchorPoint = new PointF(centerX, centerY);
217 userLocationLayer.setAnchor(anchorPoint);
218 } else {
219 userLocationLayer.resetAnchor();
220 }
221 }
222
226 public void setViewConfig(DefaultNavigationViewConfig newViewConfig) {
227 super.setViewConfig(newViewConfig);
228 }
229
241 public void setConfig(DefaultNavigationViewConfig newViewConfig,
242 ZoomControlsConfig newZoomConfig,
243 FollowMeButtonConfig newFollowMeConfig,
244 FloorSelectorViewConfig newFloorConfig) {
245 if (newFollowMeConfig != null) followMeButtonConfig = newFollowMeConfig;
246 super.setConfig(newViewConfig, newZoomConfig, newFloorConfig);
247 if (followMeButton != null) followMeButton.applyConfig(followMeButtonConfig);
249 }
250
252 @Override
254 return (DefaultNavigationViewConfig) super.getViewConfig();
255 }
256
258 public FollowMeButtonConfig getFollowMeButtonConfig() { return followMeButtonConfig; }
259
261 public FollowMeButton getFollowMeButton() { return followMeButton; }
262
263 @Override
264 protected void onAttachedToWindow() {
265 super.onAttachedToWindow();
266 registerCameraListener();
267 }
268
269 @Override
270 protected void onDetachedFromWindow() {
271 unregisterCameraListener();
272 super.onDetachedFromWindow();
273 }
274}