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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
package com.mapswithme.maps.widget.placepage;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GestureDetectorCompat;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.ads.CompoundNativeAdLoader;
import com.mapswithme.maps.ads.DefaultAdTracker;
import com.mapswithme.maps.ads.MwmNativeAd;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.bookmarks.data.RoadWarningMarkType;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.location.LocationListener;
import com.mapswithme.maps.promo.Promo;
import com.mapswithme.maps.purchase.AdsRemovalPurchaseControllerProvider;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.statistics.PlacePageTracker;
import com.trafi.anchorbottomsheetbehavior.AnchorBottomSheetBehavior;
public class BottomSheetPlacePageController implements PlacePageController, LocationListener,
View.OnLayoutChangeListener,
BannerController.BannerStateRequester,
BannerController.BannerStateListener,
Closable
{
private static final float ANCHOR_RATIO = 0.3f;
private static final float PREVIEW_PLUS_RATIO = 0.45f;
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = BottomSheetPlacePageController.class.getSimpleName();
private static final String EXTRA_MAP_OBJECT = "extra_map_object";
private static final int ANIM_BANNER_APPEARING_MS = 300;
private static final int ANIM_CHANGE_PEEK_HEIGHT_MS = 100;
@NonNull
private final Activity mActivity;
@SuppressWarnings("NullableProblems")
@NonNull
private AnchorBottomSheetBehavior<View> mPlacePageBehavior;
@SuppressWarnings("NullableProblems")
@NonNull
private View mButtonsLayout;
@SuppressWarnings("NullableProblems")
@NonNull
private PlacePageView mPlacePage;
@SuppressWarnings("NullableProblems")
@NonNull
private PlacePageTracker mPlacePageTracker;
@SuppressWarnings("NullableProblems")
@NonNull
private Toolbar mToolbar;
private int mViewportMinHeight;
private int mCurrentTop;
private boolean mPeekHeightAnimating;
private int mOpenBannerTouchSlop;
/**
* Represents a value that describes how much banner details are opened.
* Must be in the range [0;1]. 0 means that the banner details are completely closed,
* 1 - the details are completely opened.
*/
private float mBannerRatio;
@SuppressWarnings("NullableProblems")
@NonNull
private BannerController mBannerController;
@NonNull
private final AdsRemovalPurchaseControllerProvider mPurchaseControllerProvider;
@NonNull
private final SlideListener mSlideListener;
@NonNull
private final GestureDetectorCompat mGestureDetector;
@Nullable
private final RoutingModeListener mRoutingModeListener;
@NonNull
private final AnchorBottomSheetBehavior.BottomSheetCallback mSheetCallback
= new AnchorBottomSheetBehavior.BottomSheetCallback()
{
@Override
public void onStateChanged(@NonNull View bottomSheet, int oldState, int newState)
{
LOGGER.d(TAG, "State change, new = " + BottomSheetPlacePageController.toString(newState)
+ " old = " + BottomSheetPlacePageController.toString(oldState)
+ " placepage height = " + mPlacePage.getHeight());
if (isSettlingState(newState) || isDraggingState(newState))
{
return;
}
if (isHiddenState(newState))
{
onHiddenInternal();
return;
}
setPullDrawable();
if (isAnchoredState(newState) || isExpandedState(newState))
{
mBannerController.onPlacePageStateChanged();
mPlacePageTracker.onDetails();
return;
}
mBannerController.onPlacePageStateChanged();
setPeekHeight();
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset)
{
mSlideListener.onPlacePageSlide(bottomSheet.getTop());
mPlacePageTracker.onMove();
if (slideOffset < 0)
return;
updateViewPortRect();
resizeBanner();
}
};
private void onHiddenInternal()
{
Framework.nativeDeactivatePopup();
updateViewPortRect();
UiUtils.invisible(mButtonsLayout);
mPlacePageTracker.onHidden();
}
private void setPullDrawable()
{
@AnchorBottomSheetBehavior.State
int state = mPlacePageBehavior.getState();
@DrawableRes
int drawableId = UiUtils.NO_ID;
if (isCollapsedState(state))
drawableId = R.drawable.ic_disclosure_up;
else if (isAnchoredState(state) || isExpandedState(state))
drawableId = R.drawable.ic_disclosure_down;
if (drawableId == UiUtils.NO_ID)
return;
ImageView img = mPlacePage.findViewById(R.id.pull_icon);
Drawable drawable = Graphics.tint(mActivity, drawableId, R.attr.bannerButtonBackgroundColor);
img.setImageDrawable(drawable);
}
private void resizeBanner()
{
int lastTop = mCurrentTop;
mCurrentTop = mPlacePage.getTop();
if (!mBannerController.hasAd())
return;
int bannerMaxY = calculateBannerMaxY();
int bannerMinY = calculateBannerMinY();
int maxDistance = Math.abs(bannerMaxY - bannerMinY);
int yDistance = Math.abs(mCurrentTop - bannerMinY);
float ratio = (float) yDistance / maxDistance;
mBannerRatio = ratio;
if (ratio >= 1)
{
mBannerController.zoomOut(1);
mBannerController.open();
return;
}
if (ratio == 0)
{
mBannerController.zoomIn(ratio);
mBannerController.close();
return;
}
if (mCurrentTop < lastTop)
mBannerController.zoomOut(ratio);
else
mBannerController.zoomIn(ratio);
}
private int calculateBannerMaxY()
{
View coordinatorLayout = (ViewGroup) mPlacePage.getParent();
int height = coordinatorLayout.getHeight();
int maxY = mPlacePage.getHeight() > height * (1 - ANCHOR_RATIO)
? (int) (height * ANCHOR_RATIO) : height - mPlacePage.getHeight();
return maxY + mOpenBannerTouchSlop;
}
private int calculateBannerMinY()
{
View coordinatorLayout = (ViewGroup) mPlacePage.getParent();
int height = coordinatorLayout.getHeight();
return height - mPlacePageBehavior.getPeekHeight();
}
public BottomSheetPlacePageController(@NonNull Activity activity,
@NonNull AdsRemovalPurchaseControllerProvider provider,
@NonNull SlideListener listener,
@Nullable RoutingModeListener routingModeListener)
{
mActivity = activity;
mPurchaseControllerProvider = provider;
mSlideListener = listener;
mGestureDetector = new GestureDetectorCompat(activity, new PlacePageGestureListener());
mRoutingModeListener = routingModeListener;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void initialize()
{
Resources res = mActivity.getResources();
mViewportMinHeight = res.getDimensionPixelSize(R.dimen.viewport_min_height);
mOpenBannerTouchSlop = res.getDimensionPixelSize(R.dimen.placepage_banner_open_touch_slop);
mToolbar = mActivity.findViewById(R.id.pp_toolbar);
UiUtils.extendViewWithStatusBar(mToolbar);
UiUtils.showHomeUpButton(mToolbar);
mToolbar.setNavigationOnClickListener(v -> close());
mPlacePage = mActivity.findViewById(R.id.placepage);
mPlacePageBehavior = AnchorBottomSheetBehavior.from(mPlacePage);
mPlacePageBehavior.addBottomSheetCallback(mSheetCallback);
mPlacePage.setOnTouchListener((v, event) -> mGestureDetector.onTouchEvent(event));
mPlacePage.addOnLayoutChangeListener(this);
mPlacePage.addClosable(this);
mPlacePage.setRoutingModeListener(mRoutingModeListener);
ViewGroup bannerContainer = mPlacePage.findViewById(R.id.banner_container);
DefaultAdTracker tracker = new DefaultAdTracker();
CompoundNativeAdLoader loader = com.mapswithme.maps.ads.Factory.createCompoundLoader(tracker,
tracker);
mBannerController = new BannerController(bannerContainer, loader, tracker,
mPurchaseControllerProvider, this, this);
mButtonsLayout = mActivity.findViewById(R.id.pp_buttons_layout);
ViewGroup buttons = mButtonsLayout.findViewById(R.id.container);
mPlacePage.initButtons(buttons);
UiUtils.bringViewToFrontOf(mButtonsLayout, mPlacePage);
UiUtils.bringViewToFrontOf(mActivity.findViewById(R.id.app_bar), mPlacePage);
mPlacePageTracker = new PlacePageTracker(mPlacePage, mButtonsLayout);
LocationHelper.INSTANCE.addListener(this);
}
@Override
public void destroy()
{
LocationHelper.INSTANCE.removeListener(this);
}
@Override
public void openFor(@NonNull MapObject object)
{
mPlacePage.setMapObject(object, (policy, isSameObject) -> {
@AnchorBottomSheetBehavior.State
int state = mPlacePageBehavior.getState();
if (isSameObject && !isHiddenState(state))
return;
mBannerRatio = 0;
mPlacePage.resetScroll();
if (object.getOpeningMode() == MapObject.OPENING_MODE_DETAILS)
{
mPlacePageBehavior.setState(AnchorBottomSheetBehavior.STATE_ANCHORED);
return;
}
UiUtils.show(mButtonsLayout);
openPlacePage();
showBanner(object, policy);
});
mToolbar.setTitle(object.getTitle());
mPlacePageTracker.setMapObject(object);
Framework.logLocalAdsEvent(Framework.LocalAdsEventType.LOCAL_ADS_EVENT_OPEN_INFO, object);
}
private void showBanner(@NonNull MapObject object, NetworkPolicy policy)
{
boolean canShowBanner = object.getMapObjectType() != MapObject.MY_POSITION
&& policy.canUseNetwork()
&& object.getRoadWarningMarkType() == RoadWarningMarkType.UNKNOWN;
mBannerController.updateData(canShowBanner ? object.getBanners() : null);
}
private void openPlacePage()
{
mPlacePage.post(() -> {
setPeekHeight();
mPlacePageBehavior.setState(AnchorBottomSheetBehavior.STATE_COLLAPSED);
setPlacePageAnchor();
});
}
private void setPeekHeight()
{
if (mPeekHeightAnimating)
{
Log.d(TAG, "Peek animation in progress, ignore.");
return;
}
// If banner details are little bit or completely opened we haven't to change the peek height,
// because the peek height is reasonable only for collapsed state and banner details are always
// closed in collapsed state.
if (mBannerRatio > 0)
return;
final int peekHeight = calculatePeekHeight();
if (peekHeight == mPlacePageBehavior.getPeekHeight())
return;
@AnchorBottomSheetBehavior.State
int currentState = mPlacePageBehavior.getState();
if (isSettlingState(currentState) || isDraggingState(currentState))
{
LOGGER.d(TAG, "Sheet state inappropriate, ignore.");
return;
}
if (isCollapsedState(currentState) && mPlacePageBehavior.getPeekHeight() > 0)
{
setPeekHeightAnimatedly(peekHeight);
return;
}
mPlacePageBehavior.setPeekHeight(peekHeight);
}
private void setPeekHeightAnimatedly(int peekHeight)
{
int delta = peekHeight - mPlacePageBehavior.getPeekHeight();
ObjectAnimator animator = ObjectAnimator.ofFloat(mPlacePage, "translationY", -delta);
animator.setDuration(delta == mBannerController.getClosedHeight() ? ANIM_BANNER_APPEARING_MS
: ANIM_CHANGE_PEEK_HEIGHT_MS);
animator.addListener(new UiUtils.SimpleAnimatorListener()
{
@Override
public void onAnimationStart(Animator animation)
{
mPeekHeightAnimating = true;
mPlacePage.setScrollable(false);
mPlacePageBehavior.setAllowUserDragging(false);
}
@Override
public void onAnimationEnd(Animator animation)
{
mPlacePage.setTranslationY(0);
mPeekHeightAnimating = false;
mPlacePage.setScrollable(true);
mPlacePageBehavior.setAllowUserDragging(true);
mPlacePageBehavior.setPeekHeight(peekHeight);
}
});
animator.addUpdateListener(animation -> onUpdateTranslation());
animator.start();
}
private void onUpdateTranslation()
{
mSlideListener.onPlacePageSlide((int) (mPlacePage.getTop() + mPlacePage.getTranslationY()));
}
private void setPlacePageAnchor()
{
View parent = (View) mPlacePage.getParent();
mPlacePageBehavior.setAnchorOffset((int) (parent.getHeight() * ANCHOR_RATIO));
}
private int calculatePeekHeight()
{
final int organicPeekHeight = mPlacePage.getPreviewHeight() + mButtonsLayout.getHeight();
final MapObject object = mPlacePage.getMapObject();
if (object != null)
{
@MapObject.OpeningMode
int mode = object.getOpeningMode();
if (mode == MapObject.OPENING_MODE_PREVIEW_PLUS)
{
View parent = (View) mPlacePage.getParent();
int promoPeekHeight = (int) (parent.getHeight() * PREVIEW_PLUS_RATIO);
return promoPeekHeight <= organicPeekHeight ? organicPeekHeight : promoPeekHeight;
}
}
return organicPeekHeight;
}
@Override
public void close()
{
mPlacePageBehavior.setState(AnchorBottomSheetBehavior.STATE_HIDDEN);
mPlacePage.reset();
}
@Override
public boolean isClosed()
{
return isHiddenState(mPlacePageBehavior.getState());
}
@Override
public void onLocationUpdated(Location location)
{
mPlacePage.refreshLocation(location);
}
@Override
public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy)
{
@AnchorBottomSheetBehavior.State
int currentState = mPlacePageBehavior.getState();
if (isHiddenState(currentState) || isDraggingState(currentState) || isSettlingState(currentState))
return;
double north = trueNorth >= 0.0 ? trueNorth : magneticNorth;
mPlacePage.refreshAzimuth(north);
}
@Override
public void onLocationError(int errorCode)
{
// Do nothing by default.
}
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int
oldTop, int oldRight, int oldBottom)
{
if (mPlacePageBehavior.getPeekHeight() == 0)
{
LOGGER.d(TAG, "Layout change ignored, peek height not calculated yet");
return;
}
mPlacePage.post(this::setPeekHeight);
if (isHiddenState(mPlacePageBehavior.getState()))
return;
updateViewPortRect();
}
private void updateViewPortRect()
{
mPlacePage.post(() -> {
View coordinatorLayout = (ViewGroup) mPlacePage.getParent();
int viewPortWidth = coordinatorLayout.getWidth();
int viewPortHeight = coordinatorLayout.getHeight();
Rect sheetRect = new Rect();
mPlacePage.getGlobalVisibleRect(sheetRect);
if (sheetRect.top < mViewportMinHeight)
return;
if (sheetRect.top >= viewPortHeight)
{
Framework.nativeSetVisibleRect(0, 0, viewPortWidth, viewPortHeight);
return;
}
viewPortHeight -= sheetRect.height();
Framework.nativeSetVisibleRect(0, 0, viewPortWidth, viewPortHeight);
});
}
@NonNull
private static String toString(@AnchorBottomSheetBehavior.State int state)
{
switch (state)
{
case AnchorBottomSheetBehavior.STATE_EXPANDED:
return "EXPANDED";
case AnchorBottomSheetBehavior.STATE_COLLAPSED:
return "COLLAPSED";
case AnchorBottomSheetBehavior.STATE_ANCHORED:
return "ANCHORED";
case AnchorBottomSheetBehavior.STATE_DRAGGING:
return "DRAGGING";
case AnchorBottomSheetBehavior.STATE_SETTLING:
return "SETTLING";
case AnchorBottomSheetBehavior.STATE_HIDDEN:
return "HIDDEN";
default:
throw new AssertionError("Unsupported state detected: " + state);
}
}
@Override
public void onSave(@NonNull Bundle outState)
{
mPlacePageTracker.onSave(outState);
outState.putParcelable(EXTRA_MAP_OBJECT, mPlacePage.getMapObject());
}
@Override
public void onRestore(@NonNull Bundle inState)
{
mPlacePageTracker.onRestore(inState);
if (mPlacePageBehavior.getState() == AnchorBottomSheetBehavior.STATE_HIDDEN)
return;
MapObject object = inState.getParcelable(EXTRA_MAP_OBJECT);
if (object == null)
return;
@AnchorBottomSheetBehavior.State
int state = mPlacePageBehavior.getState();
mPlacePage.setMapObject(object, (policy, isSameObject) -> {
restorePlacePageState(object, policy, state);
});
mToolbar.setTitle(object.getTitle());
}
private void restorePlacePageState(@NonNull MapObject object, @NonNull NetworkPolicy policy,
@AnchorBottomSheetBehavior.State int state)
{
mPlacePage.post(() -> {
setPlacePageAnchor();
mPlacePageBehavior.setState(state);
UiUtils.show(mButtonsLayout);
setPeekHeight();
showBanner(object, policy);
setPullDrawable();
});
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState)
{
}
@Override
public void onActivityStarted(Activity activity)
{
mBannerController.attach();
mPlacePage.attach(activity);
}
@Override
public void onActivityResumed(Activity activity)
{
mBannerController.onChangedVisibility(true);
}
@Override
public void onActivityPaused(Activity activity)
{
mBannerController.onChangedVisibility(false);
}
@Override
public void onActivityStopped(Activity activity)
{
mBannerController.detach();
mPlacePage.detach();
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState)
{
// No op.
}
@Override
public void onActivityDestroyed(Activity activity)
{
Promo.INSTANCE.setListener(null);
}
private static boolean isSettlingState(@AnchorBottomSheetBehavior.State int state)
{
return state == AnchorBottomSheetBehavior.STATE_SETTLING;
}
private static boolean isDraggingState(@AnchorBottomSheetBehavior.State int state)
{
return state == AnchorBottomSheetBehavior.STATE_DRAGGING;
}
private static boolean isCollapsedState(@AnchorBottomSheetBehavior.State int state)
{
return state == AnchorBottomSheetBehavior.STATE_COLLAPSED;
}
private static boolean isAnchoredState(@AnchorBottomSheetBehavior.State int state)
{
return state == AnchorBottomSheetBehavior.STATE_ANCHORED;
}
private static boolean isExpandedState(@AnchorBottomSheetBehavior.State int state)
{
return state == AnchorBottomSheetBehavior.STATE_EXPANDED;
}
private static boolean isHiddenState(@AnchorBottomSheetBehavior.State int state)
{
return state == AnchorBottomSheetBehavior.STATE_HIDDEN;
}
@Nullable
@Override
public BannerController.BannerState requestBannerState()
{
@AnchorBottomSheetBehavior.State
int state = mPlacePageBehavior.getState();
if (isSettlingState(state) || isDraggingState(state) || isHiddenState(state))
return null;
if (isAnchoredState(state) || isExpandedState(state))
return BannerController.BannerState.DETAILS;
return BannerController.BannerState.PREVIEW;
}
@Override
public void onBannerDetails(@NonNull MwmNativeAd ad)
{
mPlacePageTracker.onBannerDetails(ad);
}
@Override
public void onBannerPreview(@NonNull MwmNativeAd ad)
{
mPlacePageTracker.onBannerPreview(ad);
}
@Override
public void closePlacePage()
{
close();
}
private class PlacePageGestureListener extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
@AnchorBottomSheetBehavior.State
int state = mPlacePageBehavior.getState();
if (isCollapsedState(state))
{
mPlacePageBehavior.setState(AnchorBottomSheetBehavior.STATE_ANCHORED);
return true;
}
if (isAnchoredState(state) || isExpandedState(state))
{
mPlacePage.resetScroll();
mPlacePageBehavior.setState(AnchorBottomSheetBehavior.STATE_COLLAPSED);
return true;
}
return false;
}
}
}
|