Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..adf0d2d026835fe3a8dcf2b49691d4dc34d6f6e4 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java |
| @@ -0,0 +1,192 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
gone
2017/01/03 19:20:03
Happy new year!
please use gerrit instead
2017/01/04 17:27:53
You too!
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.payments; |
| + |
| +import android.app.Activity; |
| +import android.content.Intent; |
| +import android.graphics.drawable.Drawable; |
| +import android.os.Bundle; |
| +import android.os.Handler; |
| +import android.util.JsonWriter; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.payments.mojom.PaymentItem; |
| +import org.chromium.payments.mojom.PaymentMethodData; |
| +import org.chromium.ui.base.WindowAndroid; |
| + |
| +import java.io.IOException; |
| +import java.io.StringWriter; |
| +import java.util.ArrayList; |
| +import java.util.Collections; |
| +import java.util.HashSet; |
| +import java.util.List; |
| +import java.util.Map; |
| +import java.util.Set; |
| + |
| +/** A locally installed payment app. */ |
|
gone
2017/01/03 19:20:03
nit: Maybe "interfaces with a locally installed pa
please use gerrit instead
2017/01/04 17:27:53
Done.
|
| +public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, |
| + WindowAndroid.IntentCallback { |
| + private static final String METHOD_NAME_EXTRA = "methodName"; |
|
gone
2017/01/03 19:20:03
nit: Consider prefixing these with EXTRA_ instead
please use gerrit instead
2017/01/04 17:27:53
Done.
|
| + private static final String DATA_EXTRA = "data"; |
| + private static final String ORIGIN_EXTRA = "origin"; |
| + private static final String DETAILS_EXTRA = "details"; |
| + private static final String INSTRUMENT_DETAILS_EXTRA = "instrumentDetails"; |
| + private static final String EMPTY_JSON_DATA = "{}"; |
| + |
| + private final Handler mHandler; |
| + private final WindowAndroid mWindow; |
| + private final Intent mPayIntent; |
| + private final Set<String> mMethodNames; |
| + private String mIsReadyToPayService; |
| + private InstrumentDetailsCallback mInstrumentDetailsCallback; |
| + |
| + public AndroidPaymentApp(WindowAndroid window, String packageName, String activity, |
| + String label, Drawable icon) { |
| + super(packageName, label, null, icon); |
| + mHandler = new Handler(); |
| + mWindow = window; |
| + mPayIntent = new Intent(); |
| + mPayIntent.setClassName(packageName, activity); |
| + mMethodNames = new HashSet<>(); |
| + } |
| + |
| + public void addMethodName(String methodName) { |
|
gone
2017/01/03 19:20:03
Javadocs for public methods. Findbugs might compl
please use gerrit instead
2017/01/04 17:27:53
Done.
|
| + mMethodNames.add(methodName); |
| + } |
| + |
| + public void setIsReadyToPayService(String service) { |
| + mIsReadyToPayService = service; |
| + } |
| + |
| + @Override |
| + public void getInstruments(Map<String, PaymentMethodData> methodData, String origin, |
| + final InstrumentsCallback callback) { |
| + mHandler.post(new Runnable() { |
| + @Override |
| + public void run() { |
| + List<PaymentInstrument> instruments = new ArrayList<>(); |
| + instruments.add(AndroidPaymentApp.this); |
| + callback.onInstrumentsReady(AndroidPaymentApp.this, instruments); |
| + } |
| + }); |
| + } |
| + |
| + @Override |
| + public boolean supportsMethodsAndData(Map<String, PaymentMethodData> methodsAndData) { |
| + assert methodsAndData != null; |
| + Set<String> methodNames = new HashSet<>(methodsAndData.keySet()); |
| + methodNames.retainAll(getAppMethodNames()); |
| + return !methodNames.isEmpty(); |
| + } |
| + |
| + @Override |
| + public String getAppIdentifier() { |
| + return getIdentifier(); |
| + } |
| + |
| + @Override |
| + public Set<String> getAppMethodNames() { |
| + return Collections.unmodifiableSet(mMethodNames); |
| + } |
| + |
| + @Override |
| + public Set<String> getInstrumentMethodNames() { |
| + return Collections.unmodifiableSet(mMethodNames); |
|
gone
2017/01/03 19:20:03
Is this function's return value always going to ma
please use gerrit instead
2017/01/04 17:27:53
Done.
|
| + } |
| + |
| + @Override |
| + public void invokePaymentApp(String merchantName, String origin, PaymentItem total, |
| + List<PaymentItem> cart, Map<String, PaymentMethodData> methodDataMap, |
| + InstrumentDetailsCallback callback) { |
| + assert !mMethodNames.isEmpty(); |
| + |
| + Bundle extras = new Bundle(); |
| + extras.putString(ORIGIN_EXTRA, origin); |
| + |
| + String methodName = mMethodNames.iterator().next(); |
| + extras.putString(METHOD_NAME_EXTRA, methodName); |
| + |
| + PaymentMethodData methodData = methodDataMap.get(methodName); |
| + extras.putString( |
| + DATA_EXTRA, methodData == null ? EMPTY_JSON_DATA : methodData.stringifiedData); |
| + |
| + String details = serializeDetails(total, cart); |
| + extras.putString(DETAILS_EXTRA, details == null ? EMPTY_JSON_DATA : details); |
| + mPayIntent.putExtras(extras); |
| + |
| + mInstrumentDetailsCallback = callback; |
| + if (!mWindow.showIntent(mPayIntent, this, R.string.payments_android_app_error)) { |
| + mHandler.post(new Runnable() { |
| + @Override |
| + public void run() { |
| + mInstrumentDetailsCallback.onInstrumentDetailsError(); |
| + } |
| + }); |
| + } |
| + } |
| + |
| + private static String serializeDetails(PaymentItem total, List<PaymentItem> cart) { |
| + StringWriter stringWriter = new StringWriter(); |
| + JsonWriter json = new JsonWriter(stringWriter); |
| + try { |
| + // details {{{ |
| + json.beginObject(); |
| + |
| + // total {{{ |
| + json.name("total"); |
| + serializePaymentItem(json, total); |
| + // }}} total |
| + |
| + // displayitems {{{ |
| + if (cart != null) { |
| + json.name("displayItems").beginArray(); |
| + for (int i = 0; i < cart.size(); i++) { |
| + serializePaymentItem(json, cart.get(i)); |
| + } |
| + json.endArray(); |
| + } |
| + // }}} displayItems |
| + |
| + json.endObject(); |
| + // }}} details |
| + } catch (IOException e) { |
| + return null; |
| + } |
| + |
| + return stringWriter.toString(); |
| + } |
| + |
| + private static void serializePaymentItem(JsonWriter json, PaymentItem item) throws IOException { |
| + // item {{{ |
| + json.beginObject(); |
| + json.name("label").value(item.label); |
| + |
| + // amount {{{ |
| + json.name("amount").beginObject(); |
| + json.name("currency").value(item.amount.currency); |
| + json.name("value").value(item.amount.value); |
| + json.endObject(); |
| + // }}} amount |
| + |
| + json.endObject(); |
| + // }}} item |
| + } |
| + |
| + @Override |
| + public void onIntentCompleted(WindowAndroid window, int resultCode, Intent data) { |
| + mWindow.removeIntentCallback(this); |
| + if (data == null || data.getExtras() == null || resultCode != Activity.RESULT_OK) { |
| + mInstrumentDetailsCallback.onInstrumentDetailsError(); |
| + } else { |
| + mInstrumentDetailsCallback.onInstrumentDetailsReady( |
| + data.getExtras().getString(METHOD_NAME_EXTRA), |
| + data.getExtras().getString(INSTRUMENT_DETAILS_EXTRA)); |
| + } |
| + mInstrumentDetailsCallback = null; |
| + } |
| + |
| + @Override |
| + public void dismissInstrument() {} |
| +} |