From b480dead6a541576446e7d5aab81775b760db96c Mon Sep 17 00:00:00 2001 From: Devin Moore Date: Wed, 13 Dec 2023 18:51:12 +0000 Subject: [PATCH 1/2] Revert^2 "Add isHidlSupported Java API" 109b135211ae030e8dd19ed79090cb29e55a7636 Change-Id: If68af33e95c33f8f8d9c7d66416c40bc5536a7c1 --- core/java/android/os/HidlSupport.java | 7 +++++++ core/jni/android_os_HidlSupport.cpp | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/java/android/os/HidlSupport.java b/core/java/android/os/HidlSupport.java index 91b796aba655..77160557f5c2 100644 --- a/core/java/android/os/HidlSupport.java +++ b/core/java/android/os/HidlSupport.java @@ -218,6 +218,13 @@ public class HidlSupport { @SystemApi public static native int getPidIfSharable(); + /** + * Return true if HIDL is supported on this device and false if not. + * + * @hide + */ + public static native boolean isHidlSupported(); + /** @hide */ public HidlSupport() {} } diff --git a/core/jni/android_os_HidlSupport.cpp b/core/jni/android_os_HidlSupport.cpp index e3602d8f5c72..3e51e9315d89 100644 --- a/core/jni/android_os_HidlSupport.cpp +++ b/core/jni/android_os_HidlSupport.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include "core_jni_helpers.h" @@ -24,8 +25,13 @@ static jint android_os_HidlSupport_getPidIfSharable(JNIEnv*, jclass) { return android::hardware::details::getPidIfSharable(); } +static jboolean android_os_HidlSupport_isHidlSupported(JNIEnv*, jclass) { + return android::hardware::isHidlSupported(); +} + static const JNINativeMethod gHidlSupportMethods[] = { - {"getPidIfSharable", "()I", (void*)android_os_HidlSupport_getPidIfSharable}, + {"getPidIfSharable", "()I", (void*)android_os_HidlSupport_getPidIfSharable}, + {"isHidlSupported", "()Z", (void*)android_os_HidlSupport_isHidlSupported}, }; const char* const kHidlSupportPathName = "android/os/HidlSupport"; From 25cbbbc925d61234f89562918e625792ecf3d913 Mon Sep 17 00:00:00 2001 From: Devin Moore Date: Wed, 13 Dec 2023 18:51:12 +0000 Subject: [PATCH 2/2] Revert "Revert "Create a HwNoService class that fakes hwservicem..." Revert submission 2870060-revert-2866127-HwNoService-EIABNUAXKH Reason for revert: Fixed robolectric tests with aosp/2874076 Reverted changes: /q/submissionid:2870060-revert-2866127-HwNoService-EIABNUAXKH Change-Id: I0228877982f55ed8f53f5770ee43de7fad14922f --- core/java/android/os/HwBinder.java | 12 +++++++ core/java/android/os/HwNoService.java | 52 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 core/java/android/os/HwNoService.java diff --git a/core/java/android/os/HwBinder.java b/core/java/android/os/HwBinder.java index feed20800fd4..bc19655d1618 100644 --- a/core/java/android/os/HwBinder.java +++ b/core/java/android/os/HwBinder.java @@ -18,6 +18,7 @@ package android.os; import android.annotation.SystemApi; import android.compat.annotation.UnsupportedAppUsage; +import android.util.Log; import libcore.util.NativeAllocationRegistry; @@ -78,6 +79,17 @@ public abstract class HwBinder implements IHwBinder { String iface, String serviceName) throws RemoteException, NoSuchElementException { + if (!HidlSupport.isHidlSupported() + && (iface.equals("android.hidl.manager@1.0::IServiceManager") + || iface.equals("android.hidl.manager@1.1::IServiceManager") + || iface.equals("android.hidl.manager@1.2::IServiceManager"))) { + Log.i( + TAG, + "Replacing Java hwservicemanager with a fake HwNoService" + + " because HIDL is not supported on this device."); + return new HwNoService(); + } + return getService(iface, serviceName, false /* retry */); } /** diff --git a/core/java/android/os/HwNoService.java b/core/java/android/os/HwNoService.java new file mode 100644 index 000000000000..117c3ad7ee48 --- /dev/null +++ b/core/java/android/os/HwNoService.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2023 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.os; + +/** + * A fake hwservicemanager that is used locally when HIDL isn't supported on the device. + * + * @hide + */ +final class HwNoService implements IHwBinder, IHwInterface { + /** @hide */ + @Override + public void transact(int code, HwParcel request, HwParcel reply, int flags) {} + + /** @hide */ + @Override + public IHwInterface queryLocalInterface(String descriptor) { + return new HwNoService(); + } + + /** @hide */ + @Override + public boolean linkToDeath(DeathRecipient recipient, long cookie) { + return true; + } + + /** @hide */ + @Override + public boolean unlinkToDeath(DeathRecipient recipient) { + return true; + } + + /** @hide */ + @Override + public IHwBinder asBinder() { + return this; + } +}