diff --git a/vibrator/aidl/Android.bp b/vibrator/aidl/Android.bp index 86ef027b92..c5936e3931 100644 --- a/vibrator/aidl/Android.bp +++ b/vibrator/aidl/Android.bp @@ -17,7 +17,7 @@ aidl_interface { stability: "vintf", backend: { java: { - sdk_version: "module_current", + sdk_version: "system_current", }, }, versions: [ diff --git a/vibrator/aidl/default/example_vendor_java_client/Android.bp b/vibrator/aidl/default/example_vendor_java_client/Android.bp new file mode 100644 index 0000000000..f615cb1579 --- /dev/null +++ b/vibrator/aidl/default/example_vendor_java_client/Android.bp @@ -0,0 +1,34 @@ +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "hardware_interfaces_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["hardware_interfaces_license"], +} + +cc_library { + name: "libexample_vib_getter", + srcs: ["getter.cpp"], + vendor: true, + shared_libs: [ + "liblog", + "libbinder_ndk", + ], + header_libs: ["jni_headers"], + stl: "c++_shared", + visibility: [":__subpackages__"], +} + +android_app { + name: "ExampleVibratorJavaVendorClient", + privileged: true, + vendor: true, + static_libs: ["android.hardware.vibrator-V1-java"], + jni_libs: ["libexample_vib_getter"], + jarjar_rules: "jarjar.txt", + stl: "c++_shared", + srcs: ["example/vib/MyActivity.java"], + sdk_version: "system_current", + visibility: [":__subpackages__"], +} diff --git a/vibrator/aidl/default/example_vendor_java_client/AndroidManifest.xml b/vibrator/aidl/default/example_vendor_java_client/AndroidManifest.xml new file mode 100644 index 0000000000..0561066daa --- /dev/null +++ b/vibrator/aidl/default/example_vendor_java_client/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/vibrator/aidl/default/example_vendor_java_client/example/vib/MyActivity.java b/vibrator/aidl/default/example_vendor_java_client/example/vib/MyActivity.java new file mode 100644 index 0000000000..aadce8ed00 --- /dev/null +++ b/vibrator/aidl/default/example_vendor_java_client/example/vib/MyActivity.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2022 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 example.vib; + +import android.app.Activity; +import android.hardware.vibrator.IVibrator; +import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; + +public class MyActivity extends Activity { + private static native IBinder gimme(String name); + + @Override + public void onCreate(Bundle b) { + super.onCreate(b); + System.loadLibrary("example_vib_getter"); + + // There is no API to get ahold of a Stable AIDL service from a vendor app + // in Java. This is because this is not the recommended way to get ahold + // of functionality in Android. The Android API Council recommendation is to + // implement uses-library APIs in the system/system_ext partition which add + // new APIs. AIDL as an API in Java is not recommended or supported way to + // communicate by apps - the recommendation is to use Java APIs. However, + // there also exists a large number of vendor apps which are coupled with + // hardware-specific code, and are therefore on the vendor partition. A + // large number of these use HIDL, and this is how they can continue to + // use that structure with AIDL. + IVibrator v = + IVibrator.Stub.asInterface(gimme("android.hardware.vibrator.IVibrator/default")); + + try { + v.on(100 /*ms*/, null /*cb*/); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + + finish(); + } +} diff --git a/vibrator/aidl/default/example_vendor_java_client/getter.cpp b/vibrator/aidl/default/example_vendor_java_client/getter.cpp new file mode 100644 index 0000000000..6115445a72 --- /dev/null +++ b/vibrator/aidl/default/example_vendor_java_client/getter.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 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. + */ + +#include +#include +#include +#include +#include + +extern "C" JNIEXPORT jobject JNICALL +Java_example_vib_MyActivity_gimme__Ljava_lang_String_2(JNIEnv* env, jclass /**/, jstring str) { + ALOGI("%s", __func__); + + // Best practice is probably libnativehelper ScopedUtfChars or + // libbase ScopeGuard (for platform code), but this is with minimal + // dependencies. + const char* name = env->GetStringUTFChars(str, nullptr); + + ALOGI("example vib gimme %s", name); + + jobject jbinder = nullptr; + + // Java does not have vendor variants. It's only safe to pass a service when + // 'vendor: true' if it is @VintfStability. + if (AServiceManager_isDeclared(name)) { + ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_waitForService(name)); + jbinder = AIBinder_toJavaBinder(env, binder.get()); + } else { + ALOGI("not declared"); + } + + env->ReleaseStringUTFChars(str, name); + + return jbinder; +} diff --git a/vibrator/aidl/default/example_vendor_java_client/jarjar.txt b/vibrator/aidl/default/example_vendor_java_client/jarjar.txt new file mode 100644 index 0000000000..e7613a05e9 --- /dev/null +++ b/vibrator/aidl/default/example_vendor_java_client/jarjar.txt @@ -0,0 +1,2 @@ +rule android.hardware.** example.vib.ah.@1 +