src/core/android/SDL_android.cpp
author Ryan C. Gordon <icculus@icculus.org>
Fri, 29 Jul 2011 16:51:25 -0400
changeset 5582 1281a3f1f0a6
parent 5535 96594ac5fd1a
child 5650 640c67302f8e
permissions -rw-r--r--
Allow Android platforms to read from .apk files via the RWOPS interface.

Fixes Bugzilla #1261.

Thanks to Tim Angus for the patch!
slouken@4964
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@4964
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@4964
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@4964
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@4964
    20
*/
slouken@4964
    21
#include "SDL_config.h"
slouken@5222
    22
#include "SDL_stdinc.h"
slouken@4964
    23
slouken@4989
    24
#include "SDL_android.h"
slouken@4989
    25
slouken@4980
    26
extern "C" {
slouken@5092
    27
#include "../../events/SDL_events_c.h"
slouken@5092
    28
#include "../../video/android/SDL_androidkeyboard.h"
slouken@5092
    29
#include "../../video/android/SDL_androidtouch.h"
slouken@5092
    30
#include "../../video/android/SDL_androidvideo.h"
slouken@4995
    31
slouken@4995
    32
/* Impelemented in audio/android/SDL_androidaudio.c */
slouken@4995
    33
extern void Android_RunAudioThread();
slouken@4995
    34
} // C
slouken@4980
    35
slouken@4964
    36
/*******************************************************************************
slouken@4964
    37
 This file links the Java side of Android with libsdl
slouken@4964
    38
*******************************************************************************/
slouken@4964
    39
#include <jni.h>
slouken@4964
    40
#include <android/log.h>
slouken@4964
    41
slouken@4964
    42
slouken@4964
    43
/*******************************************************************************
slouken@4964
    44
                               Globals
slouken@4964
    45
*******************************************************************************/
slouken@4995
    46
static JNIEnv* mEnv = NULL;
slouken@4995
    47
static JNIEnv* mAudioEnv = NULL;
slouken@4964
    48
slouken@4995
    49
// Main activity
slouken@4998
    50
static jclass mActivityClass;
slouken@4964
    51
slouken@4995
    52
// method signatures
slouken@4995
    53
static jmethodID midCreateGLContext;
slouken@4995
    54
static jmethodID midFlipBuffers;
slouken@4995
    55
static jmethodID midAudioInit;
slouken@4995
    56
static jmethodID midAudioWriteShortBuffer;
slouken@4995
    57
static jmethodID midAudioWriteByteBuffer;
slouken@4996
    58
static jmethodID midAudioQuit;
slouken@4964
    59
slouken@4995
    60
// Accelerometer data storage
slouken@5000
    61
static float fLastAccelerometer[3];
slouken@4964
    62
slouken@4964
    63
slouken@4964
    64
/*******************************************************************************
slouken@4964
    65
                 Functions called by JNI
slouken@4964
    66
*******************************************************************************/
slouken@4964
    67
slouken@4964
    68
// Library init
slouken@4964
    69
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    70
{
slouken@4964
    71
    return JNI_VERSION_1_4;
slouken@4964
    72
}
slouken@4964
    73
slouken@4964
    74
// Called before SDL_main() to initialize JNI bindings
slouken@4998
    75
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
slouken@4964
    76
{
slouken@4964
    77
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    78
slouken@4998
    79
    mEnv = env;
slouken@4998
    80
    mActivityClass = cls;
slouken@4998
    81
slouken@4998
    82
    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
slouken@5222
    83
                                "createGLContext","(II)Z");
slouken@4998
    84
    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    85
                                "flipBuffers","()V");
slouken@4998
    86
    midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
slouken@4998
    87
                                "audioInit", "(IZZI)Ljava/lang/Object;");
slouken@4998
    88
    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    89
                                "audioWriteShortBuffer", "([S)V");
slouken@4998
    90
    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    91
                                "audioWriteByteBuffer", "([B)V");
slouken@4998
    92
    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    93
                                "audioQuit", "()V");
slouken@4964
    94
slouken@4995
    95
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
slouken@4996
    96
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
slouken@4996
    97
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
    98
    }
slouken@4964
    99
}
slouken@4964
   100
slouken@4995
   101
// Resize
slouken@4995
   102
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4998
   103
                                    JNIEnv* env, jclass jcls,
slouken@4995
   104
                                    jint width, jint height, jint format)
slouken@4995
   105
{
slouken@4995
   106
    Android_SetScreenResolution(width, height, format);
slouken@4995
   107
}
slouken@4995
   108
slouken@4964
   109
