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

Commit e17123dd authored by Svetoslav's avatar Svetoslav
Browse files

Fix back button behavior and clean up in print preview activity.

The print preview UI owns and drives the lifecycle of several objects.
These objects were torn down too late resulting in an attempt to use
the activity as a context when it was no longer valid and service unbind
resulting in causing a crash.

Fixed a bug where if the back button is pressed mutiple times when
the print UI is being initialized a crash occurs.

bug:17454041

Change-Id: Ic1455b467586f0ad65f907e2160ec5cfb2d17d05
parent cde5bb45
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Debug;
import android.os.IBinder;
import android.util.Log;

public class PrintSpoolerProvider implements ServiceConnection {
    private final Context mContext;
+9 −10
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ public final class RemotePrintDocument {

    private final Looper mLooper;
    private final IPrintDocumentAdapter mPrintDocumentAdapter;
    private final DocumentObserver mDocumentObserver;
    private final RemoteAdapterDeathObserver mAdapterDeathObserver;

    private final UpdateResultCallbacks mUpdateCallbacks;

@@ -135,7 +135,7 @@ public final class RemotePrintDocument {
    private final DeathRecipient mDeathRecipient = new DeathRecipient() {
        @Override
        public void binderDied() {
            finish();
            mAdapterDeathObserver.onDied();
        }
    };

@@ -144,8 +144,8 @@ public final class RemotePrintDocument {
    private AsyncCommand mCurrentCommand;
    private AsyncCommand mNextCommand;

    public interface DocumentObserver {
        public void onDestroy();
    public interface RemoteAdapterDeathObserver {
        public void onDied();
    }

    public interface UpdateResultCallbacks {
@@ -155,12 +155,12 @@ public final class RemotePrintDocument {
    }

    public RemotePrintDocument(Context context, IPrintDocumentAdapter adapter,
            MutexFileProvider fileProvider, DocumentObserver destroyListener,
            MutexFileProvider fileProvider, RemoteAdapterDeathObserver deathObserver,
            UpdateResultCallbacks callbacks) {
        mPrintDocumentAdapter = adapter;
        mLooper = context.getMainLooper();
        mContext = context;
        mDocumentObserver = destroyListener;
        mAdapterDeathObserver = deathObserver;
        mDocumentInfo = new RemotePrintDocumentInfo();
        mDocumentInfo.fileProvider = fileProvider;
        mUpdateCallbacks = callbacks;
@@ -180,7 +180,7 @@ public final class RemotePrintDocument {
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error calling start()", re);
            mState = STATE_FAILED;
            mDocumentObserver.onDestroy();
            mAdapterDeathObserver.onDied();
        }
    }

@@ -269,7 +269,7 @@ public final class RemotePrintDocument {
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error calling finish()", re);
            mState = STATE_FAILED;
            mDocumentObserver.onDestroy();
            mAdapterDeathObserver.onDied();
        }
    }

@@ -302,7 +302,6 @@ public final class RemotePrintDocument {
        mState = STATE_DESTROYED;

        disconnectFromRemoteDocument();
        mDocumentObserver.onDestroy();
    }

    public boolean isUpdating() {
@@ -1124,7 +1123,7 @@ public final class RemotePrintDocument {
                new Handler(document.mLooper).post(new Runnable() {
                    @Override
                    public void run() {
                        document.mDocumentObserver.onDestroy();
                        document.mAdapterDeathObserver.onDied();
                    }
                });
            }
+29 −20
Original line number Diff line number Diff line
@@ -284,11 +284,14 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
                mFileProvider);
        mPrintedDocument = new RemotePrintDocument(PrintActivity.this,
                IPrintDocumentAdapter.Stub.asInterface(documentAdapter),
                mFileProvider, new RemotePrintDocument.DocumentObserver() {
                mFileProvider, new RemotePrintDocument.RemoteAdapterDeathObserver() {
            @Override
            public void onDestroy() {
            public void onDied() {
                if (isFinishing()) {
                    return;
                }
                setState(STATE_PRINT_CANCELED);
                finish();
                doFinish();
            }
        }, PrintActivity.this);
        mProgressMessageController = new ProgressMessageController(
@@ -342,17 +345,6 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
                    spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_CANCELED, null);
                } break;
            }

            mProgressMessageController.cancel();
            mPrinterRegistry.setTrackedPrinter(null);
            mPrintPreviewController.destroy();
            mSpoolerProvider.destroy();

            if (mPrintedDocument.isUpdating()) {
                mPrintedDocument.cancel();
            }
            mPrintedDocument.finish();
            mPrintedDocument.destroy();
        }

        mPrinterAvailabilityDetector.cancel();
@@ -372,7 +364,12 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (mState == STATE_INITIALIZING) {
            finish();
            doFinish();
            return true;
        }

        if (mState == STATE_PRINT_CANCELED ||mState == STATE_PRINT_CONFIRMED
                || mState == STATE_PRINT_COMPLETED) {
            return true;
        }

@@ -430,7 +427,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
            } break;

            case STATE_PRINT_CANCELED: {
                finish();
                doFinish();
            } break;
        }
    }
@@ -467,7 +464,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
            } break;

            case STATE_PRINT_CANCELED: {
                finish();
                doFinish();
            } break;

            default: {
@@ -600,7 +597,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
            mDestinationSpinner.post(new Runnable() {
                @Override
                public void run() {
                    finish();
                    doFinish();
                }
            });
        }
