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

Commit e8bffdcc authored by Philip P. Moltmann's avatar Philip P. Moltmann
Browse files

Reconnect to service when it is updated

- Remove service from mActiveServices when the binder died. (before update)
- Treat the failure to bind as if when the binder dies. (while updating)

If the service is removed for mActiveServices, onConfigurationChanged will
automatically restart the service.

Fixes: 27977706
Change-Id: I643248f56a9f2f4b7dce63cc1489a60739ab4537
parent 35d9e569
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -160,7 +160,10 @@ final class RemotePrintService implements DeathRecipient {
    }

    private void handleBinderDied() {
        if (mPrintService != null) {
            mPrintService.asBinder().unlinkToDeath(this, 0);
        }

        mPrintService = null;
        mServiceDied = true;
        mCallbacks.onServiceDied(this);
@@ -536,9 +539,21 @@ final class RemotePrintService implements DeathRecipient {
            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() {
+16 −2
Original line number Diff line number Diff line
@@ -95,6 +95,8 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,

    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 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
            // 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 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
        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 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
                    handleDispatchPrintServiceRecommendationsUpdated(
                            (List<RecommendationInfo>) message.obj);
                    break;
                case MSG_CHECK_CONFIG_CHANGED:
                    synchronized (mLock) {
                        onConfigurationChangedLocked();
                    }
                default:
                    // not reached
            }
@@ -1587,8 +1602,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
        }

        public void onServiceDiedLocked(RemotePrintService service) {
            // Remove the reported by that service.
            removePrintersForServiceLocked(service.getComponentName());
            removeServiceLocked(service);
        }

        public void onServiceAddedLocked(RemotePrintService service) {