Add getter to CarPowerPolicyFilter

- API linter complains about lack of getComponents().
- This CL adds getComponents() and declares components field as private.

Bug: 184910360
CTS-Coverage-Bug: 184910360
Test: atest CarPowerManagerTest CarPowerManagementServiceUnitTest
PowerComponentUtilUnitTest

Change-Id: I76b635ee0c70e7031a11d6f494fe9a0e7539ef98
diff --git a/car-lib/api/current.txt b/car-lib/api/current.txt
index 7b44f36..d3adce4 100644
--- a/car-lib/api/current.txt
+++ b/car-lib/api/current.txt
@@ -576,9 +576,9 @@
 
   public final class CarPowerPolicyFilter implements android.os.Parcelable {
     method public int describeContents();
+    method @NonNull public int[] getComponents();
     method public void writeToParcel(@NonNull android.os.Parcel, int);
     field @NonNull public static final android.os.Parcelable.Creator<android.car.hardware.power.CarPowerPolicyFilter> CREATOR;
-    field @NonNull public final int[] components;
   }
 
   public static final class CarPowerPolicyFilter.Builder {
diff --git a/car-lib/src/android/car/hardware/power/CarPowerManager.java b/car-lib/src/android/car/hardware/power/CarPowerManager.java
index 67f1d40..a003efc 100644
--- a/car-lib/src/android/car/hardware/power/CarPowerManager.java
+++ b/car-lib/src/android/car/hardware/power/CarPowerManager.java
@@ -419,11 +419,12 @@
         CarPowerPolicyFilter newFilter = null;
         synchronized (mLock) {
             mPolicyListenerMap.remove(listener);
+            int[] filterComponents = filter.getComponents().clone();
             Pair<Executor, CarPowerPolicyFilter> pair =
-                    new Pair<>(executor, new CarPowerPolicyFilter(filter.components.clone()));
+                    new Pair<>(executor, new CarPowerPolicyFilter(filterComponents));
             mPolicyListenerMap.put(listener, pair);
-            for (int i = 0; i < filter.components.length; i++) {
-                int key = filter.components[i];
+            for (int i = 0; i < filterComponents.length; i++) {
+                int key = filterComponents[i];
                 int currentCount = mInterestedComponentMap.get(key);
                 if (currentCount == 0) {
                     updateCallbackNeeded = true;
@@ -458,8 +459,9 @@
             if (pair == null) {
                 return;
             }
-            for (int i = 0; i < pair.second.components.length; i++) {
-                int key = pair.second.components[i];
+            int[] filterComponents = pair.second.getComponents();
+            for (int i = 0; i < filterComponents.length; i++) {
+                int key = filterComponents[i];
                 int currentCount = mInterestedComponentMap.get(key);
                 if (currentCount == 0 || currentCount == 1) {
                     mInterestedComponentMap.delete(key);
diff --git a/car-lib/src/android/car/hardware/power/CarPowerPolicyFilter.java b/car-lib/src/android/car/hardware/power/CarPowerPolicyFilter.java
index c4492ea..29b8ad6 100644
--- a/car-lib/src/android/car/hardware/power/CarPowerPolicyFilter.java
+++ b/car-lib/src/android/car/hardware/power/CarPowerPolicyFilter.java
@@ -33,11 +33,11 @@
      * List of components of interest. Components are one of
      * {@code android.frameworks.automotive.powerpolicy.PowerComponent}.
      */
-    public final @NonNull int[] components;
+    private @NonNull int[] mComponents = new int[]{};
 
 
 
-    // Code below generated by codegen v1.0.22.
+    // Code below generated by codegen v1.0.23.
     //
     // DO NOT MODIFY!
     // CHECKSTYLE:OFF Generated code
@@ -53,20 +53,29 @@
     @DataClass.Generated.Member
     /* package-private */ CarPowerPolicyFilter(
             @NonNull int[] components) {
-        this.components = components;
+        this.mComponents = components;
         com.android.internal.util.AnnotationValidations.validate(
-                NonNull.class, null, components);
+                NonNull.class, null, mComponents);
 
         // onConstructed(); // You can define this method to get a callback
     }
 
+    /**
+     * List of components of interest. Components are one of
+     * {@code android.frameworks.automotive.powerpolicy.PowerComponent}.
+     */
+    @DataClass.Generated.Member
+    public @NonNull int[] getComponents() {
+        return mComponents;
+    }
+
     @Override
     @DataClass.Generated.Member
     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
         // You can override field parcelling by defining methods like:
         // void parcelFieldName(Parcel dest, int flags) { ... }
 
-        dest.writeIntArray(components);
+        dest.writeIntArray(mComponents);
     }
 
     @Override
@@ -80,11 +89,11 @@
         // You can override field unparcelling by defining methods like:
         // static FieldType unparcelFieldName(Parcel in) { ... }
 
-        int[] _components = in.createIntArray();
+        int[] components = in.createIntArray();
 
-        this.components = _components;
+        this.mComponents = components;
         com.android.internal.util.AnnotationValidations.validate(
-                NonNull.class, null, components);
+                NonNull.class, null, mComponents);
 
         // onConstructed(); // You can define this method to get a callback
     }
@@ -110,10 +119,13 @@
     @DataClass.Generated.Member
     public static final class Builder {
 
-        private @NonNull int[] components;
+        private @NonNull int[] mComponents;
 
         private long mBuilderFieldsSet = 0L;
 
+        public Builder() {
+        }
+
         /**
          * List of components of interest. Components are one of
          * {@code android.frameworks.automotive.powerpolicy.PowerComponent}.
@@ -122,7 +134,7 @@
         public @NonNull Builder setComponents(@NonNull int... value) {
             checkNotUsed();
             mBuilderFieldsSet |= 0x1;
-            components = value;
+            mComponents = value;
             return this;
         }
 
@@ -131,8 +143,11 @@
             checkNotUsed();
             mBuilderFieldsSet |= 0x2; // Mark builder used
 
+            if ((mBuilderFieldsSet & 0x1) == 0) {
+                mComponents = new int[] {};
+            }
             CarPowerPolicyFilter o = new CarPowerPolicyFilter(
-                    components);
+                    mComponents);
             return o;
         }
 
@@ -145,10 +160,10 @@
     }
 
     @DataClass.Generated(
-            time = 1613117708240L,
-            codegenVersion = "1.0.22",
+            time = 1618274906419L,
+            codegenVersion = "1.0.23",
             sourceFile = "packages/services/Car/car-lib/src/android/car/hardware/power/CarPowerPolicyFilter.java",
-            inputSignatures = "public final @android.annotation.NonNull int[] components\nclass CarPowerPolicyFilter extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true)")
+            inputSignatures = "private @android.annotation.NonNull int[] mComponents\nclass CarPowerPolicyFilter extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true)")
     @Deprecated
     private void __metadata() {}
 
diff --git a/car-lib/src/android/car/hardware/power/PowerComponentUtil.java b/car-lib/src/android/car/hardware/power/PowerComponentUtil.java
index 5ea23ff..70c1b1f 100644
--- a/car-lib/src/android/car/hardware/power/PowerComponentUtil.java
+++ b/car-lib/src/android/car/hardware/power/PowerComponentUtil.java
@@ -110,7 +110,7 @@
     public static boolean hasComponents(@NonNull CarPowerPolicy policy,
             @NonNull CarPowerPolicyFilter filter) {
         SparseBooleanArray filterSet = new SparseBooleanArray();
-        int[] components = filter.components;
+        int[] components = filter.getComponents();
         for (int i = 0; i < components.length; i++) {
             filterSet.put(components[i], true);
         }
diff --git a/service/src/com/android/car/BluetoothDeviceConnectionPolicy.java b/service/src/com/android/car/BluetoothDeviceConnectionPolicy.java
index e2a8495..7b084f8 100644
--- a/service/src/com/android/car/BluetoothDeviceConnectionPolicy.java
+++ b/service/src/com/android/car/BluetoothDeviceConnectionPolicy.java
@@ -320,7 +320,7 @@
                 CarPowerManagementService.class);
         if (cpms != null) {
             CarPowerPolicyFilter filter = new CarPowerPolicyFilter.Builder()
-                    .setComponents(new int[]{PowerComponent.BLUETOOTH}).build();
+                    .setComponents(PowerComponent.BLUETOOTH).build();
             cpms.addPowerPolicyListener(filter, mPowerPolicyListener);
         } else {
             Slog.w(TAG, "Cannot find CarPowerManagementService");
diff --git a/service/src/com/android/car/audio/CarAudioPowerListener.java b/service/src/com/android/car/audio/CarAudioPowerListener.java
index b946745..eed53cb 100644
--- a/service/src/com/android/car/audio/CarAudioPowerListener.java
+++ b/service/src/com/android/car/audio/CarAudioPowerListener.java
@@ -83,7 +83,7 @@
         }
 
         CarPowerPolicyFilter filter = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{AUDIO}).build();
+                .setComponents(AUDIO).build();
         mCarPowerManagementService.addPowerPolicyListener(filter, mChangeListener);
         initializePowerState();
     }
diff --git a/tests/CarSecurityPermissionTest/src/com/android/car/power/CarPowerManagerPermissionTest.java b/tests/CarSecurityPermissionTest/src/com/android/car/power/CarPowerManagerPermissionTest.java
index e6e1107..9b7987a 100644
--- a/tests/CarSecurityPermissionTest/src/com/android/car/power/CarPowerManagerPermissionTest.java
+++ b/tests/CarSecurityPermissionTest/src/com/android/car/power/CarPowerManagerPermissionTest.java
@@ -118,7 +118,7 @@
     public void testAddPowerPolicyChangeListener() throws Exception {
         Executor executor = mContext.getMainExecutor();
         CarPowerPolicyFilter filter = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{ PowerComponent.AUDIO }).build();
+                .setComponents(PowerComponent.AUDIO).build();
         Exception e = expectThrows(SecurityException.class,
                 () -> mCarPowerManager.addPowerPolicyListener(executor, filter, (p) -> { }));
 
diff --git a/tests/carservice_unit_test/src/com/android/car/audio/CarAudioPowerListenerTest.java b/tests/carservice_unit_test/src/com/android/car/audio/CarAudioPowerListenerTest.java
index 7c2c580..1002897 100644
--- a/tests/carservice_unit_test/src/com/android/car/audio/CarAudioPowerListenerTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/audio/CarAudioPowerListenerTest.java
@@ -81,7 +81,8 @@
                 CarPowerPolicyFilter.class);
         verify(mMockCarPowerService).addPowerPolicyListener(
                 captor.capture(), any(ICarPowerPolicyListener.class));
-        assertThat(captor.getValue().components).asList().containsExactly(PowerComponent.AUDIO);
+        assertThat(captor.getValue().getComponents()).asList()
+                .containsExactly(PowerComponent.AUDIO);
 
     }
 
diff --git a/tests/carservice_unit_test/src/com/android/car/hardware/power/CarPowerManagerUnitTest.java b/tests/carservice_unit_test/src/com/android/car/hardware/power/CarPowerManagerUnitTest.java
index 489814d..6d5c7dc 100644
--- a/tests/carservice_unit_test/src/com/android/car/hardware/power/CarPowerManagerUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/hardware/power/CarPowerManagerUnitTest.java
@@ -307,11 +307,11 @@
         MockedPowerPolicyListener listenerWifi = new MockedPowerPolicyListener();
         MockedPowerPolicyListener listenerLocation = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filterAudio = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
         CarPowerPolicyFilter filterWifi = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.WIFI}).build();
+                .setComponents(PowerComponent.WIFI).build();
         CarPowerPolicyFilter filterLocation = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.LOCATION}).build();
+                .setComponents(PowerComponent.LOCATION).build();
 
         mCarPowerManager.addPowerPolicyListener(mExecutor, filterAudio, listenerAudio);
         mCarPowerManager.addPowerPolicyListener(mExecutor, filterWifi, listenerWifi);
@@ -330,9 +330,9 @@
         mService.definePowerPolicy(policyId, new String[]{"AUDIO"}, new String[]{"WIFI"});
         MockedPowerPolicyListener listener = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filterAudio = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
         CarPowerPolicyFilter filterLocation = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.LOCATION}).build();
+                .setComponents(PowerComponent.LOCATION).build();
 
         mCarPowerManager.addPowerPolicyListener(mExecutor, filterAudio, listener);
         mCarPowerManager.addPowerPolicyListener(mExecutor, filterLocation, listener);
@@ -345,7 +345,7 @@
     public void testAddPowerPolicyListener_nullListener() throws Exception {
         MockedPowerPolicyListener listener = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filter = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
 
         assertThrows(NullPointerException.class,
                 () -> mCarPowerManager.addPowerPolicyListener(null, filter, listener));
@@ -363,7 +363,7 @@
         MockedPowerPolicyListener listenerOne = new MockedPowerPolicyListener();
         MockedPowerPolicyListener listenerTwo = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filterAudio = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
 
         mCarPowerManager.addPowerPolicyListener(mExecutor, filterAudio, listenerOne);
         mCarPowerManager.addPowerPolicyListener(mExecutor, filterAudio, listenerTwo);
@@ -379,7 +379,7 @@
         grantPowerPolicyPermission();
         MockedPowerPolicyListener listener = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filter = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
 
         // Remove unregistered listener should not throw an exception.
         mCarPowerManager.removePowerPolicyListener(listener);
diff --git a/tests/carservice_unit_test/src/com/android/car/hardware/power/PowerComponentUtilUnitTest.java b/tests/carservice_unit_test/src/com/android/car/hardware/power/PowerComponentUtilUnitTest.java
index b0abb37..eea6520 100644
--- a/tests/carservice_unit_test/src/com/android/car/hardware/power/PowerComponentUtilUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/hardware/power/PowerComponentUtilUnitTest.java
@@ -58,13 +58,13 @@
         CarPowerPolicy policy = new CarPowerPolicy("testPolicy", new int[]{PowerComponent.AUDIO},
                 new int[]{PowerComponent.WIFI, PowerComponent.NFC});
         CarPowerPolicyFilter filterAudio = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
         CarPowerPolicyFilter filterWifi = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.WIFI}).build();
+                .setComponents(PowerComponent.WIFI).build();
         CarPowerPolicyFilter filterLocationNfc = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.LOCATION, PowerComponent.NFC}).build();
+                .setComponents(PowerComponent.LOCATION, PowerComponent.NFC).build();
         CarPowerPolicyFilter filterMedia = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.MEDIA}).build();
+                .setComponents(PowerComponent.MEDIA).build();
 
         assertThat(PowerComponentUtil.hasComponents(policy, filterAudio)).isTrue();
         assertThat(PowerComponentUtil.hasComponents(policy, filterWifi)).isTrue();
diff --git a/tests/carservice_unit_test/src/com/android/car/power/CarPowerManagementServiceUnitTest.java b/tests/carservice_unit_test/src/com/android/car/power/CarPowerManagementServiceUnitTest.java
index e0e07f7..14ec688 100644
--- a/tests/carservice_unit_test/src/com/android/car/power/CarPowerManagementServiceUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/power/CarPowerManagementServiceUnitTest.java
@@ -439,11 +439,11 @@
         MockedPowerPolicyListener listenerWifi = new MockedPowerPolicyListener();
         MockedPowerPolicyListener listenerLocation = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filterAudio = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
         CarPowerPolicyFilter filterWifi = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.WIFI}).build();
+                .setComponents(PowerComponent.WIFI).build();
         CarPowerPolicyFilter filterLocation = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.LOCATION}).build();
+                .setComponents(PowerComponent.LOCATION).build();
 
         mService.addPowerPolicyListener(filterAudio, listenerAudio);
         mService.addPowerPolicyListener(filterWifi, listenerWifi);
@@ -461,7 +461,7 @@
         mService.definePowerPolicy(policyId, new String[]{"AUDIO"}, new String[]{"WIFI"});
         MockedPowerPolicyListener listenerAudio = new MockedPowerPolicyListener();
         CarPowerPolicyFilter filterAudio = new CarPowerPolicyFilter.Builder()
-                .setComponents(new int[]{PowerComponent.AUDIO}).build();
+                .setComponents(PowerComponent.AUDIO).build();
 
         mService.addPowerPolicyListener(filterAudio, listenerAudio);
         mService.removePowerPolicyListener(listenerAudio);