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

BannerController.java « placepage « widget « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 61ed42975da13ada04da672e4cc23be364018aac (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
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
package com.mapswithme.maps.widget.placepage;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.ads.AdTracker;
import com.mapswithme.maps.ads.Banner;
import com.mapswithme.maps.ads.CompoundNativeAdLoader;
import com.mapswithme.maps.ads.MwmNativeAd;
import com.mapswithme.maps.ads.NativeAdError;
import com.mapswithme.maps.ads.NativeAdListener;
import com.mapswithme.maps.purchase.AdsRemovalPurchaseControllerProvider;
import com.mapswithme.maps.purchase.AdsRemovalPurchaseDialog;
import com.mapswithme.maps.purchase.PurchaseController;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.statistics.Statistics;

import java.util.Collections;
import java.util.List;

import static com.mapswithme.util.statistics.Statistics.EventName.PP_BANNER_CLICK;
import static com.mapswithme.util.statistics.Statistics.PP_BANNER_STATE_DETAILS;
import static com.mapswithme.util.statistics.Statistics.PP_BANNER_STATE_PREVIEW;


final class BannerController implements PlacePageStateObserver
{
  private static final Logger LOGGER = LoggerFactory.INSTANCE
      .getLogger(LoggerFactory.Type.MISC);
  private static final String TAG = BannerController.class.getName();

  private static final int MAX_MESSAGE_LINES = 100;
  private static final int MIN_MESSAGE_LINES = 3;
  private static final int MAX_TITLE_LINES = 2;
  private static final int MIN_TITLE_LINES = 1;

  @NonNull
  private static View inflateBannerLayout(@NonNull NativeAdWrapper.UiType type,
                                          @NonNull ViewGroup containerView)
  {
    Context context = containerView.getContext();
    LayoutInflater li = LayoutInflater.from(context);
    View bannerView = li.inflate(type.getLayoutId(), containerView, false);
    containerView.removeAllViews();
    containerView.addView(bannerView);
    return bannerView;
  }

  @Nullable
  private List<Banner> mBanners;
  @NonNull
  private final ViewGroup mContainerView;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mBannerView;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private ImageView mIcon;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mTitle;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mMessage;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mActionSmall;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mActionContainer;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mActionLarge;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private ImageView mAdChoices;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private ImageView mAdChoicesLabel;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mAdsRemovalIcon;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mAdsRemovalButton;
  @Nullable
  private BannerState mState;
  private boolean mError = false;
  @Nullable
  private NativeAdWrapper mCurrentAd;
  @NonNull
  private CompoundNativeAdLoader mAdsLoader;
  @Nullable
  private AdTracker mAdTracker;
  @NonNull
  private MyNativeAdsListener mAdsListener = new MyNativeAdsListener();
  @NonNull
  private final AdsRemovalPurchaseControllerProvider mAdsRemovalProvider;
  private int mClosedHeight;
  private int mOpenedHeight;
  @NonNull
  private final BannerStateRequester mBannerStateRequester;
  @NonNull
  private final BannerStateListener mBannerStateListener;

  BannerController(@NonNull ViewGroup bannerContainer, @NonNull CompoundNativeAdLoader loader,
                   @Nullable AdTracker tracker,
                   @NonNull AdsRemovalPurchaseControllerProvider adsRemovalProvider,
                   @NonNull BannerStateRequester bannerStateRequester,
                   @NonNull BannerStateListener bannerStateListener)
  {
    LOGGER.d(TAG, "Constructor()");
    mContainerView = bannerContainer;
    mContainerView.setOnClickListener(v -> animateActionButton());
    mBannerView = inflateBannerLayout(NativeAdWrapper.UiType.DEFAULT, mContainerView);
    mAdsLoader = loader;
    mAdTracker = tracker;
    mAdsRemovalProvider = adsRemovalProvider;
    mBannerStateRequester = bannerStateRequester;
    mBannerStateListener = bannerStateListener;
    initBannerViews();
  }

  private void initBannerViews()
  {
    mIcon = mBannerView.findViewById(R.id.iv__banner_icon);
    mTitle = mBannerView.findViewById(R.id.tv__banner_title);
    mMessage = mBannerView.findViewById(R.id.tv__banner_message);
    mActionSmall = mBannerView.findViewById(R.id.tv__action_small);
    mActionContainer = mBannerView.findViewById(R.id.action_container);
    mActionLarge = mActionContainer.findViewById(R.id.tv__action_large);
    mAdsRemovalButton = mActionContainer.findViewById(R.id.tv__action_remove);
    mAdsRemovalButton.setOnClickListener(this::handleAdsRemoval);
    mAdChoices = mBannerView.findViewById(R.id.ad_choices_icon);
    mAdChoices.setOnClickListener(v -> handlePrivacyInfoUrl());
    mAdChoicesLabel = mBannerView.findViewById(R.id.ad_choices_label);
    mAdsRemovalIcon = mBannerView.findViewById(R.id.remove_btn);
    mAdsRemovalIcon.setOnClickListener(this::handleAdsRemoval);
    expandTouchArea();
  }

  private void expandTouchArea()
  {
    Resources res = mBannerView.getResources();
    final int tapArea = res.getDimensionPixelSize(R.dimen.margin_quarter_plus);
    UiUtils.expandTouchAreaForViews(tapArea, mAdChoices);
    int crossArea = res.getDimensionPixelSize(R.dimen.margin_base_plus);
    UiUtils.expandTouchAreaForView(mAdsRemovalIcon,  tapArea, crossArea, tapArea, crossArea);
  }

  private void handlePrivacyInfoUrl()
  {
    if (mCurrentAd == null)
      return;

    String privacyUrl = mCurrentAd.getPrivacyInfoUrl();
    if (TextUtils.isEmpty(privacyUrl))
      return;

    Utils.openUrl(mBannerView.getContext(), privacyUrl);
  }

  private void handleAdsRemoval(@NonNull View clickedView)
  {
    boolean isCross = clickedView.getId() == R.id.remove_btn;
    @Statistics.BannerState
    int state = isDetailsState(mState) ? PP_BANNER_STATE_DETAILS
                                       : PP_BANNER_STATE_PREVIEW;
    Statistics.INSTANCE.trackPPBannerClose(state, isCross);
    FragmentActivity activity = (FragmentActivity) mBannerView.getContext();
    AdsRemovalPurchaseDialog.show(activity);
  }

  private void setErrorStatus(boolean value)
  {
    mError = value;
  }

  private boolean hasErrorOccurred()
  {
    return mError;
  }

  private void updateVisibility()
  {
    if (mBanners == null)
      throw new AssertionError("Banners must be non-null at this point!");

    UiUtils.hideIf(hasErrorOccurred() || mCurrentAd == null, mContainerView);

    if (mCurrentAd == null)
      throw new AssertionError("Banners must be non-null at this point!");

    UiUtils.showIf(mCurrentAd.getType().showAdChoiceIcon(), mAdChoices);
    PurchaseController<?> purchaseController
        = mAdsRemovalProvider.getAdsRemovalPurchaseController();
    boolean showRemovalButtons = purchaseController != null
                                 && purchaseController.isPurchaseSupported();
    UiUtils.showIf(showRemovalButtons, mAdsRemovalIcon, mAdsRemovalButton);
    UiUtils.show(mIcon, mTitle, mMessage, mActionSmall, mActionContainer, mActionLarge,
                 mAdsRemovalButton, mAdChoicesLabel);
    if (isDetailsState(mState))
      UiUtils.hide(mActionSmall);
    else
      UiUtils.hide(mActionContainer, mActionLarge, mAdsRemovalButton, mIcon);
    UiUtils.show(mBannerView);
  }

  void updateData(@Nullable List<Banner> banners)
  {
    if (mBanners != null && !mBanners.equals(banners))
    {
      onChangedVisibility(false);
      unregisterCurrentAd();
    }

    setErrorStatus(false);

    mBanners = banners != null ? Collections.unmodifiableList(banners) : null;
    UiUtils.showIf(mBanners != null, mContainerView);
    if (mBanners == null)
      return;

    UiUtils.hide(mBannerView);
    mAdsLoader.loadAd(mContainerView.getContext(), mBanners);
  }

  private void unregisterCurrentAd()
  {
    if (mCurrentAd != null)
    {
      LOGGER.d(TAG, "Unregister view for the ad: " + mCurrentAd.getTitle());
      mCurrentAd.unregisterView(mBannerView);
      mCurrentAd = null;
    }
  }

  private boolean isBannerContainerVisible()
  {
    return UiUtils.isVisible(mContainerView);
  }

  void open()
  {
    if (!isBannerContainerVisible() || mBanners == null || isDetailsState(mState))
      return;

    setOpenedStateInternal();

    if (mCurrentAd != null)
    {
      loadIcon(mCurrentAd);
      mCurrentAd.registerView(mBannerView);
      mBannerStateListener.onBannerDetails(mCurrentAd);
    }
  }

  void zoomIn(float ratio)
  {
    ViewGroup banner = mContainerView.findViewById(R.id.banner);
    ViewGroup.LayoutParams lp = banner.getLayoutParams();
    lp.height = (int) ((mOpenedHeight - mClosedHeight) * ratio + mClosedHeight);
    banner.setLayoutParams(lp);
  }

  void zoomOut(float ratio)
  {
    ViewGroup banner = mContainerView.findViewById(R.id.banner);
    ViewGroup.LayoutParams lp = banner.getLayoutParams();
    lp.height = (int) (mClosedHeight - (mClosedHeight - mOpenedHeight) * ratio);
    banner.setLayoutParams(lp);
  }

  void close()
  {
    if (!isBannerContainerVisible() || mBanners == null)
      return;

    setClosedStateInternal();

    if (mCurrentAd != null)
    {
      mCurrentAd.registerView(mBannerView);
      mBannerStateListener.onBannerPreview(mCurrentAd);
    }
  }

  private void discardBannerSize()
  {
    zoomOut(0);
  }

  private void measureBannerSizes()
  {
    DisplayMetrics dm = mContainerView.getResources().getDisplayMetrics();
    final float screenWidth = dm.widthPixels;

    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((int) screenWidth, View.MeasureSpec.AT_MOST);

    setClosedStateInternal();
    mBannerView.measure(widthMeasureSpec, heightMeasureSpec);
    mClosedHeight = mBannerView.getMeasuredHeight();
    LOGGER.d(TAG, "Banner close height = " + mClosedHeight);

    setOpenedStateInternal();
    mBannerView.measure(widthMeasureSpec, heightMeasureSpec);
    mOpenedHeight = mBannerView.getMeasuredHeight();
    LOGGER.d(TAG, "Banner open height = " + mOpenedHeight);
  }

  private void setOpenedStateInternal()
  {
    mState = BannerState.DETAILS;
    mMessage.setMaxLines(MAX_MESSAGE_LINES);
    mTitle.setMaxLines(MAX_TITLE_LINES);
    updateVisibility();
  }

  private void setClosedStateInternal()
  {
    mState = BannerState.PREVIEW;
    UiUtils.hide(mIcon);
    mMessage.setMaxLines(MIN_MESSAGE_LINES);
    mTitle.setMaxLines(MIN_TITLE_LINES);
    updateVisibility();
  }

  private void loadIcon(@NonNull MwmNativeAd ad)
  {
    UiUtils.show(mIcon);
    ad.loadIcon(mIcon);
  }

  void onChangedVisibility(boolean isVisible)
  {
    if (mAdTracker == null || mCurrentAd == null)
      return;

    if (isVisible)
    {
      mAdTracker.onViewShown(mCurrentAd.getProvider(), mCurrentAd.getBannerId());
      mCurrentAd.registerView(mBannerView);
    }
    else
    {
      mAdTracker.onViewHidden(mCurrentAd.getProvider(), mCurrentAd.getBannerId());
      mCurrentAd.unregisterView(mBannerView);
    }
  }

  void detach()
  {
    mAdsLoader.detach();
    mAdsLoader.setAdListener(null);
  }

  void attach()
  {
    mAdsLoader.setAdListener(mAdsListener);
  }

  private void fillViews(@NonNull MwmNativeAd data)
  {
    mTitle.setText(data.getTitle());
    mMessage.setText(data.getDescription());
    mActionSmall.setText(data.getAction());
    mActionLarge.setText(data.getAction());
  }

  private void animateActionButton()
  {
    ObjectAnimator animator;
    if (isDetailsState(mState))
    {
      Context context = mBannerView.getContext();
      Resources res = context.getResources();
      int colorFrom = ThemeUtils.isNightTheme() ? res.getColor(R.color.white_12)
                                                : res.getColor(R.color.black_12);
      int colorTo = ThemeUtils.isNightTheme() ? res.getColor(R.color.white_24)
                                              : res.getColor(R.color.black_24);
      animator = ObjectAnimator.ofObject(mActionLarge, "backgroundColor", new ArgbEvaluator(),
                                         colorFrom, colorTo, colorFrom);
    }
    else
    {
      animator = ObjectAnimator.ofFloat(mActionSmall, "alpha", 0.3f, 1f);
    }
    animator.setDuration(300);
    animator.start();
  }

  private static boolean isDetailsState(@Nullable BannerState state)
  {
    return state == BannerState.DETAILS;
  }

  private static boolean isPreviewState(@Nullable BannerState state)
  {
    return state == BannerState.PREVIEW;
  }

  boolean hasAd()
  {
    return mCurrentAd != null;
  }

  int getClosedHeight()
  {
    return mClosedHeight;
  }

  private void setBannerState(@Nullable BannerState state)
  {
    if (mCurrentAd == null)
      throw new AssertionError("Current ad must be non-null at this point!");

    if (state == null)
    {
      LOGGER.d(TAG, "Banner state not determined yet, discard banner size");
      setBannerInitialHeight(0);
      mState = null;
      return;
    }

    if (isDetailsState(state))
    {
      open();
      loadIcon(mCurrentAd);
      setBannerInitialHeight(mOpenedHeight);
      return;
    }

    if (isPreviewState(state))
    {
      close();
      setBannerInitialHeight(mClosedHeight);
      mBannerStateListener.onBannerPreview(mCurrentAd);
    }
  }

  private void setBannerInitialHeight(int height)
  {
    LOGGER.d(TAG, "Set banner initial height = " + height);
    ViewGroup banner = mContainerView.findViewById(R.id.banner);
    ViewGroup.LayoutParams lp = banner.getLayoutParams();
    lp.height = height;
    banner.setLayoutParams(lp);
  }

  @Override
  public void onPlacePageStateChanged()
  {
    if (mCurrentAd == null)
      return;

    BannerState newState =  mBannerStateRequester.requestBannerState();
    setBannerState(newState);
  }

  private class MyNativeAdsListener implements NativeAdListener
  {
    @Nullable
    private NativeAdWrapper.UiType mLastAdType;

    @Override
    public void onAdLoaded(@NonNull MwmNativeAd ad)
    {
      LOGGER.d(TAG, "onAdLoaded, ad = " + ad);
      if (mBanners == null)
        return;

      unregisterCurrentAd();
      discardBannerSize();

      mCurrentAd = new NativeAdWrapper(ad);
      if (mLastAdType != mCurrentAd.getType())
      {
        mBannerView = inflateBannerLayout(mCurrentAd.getType(), mContainerView);
        initBannerViews();
      }

      mLastAdType = mCurrentAd.getType();

      fillViews(ad);
      measureBannerSizes();
      BannerState state = mBannerStateRequester.requestBannerState();
      setBannerState(state);
      ad.registerView(mBannerView);

      if (mAdTracker != null)
      {
        onChangedVisibility(isBannerContainerVisible());
        mAdTracker.onContentObtained(ad.getProvider(), ad.getBannerId());
      }
    }

    @Override
    public void onError(@NonNull String bannerId, @NonNull String provider,
                        @NonNull NativeAdError error)
    {
      if (mBanners == null)
        return;

      boolean isNotCached = mCurrentAd == null;
      setErrorStatus(isNotCached);
      UiUtils.hide(mContainerView);

      Statistics.INSTANCE.trackPPBannerError(bannerId, provider, error,
                                             isDetailsState(mState) ? 1 : 0);
    }

    @Override
    public void onClick(@NonNull MwmNativeAd ad)
    {
      Statistics.INSTANCE.trackPPBanner(PP_BANNER_CLICK, ad,
                                        isDetailsState(mState) ? PP_BANNER_STATE_DETAILS
                                                               : PP_BANNER_STATE_PREVIEW);
    }
  }

  interface BannerStateRequester
  {
    @Nullable
    BannerState requestBannerState();
  }

  enum BannerState
  {
    PREVIEW,
    DETAILS
  }

  interface BannerStateListener
  {
    void onBannerDetails(@NonNull MwmNativeAd ad);
    void onBannerPreview(@NonNull MwmNativeAd ad);
  }
}