[android][booking] add: Refresh hotel min price and show in PP.

This commit is contained in:
Alexander Marchuk 2016-06-09 18:36:04 +03:00 committed by Vladimir Byko-Ianko
parent ed2daa5571
commit 465370792d
8 changed files with 217 additions and 63 deletions

View file

@ -88,6 +88,7 @@ LOCAL_SRC_FILES := \
com/mapswithme/maps/SearchEngine.cpp \
com/mapswithme/maps/SearchRecents.cpp \
com/mapswithme/maps/UserMarkHelper.cpp \
com/mapswithme/maps/SponsoredHotel.cpp \
com/mapswithme/maps/settings/UnitLocale.cpp \
com/mapswithme/platform/Platform.cpp \
com/mapswithme/platform/HttpThread.cpp \

View file

@ -458,6 +458,11 @@ place_page::Info & Framework::GetPlacePageInfo()
return m_info;
}
void Framework::RequestBookingMinPrice(string const & hotelId, string const & currencyCode, function<void(string const &, string const &)> const & callback)
{
return m_work.GetBookingApi().GetMinPrice(hotelId, currencyCode, callback);
}
bool Framework::HasSpaceForMigration()
{
return m_work.IsEnoughSpaceForMigrate();
@ -1048,20 +1053,4 @@ Java_com_mapswithme_maps_Framework_nativeGetActiveObjectFormattedCuisine(JNIEnv
return jni::ToJavaString(env, g_framework->GetPlacePageInfo().FormatCuisines());
}
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_Framework_nativeGetSponsoredHotelInfo(JNIEnv * env, jclass clazz)
{
place_page::Info const & ppInfo = g_framework->GetPlacePageInfo();
if (!ppInfo.m_isSponsoredHotel)
return nullptr;
static jclass const infoClass = jni::GetGlobalClassRef(env, "com/mapswithme/maps/widget/placepage/SponsoredHotelInfo");
static jmethodID const infoCtor = jni::GetConstructorID(env, infoClass, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
return env->NewObject(infoClass, infoCtor, jni::ToJavaString(env, ppInfo.GetRatingFormatted()),
jni::ToJavaString(env, ppInfo.GetApproximatePricing()),
jni::ToJavaString(env, ppInfo.GetSponsoredBookingUrl()),
jni::ToJavaString(env, ppInfo.GetSponsoredDescriptionUrl()));
}
} // extern "C"

View file

@ -152,6 +152,7 @@ namespace android
void SetPlacePageInfo(place_page::Info const & info);
place_page::Info & GetPlacePageInfo();
void RequestBookingMinPrice(string const & hotelId, string const & currency, function<void(string const &, string const &)> const & callback);
bool HasSpaceForMigration();
storage::TCountryId PreMigrate(ms::LatLon const & position, storage::Storage::TChangeCountryFunction const & statusChangeListener,

View file

@ -0,0 +1,71 @@
#include "Framework.hpp"
#include "../core/jni_helper.hpp"
#include "../platform/Platform.hpp"
#include "map/place_page_info.hpp"
#include "std/bind.hpp"
namespace
{
jclass g_hotelClass;
jmethodID g_hotelClassCtor;
jmethodID g_priceCallback;
void PrepareClassRefs(JNIEnv * env, jclass hotelClass)
{
if (g_hotelClass)
return;
g_hotelClass = static_cast<jclass>(env->NewGlobalRef(hotelClass));
// SponsoredHotel(String rating, String price, String urlBook, String urlDescription)
g_hotelClassCtor = jni::GetConstructorID(env, g_hotelClass, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
// static void onPriceReceived(final String id, final String price, final String currency)
g_priceCallback = jni::GetStaticMethodID(env, g_hotelClass, "onPriceReceived", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
}
} // namespace
extern "C"
{
// static SponsoredHotel nativeGetCurrent();
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_widget_placepage_SponsoredHotel_nativeGetCurrent(JNIEnv * env, jclass clazz)
{
PrepareClassRefs(env, clazz);
place_page::Info const & ppInfo = g_framework->GetPlacePageInfo();
if (!ppInfo.m_isSponsoredHotel)
return nullptr;
return env->NewObject(g_hotelClass, g_hotelClassCtor, jni::ToJavaString(env, ppInfo.GetRatingFormatted()),
jni::ToJavaString(env, ppInfo.GetApproximatePricing()),
jni::ToJavaString(env, ppInfo.GetSponsoredBookingUrl()),
jni::ToJavaString(env, ppInfo.GetSponsoredDescriptionUrl()));
}
// static void nativeRequestPrice(String id, String currencyCode);
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_widget_placepage_SponsoredHotel_nativeRequestPrice(JNIEnv * env, jclass clazz, jstring id, jstring currencyCode)
{
PrepareClassRefs(env, clazz);
string const hotelId = jni::ToNativeString(env, id);
string const code = jni::ToNativeString(env, currencyCode);
g_framework->RequestBookingMinPrice(hotelId, code, [hotelId](string const & price, string const & currency)
{
GetPlatform().RunOnGuiThread([=]()
{
JNIEnv * env = jni::GetEnv();
env->CallStaticVoidMethod(g_hotelClass, g_priceCallback, jni::ToJavaString(env, hotelId),
jni::ToJavaString(env, price),
jni::ToJavaString(env, currency));
});
});
}
} // extern "C"

View file

@ -11,7 +11,6 @@ import java.lang.annotation.RetentionPolicy;
import com.mapswithme.maps.bookmarks.data.DistanceAndAzimut;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.routing.RoutingInfo;
import com.mapswithme.maps.widget.placepage.SponsoredHotelInfo;
import com.mapswithme.util.Constants;
/**
@ -212,5 +211,4 @@ public class Framework
public static native boolean nativeIsInChoosePositionMode();
public static native boolean nativeIsDownloadedMapAtScreenCenter();
public static native String nativeGetActiveObjectFormattedCuisine();
public static native SponsoredHotelInfo nativeGetSponsoredHotelInfo();
}

View file

@ -40,8 +40,10 @@ import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -84,7 +86,10 @@ import com.mapswithme.util.statistics.AlohaHelper;
import com.mapswithme.util.statistics.Statistics;
public class PlacePageView extends RelativeLayout implements View.OnClickListener, View.OnLongClickListener
public class PlacePageView extends RelativeLayout
implements View.OnClickListener,
View.OnLongClickListener,
SponsoredHotel.OnPriceReceivedListener
{
private static final String PREF_USE_DMS = "use_dms";
@ -144,7 +149,8 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
private MwmActivity.LeftAnimationTrackListener mLeftAnimationTrackListener;
// Data
private MapObject mMapObject;
private SponsoredHotelInfo mSponsoredHotelInfo;
private SponsoredHotel mSponsoredHotel;
private String mSponsoredHotelPrice;
private boolean mIsLatLonDms;
// Downloader`s stuff
@ -434,6 +440,28 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
mDetails.setBackgroundResource(0);
SponsoredHotel.setListener(this);
}
@Override
public void onPriceReceived(String id, String price, String currencyCode)
{
if (mSponsoredHotel == null || !TextUtils.equals(id, mSponsoredHotel.getId()))
return;
String text;
try
{
float value = Float.valueOf(price);
text = NumberFormat.getCurrencyInstance().format(value);
} catch (NumberFormatException e)
{
text = (price + " " + currencyCode);
}
mSponsoredHotelPrice = getContext().getString(R.string.place_page_starting_from, text);
refreshPreview();
}
private void onBookingClick(final boolean book)
@ -447,7 +475,7 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
if (!result)
return;
SponsoredHotelInfo info = mSponsoredHotelInfo;
SponsoredHotel info = mSponsoredHotel;
if (info == null)
return;
@ -574,13 +602,19 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
return;
mMapObject = mapObject;
mSponsoredHotelInfo = (mMapObject == null ? null : Framework.nativeGetSponsoredHotelInfo());
mSponsoredHotel = (mMapObject == null ? null : SponsoredHotel.nativeGetCurrent());
detachCountry();
if (mMapObject != null)
{
if (mSponsoredHotelInfo != null)
mSponsoredHotelInfo.updateId(mMapObject);
if (mSponsoredHotel != null)
{
mSponsoredHotel.updateId(mMapObject);
mSponsoredHotelPrice = mSponsoredHotel.price;
Currency currency = Currency.getInstance(Locale.getDefault());
SponsoredHotel.requestPrice(mSponsoredHotel.getId(), currency.getCurrencyCode());
}
String country = MapManager.nativeGetSelectedCountry();
if (country != null)
@ -662,12 +696,12 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
UiUtils.hide(mAvDirection);
UiUtils.setTextAndHideIfEmpty(mTvAddress, mMapObject.getAddress());
boolean sponsored = (mSponsoredHotelInfo != null);
boolean sponsored = (mSponsoredHotel != null);
UiUtils.showIf(sponsored, mHotelInfo);
if (sponsored)
{
mTvHotelRating.setText(mSponsoredHotelInfo.rating);
UiUtils.setTextAndHideIfEmpty(mTvHotelPrice, mSponsoredHotelInfo.price);
mTvHotelRating.setText(mSponsoredHotel.rating);
UiUtils.setTextAndHideIfEmpty(mTvHotelPrice, mSponsoredHotelPrice);
}
}
@ -675,7 +709,7 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
{
refreshLatLon();
if (mSponsoredHotelInfo == null)
if (mSponsoredHotel == null)
{
final String website = mMapObject.getMetadata(Metadata.MetadataType.FMD_WEBSITE);
refreshMetadataOrHide(TextUtils.isEmpty(website) ? mMapObject.getMetadata(Metadata.MetadataType.FMD_URL) : website, mWebsite, mTvWebsite);
@ -703,7 +737,7 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
UiUtils.showIf(Editor.nativeShouldShowAddPlace(), mAddPlace);
}
UiUtils.showIf(mSponsoredHotelInfo != null, mMoreInfo);
UiUtils.showIf(mSponsoredHotel != null, mMoreInfo);
}
private void refreshOpeningHours()
@ -801,7 +835,7 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
if (showBackButton || ParsedMwmRequest.isPickPointMode())
buttons.add(PlacePageButtons.Item.BACK);
if (mSponsoredHotelInfo != null)
if (mSponsoredHotel != null)
buttons.add(PlacePageButtons.Item.BOOKING);
buttons.add(PlacePageButtons.Item.BOOKMARK);

View file

@ -0,0 +1,93 @@
package com.mapswithme.maps.widget.placepage;
import android.support.annotation.UiThread;
import android.text.TextUtils;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.bookmarks.data.Metadata;
@UiThread
final class SponsoredHotel
{
private static class Price
{
final String price;
final String currency;
private Price(String price, String currency)
{
this.price = price;
this.currency = currency;
}
}
interface OnPriceReceivedListener
{
void onPriceReceived(String id, String price, String currency);
}
// Hotel ID -> Price
private static final Map<String, Price> sPriceCache = new HashMap<>();
private static WeakReference<OnPriceReceivedListener> sListener;
private String mId;
final String rating;
final String price;
final String urlBook;
final String urlDescription;
public SponsoredHotel(String rating, String price, String urlBook, String urlDescription)
{
this.rating = rating;
this.price = price;
this.urlBook = urlBook;
this.urlDescription = urlDescription;
}
void updateId(MapObject point)
{
mId = point.getMetadata(Metadata.MetadataType.FMD_SPONSORED_ID);
}
public String getId()
{
return mId;
}
public static void setListener(OnPriceReceivedListener listener)
{
sListener = new WeakReference<>(listener);
}
static void requestPrice(String id, String currencyCode)
{
Price p = sPriceCache.get(id);
if (p != null)
onPriceReceived(id, p.price, p.currency);
nativeRequestPrice(id, currencyCode);
}
@SuppressWarnings("unused")
private static void onPriceReceived(String id, String price, String currency)
{
if (TextUtils.isEmpty(price))
return;
sPriceCache.put(id, new Price(price, currency));
OnPriceReceivedListener listener = sListener.get();
if (listener == null)
sListener = null;
else
listener.onPriceReceived(id, price, currency);
}
public static native SponsoredHotel nativeGetCurrent();
private static native void nativeRequestPrice(String id, String currencyCode);
}

View file

@ -1,33 +0,0 @@
package com.mapswithme.maps.widget.placepage;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.bookmarks.data.Metadata;
@SuppressWarnings("WeakerAccess")
public final class SponsoredHotelInfo
{
private String mId;
final String rating;
final String price;
final String urlBook;
final String urlDescription;
public SponsoredHotelInfo(String rating, String price, String urlBook, String urlDescription)
{
this.rating = rating;
this.price = price;
this.urlBook = urlBook;
this.urlDescription = urlDescription;
}
public void updateId(MapObject point)
{
mId = point.getMetadata(Metadata.MetadataType.FMD_SPONSORED_ID);
}
public String getId()
{
return mId;
}
}