From d3259e8e97ae27c36f543ba19c5bf5270832acb4 Mon Sep 17 00:00:00 2001 From: Umar Hussain Date: Wed, 15 Aug 2018 17:16:35 +0500 Subject: [PATCH 1/2] Added a static method to cancel async calls without clearing the user session. This will be helpful when a view is destroyed but the network call has not ended, to avoid null pointer exceptions. --- .../java/com/stripe/android/CustomerSession.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/stripe/src/main/java/com/stripe/android/CustomerSession.java b/stripe/src/main/java/com/stripe/android/CustomerSession.java index 849e6bfdbdc..9921c4cd1bb 100644 --- a/stripe/src/main/java/com/stripe/android/CustomerSession.java +++ b/stripe/src/main/java/com/stripe/android/CustomerSession.java @@ -133,6 +133,21 @@ public static void endCustomerSession() { clearInstance(); } + /** + * End any async calls in process and will not invoke callback listeners. + * It will not clear the singleton instance of a {@link CustomerSession} so it can be + * safely used when a view is being removed/destroyed to avoid null pointer exceptions + * due to async operation delay. + * No need to call {@link CustomerSession#initCustomerSession(EphemeralKeyProvider)} again + * after this operation. + */ + public static void cancelCallbacks(){ + if (mInstance == null) { + return; + } + mInstance.mThreadPoolExecutor.shutdownNow(); + } + @VisibleForTesting static void initCustomerSession( @NonNull EphemeralKeyProvider keyProvider, From 290e45043b010cf85fdf8abb4e9d2388a9104fd2 Mon Sep 17 00:00:00 2001 From: Umar Hussain Date: Tue, 18 Dec 2018 21:49:06 +0500 Subject: [PATCH 2/2] mThreadPoolExecutor made final. clearInstance() updated to use cancelCallbacks() for code reuse. Code refactored to place clearInstance() and cancelCallbacks() together. --- .../com/stripe/android/CustomerSession.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/stripe/src/main/java/com/stripe/android/CustomerSession.java b/stripe/src/main/java/com/stripe/android/CustomerSession.java index 9921c4cd1bb..7ea04a402e5 100644 --- a/stripe/src/main/java/com/stripe/android/CustomerSession.java +++ b/stripe/src/main/java/com/stripe/android/CustomerSession.java @@ -79,7 +79,7 @@ public class CustomerSession implements EphemeralKeyManager.KeyManagerListener { // A queue of Runnables for doing customer updates private final BlockingQueue mNetworkQueue = new LinkedBlockingQueue<>(); - private @NonNull ThreadPoolExecutor mThreadPoolExecutor; + private final @NonNull ThreadPoolExecutor mThreadPoolExecutor; private static final int CUSTOMER_RETRIEVED = 7; private static final int CUSTOMER_ERROR = 11; @@ -133,21 +133,6 @@ public static void endCustomerSession() { clearInstance(); } - /** - * End any async calls in process and will not invoke callback listeners. - * It will not clear the singleton instance of a {@link CustomerSession} so it can be - * safely used when a view is being removed/destroyed to avoid null pointer exceptions - * due to async operation delay. - * No need to call {@link CustomerSession#initCustomerSession(EphemeralKeyProvider)} again - * after this operation. - */ - public static void cancelCallbacks(){ - if (mInstance == null) { - return; - } - mInstance.mThreadPoolExecutor.shutdownNow(); - } - @VisibleForTesting static void initCustomerSession( @NonNull EphemeralKeyProvider keyProvider, @@ -158,11 +143,23 @@ static void initCustomerSession( @VisibleForTesting static void clearInstance() { + cancelCallbacks(); + mInstance = null; + } + + /** + * End any async calls in process and will not invoke callback listeners. + * It will not clear the singleton instance of a {@link CustomerSession} so it can be + * safely used when a view is being removed/destroyed to avoid null pointer exceptions + * due to async operation delay. + * No need to call {@link CustomerSession#initCustomerSession(EphemeralKeyProvider)} again + * after this operation. + */ + public static void cancelCallbacks() { if (mInstance == null) { return; } mInstance.mThreadPoolExecutor.shutdownNow(); - mInstance = null; } private CustomerSession(