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 |
| index b9c40ded9b8ee5e88e283281fc072f93c802fef0..5e63a84cae9e6cd9c83443300e46ad8c4808ffca 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AndroidPaymentApp.java |
| @@ -5,15 +5,23 @@ |
| package org.chromium.chrome.browser.payments; |
| import android.app.Activity; |
| +import android.content.ComponentName; |
| +import android.content.Context; |
| import android.content.Intent; |
| +import android.content.ServiceConnection; |
| import android.graphics.drawable.Drawable; |
| import android.os.Bundle; |
| import android.os.Handler; |
| +import android.os.IBinder; |
| +import android.os.RemoteException; |
| import android.util.JsonWriter; |
| +import org.chromium.IsReadyToPayService; |
| +import org.chromium.IsReadyToPayServiceCallback; |
| import org.chromium.chrome.R; |
| import org.chromium.content.browser.ContentViewCore; |
| import org.chromium.content_public.browser.WebContents; |
| + |
|
please use gerrit instead
2017/01/09 19:02:14
No need for newline here.
rwlbuis
2017/01/10 16:40:00
Done.
|
| import org.chromium.payments.mojom.PaymentItem; |
| import org.chromium.payments.mojom.PaymentMethodData; |
| import org.chromium.ui.base.WindowAndroid; |
| @@ -35,13 +43,16 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, |
| private static final String EXTRA_ORIGIN = "origin"; |
| private static final String EXTRA_DETAILS = "details"; |
| private static final String EXTRA_INSTRUMENT_DETAILS = "instrumentDetails"; |
| + private static final String READY_TO_PAY = "readyToPay"; |
|
please use gerrit instead
2017/01/09 19:02:14
Unused.
rwlbuis
2017/01/10 16:40:00
Done.
|
| private static final String EMPTY_JSON_DATA = "{}"; |
| private final Handler mHandler; |
| private final WebContents mWebContents; |
| + private final Intent mIsReadyToPayIntent; |
| private final Intent mPayIntent; |
| private final Set<String> mMethodNames; |
| - private String mIsReadyToPayService; |
| + private IsReadyToPayService mIsReadyToPayService; |
| + private InstrumentsCallback mInstrumentsCallback; |
| private InstrumentDetailsCallback mInstrumentDetailsCallback; |
| /** |
| @@ -60,6 +71,8 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, |
| mHandler = new Handler(); |
| mWebContents = webContents; |
| mPayIntent = new Intent(); |
| + mIsReadyToPayIntent = new Intent(); |
| + mIsReadyToPayIntent.setPackage(packageName); |
| mPayIntent.setClassName(packageName, activity); |
| mMethodNames = new HashSet<>(); |
| } |
| @@ -70,19 +83,22 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, |
| } |
| /** @param service The name of the "is ready to pay" service in the payment app. */ |
|
please use gerrit instead
2017/01/09 19:02:14
Update JavaDoc.
rwlbuis
2017/01/10 16:40:00
Done.
|
| - public void setIsReadyToPayService(String service) { |
| - mIsReadyToPayService = service; |
| + public void setIsReadyToPayAction(String className) { |
| + mIsReadyToPayIntent.setClassName(mIsReadyToPayIntent.getPackage(), className); |
| } |
| @Override |
| - public void getInstruments(Map<String, PaymentMethodData> methodData, String origin, |
| + public void getInstruments(final Map<String, PaymentMethodData> methodData, final String origin, |
| final InstrumentsCallback callback) { |
| + mInstrumentsCallback = callback; |
| mHandler.post(new Runnable() { |
|
please use gerrit instead
2017/01/09 19:02:14
Use the handler only to make sendInstrumentsReady(
rwlbuis
2017/01/10 16:40:00
Done.
please use gerrit instead
2017/01/10 16:49:22
You seem to have forgotten to update the code.
rwlbuis
2017/01/10 19:31:05
Oops! Done.
|
| @Override |
| public void run() { |
| - List<PaymentInstrument> instruments = new ArrayList<>(); |
| - instruments.add(AndroidPaymentApp.this); |
| - callback.onInstrumentsReady(AndroidPaymentApp.this, instruments); |
| + if (mIsReadyToPayIntent.getComponent() != null) { |
| + isReadyToPay(origin, methodData.get(mMethodNames.iterator().next())); |
| + } else { |
| + sendInstrumentsReady(AndroidPaymentApp.this); |
| + } |
| } |
| }); |
| } |
| @@ -95,6 +111,81 @@ public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, |
| return !methodNames.isEmpty(); |
| } |
| + private void sendInstrumentsReady(PaymentInstrument instrument) { |
| + List<PaymentInstrument> instruments = null; |
| + if (instrument != null) { |
| + instruments = new ArrayList<>(); |
| + instruments.add(instrument); |
| + } |
| + mInstrumentsCallback.onInstrumentsReady(AndroidPaymentApp.this, instruments); |
|
please use gerrit instead
2017/01/09 19:02:14
|this| is sufficient here, because you're not in a
rwlbuis
2017/01/10 16:40:00
Done.
|
| + } |
| + |
| + private void isReadyToPay(String origin, PaymentMethodData data) { |
| + Bundle extras = new Bundle(); |
| + extras.putString(EXTRA_METHOD_NAME, mMethodNames.iterator().next()); |
| + extras.putString(EXTRA_ORIGIN, origin); |
| + extras.putString(EXTRA_DATA, data == null ? EMPTY_JSON_DATA : data.stringifiedData); |
| + mIsReadyToPayIntent.putExtras(extras); |
| + |
| + if (mIsReadyToPayService != null) { |
| + sendIsReadyToPay(); |
| + } else { |
| + try { |
| + ContentViewCore contentView = ContentViewCore.fromWebContents(mWebContents); |
| + if (contentView == null) { |
| + notifyError(); |
| + return; |
| + } |
| + |
| + WindowAndroid window = contentView.getWindowAndroid(); |
| + if (window == null) { |
| + notifyError(); |
| + return; |
| + } |
| + |
| + window.getApplicationContext().bindService( |
| + mIsReadyToPayIntent, mServiceConnection, Context.BIND_AUTO_CREATE); |
| + } catch (SecurityException e) { |
|
please use gerrit instead
2017/01/09 19:02:14
Only bindService() can throw a SecurityException.
rwlbuis
2017/01/10 16:40:00
Done.
|
| + sendInstrumentsReady(null); |
| + } |
| + } |
| + } |
| + |
| + private final ServiceConnection mServiceConnection = new ServiceConnection() { |
|
please use gerrit instead
2017/01/09 19:02:14
Variables should be above methods.
rwlbuis
2017/01/10 16:40:00
Done.
|
| + @Override |
| + public void onServiceConnected(ComponentName name, IBinder service) { |
| + mIsReadyToPayService = IsReadyToPayService.Stub.asInterface(service); |
| + if (mIsReadyToPayService == null) { |
| + sendInstrumentsReady(null); |
| + } else { |
| + sendIsReadyToPay(); |
| + } |
| + } |
| + |
| + @Override |
| + public void onServiceDisconnected(ComponentName name) { |
| + sendInstrumentsReady(null); |
| + } |
| + }; |
| + private void sendIsReadyToPay() { |
| + assert mIsReadyToPayService != null; |
| + final IsReadyToPayServiceCallback.Stub callback = new IsReadyToPayServiceCallback.Stub() { |
|
please use gerrit instead
2017/01/09 19:02:14
The "final" keyword seems unnecessary.
rwlbuis
2017/01/10 16:40:00
Done.
|
| + @Override |
| + public void handleIsReadyToPay(boolean isReadyToPay) throws RemoteException { |
| + if (isReadyToPay) { |
| + sendInstrumentsReady(AndroidPaymentApp.this); |
| + } else { |
| + sendInstrumentsReady(null); |
| + } |
| + } |
| + }; |
| + try { |
| + mIsReadyToPayService.isReadyToPay(callback); |
| + } catch (Exception e) { |
|
please use gerrit instead
2017/01/09 19:02:14
We prefer explicit exception types.
rwlbuis
2017/01/10 16:40:00
This is actually an important part of this patch.
please use gerrit instead
2017/01/10 16:49:22
Please make sure in your patch that the payment ap
rwlbuis
2017/01/10 19:31:05
For reference I found a description of the excepti
|
| + sendInstrumentsReady(null); |
| + } |
| + } |
| + |
| @Override |
| public String getAppIdentifier() { |
| return getIdentifier(); |