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.Color;
5import android.graphics.PointF;
6import android.util.AttributeSet;
7import android.view.Gravity;
8import android.view.ViewGroup;
9import android.widget.FrameLayout;
10
11import com.navigine.idl.java.CameraListener;
12import com.navigine.idl.java.CameraUpdateReason;
13import com.navigine.idl.java.Location;
14import com.navigine.idl.java.LocationListener;
15import com.navigine.idl.java.LocationManager;
16import com.navigine.idl.java.NavigineSdk;
17import com.navigine.idl.java.Sublocation;
18import com.navigine.idl.java.SublocationChangeListener;
19import com.navigine.view.widgets.FloorSelectorView;
20import com.navigine.view.widgets.ZoomControls;
21import com.navigine.view.widgets.FollowMeButton;
22import com.navigine.idl.java.UserLocationLayer;
23
24import java.util.ArrayList;
25import java.util.List;
26
39public class DefaultNavigationView extends LocationView {
40 public static final String TAG = DefaultNavigationView.class.getCanonicalName();
41
43 public DefaultNavigationView(final Context context) {
44 super(context);
45 setupUI();
46 }
47
49 public DefaultNavigationView(final Context context, final AttributeSet attrs) {
50 super(context, attrs);
51 setupUI();
52 }
53
54 @Override
55 protected void onDetachedFromWindow() {
56 locationManager.removeLocationListener(locationListener);
57 locationWindow.removeSublocationChangeListener(sublocationChangeListener);
58 locationWindow.removeCameraListener(cameraListener);
59 super.onDetachedFromWindow();
60 }
61
63 private void setupUI() {
64 sublocationChangeListener = new SublocationChangeListener() {
65 @Override
66 public void onActiveSublocationChanged(int sublocationId) {
67 floorSelector.setSublocationId(sublocationId);
68 }
69 };
70 locationWindow.addSublocationChangeListener(sublocationChangeListener);
71
72 ZoomControls zoomControls = new ZoomControls(getContext(),
73 this::zoomInPressed,
74 this::zoomOutPressed);
75 FrameLayout.LayoutParams zoomLp = new FrameLayout.LayoutParams(
76 ViewGroup.LayoutParams.WRAP_CONTENT,
77 ViewGroup.LayoutParams.WRAP_CONTENT
78 );
79 zoomLp.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
80 addView(zoomControls, zoomLp);
81
82 followMeButton = new FollowMeButton(getContext(),
83 this::followMePressed);
84 FrameLayout.LayoutParams followLp = new FrameLayout.LayoutParams(
85 ViewGroup.LayoutParams.WRAP_CONTENT,
86 ViewGroup.LayoutParams.WRAP_CONTENT
87 );
88 followLp.gravity = Gravity.END | Gravity.BOTTOM;
89 followLp.setMargins(0, 0, dpToPx((int) com.navigine.view.widgets.WidgetStyles.STANDARD_RIGHT_PADDING),
90 dpToPx((int) com.navigine.view.widgets.WidgetStyles.FOLLOW_ME_BOTTOM_PADDING));
91 addView(followMeButton, followLp);
92
93 floorSelector = new FloorSelectorView(getContext(),
94 (sublocationId) -> locationWindow.setSublocationId(sublocationId));
95
96 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
97 ViewGroup.LayoutParams.WRAP_CONTENT,
98 ViewGroup.LayoutParams.WRAP_CONTENT
99 );
100 lp.gravity = Gravity.START | Gravity.TOP;
101 lp.setMargins(dpToPx((int) com.navigine.view.widgets.WidgetStyles.STANDARD_LEFT_PADDING),
102 dpToPx((int) com.navigine.view.widgets.WidgetStyles.FLOOR_SELECTOR_TOP_PADDING), 0, 0);
103 floorSelector.setVisibility(GONE);
104
105 addView(floorSelector, lp);
106
107 locationManager = NavigineSdk.getInstance().getLocationManager();
108 locationListener = new LocationListener() {
109 @Override
110 public void onLocationLoaded(Location location) {
111 if (location != null) {
112 floorSelector.setVisibility(VISIBLE);
113 } else {
114 return;
115 }
116 List<FloorSelectorView.LevelInfo> floors = new ArrayList<>();
117 for(Sublocation sublocation : location.getSublocations()) {
118 floors.add(new FloorSelectorView.LevelInfo(sublocation.getLevelId(), sublocation.getId()));
119 }
120
121 floorSelector.setFloors(floors);
122 }
123
124 @Override
125 public void onLocationFailed(int i, Error error) {
126 }
127
128 @Override
129 public void onLocationUploaded(int locationId) {
130 }
131
132 };
133 locationManager.addLocationListener(locationListener);
134
135 userLocationLayer = NavigineSdk.getInstance().getUserLocationLayer(locationWindow);
136 userLocationLayer.setVisible(true);
137
138 cameraListener = new CameraListener() {
139 @Override
140 public void onCameraPositionChanged(CameraUpdateReason reason, boolean finished) {
141 if (reason == CameraUpdateReason.APPLICATION) {
142 return;
143 }
144
145 if (followMeButton != null && followMeButton.isFollowing()) {
146 userLocationLayer.resetAnchor();
147 followMeButton.updateAppearanceForFollowingState(false);
148 }
149 }
150 };
151 locationWindow.addCameraListener(cameraListener);
152 }
153
154
156 private void zoomInPressed() {
157 float currentZoom = locationWindow.getZoomFactor();
158 locationWindow.setZoomFactor(currentZoom * (3.f / 2.f));
159 }
160
162 private void zoomOutPressed() {
163 float currentZoom = locationWindow.getZoomFactor();
164 locationWindow.setZoomFactor(currentZoom * (2.f / 3.f));
165 }
166
167 private int dpToPx(int dp) {
168 return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
169 }
170
175 private void followMePressed(boolean isFollowing) {
176 if (isFollowing) {
177 float centerX = getWidth() / 2f;
178 float centerY = getHeight() / 2f;
179 PointF anchorPoint = new PointF(centerX, centerY);
180 userLocationLayer.setAnchor(anchorPoint);
181 } else {
182 userLocationLayer.resetAnchor();
183 }
184 }
185
186 private FloorSelectorView floorSelector;
187 private LocationManager locationManager;
188 private LocationListener locationListener;
189 private SublocationChangeListener sublocationChangeListener;
190 private UserLocationLayer userLocationLayer;
191 private CameraListener cameraListener;
192 private FollowMeButton followMeButton;
193}