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

Commit 1ac34063 authored by Yifan Hong's avatar Yifan Hong Committed by android-build-merger
Browse files

Merge "Add Java API for libvintf." into oc-dev am: c223d70e

am: 38bf613c

Change-Id: I3d2d7c3db36395aa3fcfcbb2396619c46c353d05
parents 27a931eb 38bf613c
Loading
Loading
Loading
Loading
+64 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import java.util.ArrayList;

import android.util.Log;

/** @hide */
public class VintfObject {

    private static final String LOG_TAG = "VintfObject";

    /**
     * Slurps all device information (both manifests)
     * and report it.
     * If any error in getting one of the manifests, it is not included in
     * the list.
     */
    public static String[] report() {
        ArrayList<String> ret = new ArrayList<>();
        put(ret, getDeviceManifest(), "device manifest");
        put(ret, getFrameworkManifest(), "framework manifest");
        return ret.toArray(new String[0]);
    }

    /**
     * Verify that the given metadata for an OTA package is compatible with
     * this device.
     *
     * @param packageInfo a list of serialized form of HalMaanifest's /
     * CompatibilityMatri'ces (XML).
     * @return = 0 if success (compatible)
     *         > 0 if incompatible
     *         < 0 if any error (mount partition fails, illformed XML, etc.)
     */
    public static native int verify(String[] packageInfo);

    // return null if any error, otherwise XML string.
    private static native String getDeviceManifest();
    private static native String getFrameworkManifest();

    private static void put(ArrayList<String> list, String content, String message) {
        if (content == null || content.length() == 0) {
            Log.e(LOG_TAG, "Cannot get;" + message + "; check native logs for details.");
            return;
        }
        list.add(content);
    }
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -96,6 +96,7 @@ LOCAL_SRC_FILES:= \
    android_os_SystemProperties.cpp \
    android_os_SystemProperties.cpp \
    android_os_Trace.cpp \
    android_os_Trace.cpp \
    android_os_UEventObserver.cpp \
    android_os_UEventObserver.cpp \
    android_os_VintfObject.cpp \
    android_net_LocalSocketImpl.cpp \
    android_net_LocalSocketImpl.cpp \
    android_net_NetUtils.cpp \
    android_net_NetUtils.cpp \
    android_net_TrafficStats.cpp \
    android_net_TrafficStats.cpp \
+2 −0
Original line number Original line Diff line number Diff line
@@ -159,6 +159,7 @@ extern int register_android_os_HwRemoteBinder(JNIEnv *env);
extern int register_android_os_MessageQueue(JNIEnv* env);
extern int register_android_os_MessageQueue(JNIEnv* env);
extern int register_android_os_Parcel(JNIEnv* env);
extern int register_android_os_Parcel(JNIEnv* env);
extern int register_android_os_SELinux(JNIEnv* env);
extern int register_android_os_SELinux(JNIEnv* env);
extern int register_android_os_VintfObject(JNIEnv *env);
extern int register_android_os_seccomp(JNIEnv* env);
extern int register_android_os_seccomp(JNIEnv* env);
extern int register_android_os_SystemProperties(JNIEnv *env);
extern int register_android_os_SystemProperties(JNIEnv *env);
extern int register_android_os_SystemClock(JNIEnv* env);
extern int register_android_os_SystemClock(JNIEnv* env);
@@ -1302,6 +1303,7 @@ static const RegJNIRec gRegJNI[] = {
    REG_JNI(register_android_os_HwBlob),
    REG_JNI(register_android_os_HwBlob),
    REG_JNI(register_android_os_HwParcel),
    REG_JNI(register_android_os_HwParcel),
    REG_JNI(register_android_os_HwRemoteBinder),
    REG_JNI(register_android_os_HwRemoteBinder),
    REG_JNI(register_android_os_VintfObject),
    REG_JNI(register_android_nio_utils),
    REG_JNI(register_android_nio_utils),
    REG_JNI(register_android_graphics_Canvas),
    REG_JNI(register_android_graphics_Canvas),
    REG_JNI(register_android_graphics_Graphics),
    REG_JNI(register_android_graphics_Graphics),
+82 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2012 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.
 */

#define LOG_TAG "VintfObject"
//#define LOG_NDEBUG 0

#include <JNIHelp.h>
#include <vintf/VintfObject.h>
#include <vintf/parse_xml.h>

#include "core_jni_helpers.h"

namespace android {

using vintf::HalManifest;
using vintf::RuntimeInfo;
using vintf::VintfObject;
using vintf::gHalManifestConverter;

static jstring android_os_VintfObject_getDeviceManifest(JNIEnv* env, jclass clazz)
{
    const HalManifest *manifest = VintfObject::GetDeviceHalManifest();
    if (manifest == nullptr) {
        return nullptr;
    }
    std::string xml = gHalManifestConverter(*manifest);
    return env->NewStringUTF(xml.c_str());
}

static jstring android_os_VintfObject_getFrameworkManifest(JNIEnv* env, jclass clazz)
{
    const HalManifest *manifest = VintfObject::GetFrameworkHalManifest();
    if (manifest == nullptr) {
        return nullptr;
    }
    std::string xml = gHalManifestConverter(*manifest);
    return env->NewStringUTF(xml.c_str());
}

static jint android_os_VintfObject_verify(JNIEnv *env, jclass clazz, jobjectArray packageInfo) {
    size_t count = env->GetArrayLength(packageInfo);
    std::vector<std::string> cPackageInfo{count};
    for (size_t i = 0; i < count; ++i) {
        jstring element = (jstring)env->GetObjectArrayElement(packageInfo, i);
        const char *cString = env->GetStringUTFChars(element, NULL /* isCopy */);
        cPackageInfo[i] = cString;
        env->ReleaseStringUTFChars(element, cString);
    }
    int32_t status = VintfObject::CheckCompatibility(cPackageInfo, false /* mount */);
    return status;
}

// ----------------------------------------------------------------------------

static const JNINativeMethod gVintfObjectMethods[] = {
    {"getDeviceManifest",    "()Ljava/lang/String;",   (void*)android_os_VintfObject_getDeviceManifest},
    {"getFrameworkManifest", "()Ljava/lang/String;",   (void*)android_os_VintfObject_getFrameworkManifest},
    {"verify",               "([Ljava/lang/String;)I", (void*)android_os_VintfObject_verify},
};

const char* const kVintfObjectPathName = "android/os/VintfObject";

int register_android_os_VintfObject(JNIEnv* env)
{
    return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods,
            NELEM(gVintfObjectMethods));
}

};
+30 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import junit.framework.Assert;
import junit.framework.TestCase;

public class VintfObjectTest extends TestCase {
    public void testReport() {
        String[] xmls = VintfObject.report();
        assertTrue(xmls.length > 0);
        // From /system/manifest.xml
        assertTrue(String.join("", xmls).contains(
                "<manifest version=\"1.0\" type=\"framework\">"));
    }
}