Merge "Reconnect to service when it is updated"
diff --git a/services/print/java/com/android/server/print/RemotePrintService.java b/services/print/java/com/android/server/print/RemotePrintService.java
index 9c3a852..fa37576 100644
--- a/services/print/java/com/android/server/print/RemotePrintService.java
+++ b/services/print/java/com/android/server/print/RemotePrintService.java
@@ -160,7 +160,10 @@
     }
 
     private void handleBinderDied() {
-        mPrintService.asBinder().unlinkToDeath(this, 0);
+        if (mPrintService != null) {
+            mPrintService.asBinder().unlinkToDeath(this, 0);
+        }
+
         mPrintService = null;
         mServiceDied = true;
         mCallbacks.onServiceDied(this);
@@ -536,9 +539,21 @@
             Slog.i(LOG_TAG, "[user: " + mUserId + "] ensureBound()");
         }
         mBinding = true;
-        mContext.bindServiceAsUser(mIntent, mServiceConnection,
+
+        boolean wasBound = mContext.bindServiceAsUser(mIntent, mServiceConnection,
                 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
                 new UserHandle(mUserId));
+
+        if (!wasBound) {
+            if (DEBUG) {
+                Slog.i(LOG_TAG, "[user: " + mUserId + "] could not bind to " + mIntent);
+            }
+            mBinding = false;
+
+            if (!mServiceDied) {
+                handleBinderDied();
+            }
+        }
     }
 
     private void ensureUnbound() {
diff --git a/services/print/java/com/android/server/print/UserState.java b/services/print/java/com/android/server/print/UserState.java
index 7a3ebf4..671e65d 100644
--- a/services/print/java/com/android/server/print/UserState.java
+++ b/services/print/java/com/android/server/print/UserState.java
@@ -95,6 +95,8 @@
 
     private static final char COMPONENT_NAME_SEPARATOR = ':';
 
+    private static final int SERVICE_RESTART_DELAY_MILLIS = 500;
+
     private final SimpleStringSplitter mStringColonSplitter =
             new SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
 
@@ -754,6 +756,14 @@
             // Fail all print jobs.
             failActivePrintJobsForService(service.getComponentName());
             service.onAllPrintJobsHandled();
+
+            mActiveServices.remove(service.getComponentName());
+
+            // The service might need to be restarted if it died because of an update
+            mHandler.sendMessageDelayed(
+                    mHandler.obtainMessage(UserStateHandler.MSG_CHECK_CONFIG_CHANGED),
+                    SERVICE_RESTART_DELAY_MILLIS);
+
             // No session - nothing to do.
             if (mPrinterDiscoverySession == null) {
                 return;
@@ -1194,6 +1204,7 @@
         public static final int MSG_DISPATCH_PRINT_JOB_STATE_CHANGED = 1;
         public static final int MSG_DISPATCH_PRINT_SERVICES_CHANGED = 2;
         public static final int MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED = 3;
+        public static final int MSG_CHECK_CONFIG_CHANGED = 4;
 
         public UserStateHandler(Looper looper) {
             super(looper, null, false);
@@ -1214,6 +1225,10 @@
                     handleDispatchPrintServiceRecommendationsUpdated(
                             (List<RecommendationInfo>) message.obj);
                     break;
+                case MSG_CHECK_CONFIG_CHANGED:
+                    synchronized (mLock) {
+                        onConfigurationChangedLocked();
+                    }
                 default:
                     // not reached
             }
@@ -1587,8 +1602,7 @@
         }
 
         public void onServiceDiedLocked(RemotePrintService service) {
-            // Remove the reported by that service.
-            removePrintersForServiceLocked(service.getComponentName());
+            removeServiceLocked(service);
         }
 
         public void onServiceAddedLocked(RemotePrintService service) {