Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e49b3684 authored by Steven Moreland's avatar Steven Moreland Committed by Gerrit Code Review
Browse files

Merge "example vendor app"

parents a4f9873b 0385dac9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ aidl_interface {
    stability: "vintf",
    backend: {
        java: {
            sdk_version: "module_current",
            sdk_version: "system_current",
        },
    },
    versions: [
+34 −0
Original line number Diff line number Diff line
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__"],
}
+7 −0
Original line number Diff line number Diff line
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="example.vib">
    <application>
        <activity android:name=".MyActivity"/>
    </application>
</manifest>
+54 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
+48 −0
Original line number Diff line number Diff line
/*
 * 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 <android/binder_auto_utils.h>
#include <android/binder_ibinder_jni.h>
#include <android/binder_manager.h>
#include <jni.h>
#include <log/log.h>

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;
}
Loading