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

Commit 6cd33417 authored by Robin Lee's avatar Robin Lee Committed by Android (Google) Code Review
Browse files

Merge "Update LightsService linkToDeath to avoid capture" into main

parents 2d78fe26 fb9e7673
Loading
Loading
Loading
Loading
+41 −27
Original line number Diff line number Diff line
@@ -67,39 +67,28 @@ public class LightsService extends SystemService {

    private Handler mH;

    private final class LightsManagerBinderService extends ILightsManager.Stub {
        LightsManagerBinderService() {
            super(PermissionEnforcer.fromContext(getContext()));
        }
    private final class LightsManagerBinderService extends ILightsManager.Stub
          implements IBinder.DeathRecipient {

        private final class Session implements Comparable<Session> {
            final IBinder mToken;
            final SparseArray<LightState> mRequests = new SparseArray<>();
            final int mPriority;
        @GuardedBy("LightsService.this")
        private final List<Session> mSessions = new ArrayList<>();

            Session(IBinder token, int priority) {
                mToken = token;
                mPriority = priority;
        LightsManagerBinderService() {
            super(PermissionEnforcer.fromContext(getContext()));
        }

            void setRequest(int lightId, LightState state) {
                if (state != null) {
                    mRequests.put(lightId, state);
                } else {
                    mRequests.remove(lightId);
                }
            }
        /** @see #binderDied(IBinder) */
        @Override
        public void binderDied() {}

        /**
         * Callback for sessions which died without explicitly closing.
         */
        @Override
            public int compareTo(Session otherSession) {
                // Sort descending by priority
                return Integer.compare(otherSession.mPriority, mPriority);
            }
        public void binderDied(IBinder token) {
            closeSessionInternal(token);
        }

        @GuardedBy("LightsService.this")
        private final List<Session> mSessions = new ArrayList<>();

        /**
         * Returns the lights available for apps to control on the device. Only lights that aren't
         * reserved for system use are available to apps.
@@ -170,7 +159,7 @@ public class LightsService extends SystemService {
            synchronized (LightsService.this) {
                Preconditions.checkState(getSessionLocked(token) == null, "already registered");
                try {
                    token.linkToDeath(() -> closeSessionInternal(token), 0);
                    token.linkToDeath(LightsManagerBinderService.this, 0);
                    mSessions.add(new Session(token, priority));
                    Collections.sort(mSessions);
                } catch (RemoteException e) {
@@ -270,6 +259,31 @@ public class LightsService extends SystemService {
            }
            return null;
        }

        private final class Session implements Comparable<Session> {
            final IBinder mToken;
            final SparseArray<LightState> mRequests = new SparseArray<>();
            final int mPriority;

            Session(IBinder token, int priority) {
                mToken = token;
                mPriority = priority;
            }

            void setRequest(int lightId, LightState state) {
                if (state != null) {
                    mRequests.put(lightId, state);
                } else {
                    mRequests.remove(lightId);
                }
            }

            @Override
            public int compareTo(Session otherSession) {
                // Sort descending by priority
                return Integer.compare(otherSession.mPriority, mPriority);
            }
        }
    }

    private final class LightImpl extends LogicalLight {