Merge "Add native C APIs for working with the Asset Manager" into gingerbread
This commit is contained in:
committed by
Android (Google) Code Review
commit
2c40582a6c
@ -6,6 +6,7 @@ import android.content.Context;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
@ -52,7 +53,8 @@ public class NativeActivity extends Activity implements SurfaceHolder.Callback2,
|
||||
private boolean mDestroyed;
|
||||
|
||||
private native int loadNativeCode(String path, MessageQueue queue,
|
||||
String internalDataPath, String externalDataPath, int sdkVersion);
|
||||
String internalDataPath, String externalDataPath, int sdkVersion,
|
||||
AssetManager assetMgr);
|
||||
private native void unloadNativeCode(int handle);
|
||||
|
||||
private native void onStartNative(int handle);
|
||||
@ -138,7 +140,7 @@ public class NativeActivity extends Activity implements SurfaceHolder.Callback2,
|
||||
mNativeHandle = loadNativeCode(path, Looper.myQueue(),
|
||||
getFilesDir().toString(),
|
||||
Environment.getExternalStorageAppFilesDirectory(ai.packageName).toString(),
|
||||
Build.VERSION.SDK_INT);
|
||||
Build.VERSION.SDK_INT, getAssets());
|
||||
|
||||
if (mNativeHandle == 0) {
|
||||
throw new IllegalArgumentException("Unable to load native library: " + path);
|
||||
|
@ -70,6 +70,7 @@ public final class AssetManager {
|
||||
|
||||
// For communication with native code.
|
||||
private int mObject;
|
||||
private int mNObject; // used by the NDK
|
||||
|
||||
private StringBlock mStringBlocks[] = null;
|
||||
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <android_runtime/AndroidRuntime.h>
|
||||
#include <android_runtime/android_view_Surface.h>
|
||||
#include <android_runtime/android_app_NativeActivity.h>
|
||||
#include <android_runtime/android_util_AssetManager.h>
|
||||
#include <surfaceflinger/Surface.h>
|
||||
#include <ui/egl/android_natives.h>
|
||||
#include <ui/InputTransport.h>
|
||||
@ -451,7 +452,8 @@ static bool mainWorkCallback(int fd, int events, void* data) {
|
||||
|
||||
static jint
|
||||
loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jobject messageQueue,
|
||||
jstring internalDataDir, jstring externalDataDir, int sdkVersion)
|
||||
jstring internalDataDir, jstring externalDataDir, int sdkVersion,
|
||||
jobject jAssetMgr)
|
||||
{
|
||||
LOG_TRACE("loadNativeCode_native");
|
||||
|
||||
@ -502,7 +504,7 @@ loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jobject messageQ
|
||||
}
|
||||
code->env = env;
|
||||
code->clazz = env->NewGlobalRef(clazz);
|
||||
|
||||
|
||||
const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL);
|
||||
code->internalDataPath = dirStr;
|
||||
code->internalDataPath = code->internalDataPath.string();
|
||||
@ -512,9 +514,11 @@ loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jobject messageQ
|
||||
code->externalDataPath = dirStr;
|
||||
code->externalDataPath = code->externalDataPath.string();
|
||||
env->ReleaseStringUTFChars(path, dirStr);
|
||||
|
||||
|
||||
code->sdkVersion = sdkVersion;
|
||||
|
||||
code->assetManager = assetManagerForJavaObject(env, jAssetMgr);
|
||||
|
||||
code->createActivityFunc(code, NULL, 0);
|
||||
}
|
||||
|
||||
@ -770,7 +774,7 @@ dispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle, jobject eventOb
|
||||
}
|
||||
|
||||
static const JNINativeMethod g_methods[] = {
|
||||
{ "loadNativeCode", "(Ljava/lang/String;Landroid/os/MessageQueue;Ljava/lang/String;Ljava/lang/String;I)I",
|
||||
{ "loadNativeCode", "(Ljava/lang/String;Landroid/os/MessageQueue;Ljava/lang/String;Ljava/lang/String;I;Landroid/content/res/AssetManager)I",
|
||||
(void*)loadNativeCode_native },
|
||||
{ "unloadNativeCode", "(I)V", (void*)unloadNativeCode_native },
|
||||
{ "onStartNative", "(I)V", (void*)onStart_native },
|
||||
|
@ -29,6 +29,24 @@
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
/*
|
||||
* Native-app access is via the opaque typedef struct AAssetManager in the C namespace.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct AAssetManager { };
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Now the proper C++ android-namespace definitions
|
||||
*/
|
||||
|
||||
namespace android {
|
||||
|
||||
class Asset; // fwd decl for things that include Asset.h first
|
||||
@ -48,7 +66,7 @@ struct ResTable_config;
|
||||
* The asset hierarchy may be examined like a filesystem, using
|
||||
* AssetDir objects to peruse a single directory.
|
||||
*/
|
||||
class AssetManager {
|
||||
class AssetManager : public AAssetManager {
|
||||
public:
|
||||
typedef enum CacheMode {
|
||||
CACHE_UNKNOWN = 0,
|
||||
|
@ -6,6 +6,7 @@ include $(CLEAR_VARS)
|
||||
# our source files
|
||||
#
|
||||
LOCAL_SRC_FILES:= \
|
||||
asset_manager.cpp \
|
||||
input.cpp \
|
||||
looper.cpp \
|
||||
native_activity.cpp \
|
||||
|
210
native/android/asset_manager.cpp
Normal file
210
native/android/asset_manager.cpp
Normal file
@ -0,0 +1,210 @@
|
||||
/*
|
||||
* Copyright (C) 2010 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 "NAsset"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <utils/AssetManager.h>
|
||||
#include <utils/AssetDir.h>
|
||||
#include <utils/Asset.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include "JNIHelp.h"
|
||||
|
||||
using namespace android;
|
||||
|
||||
// -------------------- Backing implementation of the public API --------------------
|
||||
|
||||
// AAssetManager is actually a secret typedef for an empty base class of AssetManager,
|
||||
// but AAssetDir and AAsset are actual wrappers for isolation.
|
||||
|
||||
// -----
|
||||
struct AAssetDir {
|
||||
AssetDir* mAssetDir;
|
||||
size_t mCurFileIndex;
|
||||
String8 mCachedFileName;
|
||||
|
||||
AAssetDir(AssetDir* dir) : mAssetDir(dir), mCurFileIndex(0) { }
|
||||
~AAssetDir() { delete mAssetDir; }
|
||||
};
|
||||
|
||||
|
||||
// -----
|
||||
struct AAsset {
|
||||
Asset* mAsset;
|
||||
|
||||
AAsset(Asset* asset) : mAsset(asset) { }
|
||||
~AAsset() { delete mAsset; }
|
||||
};
|
||||
|
||||
// -------------------- Public native C API --------------------
|
||||
|
||||
/**
|
||||
* Supporting information
|
||||
*/
|
||||
|
||||
static struct assetmanager_offsets_t
|
||||
{
|
||||
jfieldID mObject;
|
||||
} gAssetManagerOffsets;
|
||||
|
||||
static volatile bool gJNIConfigured = false;
|
||||
static Mutex gMutex;
|
||||
|
||||
/**
|
||||
* Asset Manager functionality
|
||||
*/
|
||||
AAssetManager* AAssetManager_fromJava(JNIEnv* env, jobject assetManager)
|
||||
{
|
||||
{
|
||||
Mutex::Autolock _l(gMutex);
|
||||
|
||||
if (gJNIConfigured == false) {
|
||||
jclass amClass = env->FindClass("android/content/res/AssetManager");
|
||||
gAssetManagerOffsets.mObject = env->GetFieldID(amClass, "mObject", "I");
|
||||
gJNIConfigured = true;
|
||||
}
|
||||
}
|
||||
|
||||
return (AAssetManager*) env->GetIntField(assetManager, gAssetManagerOffsets.mObject);
|
||||
}
|
||||
|
||||
AAsset* AAssetManager_open(AAssetManager* amgr, const char* filename, int mode)
|
||||
{
|
||||
Asset::AccessMode amMode;
|
||||
switch (mode) {
|
||||
case AASSET_MODE_UNKNOWN:
|
||||
amMode = Asset::ACCESS_UNKNOWN;
|
||||
break;
|
||||
case AASSET_MODE_RANDOM:
|
||||
amMode = Asset::ACCESS_RANDOM;
|
||||
break;
|
||||
case AASSET_MODE_STREAMING:
|
||||
amMode = Asset::ACCESS_STREAMING;
|
||||
break;
|
||||
case AASSET_MODE_BUFFER:
|
||||
amMode = Asset::ACCESS_BUFFER;
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AssetManager* mgr = static_cast<AssetManager*>(amgr);
|
||||
Asset* asset = mgr->open(filename, amMode);
|
||||
if (asset == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return new AAsset(asset);
|
||||
}
|
||||
|
||||
AAssetDir* AAssetManager_openDir(AAssetManager* amgr, const char* dirName)
|
||||
{
|
||||
AssetManager* mgr = static_cast<AssetManager*>(amgr);
|
||||
return new AAssetDir(mgr->openDir(dirName));
|
||||
}
|
||||
|
||||
/**
|
||||
* AssetDir functionality
|
||||
*/
|
||||
|
||||
const char* AAssetDir_getNextFileName(AAssetDir* assetDir)
|
||||
{
|
||||
const char* returnName = NULL;
|
||||
size_t index = assetDir->mCurFileIndex;
|
||||
const size_t max = assetDir->mAssetDir->getFileCount();
|
||||
|
||||
// Find the next regular file; explicitly don't report directories even if the
|
||||
// underlying implementation changes to report them. At that point we can add
|
||||
// a more general iterator to this native interface set if appropriate.
|
||||
while ((index < max) && (assetDir->mAssetDir->getFileType(index) != kFileTypeRegular)) {
|
||||
index++;
|
||||
}
|
||||
|
||||
// still in bounds? then the one at 'index' is the next to be reported; generate
|
||||
// the string to return and advance the iterator for next time.
|
||||
if (index < max) {
|
||||
assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
|
||||
returnName = assetDir->mCachedFileName.string();
|
||||
index++;
|
||||
}
|
||||
|
||||
assetDir->mCurFileIndex = index;
|
||||
return returnName;
|
||||
}
|
||||
|
||||
void AAssetDir_rewind(AAssetDir* assetDir)
|
||||
{
|
||||
assetDir->mCurFileIndex = 0;
|
||||
}
|
||||
|
||||
const char* AAssetDir_getFileName(AAssetDir* assetDir, int index)
|
||||
{
|
||||
assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
|
||||
return assetDir->mCachedFileName.string();
|
||||
}
|
||||
|
||||
void AAssetDir_close(AAssetDir* assetDir)
|
||||
{
|
||||
delete assetDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asset functionality
|
||||
*/
|
||||
|
||||
int AAsset_read(AAsset* asset, void* buf, size_t count)
|
||||
{
|
||||
return asset->mAsset->read(buf, (size_t)count);
|
||||
}
|
||||
|
||||
off_t AAsset_seek(AAsset* asset, off_t offset, int whence)
|
||||
{
|
||||
return asset->mAsset->seek(offset, whence);
|
||||
}
|
||||
|
||||
void AAsset_close(AAsset* asset)
|
||||
{
|
||||
asset->mAsset->close();
|
||||
delete asset;
|
||||
}
|
||||
|
||||
const void* AAsset_getBuffer(AAsset* asset)
|
||||
{
|
||||
return asset->mAsset->getBuffer(false);
|
||||
}
|
||||
|
||||
off_t AAsset_getLength(AAsset* asset)
|
||||
{
|
||||
return asset->mAsset->getLength();
|
||||
}
|
||||
|
||||
off_t AAsset_getRemainingLength(AAsset* asset)
|
||||
{
|
||||
return asset->mAsset->getRemainingLength();
|
||||
}
|
||||
|
||||
int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength)
|
||||
{
|
||||
return asset->mAsset->openFileDescriptor(outStart, outLength);
|
||||
}
|
||||
|
||||
int AAsset_isAllocated(AAsset* asset)
|
||||
{
|
||||
return asset->mAsset->isAllocated() ? 1 : 0;
|
||||
}
|
148
native/include/android/asset_manager.h
Normal file
148
native/include/android/asset_manager.h
Normal file
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright (C) 2010 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ANDROID_ASSET_MANAGER_H
|
||||
#define ANDROID_ASSET_MANAGER_H
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct AAssetManager;
|
||||
typedef struct AAssetManager AAssetManager;
|
||||
|
||||
struct AAssetDir;
|
||||
typedef struct AAssetDir AAssetDir;
|
||||
|
||||
struct AAsset;
|
||||
typedef struct AAsset AAsset;
|
||||
|
||||
/* Available modes for opening assets */
|
||||
enum {
|
||||
AASSET_MODE_UNKNOWN = 0,
|
||||
AASSET_MODE_RANDOM = 1,
|
||||
AASSET_MODE_STREAMING = 2,
|
||||
AASSET_MODE_BUFFER = 3
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Given a Dalvik AssetManager object, obtain the corresponding native AAssetManager
|
||||
* object. Note that the caller is responsible for obtaining and holding a VM reference
|
||||
* to the jobject to prevent its being garbage collected while the native object is
|
||||
* in use.
|
||||
*/
|
||||
AAssetManager* AAssetManager_fromJava(JNIEnv* env, jobject assetManager);
|
||||
|
||||
/**
|
||||
* Open the named directory within the asset hierarchy. The directory can then
|
||||
* be inspected with the AAssetDir functions. To open the top-level directory,
|
||||
* pass in "" as the dirName.
|
||||
*
|
||||
* The object returned here should be freed by calling AAssetDir_close().
|
||||
*/
|
||||
AAssetDir* AAssetManager_openDir(AAssetManager* mgr, const char* dirName);
|
||||
|
||||
/**
|
||||
* Open an asset.
|
||||
*
|
||||
* The object returned here should be freed by calling AAsset_close().
|
||||
*/
|
||||
AAsset* AAssetManager_open(AAssetManager* mgr, const char* filename, int mode);
|
||||
|
||||
/**
|
||||
* Iterate over the files in an asset directory. A NULL string is returned
|
||||
* when all the file names have been returned.
|
||||
*
|
||||
* The returned file name is suitable for passing to AAssetManager_open().
|
||||
*
|
||||
* The string returned here is owned by the AssetDir implementation and is not
|
||||
* guaranteed to remain valid if any other calls are made on this AAssetDir
|
||||
* instance.
|
||||
*/
|
||||
const char* AAssetDir_getNextFileName(AAssetDir* assetDir);
|
||||
|
||||
/**
|
||||
* Reset the iteration state of AAssetDir_getNextFileName() to the beginning.
|
||||
*/
|
||||
void AAssetDir_rewind(AAssetDir* assetDir);
|
||||
|
||||
/**
|
||||
* Close an opened AAssetDir, freeing any related resources.
|
||||
*/
|
||||
void AAssetDir_close(AAssetDir* assetDir);
|
||||
|
||||
/**
|
||||
* Attempt to read 'count' bytes of data from the current offset.
|
||||
*
|
||||
* Returns the number of bytes read, zero on EOF, or < 0 on error.
|
||||
*/
|
||||
int AAsset_read(AAsset* asset, void* buf, size_t count);
|
||||
|
||||
/**
|
||||
* Seek to the specified offset within the asset data. 'whence' uses the
|
||||
* same constants as lseek()/fseek().
|
||||
*
|
||||
* Returns the new position on success, or (off_t) -1 on error.
|
||||
*/
|
||||
off_t AAsset_seek(AAsset* asset, off_t offset, int whence);
|
||||
|
||||
/**
|
||||
* Close the asset, freeing all associated resources.
|
||||
*/
|
||||
void AAsset_close(AAsset* asset);
|
||||
|
||||
/**
|
||||
* Get a pointer to a buffer holding the entire contents of the assset.
|
||||
*
|
||||
* Returns NULL on failure.
|
||||
*/
|
||||
const void* AAsset_getBuffer(AAsset* asset);
|
||||
|
||||
/**
|
||||
* Report the total size of the asset data.
|
||||
*/
|
||||
off_t AAsset_getLength(AAsset* asset);
|
||||
|
||||
/**
|
||||
* Report the total amount of asset data that can be read from the current position.
|
||||
*/
|
||||
off_t AAsset_getRemainingLength(AAsset* asset);
|
||||
|
||||
/**
|
||||
* Open a new file descriptor that can be used to read the asset data.
|
||||
*
|
||||
* Returns < 0 if direct fd access is not possible (for example, if the asset is
|
||||
* compressed).
|
||||
*/
|
||||
int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength);
|
||||
|
||||
/**
|
||||
* Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not
|
||||
* mmapped).
|
||||
*/
|
||||
int AAsset_isAllocated(AAsset* asset);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif // ANDROID_ASSET_MANAGER_H
|
@ -23,6 +23,7 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/input.h>
|
||||
#include <android/native_window.h>
|
||||
|
||||
@ -84,6 +85,12 @@ typedef struct ANativeActivity {
|
||||
* state.
|
||||
*/
|
||||
void* instance;
|
||||
|
||||
/**
|
||||
* Pointer to the Asset Manager instance for the application. The application
|
||||
* uses this to access binary assets bundled inside its own .apk file.
|
||||
*/
|
||||
AAssetManager* assetManager;
|
||||
} ANativeActivity;
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user