From 5c7d8fafa8d77cb29306791499e699a74740a2fc Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Wed, 14 Feb 2018 13:55:57 -0800 Subject: Do not transform again on crash Before: onServiceConnected AsyncTask 1: doInBackground doTransform PdfEditor service crashes system restarts PdfEditor service -> onServiceConnected AsyncTask 2: doInBackground doTransform onPostExecute Unbind from service AsyncTask 1: onPostExecute Unbind from service again -> crash Hence we have to only ever create a single async task even if we get another onServiceConnected. Test: atest CtsPrintTestCases Change-Id: Ia18e82fe6258bac9395557b2056645e87a752889 Fixes: 73127227 --- .../com/android/printspooler/ui/PrintActivity.java | 50 +++++++++++++--------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'packages/PrintSpooler/src') diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java index 44f68eca49a3..d73a5d73e5bf 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java @@ -3117,6 +3117,8 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat private final Consumer mCallback; + private boolean mIsTransformationStarted; + public DocumentTransformer(Context context, PrintJobInfo printJob, MutexFileProvider fileProvider, PrintAttributes attributes, Consumer callback) { @@ -3144,29 +3146,35 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat @Override public void onServiceConnected(ComponentName name, IBinder service) { - final IPdfEditor editor = IPdfEditor.Stub.asInterface(service); - new AsyncTask() { - @Override - protected String doInBackground(Void... params) { - // It's OK to access the data members as they are - // final and this code is the last one to touch - // them as shredding is the very last step, so the - // UI is not interactive at this point. - try { - doTransform(editor); - updatePrintJob(); - return null; - } catch (IOException | RemoteException | IllegalStateException e) { - return e.toString(); + // We might get several onServiceConnected if the service crashes and restarts. + // mIsTransformationStarted makes sure that we only try once. + if (!mIsTransformationStarted) { + final IPdfEditor editor = IPdfEditor.Stub.asInterface(service); + new AsyncTask() { + @Override + protected String doInBackground(Void... params) { + // It's OK to access the data members as they are + // final and this code is the last one to touch + // them as shredding is the very last step, so the + // UI is not interactive at this point. + try { + doTransform(editor); + updatePrintJob(); + return null; + } catch (IOException | RemoteException | IllegalStateException e) { + return e.toString(); + } } - } - @Override - protected void onPostExecute(String error) { - mContext.unbindService(DocumentTransformer.this); - mCallback.accept(error); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + @Override + protected void onPostExecute(String error) { + mContext.unbindService(DocumentTransformer.this); + mCallback.accept(error); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + mIsTransformationStarted = true; + } } @Override -- cgit v1.2.3-59-g8ed1b