From ab6e3c8cfcd97bdae185b48ff2a3f12ef2a6a23b Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Thu, 16 Mar 2017 10:02:55 +0100 Subject: [PATCH] doInBackground handlers: Check for isCancelled(). Just because it is generally good practice: https://developer.android.com/reference/android/os/AsyncTask.html However, in this particular code there is no place where we can periodically check this while doing work. --- .../com/murrayc/galaxyzoo/app/ClassifyActivity.java | 13 ++++++++++++- .../com/murrayc/galaxyzoo/app/LoginActivity.java | 4 ++++ .../java/com/murrayc/galaxyzoo/app/LoginUtils.java | 12 ++++++++++++ .../com/murrayc/galaxyzoo/app/QuestionFragment.java | 4 ++++ .../java/com/murrayc/galaxyzoo/app/Singleton.java | 4 ++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyActivity.java b/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyActivity.java index afe1d8f7..5759008e 100644 --- a/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyActivity.java +++ b/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyActivity.java @@ -160,6 +160,10 @@ protected Boolean doInBackground(final Void... params) { return Boolean.FALSE; } + if (isCancelled()) { + return Boolean.FALSE; + } + return LoginUtils.getLoggedIn(context); } @@ -205,6 +209,10 @@ protected Void doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + final AccountManager mgr = AccountManager.get(context); //Note this needs the GET_ACCOUNTS permission on @@ -216,12 +224,15 @@ protected Void doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + final Account account = accts[0]; final Bundle extras = new Bundle(); extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); - //Ask the framework to run our SyncAdapter. ContentResolver.requestSync(account, Item.AUTHORITY, extras); diff --git a/app/src/main/java/com/murrayc/galaxyzoo/app/LoginActivity.java b/app/src/main/java/com/murrayc/galaxyzoo/app/LoginActivity.java index 463749f9..90961525 100644 --- a/app/src/main/java/com/murrayc/galaxyzoo/app/LoginActivity.java +++ b/app/src/main/java/com/murrayc/galaxyzoo/app/LoginActivity.java @@ -315,6 +315,10 @@ protected Void doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + final String accountName = loginResult.getName(); final AccountManager accountManager = AccountManager.get(context); diff --git a/app/src/main/java/com/murrayc/galaxyzoo/app/LoginUtils.java b/app/src/main/java/com/murrayc/galaxyzoo/app/LoginUtils.java index 3d971315..2961fbc6 100644 --- a/app/src/main/java/com/murrayc/galaxyzoo/app/LoginUtils.java +++ b/app/src/main/java/com/murrayc/galaxyzoo/app/LoginUtils.java @@ -470,6 +470,10 @@ protected LoginDetails doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + LoginDetails result = null; try { @@ -517,6 +521,10 @@ protected Void doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + final LoginUtils.LoginDetails loginDetails = LoginUtils.getAccountLoginDetails(context); if(!LoginUtils.getLoggedIn(loginDetails)) { return null; @@ -527,6 +535,10 @@ protected Void doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + LoginUtils.removeAccount(context, accountName); LoginUtils.addAnonymousAccount(context); diff --git a/app/src/main/java/com/murrayc/galaxyzoo/app/QuestionFragment.java b/app/src/main/java/com/murrayc/galaxyzoo/app/QuestionFragment.java index c71963eb..8725a888 100644 --- a/app/src/main/java/com/murrayc/galaxyzoo/app/QuestionFragment.java +++ b/app/src/main/java/com/murrayc/galaxyzoo/app/QuestionFragment.java @@ -681,6 +681,10 @@ protected Void doInBackground(final Void... params) { return null; } + if (isCancelled()) { + return null; + } + fragment.saveClassificationSync(classificationInProgress); return null; diff --git a/app/src/main/java/com/murrayc/galaxyzoo/app/Singleton.java b/app/src/main/java/com/murrayc/galaxyzoo/app/Singleton.java index fd7b2604..c5a7e26d 100644 --- a/app/src/main/java/com/murrayc/galaxyzoo/app/Singleton.java +++ b/app/src/main/java/com/murrayc/galaxyzoo/app/Singleton.java @@ -318,6 +318,10 @@ protected Void doInBackground(final Context... params) { return null; } + if (isCancelled()) { + return null; + } + final Context context = params[0]; try { Singleton.ourInstance = new Singleton(context);