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

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

import android.content.Context;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.ads.formats.NativeAdOptions;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.util.Language;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mopub.nativeads.BaseNativeAd;
import com.mopub.nativeads.GooglePlayServicesNative;
import com.mopub.nativeads.MoPubAdRenderer;
import com.mopub.nativeads.MoPubNative;
import com.mopub.nativeads.MopubNativeAdFactory;
import com.mopub.nativeads.NativeAd;
import com.mopub.nativeads.NativeErrorCode;
import com.mopub.nativeads.RequestParameters;
import com.mopub.nativeads.StaticNativeAd;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;

class MopubNativeDownloader extends CachingNativeAdLoader
    implements MoPubNative.MoPubNativeNetworkListener, NativeAd.MoPubNativeEventListener
{
  private final static Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private final static String TAG = MopubNativeDownloader.class.getSimpleName();

  @Nullable
  private String mBannerId;

  MopubNativeDownloader(@Nullable OnAdCacheModifiedListener listener, @Nullable AdTracker tracker)
  {
    super(tracker, listener);
  }

  @Override
  public void loadAd(@NonNull Context context, @NonNull String bannerId)
  {
    mBannerId = bannerId;
    super.loadAd(context, bannerId);
  }

  @Override
  void loadAdFromProvider(@NonNull Context context, @NonNull String bannerId)
  {
    MoPubNative nativeAd = new MoPubNative(context, bannerId, this);

    nativeAd.registerAdRenderer(new DummyRenderer());

    RequestParameters.Builder requestParameters = new RequestParameters.Builder();

    EnumSet<RequestParameters.NativeAdAsset> assetsSet =
        EnumSet.of(RequestParameters.NativeAdAsset.TITLE,
                   RequestParameters.NativeAdAsset.TEXT,
                   RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
                   RequestParameters.NativeAdAsset.ICON_IMAGE);
    requestParameters.desiredAssets(assetsSet);

    Location l = LocationHelper.INSTANCE.getSavedLocation();
    if (l != null)
      requestParameters.location(l);

    String locale = Language.nativeNormalize(Language.getDefaultLocale());
    requestParameters.keywords("user_lang:" + locale);

    Map<String, Object> extras
        = Collections.singletonMap(GooglePlayServicesNative.KEY_EXTRA_AD_CHOICES_PLACEMENT,
                                   NativeAdOptions.ADCHOICES_TOP_LEFT);
    nativeAd.setLocalExtras(extras);

    nativeAd.makeRequest(requestParameters.build());
  }

  @NonNull
  @Override
  String getProvider()
  {
    return Providers.MOPUB;
  }

  @Override
  public void onNativeLoad(final NativeAd nativeAd)
  {
    nativeAd.setMoPubNativeEventListener(this);
    LOGGER.d(TAG, "onNativeLoad nativeAd = " + nativeAd);
    CachedMwmNativeAd ad = MopubNativeAdFactory.createNativeAd(nativeAd);

    if (ad != null)
      onAdLoaded(nativeAd.getAdUnitId(), ad);
  }

  @Override
  public void onNativeFail(NativeErrorCode errorCode)
  {
    LOGGER.w(TAG, "onNativeFail " + errorCode.toString());
    if (mBannerId == null)
      throw new AssertionError("A banner id must be non-null if a error is occurred");

    onError(mBannerId, getProvider(), new MopubAdError(errorCode.toString()));
  }

  @Override
  public void onImpression(View view)
  {
    LOGGER.d(TAG, "on MoPub Ad impressed");
  }

  @Override
  public void onClick(View view)
  {
    if (!TextUtils.isEmpty(mBannerId))
      onAdClicked(mBannerId);
  }

  private static class DummyRenderer implements MoPubAdRenderer<StaticNativeAd>
  {
    @NonNull
    @Override
    public View createAdView(@NonNull Context context, @Nullable ViewGroup parent)
    {
      // This method is never called, don't worry about nullness warning.
      // noinspection ConstantConditions
      return null;
    }

    @Override
    public void renderAdView(@NonNull View view, @NonNull StaticNativeAd ad)
    {
      // No op.
    }

    @Override
    public boolean supports(@NonNull BaseNativeAd nativeAd)
    {
      return true;
    }
  }
}