Hide the print dialog if the printing activity is destroyed.

1. For an app to print it creates a PrintDocumentAdapter implementation
   which is passed to the print dialog activity. If the activity that
   created the adapter is destroyed then the adapter, which may rely on
   the activity state, may be in an invalid state. For example, an app
   creates an adapter and calls print resuting in the app activity and
   the print dialog activity being stacked. Now the user rotates the
   device which triggers the recreating of the activity stack (assume the
   app does not handle rotation). The recreated print dialog activity
   receives the intent that originally created it with containing the
   adapter that was constructed in the context of the old, now destroyed,
   app activity instance.

   To handle this we are limiting an app to be able to print only from
   and activity and when this activity is destroyed we mark the adapter
   as invalid which will result in hiding the print dialog activity. Note
   that if the app process is killed we already handle this in the print
   dialog activiy by registering a death recipient on the adapter binder.

2. In the PrintManager.PrintDocumentAdapterDelegate some of the state is
   accessed only on the main thread and some from miltiple threads. The
   code was trying to avoid locking for state that is not accessed by
   multiple threads but this is error prone and the benefit does not
   justify the complexity and added fragility. Now grabbing a lock all
   the time.

3. The PrintJobConfigActivity waits for it to bind to the print spooler
   service before instantiating its print controller and editor. However,
   these can be accessed by invoking some of the activity cycle callbacks.
   This change is adding null checks for the case where the activity
   callbacks are called before the binding to the spooler is completed.


Change-Id: Id906b3170e4f0a0553772dfa62686f06fdca0eaf
5 files changed