// Keydown
slouken@4995
   110
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4998
   111
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   112
{
slouken@4980
   113
    Android_OnKeyDown(keycode);
slouken@4964
   114
}
slouken@4964
   115
slouken@4964
   116
// Keyup
slouken@4995
   117
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4998
   118
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   119
{
slouken@4980
   120
    Android_OnKeyUp(keycode);
slouken@4964
   121
}
slouken@4964
   122
slouken@4964
   123
// Touch
slouken@4995
   124
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4998
   125
                                    JNIEnv* env, jclass jcls,
slouken@4995
   126
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   127
{
slouken@5001
   128
    Android_OnTouch(action, x, y, p);
slouken@4964
   129
}
slouken@4964
   130
slouken@4995
   131
// Accelerometer
slouken@4964
   132
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4998
   133
                                    JNIEnv* env, jclass jcls,
slouken@4995
   134
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   135
{
slouken@4964
   136
    fLastAccelerometer[0] = x;
slouken@4964
   137
    fLastAccelerometer[1] = y;
slouken@4964
   138
    fLastAccelerometer[2] = z;   
slouken@4964
   139
}
slouken@4964
   140
slouken@4995
   141
// Quit
slouken@4995
   142
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4998
   143
                                    JNIEnv* env, jclass cls)
slouken@4995
   144
{    
slouken@4995
   145
    // Inject a SDL_QUIT event
slouken@4995
   146
    SDL_SendQuit();
slouken@4995
   147
}
slouken@4995
   148
slouken@4995
   149
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
slouken@4998
   150
                                    JNIEnv* env, jclass cls)
slouken@4995
   151
{
slouken@4998
   152
    /* This is the audio thread, with a different environment */
slouken@4998
   153
    mAudioEnv = env;
slouken@4998
   154
slouken@4996
   155
    Android_RunAudioThread();
slouken@4995
   156
}
slouken@4964
   157
slouken@4964
   158
slouken@4964
   159
/*******************************************************************************
slouken@4964
   160
             Functions called by SDL into Java
slouken@4964
   161
*******************************************************************************/
slouken@5222
   162
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
slouken@4964
   163
{
slouken@5222
   164
    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
slouken@5222
   165
        return SDL_TRUE;
slouken@5222
   166
    } else {
slouken@5222
   167
        return SDL_FALSE;
slouken@5222
   168
    }
slouken@4964
   169
}
slouken@4964
   170
slouken@4989
   171
extern "C" void Android_JNI_SwapWindow()
slouken@4964
   172
{
slouken@4998
   173
    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
slouken@4998
   174
}
slouken@4998
   175
slouken@4998
   176
extern "C" void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   177
{
slouken@4998
   178
    jmethodID mid;
slouken@4998
   179
slouken@4998
   180
    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
slouken@4998
   181
    if (mid) {
slouken@4998
   182
        mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
slouken@4998
   183
    }
slouken@4964
   184
}
slouken@4964
   185
slouken@5000
   186
extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
slouken@5000
   187
{
slouken@5000
   188
    int i;
slouken@5000
   189
    for (i = 0; i < 3; ++i) {
slouken@5000
   190
        values[i] = fLastAccelerometer[i];
slouken@5000
   191
    }
slouken@5000
   192
}
slouken@5000
   193
slouken@4995
   194
//
slouken@4995
   195
// Audio support
slouken@4995
   196
//
slouken@4996
   197
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   198
static jboolean audioBufferStereo = JNI_FALSE;
slouken@4996
   199
static jobject audioBuffer = NULL;
slouken@4996
   200
static void* audioBufferPinned = NULL;
slouken@4995
   201
slouken@4995
   202
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   203
{
slouken@4996
   204
    int audioBufferFrames;
slouken@4964
   205
slouken@4996
   206
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4996
   207
    audioBuffer16Bit = is16Bit;
slouken@4996
   208
    audioBufferStereo = channelCount > 1;
slouken@4964
   209
slouken@4998
   210
    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
slouken@4995
   211
slouken@4996
   212
    if (audioBuffer == NULL) {
slouken@4996
   213
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
slouken@4996
   214
        return 0;
slouken@4996
   215
    }
slouken@4996
   216
    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
slouken@4995
   217
slouken@4996
   218
    jboolean isCopy = JNI_FALSE;
slouken@4996
   219
    if (audioBuffer16Bit) {
slouken@4996
   220
        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
slouken@4996
   221
        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
slouken@4996
   222
    } else {
slouken@4996
   223
        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
slouken@4996
   224
        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
slouken@4996
   225
    }
slouken@4996
   226
    if (audioBufferStereo) {
slouken@4996
   227
        audioBufferFrames /= 2;
slouken@4996
   228
    }
slouken@4996
   229
slouken@4996
   230
    return audioBufferFrames;
slouken@4995
   231
}
slouken@4995
   232
