diff --git a/example/src/main/java/com/stripe/example/activity/CustomerSessionActivity.java b/example/src/main/java/com/stripe/example/activity/CustomerSessionActivity.java index dbe37092866..56fa97315ca 100644 --- a/example/src/main/java/com/stripe/example/activity/CustomerSessionActivity.java +++ b/example/src/main/java/com/stripe/example/activity/CustomerSessionActivity.java @@ -50,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onStringResponse(String string) { if (string.startsWith("Error: ")) { - mErrorDialogHandler.showError(string); + mErrorDialogHandler.show(string); } } })); @@ -68,7 +68,7 @@ public void onCustomerRetrieved(@NonNull Customer customer) { public void onError(int httpCode, @Nullable String errorMessage, @Nullable StripeError stripeError) { mSelectSourceButton.setEnabled(false); - mErrorDialogHandler.showError(errorMessage); + mErrorDialogHandler.show(errorMessage); mProgressBar.setVisibility(View.INVISIBLE); } }); diff --git a/example/src/main/java/com/stripe/example/activity/PaymentIntentActivity.java b/example/src/main/java/com/stripe/example/activity/PaymentIntentActivity.java index 2c68c19105b..4e3ae47e33b 100644 --- a/example/src/main/java/com/stripe/example/activity/PaymentIntentActivity.java +++ b/example/src/main/java/com/stripe/example/activity/PaymentIntentActivity.java @@ -45,9 +45,11 @@ public class PaymentIntentActivity extends AppCompatActivity { private static final String TAG = PaymentIntentActivity.class.getName(); private static final String RETURN_URL = "stripe://payment_intent_return"; + + @NonNull private final CompositeSubscription mCompositeSubscription = + new CompositeSubscription(); private ProgressDialogController mProgressDialogController; private ErrorDialogHandler mErrorDialogHandler; - private CompositeSubscription mCompositeSubscription; private Stripe mStripe; private StripeService mStripeService; private String mClientSecret; @@ -60,16 +62,15 @@ public class PaymentIntentActivity extends AppCompatActivity { protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment_intent_demo); - Button createPaymentIntent = findViewById(R.id.btn_create_payment_intent); + final Button createPaymentIntent = findViewById(R.id.btn_create_payment_intent); mRetrievePaymentIntent = findViewById(R.id.btn_retrieve_payment_intent); mConfirmPaymentIntent = findViewById(R.id.btn_confirm_payment_intent); mPaymentIntentValue = findViewById(R.id.payment_intent_value); mCardInputWidget = findViewById(R.id.card_input_widget); - mProgressDialogController = - new ProgressDialogController(getSupportFragmentManager()); + mProgressDialogController = new ProgressDialogController(getSupportFragmentManager(), + getResources()); mErrorDialogHandler = new ErrorDialogHandler(getSupportFragmentManager()); - mCompositeSubscription = new CompositeSubscription(); mStripe = new Stripe(getApplicationContext()); Retrofit retrofit = RetrofitFactory.getInstance(); mStripeService = retrofit.create(StripeService.class); @@ -98,6 +99,13 @@ public void onClick(View v) { }); } + @Override + protected void onDestroy() { + mCompositeSubscription.unsubscribe(); + super.onDestroy(); + } + + @NonNull private Map createPaymentIntentParams() { final Map params = new HashMap<>(); params.put("allowed_source_types[]", "card"); @@ -113,14 +121,13 @@ void createPaymentIntent() { .doOnSubscribe(new Action0() { @Override public void call() { - mProgressDialogController.setMessageResource(R.string.creating_payment_intent); - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.creating_payment_intent); } }) .doOnUnsubscribe(new Action0() { @Override public void call() { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } }) .subscribe( @@ -143,7 +150,7 @@ public void call(ResponseBody responseBody) { new Action1() { @Override public void call(Throwable throwable) { - mErrorDialogHandler.showError(throwable.getLocalizedMessage()); + mErrorDialogHandler.show(throwable.getLocalizedMessage()); } } ); @@ -165,14 +172,13 @@ public PaymentIntent call() throws Exception { .doOnSubscribe(new Action0() { @Override public void call() { - mProgressDialogController.setMessageResource(R.string.retrieving_payment_intent); - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.retrieving_payment_intent); } }) .doOnUnsubscribe(new Action0() { @Override public void call() { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } }) .observeOn(AndroidSchedulers.mainThread()).subscribe( @@ -216,14 +222,13 @@ public PaymentIntent call() throws Exception { .doOnSubscribe(new Action0() { @Override public void call() { - mProgressDialogController.setMessageResource(R.string.confirming_payment_intent); - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.confirming_payment_intent); } }) .doOnUnsubscribe(new Action0() { @Override public void call() { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } }) .subscribe( diff --git a/example/src/main/java/com/stripe/example/activity/PaymentMultilineActivity.java b/example/src/main/java/com/stripe/example/activity/PaymentMultilineActivity.java index db7b792c19d..837bbd11dc2 100644 --- a/example/src/main/java/com/stripe/example/activity/PaymentMultilineActivity.java +++ b/example/src/main/java/com/stripe/example/activity/PaymentMultilineActivity.java @@ -1,6 +1,7 @@ package com.stripe.example.activity; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; @@ -37,7 +38,9 @@ public class PaymentMultilineActivity extends AppCompatActivity { ErrorDialogHandler mErrorDialogHandler; CardMultilineWidget mCardMultilineWidget; - CompositeSubscription mCompositeSubscription; + + @NonNull private final CompositeSubscription mCompositeSubscription = + new CompositeSubscription(); private SimpleAdapter mSimpleAdapter; private List> mCardSources= new ArrayList<>(); @@ -47,11 +50,10 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment_multiline); - mCompositeSubscription = new CompositeSubscription(); mCardMultilineWidget = findViewById(R.id.card_multiline_widget); - mProgressDialogController = - new ProgressDialogController(getSupportFragmentManager()); + mProgressDialogController = new ProgressDialogController(getSupportFragmentManager(), + getResources()); mErrorDialogHandler = new ErrorDialogHandler(getSupportFragmentManager()); @@ -100,14 +102,14 @@ public Source call() throws Exception { new Action0() { @Override public void call() { - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.progressMessage); } }) .doOnUnsubscribe( new Action0() { @Override public void call() { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } }) .subscribe( @@ -120,7 +122,7 @@ public void call(Source source) { new Action1() { @Override public void call(Throwable throwable) { - mErrorDialogHandler.showError(throwable.getLocalizedMessage()); + mErrorDialogHandler.show(throwable.getLocalizedMessage()); } })); } @@ -139,4 +141,9 @@ private void addToList(@Nullable Source source) { mSimpleAdapter.notifyDataSetChanged(); } + @Override + protected void onDestroy() { + mCompositeSubscription.unsubscribe(); + super.onDestroy(); + } } diff --git a/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.java b/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.java index 9a981a001b3..d8ef067b2e7 100644 --- a/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.java +++ b/example/src/main/java/com/stripe/example/activity/PaymentSessionActivity.java @@ -123,7 +123,7 @@ private void setupCustomerSession() { @Override public void onStringResponse(String string) { if (string.startsWith("Error: ")) { - mErrorDialogHandler.showError(string); + mErrorDialogHandler.show(string); } } })); @@ -142,7 +142,7 @@ public void onError(int httpCode, @Nullable String errorMessage, mCustomer = null; mSelectPaymentButton.setEnabled(false); mSelectShippingButton.setEnabled(false); - mErrorDialogHandler.showError(errorMessage); + mErrorDialogHandler.show(errorMessage); mProgressBar.setVisibility(View.INVISIBLE); } }); @@ -287,7 +287,7 @@ public void onError(int errorCode, @Nullable String errorMessage) { return; } - activity.mErrorDialogHandler.showError(errorMessage); + activity.mErrorDialogHandler.show(errorMessage); } @Override diff --git a/example/src/main/java/com/stripe/example/activity/RedirectActivity.java b/example/src/main/java/com/stripe/example/activity/RedirectActivity.java index 3f1bc4ae32e..2c7112e5c38 100644 --- a/example/src/main/java/com/stripe/example/activity/RedirectActivity.java +++ b/example/src/main/java/com/stripe/example/activity/RedirectActivity.java @@ -44,8 +44,10 @@ public class RedirectActivity extends AppCompatActivity { private static final String QUERY_CLIENT_SECRET = "client_secret"; private static final String QUERY_SOURCE_ID = "source"; + @NonNull private final CompositeSubscription mCompositeSubscription = + new CompositeSubscription(); + private CardInputWidget mCardInputWidget; - private CompositeSubscription mCompositeSubscription; private RedirectAdapter mRedirectAdapter; private ErrorDialogHandler mErrorDialogHandler; private RedirectDialogController mRedirectDialogController; @@ -58,10 +60,10 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_polling); - mCompositeSubscription = new CompositeSubscription(); mCardInputWidget = findViewById(R.id.card_widget_three_d); mErrorDialogHandler = new ErrorDialogHandler(this.getSupportFragmentManager()); - mProgressDialogController = new ProgressDialogController(this.getSupportFragmentManager()); + mProgressDialogController = new ProgressDialogController(this.getSupportFragmentManager(), + getResources()); mRedirectDialogController = new RedirectDialogController(this); mStripe = new Stripe(getApplicationContext()); @@ -113,8 +115,8 @@ protected void onNewIntent(Intent intent) { @Override protected void onDestroy() { - super.onDestroy(); mCompositeSubscription.unsubscribe(); + super.onDestroy(); } void beginSequence() { @@ -149,8 +151,7 @@ public Source call() throws Exception { .doOnSubscribe(new Action0() { @Override public void call() { - mProgressDialogController.setMessageResource(R.string.createSource); - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.createSource); } }) .subscribe( @@ -176,7 +177,7 @@ public void call(Source source) { // The card Source can be used to create a 3DS Source createThreeDSecureSource(source.getId()); } else { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } } @@ -184,7 +185,7 @@ public void call(Source source) { new Action1() { @Override public void call(Throwable throwable) { - mErrorDialogHandler.showError(throwable.getMessage()); + mErrorDialogHandler.show(throwable.getMessage()); } } )); @@ -221,7 +222,7 @@ public Source call() throws Exception { .doOnUnsubscribe(new Action0() { @Override public void call() { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } }) .subscribe( @@ -238,7 +239,7 @@ public void call(Source source) { new Action1() { @Override public void call(Throwable throwable) { - mErrorDialogHandler.showError(throwable.getMessage()); + mErrorDialogHandler.show(throwable.getMessage()); } } )); diff --git a/example/src/main/java/com/stripe/example/controller/AsyncTaskTokenController.java b/example/src/main/java/com/stripe/example/controller/AsyncTaskTokenController.java index 5f803c60850..02ba58f1653 100644 --- a/example/src/main/java/com/stripe/example/controller/AsyncTaskTokenController.java +++ b/example/src/main/java/com/stripe/example/controller/AsyncTaskTokenController.java @@ -12,6 +12,7 @@ import com.stripe.android.model.Card; import com.stripe.android.model.Token; import com.stripe.android.view.CardInputWidget; +import com.stripe.example.R; /** * Logic needed to create tokens using the {@link android.os.AsyncTask} methods included in the @@ -21,7 +22,6 @@ public class AsyncTaskTokenController { @NonNull private final Stripe mStripe; @NonNull private final ErrorDialogHandler mErrorDialogHandler; - @NonNull private final ListViewController mOutputListController; @NonNull private final ProgressDialogController mProgressDialogController; @Nullable private CardInputWidget mCardInputWidget; @@ -30,19 +30,22 @@ public AsyncTaskTokenController( @NonNull Button button, @NonNull CardInputWidget cardInputWidget, @NonNull Context context, - @NonNull ErrorDialogHandler errorDialogHandler, - @NonNull ListViewController outputListController, - @NonNull ProgressDialogController progressDialogController) { + @NonNull final ErrorDialogHandler errorDialogHandler, + @NonNull final ListViewController outputListController, + @NonNull final ProgressDialogController progressDialogController) { mCardInputWidget = cardInputWidget; mStripe = new Stripe(context); mErrorDialogHandler = errorDialogHandler; mProgressDialogController = progressDialogController; - mOutputListController = outputListController; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - saveCard(); + saveCard(new TokenCallbackImpl( + errorDialogHandler, + outputListController, + progressDialogController + )); } }); } @@ -51,25 +54,39 @@ public void detach() { mCardInputWidget = null; } - private void saveCard() { - final Card cardToSave = mCardInputWidget.getCard(); + private void saveCard(@NonNull TokenCallback tokenCallback) { + final Card cardToSave = mCardInputWidget != null ? mCardInputWidget.getCard() : null; if (cardToSave == null) { - mErrorDialogHandler.showError("Invalid Card Data"); + mErrorDialogHandler.show("Invalid Card Data"); return; } - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.progressMessage); mStripe.createToken( cardToSave, PaymentConfiguration.getInstance().getPublishableKey(), - new TokenCallback() { - public void onSuccess(@NonNull Token token) { - mOutputListController.addToList(token); - mProgressDialogController.finishProgress(); - } - public void onError(@NonNull Exception error) { - mErrorDialogHandler.showError(error.getLocalizedMessage()); - mProgressDialogController.finishProgress(); - } - }); + tokenCallback); + } + + private static class TokenCallbackImpl implements TokenCallback { + @NonNull private final ErrorDialogHandler mErrorDialogHandler; + @NonNull private final ListViewController mOutputListController; + @NonNull private final ProgressDialogController mProgressDialogController; + + private TokenCallbackImpl(@NonNull ErrorDialogHandler errorDialogHandler, + @NonNull ListViewController outputListController, + @NonNull ProgressDialogController progressDialogController) { + this.mErrorDialogHandler = errorDialogHandler; + this.mOutputListController = outputListController; + this.mProgressDialogController = progressDialogController; + } + + public void onSuccess(@NonNull Token token) { + mOutputListController.addToList(token); + mProgressDialogController.dismiss(); + } + public void onError(@NonNull Exception error) { + mErrorDialogHandler.show(error.getLocalizedMessage()); + mProgressDialogController.dismiss(); + } } } diff --git a/example/src/main/java/com/stripe/example/controller/ErrorDialogHandler.java b/example/src/main/java/com/stripe/example/controller/ErrorDialogHandler.java index 5f74af7c49e..5bb7ed2070a 100644 --- a/example/src/main/java/com/stripe/example/controller/ErrorDialogHandler.java +++ b/example/src/main/java/com/stripe/example/controller/ErrorDialogHandler.java @@ -1,7 +1,6 @@ package com.stripe.example.controller; import android.support.annotation.NonNull; -import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import com.stripe.example.R; @@ -18,9 +17,8 @@ public ErrorDialogHandler(@NonNull FragmentManager fragmentManager) { mFragmentManager = fragmentManager; } - public void showError(@NonNull String errorMessage) { - DialogFragment fragment = ErrorDialogFragment.newInstance( - R.string.validationErrors, errorMessage); - fragment.show(mFragmentManager, "error"); + public void show(@NonNull String errorMessage) { + ErrorDialogFragment.newInstance(R.string.validationErrors, errorMessage) + .show(mFragmentManager, "error"); } } diff --git a/example/src/main/java/com/stripe/example/controller/IntentServiceTokenController.java b/example/src/main/java/com/stripe/example/controller/IntentServiceTokenController.java index d228242896b..6a795d0fbdf 100644 --- a/example/src/main/java/com/stripe/example/controller/IntentServiceTokenController.java +++ b/example/src/main/java/com/stripe/example/controller/IntentServiceTokenController.java @@ -13,6 +13,7 @@ import com.stripe.android.model.Card; import com.stripe.android.view.CardInputWidget; +import com.stripe.example.R; import com.stripe.example.service.TokenIntentService; /** @@ -30,14 +31,14 @@ public class IntentServiceTokenController { @Nullable private TokenBroadcastReceiver mTokenBroadcastReceiver; public IntentServiceTokenController ( - @NonNull Activity appCompatActivity, + @NonNull Activity activity, @NonNull Button button, @NonNull CardInputWidget cardInputWidget, @NonNull ErrorDialogHandler errorDialogHandler, @NonNull ListViewController outputListController, @NonNull ProgressDialogController progressDialogController) { - mActivity = appCompatActivity; + mActivity = activity; mCardInputWidget = cardInputWidget; mErrorDialogHandler = errorDialogHandler; mOutputListViewController = outputListController; @@ -75,7 +76,7 @@ private void registerBroadcastReceiver() { private void saveCard() { final Card cardToSave = mCardInputWidget.getCard(); if (cardToSave == null) { - mErrorDialogHandler.showError("Invalid Card Data"); + mErrorDialogHandler.show("Invalid Card Data"); return; } final Intent tokenServiceIntent = TokenIntentService.createTokenIntent( @@ -84,7 +85,7 @@ private void saveCard() { cardToSave.getExpMonth(), cardToSave.getExpYear(), cardToSave.getCVC()); - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.progressMessage); mActivity.startService(tokenServiceIntent); } @@ -95,14 +96,14 @@ private TokenBroadcastReceiver() { } @Override public void onReceive(Context context, Intent intent) { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); if (intent == null) { return; } if (intent.hasExtra(TokenIntentService.STRIPE_ERROR_MESSAGE)) { - mErrorDialogHandler.showError( + mErrorDialogHandler.show( intent.getStringExtra(TokenIntentService.STRIPE_ERROR_MESSAGE)); return; } diff --git a/example/src/main/java/com/stripe/example/controller/ListViewController.java b/example/src/main/java/com/stripe/example/controller/ListViewController.java index 9a668ea8b52..136b2e5c127 100644 --- a/example/src/main/java/com/stripe/example/controller/ListViewController.java +++ b/example/src/main/java/com/stripe/example/controller/ListViewController.java @@ -39,7 +39,7 @@ void addToList(@NonNull Token token) { addToList(token.getCard().getLast4(), token.getId()); } - public void addToList(@NonNull String last4, @NonNull String tokenId) { + void addToList(@NonNull String last4, @NonNull String tokenId) { final Map map = new HashMap<>(); map.put("last4", mResources.getString(R.string.endingIn) + " " + last4); map.put("tokenId", tokenId); diff --git a/example/src/main/java/com/stripe/example/controller/ProgressDialogController.java b/example/src/main/java/com/stripe/example/controller/ProgressDialogController.java index 97bf205d144..f8d1376c7af 100644 --- a/example/src/main/java/com/stripe/example/controller/ProgressDialogController.java +++ b/example/src/main/java/com/stripe/example/controller/ProgressDialogController.java @@ -1,6 +1,8 @@ package com.stripe.example.controller; +import android.content.res.Resources; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.v4.app.FragmentManager; @@ -12,27 +14,28 @@ */ public class ProgressDialogController { + @NonNull private final Resources mRes; @NonNull private final FragmentManager mFragmentManager; - private ProgressDialogFragment mProgressFragment; + @Nullable private ProgressDialogFragment mProgressFragment; - public ProgressDialogController(@NonNull FragmentManager fragmentManager) { + public ProgressDialogController(@NonNull FragmentManager fragmentManager, + @NonNull Resources res) { mFragmentManager = fragmentManager; - mProgressFragment = ProgressDialogFragment.newInstance(R.string.progressMessage); + mRes = res; } - public void setMessageResource(@StringRes int resId) { - if (mProgressFragment.isVisible()) { + public void show(@StringRes int resId) { + if (mProgressFragment != null && mProgressFragment.isVisible()) { mProgressFragment.dismiss(); mProgressFragment = null; } - mProgressFragment = ProgressDialogFragment.newInstance(resId); - } - - public void startProgress() { + mProgressFragment = ProgressDialogFragment.newInstance(mRes.getString(resId)); mProgressFragment.show(mFragmentManager, "progress"); } - public void finishProgress() { - mProgressFragment.dismiss(); + public void dismiss() { + if (mProgressFragment != null) { + mProgressFragment.dismiss(); + } } } diff --git a/example/src/main/java/com/stripe/example/controller/RxTokenController.java b/example/src/main/java/com/stripe/example/controller/RxTokenController.java index a39c8b7a468..a7d07370e89 100644 --- a/example/src/main/java/com/stripe/example/controller/RxTokenController.java +++ b/example/src/main/java/com/stripe/example/controller/RxTokenController.java @@ -11,6 +11,7 @@ import com.stripe.android.model.Card; import com.stripe.android.model.Token; import com.stripe.android.view.CardInputWidget; +import com.stripe.example.R; import java.util.concurrent.Callable; @@ -71,7 +72,7 @@ public void detach() { private void saveCard() { final Card cardToSave = mCardInputWidget.getCard(); if (cardToSave == null) { - mErrorDialogHandler.showError("Invalid Card Data"); + mErrorDialogHandler.show("Invalid Card Data"); return; } final Stripe stripe = new Stripe(mContext); @@ -95,14 +96,14 @@ public Token call() throws Exception { new Action0() { @Override public void call() { - mProgressDialogController.startProgress(); + mProgressDialogController.show(R.string.progressMessage); } }) .doOnUnsubscribe( new Action0() { @Override public void call() { - mProgressDialogController.finishProgress(); + mProgressDialogController.dismiss(); } }) .subscribe( @@ -115,7 +116,7 @@ public void call(Token token) { new Action1() { @Override public void call(Throwable throwable) { - mErrorDialogHandler.showError(throwable.getLocalizedMessage()); + mErrorDialogHandler.show(throwable.getLocalizedMessage()); } })); } diff --git a/example/src/main/java/com/stripe/example/dialog/ProgressDialogFragment.java b/example/src/main/java/com/stripe/example/dialog/ProgressDialogFragment.java index 869cec40ef7..3119b9e5f9e 100644 --- a/example/src/main/java/com/stripe/example/dialog/ProgressDialogFragment.java +++ b/example/src/main/java/com/stripe/example/dialog/ProgressDialogFragment.java @@ -3,14 +3,17 @@ import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; public class ProgressDialogFragment extends DialogFragment { - public static ProgressDialogFragment newInstance(int msgId) { + @NonNull + public static ProgressDialogFragment newInstance(@NonNull String message) { ProgressDialogFragment fragment = new ProgressDialogFragment(); Bundle args = new Bundle(); - args.putInt("msgId", msgId); + args.putString("message", message); fragment.setArguments(args); @@ -21,11 +24,16 @@ public ProgressDialogFragment() { // Empty constructor required for DialogFragment } + @NonNull @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - int msgId = getArguments().getInt("msgId"); - ProgressDialog dialog = new ProgressDialog(getActivity()); - dialog.setMessage(getActivity().getResources().getString(msgId)); + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + final ProgressDialog dialog = new ProgressDialog(getActivity()); + dialog.setMessage(getMessage()); return dialog; } + + @Nullable + private String getMessage() { + return getArguments() != null ? getArguments().getString("message") : null; + } } diff --git a/example/src/main/java/com/stripe/example/module/DependencyHandler.java b/example/src/main/java/com/stripe/example/module/DependencyHandler.java index 951030d7aed..5e880ffdb90 100644 --- a/example/src/main/java/com/stripe/example/module/DependencyHandler.java +++ b/example/src/main/java/com/stripe/example/module/DependencyHandler.java @@ -26,7 +26,7 @@ public class DependencyHandler { @Nullable private AsyncTaskTokenController mAsyncTaskController; @NonNull private final CardInputWidget mCardInputWidget; @NonNull private final Context mContext; - @NonNull private final ProgressDialogController mProgresDialogController; + @NonNull private final ProgressDialogController mProgressDialogController; @NonNull private final ErrorDialogHandler mErrorDialogHandler; @Nullable private IntentServiceTokenController mIntentServiceTokenController; @NonNull private final ListViewController mListViewController; @@ -40,8 +40,10 @@ public DependencyHandler( mCardInputWidget = cardInputWidget; mContext = activity.getApplicationContext(); - mProgresDialogController = - new ProgressDialogController(activity.getSupportFragmentManager()); + mProgressDialogController = new ProgressDialogController( + activity.getSupportFragmentManager(), + activity.getResources() + ); mListViewController = new ListViewController(outputListView); @@ -64,7 +66,7 @@ public AsyncTaskTokenController attachAsyncTaskTokenController(@NonNull Button b mContext, mErrorDialogHandler, mListViewController, - mProgresDialogController); + mProgressDialogController); } return mAsyncTaskController; } @@ -89,7 +91,7 @@ public IntentServiceTokenController attachIntentServiceTokenController( mCardInputWidget, mErrorDialogHandler, mListViewController, - mProgresDialogController); + mProgressDialogController); } return mIntentServiceTokenController; } @@ -112,7 +114,7 @@ public RxTokenController attachRxTokenController(Button button) { mContext, mErrorDialogHandler, mListViewController, - mProgresDialogController); + mProgressDialogController); } return mRxTokenController; } diff --git a/samplestore/src/main/java/com/stripe/samplestore/PaymentActivity.java b/samplestore/src/main/java/com/stripe/samplestore/PaymentActivity.java index db3507f7e89..a07533afc31 100644 --- a/samplestore/src/main/java/com/stripe/samplestore/PaymentActivity.java +++ b/samplestore/src/main/java/com/stripe/samplestore/PaymentActivity.java @@ -61,8 +61,10 @@ public class PaymentActivity extends AppCompatActivity { private static final String TOTAL_LABEL = "Total:"; private static final String SHIPPING = "Shipping"; + @NonNull private final CompositeSubscription mCompositeSubscription = + new CompositeSubscription(); + private BroadcastReceiver mBroadcastReceiver; - private CompositeSubscription mCompositeSubscription; private ProgressDialogFragment mProgressDialogFragment; private LinearLayout mCartItemLayout; @@ -93,10 +95,9 @@ protected void onCreate(Bundle savedInstanceState) { mCartItemLayout = findViewById(R.id.cart_list_items); addCartItems(); - mCompositeSubscription = new CompositeSubscription(); - mProgressDialogFragment = - ProgressDialogFragment.newInstance(R.string.completing_purchase); + mProgressDialogFragment = ProgressDialogFragment + .newInstance(getString(R.string.completing_purchase)); mConfirmPaymentButton = findViewById(R.id.btn_purchase); updateConfirmPaymentButton(); @@ -165,10 +166,7 @@ private boolean isShippingInfoValid(@NonNull ShippingInformation shippingInfo) { @Override protected void onDestroy() { super.onDestroy(); - if (mCompositeSubscription != null) { - mCompositeSubscription.unsubscribe(); - mCompositeSubscription = null; - } + mCompositeSubscription.unsubscribe(); LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver); mPaymentSession.onDestroy(); } diff --git a/samplestore/src/main/java/com/stripe/samplestore/ProgressDialogFragment.java b/samplestore/src/main/java/com/stripe/samplestore/ProgressDialogFragment.java index 59639731f45..5aec746f4bf 100644 --- a/samplestore/src/main/java/com/stripe/samplestore/ProgressDialogFragment.java +++ b/samplestore/src/main/java/com/stripe/samplestore/ProgressDialogFragment.java @@ -3,15 +3,18 @@ import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; public class ProgressDialogFragment extends DialogFragment { - public static ProgressDialogFragment newInstance(int msgId) { + @NonNull + public static ProgressDialogFragment newInstance(@NonNull String message) { ProgressDialogFragment fragment = new ProgressDialogFragment(); - Bundle args = new Bundle(); - args.putInt("msgId", msgId); + final Bundle args = new Bundle(); + args.putString("message", message); fragment.setArguments(args); @@ -22,11 +25,16 @@ public ProgressDialogFragment() { // Empty constructor required for DialogFragment } + @NonNull @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - int msgId = getArguments().getInt("msgId"); - ProgressDialog dialog = new ProgressDialog(getActivity()); - dialog.setMessage(getActivity().getResources().getString(msgId)); + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + final ProgressDialog dialog = new ProgressDialog(getActivity()); + dialog.setMessage(getMessage()); return dialog; } + + @Nullable + private String getMessage() { + return getArguments() != null ? getArguments().getString("message") : null; + } } diff --git a/samplestore/src/main/java/com/stripe/samplestore/StoreActivity.java b/samplestore/src/main/java/com/stripe/samplestore/StoreActivity.java index 54054f20659..9820c61fcb4 100644 --- a/samplestore/src/main/java/com/stripe/samplestore/StoreActivity.java +++ b/samplestore/src/main/java/com/stripe/samplestore/StoreActivity.java @@ -2,6 +2,7 @@ import android.content.Intent; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; @@ -116,9 +117,11 @@ private void setupCustomerSession() { new SampleStoreEphemeralKeyProvider( new SampleStoreEphemeralKeyProvider.ProgressListener() { @Override - public void onStringResponse(String string) { + public void onStringResponse(@NonNull String string) { if (string.startsWith("Error: ")) { - new AlertDialog.Builder(StoreActivity.this).setMessage(string).show(); + new AlertDialog.Builder(StoreActivity.this) + .setMessage(string) + .show(); } } })); diff --git a/samplestore/src/main/java/com/stripe/samplestore/service/SampleStoreEphemeralKeyProvider.java b/samplestore/src/main/java/com/stripe/samplestore/service/SampleStoreEphemeralKeyProvider.java index 7918e931578..6d7bc455756 100644 --- a/samplestore/src/main/java/com/stripe/samplestore/service/SampleStoreEphemeralKeyProvider.java +++ b/samplestore/src/main/java/com/stripe/samplestore/service/SampleStoreEphemeralKeyProvider.java @@ -19,13 +19,12 @@ import rx.subscriptions.CompositeSubscription; public class SampleStoreEphemeralKeyProvider implements EphemeralKeyProvider { - private @NonNull - CompositeSubscription mCompositeSubscription; - private @NonNull StripeService mStripeService; - private @NonNull ProgressListener mProgressListener; + @NonNull private final CompositeSubscription mCompositeSubscription; + @NonNull private final StripeService mStripeService; + @NonNull private final ProgressListener mProgressListener; public SampleStoreEphemeralKeyProvider(@NonNull ProgressListener progressListener) { - Retrofit retrofit = RetrofitFactory.getInstance(); + final Retrofit retrofit = RetrofitFactory.getInstance(); mStripeService = retrofit.create(StripeService.class); mCompositeSubscription = new CompositeSubscription(); mProgressListener = progressListener; @@ -34,7 +33,7 @@ public SampleStoreEphemeralKeyProvider(@NonNull ProgressListener progressListene @Override public void createEphemeralKey(@NonNull @Size(min = 4) String apiVersion, @NonNull final EphemeralKeyUpdateListener keyUpdateListener) { - Map apiParamMap = new HashMap<>(); + final Map apiParamMap = new HashMap<>(); apiParamMap.put("api_version", apiVersion); mCompositeSubscription.add( @@ -48,8 +47,7 @@ public void call(ResponseBody response) { String rawKey = response.string(); keyUpdateListener.onKeyUpdate(rawKey); mProgressListener.onStringResponse(rawKey); - } catch (IOException iox) { - + } catch (IOException ignored) { } } }, new Action1() { @@ -61,6 +59,6 @@ public void call(Throwable throwable) { } public interface ProgressListener { - void onStringResponse(String string); + void onStringResponse(@NonNull String string); } } diff --git a/stripe/src/main/java/com/stripe/android/ActivitySourceCallback.java b/stripe/src/main/java/com/stripe/android/ActivitySourceCallback.java new file mode 100644 index 00000000000..7ab77f291c0 --- /dev/null +++ b/stripe/src/main/java/com/stripe/android/ActivitySourceCallback.java @@ -0,0 +1,24 @@ +package com.stripe.android; + +import android.app.Activity; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import java.lang.ref.WeakReference; + +/** + * Abstract implementation of {@link SourceCallback} that holds a {@link WeakReference} to + * an {@link Activity} object. + */ +public abstract class ActivitySourceCallback implements SourceCallback { + @NonNull private final WeakReference mActivityRef; + + public ActivitySourceCallback(@NonNull A activity) { + mActivityRef = new WeakReference<>(activity); + } + + @Nullable + public A getActivity() { + return mActivityRef.get(); + } +} diff --git a/stripe/src/main/java/com/stripe/android/view/AddSourceActivity.java b/stripe/src/main/java/com/stripe/android/view/AddSourceActivity.java index b4585ffa376..aeaefb72b80 100644 --- a/stripe/src/main/java/com/stripe/android/view/AddSourceActivity.java +++ b/stripe/src/main/java/com/stripe/android/view/AddSourceActivity.java @@ -13,10 +13,10 @@ import android.widget.FrameLayout; import android.widget.TextView; +import com.stripe.android.ActivitySourceCallback; import com.stripe.android.CustomerSession; import com.stripe.android.PaymentConfiguration; import com.stripe.android.R; -import com.stripe.android.SourceCallback; import com.stripe.android.Stripe; import com.stripe.android.StripeError; import com.stripe.android.model.Card; @@ -64,25 +64,6 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { } }; - @NonNull private final SourceCallback mSourceCallback = new SourceCallback() { - @Override - public void onError(@NonNull Exception error) { - setCommunicatingProgress(false); - // This error is independent of the CustomerSession, so - // we have to surface it here. - showError(error.getLocalizedMessage()); - } - - @Override - public void onSuccess(@NonNull Source source) { - if (mUpdatesCustomer) { - attachCardToCustomer(source); - } else { - finishWithSource(source); - } - } - }; - /** * Create an {@link Intent} to start a {@link AddSourceActivity}. * @@ -155,7 +136,7 @@ protected void onActionSave() { final SourceParams sourceParams = SourceParams.createCardParams(card); setCommunicatingProgress(true); - stripe.createSource(sourceParams, mSourceCallback); + stripe.createSource(sourceParams, new SourceCallbackImpl(this, mUpdatesCustomer)); } @@ -259,4 +240,41 @@ interface CustomerSessionProxy { void addCustomerSource(String sourceId, @NonNull CustomerSession.SourceRetrievalListener listener); } + + private static final class SourceCallbackImpl + extends ActivitySourceCallback { + private final boolean mUpdatesCustomer; + + private SourceCallbackImpl(@NonNull AddSourceActivity activity, + boolean updatesCustomer) { + super(activity); + mUpdatesCustomer = updatesCustomer; + } + + @Override + public void onError(@NonNull Exception error) { + final AddSourceActivity activity = getActivity(); + if (activity == null) { + return; + } + + activity.setCommunicatingProgress(false); + // This error is independent of the CustomerSession, so we have to surface it here. + activity.showError(error.getLocalizedMessage()); + } + + @Override + public void onSuccess(@NonNull Source source) { + final AddSourceActivity activity = getActivity(); + if (activity == null) { + return; + } + + if (mUpdatesCustomer) { + activity.attachCardToCustomer(source); + } else { + activity.finishWithSource(source); + } + } + } }