[android] Refactored showing the banner in pp preview

This commit is contained in:
Александр Зацепин 2019-01-31 15:30:40 +03:00 committed by yoksnod
parent 33ef354f0b
commit bf2f453e28
6 changed files with 124 additions and 172 deletions

View file

@ -7,7 +7,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:background="?cardBackground"
android:paddingTop="@dimen/margin_base">
android:paddingTop="@dimen/margin_base"
android:paddingBottom="@dimen/margin_quarter">
<include
android:id="@+id/downloader_status_frame"
@ -179,7 +180,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_half_plus"
android:layout_marginBottom="@dimen/margin_half_plus"
android:layout_marginStart="@dimen/margin_base"
android:layout_marginLeft="@dimen/margin_base"
android:layout_marginEnd="@dimen/margin_base"
@ -194,7 +194,6 @@
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="@dimen/margin_quarter"
android:layout_marginStart="@dimen/margin_quarter"
android:layout_marginBottom="@dimen/margin_quarter"
android:layout_marginRight="@dimen/margin_quarter"
android:layout_marginEnd="@dimen/margin_quarter"
android:layout_below="@id/tv__address"

View file

@ -481,7 +481,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
setContentView(R.layout.activity_map);
mPlacePageController = new BottomSheetPlacePageController(this);
mPlacePageController = new BottomSheetPlacePageController(this, this);
mPlacePageController.initialize();
mIsLaunchByDeepLink = getIntent().getBooleanExtra(EXTRA_LAUNCH_BY_DEEP_LINK, false);
initViews();
@ -1183,8 +1183,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
mNavigationController.onResume();
if (mNavAnimationController != null)
mNavAnimationController.onResume();
// TODO:
// mPlacePage.onActivityResume();
mPlacePageController.onActivityResumed(this);
}
@Override
@ -1201,8 +1200,6 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
super.onResumeFragments();
RoutingController.get().restore();
// TODO:
// mPlacePage.restore();
if (!LikesManager.INSTANCE.isNewUser() && Counters.isShowReviewForOldUser())
{
@ -1222,8 +1219,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
LikesManager.INSTANCE.cancelDialogs();
if (mOnmapDownloader != null)
mOnmapDownloader.onPause();
// TODO:
// mPlacePage.onActivityPause();
mPlacePageController.onActivityPaused(this);
super.onPause();
}
@ -1241,8 +1237,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
mToggleMapLayerController.attachCore();
if (mNavigationController != null)
TrafficManager.INSTANCE.attach(mNavigationController);
// TODO:
// mPlacePage.onActivityStarted();
mPlacePageController.onActivityStarted(this);
}
@Override
@ -1257,8 +1252,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
RoutingController.get().detach();
TrafficManager.INSTANCE.detachAll();
mToggleMapLayerController.detachCore();
// TODO:
// mPlacePage.onActivityStopped();
mPlacePageController.onActivityStopped(this);
}
@Override

View file

