Implemented CarUserManager.getUserAssociation().

Test: adb shell lshal debug android.hardware.automotive.vehicle@2.0::IVehicle/default --set 299896587 i 1 i 1 i 2 &&
      adb shell cmd car_service get-user-auth-association KEY_FOB
Test: adb logcat -b events | grep car_user_
Test: atest UserHalServiceTest CarUserServiceTest CarUserManagerUnitTest

Bug: 150409351

Change-Id: I82d7e56e0244c9835586d72bab48dcdcb23dfc61
diff --git a/car-internal-lib/src/com/android/internal/car/EventLogTags.logtags b/car-internal-lib/src/com/android/internal/car/EventLogTags.logtags
index 46ca755..e5b8db8 100644
--- a/car-internal-lib/src/com/android/internal/car/EventLogTags.logtags
+++ b/car-internal-lib/src/com/android/internal/car/EventLogTags.logtags
@@ -72,6 +72,8 @@
 150106 car_user_svc_switch_user_req (user_id|1),(timeout|1)
 150107 car_user_svc_switch_user_resp (status|1),(result|1),(error_message|3)
 150108 car_user_svc_post_switch_user_req (target_user_id|1),(current_user_id|1)
+150109 car_user_svc_get_user_auth_req (uid|1),(user_id|1),(number_types|1)
+150110 car_user_svc_get_user_auth_resp (number_values|1)
 
 150140 car_user_hal_initial_user_info_req (request_id|1),(request_type|1),(timeout|1)
 150141 car_user_hal_initial_user_info_resp (request_id|1),(status|1),(action|1),(user_id|1),(flags|1),(safe_name|3)
@@ -79,10 +81,12 @@
 150143 car_user_hal_switch_user_resp (request_id|1),(status|1),(result|1),(error_message|3)
 150144 car_user_hal_post_switch_user_req (request_id|1),(target_user_id|1),(current_user_id|1)
 150145 car_user_hal_get_user_auth_req (int32values|4)
-150146 car_user_hal_get_user_auth_resp (int32values|4)
+150146 car_user_hal_get_user_auth_resp (int32values|4),(error_message|3)
 
 150171 car_user_mgr_add_listener (uid|1)
 150172 car_user_mgr_remove_listener (uid|1)
 150173 car_user_mgr_disconnected (uid|1)
 150174 car_user_mgr_switch_user_request (uid|1),(user_id|1)
-150175 car_user_mgr_switch_user_response (uid|1),(status|1),(error_msg|3)
+150175 car_user_mgr_switch_user_response (uid|1),(status|1),(error_message|3)
+150176 car_user_mgr_get_user_auth_req (types|4)
+150177 car_user_mgr_get_user_auth_resp (values|4)
diff --git a/car-lib/src/android/car/ICarUserService.aidl b/car-lib/src/android/car/ICarUserService.aidl
index 4950b1b..9e49790 100644
--- a/car-lib/src/android/car/ICarUserService.aidl
+++ b/car-lib/src/android/car/ICarUserService.aidl
@@ -17,6 +17,7 @@
 package android.car;
 
 import android.content.pm.UserInfo;
+import android.car.user.GetUserIdentificationAssociationResponse;
 import android.car.user.UserSwitchResult;
 import com.android.internal.infra.AndroidFuture;
 import com.android.internal.os.IResultReceiver;
@@ -35,4 +36,5 @@
     oneway void setLifecycleListenerForUid(in IResultReceiver listener);
     oneway void resetLifecycleListenerForUid();
     oneway void getInitialUserInfo(int requestType, int timeoutMs, in IResultReceiver receiver);
+    GetUserIdentificationAssociationResponse getUserIdentificationAssociation(in int[] types);
 }
diff --git a/car-lib/src/android/car/user/CarUserManager.java b/car-lib/src/android/car/user/CarUserManager.java
index 7e57e1a..599d9b7 100644
--- a/car-lib/src/android/car/user/CarUserManager.java
+++ b/car-lib/src/android/car/user/CarUserManager.java
@@ -49,6 +49,8 @@
 import com.android.internal.car.EventLogTags;
 import com.android.internal.infra.AndroidFuture;
 import com.android.internal.os.IResultReceiver;
+import com.android.internal.util.ArrayUtils;
+import com.android.internal.util.Preconditions;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -311,6 +313,30 @@
         }
     }
 
