Commit 00901f19 authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Chromium LUCI CQ

[WebLayer] Clean up getSelectedPaymentApp()

In PaymentUiService, the way to get the selected payment app is through
mPaymentMethodsSection, in spite of the fact that
getSelectedPaymentApp() is available. This CL is to replace these
mPaymentMethodsSection usages with getSelectedPaymentApp(). This will
help moving the payment app management logic out of PaymentUiService.

Bug: 1155614
Change-Id: I870d9b24f3cc78e007817c7b67bf9ca0639ccf34
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2574598
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833850}
parent 35e91505
......@@ -142,7 +142,7 @@ public class ChromePaymentRequestService
// Implements BrowserPaymentRequest:
@Override
public PaymentApp getSelectedPaymentApp() {
return (PaymentApp) mPaymentUiService.getSelectedPaymentApp();
return mPaymentUiService.getSelectedPaymentApp();
}
// Implements BrowserPaymentRequest:
......@@ -300,7 +300,7 @@ public class ChromePaymentRequestService
}
assert !mPaymentUiService.getPaymentApps().isEmpty();
PaymentApp selectedApp = (PaymentApp) mPaymentUiService.getSelectedPaymentApp();
PaymentApp selectedApp = mPaymentUiService.getSelectedPaymentApp();
dimBackgroundIfNotBottomSheetPaymentHandler(selectedApp);
mDidRecordShowEvent = true;
mJourneyLogger.setEventOccurred(Event.SKIPPED_SHOW);
......@@ -325,7 +325,7 @@ public class ChromePaymentRequestService
return false;
}
PaymentApp app = (PaymentApp) mPaymentUiService.getSelectedPaymentApp();
PaymentApp app = mPaymentUiService.getSelectedPaymentApp();
if (app == null || !app.isReadyForMinimalUI() || TextUtils.isEmpty(app.accountBalance())) {
return false;
}
......@@ -648,13 +648,13 @@ public class ChromePaymentRequestService
// Implements BrowserPaymentRequest:
@Override
public void onInstrumentDetailsReady() {
// If the payment method was an Autofill credit card with an identifier, record its use.
PaymentApp selectedPaymentMethod = (PaymentApp) mPaymentUiService.getSelectedPaymentApp();
if (selectedPaymentMethod != null
&& selectedPaymentMethod.getPaymentAppType() == PaymentAppType.AUTOFILL
&& !selectedPaymentMethod.getIdentifier().isEmpty()) {
// If the payment app was an Autofill credit card with an identifier, record its use.
PaymentApp selectedPaymentApp = mPaymentUiService.getSelectedPaymentApp();
if (selectedPaymentApp != null
&& selectedPaymentApp.getPaymentAppType() == PaymentAppType.AUTOFILL
&& !selectedPaymentApp.getIdentifier().isEmpty()) {
PersonalDataManager.getInstance().recordAndLogCreditCardUse(
selectedPaymentMethod.getIdentifier());
selectedPaymentApp.getIdentifier());
}
// Showing the app selector UI if we were previously skipping it so the loading
......
......@@ -338,6 +338,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/** @return The payment apps. */
public List<PaymentApp> getPaymentApps() {
List<PaymentApp> paymentApps = new ArrayList<>();
if (mPaymentMethodsSection == null) return paymentApps;
for (EditableOption each : mPaymentMethodsSection.getItems()) {
paymentApps.add((PaymentApp) each);
}
......@@ -349,8 +350,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
* @return The selected payment app or null if none selected.
*/
@Nullable
public EditableOption getSelectedPaymentApp() {
return mPaymentMethodsSection.getSelectedItem();
public PaymentApp getSelectedPaymentApp() {
return mPaymentMethodsSection == null
? null
: (PaymentApp) mPaymentMethodsSection.getSelectedItem();
}
/**
......@@ -472,9 +475,9 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// Do not show the Payment Request UI dialog even if the minimal UI is suppressed.
mPaymentUisShowStateReconciler.onBottomSheetShown();
mMinimalUi = new MinimalUICoordinator();
PaymentApp selectedApp = getSelectedPaymentApp();
return mMinimalUi.show(chromeActivity,
BottomSheetControllerProvider.from(chromeActivity.getWindowAndroid()),
(PaymentApp) mPaymentMethodsSection.getSelectedItem(),
BottomSheetControllerProvider.from(chromeActivity.getWindowAndroid()), selectedApp,
mCurrencyFormatterMap.get(mRawTotal.amount.currency), mUiShoppingCart.getTotal(),
readyObserver, confirmObserver, dismissObserver);
}
......@@ -491,10 +494,12 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
MinimalUICoordinator.ErrorAndCloseObserver onMinimalUiErroredAndClosed,
Runnable onUiCompleted) {
// Update records of the used payment app for sorting payment apps next time.
EditableOption selectedPaymentMethod = mPaymentMethodsSection.getSelectedItem();
PaymentPreferencesUtil.increasePaymentAppUseCount(selectedPaymentMethod.getIdentifier());
PaymentApp paymentApp = getSelectedPaymentApp();
assert paymentApp != null;
String selectedPaymentMethod = paymentApp.getIdentifier();
PaymentPreferencesUtil.increasePaymentAppUseCount(selectedPaymentMethod);
PaymentPreferencesUtil.setPaymentAppLastUseDate(
selectedPaymentMethod.getIdentifier(), System.currentTimeMillis());
selectedPaymentMethod, System.currentTimeMillis());
if (mMinimalUi != null) {
mMinimalUi.onPaymentRequestComplete(result,
......@@ -642,7 +647,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
public void onRetry(PaymentValidationErrors errors) {
// Remove all payment apps except the selected one.
assert mPaymentMethodsSection != null;
PaymentApp selectedApp = (PaymentApp) mPaymentMethodsSection.getSelectedItem();
PaymentApp selectedApp = getSelectedPaymentApp();
assert selectedApp != null;
mPaymentMethodsSection = new SectionInformation(PaymentRequestUI.DataType.PAYMENT_METHODS,
/* selection = */ 0, new ArrayList<>(Arrays.asList(selectedApp)));
......@@ -698,9 +703,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/** @return The selected payment app type. */
public @PaymentAppType int getSelectedPaymentAppType() {
return mPaymentMethodsSection != null && mPaymentMethodsSection.getSelectedItem() != null
? ((PaymentApp) mPaymentMethodsSection.getSelectedItem()).getPaymentAppType()
: PaymentAppType.UNDEFINED;
PaymentApp paymentApp = getSelectedPaymentApp();
return paymentApp == null ? PaymentAppType.UNDEFINED : paymentApp.getPaymentAppType();
}
/** Sets the modifier for the order summary based on the given app, if any. */
......@@ -758,7 +762,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
.format(modifier.total.amount.value));
}
updateOrderSummary((PaymentApp) mPaymentMethodsSection.getSelectedItem());
updateOrderSummary(getSelectedPaymentApp());
}
/**
......@@ -927,18 +931,14 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
public boolean shouldShowShippingSection() {
if (mParams.hasClosed() || !mParams.getPaymentOptions().requestShipping) return false;
if (mPaymentMethodsSection == null) return true;
PaymentApp selectedApp = (PaymentApp) mPaymentMethodsSection.getSelectedItem();
PaymentApp selectedApp = getSelectedPaymentApp();
return selectedApp == null || !selectedApp.handlesShippingAddress();
}
// Implements PaymentUiService.Delegate:
@Override
public boolean shouldShowContactSection() {
PaymentApp selectedApp = (mPaymentMethodsSection == null)
? null
: (PaymentApp) mPaymentMethodsSection.getSelectedItem();
PaymentApp selectedApp = getSelectedPaymentApp();
if (mParams.hasClosed()) return false;
PaymentOptions options = mParams.getPaymentOptions();
if (options.requestPayerName && (selectedApp == null || !selectedApp.handlesPayerName())) {
......@@ -1635,9 +1635,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
mPaymentUisShowStateReconciler.onPaymentRequestUiClosed();
}
if (mPaymentMethodsSection != null) {
for (int i = 0; i < mPaymentMethodsSection.getSize(); i++) {
EditableOption option = mPaymentMethodsSection.getItem(i);
((PaymentApp) option).dismissInstrument();
for (PaymentApp app : getPaymentApps()) {
app.dismissInstrument();
}
mPaymentMethodsSection = null;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment