[android] Show online-aware dialog before Place Page is shown.

This commit is contained in:
Roman Romanov 2017-02-15 14:17:27 +04:00
parent a95f296547
commit c0cf6b3ceb
6 changed files with 108 additions and 53 deletions

View file

@ -89,6 +89,7 @@ import com.mapswithme.maps.widget.placepage.PlacePageView.State;
import com.mapswithme.util.Animations;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.InputUtils;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
@ -894,12 +895,19 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
super.onRestoreInstanceState(savedInstanceState);
State state = State.values()[savedInstanceState.getInt(STATE_PP, 0)];
final State state = State.values()[savedInstanceState.getInt(STATE_PP, 0)];
if (state != State.HIDDEN)
{
mPlacePageRestored = true;
mPlacePage.setMapObject((MapObject) savedInstanceState.getParcelable(STATE_MAP_OBJECT), true);
mPlacePage.setState(state);
mPlacePage.setMapObject((MapObject) savedInstanceState.getParcelable(STATE_MAP_OBJECT), true,
new PlacePageView.SetMapObjectListener()
{
@Override
public void onSetMapObjectComplete()
{
mPlacePage.setState(state);
}
});
}
if (!mIsFragmentContainer && RoutingController.get().isPlanning())
@ -1165,10 +1173,16 @@ public class MwmActivity extends BaseMwmFragmentActivity
setFullscreen(false);
mPlacePage.setMapObject(object, true);
if (!mPlacePageRestored)
mPlacePage.setState(State.PREVIEW);
mPlacePageRestored = false;
mPlacePage.setMapObject(object, true, new PlacePageView.SetMapObjectListener()
{
@Override
public void onSetMapObjectComplete()
{
if (!mPlacePageRestored)
mPlacePage.setState(State.PREVIEW);
mPlacePageRestored = false;
}
});
if (UiUtils.isVisible(mFadeView))
mFadeView.fadeOut();
@ -1281,7 +1295,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
else
{
Framework.nativeDeactivatePopup();
mPlacePage.setMapObject(null, false);
mPlacePage.setMapObject(null, false, null);
}
}
@ -1583,7 +1597,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
updateSearchBar();
}
mPlacePage.refreshViews();
mPlacePage.refreshViews(null);
}
private void adjustCompassAndTraffic(int offsetY)
@ -1640,7 +1654,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
@Override
public void showNavigation(boolean show)
{
mPlacePage.refreshViews();
mPlacePage.refreshViews(null);
if (mNavigationController != null)
mNavigationController.show(show);
refreshFade();

View file

@ -123,7 +123,7 @@ public class MwmApplication extends Application
return sSelf.mPrefs;
}
private static boolean isCrashlyticsEnabled()
public static boolean isCrashlyticsEnabled()
{
return !BuildConfig.FABRIC_API_KEY.startsWith("0000");
}

View file

@ -108,7 +108,8 @@ final class BannerController implements View.OnClickListener
if (TextUtils.isEmpty(labelId))
{
Crashlytics.logException(new Resources.NotFoundException("An empty string id obtained for: "
if (MwmApplication.isCrashlyticsEnabled())
Crashlytics.logException(new Resources.NotFoundException("An empty string id obtained for: "
+ mBanner));
return;
}
@ -121,7 +122,8 @@ final class BannerController implements View.OnClickListener
}
catch (Resources.NotFoundException e)
{
Crashlytics.logException(new IllegalStateException("Unknown banner is found: " + mBanner, e));
if (MwmApplication.isCrashlyticsEnabled())
Crashlytics.logException(new IllegalStateException("Unknown banner is found: " + mBanner, e));
}
}

View file

