Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RoutingLayout.java « widget « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0874ddeed2dc64de6d1c24e1abad5c38f439afe (plain)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package com.mapswithme.maps.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.os.Build;
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.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mapswithme.maps.Framework;
import com.mapswithme.maps.LocationState;
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.maps.routing.RoutingResultCodesProcessor;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.statistics.AlohaHelper;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * Layout for routing setup & turn instruction box.
 */
public class RoutingLayout extends RelativeLayout implements 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 mTvExitNum;
  private View mNextTurn;
  private ImageView mIvNextTurn;
  private TextView mTvTurnDistance;
  private TextView mTvPrepareDistance;
  private MapObject mEndPoint;
  private View mLayoutSetupRouting;
  private View mLayoutTurnInstructions;
  private View mBtnStart;
  private FlatProgressView mFpRouteProgress;
  private RadioGroup mRgRouterType;
  private TextView mTvNextStreet;
  private TextView mTvArrivalTime;

  private double mNorth;
  private RoutingInfo mCachedRoutingInfo;

  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);
    inflate(getContext(), R.layout.layout_routing_full, this);
    setClipToPadding(false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      setElevation(UiUtils.dimen(R.dimen.appbar_elevation));

    if (isInEditMode())
      return;

    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 = (TextView) mLayoutSetupRouting.findViewById(R.id.tv__planning_route);
    mRgRouterType = (RadioGroup) mLayoutSetupRouting.findViewById(R.id.rg__router);
    mRgRouterType.findViewById(R.id.rb__vehicle).setOnClickListener(this);
    mRgRouterType.findViewById(R.id.rb__pedestrian).setOnClickListener(this);
    mIvCancelRouteBuild = mLayoutSetupRouting.findViewById(R.id.iv__routing_close);
    mIvCancelRouteBuild.setOnClickListener(this);
    mTvPrepareDistance = (TextView) mLayoutSetupRouting.findViewById(R.id.tv__routing_distance);
    mTvPrepareTime = (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 = (TextView) mLayoutTurnInstructions.findViewById(R.id.tv__total_distance);
    mTvTotalTime = (TextView) mLayoutTurnInstructions.findViewById(R.id.tv__total_time);
    mTvArrivalTime = (TextView) mLayoutTurnInstructions.findViewById(R.id.tv__arrival_time);
    mIvTurn = (ImageView) mLayoutTurnInstructions.findViewById(R.id.iv__turn);
    mTvExitNum = (TextView) mLayoutTurnInstructions.findViewById(R.id.tv__exit_num);
    mNextTurn = findViewById(R.id.next_turn);
    mIvNextTurn = (ImageView) mNextTurn.findViewById(R.id.iv__next_turn);
    mTvTurnDistance = (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);
    mTvNextStreet = (TextView) mLayoutTurnInstructions.findViewById(R.id.tv__next_street);
  }

  @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;
    case R.id.rb__pedestrian:
      AlohaHelper.logClick(AlohaHelper.ROUTING_PEDESTRIAN_SET);
      Framework.setRouter(Framework.ROUTER_TYPE_PEDESTRIAN);
      mListener.onRouteTypeChange(Framework.ROUTER_TYPE_PEDESTRIAN);
      setState(State.PREPARING, true);
      break;
    case R.id.rb__vehicle:
      AlohaHelper.logClick(AlohaHelper.ROUTING_VEHICLE_SET);
      Framework.setRouter(Framework.ROUTER_TYPE_VEHICLE);
      mListener.onRouteTypeChange(Framework.ROUTER_TYPE_VEHICLE);
      setState(State.PREPARING, true);
      break;
    }
  }

  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, mTvExitNum);
      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);
      // TODO (marchuk): Uncomment after the second turn notification is fixed.
//      UiUtils.hide(mLayoutTurnInstructions, mTvPrepareDistance, mTvPrepareTime, mIvCancelRouteBuild, mNextTurn);
      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);
      // TODO (marchuk): Uncomment after the second turn notification is fixed.
