src/core/android/SDL_android.cpp
changeset 5582 1281a3f1f0a6
parent 5535 96594ac5fd1a
child 5650 640c67302f8e
     1.1 --- a/src/core/android/SDL_android.cpp	Wed Jul 27 18:07:40 2011 -0400
     1.2 +++ b/src/core/android/SDL_android.cpp	Fri Jul 29 16:51:25 2011 -0400
     1.3 @@ -259,4 +259,234 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +static int Android_JNI_FileOpen(SDL_RWops* ctx)
     1.8 +{
     1.9 +    jstring fileNameJString = (jstring)ctx->hidden.androidio.fileName;
    1.10 +
    1.11 +    // context = SDLActivity.getContext();
    1.12 +    jmethodID mid = mEnv->GetStaticMethodID(mActivityClass,
    1.13 +            "getContext","()Landroid/content/Context;");
    1.14 +    jobject context = mEnv->CallStaticObjectMethod(mActivityClass, mid);
    1.15 +
    1.16 +    // assetManager = context.getAssets();
    1.17 +    mid = mEnv->GetMethodID(mEnv->GetObjectClass(context),
    1.18 +            "getAssets","()Landroid/content/res/AssetManager;");
    1.19 +    jobject assetManager = mEnv->CallObjectMethod(context, mid);
    1.20 +
    1.21 +    // inputStream = assetManager.open(<filename>);
    1.22 +    mEnv->ExceptionClear();
    1.23 +    mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager),
    1.24 +            "open", "(Ljava/lang/String;)Ljava/io/InputStream;");
    1.25 +    jobject inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString);
    1.26 +    if (mEnv->ExceptionOccurred()) {
    1.27 +        mEnv->ExceptionDescribe();
    1.28 +        mEnv->ExceptionClear();
    1.29 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
    1.30 +        return -1;
    1.31 +    } else {
    1.32 +        ctx->hidden.androidio.inputStream = inputStream;
    1.33 +        ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
    1.34 +    }
    1.35 +
    1.36 +    // Store .skip id for seeking purposes
    1.37 +    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
    1.38 +            "skip", "(J)J");
    1.39 +    ctx->hidden.androidio.skipMethod = mid;
    1.40 +
    1.41 +    // Despite all the visible documentation on [Asset]InputStream claiming
    1.42 +    // that the .available() method is not guaranteed to return the entire file
    1.43 +    // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
    1.44 +    // android/apis/content/ReadAsset.java imply that Android's
    1.45 +    // AssetInputStream.available() /will/ always return the total file size
    1.46 +
    1.47 +    // size = inputStream.available();
    1.48 +    mEnv->ExceptionClear();
    1.49 +    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
    1.50 +            "available", "()I");
    1.51 +    ctx->hidden.androidio.size = mEnv->CallIntMethod(inputStream, mid);
    1.52 +    if (mEnv->ExceptionOccurred()) {
    1.53 +        mEnv->ExceptionDescribe();
    1.54 +        mEnv->ExceptionClear();
    1.55 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
    1.56 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
    1.57 +        return -1;
    1.58 +    }
    1.59 +
    1.60 +    // readableByteChannel = Channels.newChannel(inputStream);
    1.61 +    mEnv->ExceptionClear();
    1.62 +    jclass channels = mEnv->FindClass("java/nio/channels/Channels");
    1.63 +    mid = mEnv->GetStaticMethodID(channels,
    1.64 +            "newChannel",
    1.65 +            "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
    1.66 +    jobject readableByteChannel = mEnv->CallStaticObjectMethod(
    1.67 +            channels, mid, inputStream);
    1.68 +    if (mEnv->ExceptionOccurred()) {
    1.69 +        mEnv->ExceptionDescribe();
    1.70 +        mEnv->ExceptionClear();
    1.71 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
    1.72 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
    1.73 +        return -1;
    1.74 +    } else {
    1.75 +        ctx->hidden.androidio.readableByteChannel = readableByteChannel;
    1.76 +        ctx->hidden.androidio.readableByteChannelRef =
    1.77 +            mEnv->NewGlobalRef(readableByteChannel);
    1.78 +    }
    1.79 +
    1.80 +    // Store .read id for reading purposes
    1.81 +    mid = mEnv->GetMethodID(mEnv->GetObjectClass(readableByteChannel),
    1.82 +            "read", "(Ljava/nio/ByteBuffer;)I");
    1.83 +    ctx->hidden.androidio.readMethod = mid;
    1.84 +
    1.85 +    ctx->hidden.androidio.position = 0;
    1.86 +
    1.87 +    return 0;
    1.88 +}
    1.89 +
    1.90 +extern "C" int Android_JNI_FileOpen(SDL_RWops* ctx,
    1.91 +        const char* fileName, const char*)
    1.92 +{
    1.93 +    if (!ctx) {
    1.94 +        return -1;
    1.95 +    }
    1.96 +
    1.97 +    jstring fileNameJString = mEnv->NewStringUTF(fileName);
    1.98 +    ctx->hidden.androidio.fileName = fileNameJString;
    1.99 +    ctx->hidden.androidio.fileNameRef = mEnv->NewGlobalRef(fileNameJString);
   1.100 +
   1.101 +    return Android_JNI_FileOpen(ctx);
   1.102 +}
   1.103 +
   1.104 +extern "C" size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
   1.105 +        size_t size, size_t maxnum)
   1.106 +{
   1.107 +    int bytesRemaining = size * maxnum;
   1.108 +    int bytesRead = 0;
   1.109 +
   1.110 +    jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannel;
   1.111 +    jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
   1.112 +    jobject byteBuffer = mEnv->NewDirectByteBuffer(buffer, bytesRemaining);
   1.113 +
   1.114 +    mEnv->ExceptionClear();
   1.115 +    while (bytesRemaining > 0) {
   1.116 +        // result = readableByteChannel.read(...);
   1.117 +        int result = mEnv->CallIntMethod(readableByteChannel, readMethod, byteBuffer);
   1.118 +
   1.119 +        if (mEnv->ExceptionOccurred()) {
   1.120 +            mEnv->ExceptionDescribe();
   1.121 +            mEnv->ExceptionClear();
   1.122 +            return 0;
   1.123 +        }
   1.124 +
   1.125 +        if (result < 0) {
   1.126 +            break;
   1.127 +        }
   1.128 +
   1.129 +        bytesRemaining -= result;
   1.130 +        bytesRead += result;
   1.131 +        ctx->hidden.androidio.position += result;
   1.132 +    }
   1.133 +
   1.134 +    return bytesRead / size;
   1.135 +}
   1.136 +
   1.137 +extern "C" size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
   1.138 +        size_t size, size_t num)
   1.139 +{
   1.140 +    SDL_SetError("Cannot write to Android package filesystem");
   1.141 +    return 0;
   1.142 +}
   1.143 +
   1.144 +static int Android_JNI_FileClose(SDL_RWops* ctx, bool release)
   1.145 +{
   1.146 +    int result = 0;
   1.147 +
   1.148 +    if (ctx) {
   1.149 +        if (release) {
   1.150 +            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
   1.151 +        }
   1.152 +
   1.153 +        jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
   1.154 +
   1.155 +        // inputStream.close();
   1.156 +        mEnv->ExceptionClear();
   1.157 +        jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
   1.158 +                "close", "()V");
   1.159 +        mEnv->CallVoidMethod(inputStream, mid);
   1.160 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
   1.161 +        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
   1.162 +        if (mEnv->ExceptionOccurred()) {
   1.163 +            result = -1;
   1.164 +            mEnv->ExceptionDescribe();
   1.165 +            mEnv->ExceptionClear();
   1.166 +        }
   1.167 +
   1.168 +        if (release) {
   1.169 +            SDL_FreeRW(ctx);
   1.170 +        }
   1.171 +    }
   1.172 +
   1.173 +    return result;
   1.174 +}
   1.175 +
   1.176 +
   1.177 +extern "C" long Android_JNI_FileSeek(SDL_RWops* ctx, long offset, int whence)
   1.178 +{
   1.179 +    long newPosition;
   1.180 +
   1.181 +    switch (whence) {
   1.182 +        case RW_SEEK_SET:
   1.183 +            newPosition = offset;
   1.184 +            break;
   1.185 +        case RW_SEEK_CUR:
   1.186 +            newPosition = ctx->hidden.androidio.position + offset;
   1.187 +            break;
   1.188 +        case RW_SEEK_END:
   1.189 +            newPosition = ctx->hidden.androidio.size + offset;
   1.190 +            break;
   1.191 +        default:
   1.192 +            SDL_SetError("Unknown value for 'whence'");
   1.193 +            return -1;
   1.194 +    }
   1.195 +    if (newPosition < 0) {
   1.196 +        newPosition = 0;
   1.197 +    }
   1.198 +    if (newPosition > ctx->hidden.androidio.size) {
   1.199 +        newPosition = ctx->hidden.androidio.size;
   1.200 +    }
   1.201 +
   1.202 +    long movement = newPosition - ctx->hidden.androidio.position;
   1.203 +    jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
   1.204 +    jmethodID skipMethod = (jmethodID)ctx->hidden.androidio.skipMethod;
   1.205 +
   1.206 +    if (movement > 0) {
   1.207 +        // The easy case where we're seeking forwards
   1.208 +        mEnv->ExceptionClear();
   1.209 +        while (movement > 0) {
   1.210 +            // inputStream.skip(...);
   1.211 +            movement -= mEnv->CallLongMethod(inputStream, skipMethod, movement);
   1.212 +            if (mEnv->ExceptionOccurred()) {
   1.213 +                mEnv->ExceptionDescribe();
   1.214 +                mEnv->ExceptionClear();
   1.215 +                SDL_SetError("Exception while seeking");
   1.216 +                return -1;
   1.217 +            }
   1.218 +        }
   1.219 +    } else if (movement < 0) {
   1.220 +        // We can't seek backwards so we have to reopen the file and seek
   1.221 +        // forwards which obviously isn't very efficient
   1.222 +        Android_JNI_FileClose(ctx, false);
   1.223 +        Android_JNI_FileOpen(ctx);
   1.224 +        Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
   1.225 +    }
   1.226 +
   1.227 +    ctx->hidden.androidio.position = newPosition;
   1.228 +
   1.229 +    return ctx->hidden.androidio.position;
   1.230 +}
   1.231 +
   1.232 +extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
   1.233 +{
   1.234 +    return Android_JNI_FileClose(ctx, true);
   1.235 +}
   1.236 +
   1.237  /* vi: set ts=4 sw=4 expandtab: */