@@ -962,7 +959,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
        if (mPrintedDocument.isUpdating()) {
            mPrintedDocument.cancel();
        }
        finish();
        doFinish();
    }

    private void confirmPrint() {
@@ -1539,11 +1536,23 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
                if (writeToUri != null) {
                    mPrintedDocument.writeContent(getContentResolver(), writeToUri);
                }
                finish();
                doFinish();
            }
        }).shred();
    }

    private void doFinish() {
        if (mState != STATE_INITIALIZING) {
            mProgressMessageController.cancel();
            mPrinterRegistry.setTrackedPrinter(null);
            mPrintPreviewController.destroy();
            mSpoolerProvider.destroy();
            mPrintedDocument.finish();
            mPrintedDocument.destroy();
        }
        finish();
    }

    private final class SpinnerItem<T> {
        final T value;
        final CharSequence label;
+23 −5
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.os.Message;
import android.print.PrinterId;
import android.print.PrinterInfo;
import com.android.internal.os.SomeArgs;
import com.android.printspooler.model.PrintSpoolerService;

import java.util.ArrayList;
import java.util.List;
@@ -68,24 +69,41 @@ public class PrinterRegistry {
    }

    public void addHistoricalPrinter(PrinterInfo printer) {
        FusedPrintersProvider provider = getPrinterProvider();
        if (provider != null) {
            getPrinterProvider().addHistoricalPrinter(printer);
        }
    }

    public void forgetFavoritePrinter(PrinterId printerId) {
        getPrinterProvider().forgetFavoritePrinter(printerId);
        FusedPrintersProvider provider = getPrinterProvider();
        if (provider != null) {
            provider.forgetFavoritePrinter(printerId);
        }
    }

    public boolean isFavoritePrinter(PrinterId printerId) {
        return getPrinterProvider().isFavoritePrinter(printerId);
        FusedPrintersProvider provider = getPrinterProvider();
        if (provider != null) {
            return provider.isFavoritePrinter(printerId);
        }
        return false;
    }

    public void setTrackedPrinter(PrinterId printerId) {
        getPrinterProvider().setTrackedPrinter(printerId);
        FusedPrintersProvider provider = getPrinterProvider();
        if (provider != null) {
            provider.setTrackedPrinter(printerId);
        }
    }

    public boolean areHistoricalPrintersLoaded() {
        FusedPrintersProvider provider = getPrinterProvider();
        if (provider != null) {
            return getPrinterProvider().areHistoricalPrintersLoaded();
        }
        return false;
    }

    private FusedPrintersProvider getPrinterProvider() {
        Loader<?> loader = mActivity.getLoaderManager().getLoader(LOADER_ID_PRINTERS_LOADER);