1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
package com.mapswithme.maps.widget;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MWMApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.DistanceAndAzimut;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.routing.RoutingInfo;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.statistics.AlohaHelper;
import java.util.concurrent.TimeUnit;
/**
* Layout for routing setup & turn instruction box.
*/
public class RoutingLayout extends FrameLayout implements CompoundButton.OnCheckedChangeListener, View.OnClickListener
{
private static final String IS_ROUTING_DISCLAIMER_APPROVED = "IsDisclaimerApproved";
private WheelProgressView mWvProgress;
private TextView mTvPlanning;
private View mIvCancelRouteBuild;
private TextView mTvPrepareTime;
private TextView mTvTotalDistance;
private TextView mTvTotalTime;
private ImageView mIvTurn;
private TextView mTvTurnDistance;
private TextView mTvPrepareDistance;
private MapObject mEndPoint;
private View mLayoutSetupRouting;
private View mLayoutTurnInstructions;
private View mBtnStart;
private FlatProgressView mFpRouteProgress;
private double mNorth;
public enum State
{
HIDDEN,
PREPARING,
ROUTE_BUILT,
ROUTE_BUILD_ERROR,
TURN_INSTRUCTIONS
}
private State mState = State.HIDDEN;
public interface ActionListener
{
void onCloseRouting();
void onStartRouteFollow();
void onRouteTypeChange(int type);
}
private ActionListener mListener;
public RoutingLayout(Context context)
{
this(context, null, 0);
}
public RoutingLayout(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public RoutingLayout(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.layout_routing_full, this);
initViews();
UiUtils.hide(this, mLayoutSetupRouting, mBtnStart, mLayoutTurnInstructions);
}
public void setListener(ActionListener listener)
{
mListener = listener;
}
public void refreshAzimuth(double north)
{
mNorth = north;
if (mState == State.TURN_INSTRUCTIONS)
refreshTurnInstructions();
}
private void initViews()
{
mLayoutSetupRouting = findViewById(R.id.layout__routing_setup);
mWvProgress = (WheelProgressView) mLayoutSetupRouting.findViewById(R.id.wp__routing_progress);
mWvProgress.setOnClickListener(this);
mTvPlanning = (android.widget.TextView) mLayoutSetupRouting.findViewById(R.id.tv__planning_route);
RadioButton rbPedestrian = (RadioButton) mLayoutSetupRouting.findViewById(R.id.rb__pedestrian);
RadioButton rbVehicle = (RadioButton) mLayoutSetupRouting.findViewById(R.id.rb__vehicle);
if (Framework.getRouter() == Framework.ROUTER_TYPE_PEDESTRIAN)
rbPedestrian.setChecked(true);
else
rbVehicle.setChecked(true);
rbPedestrian.setOnCheckedChangeListener(this);
rbVehicle.setOnCheckedChangeListener(this);
mIvCancelRouteBuild = mLayoutSetupRouting.findViewById(R.id.iv__routing_close);
mIvCancelRouteBuild.setOnClickListener(this);
mTvPrepareDistance = (android.widget.TextView) mLayoutSetupRouting.findViewById(R.id.tv__routing_distance);
mTvPrepareTime = (android.widget.TextView) mLayoutSetupRouting.findViewById(R.id.tv__routing_time);
mBtnStart = mLayoutSetupRouting.findViewById(R.id.btn__start_routing);
mBtnStart.setOnClickListener(this);
mLayoutTurnInstructions = findViewById(R.id.layout__turn_instructions);
mTvTotalDistance = (android.widget.TextView) mLayoutTurnInstructions.findViewById(R.id.tv__total_distance);
mTvTotalTime = (android.widget.TextView) mLayoutTurnInstructions.findViewById(R.id.tv__total_time);
mIvTurn = (ImageView) mLayoutTurnInstructions.findViewById(R.id.iv__turn);
mTvTurnDistance = (android.widget.TextView) mLayoutTurnInstructions.findViewById(R.id.tv__turn_distance);
mLayoutTurnInstructions.findViewById(R.id.btn__close).setOnClickListener(this);
mFpRouteProgress = (FlatProgressView) mLayoutTurnInstructions.findViewById(R.id.fp__route_progress);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn__close:
AlohaHelper.logClick(AlohaHelper.ROUTING_CLOSE);
setState(State.HIDDEN, true);
mListener.onCloseRouting();
break;
case R.id.iv__routing_close:
AlohaHelper.logClick(AlohaHelper.ROUTING_GO_CLOSE);
setState(State.HIDDEN, true);
mListener.onCloseRouting();
break;
case R.id.wp__routing_progress:
AlohaHelper.logClick(AlohaHelper.ROUTING_PROGRESS_CLOSE);
setState(State.HIDDEN, true);
mListener.onCloseRouting();
break;
case R.id.btn__start_routing:
AlohaHelper.logClick(AlohaHelper.ROUTING_GO);
setState(State.TURN_INSTRUCTIONS, true);
Framework.nativeFollowRoute();
mListener.onStartRouteFollow();
break;
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (!isChecked)
return;
if (buttonView.getId() == R.id.rb__pedestrian)
{
AlohaHelper.logClick(AlohaHelper.ROUTING_PEDESTRIAN_SET);
Framework.setRouter(Framework.ROUTER_TYPE_PEDESTRIAN);
mListener.onRouteTypeChange(Framework.ROUTER_TYPE_PEDESTRIAN);
}
else
{
AlohaHelper.logClick(AlohaHelper.ROUTING_VEHICLE_SET);
Framework.setRouter(Framework.ROUTER_TYPE_VEHICLE);
mListener.onRouteTypeChange(Framework.ROUTER_TYPE_VEHICLE);
}
setState(State.PREPARING, true);
}
public void setState(State state, boolean animated)
{
// TODO show routing progress after its implemented.
mState = state;
switch (mState)
{
case HIDDEN:
if (animated)
UiUtils.disappearSlidingUp(this, null);
else
UiUtils.hide(this);
UiUtils.hide(mBtnStart);
Framework.nativeCloseRouting();
mEndPoint = null;
break;
case PREPARING:
if (mEndPoint == null)
throw new IllegalStateException("End point should be not null to prepare routing");
Framework.nativeCloseRouting();
UiUtils.show(mLayoutSetupRouting, mWvProgress, mTvPlanning);
UiUtils.hide(mLayoutTurnInstructions, mTvPrepareDistance, mTvPrepareTime, mIvCancelRouteBuild);
mTvPlanning.setText(R.string.routing_planning);
mWvProgress.setProgress(0);
if (animated)
{
UiUtils.appearSlidingDown(this, null);
UiUtils.disappearSlidingUp(mBtnStart, null);
}
else
{
UiUtils.show(this);
UiUtils.hide(mBtnStart);
}
buildRoute();
break;
case ROUTE_BUILT:
UiUtils.show(this, mLayoutSetupRouting, mTvPrepareDistance, mTvPrepareTime, mIvCancelRouteBuild);
UiUtils.hide(mLayoutTurnInstructions, mWvProgress, mTvPlanning);
if (animated)
UiUtils.appearSlidingDown(mBtnStart, null);
else
UiUtils.show(mBtnStart);
refreshRouteSetup();
break;
case ROUTE_BUILD_ERROR:
UiUtils.show(mLayoutSetupRouting, mIvCancelRouteBuild, mTvPlanning);
UiUtils.hide(mLayoutTurnInstructions, mTvPrepareDistance, mTvPrepareTime, mWvProgress);
mTvPlanning.setText(R.string.routing_planning_error);
break;
case TURN_INSTRUCTIONS:
UiUtils.show(this, mLayoutTurnInstructions);
UiUtils.disappearSlidingUp(mLayoutSetupRouting, null);
refreshTurnInstructions();
break;
}
}
public State getState()
{
return mState;
}
public void setEndPoint(MapObject mapObject)
{
mEndPoint = mapObject;
}
public MapObject getEndPoint()
{
return mEndPoint;
}
public void updateRouteInfo()
{
if (mState == State.TURN_INSTRUCTIONS)
refreshTurnInstructions();
}
public void setRouteBuildingProgress(float progress)
{
mWvProgress.setProgress((int) progress);
}
private void refreshTurnInstructions()
{
RoutingInfo info = Framework.nativeGetRouteFollowingInfo();
if (Framework.getRouter() == Framework.ROUTER_TYPE_VEHICLE)
{
mTvTurnDistance.setText(getSpannedDistance(getResources().getDimensionPixelSize(R.dimen.text_size_display_1),
getResources().getDimensionPixelSize(R.dimen.text_size_toolbar), info.distToTurn, info.turnUnits.toLowerCase()));
info.vehicleTurnDirection.setTurnDrawable(mIvTurn);
}
else
refreshPedestrianAzimutAndDistance(info);
mTvTotalTime.setText(formatTime(info.totalTimeInSeconds));
mTvTotalDistance.setText(info.distToTarget + " " + info.targetUnits);
}
private void refreshPedestrianAzimutAndDistance(RoutingInfo info)
{
Location location = LocationHelper.INSTANCE.getLastLocation();
DistanceAndAzimut distanceAndAzimut = Framework.nativeGetDistanceAndAzimutFromLatLon(
mEndPoint.getLat(), mEndPoint.getLon(), location.getLatitude(), location.getLongitude(), mNorth);
String[] splitDistance = distanceAndAzimut.getDistance().split(" ");
mTvTurnDistance.setText(getSpannedDistance(getResources().getDimensionPixelSize(R.dimen.text_size_display_1),
getResources().getDimensionPixelSize(R.dimen.text_size_toolbar), splitDistance[0], splitDistance[1].toLowerCase()));
if (info.pedestrianTurnDirection != null)
info.pedestrianTurnDirection.setTurnDrawable(mIvTurn, distanceAndAzimut);
}
private void refreshRouteSetup()
{
RoutingInfo info = Framework.nativeGetRouteFollowingInfo();
mTvPrepareDistance.setText(info.distToTarget + " " + info.targetUnits.toUpperCase());
mTvPrepareTime.setText(formatTime(info.totalTimeInSeconds));
}
private SpannableStringBuilder getSpannedDistance(int distTextSize, int unitsTextSize, String distToTarget, String units)
{
SpannableStringBuilder builder = new SpannableStringBuilder(distToTarget).append(" ").append(units.toUpperCase());
builder.setSpan(new AbsoluteSizeSpan(distTextSize, false), 0, distToTarget.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(new AbsoluteSizeSpan(unitsTextSize, false), distToTarget.length(), builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
private String formatTime(int seconds)
{
long minutes = TimeUnit.SECONDS.toMinutes(seconds);
long hours = TimeUnit.MINUTES.toHours(minutes);
if (hours == 0 && minutes == 0)
// one minute is added to estimated time to destination point to prevent displaying that zero minutes are left to the finish near destination point
minutes++;
return String.format("%d:%02d", hours, minutes - TimeUnit.HOURS.toMinutes(hours));
}
private void buildRoute()
{
if (!MWMApplication.get().nativeGetBoolean(IS_ROUTING_DISCLAIMER_APPROVED, false))
{
showRoutingDisclaimer();
return;
}
Framework.nativeBuildRoute(mEndPoint.getLat(), mEndPoint.getLon());
}
private void showRoutingDisclaimer()
{
StringBuilder builder = new StringBuilder();
for (int resId : new int[]{R.string.dialog_routing_disclaimer_priority, R.string.dialog_routing_disclaimer_precision,
R.string.dialog_routing_disclaimer_recommendations, R.string.dialog_routing_disclaimer_beware})
builder.append(getContext().getString(resId)).append("\n\n");
new AlertDialog.Builder(getContext())
.setTitle(R.string.dialog_routing_disclaimer_title)
.setMessage(builder.toString())
.setCancelable(false)
.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dlg, int which)
{
MWMApplication.get().nativeSetBoolean(IS_ROUTING_DISCLAIMER_APPROVED, true);
dlg.dismiss();
buildRoute();
}
})
.setNegativeButton(getContext().getString(R.string.cancel), null)
.show();
}
@Override
protected Parcelable onSaveInstanceState()
{
Parcelable parentState = super.onSaveInstanceState();
SavedState savedState = new SavedState(parentState);
savedState.object = getEndPoint();
savedState.routingStateOrdinal = mState.ordinal();
return savedState;
}
@Override
protected void onRestoreInstanceState(Parcelable state)
{
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
setEndPoint(savedState.object);
// if route was build but it was lost before state was restored - we should rebuild again from scratch
int correctOrdinal;
if (savedState.routingStateOrdinal > State.PREPARING.ordinal() && !Framework.nativeIsRouteBuilt())
correctOrdinal = State.PREPARING.ordinal();
else
correctOrdinal = savedState.routingStateOrdinal;
setState(State.values()[correctOrdinal], false);
}
static class SavedState extends BaseSavedState
{
int routingStateOrdinal;
MapObject object;
public SavedState(Parcel source)
{
super(source);
routingStateOrdinal = source.readInt();
object = source.readParcelable(MapObject.class.getClassLoader());
}
public SavedState(Parcelable superState)
{
super(superState);
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags)
{
super.writeToParcel(dest, flags);
dest.writeInt(routingStateOrdinal);
dest.writeParcelable(object, 0);
}
public static final Creator<SavedState> CREATOR = new Creator<SavedState>()
{
@Override
public SavedState createFromParcel(Parcel source)
{
return new SavedState(source);
}
@Override
public SavedState[] newArray(int size)
{
return new SavedState[0];
}
};
}
}
|