Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ActivitySourceRetrievalListener #867

Merged
merged 1 commit into from Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions stripe/src/main/java/com/stripe/android/CustomerSession.java
@@ -1,5 +1,6 @@
package com.stripe.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
Expand All @@ -17,6 +18,7 @@
import com.stripe.android.model.ShippingInformation;
import com.stripe.android.model.Source;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -776,6 +778,24 @@ void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError);
}

/**
* Abstract implementation of {@link SourceRetrievalListener} that holds a
* {@link WeakReference} to an {@link Activity} object.
*/
public abstract static class ActivitySourceRetrievalListener<A extends Activity>
implements SourceRetrievalListener {
@NonNull private final WeakReference<A> mActivityRef;

public ActivitySourceRetrievalListener(@NonNull A activity) {
this.mActivityRef = new WeakReference<>(activity);
}

@Nullable
protected A getActivity() {
return mActivityRef.get();
}
}

private static class CustomerMessage {
@Nullable private final String operationId;
@Nullable private final Customer customer;
Expand Down
Expand Up @@ -10,7 +10,6 @@
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.stripe.android.ActivitySourceCallback;
Expand Down Expand Up @@ -42,7 +41,6 @@ public class AddSourceActivity extends StripeActivity {

@Nullable private CardMultilineWidget mCardMultilineWidget;
@Nullable private CustomerSessionProxy mCustomerSessionProxy;
private FrameLayout mErrorLayout;
@Nullable private StripeProvider mStripeProvider;

private boolean mStartedFromPaymentSession;
Expand Down Expand Up @@ -90,8 +88,7 @@ protected void onCreate(Bundle savedInstanceState) {
mViewStub.inflate();
mCardMultilineWidget = findViewById(R.id.add_source_card_entry_widget);
initEnterListeners();
mErrorLayout = findViewById(R.id.add_source_error_container);
boolean showZip = getIntent().getBooleanExtra(EXTRA_SHOW_ZIP, false);
final boolean showZip = getIntent().getBooleanExtra(EXTRA_SHOW_ZIP, false);
mUpdatesCustomer = getIntent().getBooleanExtra(EXTRA_UPDATE_CUSTOMER, false);
mStartedFromPaymentSession =
getIntent().getBooleanExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true);
Expand Down Expand Up @@ -123,7 +120,7 @@ void initCustomerSessionTokens() {

@Override
protected void onActionSave() {
final Card card = mCardMultilineWidget.getCard();
final Card card = mCardMultilineWidget != null ? mCardMultilineWidget.getCard() : null;
if (card == null) {
// In this case, the error will be displayed on the card widget itself.
return;
Expand All @@ -142,20 +139,7 @@ protected void onActionSave() {

private void attachCardToCustomer(@NonNull final StripePaymentSource source) {
final CustomerSession.SourceRetrievalListener listener =
new CustomerSession.SourceRetrievalListener() {
@Override
public void onSourceRetrieved(@NonNull Source source) {
finishWithSource(source);
}

@Override
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
// No need to show this error, because it will be broadcast
// from the CustomerSession
setCommunicatingProgress(false);
}
};
new SourceRetrievalListenerImpl(this);

if (mCustomerSessionProxy == null) {
@Source.SourceType final String sourceType;
Expand Down Expand Up @@ -277,4 +261,34 @@ public void onSuccess(@NonNull Source source) {
}
}
}

private static final class SourceRetrievalListenerImpl
extends CustomerSession.ActivitySourceRetrievalListener<AddSourceActivity> {
private SourceRetrievalListenerImpl(@NonNull AddSourceActivity activity) {
super(activity);
}

@Override
public void onSourceRetrieved(@NonNull Source source) {
final AddSourceActivity activity = getActivity();
if (activity == null) {
return;
}

activity.finishWithSource(source);
}

@Override
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
final AddSourceActivity activity = getActivity();
if (activity == null) {
return;
}

// No need to show this error, because it will be broadcast
// from the CustomerSession
activity.setCommunicatingProgress(false);
}
}
}