@ -6,10 +6,10 @@ import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.transition.TransitionManager;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
@ -53,18 +53,15 @@ final class BannerController
private static final int MAX_TITLE_LINES = 2;
private static final int MIN_TITLE_LINES = 1;
private static boolean isTouched(@Nullable View view, @NonNull MotionEvent event)
{
return view != null && !UiUtils.isHidden(view) && UiUtils.isViewTouched(event, view);
}
@NonNull
private static View inflateBannerLayout(@NonNull NativeAdWrapper.UiType type, @NonNull ViewGroup containerView)
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();
TransitionManager.beginDelayedTransition(containerView);
containerView.addView(bannerView);
return bannerView;
}
@ -109,9 +106,6 @@ final class BannerController
private final float mCloseFrameHeight;
@Nullable
private final BannerListener mListener;
private boolean mOpened = false;
private boolean mError = false;
@Nullable
@ -125,15 +119,14 @@ final class BannerController
@NonNull
private final AdsRemovalPurchaseControllerProvider mAdsRemovalProvider;
BannerController(@NonNull ViewGroup bannerContainer, @Nullable BannerListener listener,
@NonNull CompoundNativeAdLoader loader, @Nullable AdTracker tracker,
BannerController(@NonNull ViewGroup bannerContainer, @NonNull CompoundNativeAdLoader loader,
@Nullable AdTracker tracker,
@NonNull AdsRemovalPurchaseControllerProvider adsRemovalProvider)
{
LOGGER.d(TAG, "Constructor()");
mContainerView = bannerContainer;
mContainerView.setOnClickListener(v -> animateActionButton());
mBannerView = inflateBannerLayout(NativeAdWrapper.UiType.DEFAULT, mContainerView);
mListener = listener;
mAdsLoader = loader;
mAdTracker = tracker;
Resources resources = mBannerView.getResources();
@ -196,7 +189,7 @@ final class BannerController
mError = value;
}
boolean hasErrorOccurred()
private boolean hasErrorOccurred()
{
return mError;
}
@ -206,7 +199,7 @@ final class BannerController
if (mBanners == null)
return;
UiUtils.showIf(!hasErrorOccurred(), mContainerView);
UiUtils.showIf(!hasErrorOccurred() && mCurrentAd != null, mContainerView);
if ((mAdsLoader.isAdLoading() || hasErrorOccurred())
&& mCurrentAd == null)
{
@ -260,7 +253,7 @@ final class BannerController
}
}
boolean isBannerContainerVisible()
private boolean isBannerContainerVisible()
{
return UiUtils.isVisible(mContainerView);
}
@ -300,11 +293,6 @@ final class BannerController
return true;
}
int getLastBannerHeight()
{
return mBannerView.getHeight();
}
private void setFrameHeight(int height)
{
ViewGroup.LayoutParams lp = mBannerView.getLayoutParams();
@ -374,11 +362,6 @@ final class BannerController
}
}
boolean isActionButtonTouched(@NonNull MotionEvent event)
{
return isTouched(mBannerView, event);
}
private void animateActionButton()
{
View view = mOpened ? mActionLarge : mActionSmall;
@ -407,11 +390,6 @@ final class BannerController
return mOpened;
}
interface BannerListener
{
void onSizeChanged();
}
private class MyNativeAdsListener implements NativeAdListener
{
@Nullable
@ -420,7 +398,7 @@ final class BannerController
@Override
public void onAdLoaded(@NonNull MwmNativeAd ad)
{
/* LOGGER.d(TAG, "onAdLoaded, ad = " + ad);
LOGGER.d("XXX", "onAdLoaded, ad = " + ad);
if (mBanners == null)
return;
@ -441,18 +419,13 @@ final class BannerController
ad.registerView(mBannerView);
// TODO:
// loadIconAndOpenIfNeeded(ad);
loadIconAndOpenIfNeeded(ad);
if (mAdTracker != null)
{
onChangedVisibility(isBannerContainerVisible());
mAdTracker.onContentObtained(ad.getProvider(), ad.getBannerId());
}
if (mListener != null && mOpened)
mListener.onSizeChanged();*/
}
@Override
@ -465,9 +438,6 @@ final class BannerController
setErrorStatus(isNotCached);
updateVisibility();
if (mListener != null && isNotCached)
mListener.onSizeChanged();
Statistics.INSTANCE.trackPPBannerError(bannerId, provider, error, mOpened ? 1 : 0);
}

View file

@ -11,9 +11,13 @@ import android.view.ViewGroup;
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.bookmarks.data.MapObject;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.location.LocationListener;
import com.mapswithme.maps.purchase.AdsRemovalPurchaseControllerProvider;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
@ -44,8 +48,12 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
@SuppressWarnings("NullableProblems")
@NonNull
private Toolbar mToolbar;
private int mLastPeekHeight;
private int mViewportMinHeight;
@SuppressWarnings("NullableProblems")
@NonNull
private BannerController mBannerController;
@NonNull
private final AdsRemovalPurchaseControllerProvider mPurchaseControllerProvider;
@NonNull
private final AnchorBottomSheetBehavior.BottomSheetCallback mSheetCallback
= new AnchorBottomSheetBehavior.BottomSheetCallback()
@ -68,6 +76,8 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
Framework.nativeDeactivatePopup();
UiUtils.invisible(mButtonsLayout);
}
setPeekHeight();
}
@Override
@ -77,9 +87,11 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
}
};
public BottomSheetPlacePageController(@NonNull Activity activity)
public BottomSheetPlacePageController(@NonNull Activity activity,
@NonNull AdsRemovalPurchaseControllerProvider provider)
{
mActivity = activity;
mPurchaseControllerProvider = provider;
}
@Override
@ -94,6 +106,12 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
mPlacePageBehavior = AnchorBottomSheetBehavior.from(mPlacePage);
mPlacePageBehavior.addBottomSheetCallback(mSheetCallback);
mPlacePage.addOnLayoutChangeListener(this);
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);
mButtonsLayout = mActivity.findViewById(R.id.pp_buttons_layout);
ViewGroup buttons = mButtonsLayout.findViewById(R.id.container);
mPlacePage.initButtons(buttons);
@ -112,7 +130,7 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
@Override
public void openFor(@NonNull MapObject object)
{
mPlacePage.setMapObject(object, false, () -> {
mPlacePage.setMapObject(object, false, policy -> {
if (object.isExtendedView())
{
mPlacePageBehavior.setState(AnchorBottomSheetBehavior.STATE_EXPANDED);
@ -121,12 +139,20 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
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.сanUseNetwork();
mBannerController.updateData(canShowBanner ? object.getBanners() : null);
}
private void openPlacePage()
{
mPlacePage.post(() -> {
@ -139,9 +165,18 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
private void setPeekHeight()
{
int peekHeight = getPeekHeight();
LOGGER.d(TAG, "Peek height = " + peekHeight);
mLastPeekHeight = peekHeight;
mPlacePageBehavior.setPeekHeight(mLastPeekHeight);
if (peekHeight == mPlacePageBehavior.getPeekHeight())
return;
if (mPlacePageBehavior.getState() == AnchorBottomSheetBehavior.STATE_SETTLING ||
mPlacePageBehavior.getState() == AnchorBottomSheetBehavior.STATE_DRAGGING)
{
LOGGER.d(TAG, "Set new peek height ignored, sheet state inappropriate");
return;
}
LOGGER.d(TAG, "Set new peek height = " + peekHeight);
mPlacePageBehavior.setPeekHeight(peekHeight);
}
private void setPlacePageAnchor()
@ -192,20 +227,14 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
oldTop, int oldRight, int oldBottom)
{
LOGGER.d(TAG, "Layout changed, current state = " + toString(mPlacePageBehavior.getState()));
if (mLastPeekHeight == 0)
if (mPlacePageBehavior.getPeekHeight() == 0)
{
LOGGER.d(TAG, "Layout changed - ignoring, peek height not calculated yet");
LOGGER.d(TAG, "Layout change ignored, peek height not calculated yet");
return;
}
updateViewPortRect();
if (mPlacePageBehavior.getState() != AnchorBottomSheetBehavior.STATE_COLLAPSED)
return;
if (getPeekHeight() == mLastPeekHeight)
return;
mPlacePage.post(this::setPeekHeight);
}
@ -268,16 +297,61 @@ public class BottomSheetPlacePageController implements PlacePageController, Loca
if (object == null)
return;
mPlacePage.setMapObject(object, true, this::restorePlacePage);
mPlacePage.setMapObject(object, true, policy -> {
restorePlacePage(object, policy);
});
mToolbar.setTitle(object.getTitle());
}
private void restorePlacePage()
private void restorePlacePage(MapObject object, NetworkPolicy policy)
{
mPlacePage.post(() -> {
UiUtils.show(mButtonsLayout);
setPeekHeight();
setPlacePageAnchor();
UiUtils.show(mButtonsLayout);
showBanner(object, policy);
});
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState)
{
// No op.
}
@Override
public void onActivityStarted(Activity activity)
{
mBannerController.attach();
}
@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();
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState)
{
// No op.
}
@Override
public void onActivityDestroyed(Activity activity)
{
// No op.
}
}

View file

@ -1,5 +1,6 @@
package com.mapswithme.maps.widget.placepage;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;
@ -7,7 +8,8 @@ import com.mapswithme.maps.base.Initializable;
import com.mapswithme.maps.base.Savable;
import com.mapswithme.maps.bookmarks.data.MapObject;
public interface PlacePageController extends Initializable, Savable<Bundle>
public interface PlacePageController extends Initializable, Savable<Bundle>,
Application.ActivityLifecycleCallbacks
{
void openFor(@NonNull MapObject object);
void close();

View file

@ -1,6 +1,5 @@
package com.mapswithme.maps.widget.placepage;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
@ -29,7 +28,6 @@ import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
@ -44,8 +42,6 @@ import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.ads.CompoundNativeAdLoader;
import com.mapswithme.maps.ads.DefaultAdTracker;
import com.mapswithme.maps.ads.LocalAdInfo;
import com.mapswithme.maps.api.ParsedMwmRequest;
import com.mapswithme.maps.bookmarks.PlaceDescriptionActivity;
@ -119,8 +115,7 @@ public class PlacePageView extends NestedScrollView
LineCountTextView.OnLineCountCalculatedListener,
RecyclerClickListener,
NearbyAdapter.OnItemClickListener,
EditBookmarkFragment.EditBookmarkListener,
BannerController.BannerListener
EditBookmarkFragment.EditBookmarkListener
{
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = PlacePageView.class.getSimpleName();
@ -216,9 +211,6 @@ public class PlacePageView extends NestedScrollView
@NonNull
private UGCController mUgcController;
@Nullable
BannerController mBannerController;
// Data
@Nullable
private MapObject mMapObject;
@ -242,8 +234,6 @@ public class PlacePageView extends NestedScrollView
@Nullable
private CountryItem mCurrentCountry;
private final int mMarginBase;
private final MapManager.StorageCallback mStorageCallback = new MapManager.StorageCallback()
{
@Override
@ -323,7 +313,7 @@ public class PlacePageView extends NestedScrollView
public interface SetMapObjectListener
{
void onSetMapObjectComplete();
void onSetMapObjectComplete(@NonNull NetworkPolicy policy);
}
public PlacePageView(Context context)
@ -339,44 +329,11 @@ public class PlacePageView extends NestedScrollView
public PlacePageView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs);
Activity activity = (Activity) context;
mIsLatLonDms = MwmApplication.prefs().getBoolean(PREF_USE_DMS, false);
mGalleryAdapter = new com.mapswithme.maps.widget.placepage.GalleryAdapter(context);
mMarginBase = (int) getResources().getDimension(R.dimen.margin_base);
init(attrs, defStyleAttr);
}
public ViewGroup GetPreview() { return mPreview; }
public boolean isHorizontalScrollAreaTouched(@NonNull MotionEvent event)
{
return UiUtils.isViewTouched(event, mHotelGallery);
}
public void onActivityResume()
{
if (mBannerController != null)
mBannerController.onChangedVisibility(true);
}
public void onActivityPause()
{
if (mBannerController != null)
mBannerController.onChangedVisibility(false);
}
public void onActivityStopped()
{
if (mBannerController != null)
mBannerController.detach();
}
public void onActivityStarted()
{
if (mBannerController != null)
mBannerController.attach();
}
@Override
protected void onFinishInflate()
{
@ -468,15 +425,6 @@ public class PlacePageView extends NestedScrollView
mUgcController = new UGCController(this);
ViewGroup bannerContainer = findViewById(R.id.banner_container);
if (bannerContainer != null)
{
DefaultAdTracker tracker = new DefaultAdTracker();
CompoundNativeAdLoader loader = com.mapswithme.maps.ads.Factory.createCompoundLoader(tracker, tracker);
mBannerController = new BannerController(bannerContainer, this, loader, tracker,
getActivity());
}
mDownloaderIcon = new DownloaderStatusIcon(mPreview.findViewById(R.id.downloader_status_frame));
mDownloaderInfo = mPreview.findViewById(R.id.tv__downloader_details);
@ -775,7 +723,7 @@ public class PlacePageView extends NestedScrollView
LOGGER.e(TAG, "A sponsored info cannot be updated, mMapObject is null!");
return;
}
refreshPreview(mMapObject, NetworkPolicy.newInstance(true), priceInfo);
refreshPreview(mMapObject, priceInfo);
}
@Override
@ -1031,7 +979,10 @@ public class PlacePageView extends NestedScrollView
if (!force && MapObject.same(mMapObject, mapObject))
{
if (listener != null)
listener.onSetMapObjectComplete();
{
NetworkPolicy policy = NetworkPolicy.newInstance(NetworkPolicy.getCurrentNetworkUsageStatus());
listener.onSetMapObjectComplete(policy);
}
return;
}
@ -1048,15 +999,16 @@ public class PlacePageView extends NestedScrollView
{
setMapObjectInternal(policy);
if (listener != null)
listener.onSetMapObjectComplete();
listener.onSetMapObjectComplete(policy);
}
});
}
else
{
setMapObjectInternal(NetworkPolicy.newInstance(false));
NetworkPolicy policy = NetworkPolicy.newInstance(false);
setMapObjectInternal(policy);
if (listener != null)
listener.onSetMapObjectComplete();
listener.onSetMapObjectComplete(policy);
}
}
@ -1112,7 +1064,7 @@ public class PlacePageView extends NestedScrollView
return;
}
refreshPreview(mMapObject, policy, null);
refreshPreview(mMapObject, null);
refreshViewsInternal(mMapObject);
}
@ -1185,19 +1137,6 @@ public class PlacePageView extends NestedScrollView
}
}
private void refreshPreview(@NonNull MapObject mapObject, @NonNull NetworkPolicy policy,
@Nullable HotelPriceInfo priceInfo)
{
if (mBannerController != null)
{
boolean canShow = mapObject.getMapObjectType() != MapObject.MY_POSITION
&& policy.сanUseNetwork();
mBannerController.updateData(canShow ? mapObject.getBanners() : null);
}
refreshPreview(mapObject, priceInfo);
}
private void refreshPreview(@NonNull MapObject mapObject, @Nullable HotelPriceInfo priceInfo)
{
UiUtils.setTextAndHideIfEmpty(mTvTitle, mapObject.getTitle());
@ -1987,32 +1926,6 @@ public class PlacePageView extends NestedScrollView
setMapObject(BookmarkManager.INSTANCE.getBookmark(bookmarkId), true, null);
}
public boolean isBannerTouched(@NonNull MotionEvent event)
{
return mBannerController != null && mBannerController.isActionButtonTouched(event);
}
/* public boolean isLeaveReviewButtonTouched(@NonNull MotionEvent event)
{
return mUgcController != null && mUgcController.isLeaveReviewButtonTouched(event);
}*/
public boolean isSearchSimilarHotelsButtonTouched(@NonNull MotionEvent event)
{
return UiUtils.isViewTouched(event, mPreview.findViewById(R.id.search_hotels_btn));
}
@Override
public void onSizeChanged()
{
/* if (mBannerController != null && mBannerController.hasErrorOccurred())
{
mPreview.setPadding(mPreview.getPaddingLeft(), mPreview.getPaddingTop(),
getPaddingRight(), mMarginBase);
}*/
}
int getPreviewHeight()
{
return mPreview.getHeight();