@ -70,6 +70,7 @@ import com.mapswithme.maps.widget.recycler.DividerItemDecoration;
import com.mapswithme.maps.widget.recycler.RecyclerClickListener;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.StringUtils;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
@ -237,6 +238,11 @@ public class PlacePageView extends RelativeLayout
FULLSCREEN
}
public interface SetMapObjectListener
{
void onSetMapObjectComplete();
}
public PlacePageView(Context context)
{
this(context, null, 0);
@ -569,7 +575,7 @@ public class PlacePageView extends RelativeLayout
}
mSponsoredPrice = getContext().getString(R.string.place_page_starting_from, text);
refreshPreview();
refreshPreview(NetworkPolicy.newInstance(true));
}
@Override
@ -866,15 +872,44 @@ public class PlacePageView extends RelativeLayout
/**
* @param mapObject new MapObject
* @param force if true, new object'll be set without comparison with the old one
* @param listener listener
*/
public void setMapObject(MapObject mapObject, boolean force)
public void setMapObject(@Nullable MapObject mapObject, boolean force,
@Nullable final SetMapObjectListener listener)
{
if (!force && MapObject.same(mMapObject, mapObject))
{
if (listener != null)
listener.onSetMapObjectComplete();
return;
}
mMapObject = mapObject;
mSponsored = (mMapObject == null ? null : Sponsored.nativeGetCurrent());
if (isNetworkNeeded())
{
NetworkPolicy.checkNetworkPolicy(getContext(), new NetworkPolicy.NetworkPolicyListener()
{
@Override
public void onResult(@NonNull NetworkPolicy policy)
{
setMapObjectInternal(policy);
if (listener != null)
listener.onSetMapObjectComplete();
}
});
}
else
{
setMapObjectInternal(NetworkPolicy.newInstance(false));
if (listener != null)
listener.onSetMapObjectComplete();
}
}
private void setMapObjectInternal(@NonNull NetworkPolicy policy)
{
detachCountry();
if (mMapObject != null)
{
@ -886,10 +921,10 @@ public class PlacePageView extends RelativeLayout
Locale locale = Locale.getDefault();
Currency currency = Currency.getInstance(locale);
if (mSponsored.getType() == Sponsored.TYPE_BOOKING && mSponsored.getId() != null)
Sponsored.requestPrice(getContext(), mSponsored.getId(), currency.getCurrencyCode());
Sponsored.requestPrice(mSponsored.getId(), currency.getCurrencyCode(), policy);
// TODO: remove this after booking_api.cpp will be done
if (!USE_OLD_BOOKING)
Sponsored.requestInfo(getContext(), mSponsored, locale.toString());
Sponsored.requestInfo(mSponsored, locale.toString(), policy);
}
String country = MapManager.nativeGetSelectedCountry();
@ -897,15 +932,20 @@ public class PlacePageView extends RelativeLayout
attachCountry(country);
}
refreshViews();
refreshViews(policy);
}
public void refreshViews()
private boolean isNetworkNeeded()
{
return mMapObject != null && (isSponsored() || mMapObject.getBanner() != null);
}
public void refreshViews(@Nullable NetworkPolicy policy)
{
if (mMapObject == null)
return;
refreshPreview();
refreshPreview(policy);
refreshDetails();
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
@ -963,7 +1003,7 @@ public class PlacePageView extends RelativeLayout
}
}
private void refreshPreview()
private void refreshPreview(@Nullable NetworkPolicy policy)
{
UiUtils.setTextAndHideIfEmpty(mTvTitle, mMapObject.getTitle());
if (mToolbar != null)
@ -973,7 +1013,7 @@ public class PlacePageView extends RelativeLayout
UiUtils.hide(mAvDirection);
UiUtils.setTextAndHideIfEmpty(mTvAddress, mMapObject.getAddress());
boolean sponsored = (mSponsored != null && mSponsored.getType() != Sponsored.TYPE_NONE);
boolean sponsored = isSponsored();
UiUtils.showIf(sponsored, mSponsoredInfo);
if (sponsored)
{
@ -986,7 +1026,15 @@ public class PlacePageView extends RelativeLayout
}
if (mBannerController != null)
mBannerController.updateData(mMapObject.getBanner());
{
mBannerController.updateData(policy != null && policy.сanUseNetwork()
? mMapObject.getBanner() : null);
}
}
private boolean isSponsored()
{
return mSponsored != null && mSponsored.getType() != Sponsored.TYPE_NONE;
}
private void refreshDetails()
@ -1375,10 +1423,10 @@ public class PlacePageView extends RelativeLayout
private void toggleIsBookmark()
{
if (MapObject.isOfType(MapObject.BOOKMARK, mMapObject))
setMapObject(Framework.nativeDeleteBookmarkFromMapObject(), true);
setMapObject(Framework.nativeDeleteBookmarkFromMapObject(), true, null);
else
setMapObject(BookmarkManager.INSTANCE.addNewBookmark(BookmarkManager.nativeFormatNewBookmarkName(),
mMapObject.getLat(), mMapObject.getLon()), true);
mMapObject.getLat(), mMapObject.getLon()), true, null);
post(new Runnable()
{
@Override
@ -1571,6 +1619,6 @@ public class PlacePageView extends RelativeLayout
@Override
public void onBookmarkSaved(int categoryId, int bookmarkId)
{
setMapObject(BookmarkManager.INSTANCE.getBookmark(categoryId, bookmarkId), true);
setMapObject(BookmarkManager.INSTANCE.getBookmark(categoryId, bookmarkId), true, null);
}
}

View file

@ -263,30 +263,23 @@ public final class Sponsored
* Make request to obtain hotel price information.
* This method also checks cache for requested hotel id
* and if cache exists - call {@link #onPriceReceived(String, String, String) onPriceReceived} immediately
*
* @param id A Hotel id
* @param id A Hotel id
* @param currencyCode A user currency
* @param policy A network policy
*/
static void requestPrice(@NonNull Context context, @NonNull final String id,
@NonNull final String currencyCode)
static void requestPrice(@NonNull String id, @NonNull String currencyCode,
@NonNull NetworkPolicy policy)
{
Price p = sPriceCache.get(id);
if (p != null)
onPriceReceived(id, p.mPrice, p.mCurrency);
NetworkPolicy.checkNetworkPolicy(context, new NetworkPolicy.NetworkPolicyListener()
{
@Override
public void onResult(@NonNull NetworkPolicy policy)
{
nativeRequestPrice(policy, id, currencyCode);
}
});
nativeRequestPrice(policy, id, currencyCode);
}
static void requestInfo(@NonNull Context context, @NonNull Sponsored sponsored,
@NonNull String locale)
static void requestInfo(@NonNull Sponsored sponsored,
@NonNull String locale, @NonNull NetworkPolicy policy)
{
String id = sponsored.getId();
if (id == null)
@ -295,7 +288,7 @@ public final class Sponsored
switch (sponsored.getType())
{
case TYPE_BOOKING:
requestHotelInfo(context, id, locale);
requestHotelInfo(id, locale, policy);
break;
case TYPE_GEOCHAT:
// TODO: request geochat info
@ -312,25 +305,18 @@ public final class Sponsored
* Make request to obtain hotel information.
* This method also checks cache for requested hotel id
* and if cache exists - call {@link #onHotelInfoReceived(String, HotelInfo) onHotelInfoReceived} immediately
*
* @param id A Hotel id
* @param id A Hotel id
* @param locale A user locale
* @param policy A network policy
*/
private static void requestHotelInfo(@NonNull Context context, @NonNull final String id,
@NonNull final String locale)
private static void requestHotelInfo(@NonNull String id, @NonNull String locale,
@NonNull NetworkPolicy policy)
{
HotelInfo info = sInfoCache.get(id);
if (info != null)
onHotelInfoReceived(id, info);
NetworkPolicy.checkNetworkPolicy(context, new NetworkPolicy.NetworkPolicyListener()
{
@Override
public void onResult(@NonNull NetworkPolicy policy)
{
nativeRequestHotelInfo(policy, id, locale);
}
});
nativeRequestHotelInfo(policy, id, locale);
}
private static void onPriceReceived(@NonNull String id, @NonNull String price,

View file

@ -133,6 +133,11 @@ public final class NetworkPolicy
sDialog = new WeakReference<>(dialog);
}
public static NetworkPolicy newInstance(boolean canUse)
{
return new NetworkPolicy(canUse);
}
private final boolean mCanUseNetwork;
private NetworkPolicy(boolean canUse)