diff --git a/packages/PrintSpooler/AndroidManifest.xml b/packages/PrintSpooler/AndroidManifest.xml index ed6fdb7ce4d83..2da4d6a25dd31 100644 --- a/packages/PrintSpooler/AndroidManifest.xml +++ b/packages/PrintSpooler/AndroidManifest.xml @@ -57,7 +57,7 @@ diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java index bb359176bdf1d..999d82d592ea3 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java +++ b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java @@ -510,7 +510,12 @@ public final class PageContentRepository { public void destroy() { if (mBoundToService) { mBoundToService = false; - mContext.unbindService(AsyncRenderer.this); + try { + mContext.unbindService(AsyncRenderer.this); + } catch (IllegalArgumentException e) { + // Service might have been forcefully unbound in onDestroy() + Log.e(LOG_TAG, "Cannot unbind service", e); + } } mPageContentCache.invalidate(); diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java index 74bd016898742..cc0b4ca46d481 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java @@ -268,7 +268,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat new Runnable() { @Override public void run() { - if (isFinishing()) { + if (isFinishing() || isDestroyed()) { // onPause might have not been able to cancel the job, see PrintActivity#onPause // To be sure, cancel the job again. Double canceling does no harm. mSpoolerProvider.getSpooler().setPrintJobState(mPrintJob.getId(), @@ -320,7 +320,8 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat // If we are finishing or we are in a state that we do not need any // data from the printing app, then no need to finish. - if (isFinishing() || (isFinalState(mState) && !mPrintedDocument.isUpdating())) { + if (isFinishing() || isDestroyed() || + (isFinalState(mState) && !mPrintedDocument.isUpdating())) { return; } setState(STATE_PRINT_CANCELED); @@ -617,6 +618,17 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat } } + @Override + protected void onDestroy() { + if (mPrintedDocument != null) { + mPrintedDocument.cancel(true); + } + + doFinish(); + + super.onDestroy(); + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { @@ -960,7 +972,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat } private void ensureProgressUiShown() { - if (isFinishing()) { + if (isFinishing() || isDestroyed()) { return; } if (mUiState != UI_STATE_PROGRESS) { @@ -972,7 +984,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat } private void ensurePreviewUiShown() { - if (isFinishing()) { + if (isFinishing() || isDestroyed()) { return; } if (mUiState != UI_STATE_PREVIEW) { @@ -983,7 +995,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat } private void ensureErrorUiShown(CharSequence message, int action) { - if (isFinishing()) { + if (isFinishing() || isDestroyed()) { return; } if (mUiState != UI_STATE_ERROR) { @@ -1350,7 +1362,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat @Override public void onLoaderReset(Loader> loader) { - if (!isFinishing()) { + if (!(isFinishing() || isDestroyed())) { onLoadFinished(loader, null); } } @@ -2033,7 +2045,9 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat mSpoolerProvider.destroy(); } - setState(mProgressMessageController.cancel()); + if (mProgressMessageController != null) { + setState(mProgressMessageController.cancel()); + } if (mState != STATE_INITIALIZING) { mPrintedDocument.finish();