Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9424b733 authored by Svetoslav's avatar Svetoslav
Browse files

Fix a print spooler crash when printing.

The spooler communicates with he remote PDF renderer asynchronously.
When print is confirmed we close the renderer, destroy it, and unbind
from its service. If we unbind from the service after the print activiy
is finished we get a crash. The bug was that we did not wait until we
disconnect from the remote renderer before finishing the print activity.

bug:17583115

Change-Id: I55b0135f9c5658b3a4fda2901b8b3bdef044e211
parent ddc5e5f1
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -110,13 +110,13 @@ public final class PageContentRepository {
        mRenderer.close(callback);
    }

    public void destroy() {
    public void destroy(Runnable callback) {
        throwIfNotClosed();
        mState = STATE_DESTROYED;
        if (DEBUG) {
            Log.i(LOG_TAG, "STATE_DESTROYED");
        }
        doDestroy();
        doDestroy(callback);
    }

    public void startPreload(int firstShownPage, int lastShownPage) {
@@ -163,19 +163,19 @@ public final class PageContentRepository {
        try {
            if (mState != STATE_DESTROYED) {
                mCloseGuard.warnIfOpen();
                doDestroy();
                doDestroy(null);
            }
        } finally {
            super.finalize();
        }
    }

    private void doDestroy() {
    private void doDestroy(Runnable callback) {
        mState = STATE_DESTROYED;
        if (DEBUG) {
            Log.i(LOG_TAG, "STATE_DESTROYED");
        }
        mRenderer.destroy();
        mRenderer.destroy(callback);
    }

    private void throwIfNotOpened() {
@@ -536,7 +536,7 @@ public final class PageContentRepository {
            }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        }

        public void destroy() {
        public void destroy(final Runnable callback) {
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
@@ -551,6 +551,10 @@ public final class PageContentRepository {
                    }
                    mPageContentCache.invalidate();
                    mPageContentCache.clear();
                    if (callback != null) {
                        callback.run();
                    }

                }
            }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        }
+5 −5
Original line number Diff line number Diff line
@@ -488,9 +488,9 @@ public final class PageAdapter extends Adapter implements
        return selectedPages;
    }

    public void destroy() {
    public void destroy(Runnable callback) {
        throwIfNotClosed();
        doDestroy();
        doDestroy(callback);
    }

    @Override
@@ -498,7 +498,7 @@ public final class PageAdapter extends Adapter implements
        try {
            if (mState != STATE_DESTROYED) {
                mCloseGuard.warnIfOpen();
                doDestroy();
                doDestroy(null);
            }
        } finally {
            super.finalize();
@@ -745,8 +745,8 @@ public final class PageAdapter extends Adapter implements
        mPageContentRepository.stopPreload();
    }

    private void doDestroy() {
        mPageContentRepository.destroy();
    private void doDestroy(Runnable callback) {
        mPageContentRepository.destroy(callback);
        mCloseGuard.close();
        mState = STATE_DESTROYED;
        if (DEBUG) {
+8 −2
Original line number Diff line number Diff line
@@ -1562,13 +1562,19 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
        if (mState != STATE_INITIALIZING) {
            mProgressMessageController.cancel();
            mPrinterRegistry.setTrackedPrinter(null);
            mPrintPreviewController.destroy();
            mSpoolerProvider.destroy();
            mPrintedDocument.finish();
            mPrintedDocument.destroy();
            mPrintPreviewController.destroy(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            });
        } else {
            finish();
        }
    }

    private final class SpinnerItem<T> {
        final T value;
+10 −5
Original line number Diff line number Diff line
@@ -192,12 +192,15 @@ class PrintPreviewController implements MutexFileProvider.OnReleaseRequestCallba
        });
    }

    public void destroy() {
    public void destroy(Runnable callback) {
        if (mPageAdapter.isOpened()) {
            mPageAdapter.close(null);
            Message operation = mHandler.obtainMessage(MyHandler.MSG_CLOSE);
            mHandler.enqueueOperation(operation);
        }
        mRecyclerView.setAdapter(null);
        mPageAdapter.destroy();

        Message operation = mHandler.obtainMessage(MyHandler.MSG_DESTROY);
        operation.obj = callback;
        mHandler.enqueueOperation(operation);
    }

    @Override
@@ -292,7 +295,9 @@ class PrintPreviewController implements MutexFileProvider.OnReleaseRequestCallba
                } break;

                case MSG_DESTROY: {
                    mPageAdapter.destroy();
                    Runnable callback = (Runnable) message.obj;
                    mRecyclerView.setAdapter(null);
                    mPageAdapter.destroy(callback);
                    handleNextOperation();
                } break;