+    /**
+     * Gets the user authentication types associated with this manager's user.
+     *
+     * @hide
+     */
+    @Nullable
+    @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
+    public GetUserIdentificationAssociationResponse getUserIdentificationAssociation(
+            @NonNull int... types) {
+        Preconditions.checkArgument(!ArrayUtils.isEmpty(types), "must have at least one type");
+        EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_REQ, types.length);
+        try {
+            GetUserIdentificationAssociationResponse response =
+                    mService.getUserIdentificationAssociation(types);
+            if (response != null) {
+                EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_RESP,
+                        response.getValues().length);
+            }
+            return response;
+        } catch (RemoteException e) {
+            return handleRemoteExceptionFromCarService(e, null);
+        }
+    }
+
     /** @hide */
     @TestApi
     // TODO(b/144120654): temp method used by CTS; will eventually be refactored to take a listener
diff --git a/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.aidl b/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.aidl
new file mode 100644
index 0000000..d8ac0dc
--- /dev/null
+++ b/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.car.user;
+
+parcelable GetUserIdentificationAssociationResponse;
diff --git a/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.java b/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.java
new file mode 100644
index 0000000..12bee07
--- /dev/null
+++ b/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.car.user;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.os.Parcelable;
+
+import com.android.internal.util.DataClass;
+
+/**
+ * Results of a {@link CarUserManager#getUserIdentificationAssociation(int[]) request.
+ *
+ * @hide
+ */
+@DataClass(
+        genToString = true,
+        genHiddenConstructor = true,
+        genHiddenConstDefs = true)
+public final class GetUserIdentificationAssociationResponse implements Parcelable {
+
+    /**
+     * Gets the error message returned by the HAL.
+     */
+    @Nullable
+    private String mErrorMessage;
+
+    /**
+     * Gets the list of
+     * {@link android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationValue}
+     * associates with the request.
+     */
+    @NonNull
+    private final int[] mValues;
+
+
+
+
+    // Code below generated by codegen v1.0.15.
+    //
+    // DO NOT MODIFY!
+    // CHECKSTYLE:OFF Generated code
+    //
+    // To regenerate run:
+    // $ codegen $ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.java
+    //
+    // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
+    //   Settings > Editor > Code Style > Formatter Control
+    //@formatter:off
+
+
+    /**
+     * Creates a new GetUserIdentificationAssociationResponse.
+     *
+     * @param errorMessage
+     *   Gets the error message returned by the HAL.
+     * @param values
+     *   Gets the list of
+     *   {@link android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationValue}
+     *   associates with the request.
+     * @hide
+     */
+    @DataClass.Generated.Member
+    public GetUserIdentificationAssociationResponse(
+            @Nullable String errorMessage,
+            @NonNull int[] values) {
+        this.mErrorMessage = errorMessage;
+        this.mValues = values;
+        com.android.internal.util.AnnotationValidations.validate(
+                NonNull.class, null, mValues);
+
+        // onConstructed(); // You can define this method to get a callback
+    }
+
+    /**
+     * Gets the error message returned by the HAL.
+     */
+    @DataClass.Generated.Member
+    public @Nullable String getErrorMessage() {
+        return mErrorMessage;
+    }
+
+    /**
+     * Gets the list of
+     * {@link android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationValue}
+     * associates with the request.
+     */
+    @DataClass.Generated.Member
+    public @NonNull int[] getValues() {
+        return mValues;
+    }
+
+    @Override
+    @DataClass.Generated.Member
+    public String toString() {
+        // You can override field toString logic by defining methods like:
+        // String fieldNameToString() { ... }
+
+        return "GetUserIdentificationAssociationResponse { " +
+                "errorMessage = " + mErrorMessage + ", " +
+                "values = " + java.util.Arrays.toString(mValues) +
+        " }";
+    }
+
+    @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) { ... }
+
+        byte flg = 0;
+        if (mErrorMessage != null) flg |= 0x1;
+        dest.writeByte(flg);
+        if (mErrorMessage != null) dest.writeString(mErrorMessage);
+        dest.writeIntArray(mValues);
+    }
+
+    @Override
+    @DataClass.Generated.Member
+    public int describeContents() { return 0; }
+
+    /** @hide */
+    @SuppressWarnings({"unchecked", "RedundantCast"})
+    @DataClass.Generated.Member
+    /* package-private */ GetUserIdentificationAssociationResponse(@NonNull android.os.Parcel in) {
+        // You can override field unparcelling by defining methods like:
+        // static FieldType unparcelFieldName(Parcel in) { ... }
+
+        byte flg = in.readByte();
+        String errorMessage = (flg & 0x1) == 0 ? null : in.readString();
+        int[] values = in.createIntArray();
+
+        this.mErrorMessage = errorMessage;
+        this.mValues = values;
+        com.android.internal.util.AnnotationValidations.validate(
+                NonNull.class, null, mValues);
+
+        // onConstructed(); // You can define this method to get a callback
+    }
+
+    @DataClass.Generated.Member
+    public static final @NonNull Parcelable.Creator<GetUserIdentificationAssociationResponse> CREATOR
+            = new Parcelable.Creator<GetUserIdentificationAssociationResponse>() {
+        @Override
+        public GetUserIdentificationAssociationResponse[] newArray(int size) {
+            return new GetUserIdentificationAssociationResponse[size];
+        }
+
+        @Override
+        public GetUserIdentificationAssociationResponse createFromParcel(@NonNull android.os.Parcel in) {
+            return new GetUserIdentificationAssociationResponse(in);
+        }
+    };
+
+    @DataClass.Generated(
+            time = 1587769987549L,
+            codegenVersion = "1.0.15",
+            sourceFile = "packages/services/Car/car-lib/src/android/car/user/GetUserIdentificationAssociationResponse.java",
+            inputSignatures = "private @android.annotation.Nullable java.lang.String mErrorMessage\nprivate final @android.annotation.NonNull int[] mValues\nclass GetUserIdentificationAssociationResponse extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)")
+    @Deprecated
+    private void __metadata() {}
+
+
+    //@formatter:on
+    // End of generated code
+
+}
diff --git a/service/src/com/android/car/CarShellCommand.java b/service/src/com/android/car/CarShellCommand.java
index 3ead1de..c889c9d 100644
--- a/service/src/com/android/car/CarShellCommand.java
+++ b/service/src/com/android/car/CarShellCommand.java
@@ -30,6 +30,7 @@
 import android.car.input.CarInputManager;
 import android.car.input.RotaryEvent;
 import android.car.user.CarUserManager;