//      UiUtils.hide(mLayoutTurnInstructions, mWvProgress, mTvPlanning, mNextTurn);
      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);
      // TODO (marchuk): Uncomment after the second turn notification is fixed.
//      UiUtils.hide(mLayoutTurnInstructions, mTvPrepareDistance, mTvPrepareTime, mWvProgress, mNextTurn);
      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;
    checkBestRouter();
  }

  public MapObject getEndPoint()
  {
    return mEndPoint;
  }

  public void updateRouteInfo()
  {
    mCachedRoutingInfo = Framework.nativeGetRouteFollowingInfo();
    if (mState == State.TURN_INSTRUCTIONS)
      refreshTurnInstructions();
  }

  public void setRouteBuildingProgress(float progress)
  {
    mWvProgress.setProgress((int) progress);
  }

  private void refreshTurnInstructions()
  {
    if (mCachedRoutingInfo == null)
      return;

    if (Framework.getRouter() == Framework.ROUTER_TYPE_VEHICLE)
      refreshVehicleInfo(mCachedRoutingInfo);
    else
      refreshPedestrianAzimutAndDistance(mCachedRoutingInfo);

    mTvTotalTime.setText(formatRoutingTime(mCachedRoutingInfo.totalTimeInSeconds));
    mTvTotalDistance.setText(buildSpannedText(UiUtils.dimen(R.dimen.text_size_routing_number), UiUtils.dimen(R.dimen.text_size_routing_dimension),
                                              mCachedRoutingInfo.distToTarget, mCachedRoutingInfo.targetUnits));
    mTvArrivalTime.setText(formatArrivalTime(mCachedRoutingInfo.totalTimeInSeconds));
    UiUtils.setTextAndHideIfEmpty(mTvNextStreet, mCachedRoutingInfo.nextStreet);
    mFpRouteProgress.setProgress((int) mCachedRoutingInfo.completionPercent);
  }

  private void refreshVehicleInfo(RoutingInfo routingInfo)
  {
    mTvTurnDistance.setText(buildSpannedText(UiUtils.dimen(R.dimen.text_size_display_1), UiUtils.dimen(R.dimen.text_size_toolbar),
                                             routingInfo.distToTurn, routingInfo.turnUnits));
    routingInfo.vehicleTurnDirection.setTurnDrawable(mIvTurn);
    if (RoutingInfo.VehicleTurnDirection.isRoundAbout(routingInfo.vehicleTurnDirection))
      UiUtils.setTextAndShow(mTvExitNum, String.valueOf(routingInfo.exitNum));
    else
      UiUtils.hide(mTvExitNum);

    // TODO (marchuk): Uncomment after the second turn notification is fixed.
//    if (routingInfo.vehicleNextTurnDirection.containsNextTurn())
//    {
//      UiUtils.appearSlidingDown(mNextTurn, null);
//      routingInfo.vehicleNextTurnDirection.setNextTurnDrawable(mIvNextTurn);
//    }
//    else
//      UiUtils.disappearSlidingUp(mNextTurn, null);
  }

  private void refreshPedestrianAzimutAndDistance(RoutingInfo info)
  {
    Location location = LocationHelper.INSTANCE.getLastLocation();
    DistanceAndAzimut distanceAndAzimut = Framework.nativeGetDistanceAndAzimutFromLatLon(
        info.pedestrianNextDirection.getLatitude(), info.pedestrianNextDirection.getLongitude(), location.getLatitude(), location.getLongitude(), mNorth);

    String[] splitDistance = distanceAndAzimut.getDistance().split(" ");
    mTvTurnDistance.setText(buildSpannedText(UiUtils.dimen(R.dimen.text_size_display_1), UiUtils.dimen(R.dimen.text_size_toolbar),
                                             splitDistance[0], splitDistance[1]));

    if (info.pedestrianTurnDirection != null)
      info.pedestrianTurnDirection.setTurnDrawable(mIvTurn, distanceAndAzimut);
  }

  private void refreshRouteSetup()
  {
    mCachedRoutingInfo = Framework.nativeGetRouteFollowingInfo();
    if (mCachedRoutingInfo == null)
      return;

    mTvPrepareDistance.setText(buildSpannedText(UiUtils.dimen(R.dimen.text_size_routing_number), UiUtils.dimen(R.dimen.text_size_routing_dimension),
                                                mCachedRoutingInfo.distToTarget, mCachedRoutingInfo.targetUnits));
    mTvPrepareTime.setText(formatRoutingTime(mCachedRoutingInfo.totalTimeInSeconds));
  }

  private static SpannableStringBuilder buildSpannedText(int mainTextSize, int unitsTextSize, String main, String units)
  {
    SpannableStringBuilder builder = new SpannableStringBuilder(main).append(" ").append(units);
    builder.setSpan(new AbsoluteSizeSpan(mainTextSize, false), 0, main.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setSpan(new AbsoluteSizeSpan(unitsTextSize, false), main.length(), builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return builder;
  }

  private static CharSequence formatRoutingTime(int seconds)
  {
    long minutes = TimeUnit.SECONDS.toMinutes(seconds) % 60;
    long hours = TimeUnit.SECONDS.toHours(seconds);
    if (hours == 0 && minutes == 0)
      // one minute is added to estimated time to destination point to prevent displaying zero minutes left
      minutes++;

    return hours == 0 ?
           buildSpannedText(UiUtils.dimen(R.dimen.text_size_routing_number), UiUtils.dimen(R.dimen.text_size_routing_dimension), String.valueOf(minutes), "min") :
           TextUtils.concat(buildSpannedText(UiUtils.dimen(R.dimen.text_size_routing_number), UiUtils.dimen(R.dimen.text_size_routing_dimension),
                                             String.valueOf(hours), "h "),
                            buildSpannedText(UiUtils.dimen(R.dimen.text_size_routing_number), UiUtils.dimen(R.dimen.text_size_routing_dimension),
                                             String.valueOf(minutes), "min"));
  }

  private static String formatArrivalTime(int seconds)
  {
    Calendar current = Calendar.getInstance();
    current.add(Calendar.SECOND, seconds);
    return String.format("%d:%02d", current.get(Calendar.HOUR_OF_DAY), current.get(Calendar.MINUTE));
  }

  private void buildRoute()
  {
    if (!MwmApplication.get().nativeGetBoolean(IS_ROUTING_DISCLAIMER_APPROVED, false))
    {
      showRoutingDisclaimer();
      return;
    }
    if (!LocationState.isTurnedOn())
    {
      onMissingLocation();
      return;
    }

    Location location = LocationHelper.INSTANCE.getLastLocation();
    if (location == null)
    {
      // TODO remove that hack after proper route reconstruction logic will be finished
      setState(State.HIDDEN, false);
      return;
    }
    Framework.nativeBuildRoute(location.getLatitude(), location.getLongitude(), mEndPoint.getLat(), mEndPoint.getLon());
  }

  private void checkBestRouter()
  {
    final Location location = LocationHelper.INSTANCE.getLastLocation();
    if (location == null || mEndPoint == null)
      return;

    final int bestRouter = Framework.nativeGetBestRouter(location.getLatitude(), location.getLongitude(), mEndPoint.getLat(), mEndPoint.getLon());
    mRgRouterType.check(bestRouter == Framework.ROUTER_TYPE_PEDESTRIAN ? R.id.rb__pedestrian : R.id.rb__vehicle);
    Framework.setRouter(bestRouter);
  }

  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();
  }

  private void onMissingLocation()
  {
    Context context = getContext();
    if (context instanceof Framework.RoutingListener)
      ((Framework.RoutingListener) context).onRoutingEvent(RoutingResultCodesProcessor.NO_POSITION, null, null);
  }

  @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];
      }
    };
  }
}