slouken@4996
   233
extern "C" void * Android_JNI_GetAudioBuffer()
slouken@4995
   234
{
slouken@4996
   235
    return audioBufferPinned;
slouken@4995
   236
}
slouken@4995
   237
slouken@4996
   238
extern "C" void Android_JNI_WriteAudioBuffer()
slouken@4995
   239
{
slouken@4996
   240
    if (audioBuffer16Bit) {
slouken@4996
   241
        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   242
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   243
    } else {
slouken@4996
   244
        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   245
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   246
    }
slouken@4995
   247
slouken@4996
   248
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   249
}
slouken@4995
   250
slouken@4995
   251
extern "C" void Android_JNI_CloseAudioDevice()
slouken@4995
   252
{
slouken@4998
   253
    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
slouken@4964
   254
slouken@4997
   255
    if (audioBuffer) {
slouken@4997
   256
        mEnv->DeleteGlobalRef(audioBuffer);
slouken@4997
   257
        audioBuffer = NULL;
slouken@4997
   258
        audioBufferPinned = NULL;
slouken@4997
   259
    }
slouken@4964
   260
}
slouken@4981
   261
icculus@5582
   262
static int Android_JNI_FileOpen(SDL_RWops* ctx)
icculus@5582
   263
{
icculus@5582
   264
    jstring fileNameJString = (jstring)ctx->hidden.androidio.fileName;
icculus@5582
   265
icculus@5582
   266
    // context = SDLActivity.getContext();
icculus@5582
   267
    jmethodID mid = mEnv->GetStaticMethodID(mActivityClass,
icculus@5582
   268
            "getContext","()Landroid/content/Context;");
icculus@5582
   269
    jobject context = mEnv->CallStaticObjectMethod(mActivityClass, mid);
icculus@5582
   270
icculus@5582
   271
    // assetManager = context.getAssets();
icculus@5582
   272
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(context),
icculus@5582
   273
            "getAssets","()Landroid/content/res/AssetManager;");
icculus@5582
   274
    jobject assetManager = mEnv->CallObjectMethod(context, mid);
icculus@5582
   275
icculus@5582
   276
    // inputStream = assetManager.open(<filename>);
icculus@5582
   277
    mEnv->ExceptionClear();
icculus@5582
   278
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager),
icculus@5582
   279
            "open", "(Ljava/lang/String;)Ljava/io/InputStream;");
icculus@5582
   280
    jobject inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString);
icculus@5582
   281
    if (mEnv->ExceptionOccurred()) {
icculus@5582
   282
        mEnv->ExceptionDescribe();
icculus@5582
   283
        mEnv->ExceptionClear();
icculus@5582
   284
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   285
        return -1;
icculus@5582
   286
    } else {
icculus@5582
   287
        ctx->hidden.androidio.inputStream = inputStream;
icculus@5582
   288
        ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
icculus@5582
   289
    }
icculus@5582
   290
icculus@5582
   291
    // Store .skip id for seeking purposes
icculus@5582
   292
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
icculus@5582
   293
            "skip", "(J)J");
icculus@5582
   294
    ctx->hidden.androidio.skipMethod = mid;
icculus@5582
   295
icculus@5582
   296
    // Despite all the visible documentation on [Asset]InputStream claiming
icculus@5582
   297
    // that the .available() method is not guaranteed to return the entire file
icculus@5582
   298
    // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
icculus@5582
   299
    // android/apis/content/ReadAsset.java imply that Android's
icculus@5582
   300
    // AssetInputStream.available() /will/ always return the total file size
icculus@5582
   301
icculus@5582
   302
    // size = inputStream.available();
icculus@5582
   303
    mEnv->ExceptionClear();
icculus@5582
   304
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
icculus@5582
   305
            "available", "()I");
icculus@5582
   306
    ctx->hidden.androidio.size = mEnv->CallIntMethod(inputStream, mid);
icculus@5582
   307
    if (mEnv->ExceptionOccurred()) {
icculus@5582
   308
        mEnv->ExceptionDescribe();
icculus@5582
   309
        mEnv->ExceptionClear();
icculus@5582
   310
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   311
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
icculus@5582
   312
        return -1;
icculus@5582
   313
    }
icculus@5582
   314
icculus@5582
   315
    // readableByteChannel = Channels.newChannel(inputStream);