+import android.car.user.GetUserIdentificationAssociationResponse;
 import android.car.user.UserSwitchResult;
 import android.car.userlib.HalCallback;
 import android.car.userlib.UserHalHelper;
@@ -41,6 +42,7 @@
 import android.hardware.automotive.vehicle.V2_0.SwitchUserStatus;
 import android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociation;
 import android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationType;
+import android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationValue;
 import android.hardware.automotive.vehicle.V2_0.UserIdentificationGetRequest;
 import android.hardware.automotive.vehicle.V2_0.UserIdentificationResponse;
 import android.hardware.automotive.vehicle.V2_0.UserInfo;
@@ -944,32 +946,69 @@
             }
 
         }
-        int requestSize = request.associationTypes.size();
-        request.numberAssociationTypes = requestSize;
         if (userId == UserHandle.USER_CURRENT) {
             userId = ActivityManager.getCurrentUser();
         }
-        // TODO(b/150413515): use UserHalHelper to set user flags
-        request.userInfo.userId = userId;
+        int requestSize = request.associationTypes.size();
+        if (halOnly) {
+            request.numberAssociationTypes = requestSize;
+            // TODO(b/150413515): use UserHalHelper to set user flags
+            request.userInfo.userId = userId;
 
-        if (!halOnly) {
-            // TODO(b/150409351): temporary restriction until CarUserManager implements it
-            throw new IllegalArgumentException("only --hal-only is supported for now");
+            Log.d(TAG, "getUserAuthAssociation(): user=" + userId + ", halOnly=" + halOnly
+                    + ", request=" + request);
+            UserIdentificationResponse response = mHal.getUserHal().getUserAssociation(request);
+            Log.d(TAG, "getUserAuthAssociation(): response=" + response);
+
+            if (response == null) {
+                writer.println("null response");
+                return;
+            }
+
+            if (!TextUtils.isEmpty(response.errorMessage)) {
+                writer.printf("Error message: %s\n", response.errorMessage);
+            }
+            int numberAssociations = response.associations.size();
+            writer.printf("%d associations:\n", numberAssociations);
+            for (int i = 0; i < numberAssociations; i++) {
+                UserIdentificationAssociation association = response.associations.get(i);
+                writer.printf("  %s\n", association);
+            }
+            return;
         }
 
-        Log.d(TAG, "getUserAuthAssociation(): user=" + userId + ", halOnly=" + halOnly
-                + ", request=" + request);
-        UserIdentificationResponse response = mHal.getUserHal().getUserAssociation(request);
-        Log.d(TAG, "getUserAuthAssociation(): response=" + response);
-
-        if (!TextUtils.isEmpty(response.errorMessage)) {
-            writer.printf("Error message: %s\n", response.errorMessage);
+        Context context;
+        if (userId == mContext.getUserId()) {
+            context = mContext;
+        } else {
+            context = mContext.createContextAsUser(UserHandle.of(userId), /* flags= */ 0);
         }
-        int numberAssociations = response.associations.size();
-        writer.printf("%d associations:\n", numberAssociations);
-        for (int i = 0; i < numberAssociations; i++) {
-            UserIdentificationAssociation association = response.associations.get(i);
-            writer.printf("  %s\n", association);
+        int actualUserId = Binder.getCallingUid();
+        if (actualUserId != userId) {
+            writer.printf("Emulating call for user id %d, but caller's user id is %d, so that's "
+                    + "what CarUserService will use when calling HAL.\n", userId, actualUserId);
+        }
+
+        Car car = Car.createCar(context);
+        CarUserManager carUserManager = (CarUserManager) car.getCarManager(Car.CAR_USER_SERVICE);
+        int[] types = new int[requestSize];
+        for (int i = 0; i < types.length; i++) {
+            types[i] = request.associationTypes.get(i);
+        }
+        GetUserIdentificationAssociationResponse response = carUserManager
+                .getUserIdentificationAssociation(types);
+        if (response == null) {
+            writer.println("null response");
+            return;
+        }
+        String errorMessage = response.getErrorMessage();
+        if (!TextUtils.isEmpty(errorMessage)) {
+            writer.printf("Error message: %s\n", errorMessage);
+        }
+        int[] values = response.getValues();
+        writer.printf("%d associations:\n", values.length);
+        for (int i = 0; i < values.length; i++) {
+            writer.printf("  %s\n", UserIdentificationAssociationValue.toString(values[i]));
         }
     }
 
diff --git a/service/src/com/android/car/hal/UserHalService.java b/service/src/com/android/car/hal/UserHalService.java
index f23b6f3..7b81e0a 100644
--- a/service/src/com/android/car/hal/UserHalService.java
+++ b/service/src/com/android/car/hal/UserHalService.java
@@ -44,11 +44,13 @@
 import android.os.ServiceSpecificException;
 import android.os.UserHandle;
 import android.sysprop.CarProperties;
+import android.text.TextUtils;
 import android.util.EventLog;
 import android.util.Log;
 import android.util.Pair;
 import android.util.Slog;
 import android.util.SparseArray;
+import android.util.SparseBooleanArray;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.car.EventLogTags;
@@ -328,43 +330,76 @@
 
     /**
      * Calls HAL to get the value of the user identifications associated with the given user.
+     *
+     * @return HAL response or {@code null} if it was invalid (for example, mismatch on the
+     * requested number of associations).
+     *
+     * @throws IllegalArgumentException if request is invalid (mismatch on number of associations,
+     *   duplicated association, invalid association type values, etc).
      */
-    @NonNull
+    @Nullable
     public UserIdentificationResponse getUserAssociation(
             @NonNull UserIdentificationGetRequest request) {
         Objects.requireNonNull(request, "request cannot be null");
 
+        // Check that it doesn't have dupes
+        SparseBooleanArray types = new SparseBooleanArray(request.numberAssociationTypes);
+        for (int i = 0; i < request.numberAssociationTypes; i++) {
+            int type = request.associationTypes.get(i);
+            Preconditions.checkArgument(!types.get(type), "type %s found more than once on %s",
+                    UserIdentificationAssociationType.toString(type), request);
+            types.put(type, true);
+        }
+
         if (DBG) Log.d(TAG, "getUserAssociation(): req=" + request);
         VehiclePropValue requestAsPropValue = UserHalHelper.toVehiclePropValue(request);
         EventLog.writeEvent(EventLogTags.CAR_USER_HAL_GET_USER_AUTH_REQ,
                 requestAsPropValue.value.int32Values.toArray());
 
         VehiclePropValue responseAsPropValue = mHal.get(requestAsPropValue);
-        EventLog.writeEvent(EventLogTags.CAR_USER_HAL_GET_USER_AUTH_RESP,
-                responseAsPropValue.value.int32Values.toArray());
+        if (responseAsPropValue == null) {
+            Log.w(TAG, "HAL returned null for request " + requestAsPropValue);
+            return null;
+        }
+
+        if (TextUtils.isEmpty(responseAsPropValue.value.stringValue)) {
+            EventLog.writeEvent(EventLogTags.CAR_USER_HAL_GET_USER_AUTH_RESP,
+                    responseAsPropValue.value.int32Values.toArray());
+        } else {
+            // Must manually append the error message to the array of values
+            int size = responseAsPropValue.value.int32Values.size();
+            Object[] list = new Object[size + 1];
+            responseAsPropValue.value.int32Values.toArray(list);
+            list[list.length - 1] = responseAsPropValue.value.stringValue;
+            EventLog.writeEvent(EventLogTags.CAR_USER_HAL_GET_USER_AUTH_RESP, list);
+        }
+        if (DBG) Log.d(TAG, "getUserAssociation(): responseAsPropValue=" + responseAsPropValue);
 
         UserIdentificationResponse response;
         try {
             response = UserHalHelper.toUserIdentificationGetResponse(responseAsPropValue);
         } catch (IllegalArgumentException e) {
-            throw new IllegalStateException("invalid response from HAL", e);
+            Log.w(TAG, "invalid response from HAL for " + requestAsPropValue, e);
+            return null;
         }
-        if (DBG) Log.d(TAG, "getUserAssociation(): resp=" + response);
+        if (DBG) Log.d(TAG, "getUserAssociation(): response=" + response);
 
         // Validate the response according to the request
         if (response.numberAssociation != request.numberAssociationTypes) {
-            throw new IllegalStateException(
-                    "Wrong number of association types on HAL response (expected "
-                            + request.numberAssociationTypes + "): " + response);
+            Log.w(TAG, "Wrong number of association types on HAL response (expected "
+                    + request.numberAssociationTypes + ") for request " + requestAsPropValue
+                    + ": " + response);
+            return null;
         }
         for (int i = 0; i < request.numberAssociationTypes; i++) {
             int expectedType = request.associationTypes.get(i);
             int actualType = response.associations.get(i).type;
             if (actualType != expectedType) {
-                throw new IllegalStateException("Wrong type on index " + i
-                        + " of HAL response (" + response + "): "
-                        + "expected " + UserIdentificationAssociationType.toString(expectedType)
+                Log.w(TAG, "Wrong type on index " + i + " of HAL response (" + response + ") for "
+                        + "request " + requestAsPropValue + " : expected "
+                        + UserIdentificationAssociationType.toString(expectedType)
                         + ", got " + UserIdentificationAssociationType.toString(actualType));
+                return null;
             }
         }
 
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index f96c19b..398e38b 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -33,6 +33,7 @@
 import android.car.user.CarUserManager.UserLifecycleEvent;
 import android.car.user.CarUserManager.UserLifecycleEventType;
 import android.car.user.CarUserManager.UserLifecycleListener;
+import android.car.user.GetUserIdentificationAssociationResponse;
 import android.car.user.UserSwitchResult;
 import android.car.userlib.CarUserManagerHelper;
 import android.car.userlib.HalCallback;
@@ -44,6 +45,8 @@
 import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponse;
 import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponseAction;
 import android.hardware.automotive.vehicle.V2_0.SwitchUserStatus;
+import android.hardware.automotive.vehicle.V2_0.UserIdentificationGetRequest;
+import android.hardware.automotive.vehicle.V2_0.UserIdentificationResponse;
 import android.hardware.automotive.vehicle.V2_0.UsersInfo;
 import android.location.LocationManager;
 import android.os.Binder;
@@ -70,6 +73,7 @@
 import com.android.internal.car.EventLogTags;
 import com.android.internal.infra.AndroidFuture;
 import com.android.internal.os.IResultReceiver;
+import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FunctionalUtils;
 import com.android.internal.util.Preconditions;
 import com.android.internal.util.UserIcons;
@@ -821,6 +825,51 @@
         });
     }
 
+    @Override
+    public GetUserIdentificationAssociationResponse getUserIdentificationAssociation(int[] types) {
+        Preconditions.checkArgument(!ArrayUtils.isEmpty(types), "must have at least one type");
+        checkManageUsersPermission("getUserIdentificationAssociation");
+
+        int uid = getCallingUid();
+        int userId = UserHandle.getUserId(uid);
+        EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_REQ, uid, userId);
+
+        UserIdentificationGetRequest request = new UserIdentificationGetRequest();
+        request.userInfo.userId = userId;
+        request.userInfo.flags = getHalUserInfoFlags(userId);
+
+        request.numberAssociationTypes = types.length;
+        for (int i = 0; i < types.length; i++) {
+            request.associationTypes.add(types[i]);
+        }
+
+        UserIdentificationResponse halResponse = mHal.getUserAssociation(request);
+        if (halResponse == null) {
+            Log.w(TAG, "getUserIdentificationAssociation(): HAL returned null for "
+                    + Arrays.toString(types));
+            return null;
+        }
+
+        int[] values = new int[halResponse.associations.size()];
+        for (int i = 0; i < values.length; i++) {
+            values[i] = halResponse.associations.get(i).value;
+        }
+        EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_RESP, values.length);
+
+        return new GetUserIdentificationAssociationResponse(halResponse.errorMessage, values);
+    }
+
+    /**
+     * Gets the User HAL flags for the given user.
+     *
+     * @throws IllegalArgumentException if the user does not exist.
+     */
+    private int getHalUserInfoFlags(@UserIdInt int userId) {
+        UserInfo user = mUserManager.getUserInfo(userId);
+        Preconditions.checkArgument(user != null, "no user for id %d", userId);
+        return UserHalHelper.convertFlags(user);
+    }
+
     private void sendResult(@NonNull IResultReceiver receiver, int resultCode,
             @Nullable Bundle resultData) {
         try {
diff --git a/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java b/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java
index d865dfb..24821ba 100644
--- a/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java
@@ -586,6 +586,17 @@
     }
 
     @Test
+    public void testGetUserAssociation_requestWithDuplicatedTypes() {
+        UserIdentificationGetRequest request = new UserIdentificationGetRequest();
+        request.numberAssociationTypes = 2;
+        request.associationTypes.add(KEY_FOB);
+        request.associationTypes.add(KEY_FOB);
+
+        assertThrows(IllegalArgumentException.class,
+                () -> mUserHalService.getUserAssociation(request));
+    }
+
+    @Test
     public void testGetUserAssociation_invalidResponse() {
         VehiclePropValue mockedResponse = new VehiclePropValue();
         mockedResponse.prop = USER_IDENTIFICATION_ASSOCIATION;
@@ -600,8 +611,28 @@
         request.userInfo.flags = 108;
         request.numberAssociationTypes = 1;
         request.associationTypes.add(KEY_FOB);
-        assertThrows(IllegalStateException.class,
-                () -> mUserHalService.getUserAssociation(request));
+
+        assertThat(mUserHalService.getUserAssociation(request)).isNull();
+    }
+
+    @Test
+    public void testGetUserAssociation_nullResponse() {
+        VehiclePropValue mockedResponse = new VehiclePropValue();
+        mockedResponse.prop = USER_IDENTIFICATION_ASSOCIATION;
+        mockedResponse.value.int32Values.add(1); // 1 association
+        mockedResponse.value.int32Values.add(KEY_FOB);
+        mockedResponse.value.int32Values.add(ASSOCIATED_CURRENT_USER);
+        when(mVehicleHal.get(
+                isPropertyWithValues(USER_IDENTIFICATION_ASSOCIATION, 42, 108, 1, KEY_FOB)))
+                        .thenReturn(null);
+
+        UserIdentificationGetRequest request = new UserIdentificationGetRequest();
+        request.userInfo.userId = 42;
+        request.userInfo.flags = 108;
+        request.numberAssociationTypes = 1;
+        request.associationTypes.add(KEY_FOB);
+
+        assertThat(mUserHalService.getUserAssociation(request)).isNull();
     }
 
     @Test
@@ -622,8 +653,8 @@
         request.userInfo.flags = 108;
         request.numberAssociationTypes = 1;
         request.associationTypes.add(KEY_FOB);
-        assertThrows(IllegalStateException.class,
-                () -> mUserHalService.getUserAssociation(request));
+
+        assertThat(mUserHalService.getUserAssociation(request)).isNull();
     }
 
     @Test
