Skip to content

Commit

Permalink
Create ActivitySourceRetrievalListener (#867)
Browse files Browse the repository at this point in the history
Correctly handle Activity weak references in `AddSourceActivity`
  • Loading branch information
mshafrir-stripe committed Apr 12, 2019
1 parent 039f214 commit 89d0c6c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
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
52 changes: 33 additions & 19 deletions stripe/src/main/java/com/stripe/android/view/AddSourceActivity.java
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);
}
}
}

0 comments on commit 89d0c6c

Please sign in to comment.