icculus@5582
   316
    mEnv->ExceptionClear();
icculus@5582
   317
    jclass channels = mEnv->FindClass("java/nio/channels/Channels");
icculus@5582
   318
    mid = mEnv->GetStaticMethodID(channels,
icculus@5582
   319
            "newChannel",
icculus@5582
   320
            "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
icculus@5582
   321
    jobject readableByteChannel = mEnv->CallStaticObjectMethod(
icculus@5582
   322
            channels, mid, inputStream);
icculus@5582
   323
    if (mEnv->ExceptionOccurred()) {
icculus@5582
   324
        mEnv->ExceptionDescribe();
icculus@5582
   325
        mEnv->ExceptionClear();
icculus@5582
   326
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   327
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
icculus@5582
   328
        return -1;
icculus@5582
   329
    } else {
icculus@5582
   330
        ctx->hidden.androidio.readableByteChannel = readableByteChannel;
icculus@5582
   331
        ctx->hidden.androidio.readableByteChannelRef =
icculus@5582
   332
            mEnv->NewGlobalRef(readableByteChannel);
icculus@5582
   333
    }
icculus@5582
   334
icculus@5582
   335
    // Store .read id for reading purposes
icculus@5582
   336
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(readableByteChannel),
icculus@5582
   337
            "read", "(Ljava/nio/ByteBuffer;)I");
icculus@5582
   338
    ctx->hidden.androidio.readMethod = mid;
icculus@5582
   339
icculus@5582
   340
    ctx->hidden.androidio.position = 0;
icculus@5582
   341
icculus@5582
   342
    return 0;
icculus@5582
   343
}
icculus@5582
   344
icculus@5582
   345
extern "C" int Android_JNI_FileOpen(SDL_RWops* ctx,
icculus@5582
   346
        const char* fileName, const char*)
icculus@5582
   347
{
icculus@5582
   348
    if (!ctx) {
icculus@5582
   349
        return -1;
icculus@5582
   350
    }
icculus@5582
   351
icculus@5582
   352
    jstring fileNameJString = mEnv->NewStringUTF(fileName);
icculus@5582
   353
    ctx->hidden.androidio.fileName = fileNameJString;
icculus@5582
   354
    ctx->hidden.androidio.fileNameRef = mEnv->NewGlobalRef(fileNameJString);
icculus@5582
   355
icculus@5582
   356
    return Android_JNI_FileOpen(ctx);
icculus@5582
   357
}
icculus@5582
   358
icculus@5582
   359
extern "C" size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
icculus@5582
   360
        size_t size, size_t maxnum)
icculus@5582
   361
{
icculus@5582
   362
    int bytesRemaining = size * maxnum;
icculus@5582
   363
    int bytesRead = 0;
icculus@5582
   364
icculus@5582
   365
    jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannel;
icculus@5582
   366
    jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
icculus@5582
   367
    jobject byteBuffer = mEnv->NewDirectByteBuffer(buffer, bytesRemaining);
icculus@5582
   368
icculus@5582
   369
    mEnv->ExceptionClear();
icculus@5582
   370
    while (bytesRemaining > 0) {
icculus@5582
   371
        // result = readableByteChannel.read(...);
icculus@5582
   372
        int result = mEnv->CallIntMethod(readableByteChannel, readMethod, byteBuffer);
icculus@5582
   373
icculus@5582
   374
        if (mEnv->ExceptionOccurred()) {
icculus@5582
   375
            mEnv->ExceptionDescribe();
icculus@5582
   376
            mEnv->ExceptionClear();
icculus@5582
   377
            return 0;
icculus@5582
   378
        }
icculus@5582
   379
icculus@5582
   380
        if (result < 0) {
icculus@5582
   381
            break;
icculus@5582
   382
        }
icculus@5582
   383
icculus@5582
   384
        bytesRemaining -= result;
icculus@5582
   385
        bytesRead += result;
icculus@5582
   386
        ctx->hidden.androidio.position += result;
icculus@5582
   387
    }
icculus@5582
   388
icculus@5582
   389
    return bytesRead / size;
icculus@5582
   390
}
icculus@5582
   391
icculus@5582
   392
extern "C" size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
icculus@5582
   393
        size_t size, size_t num)
icculus@5582
   394
{
icculus@5582
   395
    SDL_SetError("Cannot write to Android package filesystem");
icculus@5582
   396
    return 0;
icculus@5582
   397
}
icculus@5582
   398
icculus@5582
   399