@@ -642,8 +673,8 @@
         request.userInfo.flags = 108;
         request.numberAssociationTypes = 1;
         request.associationTypes.add(KEY_FOB);
-        assertThrows(IllegalStateException.class,
-                () -> mUserHalService.getUserAssociation(request));
+
+        assertThat(mUserHalService.getUserAssociation(request)).isNull();
     }
 
     @Test
@@ -653,7 +684,6 @@
         mockedResponse.value.int32Values.add(1); // 1 association
         mockedResponse.value.int32Values.add(KEY_FOB);
         mockedResponse.value.int32Values.add(ASSOCIATED_CURRENT_USER);
-
         when(mVehicleHal.get(
                 isPropertyWithValues(USER_IDENTIFICATION_ASSOCIATION, 42, 108, 1, KEY_FOB)))
                         .thenReturn(mockedResponse);
@@ -663,6 +693,7 @@
         request.userInfo.flags = 108;
         request.numberAssociationTypes = 1;
         request.associationTypes.add(KEY_FOB);
+
         UserIdentificationResponse actualResponse = mUserHalService.getUserAssociation(request);
 
         assertThat(actualResponse.numberAssociation).isEqualTo(1);
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java
index b020b01..2d7c6cd 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java
@@ -27,6 +27,7 @@
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertThrows;
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -35,6 +36,7 @@
 import android.car.ICarUserService;
 import android.car.test.mocks.AbstractExtendedMockitoTestCase;
 import android.car.user.CarUserManager;
