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 updateFollowMeAnchor();
213 } else {
214 userLocationLayer.resetAnchor();
215 }
216 }
217
218 private void updateFollowMeAnchor() {
219 if (userLocationLayer == null) {
220 return;
221 }
222
223 // Android view sizes and MotionEvent coordinates are already screen-space pixels.
224 PointF anchorPoint = new PointF(getWidth() / 2f, getHeight() / 2f);
225 userLocationLayer.setAnchor(anchorPoint);
226 }
227
231 public void setViewConfig(DefaultNavigationViewConfig newViewConfig) {
232 super.setViewConfig(newViewConfig);
233 }
234
246 public void setConfig(DefaultNavigationViewConfig newViewConfig,
247 ZoomControlsConfig newZoomConfig,
248 FollowMeButtonConfig newFollowMeConfig,
249 FloorSelectorViewConfig newFloorConfig) {
250 if (newFollowMeConfig != null) followMeButtonConfig = newFollowMeConfig;
251 super.setConfig(newViewConfig, newZoomConfig, newFloorConfig);
252 if (followMeButton != null) followMeButton.applyConfig(followMeButtonConfig);
254 }
255
257 @Override
259 return (DefaultNavigationViewConfig) super.getViewConfig();
260 }
261
263 public FollowMeButtonConfig getFollowMeButtonConfig() { return followMeButtonConfig; }
264
266 public FollowMeButton getFollowMeButton() { return followMeButton; }
267
268 @Override
269 protected void onAttachedToWindow() {
270 super.onAttachedToWindow();
271 registerCameraListener();
272 }
273
274 @Override
275 protected void onDetachedFromWindow() {
276 unregisterCameraListener();
277 super.onDetachedFromWindow();
278 }
279
280 @Override
281 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
282 super.onSizeChanged(w, h, oldw, oldh);
283
284 if (followMeButton != null && followMeButton.isFollowing()) {
285 updateFollowMeAnchor();
286 }
287 }
288}