Source:
<code>private ResultAndError<PullTaskResult> doTaskBackgroundHelper() { publishProgress(PROGRESS_STARTED); recordSyncAttemptTime(); Logger.log(AndroidLogger.TYPE_USER, "Starting Sync"); determineIfLoginNeeded(); AndroidTransactionParserFactory factory = getTransactionParserFactory(); byte[] wrappedEncryptionKey = getEncryptionKey(); if (wrappedEncryptionKey == null) { this.publishProgress(PROGRESS_DONE); return new ResultAndError<>(PullTaskResult.UNKNOWN_FAILURE, "Unable to get or generate encryption key"); } else { factory.initUserParser(wrappedEncryptionKey); } <START> if (isCancelled()) { <END> return new ResultAndError<>(PullTaskResult.UNKNOWN_FAILURE, ""); } PullTaskResult responseError = PullTaskResult.UNKNOWN_FAILURE; try { ResultAndError<PullTaskResult> resultFromRequest = makeRequestAndHandleResponse(factory); if (resultFromRequest != null) { return resultFromRequest; } } catch (SocketTimeoutException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Timed out listening to receive data during sync"); responseError = PullTaskResult.CONNECTION_TIMEOUT; } catch (ConnectTimeoutException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Timed out listening to receive data during sync"); responseError = PullTaskResult.CONNECTION_TIMEOUT; } catch (ClientProtocolException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due network error|" + e.getMessage()); } catch (UnknownHostException e) { Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due to bad network"); responseError = PullTaskResult.UNREACHABLE_HOST; } catch (IOException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due to IO Error|" + e.getMessage()); } wipeLoginIfItOccurred(); this.publishProgress(PROGRESS_DONE); return new ResultAndError<>(responseError); }</code><technical_language>I practice check for cancellation close "blocking" operation ensure user maximum amount of time 'undo' operation, moving earlier prevents option</technical_language>

Target:
private ResultAndError<PullTaskResult> doTaskBackgroundHelper() { publishProgress(PROGRESS_STARTED); recordSyncAttemptTime(); Logger.log(AndroidLogger.TYPE_USER, "Starting Sync"); determineIfLoginNeeded(); AndroidTransactionParserFactory factory = getTransactionParserFactory(); byte[] wrappedEncryptionKey = getEncryptionKey(); if (wrappedEncryptionKey == null) { this.publishProgress(PROGRESS_DONE); return new ResultAndError<>(PullTaskResult.UNKNOWN_FAILURE, "Unable to get or generate encryption key"); } factory.initUserParser(wrappedEncryptionKey); if (!loginNeeded) { CaseUtils.purgeCases(); } if (isCancelled()) { return new ResultAndError<>(PullTaskResult.UNKNOWN_FAILURE, ""); } PullTaskResult responseError = PullTaskResult.UNKNOWN_FAILURE; try { return makeRequestAndHandleResponse(factory); } catch (SocketTimeoutException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Timed out listening to receive data during sync"); responseError = PullTaskResult.CONNECTION_TIMEOUT; } catch (ConnectTimeoutException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Timed out listening to receive data during sync"); responseError = PullTaskResult.CONNECTION_TIMEOUT; } catch (ClientProtocolException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due network error|" + e.getMessage()); } catch (UnknownHostException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due to bad network"); responseError = PullTaskResult.UNREACHABLE_HOST; } catch (IOException e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due to IO Error|" + e.getMessage()); } catch (UnknownSyncError e) { e.printStackTrace(); Logger.log(AndroidLogger.TYPE_WARNING_NETWORK, "Couldn't sync due to Unknown Error|" + e.getMessage()); } wipeLoginIfItOccurred(); this.publishProgress(PROGRESS_DONE); return new ResultAndError<>(responseError); }