upload android base code part6
This commit is contained in:
parent
421e214c7d
commit
4e516ec6ed
35396 changed files with 9188716 additions and 0 deletions
1
android/device/sample/frameworks/Android.mk
Normal file
1
android/device/sample/frameworks/Android.mk
Normal file
|
@ -0,0 +1 @@
|
|||
include $(call all-subdir-makefiles)
|
60
android/device/sample/frameworks/PlatformLibrary/Android.mk
Normal file
60
android/device/sample/frameworks/PlatformLibrary/Android.mk
Normal file
|
@ -0,0 +1,60 @@
|
|||
#
|
||||
# Copyright (C) 2008 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.
|
||||
#
|
||||
|
||||
ifneq ($(TARGET_BUILD_JAVA_SUPPORT_LEVEL),)
|
||||
|
||||
# This makefile shows how to build your own shared library that can be
|
||||
# shipped on the system of a phone, and included additional examples of
|
||||
# including JNI code with the library and writing client applications against it.
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
# the library
|
||||
# ============================================================
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_SRC_FILES := \
|
||||
$(call all-subdir-java-files)
|
||||
|
||||
LOCAL_MODULE_TAGS := optional
|
||||
|
||||
# This is the target being built.
|
||||
LOCAL_MODULE:= com.example.android.platform_library
|
||||
|
||||
include $(BUILD_JAVA_LIBRARY)
|
||||
|
||||
# the documentation
|
||||
# ============================================================
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_SRC_FILES := $(call all-subdir-java-files) $(call all-subdir-html-files)
|
||||
|
||||
LOCAL_MODULE:= platform_library
|
||||
LOCAL_DROIDDOC_OPTIONS :=
|
||||
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
|
||||
LOCAL_DROIDDOC_USE_STANDARD_DOCLET := true
|
||||
|
||||
include $(BUILD_DROIDDOC)
|
||||
|
||||
# The JNI component
|
||||
# ============================================================
|
||||
# Also build all of the sub-targets under this one: the library's
|
||||
# associated JNI code, and a sample client of the library.
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
include $(call all-makefiles-under,$(LOCAL_PATH))
|
||||
|
||||
endif # JAVA_SUPPORT
|
74
android/device/sample/frameworks/PlatformLibrary/README.txt
Normal file
74
android/device/sample/frameworks/PlatformLibrary/README.txt
Normal file
|
@ -0,0 +1,74 @@
|
|||
Platform Library Example
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
This directory contains a full example of writing your own Android platform
|
||||
shared library, without changing the Android framework. It also shows how to
|
||||
write JNI code for incorporating native code into the library, and a client
|
||||
application that uses the library.
|
||||
|
||||
This example is ONLY for people working with the open source platform to
|
||||
create a system image that will be delivered on a device which will include
|
||||
a custom library as shown here. It can not be used to create a third party
|
||||
shared library, which is not currently supported in Android.
|
||||
|
||||
To declare your library to the framework, you must place a file with a .xml
|
||||
extension in the /system/etc/permissions directory with the following contents:
|
||||
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<permissions>
|
||||
<library name="com.example.android.platform_library"
|
||||
file="/system/framework/com.example.android.platform_library.jar"/>
|
||||
</permissions>
|
||||
|
||||
There are three major parts of this example, supplying three distinct
|
||||
build targets and corresponding build outputs:
|
||||
|
||||
|
||||
com.example.android.platform_library
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The top-level Android.mk defines the rules to build the shared library itself,
|
||||
whose target is "com.example.android.platform_library". The code for this
|
||||
library lives under java/.
|
||||
|
||||
Note that the product for this library is a raw .jar file, NOT a .apk, which
|
||||
means there is no manifest or resources associated with the library.
|
||||
Unfortunately this means that if you need any resources for the library, such
|
||||
as drawables or layout files, you will need to add these to the core framework
|
||||
resources under frameworks/base/res. Please make sure when doing this that
|
||||
you do not make any of these resources public, they should not become part of
|
||||
the Android API. In the future we will allow shared libraries to have their
|
||||
own resources.
|
||||
|
||||
Other than that, the library is very straight-forward, and you can write
|
||||
basically whatever code you want. You can also put code in other Java
|
||||
namespaces -- the namespace given in the <library> tag above is just the
|
||||
public unique name by which clients will link to your library, but once this
|
||||
link happens all of the Java namespaces in that library will be available
|
||||
to the client.
|
||||
|
||||
|
||||
libplatform_library_jni
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This is an optional example of how to write JNI code associated with a
|
||||
shared library. This code lives under jni/. The jni/Android.mk file defines
|
||||
the rules for building the final .so in which the code lives. This example
|
||||
provides everything needed to hook up the native code with the Java library
|
||||
and call through to it, plus a very simple JNI call.
|
||||
|
||||
|
||||
PlatformLibraryClient
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This shows an example of how you can write client applications for your new
|
||||
shared library. This code lives under client/. Note that the example is
|
||||
simply a regular Android .apk, like all of the other .apks created by the
|
||||
build system. The only two special things needed to use your library are:
|
||||
|
||||
- A LOCAL_JAVA_LIBRARIES line in the Android.mk to have the build system link
|
||||
against your shared library.
|
||||
|
||||
- A <uses-library> line in the AndroidManifest.xml to have the runtime load
|
||||
your library into the application.
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<permissions>
|
||||
<library name="com.example.android.platform_library"
|
||||
file="/system/framework/com.example.android.platform_library.jar"/>
|
||||
</permissions>
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (C) 2008 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 com.example.android.platform_library;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public final class PlatformLibrary {
|
||||
static {
|
||||
/*
|
||||
* Load the library. If it's already loaded, this does nothing.
|
||||
*/
|
||||
System.loadLibrary("platform_library_jni");
|
||||
}
|
||||
|
||||
private int mJniInt = -1;
|
||||
|
||||
public PlatformLibrary() {}
|
||||
|
||||
/*
|
||||
* Test native methods.
|
||||
*/
|
||||
public int getInt(boolean bad) {
|
||||
/* this alters mJniInt */
|
||||
int result = getJniInt(bad);
|
||||
|
||||
/* reverse a string, for no very good reason */
|
||||
String reverse = reverseString("Android!");
|
||||
|
||||
Log.i("PlatformLibrary", "getInt: " + result + ", '" + reverse + "'");
|
||||
|
||||
return mJniInt;
|
||||
}
|
||||
|
||||
/*
|
||||
* Simple method, called from native code.
|
||||
*/
|
||||
private static void yodel(String msg) {
|
||||
Log.d("PlatformLibrary", "yodel: " + msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* Trivial native method call. If "bad" is true, this will throw an
|
||||
* exception.
|
||||
*/
|
||||
native private int getJniInt(boolean bad);
|
||||
|
||||
/*
|
||||
* Native method that returns a new string that is the reverse of
|
||||
* the original. This also calls yodel().
|
||||
*/
|
||||
native private static String reverseString(String str);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#
|
||||
# Copyright (C) 2008 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.
|
||||
#
|
||||
|
||||
# This makefile supplies the rules for building a library of JNI code for
|
||||
# use by our example platform shared library.
|
||||
|
||||
LOCAL_PATH:= $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_MODULE_TAGS := optional
|
||||
|
||||
# This is the target being built.
|
||||
LOCAL_MODULE:= libplatform_library_jni
|
||||
|
||||
# All of the source files that we will compile.
|
||||
LOCAL_SRC_FILES:= \
|
||||
PlatformLibrary.cpp
|
||||
|
||||
# All of the shared libraries we link against.
|
||||
LOCAL_SHARED_LIBRARIES := \
|
||||
libnativehelper \
|
||||
libcutils \
|
||||
libutils \
|
||||
liblog
|
||||
|
||||
# No static libraries.
|
||||
LOCAL_STATIC_LIBRARIES :=
|
||||
|
||||
# Also need the JNI headers.
|
||||
LOCAL_C_INCLUDES += \
|
||||
$(JNI_H_INCLUDE)
|
||||
|
||||
# No specia compiler flags.
|
||||
LOCAL_CFLAGS +=
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
|
@ -0,0 +1,273 @@
|
|||
/*
|
||||
* Copyright (C) 2008 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 "PlatformLibrary"
|
||||
#include "utils/Log.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "jni.h"
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* Field/method IDs and class object references.
|
||||
*
|
||||
* You should not need to store the JNIEnv pointer in here. It is
|
||||
* thread-specific and will be passed back in on every call.
|
||||
*/
|
||||
static struct {
|
||||
jclass platformLibraryClass;
|
||||
jfieldID jniInt;
|
||||
jmethodID yodel;
|
||||
} gCachedState;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* Helper function to throw an arbitrary exception.
|
||||
*
|
||||
* Takes the exception class name, a format string, and one optional integer
|
||||
* argument (useful for including an error code, perhaps from errno).
|
||||
*/
|
||||
static void throwException(JNIEnv* env, const char* ex, const char* fmt,
|
||||
int data) {
|
||||
|
||||
if (jclass cls = env->FindClass(ex)) {
|
||||
if (fmt != NULL) {
|
||||
char msg[1000];
|
||||
snprintf(msg, sizeof(msg), fmt, data);
|
||||
env->ThrowNew(cls, msg);
|
||||
} else {
|
||||
env->ThrowNew(cls, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* This is usually not necessary -- local references are released
|
||||
* automatically when the native code returns to the VM. It's
|
||||
* required if the code doesn't actually return, e.g. it's sitting
|
||||
* in a native event loop.
|
||||
*/
|
||||
env->DeleteLocalRef(cls);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Trivial sample method.
|
||||
*
|
||||
* If "bad" is true, this throws an exception. Otherwise, this sets the
|
||||
* "mJniInt" field to 42 and returns 24.
|
||||
*/
|
||||
static jint PlatformLibrary_getJniInt(JNIEnv* env, jobject thiz, jboolean bad) {
|
||||
if (bad) {
|
||||
throwException(env, "java/lang/IllegalStateException",
|
||||
"you are bad", 0);
|
||||
return 0; /* return value will be ignored */
|
||||
}
|
||||
env->SetIntField(thiz, gCachedState.jniInt, 42);
|
||||
return (jint)24;
|
||||
}
|
||||
|
||||
/*
|
||||
* A more complex sample method.
|
||||
*
|
||||
* This takes a String as an argument, and returns a new String with
|
||||
* characters in reverse order. The new string is passed to another method.
|
||||
* This demonstrates basic String manipulation functions and method
|
||||
* invocation.
|
||||
*
|
||||
* This method is declared "static", so there's no "this" pointer; instead,
|
||||
* we get a pointer to the class object.
|
||||
*/
|
||||
static jstring PlatformLibrary_reverseString(JNIEnv* env, jclass clazz,
|
||||
jstring str) {
|
||||
|
||||
if (str == NULL) {
|
||||
throwException(env, "java/lang/NullPointerException", NULL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a pointer to the string's UTF-16 character data. The data
|
||||
* may be a copy or a pointer to the original. Since String data
|
||||
* is immutable, we're not allowed to touch it.
|
||||
*/
|
||||
const jchar* strChars = env->GetStringChars(str, NULL);
|
||||
if (strChars == NULL) {
|
||||
/* something went wrong */
|
||||
ALOGW("Couldn't get string chars\n");
|
||||
return NULL;
|
||||
}
|
||||
jsize strLength = env->GetStringLength(str);
|
||||
|
||||
/*
|
||||
* Write a progress message to the log. Log messages are UTF-8, so
|
||||
* we want to convert the string to show it.
|
||||
*/
|
||||
const char* printable = env->GetStringUTFChars(str, NULL);
|
||||
if (printable != NULL) {
|
||||
ALOGD("Reversing string '%s'\n", printable);
|
||||
env->ReleaseStringUTFChars(str, printable);
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy the characters to temporary storage, reversing as we go.
|
||||
*/
|
||||
jchar tempChars[strLength];
|
||||
for (int i = 0; i < strLength; i++) {
|
||||
tempChars[i] = strChars[strLength -1 -i];
|
||||
}
|
||||
|
||||
/*
|
||||
* Release the original String. That way, if something fails later on,
|
||||
* we don't have to worry about this leading to a memory leak.
|
||||
*/
|
||||
env->ReleaseStringChars(str, strChars);
|
||||
strChars = NULL; /* this pointer no longer valid */
|
||||
|
||||
/*
|
||||
* Create a new String with the chars.
|
||||
*/
|
||||
jstring result = env->NewString(tempChars, strLength);
|
||||
if (result == NULL) {
|
||||
ALOGE("NewString failed\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now let's do something with it. We already have the methodID for
|
||||
* "yodel", so we can invoke it directly. It's in our class, so we
|
||||
* can use the Class object reference that was passed in.
|
||||
*/
|
||||
env->CallStaticVoidMethod(clazz, gCachedState.yodel, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* Array of methods.
|
||||
*
|
||||
* Each entry has three fields: the name of the method, the method
|
||||
* signature, and a pointer to the native implementation.
|
||||
*/
|
||||
static const JNINativeMethod gMethods[] = {
|
||||
{ "getJniInt", "(Z)I",
|
||||
(void*)PlatformLibrary_getJniInt },
|
||||
{ "reverseString", "(Ljava/lang/String;)Ljava/lang/String;",
|
||||
(void*)PlatformLibrary_reverseString },
|
||||
};
|
||||
|
||||
/*
|
||||
* Do some (slow-ish) lookups now and save the results.
|
||||
*
|
||||
* Returns 0 on success.
|
||||
*/
|
||||
static int cacheIds(JNIEnv* env, jclass clazz) {
|
||||
/*
|
||||
* Save the class in case we want to use it later. Because this is a
|
||||
* reference to the Class object, we need to convert it to a JNI global
|
||||
* reference.
|
||||
*/
|
||||
gCachedState.platformLibraryClass = (jclass) env->NewGlobalRef(clazz);
|
||||
if (clazz == NULL) {
|
||||
ALOGE("Can't create new global ref\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Cache field and method IDs. IDs are not references, which means we
|
||||
* don't need to call NewGlobalRef on them.
|
||||
*/
|
||||
gCachedState.jniInt = env->GetFieldID(clazz, "mJniInt", "I");
|
||||
if (gCachedState.jniInt == NULL) {
|
||||
ALOGE("Can't find PlatformLibrary.mJniInt\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
gCachedState.yodel = env->GetStaticMethodID(clazz, "yodel",
|
||||
"(Ljava/lang/String;)V");
|
||||
if (gCachedState.yodel == NULL) {
|
||||
ALOGE("Can't find PlatformLibrary.yodel\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Explicitly register all methods for our class.
|
||||
*
|
||||
* While we're at it, cache some class references and method/field IDs.
|
||||
*
|
||||
* Returns 0 on success.
|
||||
*/
|
||||
static int registerMethods(JNIEnv* env) {
|
||||
static const char* const kClassName =
|
||||
"com/example/android/platform_library/PlatformLibrary";
|
||||
jclass clazz;
|
||||
|
||||
/* look up the class */
|
||||
clazz = env->FindClass(kClassName);
|
||||
if (clazz == NULL) {
|
||||
ALOGE("Can't find class %s\n", kClassName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* register all the methods */
|
||||
if (env->RegisterNatives(clazz, gMethods,
|
||||
sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
|
||||
{
|
||||
ALOGE("Failed registering methods for %s\n", kClassName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* fill out the rest of the ID cache */
|
||||
return cacheIds(env, clazz);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* This is called by the VM when the shared library is first loaded.
|
||||
*/
|
||||
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||
JNIEnv* env = NULL;
|
||||
jint result = -1;
|
||||
|
||||
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
|
||||
ALOGE("ERROR: GetEnv failed\n");
|
||||
goto bail;
|
||||
}
|
||||
assert(env != NULL);
|
||||
|
||||
if (registerMethods(env) != 0) {
|
||||
ALOGE("ERROR: PlatformLibrary native registration failed\n");
|
||||
goto bail;
|
||||
}
|
||||
|
||||
/* success -- return valid version number */
|
||||
result = JNI_VERSION_1_4;
|
||||
|
||||
bail:
|
||||
return result;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue