Loading services/core/java/com/android/server/BatteryService.java +16 −25 Original line number Original line Diff line number Diff line Loading @@ -81,8 +81,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference; Loading Loading @@ -1435,11 +1433,7 @@ public final class BatteryService extends SystemService { */ */ public static final class HealthServiceWrapper { public static final class HealthServiceWrapper { private static final String TAG = "HealthServiceWrapper"; private static final String TAG = "HealthServiceWrapper"; public static final String INSTANCE_HEALTHD = "backup"; public static final String INSTANCE_VENDOR = "default"; public static final String INSTANCE_VENDOR = "default"; // All interesting instances, sorted by priority high -> low. private static final List<String> sAllInstances = Arrays.asList(INSTANCE_VENDOR, INSTANCE_HEALTHD); private final IServiceNotification mNotification = new Notification(); private final IServiceNotification mNotification = new Notification(); private final HandlerThread mHandlerThread = new HandlerThread("HealthServiceHwbinder"); private final HandlerThread mHandlerThread = new HandlerThread("HealthServiceHwbinder"); Loading Loading @@ -1471,8 +1465,8 @@ public final class BatteryService extends SystemService { } } /** /** * Start monitoring registration of new IHealth services. Only instances that are in * Start monitoring registration of new IHealth services. Only instance * {@code sAllInstances} and in device / framework manifest are used. This function should * {@link #INSTANCE_VENDOR} and in device / framework manifest are used. This function should * only be called once. * only be called once. * * * mCallback.onRegistration() is called synchronously (aka in init thread) before * mCallback.onRegistration() is called synchronously (aka in init thread) before Loading @@ -1481,7 +1475,7 @@ public final class BatteryService extends SystemService { * @throws RemoteException transaction error when talking to IServiceManager * @throws RemoteException transaction error when talking to IServiceManager * @throws NoSuchElementException if one of the following cases: * @throws NoSuchElementException if one of the following cases: * - No service manager; * - No service manager; * - none of {@code sAllInstances} are in manifests (i.e. not * - {@link #INSTANCE_VENDOR} is not in manifests (i.e. not * available on this device), or none of these instances are available to current * available on this device), or none of these instances are available to current * process. * process. * @throws NullPointerException when supplier is null * @throws NullPointerException when supplier is null Loading @@ -1499,26 +1493,23 @@ public final class BatteryService extends SystemService { // Initialize mLastService and call callback for the first time (in init thread) // Initialize mLastService and call callback for the first time (in init thread) IHealth newService = null; IHealth newService = null; for (String name : sAllInstances) { traceBegin("HealthInitGetService_" + INSTANCE_VENDOR); traceBegin("HealthInitGetService_" + name); try { try { newService = healthSupplier.get(name); newService = healthSupplier.get(INSTANCE_VENDOR); } catch (NoSuchElementException ex) { } catch (NoSuchElementException ex) { /* ignored, handled below */ /* ignored, handled below */ } finally { } finally { traceEnd(); traceEnd(); } } if (newService != null) { if (newService != null) { mInstanceName = name; mInstanceName = INSTANCE_VENDOR; mLastService.set(newService); mLastService.set(newService); break; } } } if (mInstanceName == null || newService == null) { if (mInstanceName == null || newService == null) { throw new NoSuchElementException(String.format( throw new NoSuchElementException(String.format( "No IHealth service instance among %s is available. Perhaps no permission?", "IHealth service instance %s isn't available. Perhaps no permission?", sAllInstances.toString())); INSTANCE_VENDOR)); } } if (callback != null) { if (callback != null) { Loading services/tests/servicestests/src/com/android/server/BatteryServiceTest.java +1 −12 Original line number Original line Diff line number Diff line Loading @@ -47,7 +47,6 @@ public class BatteryServiceTest extends AndroidTestCase { @Mock BatteryService.HealthServiceWrapper.IHealthSupplier mHealthServiceSupplier; @Mock BatteryService.HealthServiceWrapper.IHealthSupplier mHealthServiceSupplier; BatteryService.HealthServiceWrapper mWrapper; BatteryService.HealthServiceWrapper mWrapper; private static final String HEALTHD = BatteryService.HealthServiceWrapper.INSTANCE_HEALTHD; private static final String VENDOR = BatteryService.HealthServiceWrapper.INSTANCE_VENDOR; private static final String VENDOR = BatteryService.HealthServiceWrapper.INSTANCE_VENDOR; @Override @Override Loading Loading @@ -117,7 +116,7 @@ public class BatteryServiceTest extends AndroidTestCase { @SmallTest @SmallTest public void testWrapPreferVendor() throws Exception { public void testWrapPreferVendor() throws Exception { initForInstances(VENDOR, HEALTHD); initForInstances(VENDOR); mWrapper.init(mCallback, mManagerSupplier, mHealthServiceSupplier); mWrapper.init(mCallback, mManagerSupplier, mHealthServiceSupplier); waitHandlerThreadFinish(); waitHandlerThreadFinish(); verify(mCallback, times(1)).onRegistration(same(null), same(mMockedHal), eq(VENDOR)); verify(mCallback, times(1)).onRegistration(same(null), same(mMockedHal), eq(VENDOR)); Loading @@ -125,16 +124,6 @@ public class BatteryServiceTest extends AndroidTestCase { verify(mCallback, times(1)).onRegistration(same(mMockedHal), same(mMockedHal2), eq(VENDOR)); verify(mCallback, times(1)).onRegistration(same(mMockedHal), same(mMockedHal2), eq(VENDOR)); } } @SmallTest public void testUseHealthd() throws Exception { initForInstances(HEALTHD); mWrapper.init(mCallback, mManagerSupplier, mHealthServiceSupplier); waitHandlerThreadFinish(); verify(mCallback, times(1)).onRegistration(same(null), same(mMockedHal), eq(HEALTHD)); verify(mCallback, never()).onRegistration(same(mMockedHal), same(mMockedHal), anyString()); verify(mCallback, times(1)).onRegistration(same(mMockedHal), same(mMockedHal2), eq(HEALTHD)); } @SmallTest @SmallTest public void testNoService() throws Exception { public void testNoService() throws Exception { initForInstances("unrelated"); initForInstances("unrelated"); Loading Loading
services/core/java/com/android/server/BatteryService.java +16 −25 Original line number Original line Diff line number Diff line Loading @@ -81,8 +81,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference; Loading Loading @@ -1435,11 +1433,7 @@ public final class BatteryService extends SystemService { */ */ public static final class HealthServiceWrapper { public static final class HealthServiceWrapper { private static final String TAG = "HealthServiceWrapper"; private static final String TAG = "HealthServiceWrapper"; public static final String INSTANCE_HEALTHD = "backup"; public static final String INSTANCE_VENDOR = "default"; public static final String INSTANCE_VENDOR = "default"; // All interesting instances, sorted by priority high -> low. private static final List<String> sAllInstances = Arrays.asList(INSTANCE_VENDOR, INSTANCE_HEALTHD); private final IServiceNotification mNotification = new Notification(); private final IServiceNotification mNotification = new Notification(); private final HandlerThread mHandlerThread = new HandlerThread("HealthServiceHwbinder"); private final HandlerThread mHandlerThread = new HandlerThread("HealthServiceHwbinder"); Loading Loading @@ -1471,8 +1465,8 @@ public final class BatteryService extends SystemService { } } /** /** * Start monitoring registration of new IHealth services. Only instances that are in * Start monitoring registration of new IHealth services. Only instance * {@code sAllInstances} and in device / framework manifest are used. This function should * {@link #INSTANCE_VENDOR} and in device / framework manifest are used. This function should * only be called once. * only be called once. * * * mCallback.onRegistration() is called synchronously (aka in init thread) before * mCallback.onRegistration() is called synchronously (aka in init thread) before Loading @@ -1481,7 +1475,7 @@ public final class BatteryService extends SystemService { * @throws RemoteException transaction error when talking to IServiceManager * @throws RemoteException transaction error when talking to IServiceManager * @throws NoSuchElementException if one of the following cases: * @throws NoSuchElementException if one of the following cases: * - No service manager; * - No service manager; * - none of {@code sAllInstances} are in manifests (i.e. not * - {@link #INSTANCE_VENDOR} is not in manifests (i.e. not * available on this device), or none of these instances are available to current * available on this device), or none of these instances are available to current * process. * process. * @throws NullPointerException when supplier is null * @throws NullPointerException when supplier is null Loading @@ -1499,26 +1493,23 @@ public final class BatteryService extends SystemService { // Initialize mLastService and call callback for the first time (in init thread) // Initialize mLastService and call callback for the first time (in init thread) IHealth newService = null; IHealth newService = null; for (String name : sAllInstances) { traceBegin("HealthInitGetService_" + INSTANCE_VENDOR); traceBegin("HealthInitGetService_" + name); try { try { newService = healthSupplier.get(name); newService = healthSupplier.get(INSTANCE_VENDOR); } catch (NoSuchElementException ex) { } catch (NoSuchElementException ex) { /* ignored, handled below */ /* ignored, handled below */ } finally { } finally { traceEnd(); traceEnd(); } } if (newService != null) { if (newService != null) { mInstanceName = name; mInstanceName = INSTANCE_VENDOR; mLastService.set(newService); mLastService.set(newService); break; } } } if (mInstanceName == null || newService == null) { if (mInstanceName == null || newService == null) { throw new NoSuchElementException(String.format( throw new NoSuchElementException(String.format( "No IHealth service instance among %s is available. Perhaps no permission?", "IHealth service instance %s isn't available. Perhaps no permission?", sAllInstances.toString())); INSTANCE_VENDOR)); } } if (callback != null) { if (callback != null) { Loading
services/tests/servicestests/src/com/android/server/BatteryServiceTest.java +1 −12 Original line number Original line Diff line number Diff line Loading @@ -47,7 +47,6 @@ public class BatteryServiceTest extends AndroidTestCase { @Mock BatteryService.HealthServiceWrapper.IHealthSupplier mHealthServiceSupplier; @Mock BatteryService.HealthServiceWrapper.IHealthSupplier mHealthServiceSupplier; BatteryService.HealthServiceWrapper mWrapper; BatteryService.HealthServiceWrapper mWrapper; private static final String HEALTHD = BatteryService.HealthServiceWrapper.INSTANCE_HEALTHD; private static final String VENDOR = BatteryService.HealthServiceWrapper.INSTANCE_VENDOR; private static final String VENDOR = BatteryService.HealthServiceWrapper.INSTANCE_VENDOR; @Override @Override Loading Loading @@ -117,7 +116,7 @@ public class BatteryServiceTest extends AndroidTestCase { @SmallTest @SmallTest public void testWrapPreferVendor() throws Exception { public void testWrapPreferVendor() throws Exception { initForInstances(VENDOR, HEALTHD); initForInstances(VENDOR); mWrapper.init(mCallback, mManagerSupplier, mHealthServiceSupplier); mWrapper.init(mCallback, mManagerSupplier, mHealthServiceSupplier); waitHandlerThreadFinish(); waitHandlerThreadFinish(); verify(mCallback, times(1)).onRegistration(same(null), same(mMockedHal), eq(VENDOR)); verify(mCallback, times(1)).onRegistration(same(null), same(mMockedHal), eq(VENDOR)); Loading @@ -125,16 +124,6 @@ public class BatteryServiceTest extends AndroidTestCase { verify(mCallback, times(1)).onRegistration(same(mMockedHal), same(mMockedHal2), eq(VENDOR)); verify(mCallback, times(1)).onRegistration(same(mMockedHal), same(mMockedHal2), eq(VENDOR)); } } @SmallTest public void testUseHealthd() throws Exception { initForInstances(HEALTHD); mWrapper.init(mCallback, mManagerSupplier, mHealthServiceSupplier); waitHandlerThreadFinish(); verify(mCallback, times(1)).onRegistration(same(null), same(mMockedHal), eq(HEALTHD)); verify(mCallback, never()).onRegistration(same(mMockedHal), same(mMockedHal), anyString()); verify(mCallback, times(1)).onRegistration(same(mMockedHal), same(mMockedHal2), eq(HEALTHD)); } @SmallTest @SmallTest public void testNoService() throws Exception { public void testNoService() throws Exception { initForInstances("unrelated"); initForInstances("unrelated"); Loading