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

Commit 1fff9847 authored by Yifan Hong's avatar Yifan Hong
Browse files

BatteryService: restrict service notification logic

Record instance name in init() and compare in
Notification.onRegistration.

Test: pass
Bug: 63702641
Change-Id: I74ac5abba385275c46ccd65a4d963fc1d8a6279b
parent 65d74c77
Loading
Loading
Loading
Loading
+13 −9
Original line number Diff line number Diff line
@@ -1163,8 +1163,10 @@ public final class BatteryService extends SystemService {
                Arrays.asList(INSTANCE_VENDOR, INSTANCE_HEALTHD);

        private final IServiceNotification mNotification = new Notification();
        // These variables are fixed after init.
        private Callback mCallback;
        private IHealthSupplier mHealthSupplier;
        private String mInstanceName;

        private final Object mLastServiceSetLock = new Object();
        // Last IHealth service received.
@@ -1206,21 +1208,23 @@ public final class BatteryService extends SystemService {

            IServiceManager manager = managerSupplier.get();
            for (String name : sAllInstances) {
                if (manager.getTransport(IHealth.kInterfaceName, name) ==
                if (manager.getTransport(IHealth.kInterfaceName, name) !=
                        IServiceManager.Transport.EMPTY) {
                    continue;
                    mInstanceName = name;
                    break;
                }

                manager.registerForNotifications(IHealth.kInterfaceName, name, mNotification);
                Slog.i(TAG, "health: HealthServiceWrapper listening to instance " + name);
                return;
            }

            if (mInstanceName == null) {
                throw new NoSuchElementException(String.format(
                        "No IHealth service instance among %s is available. Perhaps no permission?",
                        sAllInstances.toString()));
            }

            manager.registerForNotifications(IHealth.kInterfaceName, mInstanceName, mNotification);
            Slog.i(TAG, "health: HealthServiceWrapper listening to instance " + mInstanceName);
        }

        interface Callback {
            /**
             * This function is invoked asynchronously when a new and related IServiceNotification
@@ -1258,7 +1262,7 @@ public final class BatteryService extends SystemService {
            public final void onRegistration(String interfaceName, String instanceName,
                    boolean preexisting) {
                if (!IHealth.kInterfaceName.equals(interfaceName)) return;
                if (!sAllInstances.contains(instanceName)) return;
                if (!mInstanceName.equals(instanceName)) return;
                try {
                    // ensures the order of multiple onRegistration on different threads.
                    synchronized (mLastServiceSetLock) {