static int Android_JNI_FileClose(SDL_RWops* ctx, bool release)
icculus@5582
   400
{
icculus@5582
   401
    int result = 0;
icculus@5582
   402
icculus@5582
   403
    if (ctx) {
icculus@5582
   404
        if (release) {
icculus@5582
   405
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   406
        }
icculus@5582
   407
icculus@5582
   408
        jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
icculus@5582
   409
icculus@5582
   410
        // inputStream.close();
icculus@5582
   411
        mEnv->ExceptionClear();
icculus@5582
   412
        jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
icculus@5582
   413
                "close", "()V");
icculus@5582
   414
        mEnv->CallVoidMethod(inputStream, mid);
icculus@5582
   415
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
icculus@5582
   416
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
icculus@5582
   417
        if (mEnv->ExceptionOccurred()) {
icculus@5582
   418
            result = -1;
icculus@5582
   419
            mEnv->ExceptionDescribe();
icculus@5582
   420
            mEnv->ExceptionClear();
icculus@5582
   421
        }
icculus@5582
   422
icculus@5582
   423
        if (release) {
icculus@5582
   424
            SDL_FreeRW(ctx);
icculus@5582
   425
        }
icculus@5582
   426
    }
icculus@5582
   427
icculus@5582
   428
    return result;
icculus@5582
   429
}
icculus@5582
   430
icculus@5582
   431
icculus@5582
   432
extern "C" long Android_JNI_FileSeek(SDL_RWops* ctx, long offset, int whence)
icculus@5582
   433
{
icculus@5582
   434
    long newPosition;
icculus@5582
   435
icculus@5582
   436
    switch (whence) {
icculus@5582
   437
        case RW_SEEK_SET:
icculus@5582
   438
            newPosition = offset;
icculus@5582
   439
            break;
icculus@5582
   440
        case RW_SEEK_CUR:
icculus@5582
   441
            newPosition = ctx->hidden.androidio.position + offset;
icculus@5582
   442
            break;
icculus@5582
   443
        case RW_SEEK_END:
icculus@5582
   444
            newPosition = ctx->hidden.androidio.size + offset;
icculus@5582
   445
            break;
icculus@5582
   446
        default:
icculus@5582
   447
            SDL_SetError("Unknown value for 'whence'");
icculus@5582
   448
            return -1;
icculus@5582
   449
    }
icculus@5582
   450
    if (newPosition < 0) {
icculus@5582
   451
        newPosition = 0;
icculus@5582
   452
    }
icculus@5582
   453
    if (newPosition > ctx->hidden.androidio.size) {
icculus@5582
   454
        newPosition = ctx->hidden.androidio.size;
icculus@5582
   455
    }
icculus@5582
   456
icculus@5582
   457
    long movement = newPosition - ctx->hidden.androidio.position;
icculus@5582
   458
    jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
icculus@5582
   459
    jmethodID skipMethod = (jmethodID)ctx->hidden.androidio.skipMethod;
icculus@5582
   460
icculus@5582
   461
    if (movement > 0) {
icculus@5582
   462
        // The easy case where we're seeking forwards
icculus@5582
   463
        mEnv->ExceptionClear();
icculus@5582
   464
        while (movement > 0) {
icculus@5582
   465
            // inputStream.skip(...);
icculus@5582
   466
            movement -= mEnv->CallLongMethod(inputStream, skipMethod, movement);
icculus@5582
   467
            if (mEnv->ExceptionOccurred()) {
icculus@5582
   468
                mEnv->ExceptionDescribe();
icculus@5582
   469
                mEnv->ExceptionClear();
icculus@5582
   470
                SDL_SetError("Exception while seeking");
icculus@5582
   471
                return -1;
icculus@5582
   472
            }
icculus@5582
   473
        }
icculus@5582
   474
    } else if (movement < 0) {
icculus@5582
   475
        // We can't seek backwards so we have to reopen the file and seek
icculus@5582
   476
        // forwards which obviously isn't very efficient
icculus@5582
   477
        Android_JNI_FileClose(ctx, false);
icculus@5582
   478
        Android_JNI_FileOpen(ctx);
icculus@5582
   479
        Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
icculus@5582
   480
    }
icculus@5582
   481
icculus@5582
   482
    ctx->hidden.androidio.position = newPosition;
icculus@5582
   483
icculus@5582
   484
    return ctx->hidden.androidio.position;
icculus@5582
   485
}
icculus@5582
   486
icculus@5582
   487
extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
icculus@5582
   488
{
icculus@5582
   489
    return Android_JNI_FileClose(ctx, true);
icculus@5582
   490
}
icculus@5582
   491
slouken@4981
   492
/* vi: set ts=4 sw=4 expandtab: */