[android] Added ui wrapper over native ad to match network types to google/default ui types

This commit is contained in:
Alexander Zatsepin 2018-07-16 16:11:30 +03:00 committed by Vlad Mihaylenko
parent 5bd3c32398
commit 191f2ed6a8
3 changed files with 149 additions and 40 deletions

View file

@ -1,38 +1,10 @@
package com.mapswithme.maps.ads;
import android.support.annotation.LayoutRes;
import com.mapswithme.maps.R;
public enum NetworkType
{
FACEBOOK,
GOOGLE,
MOPUB,
MOPUB_GOOGLE
{
@Override
public int getLayoutId()
{
return R.layout.place_page_banner_google;
}
@Override
public boolean showCustomAdChoiceIcon()
{
return false;
}
},
MOPUB_GOOGLE,
MYTARGET;
@LayoutRes
public int getLayoutId()
{
return R.layout.place_page_banner;
}
public boolean showCustomAdChoiceIcon()
{
return true;
}
}

View file

@ -21,7 +21,6 @@ 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.ads.NetworkType;
import com.mapswithme.util.Config;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
@ -58,7 +57,7 @@ final class BannerController
}
@NonNull
private static View inflateBannerLayout(@NonNull NetworkType type, @NonNull ViewGroup containerView)
private static View inflateBannerLayout(@NonNull NativeAdWrapper.UiType type, @NonNull ViewGroup containerView)
{
Context context = containerView.getContext();
LayoutInflater li = LayoutInflater.from(context);
@ -105,7 +104,7 @@ final class BannerController
private boolean mOpened = false;
private boolean mError = false;
@Nullable
private MwmNativeAd mCurrentAd;
private NativeAdWrapper mCurrentAd;
@NonNull
private CompoundNativeAdLoader mAdsLoader;
@Nullable
@ -119,7 +118,7 @@ final class BannerController
LOGGER.d(TAG, "Constructor()");
mContainerView = bannerContainer;
mContainerView.setOnClickListener(v -> animateActionButton());
mBannerView = inflateBannerLayout(NetworkType.MOPUB, mContainerView);
mBannerView = inflateBannerLayout(NativeAdWrapper.UiType.DEFAULT, mContainerView);
mListener = listener;
mAdsLoader = loader;
mAdTracker = tracker;
@ -177,7 +176,7 @@ final class BannerController
}
else if (mCurrentAd != null)
{
UiUtils.showIf(mCurrentAd.getNetworkType().showCustomAdChoiceIcon(), mAdChoices);
UiUtils.showIf(mCurrentAd.getType().showAdChoiceIcon(), mAdChoices);
UiUtils.show(mIcon, mTitle, mMessage, mActionSmall, mActionLarge, mAdChoicesLabel);
if (mOpened)
UiUtils.hide(mActionSmall);
@ -375,7 +374,7 @@ final class BannerController
private class MyNativeAdsListener implements NativeAdListener
{
@Nullable
private NetworkType mLastAdType;
private NativeAdWrapper.UiType mLastAdType;
@Override
public void onAdLoaded(@NonNull MwmNativeAd ad)
@ -386,15 +385,14 @@ final class BannerController
unregisterCurrentAd();
if (mLastAdType != ad.getNetworkType())
mCurrentAd = new NativeAdWrapper(ad);
if (mLastAdType != mCurrentAd.getType())
{
mBannerView = inflateBannerLayout(ad.getNetworkType(), mContainerView);
mBannerView = inflateBannerLayout(mCurrentAd.getType(), mContainerView);
initBannerViews();
}
mLastAdType = ad.getNetworkType();
mCurrentAd = ad;
mLastAdType = mCurrentAd.getType();
updateVisibility();

View file

@ -0,0 +1,139 @@
package com.mapswithme.maps.widget.placepage;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import com.mapswithme.maps.R;
import com.mapswithme.maps.ads.MwmNativeAd;
import com.mapswithme.maps.ads.NetworkType;
import java.util.EnumMap;
import java.util.Map;
public class NativeAdWrapper implements MwmNativeAd
{
private final static Map<NetworkType, UiType> TYPES
= new EnumMap<NetworkType, UiType>(NetworkType.class)
{
{
put(NetworkType.MOPUB_GOOGLE, UiType.GOOGLE);
put(NetworkType.MOPUB, UiType.DEFAULT);
put(NetworkType.FACEBOOK, UiType.DEFAULT);
put(NetworkType.GOOGLE, UiType.GOOGLE);
put(NetworkType.MYTARGET, UiType.DEFAULT);
}
};
@NonNull
private final MwmNativeAd mNativeAd;
@NonNull
private final UiType mType;
NativeAdWrapper(@NonNull MwmNativeAd nativeAd)
{
mNativeAd = nativeAd;
mType = TYPES.get(nativeAd.getNetworkType());
}
@NonNull
@Override
public String getBannerId()
{
return mNativeAd.getBannerId();
}
@NonNull
@Override
public String getTitle()
{
return mNativeAd.getTitle();
}
@NonNull
@Override
public String getDescription()
{
return mNativeAd.getDescription();
}
@NonNull
@Override
public String getAction()
{
return mNativeAd.getAction();
}
@Override
public void loadIcon(@NonNull View view)
{
mNativeAd.loadIcon(view);
}
@Override
public void registerView(@NonNull View bannerView)
{
mNativeAd.registerView(bannerView);
}
@Override
public void unregisterView(@NonNull View bannerView)
{
mNativeAd.unregisterView(bannerView);
}
@NonNull
@Override
public String getProvider()
{
return mNativeAd.getProvider();
}
@Nullable
@Override
public String getPrivacyInfoUrl()
{
return mNativeAd.getPrivacyInfoUrl();
}
@NonNull
@Override
public NetworkType getNetworkType()
{
throw new UnsupportedOperationException("It's not supported for UI!");
}
@NonNull
public UiType getType()
{
return mType;
}
public enum UiType
{
DEFAULT(R.layout.place_page_banner, true),
GOOGLE(R.layout.place_page_banner_google, false);
@LayoutRes
private final int mLayoutId;
private final boolean mShowAdChoiceIcon;
UiType(int layoutId, boolean showAdChoiceIcon)
{
mLayoutId = layoutId;
mShowAdChoiceIcon = showAdChoiceIcon;
}
@LayoutRes
public int getLayoutId()
{
return mLayoutId;
}
public boolean showAdChoiceIcon()
{
return mShowAdChoiceIcon;
}
}
}