+import android.car.user.GetUserIdentificationAssociationResponse;
 import android.car.user.UserSwitchResult;
 import android.content.pm.UserInfo;
 import android.os.RemoteException;
@@ -136,6 +138,38 @@
         assertThat(result.getErrorMessage()).isNull();
     }
 
+    @Test
+    public void testGetUserIdentificationAssociation_nullTypes() throws Exception {
+        assertThrows(IllegalArgumentException.class,
+                () -> mMgr.getUserIdentificationAssociation(null));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_emptyTypes() throws Exception {
+        assertThrows(IllegalArgumentException.class,
+                () -> mMgr.getUserIdentificationAssociation(new int[] {}));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_remoteException() throws Exception {
+        mockHandleRemoteExceptionFromCarServiceWithDefaultValue(mCar);
+        assertThrows(IllegalArgumentException.class,
+                () -> mMgr.getUserIdentificationAssociation(new int[] {}));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_ok() throws Exception {
+        int[] types = new int[] { 4, 8, 15, 16, 23, 42 };
+        GetUserIdentificationAssociationResponse expectedResponse =
+                new GetUserIdentificationAssociationResponse(null, new int[] {});
+        when(mService.getUserIdentificationAssociation(types)).thenReturn(expectedResponse);
+
+        GetUserIdentificationAssociationResponse actualResponse =
+                mMgr.getUserIdentificationAssociation(types);
+
+        assertThat(actualResponse).isSameAs(expectedResponse);
+    }
+
     private void expectServiceSwitchUserSucceeds(@UserIdInt int userId,
             @UserSwitchResult.Status int status, @Nullable String errorMessage)
             throws RemoteException {
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
index cc9181e..3a9e8c7 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
@@ -30,6 +30,7 @@
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.notNull;
 import static org.mockito.ArgumentMatchers.same;
@@ -57,12 +58,14 @@
 import android.car.user.CarUserManager.UserLifecycleEvent;
 import android.car.user.CarUserManager.UserLifecycleEventType;
 import android.car.user.CarUserManager.UserLifecycleListener;
+import android.car.user.GetUserIdentificationAssociationResponse;
 import android.car.user.UserSwitchResult;
 import android.car.userlib.CarUserManagerHelper;
 import android.car.userlib.HalCallback;
 import android.car.userlib.UserHalHelper;
 import android.content.Context;
 import android.content.pm.UserInfo;
+import android.content.pm.UserInfo.UserInfoFlag;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.hardware.automotive.vehicle.V2_0.InitialUserInfoRequestType;
@@ -71,6 +74,9 @@
 import android.hardware.automotive.vehicle.V2_0.SwitchUserResponse;
 import android.hardware.automotive.vehicle.V2_0.SwitchUserStatus;
 import android.hardware.automotive.vehicle.V2_0.UserFlags;
+import android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociation;
+import android.hardware.automotive.vehicle.V2_0.UserIdentificationGetRequest;
+import android.hardware.automotive.vehicle.V2_0.UserIdentificationResponse;
 import android.hardware.automotive.vehicle.V2_0.UsersInfo;
 import android.location.LocationManager;
 import android.os.Bundle;
@@ -92,6 +98,7 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatcher;
 import org.mockito.Captor;
 import org.mockito.Mock;
 
@@ -1034,6 +1041,68 @@
         assertThat(mCarUserService.isUserHalSupported()).isTrue();
     }
 
+    @Test
+    public void testGetUserIdentificationAssociation_nullTypes() throws Exception {
+        assertThrows(IllegalArgumentException.class,
+                () -> mCarUserService.getUserIdentificationAssociation(null));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_emptyTypes() throws Exception {
+        assertThrows(IllegalArgumentException.class,
+                () -> mCarUserService.getUserIdentificationAssociation(new int[] {}));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_noPermission() throws Exception {
+        mockManageUsersPermission(android.Manifest.permission.MANAGE_USERS, false);
+        assertThrows(SecurityException.class,
+                () -> mCarUserService.getUserIdentificationAssociation(new int[] { 42 }));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_noSuchUser() throws Exception {
+        // Should fail because we're not mocking UserManager.getUserInfo() to set the flag
+        assertThrows(IllegalArgumentException.class,
+                () -> mCarUserService.getUserIdentificationAssociation(new int[] { 42 }));
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_service_returnNull() throws Exception {
+        // Must use the real user id - and not mock it - as the service will infer the id from
+        // the Binder call - it's not worth the effort of mocking that.
+        int currentUserId = ActivityManager.getCurrentUser();
+        Log.d(TAG, "testGetUserIdentificationAssociation_ok(): current user is " + currentUserId);
+        UserInfo currentUser = mockGetUserInfo(currentUserId, UserInfo.FLAG_ADMIN);
+
+        // Not mocking service call, so it will return null
+
+        GetUserIdentificationAssociationResponse response = mCarUserService
+                .getUserIdentificationAssociation(new int[] { 108 });
+
+        assertThat(response).isNull();
+    }
+
+    @Test
+    public void testGetUserIdentificationAssociation_ok() throws Exception {
+        // Must use the real user id - and not mock it - as the service will infer the id from
+        // the Binder call - it's not worth the effort of mocking that.
+        int currentUserId = ActivityManager.getCurrentUser();
+        Log.d(TAG, "testGetUserIdentificationAssociation_ok(): current user is " + currentUserId);
+        UserInfo currentUser = mockGetUserInfo(currentUserId, UserInfo.FLAG_ADMIN);
+
+        int[] types = new int[] { 1, 2, 3 };
+        mockHalGetUserIdentificationAssociation(currentUser, types, new int[] { 10, 20, 30 },
+                "D'OH!");
+
+        GetUserIdentificationAssociationResponse response = mCarUserService
+                .getUserIdentificationAssociation(types);
+
+        assertThat(response.getValues()).asList().containsExactly(10, 20, 30)
+                .inOrder();
+        assertThat(response.getErrorMessage()).isEqualTo("D'OH!");
+    }
+
     @NonNull
     private UserSwitchResult getUserSwitchResult() throws Exception {
         return getResult(mUserSwitchFuture);
@@ -1132,6 +1201,25 @@
                 notNull());
     }
 
+    private void mockHalGetUserIdentificationAssociation(@NonNull UserInfo user,
+            @NonNull int[] types, @NonNull int[] values,  @Nullable String errorMessage) {
+        assertWithMessage("mismatch on number of types and values").that(types.length)
+                .isEqualTo(values.length);
+
+        UserIdentificationResponse response = new UserIdentificationResponse();
+        response.numberAssociation = types.length;
+        response.errorMessage = errorMessage;
+        for (int i = 0; i < types.length; i++) {
+            UserIdentificationAssociation association = new UserIdentificationAssociation();
+            association.type = types[i];
+            association.value = values[i];
+            response.associations.add(association);
+        }
+
+        when(mUserHal.getUserAssociation(isUserIdentificationGetRequest(user, types)))
+                .thenReturn(response);
+    }
+
     private void mockManageUsersPermission(String permission, boolean granted) {
         int result;
         if (granted) {
@@ -1362,4 +1450,85 @@
     private void sendUserSwitchingEvent(@UserIdInt int userId) {
         sendUserLifecycleEvent(userId, CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING);
     }
+
+    @NonNull
+    private static UserIdentificationGetRequest isUserIdentificationGetRequest(
+            @NonNull UserInfo user, @NonNull int[] types) {
+        return argThat(new UserIdentificationGetRequestMatcher(user, types));
+    }
+
+    private static class UserIdentificationGetRequestMatcher implements
+            ArgumentMatcher<UserIdentificationGetRequest> {
+
+        private static final String MY_TAG =
+                UserIdentificationGetRequestMatcher.class.getSimpleName();
+
+        private final @UserIdInt int mUserId;
+        private final int mHalFlags;
+        private final @NonNull int[] mTypes;
+
+        private UserIdentificationGetRequestMatcher(@NonNull UserInfo user, @NonNull int[] types) {
+            mUserId = user.id;
+            mHalFlags = UserHalHelper.convertFlags(user);
+            mTypes = types;
+        }
+
+        @Override
+        public boolean matches(UserIdentificationGetRequest argument) {
+            if (argument == null) {
+                Log.w(MY_TAG, "null argument");
+                return false;
+            }
+            if (argument.userInfo.userId != mUserId) {
+                Log.w(MY_TAG, "wrong user id on " + argument + "; expected " + mUserId);
+                return false;
+            }
+            if (argument.userInfo.flags != mHalFlags) {
+                Log.w(MY_TAG, "wrong flags on " + argument + "; expected " + mHalFlags);
+                return false;
+            }
+            if (argument.numberAssociationTypes != mTypes.length) {
+                Log.w(MY_TAG, "wrong numberAssociationTypes on " + argument + "; expected "
+                        + mTypes.length);
+                return false;
+            }
+            if (argument.associationTypes.size() != mTypes.length) {
+                Log.w(MY_TAG, "wrong associationTypes size on " + argument + "; expected "
+                        + mTypes.length);
+                return false;
+            }
+            for (int i = 0; i < mTypes.length; i++) {
+                if (argument.associationTypes.get(i) != mTypes[i]) {
+                    Log.w(MY_TAG, "wrong association type on index " + i + " on " + argument
+                            + "; expected types: " + Arrays.toString(mTypes));
+                    return false;
+                }
+            }
+            Log.d(MY_TAG, "Good News, Everyone! " + argument + " matches " + this);
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            return "isUserIdentificationGetRequest(userId=" + mUserId + ", flags="
+                    + UserHalHelper.userFlagsToString(mHalFlags) + ", types="
+                    + Arrays.toString(mTypes) + ")";
+        }
+    }
+
+    // TODO(b/149099817): Move code below to common place
+
+    /**
+     * Mocks {@code UserManager.getuserInfo(userId)} to return a {@link UserInfo} with the given
+     * {@code flags}.
+     */
+    @NonNull
+    private UserInfo mockGetUserInfo(@UserIdInt int userId, @UserInfoFlag int flags) {
+        UserInfo userInfo = new UserInfo();
+        userInfo.id = userId;
+        userInfo.flags = flags;
+        when(mMockedUserManager.getUserInfo(userId)).thenReturn(userInfo);
+
+        return userInfo;
+    }
 }