| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.ComponentName; |
| 9 import android.content.Context; |
| 8 import android.content.Intent; | 10 import android.content.Intent; |
| 11 import android.content.ServiceConnection; |
| 9 import android.graphics.drawable.Drawable; | 12 import android.graphics.drawable.Drawable; |
| 10 import android.os.Bundle; | 13 import android.os.Bundle; |
| 11 import android.os.Handler; | 14 import android.os.Handler; |
| 15 import android.os.IBinder; |
| 16 import android.os.RemoteException; |
| 12 import android.util.JsonWriter; | 17 import android.util.JsonWriter; |
| 13 | 18 |
| 19 import org.chromium.IsReadyToPayService; |
| 20 import org.chromium.IsReadyToPayServiceCallback; |
| 14 import org.chromium.chrome.R; | 21 import org.chromium.chrome.R; |
| 15 import org.chromium.content.browser.ContentViewCore; | 22 import org.chromium.content.browser.ContentViewCore; |
| 16 import org.chromium.content_public.browser.WebContents; | 23 import org.chromium.content_public.browser.WebContents; |
| 17 import org.chromium.payments.mojom.PaymentItem; | 24 import org.chromium.payments.mojom.PaymentItem; |
| 18 import org.chromium.payments.mojom.PaymentMethodData; | 25 import org.chromium.payments.mojom.PaymentMethodData; |
| 19 import org.chromium.ui.base.WindowAndroid; | 26 import org.chromium.ui.base.WindowAndroid; |
| 20 | 27 |
| 21 import java.io.IOException; | 28 import java.io.IOException; |
| 22 import java.io.StringWriter; | 29 import java.io.StringWriter; |
| 23 import java.util.ArrayList; | 30 import java.util.ArrayList; |
| 24 import java.util.Collections; | 31 import java.util.Collections; |
| 25 import java.util.HashSet; | 32 import java.util.HashSet; |
| 26 import java.util.List; | 33 import java.util.List; |
| 27 import java.util.Map; | 34 import java.util.Map; |
| 28 import java.util.Set; | 35 import java.util.Set; |
| 29 | 36 |
| 30 /** The point of interaction with a locally installed 3rd party native Android p
ayment app. */ | 37 /** The point of interaction with a locally installed 3rd party native Android p
ayment app. */ |
| 31 public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, | 38 public class AndroidPaymentApp extends PaymentInstrument implements PaymentApp, |
| 32 WindowAndroid.IntentCallback { | 39 WindowAndroid.IntentCallback { |
| 33 /** The action name for the Pay Intent. */ | 40 /** The action name for the Pay Intent. */ |
| 34 public static final String ACTION_PAY = "org.chromium.intent.action.PAY"; | 41 public static final String ACTION_PAY = "org.chromium.intent.action.PAY"; |
| 35 | 42 |
| 36 private static final String EXTRA_METHOD_NAME = "methodName"; | 43 private static final String EXTRA_METHOD_NAME = "methodName"; |
| 37 private static final String EXTRA_DATA = "data"; | 44 private static final String EXTRA_DATA = "data"; |
| 38 private static final String EXTRA_ORIGIN = "origin"; | 45 private static final String EXTRA_ORIGIN = "origin"; |
| 39 private static final String EXTRA_DETAILS = "details"; | 46 private static final String EXTRA_DETAILS = "details"; |
| 40 private static final String EXTRA_INSTRUMENT_DETAILS = "instrumentDetails"; | 47 private static final String EXTRA_INSTRUMENT_DETAILS = "instrumentDetails"; |
| 41 private static final String EMPTY_JSON_DATA = "{}"; | 48 private static final String EMPTY_JSON_DATA = "{}"; |
| 42 | |
| 43 private final Handler mHandler; | 49 private final Handler mHandler; |
| 44 private final WebContents mWebContents; | 50 private final WebContents mWebContents; |
| 51 private final Intent mIsReadyToPayIntent; |
| 45 private final Intent mPayIntent; | 52 private final Intent mPayIntent; |
| 46 private final Set<String> mMethodNames; | 53 private final Set<String> mMethodNames; |
| 47 private String mIsReadyToPayService; | 54 private IsReadyToPayService mIsReadyToPayService; |
| 55 private InstrumentsCallback mInstrumentsCallback; |
| 48 private InstrumentDetailsCallback mInstrumentDetailsCallback; | 56 private InstrumentDetailsCallback mInstrumentDetailsCallback; |
| 57 private final ServiceConnection mServiceConnection = new ServiceConnection()
{ |
| 58 @Override |
| 59 public void onServiceConnected(ComponentName name, IBinder service) { |
| 60 mIsReadyToPayService = IsReadyToPayService.Stub.asInterface(service)
; |
| 61 if (mIsReadyToPayService == null) { |
| 62 respondToGetInstrumentsQuery(null); |
| 63 } else { |
| 64 sendIsReadyToPayIntentToPaymentApp(); |
| 65 } |
| 66 } |
| 49 | 67 |
| 68 @Override |
| 69 public void onServiceDisconnected(ComponentName name) { |
| 70 respondToGetInstrumentsQuery(null); |
| 71 } |
| 72 }; |
| 50 /** | 73 /** |
| 51 * Builds the point of interaction with a locally installed 3rd party native
Android payment | 74 * Builds the point of interaction with a locally installed 3rd party native
Android payment |
| 52 * app. | 75 * app. |
| 53 * | 76 * |
| 54 * @param webContents The web contents. | 77 * @param webContents The web contents. |
| 55 * @param packageName The name of the package of the payment app. | 78 * @param packageName The name of the package of the payment app. |
| 56 * @param activity The name of the payment activity in the payment app. | 79 * @param activity The name of the payment activity in the payment app. |
| 57 * @param label The UI label to use for the payment app. | 80 * @param label The UI label to use for the payment app. |
| 58 * @param icon The icon to use in UI for the payment app. | 81 * @param icon The icon to use in UI for the payment app. |
| 59 */ | 82 */ |
| 60 public AndroidPaymentApp(WebContents webContents, String packageName, String
activity, | 83 public AndroidPaymentApp(WebContents webContents, String packageName, String
activity, |
| 61 String label, Drawable icon) { | 84 String label, Drawable icon) { |
| 62 super(packageName, label, null, icon); | 85 super(packageName, label, null, icon); |
| 63 mHandler = new Handler(); | 86 mHandler = new Handler(); |
| 64 mWebContents = webContents; | 87 mWebContents = webContents; |
| 65 mPayIntent = new Intent(); | 88 mPayIntent = new Intent(); |
| 89 mIsReadyToPayIntent = new Intent(); |
| 90 mIsReadyToPayIntent.setPackage(packageName); |
| 66 mPayIntent.setClassName(packageName, activity); | 91 mPayIntent.setClassName(packageName, activity); |
| 67 mPayIntent.setAction(ACTION_PAY); | 92 mPayIntent.setAction(ACTION_PAY); |
| 68 mMethodNames = new HashSet<>(); | 93 mMethodNames = new HashSet<>(); |
| 69 } | 94 } |
| 70 | 95 |
| 71 /** @param methodName A payment method that this app supports, e.g., "https:
//bobpay.com". */ | 96 /** @param methodName A payment method that this app supports, e.g., "https:
//bobpay.com". */ |
| 72 public void addMethodName(String methodName) { | 97 public void addMethodName(String methodName) { |
| 73 mMethodNames.add(methodName); | 98 mMethodNames.add(methodName); |
| 74 } | 99 } |
| 75 | 100 |
| 76 /** @param service The name of the "is ready to pay" service in the payment
app. */ | 101 /** @param className The class name of the "is ready to pay" service in the
payment app. */ |
| 77 public void setIsReadyToPayService(String service) { | 102 public void setIsReadyToPayAction(String className) { |
| 78 mIsReadyToPayService = service; | 103 mIsReadyToPayIntent.setClassName(mIsReadyToPayIntent.getPackage(), class
Name); |
| 79 } | 104 } |
| 80 | 105 |
| 81 @Override | 106 @Override |
| 82 public void getInstruments(Map<String, PaymentMethodData> methodData, String
origin, | 107 public void getInstruments(Map<String, PaymentMethodData> methodData, String
origin, |
| 83 final InstrumentsCallback callback) { | 108 InstrumentsCallback callback) { |
| 84 mHandler.post(new Runnable() { | 109 mInstrumentsCallback = callback; |
| 110 if (mIsReadyToPayIntent.getPackage() == null) { |
| 111 mHandler.post(new Runnable() { |
| 112 @Override |
| 113 public void run() { |
| 114 respondToGetInstrumentsQuery(AndroidPaymentApp.this); |
| 115 } |
| 116 }); |
| 117 return; |
| 118 } |
| 119 Bundle extras = new Bundle(); |
| 120 extras.putString(EXTRA_METHOD_NAME, mMethodNames.iterator().next()); |
| 121 extras.putString(EXTRA_ORIGIN, origin); |
| 122 PaymentMethodData data = methodData.get(mMethodNames.iterator().next()); |
| 123 extras.putString(EXTRA_DATA, data == null ? EMPTY_JSON_DATA : data.strin
gifiedData); |
| 124 mIsReadyToPayIntent.putExtras(extras); |
| 125 |
| 126 if (mIsReadyToPayService != null) { |
| 127 sendIsReadyToPayIntentToPaymentApp(); |
| 128 } else { |
| 129 ContentViewCore contentView = ContentViewCore.fromWebContents(mWebCo
ntents); |
| 130 if (contentView == null) { |
| 131 notifyError(); |
| 132 return; |
| 133 } |
| 134 |
| 135 WindowAndroid window = contentView.getWindowAndroid(); |
| 136 if (window == null) { |
| 137 notifyError(); |
| 138 return; |
| 139 } |
| 140 |
| 141 try { |
| 142 window.getApplicationContext().bindService( |
| 143 mIsReadyToPayIntent, mServiceConnection, Context.BIND_AU
TO_CREATE); |
| 144 } catch (SecurityException e) { |
| 145 respondToGetInstrumentsQuery(null); |
| 146 } |
| 147 } |
| 148 } |
| 149 |
| 150 private void respondToGetInstrumentsQuery(PaymentInstrument instrument) { |
| 151 List<PaymentInstrument> instruments = null; |
| 152 if (instrument != null) { |
| 153 instruments = new ArrayList<>(); |
| 154 instruments.add(instrument); |
| 155 } |
| 156 mInstrumentsCallback.onInstrumentsReady(this, instruments); |
| 157 } |
| 158 |
| 159 private void sendIsReadyToPayIntentToPaymentApp() { |
| 160 assert mIsReadyToPayService != null; |
| 161 IsReadyToPayServiceCallback.Stub callback = new IsReadyToPayServiceCallb
ack.Stub() { |
| 85 @Override | 162 @Override |
| 86 public void run() { | 163 public void handleIsReadyToPay(boolean isReadyToPay) throws RemoteEx
ception { |
| 87 List<PaymentInstrument> instruments = new ArrayList<>(); | 164 if (isReadyToPay) { |
| 88 instruments.add(AndroidPaymentApp.this); | 165 respondToGetInstrumentsQuery(AndroidPaymentApp.this); |
| 89 callback.onInstrumentsReady(AndroidPaymentApp.this, instruments)
; | 166 } else { |
| 167 respondToGetInstrumentsQuery(null); |
| 168 } |
| 90 } | 169 } |
| 91 }); | 170 }; |
| 171 try { |
| 172 mIsReadyToPayService.isReadyToPay(callback); |
| 173 } catch (Throwable e) { |
| 174 /** Many undocument exceptions are not caught in the remote Service
but passed on to |
| 175 the Service caller, see writeException in Parcel.java. */ |
| 176 respondToGetInstrumentsQuery(null); |
| 177 } |
| 92 } | 178 } |
| 93 | 179 |
| 94 @Override | 180 @Override |
| 95 public boolean supportsMethodsAndData(Map<String, PaymentMethodData> methods
AndData) { | 181 public boolean supportsMethodsAndData(Map<String, PaymentMethodData> methods
AndData) { |
| 96 assert methodsAndData != null; | 182 assert methodsAndData != null; |
| 97 Set<String> methodNames = new HashSet<>(methodsAndData.keySet()); | 183 Set<String> methodNames = new HashSet<>(methodsAndData.keySet()); |
| 98 methodNames.retainAll(getAppMethodNames()); | 184 methodNames.retainAll(getAppMethodNames()); |
| 99 return !methodNames.isEmpty(); | 185 return !methodNames.isEmpty(); |
| 100 } | 186 } |
| 101 | 187 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 mInstrumentDetailsCallback.onInstrumentDetailsReady( | 303 mInstrumentDetailsCallback.onInstrumentDetailsReady( |
| 218 data.getExtras().getString(EXTRA_METHOD_NAME), | 304 data.getExtras().getString(EXTRA_METHOD_NAME), |
| 219 data.getExtras().getString(EXTRA_INSTRUMENT_DETAILS)); | 305 data.getExtras().getString(EXTRA_INSTRUMENT_DETAILS)); |
| 220 } | 306 } |
| 221 mInstrumentDetailsCallback = null; | 307 mInstrumentDetailsCallback = null; |
| 222 } | 308 } |
| 223 | 309 |
| 224 @Override | 310 @Override |
| 225 public void dismissInstrument() {} | 311 public void dismissInstrument() {} |
| 226 } | 312 } |
| OLD | NEW |