forked from organicmaps/organicmaps
[android] Added bookmark payment data parsing
This commit is contained in:
parent
fa686ba7a6
commit
ba6f7f4c19
3 changed files with 120 additions and 0 deletions
|
@ -2,6 +2,7 @@ package com.mapswithme.maps.bookmarks;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
|
@ -10,6 +11,7 @@ import android.widget.Toast;
|
|||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.auth.Authorizer;
|
||||
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
|
||||
import com.mapswithme.maps.bookmarks.data.PaymentData;
|
||||
import com.mapswithme.util.log.Logger;
|
||||
import com.mapswithme.util.log.LoggerFactory;
|
||||
|
||||
|
@ -87,6 +89,12 @@ class DefaultBookmarkDownloadController implements BookmarkDownloadController,
|
|||
dm.enqueueRequest(url);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static PaymentDataParser createPaymentDataParser()
|
||||
{
|
||||
return new BookmarkPaymentDataParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationRequired()
|
||||
{
|
||||
|
@ -96,9 +104,20 @@ class DefaultBookmarkDownloadController implements BookmarkDownloadController,
|
|||
@Override
|
||||
public void onPaymentRequired()
|
||||
{
|
||||
if (TextUtils.isEmpty(mDownloadUrl))
|
||||
throw new IllegalStateException("Download url must be non-null if payment required!");
|
||||
|
||||
PaymentData data = parsePaymentData(mDownloadUrl);
|
||||
//TODO: pass data and show payment UI.
|
||||
Toast.makeText(getActivityOrThrow(), "Payment required. Ui coming soon!",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static PaymentData parsePaymentData(@NonNull String url)
|
||||
{
|
||||
PaymentDataParser parser = createPaymentDataParser();
|
||||
return parser.parse(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -141,4 +160,36 @@ class DefaultBookmarkDownloadController implements BookmarkDownloadController,
|
|||
{
|
||||
// Do nothing by default.
|
||||
}
|
||||
|
||||
private static class BookmarkPaymentDataParser implements PaymentDataParser
|
||||
{
|
||||
private final static String SERVER_ID = "id";
|
||||
private final static String PRODUCT_ID = "tier";
|
||||
private final static String NAME = "name";
|
||||
private final static String IMG_URL = "img";
|
||||
private final static String AUTHOR_NAME = "author_name";
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public PaymentData parse(@NonNull String url)
|
||||
{
|
||||
Uri uri = Uri.parse(url);
|
||||
String serverId = getQueryRequiredParameter(uri, SERVER_ID);
|
||||
String productId = getQueryRequiredParameter(uri, PRODUCT_ID);
|
||||
String name = getQueryRequiredParameter(uri, NAME);
|
||||
String authorName = getQueryRequiredParameter(uri, AUTHOR_NAME);
|
||||
String imgUrl = uri.getQueryParameter(IMG_URL);
|
||||
return new PaymentData(serverId, productId, name, imgUrl, authorName);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static String getQueryRequiredParameter(@NonNull Uri uri, @NonNull String name)
|
||||
{
|
||||
String parameter = uri.getQueryParameter(name);
|
||||
if (TextUtils.isEmpty(parameter))
|
||||
throw new AssertionError("'" + parameter + "' parameter is required!");
|
||||
|
||||
return parameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mapswithme.maps.bookmarks.data.PaymentData;
|
||||
|
||||
interface PaymentDataParser
|
||||
{
|
||||
@NonNull
|
||||
PaymentData parse(@NonNull String url);
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.mapswithme.maps.bookmarks.data;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
public class PaymentData
|
||||
{
|
||||
@NonNull
|
||||
private final String mServerId;
|
||||
@NonNull
|
||||
private final String mProductId;
|
||||
@NonNull
|
||||
private final String mName;
|
||||
@Nullable
|
||||
private final String mImgUrl;
|
||||
@NonNull
|
||||
private final String mAuthorName;
|
||||
|
||||
public PaymentData(@NonNull String serverId, @NonNull String productId, @NonNull String name,
|
||||
@Nullable String imgUrl, @NonNull String authorName)
|
||||
{
|
||||
mServerId = serverId;
|
||||
mProductId = productId;
|
||||
mName = name;
|
||||
mImgUrl = imgUrl;
|
||||
mAuthorName = authorName;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getServerId()
|
||||
{
|
||||
return mServerId;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getProductId()
|
||||
{
|
||||
return mProductId;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getName()
|
||||
{
|
||||
return mName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getImgUrl()
|
||||
{
|
||||
return mImgUrl;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getAuthorName()
|
||||
{
|
||||
return mAuthorName;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue