src/core/android/SDL_android.c
author stopiccot <alexey.petruchik@gmail.com>
Wed, 23 Apr 2014 03:42:32 +0300
branchapk
changeset 8897 fff9c0b9f1fb
parent 8896 d931167c375a
child 8900 537445b25e4e
permissions -rw-r--r--
Fixing issues discovered by Philipp Wiesemann
slouken@4964
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@5222
    22
#include "SDL_stdinc.h"
slouken@6284
    23
#include "SDL_assert.h"
slouken@6650
    24
#include "SDL_log.h"
slouken@4964
    25
slouken@6044
    26
#ifdef __ANDROID__
slouken@6044
    27
slouken@6630
    28
#include "SDL_system.h"
slouken@4989
    29
#include "SDL_android.h"
slouken@6792
    30
#include <EGL/egl.h>
slouken@4989
    31
slouken@5092
    32
#include "../../events/SDL_events_c.h"
slouken@5092
    33
#include "../../video/android/SDL_androidkeyboard.h"
slouken@5092
    34
#include "../../video/android/SDL_androidtouch.h"
slouken@5092
    35
#include "../../video/android/SDL_androidvideo.h"
gabomdq@7659
    36
#include "../../video/android/SDL_androidwindow.h"
gabomdq@8057
    37
#include "../../joystick/android/SDL_sysjoystick_c.h"
slouken@4995
    38
icculus@5996
    39
#include <android/log.h>
gabomdq@6354
    40
#include <pthread.h>
gabomdq@6806
    41
#include <sys/types.h>
gabomdq@6806
    42
#include <unistd.h>
icculus@5996
    43
#define LOG_TAG "SDL_android"
gabomdq@7678
    44
/* #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
gabomdq@7678
    45
/* #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
icculus@5996
    46
#define LOGI(...) do {} while (false)
icculus@5996
    47
#define LOGE(...) do {} while (false)
icculus@5996
    48
slouken@6650
    49
/* Uncomment this to log messages entering and exiting methods in this file */
gabomdq@7678
    50
/* #define DEBUG_JNI */
icculus@5996
    51
philipp@7246
    52
static void Android_JNI_ThreadDestroyed(void*);
slouken@4980
    53
slouken@4964
    54
/*******************************************************************************
slouken@4964
    55
 This file links the Java side of Android with libsdl
slouken@4964
    56
*******************************************************************************/
slouken@4964
    57
#include <jni.h>
slouken@4964
    58
#include <android/log.h>
ewing@7501
    59
#include <stdbool.h>
slouken@4964
    60
slouken@4964
    61
slouken@4964
    62
/*******************************************************************************
slouken@4964
    63
                               Globals
slouken@4964
    64
*******************************************************************************/
gabomdq@6354
    65
static pthread_key_t mThreadKey;
icculus@5996
    66
static JavaVM* mJavaVM;
slouken@4964
    67
gabomdq@7663
    68
/* Main activity */
slouken@4998
    69
static jclass mActivityClass;
slouken@4964
    70
gabomdq@7663
    71
/* method signatures */
gabomdq@7659
    72
static jmethodID midGetNativeSurface;
slouken@4995
    73
static jmethodID midFlipBuffers;
slouken@4995
    74
static jmethodID midAudioInit;
slouken@4995
    75
static jmethodID midAudioWriteShortBuffer;
slouken@4995
    76
static jmethodID midAudioWriteByteBuffer;
slouken@4996
    77
static jmethodID midAudioQuit;
gabomdq@8057
    78
static jmethodID midPollInputDevices;
slouken@4964
    79
gabomdq@7663
    80
/* Accelerometer data storage */
slouken@5000
    81
static float fLastAccelerometer[3];
slouken@6212
    82
static bool bHasNewData;
slouken@4964
    83
slouken@4964
    84
/*******************************************************************************
slouken@4964
    85
                 Functions called by JNI
slouken@4964
    86
*******************************************************************************/
slouken@4964
    87
gabomdq@7663
    88
/* Library init */
ewing@7501
    89
jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    90
{
icculus@5996
    91
    JNIEnv *env;
icculus@5996
    92
    mJavaVM = vm;
icculus@5996
    93
    LOGI("JNI_OnLoad called");
ewing@7501
    94
    if ((*mJavaVM)->GetEnv(mJavaVM, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
icculus@5996
    95
        LOGE("Failed to get the environment using GetEnv()");
icculus@5996
    96
        return -1;
icculus@5996
    97
    }
gabomdq@6354
    98
    /*
gabomdq@6354
    99
     * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
gabomdq@6354
   100
     * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
gabomdq@6354
   101
     */
slouken@8055
   102
    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed) != 0) {
gabomdq@6354
   103
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
gabomdq@6354
   104
    }
slouken@8055
   105
    Android_JNI_SetupThread();
icculus@5996
   106
slouken@4964
   107
    return JNI_VERSION_1_4;
slouken@4964
   108
}
slouken@4964
   109
gabomdq@7663
   110
/* Called before SDL_main() to initialize JNI bindings */
ewing@7501
   111
void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
slouken@4964
   112
{
slouken@4964
   113
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
   114
gabomdq@6354
   115
    Android_JNI_SetupThread();
gabomdq@6354
   116
ewing@7501
   117
    mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
slouken@4998
   118
gabomdq@7659
   119
    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@7659
   120
                                "getNativeSurface","()Landroid/view/Surface;");
ewing@7501
   121
    midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   122
                                "flipBuffers","()V");
ewing@7501
   123
    midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@7552
   124
                                "audioInit", "(IZZI)I");
ewing@7501
   125
    midAudioWriteShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   126
                                "audioWriteShortBuffer", "([S)V");
ewing@7501
   127
    midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   128
                                "audioWriteByteBuffer", "([B)V");
ewing@7501
   129
    midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   130
                                "audioQuit", "()V");
gabomdq@8057
   131
    midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@8057
   132
                                "pollInputDevices", "()V");
slouken@4964
   133
slouken@6212
   134
    bHasNewData = false;
slouken@6212
   135
gabomdq@7659
   136
    if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
gabomdq@8057
   137
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit || !midPollInputDevices) {
slouken@4996
   138
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
   139
    }
icculus@5996
   140
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
slouken@4964
   141
}
slouken@4964
   142
gabomdq@7663
   143
/* Resize */
ewing@7501
   144
void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4998
   145
                                    JNIEnv* env, jclass jcls,
slouken@4995
   146
                                    jint width, jint height, jint format)
slouken@4995
   147
{
slouken@4995
   148
    Android_SetScreenResolution(width, height, format);
slouken@4995
   149
}
slouken@4995
   150
gabomdq@7907
   151
// Paddown
gabomdq@8013
   152
int Java_org_libsdl_app_SDLActivity_onNativePadDown(
gabomdq@7907
   153
                                    JNIEnv* env, jclass jcls,
gabomdq@8057
   154
                                    jint device_id, jint keycode)
gabomdq@7907
   155
{
gabomdq@8057
   156
    return Android_OnPadDown(device_id, keycode);
gabomdq@7907
   157
}
gabomdq@7907
   158
gabomdq@7907
   159
// Padup
gabomdq@8013
   160
int Java_org_libsdl_app_SDLActivity_onNativePadUp(
gabomdq@7907
   161
                                   JNIEnv* env, jclass jcls,
gabomdq@8057
   162
                                   jint device_id, jint keycode)
gabomdq@7907
   163
{
gabomdq@8057
   164
    return Android_OnPadUp(device_id, keycode);
gabomdq@7907
   165
}
gabomdq@7907
   166
philipp@7926
   167
/* Joy */
gabomdq@7907
   168
void Java_org_libsdl_app_SDLActivity_onNativeJoy(
gabomdq@7907
   169
                                    JNIEnv* env, jclass jcls,
gabomdq@8057
   170
                                    jint device_id, jint axis, jfloat value)
gabomdq@7907
   171
{
gabomdq@8057
   172
    Android_OnJoy(device_id, axis, value);
gabomdq@8057
   173
}
gabomdq@8057
   174
dbrady@8140
   175
/* POV Hat */
dbrady@8140
   176
void Java_org_libsdl_app_SDLActivity_onNativeHat(
dbrady@8140
   177
                                    JNIEnv* env, jclass jcls,
dbrady@8140
   178
                                    jint device_id, jint hat_id, jint x, jint y)
dbrady@8140
   179
{
dbrady@8140
   180
    Android_OnHat(device_id, hat_id, x, y);
dbrady@8140
   181
}
dbrady@8140
   182
gabomdq@8057
   183
gabomdq@8057
   184
int Java_org_libsdl_app_SDLActivity_nativeAddJoystick(
gabomdq@8057
   185
    JNIEnv* env, jclass jcls,
gabomdq@8057
   186
    jint device_id, jstring device_name, jint is_accelerometer, 
gabomdq@8057
   187
    jint nbuttons, jint naxes, jint nhats, jint nballs)
gabomdq@8057
   188
{
gabomdq@8057
   189
    int retval;
gabomdq@8057
   190
    const char *name = (*env)->GetStringUTFChars(env, device_name, NULL);
gabomdq@8057
   191
gabomdq@8057
   192
    retval = Android_AddJoystick(device_id, name, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
gabomdq@8057
   193
gabomdq@8057
   194
    (*env)->ReleaseStringUTFChars(env, device_name, name);
gabomdq@8057
   195
    
gabomdq@8057
   196
    return retval;
gabomdq@8057
   197
}
gabomdq@8057
   198
gabomdq@8057
   199
int Java_org_libsdl_app_SDLActivity_nativeRemoveJoystick(
gabomdq@8057
   200
    JNIEnv* env, jclass jcls, jint device_id)
gabomdq@8057
   201
{
gabomdq@8057
   202
    return Android_RemoveJoystick(device_id);
gabomdq@7907
   203
}
gabomdq@7907
   204
gabomdq@7659
   205
gabomdq@7663
   206
/* Surface Created */
gabomdq@7659
   207
void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
gabomdq@7659
   208
{
gabomdq@7659
   209
    SDL_WindowData *data;
gabomdq@7659
   210
    SDL_VideoDevice *_this;
gabomdq@7659
   211
gabomdq@7659
   212
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
gabomdq@7659
   213
        return;
gabomdq@7659
   214
    }
gabomdq@7659
   215
    
gabomdq@7659
   216
    _this =  SDL_GetVideoDevice();
gabomdq@7659
   217
    data =  (SDL_WindowData *) Android_Window->driverdata;
gabomdq@7659
   218
    
gabomdq@7659
   219
    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
gabomdq@7659
   220
    if (data->egl_surface == EGL_NO_SURFACE) {
gabomdq@7659
   221
        if(data->native_window) {
gabomdq@7659
   222
            ANativeWindow_release(data->native_window);
gabomdq@7659
   223
        }
gabomdq@7659
   224
        data->native_window = Android_JNI_GetNativeWindow();
gabomdq@7659
   225
        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
gabomdq@7659
   226
    }
gabomdq@7659
   227
    
gabomdq@7659
   228
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
gabomdq@7659
   229
    
gabomdq@7659
   230
}
gabomdq@7659
   231
gabomdq@7663
   232
/* Surface Destroyed */
gabomdq@7659
   233
void Java_org_libsdl_app_SDLActivity_onNativeSurfaceDestroyed(JNIEnv* env, jclass jcls)
gabomdq@7659
   234
{
gabomdq@7659
   235
    /* We have to clear the current context and destroy the egl surface here
gabomdq@7659
   236
     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
gabomdq@7659
   237
     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
gabomdq@7659
   238
     */
gabomdq@7659
   239
    SDL_WindowData *data;
gabomdq@7659
   240
    SDL_VideoDevice *_this;
gabomdq@7659
   241
    
gabomdq@7659
   242
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
gabomdq@7659
   243
        return;
gabomdq@7659
   244
    }
gabomdq@7659
   245
    
gabomdq@7659
   246
    _this =  SDL_GetVideoDevice();
gabomdq@7659
   247
    data = (SDL_WindowData *) Android_Window->driverdata;
gabomdq@7659
   248
    
gabomdq@7659
   249
    if (data->egl_surface != EGL_NO_SURFACE) {
gabomdq@7659
   250
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
gabomdq@7659
   251
        SDL_EGL_DestroySurface(_this, data->egl_surface);
gabomdq@7659
   252
        data->egl_surface = EGL_NO_SURFACE;
gabomdq@7659
   253
    }
gabomdq@7659
   254
    
gabomdq@7659
   255
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
gabomdq@7659
   256
gabomdq@7659
   257
}
gabomdq@7659
   258
gabomdq@7659
   259
void Java_org_libsdl_app_SDLActivity_nativeFlipBuffers(JNIEnv* env, jclass jcls)
gabomdq@7659
   260
{
gabomdq@7659
   261
    SDL_GL_SwapWindow(Android_Window);
gabomdq@7659
   262
}
gabomdq@7659
   263
gabomdq@7663
   264
/* Keydown */
ewing@7501
   265
void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4998
   266
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   267
{
slouken@4980
   268
    Android_OnKeyDown(keycode);
slouken@4964
   269
}
slouken@4964
   270
gabomdq@7663
   271
/* Keyup */
ewing@7501
   272
void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4998
   273
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   274
{
slouken@4980
   275
    Android_OnKeyUp(keycode);
slouken@4964
   276
}
slouken@4964
   277
gabomdq@7663
   278
/* Keyboard Focus Lost */
gabomdq@7564
   279
void Java_org_libsdl_app_SDLActivity_onNativeKeyboardFocusLost(
gabomdq@7564
   280
                                    JNIEnv* env, jclass jcls)
gabomdq@7564
   281
{
gabomdq@7564
   282
    /* Calling SDL_StopTextInput will take care of hiding the keyboard and cleaning up the DummyText widget */
gabomdq@7564
   283
    SDL_StopTextInput();
gabomdq@7564
   284
}
gabomdq@7564
   285
gabomdq@7564
   286
gabomdq@7663
   287
/* Touch */
ewing@7501
   288
void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4998
   289
                                    JNIEnv* env, jclass jcls,
icculus@5982
   290
                                    jint touch_device_id_in, jint pointer_finger_id_in,
slouken@4995
   291
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   292
{
icculus@5982
   293
    Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
slouken@4964
   294
}
slouken@4964
   295
gabomdq@7663
   296
/* Accelerometer */
ewing@7501
   297
void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4998
   298
                                    JNIEnv* env, jclass jcls,
slouken@4995
   299
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   300
{
slouken@4964
   301
    fLastAccelerometer[0] = x;
slouken@4964
   302
    fLastAccelerometer[1] = y;
slouken@6212
   303
    fLastAccelerometer[2] = z;
slouken@6212
   304
    bHasNewData = true;
slouken@4964
   305
}
slouken@4964
   306
gabomdq@7663
   307
/* Low memory */
ewing@7501
   308
void Java_org_libsdl_app_SDLActivity_nativeLowMemory(
slouken@7190
   309
                                    JNIEnv* env, jclass cls)
slouken@7191
   310
{
slouken@7190
   311
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
slouken@7190
   312
}
slouken@7190
   313
gabomdq@7663
   314
/* Quit */
ewing@7501
   315
void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4998
   316
                                    JNIEnv* env, jclass cls)
slouken@7191
   317
{
gabomdq@7910
   318
    /* Discard previous events. The user should have handled state storage
gabomdq@7910
   319
     * in SDL_APP_WILLENTERBACKGROUND. After nativeQuit() is called, no
gabomdq@7910
   320
     * events other than SDL_QUIT and SDL_APP_TERMINATING should fire */
gabomdq@7910
   321
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
gabomdq@7663
   322
    /* Inject a SDL_QUIT event */
slouken@4995
   323
    SDL_SendQuit();
slouken@7190
   324
    SDL_SendAppEvent(SDL_APP_TERMINATING);
gabomdq@7910
   325
    /* Resume the event loop so that the app can catch SDL_QUIT which
gabomdq@7910
   326
     * should now be the top event in the event queue. */
gabomdq@7910
   327
    if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
slouken@4995
   328
}
slouken@4995
   329
gabomdq@7663
   330
/* Pause */
ewing@7501
   331
void Java_org_libsdl_app_SDLActivity_nativePause(
slouken@6186
   332
                                    JNIEnv* env, jclass cls)
slouken@6186
   333
{
gabomdq@8039
   334
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
slouken@6186
   335
    if (Android_Window) {
slouken@6186
   336
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@6191
   337
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
gabomdq@8039
   338
        SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
gabomdq@8039
   339
        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
gabomdq@8039
   340
    
gabomdq@8039
   341
        /* *After* sending the relevant events, signal the pause semaphore 
gabomdq@8039
   342
         * so the event loop knows to pause and (optionally) block itself */
gabomdq@8039
   343
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
slouken@6186
   344
    }
slouken@6186
   345
}
slouken@6186
   346
gabomdq@7663
   347
/* Resume */
ewing@7501
   348
void Java_org_libsdl_app_SDLActivity_nativeResume(
slouken@6186
   349
                                    JNIEnv* env, jclass cls)
slouken@6186
   350
{
slouken@7190
   351
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
slouken@7190
   352
slouken@6186
   353
    if (Android_Window) {
gabomdq@8047
   354
        SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
gabomdq@8047
   355
        SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
gabomdq@8047
   356
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
gabomdq@8047
   357
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
gabomdq@6330
   358
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
gabomdq@6330
   359
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
gabomdq@6330
   360
         * and this function will be called from the Java thread instead.
gabomdq@6330
   361
         */
gabomdq@6330
   362
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
slouken@6186
   363
    }
slouken@6186
   364
}
slouken@6186
   365
ewing@7501
   366
void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
slouken@6555
   367
                                    JNIEnv* env, jclass cls,
slouken@6555
   368
                                    jstring text, jint newCursorPosition)
slouken@6555
   369
{
ewing@7501
   370
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
slouken@6555
   371
slouken@6555
   372
    SDL_SendKeyboardText(utftext);
slouken@6555
   373
ewing@7501
   374
    (*env)->ReleaseStringUTFChars(env, text, utftext);
slouken@6555
   375
}
slouken@6555
   376
ewing@7501
   377
void Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
slouken@6555
   378
                                    JNIEnv* env, jclass cls,
slouken@6555
   379
                                    jstring text, jint newCursorPosition)
slouken@6555
   380
{
ewing@7501
   381
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
slouken@6555
   382
slouken@6555
   383
    SDL_SendEditingText(utftext, 0, 0);
slouken@6555
   384
ewing@7501
   385
    (*env)->ReleaseStringUTFChars(env, text, utftext);
slouken@6555
   386
}
slouken@6555
   387
alexey@8897
   388
jstring Java_org_libsdl_app_SDLActivity_nativeGetHint(JNIEnv* env, jclass cls, jstring name) {
alexey@8896
   389
    const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
alexey@8896
   390
    const char *hint = SDL_GetHint(utfname);
slouken@6555
   391
alexey@8896
   392
    jstring result = (*env)->NewStringUTF(env, hint);
alexey@8896
   393
    (*env)->ReleaseStringUTFChars(env, name, utfname);
alexey@8896
   394
alexey@8896
   395
    return result;
alexey@8896
   396
}
slouken@6555
   397
slouken@4964
   398
/*******************************************************************************
slouken@4964
   399
             Functions called by SDL into Java
slouken@4964
   400
*******************************************************************************/
slouken@6284
   401
ewing@7501
   402
static int s_active = 0;
ewing@7501
   403
struct LocalReferenceHolder
slouken@6284
   404
{
slouken@6284
   405
    JNIEnv *m_env;
slouken@6650
   406
    const char *m_func;
slouken@6284
   407
};
slouken@6284
   408
ewing@7501
   409
static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func)
ewing@7501
   410
{
ewing@7501
   411
    struct LocalReferenceHolder refholder;
ewing@7501
   412
    refholder.m_env = NULL;
ewing@7501
   413
    refholder.m_func = func;
ewing@7501
   414
#ifdef DEBUG_JNI
ewing@7501
   415
    SDL_Log("Entering function %s", func);
ewing@7501
   416
#endif
ewing@7501
   417
    return refholder;
ewing@7501
   418
}
ewing@7501
   419
ewing@7501
   420
static SDL_bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env)
ewing@7501
   421
{
ewing@7501
   422
    const int capacity = 16;
ewing@7501
   423
    if ((*env)->PushLocalFrame(env, capacity) < 0) {
ewing@7501
   424
        SDL_SetError("Failed to allocate enough JVM local references");
philipp@7516
   425
        return SDL_FALSE;
ewing@7501
   426
    }
ewing@7501
   427
    ++s_active;
ewing@7501
   428
    refholder->m_env = env;
ewing@7501
   429
    return SDL_TRUE;
ewing@7501
   430
}
ewing@7501
   431
ewing@7501
   432
static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder)
ewing@7501
   433
{
ewing@7501
   434
#ifdef DEBUG_JNI
ewing@7501
   435
    SDL_Log("Leaving function %s", refholder->m_func);
ewing@7501
   436
#endif
ewing@7501
   437
    if (refholder->m_env) {
ewing@7501
   438
        JNIEnv* env = refholder->m_env;
ewing@7501
   439
        (*env)->PopLocalFrame(env, NULL);
ewing@7501
   440
        --s_active;
ewing@7501
   441
    }
ewing@7501
   442
}
ewing@7501
   443
ewing@7501
   444
static SDL_bool LocalReferenceHolder_IsActive()
ewing@7501
   445
{
ewing@7501
   446
    return s_active > 0;    
ewing@7501
   447
}
ewing@7501
   448
gabomdq@7659
   449
ANativeWindow* Android_JNI_GetNativeWindow(void)
slouken@4964
   450
{
gabomdq@7659
   451
    ANativeWindow* anw;
gabomdq@7659
   452
    jobject s;
slouken@6792
   453
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6792
   454
gabomdq@7659
   455
    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
gabomdq@7659
   456
    anw = ANativeWindow_fromSurface(env, s);
gabomdq@7659
   457
    (*env)->DeleteLocalRef(env, s);
gabomdq@7659
   458
  
gabomdq@7659
   459
    return anw;
gabomdq@7567
   460
}
gabomdq@7567
   461
ewing@7501
   462
void Android_JNI_SwapWindow()
slouken@4964
   463
{
gabomdq@6354
   464
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   465
    (*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midFlipBuffers);
slouken@4998
   466
}
slouken@4998
   467
ewing@7501
   468
void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   469
{
slouken@4998
   470
    jmethodID mid;
gabomdq@6354
   471
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   472
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,"setActivityTitle","(Ljava/lang/String;)Z");
slouken@4998
   473
    if (mid) {
ewing@7501
   474
        jstring jtitle = (jstring)((*mEnv)->NewStringUTF(mEnv, title));
ewing@7501
   475
        (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, mid, jtitle);
ewing@7501
   476
        (*mEnv)->DeleteLocalRef(mEnv, jtitle);
slouken@4998
   477
    }
slouken@4964
   478
}
slouken@4964
   479
ewing@7501
   480
SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
slouken@5000
   481
{
slouken@5000
   482
    int i;
slouken@6212
   483
    SDL_bool retval = SDL_FALSE;
slouken@6212
   484
slouken@6212
   485
    if (bHasNewData) {
slouken@6212
   486
        for (i = 0; i < 3; ++i) {
slouken@6212
   487
            values[i] = fLastAccelerometer[i];
slouken@6212
   488
        }
slouken@6212
   489
        bHasNewData = false;
slouken@6212
   490
        retval = SDL_TRUE;
slouken@5000
   491
    }
slouken@6212
   492
slouken@6212
   493
    return retval;
slouken@5000
   494
}
slouken@5000
   495
slouken@8055
   496
static void Android_JNI_ThreadDestroyed(void* value)
slouken@8055
   497
{
gabomdq@6354
   498
    /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
gabomdq@6354
   499
    JNIEnv *env = (JNIEnv*) value;
gabomdq@6354
   500
    if (env != NULL) {
ewing@7501
   501
        (*mJavaVM)->DetachCurrentThread(mJavaVM);
gabomdq@6354
   502
        pthread_setspecific(mThreadKey, NULL);
gabomdq@6354
   503
    }
gabomdq@6354
   504
}
gabomdq@6354
   505
slouken@8055
   506
JNIEnv* Android_JNI_GetEnv(void)
slouken@8055
   507
{
gabomdq@6354
   508
    /* From http://developer.android.com/guide/practices/jni.html
gabomdq@6354
   509
     * All threads are Linux threads, scheduled by the kernel.
gabomdq@6354
   510
     * They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then
gabomdq@6354
   511
     * attached to the JavaVM. For example, a thread started with pthread_create can be attached with the
gabomdq@6354
   512
     * JNI AttachCurrentThread or AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv,
gabomdq@6354
   513
     * and cannot make JNI calls.
gabomdq@6354
   514
     * Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the "main"
gabomdq@6354
   515
     * ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread
gabomdq@6354
   516
     * is a no-op.
gabomdq@6354
   517
     * Note: You can call this function any number of times for the same thread, there's no harm in it
gabomdq@6354
   518
     */
gabomdq@6354
   519
gabomdq@6354
   520
    JNIEnv *env;
ewing@7501
   521
    int status = (*mJavaVM)->AttachCurrentThread(mJavaVM, &env, NULL);
gabomdq@6354
   522
    if(status < 0) {
gabomdq@6354
   523
        LOGE("failed to attach current thread");
gabomdq@6354
   524
        return 0;
gabomdq@6354
   525
    }
gabomdq@6354
   526
gabomdq@6354
   527
    /* From http://developer.android.com/guide/practices/jni.html
gabomdq@6354
   528
     * Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward,
gabomdq@6354
   529
     * in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be
gabomdq@6354
   530
     * called before the thread exits, and call DetachCurrentThread from there. (Use that key with pthread_setspecific
gabomdq@6354
   531
     * to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
gabomdq@6354
   532
     * Note: The destructor is not called unless the stored value is != NULL
gabomdq@6354
   533
     * Note: You can call this function any number of times for the same thread, there's no harm in it
gabomdq@6354
   534
     *       (except for some lost CPU cycles)
gabomdq@6354
   535
     */
gabomdq@6354
   536
    pthread_setspecific(mThreadKey, (void*) env);
slouken@8055
   537
slouken@8055
   538
    return env;
slouken@8055
   539
}
slouken@8055
   540
slouken@8055
   541
int Android_JNI_SetupThread(void)
slouken@8055
   542
{
slouken@8055
   543
    Android_JNI_GetEnv();
gabomdq@6354
   544
    return 1;
gabomdq@6354
   545
}
gabomdq@6354
   546
gabomdq@7663
   547
/*
gabomdq@7663
   548
 * Audio support
gabomdq@7663
   549
 */
slouken@4996
   550
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   551
static jboolean audioBufferStereo = JNI_FALSE;
slouken@4996
   552
static jobject audioBuffer = NULL;
slouken@4996
   553
static void* audioBufferPinned = NULL;
slouken@4995
   554
ewing@7501
   555
int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   556
{
slouken@4996
   557
    int audioBufferFrames;
slouken@4964
   558
gabomdq@6354
   559
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@6354
   560
gabomdq@6354
   561
    if (!env) {
gabomdq@6354
   562
        LOGE("callback_handler: failed to attach current thread");
icculus@5996
   563
    }
gabomdq@6354
   564
    Android_JNI_SetupThread();
icculus@5996
   565
slouken@4996
   566
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4996
   567
    audioBuffer16Bit = is16Bit;
slouken@4996
   568
    audioBufferStereo = channelCount > 1;
slouken@4964
   569
gabomdq@7552
   570
    if ((*env)->CallStaticIntMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
gabomdq@7552
   571
        /* Error during audio initialization */
gabomdq@7552
   572
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
gabomdq@7552
   573
        return 0;
gabomdq@7552
   574
    }
gabomdq@6802
   575
gabomdq@6802
   576
    /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
gabomdq@6802
   577
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
slouken@7191
   578
gabomdq@6802
   579
    if (is16Bit) {
ewing@7501
   580
        jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
gabomdq@6802
   581
        if (audioBufferLocal) {
ewing@7501
   582
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
ewing@7501
   583
            (*env)->DeleteLocalRef(env, audioBufferLocal);
gabomdq@6802
   584
        }
gabomdq@6802
   585
    }
gabomdq@6802
   586
    else {
ewing@7501
   587
        jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
gabomdq@6802
   588
        if (audioBufferLocal) {
ewing@7501
   589
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
ewing@7501
   590
            (*env)->DeleteLocalRef(env, audioBufferLocal);
gabomdq@6802
   591
        }
gabomdq@6802
   592
    }
slouken@4995
   593
slouken@4996
   594
    if (audioBuffer == NULL) {
gabomdq@6802
   595
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: could not allocate an audio buffer!");
slouken@4996
   596
        return 0;
slouken@4996
   597
    }
slouken@4995
   598
slouken@4996
   599
    jboolean isCopy = JNI_FALSE;
slouken@4996
   600
    if (audioBuffer16Bit) {
ewing@7501
   601
        audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
ewing@7501
   602
        audioBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
slouken@4996
   603
    } else {
ewing@7501
   604
        audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
ewing@7501
   605
        audioBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
slouken@4996
   606
    }
slouken@4996
   607
    if (audioBufferStereo) {
slouken@4996
   608
        audioBufferFrames /= 2;
slouken@4996
   609
    }
gabomdq@6864
   610
slouken@4996
   611
    return audioBufferFrames;
slouken@4995
   612
}
slouken@4995
   613
ewing@7501
   614
void * Android_JNI_GetAudioBuffer()
slouken@4995
   615
{
slouken@4996
   616
    return audioBufferPinned;
slouken@4995
   617
}
slouken@4995
   618
ewing@7501
   619
void Android_JNI_WriteAudioBuffer()
slouken@4995
   620
{
gabomdq@6354
   621
    JNIEnv *mAudioEnv = Android_JNI_GetEnv();
gabomdq@6354
   622
slouken@4996
   623
    if (audioBuffer16Bit) {
ewing@7501
   624
        (*mAudioEnv)->ReleaseShortArrayElements(mAudioEnv, (jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
ewing@7501
   625
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   626
    } else {
ewing@7501
   627
        (*mAudioEnv)->ReleaseByteArrayElements(mAudioEnv, (jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
ewing@7501
   628
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   629
    }
slouken@4995
   630
slouken@4996
   631
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   632
}
slouken@4995
   633
ewing@7501
   634
void Android_JNI_CloseAudioDevice()
slouken@4995
   635
{
gabomdq@6354
   636
    JNIEnv *env = Android_JNI_GetEnv();
icculus@5996
   637
ewing@7501
   638
    (*env)->CallStaticVoidMethod(env, mActivityClass, midAudioQuit);
slouken@4964
   639
slouken@4997
   640
    if (audioBuffer) {
ewing@7501
   641
        (*env)->DeleteGlobalRef(env, audioBuffer);
slouken@4997
   642
        audioBuffer = NULL;
slouken@4997
   643
        audioBufferPinned = NULL;
slouken@4997
   644
    }
slouken@4964
   645
}
slouken@4981
   646
gabomdq@7663
   647
/* Test for an exception and call SDL_SetError with its detail if one occurs */
gabomdq@7663
   648
/* If the parameter silent is truthy then SDL_SetError() will not be called. */
ewing@7501
   649
static bool Android_JNI_ExceptionOccurred(bool silent)
tim@5650
   650
{
ewing@7501
   651
    SDL_assert(LocalReferenceHolder_IsActive());
gabomdq@6354
   652
    JNIEnv *mEnv = Android_JNI_GetEnv();
slouken@6284
   653
ewing@7501
   654
    jthrowable exception = (*mEnv)->ExceptionOccurred(mEnv);
tim@5650
   655
    if (exception != NULL) {
tim@5650
   656
        jmethodID mid;
tim@5650
   657
gabomdq@7663
   658
        /* Until this happens most JNI operations have undefined behaviour */
ewing@7501
   659
        (*mEnv)->ExceptionClear(mEnv);
tim@5650
   660
slouken@7045
   661
        if (!silent) {
ewing@7501
   662
            jclass exceptionClass = (*mEnv)->GetObjectClass(mEnv, exception);
ewing@7501
   663
            jclass classClass = (*mEnv)->FindClass(mEnv, "java/lang/Class");
tim@5650
   664
ewing@7501
   665
            mid = (*mEnv)->GetMethodID(mEnv, classClass, "getName", "()Ljava/lang/String;");
ewing@7501
   666
            jstring exceptionName = (jstring)(*mEnv)->CallObjectMethod(mEnv, exceptionClass, mid);
ewing@7501
   667
            const char* exceptionNameUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionName, 0);
tim@5650
   668
ewing@7501
   669
            mid = (*mEnv)->GetMethodID(mEnv, exceptionClass, "getMessage", "()Ljava/lang/String;");
ewing@7501
   670
            jstring exceptionMessage = (jstring)(*mEnv)->CallObjectMethod(mEnv, exception, mid);
tim@5650
   671
slouken@7045
   672
            if (exceptionMessage != NULL) {
ewing@7501
   673
                const char* exceptionMessageUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionMessage, 0);
slouken@7045
   674
                SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
ewing@7501
   675
                (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionMessage, exceptionMessageUTF8);
slouken@7045
   676
            } else {
slouken@7045
   677
                SDL_SetError("%s", exceptionNameUTF8);
slouken@7045
   678
            }
slouken@7045
   679
ewing@7501
   680
            (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionName, exceptionNameUTF8);
tim@5650
   681
        }
tim@5650
   682
tim@5650
   683
        return true;
tim@5650
   684
    }
tim@5650
   685
tim@5650
   686
    return false;
tim@5650
   687
}
tim@5650
   688
ewing@7501
   689
static int Internal_Android_JNI_FileOpen(SDL_RWops* ctx)
icculus@5582
   690
{
ewing@7501
   691
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
ewing@7501
   692
tim@5650
   693
    int result = 0;
tim@5650
   694
tim@5650
   695
    jmethodID mid;
tim@5650
   696
    jobject context;
tim@5650
   697
    jobject assetManager;
tim@5650
   698
    jobject inputStream;
tim@5650
   699
    jclass channels;
tim@5650
   700
    jobject readableByteChannel;
tim@5650
   701
    jstring fileNameJString;
gabomdq@6806
   702
    jobject fd;
gabomdq@6806
   703
    jclass fdCls;
gabomdq@6806
   704
    jfieldID descriptor;
tim@5650
   705
gabomdq@6354
   706
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   707
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
tim@5650
   708
        goto failure;
tim@5650
   709
    }
tim@5650
   710
gabomdq@6308
   711
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
gabomdq@6806
   712
    ctx->hidden.androidio.position = 0;
icculus@5582
   713
gabomdq@7663
   714
    /* context = SDLActivity.getContext(); */
ewing@7501
   715
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@5582
   716
            "getContext","()Landroid/content/Context;");
ewing@7501
   717
    context = (*mEnv)->CallStaticObjectMethod(mEnv, mActivityClass, mid);
slouken@7191
   718
icculus@5582
   719
gabomdq@7663
   720
    /* assetManager = context.getAssets(); */
ewing@7501
   721
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
tim@5650
   722
            "getAssets", "()Landroid/content/res/AssetManager;");
ewing@7501
   723
    assetManager = (*mEnv)->CallObjectMethod(mEnv, context, mid);
icculus@5582
   724
gabomdq@6806
   725
    /* First let's try opening the file to obtain an AssetFileDescriptor.
gabomdq@6806
   726
    * This method reads the files directly from the APKs using standard *nix calls
gabomdq@6806
   727
    */
ewing@7501
   728
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager), "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;");
ewing@7501
   729
    inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString);
slouken@7045
   730
    if (Android_JNI_ExceptionOccurred(true)) {
gabomdq@6806
   731
        goto fallback;
icculus@5582
   732
    }
icculus@5582
   733
ewing@7501
   734
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getStartOffset", "()J");
ewing@7501
   735
    ctx->hidden.androidio.offset = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
slouken@7045
   736
    if (Android_JNI_ExceptionOccurred(true)) {
gabomdq@6806
   737
        goto fallback;
icculus@5582
   738
    }
icculus@5582
   739
ewing@7501
   740
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getDeclaredLength", "()J");
ewing@7501
   741
    ctx->hidden.androidio.size = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
slouken@7045
   742
    if (Android_JNI_ExceptionOccurred(true)) {
gabomdq@6806
   743
        goto fallback;
icculus@5582
   744
    }
icculus@5582
   745
ewing@7501
   746
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
ewing@7501
   747
    fd = (*mEnv)->CallObjectMethod(mEnv, inputStream, mid);
ewing@7501
   748
    fdCls = (*mEnv)->GetObjectClass(mEnv, fd);
ewing@7501
   749
    descriptor = (*mEnv)->GetFieldID(mEnv, fdCls, "descriptor", "I");
ewing@7501
   750
    ctx->hidden.androidio.fd = (*mEnv)->GetIntField(mEnv, fd, descriptor);
ewing@7501
   751
    ctx->hidden.androidio.assetFileDescriptorRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
tim@5650
   752
gabomdq@7663
   753
    /* Seek to the correct offset in the file. */
gabomdq@6816
   754
    lseek(ctx->hidden.androidio.fd, (off_t)ctx->hidden.androidio.offset, SEEK_SET);
gabomdq@6816
   755
gabomdq@6806
   756
    if (false) {
gabomdq@6806
   757
fallback:
gabomdq@7663
   758
        /* Disabled log message because of spam on the Nexus 7 */
gabomdq@7678
   759
        /* __android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file"); */
slouken@7190
   760
gabomdq@6806
   761
        /* Try the old method using InputStream */
gabomdq@6806
   762
        ctx->hidden.androidio.assetFileDescriptorRef = NULL;
icculus@5582
   763
gabomdq@7663
   764
        /* inputStream = assetManager.open(<filename>); */
ewing@7501
   765
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager),
gabomdq@6806
   766
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
gabomdq@7678
   767
        inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /* ACCESS_RANDOM */);
ewing@7501
   768
        if (Android_JNI_ExceptionOccurred(false)) {
alexey@8896
   769
            // Try fallback to APK Extension files
alexey@8896
   770
            mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
alexey@8896
   771
                "openAPKExtensionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
alexey@8896
   772
            inputStream = (*mEnv)->CallObjectMethod(mEnv, context, mid, fileNameJString);
alexey@8896
   773
alexey@8896
   774
            if (Android_JNI_ExceptionOccurred(false)) {
alexey@8896
   775
                goto failure;
alexey@8896
   776
            }
gabomdq@6806
   777
        }
gabomdq@6806
   778
ewing@7501
   779
        ctx->hidden.androidio.inputStreamRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
gabomdq@6806
   780
gabomdq@7663
   781
        /* Despite all the visible documentation on [Asset]InputStream claiming
gabomdq@7663
   782
         * that the .available() method is not guaranteed to return the entire file
gabomdq@7663
   783
         * size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
gabomdq@7663
   784
         * android/apis/content/ReadAsset.java imply that Android's
gabomdq@7663
   785
         * AssetInputStream.available() /will/ always return the total file size
gabomdq@7663
   786
        */
gabomdq@7663
   787
        
gabomdq@7663
   788
        /* size = inputStream.available(); */
ewing@7501
   789
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
   790
                "available", "()I");
ewing@7501
   791
        ctx->hidden.androidio.size = (long)(*mEnv)->CallIntMethod(mEnv, inputStream, mid);
ewing@7501
   792
        if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   793
            goto failure;
gabomdq@6806
   794
        }
gabomdq@6806
   795
gabomdq@7663
   796
        /* readableByteChannel = Channels.newChannel(inputStream); */
ewing@7501
   797
        channels = (*mEnv)->FindClass(mEnv, "java/nio/channels/Channels");
ewing@7501
   798
        mid = (*mEnv)->GetStaticMethodID(mEnv, channels,
gabomdq@6806
   799
                "newChannel",
gabomdq@6806
   800
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
ewing@7501
   801
        readableByteChannel = (*mEnv)->CallStaticObjectMethod(
ewing@7501
   802
                mEnv, channels, mid, inputStream);
ewing@7501
   803
        if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   804
            goto failure;
gabomdq@6806
   805
        }
gabomdq@6806
   806
gabomdq@6806
   807
        ctx->hidden.androidio.readableByteChannelRef =
ewing@7501
   808
            (*mEnv)->NewGlobalRef(mEnv, readableByteChannel);
gabomdq@6806
   809
gabomdq@7663
   810
        /* Store .read id for reading purposes */
ewing@7501
   811
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, readableByteChannel),
gabomdq@6806
   812
                "read", "(Ljava/nio/ByteBuffer;)I");
gabomdq@6806
   813
        ctx->hidden.androidio.readMethod = mid;
gabomdq@6806
   814
    }
icculus@5582
   815
tim@5650
   816
    if (false) {
tim@5650
   817
failure:
tim@5650
   818
        result = -1;
tim@5650
   819
ewing@7501
   820
        (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
tim@5650
   821
tim@5650
   822
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
ewing@7501
   823
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
tim@5650
   824
        }
gabomdq@6308
   825
gabomdq@6308
   826
        if(ctx->hidden.androidio.readableByteChannelRef != NULL) {
ewing@7501
   827
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
gabomdq@6308
   828
        }
gabomdq@6308
   829
gabomdq@6806
   830
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
ewing@7501
   831
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
gabomdq@6806
   832
        }
gabomdq@6806
   833
tim@5650
   834
    }
ewing@7501
   835
    
ewing@7501
   836
    LocalReferenceHolder_Cleanup(&refs);
tim@5650
   837
    return result;
icculus@5582
   838
}
icculus@5582
   839
ewing@7501
   840
int Android_JNI_FileOpen(SDL_RWops* ctx,
ewing@7501
   841
        const char* fileName, const char* mode)
icculus@5582
   842
{
ewing@7501
   843
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
gabomdq@6354
   844
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   845
    int retval;
slouken@6284
   846
ewing@7501
   847
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
   848
        LocalReferenceHolder_Cleanup(&refs);        
slouken@6284
   849
        return -1;
slouken@6284
   850
    }
slouken@6284
   851
icculus@5582
   852
    if (!ctx) {
ewing@7501
   853
        LocalReferenceHolder_Cleanup(&refs);
icculus@5582
   854
        return -1;
icculus@5582
   855
    }
icculus@5582
   856
ewing@7501
   857
    jstring fileNameJString = (*mEnv)->NewStringUTF(mEnv, fileName);
ewing@7501
   858
    ctx->hidden.androidio.fileNameRef = (*mEnv)->NewGlobalRef(mEnv, fileNameJString);
tim@5650
   859
    ctx->hidden.androidio.inputStreamRef = NULL;
gabomdq@6335
   860
    ctx->hidden.androidio.readableByteChannelRef = NULL;
gabomdq@6335
   861
    ctx->hidden.androidio.readMethod = NULL;
gabomdq@6806
   862
    ctx->hidden.androidio.assetFileDescriptorRef = NULL;
icculus@5582
   863
ewing@7501
   864
    retval = Internal_Android_JNI_FileOpen(ctx);
ewing@7501
   865
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
   866
    return retval;
icculus@5582
   867
}
icculus@5582
   868
ewing@7501
   869
size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
icculus@5582
   870
        size_t size, size_t maxnum)
icculus@5582
   871
{
ewing@7501
   872
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
icculus@5582
   873
gabomdq@6806
   874
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
   875
        size_t bytesMax = size * maxnum;
gabomdq@7678
   876
        if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && ctx->hidden.androidio.position + bytesMax > ctx->hidden.androidio.size) {
gabomdq@6806
   877
            bytesMax = ctx->hidden.androidio.size - ctx->hidden.androidio.position;
gabomdq@6806
   878
        }
gabomdq@6806
   879
        size_t result = read(ctx->hidden.androidio.fd, buffer, bytesMax );
gabomdq@6806
   880
        if (result > 0) {
gabomdq@6806
   881
            ctx->hidden.androidio.position += result;
ewing@7501
   882
            LocalReferenceHolder_Cleanup(&refs);
gabomdq@6806
   883
            return result / size;
gabomdq@6806
   884
        }
ewing@7501
   885
        LocalReferenceHolder_Cleanup(&refs);
gabomdq@6806
   886
        return 0;
gabomdq@6806
   887
    } else {
gabomdq@6806
   888
        jlong bytesRemaining = (jlong) (size * maxnum);
gabomdq@6806
   889
        jlong bytesMax = (jlong) (ctx->hidden.androidio.size -  ctx->hidden.androidio.position);
gabomdq@6806
   890
        int bytesRead = 0;
gabomdq@6377
   891
gabomdq@6806
   892
        /* Don't read more bytes than those that remain in the file, otherwise we get an exception */
gabomdq@6806
   893
        if (bytesRemaining >  bytesMax) bytesRemaining = bytesMax;
slouken@6284
   894
gabomdq@6806
   895
        JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   896
        if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
   897
            LocalReferenceHolder_Cleanup(&refs);            
philipp@7368
   898
            return 0;
icculus@5582
   899
        }
icculus@5582
   900
gabomdq@6806
   901
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
gabomdq@6806
   902
        jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
ewing@7501
   903
        jobject byteBuffer = (*mEnv)->NewDirectByteBuffer(mEnv, buffer, bytesRemaining);
gabomdq@6806
   904
gabomdq@6806
   905
        while (bytesRemaining > 0) {
gabomdq@7663
   906
            /* result = readableByteChannel.read(...); */
ewing@7501
   907
            int result = (*mEnv)->CallIntMethod(mEnv, readableByteChannel, readMethod, byteBuffer);
gabomdq@6806
   908
ewing@7501
   909
            if (Android_JNI_ExceptionOccurred(false)) {
ewing@7501
   910
                LocalReferenceHolder_Cleanup(&refs);            
gabomdq@6806
   911
                return 0;
gabomdq@6806
   912
            }
gabomdq@6806
   913
gabomdq@6806
   914
            if (result < 0) {
gabomdq@6806
   915
                break;
gabomdq@6806
   916
            }
gabomdq@6806
   917
gabomdq@6806
   918
            bytesRemaining -= result;
gabomdq@6806
   919
            bytesRead += result;
gabomdq@6806
   920
            ctx->hidden.androidio.position += result;
icculus@5582
   921
        }
ewing@7501
   922
        LocalReferenceHolder_Cleanup(&refs);                    
gabomdq@6806
   923
        return bytesRead / size;
slouken@7191
   924
    }
icculus@5582
   925
}
icculus@5582
   926
ewing@7501
   927
size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
icculus@5582
   928
        size_t size, size_t num)
icculus@5582
   929
{
icculus@5582
   930
    SDL_SetError("Cannot write to Android package filesystem");
icculus@5582
   931
    return 0;
icculus@5582
   932
}
icculus@5582
   933
ewing@7501
   934
static int Internal_Android_JNI_FileClose(SDL_RWops* ctx, bool release)
icculus@5582
   935
{
ewing@7501
   936
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
ewing@7501
   937
icculus@5582
   938
    int result = 0;
gabomdq@6354
   939
    JNIEnv *mEnv = Android_JNI_GetEnv();
icculus@5582
   940
ewing@7501
   941
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
   942
        LocalReferenceHolder_Cleanup(&refs);
icculus@7037
   943
        return SDL_SetError("Failed to allocate enough JVM local references");
slouken@6284
   944
    }
slouken@6284
   945
icculus@5582
   946
    if (ctx) {
icculus@5582
   947
        if (release) {
ewing@7501
   948
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   949
        }
icculus@5582
   950
gabomdq@6806
   951
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
   952
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
ewing@7501
   953
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
   954
                    "close", "()V");
ewing@7501
   955
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
ewing@7501
   956
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
ewing@7501
   957
            if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   958
                result = -1;
gabomdq@6806
   959
            }
gabomdq@6806
   960
        }
gabomdq@6806
   961
        else {
gabomdq@6806
   962
            jobject inputStream = (jobject)ctx->hidden.androidio.inputStreamRef;
icculus@5582
   963
gabomdq@7677
   964
            /* inputStream.close(); */
ewing@7501
   965
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
   966
                    "close", "()V");
ewing@7501
   967
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
ewing@7501
   968
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
ewing@7501
   969
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
ewing@7501
   970
            if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   971
                result = -1;
gabomdq@6806
   972
            }
icculus@5582
   973
        }
icculus@5582
   974
icculus@5582
   975
        if (release) {
icculus@5582
   976
            SDL_FreeRW(ctx);
icculus@5582
   977
        }
icculus@5582
   978
    }
icculus@5582
   979
ewing@7501
   980
    LocalReferenceHolder_Cleanup(&refs);
icculus@5582
   981
    return result;
icculus@5582
   982
}
icculus@5582
   983
icculus@5582
   984
ewing@7501
   985
Sint64 Android_JNI_FileSize(SDL_RWops* ctx)
icculus@5582
   986
{
slouken@6642
   987
    return ctx->hidden.androidio.size;
slouken@6642
   988
}
slouken@6642
   989
ewing@7501
   990
Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence)
slouken@6642
   991
{
gabomdq@6806
   992
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
   993
        switch (whence) {
gabomdq@6806
   994
            case RW_SEEK_SET:
gabomdq@7678
   995
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
gabomdq@6806
   996
                offset += ctx->hidden.androidio.offset;
gabomdq@6806
   997
                break;
gabomdq@6806
   998
            case RW_SEEK_CUR:
gabomdq@6806
   999
                offset += ctx->hidden.androidio.position;
gabomdq@7678
  1000
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
gabomdq@6806
  1001
                offset += ctx->hidden.androidio.offset;
gabomdq@6806
  1002
                break;
gabomdq@6806
  1003
            case RW_SEEK_END:
gabomdq@6806
  1004
                offset = ctx->hidden.androidio.offset + ctx->hidden.androidio.size + offset;
gabomdq@6806
  1005
                break;
gabomdq@6806
  1006
            default:
icculus@7037
  1007
                return SDL_SetError("Unknown value for 'whence'");
gabomdq@6806
  1008
        }
gabomdq@6806
  1009
        whence = SEEK_SET;
icculus@5582
  1010
gabomdq@6806
  1011
        off_t ret = lseek(ctx->hidden.androidio.fd, (off_t)offset, SEEK_SET);
gabomdq@6806
  1012
        if (ret == -1) return -1;
gabomdq@6806
  1013
        ctx->hidden.androidio.position = ret - ctx->hidden.androidio.offset;
gabomdq@6806
  1014
    } else {
gabomdq@6806
  1015
        Sint64 newPosition;
gabomdq@6806
  1016
gabomdq@6806
  1017
        switch (whence) {
gabomdq@6806
  1018
            case RW_SEEK_SET:
gabomdq@6806
  1019
                newPosition = offset;
gabomdq@6806
  1020
                break;
gabomdq@6806
  1021
            case RW_SEEK_CUR:
gabomdq@6806
  1022
                newPosition = ctx->hidden.androidio.position + offset;
gabomdq@6806
  1023
                break;
gabomdq@6806
  1024
            case RW_SEEK_END:
gabomdq@6806
  1025
                newPosition = ctx->hidden.androidio.size + offset;
gabomdq@6806
  1026
                break;
gabomdq@6806
  1027
            default:
icculus@7037
  1028
                return SDL_SetError("Unknown value for 'whence'");
gabomdq@6806
  1029
        }
gabomdq@6806
  1030
gabomdq@6806
  1031
        /* Validate the new position */
gabomdq@6806
  1032
        if (newPosition < 0) {
icculus@7037
  1033
            return SDL_Error(SDL_EFSEEK);
gabomdq@6806
  1034
        }
gabomdq@6806
  1035
        if (newPosition > ctx->hidden.androidio.size) {
gabomdq@6806
  1036
            newPosition = ctx->hidden.androidio.size;
gabomdq@6806
  1037
        }
slouken@6642
  1038
gabomdq@6806
  1039
        Sint64 movement = newPosition - ctx->hidden.androidio.position;
gabomdq@6806
  1040
        if (movement > 0) {
gabomdq@6806
  1041
            unsigned char buffer[4096];
icculus@5582
  1042
gabomdq@7663
  1043
            /* The easy case where we're seeking forwards */
gabomdq@6806
  1044
            while (movement > 0) {
gabomdq@6806
  1045
                Sint64 amount = sizeof (buffer);
gabomdq@6806
  1046
                if (amount > movement) {
gabomdq@6806
  1047
                    amount = movement;
gabomdq@6806
  1048
                }
gabomdq@6806
  1049
                size_t result = Android_JNI_FileRead(ctx, buffer, 1, amount);
gabomdq@6806
  1050
                if (result <= 0) {
gabomdq@7663
  1051
                    /* Failed to read/skip the required amount, so fail */
gabomdq@6806
  1052
                    return -1;
gabomdq@6806
  1053
                }
tim@5993
  1054
gabomdq@6806
  1055
                movement -= result;
icculus@5582
  1056
            }
tim@5993
  1057
gabomdq@6806
  1058
        } else if (movement < 0) {
gabomdq@7663
  1059
            /* We can't seek backwards so we have to reopen the file and seek */
gabomdq@7663
  1060
            /* forwards which obviously isn't very efficient */
ewing@7501
  1061
            Internal_Android_JNI_FileClose(ctx, false);
ewing@7501
  1062
            Internal_Android_JNI_FileOpen(ctx);
gabomdq@6806
  1063
            Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
icculus@5582
  1064
        }
icculus@5582
  1065
    }
icculus@5582
  1066
icculus@5582
  1067
    return ctx->hidden.androidio.position;
slouken@7191
  1068
icculus@5582
  1069
}
icculus@5582
  1070
ewing@7501
  1071
int Android_JNI_FileClose(SDL_RWops* ctx)
icculus@5582
  1072
{
ewing@7501
  1073
    return Internal_Android_JNI_FileClose(ctx, true);
icculus@5582
  1074
}
icculus@5582
  1075
gabomdq@7663
  1076
/* returns a new global reference which needs to be released later */
slouken@6464
  1077
static jobject Android_JNI_GetSystemServiceObject(const char* name)
slouken@6464
  1078
{
ewing@7501
  1079
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6464
  1080
    JNIEnv* env = Android_JNI_GetEnv();
ewing@7501
  1081
    jobject retval = NULL;
ewing@7501
  1082
ewing@7501
  1083
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1084
        LocalReferenceHolder_Cleanup(&refs);
slouken@6464
  1085
        return NULL;
slouken@6464
  1086
    }
slouken@6464
  1087
ewing@7501
  1088
    jstring service = (*env)->NewStringUTF(env, name);
slouken@6464
  1089
slouken@6464
  1090
    jmethodID mid;
slouken@6464
  1091
ewing@7501
  1092
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
ewing@7501
  1093
    jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6464
  1094
philipp@8029
  1095
    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemServiceFromUiThread", "(Ljava/lang/String;)Ljava/lang/Object;");
ewing@7501
  1096
    jobject manager = (*env)->CallObjectMethod(env, context, mid, service);
slouken@6464
  1097
ewing@7501
  1098
    (*env)->DeleteLocalRef(env, service);
slouken@6464
  1099
ewing@7501
  1100
    retval = manager ? (*env)->NewGlobalRef(env, manager) : NULL;
ewing@7501
  1101
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1102
    return retval;
slouken@6464
  1103
}
slouken@6464
  1104
slouken@6464
  1105
#define SETUP_CLIPBOARD(error) \
ewing@7501
  1106
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__); \
slouken@6464
  1107
    JNIEnv* env = Android_JNI_GetEnv(); \
ewing@7501
  1108
    if (!LocalReferenceHolder_Init(&refs, env)) { \
ewing@7501
  1109
        LocalReferenceHolder_Cleanup(&refs); \
slouken@6464
  1110
        return error; \
slouken@6464
  1111
    } \
slouken@6464
  1112
    jobject clipboard = Android_JNI_GetSystemServiceObject("clipboard"); \
slouken@6464
  1113
    if (!clipboard) { \
ewing@7501
  1114
        LocalReferenceHolder_Cleanup(&refs); \
slouken@6464
  1115
        return error; \
slouken@6464
  1116
    }
slouken@6464
  1117
ewing@7501
  1118
#define CLEANUP_CLIPBOARD() \
ewing@7501
  1119
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1120
ewing@7501
  1121
int Android_JNI_SetClipboardText(const char* text)
slouken@6464
  1122
{
slouken@6464
  1123
    SETUP_CLIPBOARD(-1)
slouken@6464
  1124
ewing@7501
  1125
    jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "setText", "(Ljava/lang/CharSequence;)V");
ewing@7501
  1126
    jstring string = (*env)->NewStringUTF(env, text);
ewing@7501
  1127
    (*env)->CallVoidMethod(env, clipboard, mid, string);
ewing@7501
  1128
    (*env)->DeleteGlobalRef(env, clipboard);
ewing@7501
  1129
    (*env)->DeleteLocalRef(env, string);
ewing@7501
  1130
ewing@7501
  1131
    CLEANUP_CLIPBOARD();
ewing@7501
  1132
slouken@6464
  1133
    return 0;
slouken@6464
  1134
}
slouken@6464
  1135
ewing@7501
  1136
char* Android_JNI_GetClipboardText()
slouken@6464
  1137
{
slouken@6464
  1138
    SETUP_CLIPBOARD(SDL_strdup(""))
slouken@6464
  1139
ewing@7501
  1140
    jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "getText", "()Ljava/lang/CharSequence;");
ewing@7501
  1141
    jobject sequence = (*env)->CallObjectMethod(env, clipboard, mid);
ewing@7501
  1142
    (*env)->DeleteGlobalRef(env, clipboard);
slouken@6464
  1143
    if (sequence) {
ewing@7501
  1144
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, sequence), "toString", "()Ljava/lang/String;");
ewing@7501
  1145
        jstring string = (jstring)((*env)->CallObjectMethod(env, sequence, mid));
ewing@7501
  1146
        const char* utf = (*env)->GetStringUTFChars(env, string, 0);
slouken@6464
  1147
        if (utf) {
slouken@6464
  1148
            char* text = SDL_strdup(utf);
ewing@7501
  1149
            (*env)->ReleaseStringUTFChars(env, string, utf);
ewing@7501
  1150
ewing@7501
  1151
            CLEANUP_CLIPBOARD();
ewing@7501
  1152
slouken@6464
  1153
            return text;
slouken@6464
  1154
        }
slouken@6464
  1155
    }
ewing@7501
  1156
ewing@7501
  1157
    CLEANUP_CLIPBOARD();    
ewing@7501
  1158
slouken@6464
  1159
    return SDL_strdup("");
slouken@6464
  1160
}
slouken@6464
  1161
ewing@7501
  1162
SDL_bool Android_JNI_HasClipboardText()
slouken@6464
  1163
{
slouken@6464
  1164
    SETUP_CLIPBOARD(SDL_FALSE)
slouken@6464
  1165
ewing@7501
  1166
    jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "hasText", "()Z");
ewing@7501
  1167
    jboolean has = (*env)->CallBooleanMethod(env, clipboard, mid);
ewing@7501
  1168
    (*env)->DeleteGlobalRef(env, clipboard);
ewing@7501
  1169
ewing@7501
  1170
    CLEANUP_CLIPBOARD();
ewing@7501
  1171
    
slouken@6464
  1172
    return has ? SDL_TRUE : SDL_FALSE;
slouken@6464
  1173
}
slouken@6464
  1174
slouken@6464
  1175
gabomdq@7663
  1176
/* returns 0 on success or -1 on error (others undefined then)
gabomdq@7663
  1177
 * returns truthy or falsy value in plugged, charged and battery
gabomdq@7663
  1178
 * returns the value in seconds and percent or -1 if not available
gabomdq@7663
  1179
 */
ewing@7501
  1180
int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
slouken@6448
  1181
{
ewing@7501
  1182
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6448
  1183
    JNIEnv* env = Android_JNI_GetEnv();
ewing@7501
  1184
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1185
        LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1186
        return -1;
slouken@6448
  1187
    }
slouken@6448
  1188
slouken@6448
  1189
    jmethodID mid;
slouken@6448
  1190
ewing@7501
  1191
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
ewing@7501
  1192
    jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6448
  1193
ewing@7501
  1194
    jstring action = (*env)->NewStringUTF(env, "android.intent.action.BATTERY_CHANGED");
slouken@6448
  1195
ewing@7501
  1196
    jclass cls = (*env)->FindClass(env, "android/content/IntentFilter");
slouken@6448
  1197
ewing@7501
  1198
    mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
ewing@7501
  1199
    jobject filter = (*env)->NewObject(env, cls, mid, action);
slouken@6448
  1200
ewing@7501
  1201
    (*env)->DeleteLocalRef(env, action);
slouken@6448
  1202
ewing@7501
  1203
    mid = (*env)->GetMethodID(env, mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;");
ewing@7501
  1204
    jobject intent = (*env)->CallObjectMethod(env, context, mid, NULL, filter);
slouken@6448
  1205
ewing@7501
  1206
    (*env)->DeleteLocalRef(env, filter);
slouken@6448
  1207
ewing@7501
  1208
    cls = (*env)->GetObjectClass(env, intent);
slouken@6448
  1209
slouken@6448
  1210
    jstring iname;
ewing@7501
  1211
    jmethodID imid = (*env)->GetMethodID(env, cls, "getIntExtra", "(Ljava/lang/String;I)I");
slouken@6448
  1212
slouken@6448
  1213
#define GET_INT_EXTRA(var, key) \
ewing@7501
  1214
    iname = (*env)->NewStringUTF(env, key); \
ewing@7501
  1215
    int var = (*env)->CallIntMethod(env, intent, imid, iname, -1); \
ewing@7501
  1216
    (*env)->DeleteLocalRef(env, iname);
slouken@6448
  1217
slouken@6448
  1218
    jstring bname;
ewing@7501
  1219
    jmethodID bmid = (*env)->GetMethodID(env, cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
slouken@6448
  1220
slouken@6448
  1221
#define GET_BOOL_EXTRA(var, key) \
ewing@7501
  1222
    bname = (*env)->NewStringUTF(env, key); \
ewing@7501
  1223
    int var = (*env)->CallBooleanMethod(env, intent, bmid, bname, JNI_FALSE); \
ewing@7501
  1224
    (*env)->DeleteLocalRef(env, bname);
slouken@6448
  1225
slouken@6448
  1226
    if (plugged) {
gabomdq@7663
  1227
        GET_INT_EXTRA(plug, "plugged") /* == BatteryManager.EXTRA_PLUGGED (API 5) */
slouken@6448
  1228
        if (plug == -1) {
ewing@7501
  1229
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1230
            return -1;
slouken@6448
  1231
        }
gabomdq@7663
  1232
        /* 1 == BatteryManager.BATTERY_PLUGGED_AC */
gabomdq@7663
  1233
        /* 2 == BatteryManager.BATTERY_PLUGGED_USB */
slouken@6448
  1234
        *plugged = (0 < plug) ? 1 : 0;
slouken@6448
  1235
    }
slouken@6448
  1236
slouken@6448
  1237
    if (charged) {
gabomdq@7663
  1238
        GET_INT_EXTRA(status, "status") /* == BatteryManager.EXTRA_STATUS (API 5) */
slouken@6448
  1239
        if (status == -1) {
ewing@7501
  1240
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1241
            return -1;
slouken@6448
  1242
        }
gabomdq@7663
  1243
        /* 5 == BatteryManager.BATTERY_STATUS_FULL */
slouken@6448
  1244
        *charged = (status == 5) ? 1 : 0;
slouken@6448
  1245
    }
slouken@6448
  1246
slouken@6448
  1247
    if (battery) {
gabomdq@7663
  1248
        GET_BOOL_EXTRA(present, "present") /* == BatteryManager.EXTRA_PRESENT (API 5) */
slouken@6448
  1249
        *battery = present ? 1 : 0;
slouken@6448
  1250
    }
slouken@6448
  1251
slouken@6448
  1252
    if (seconds) {
gabomdq@7663
  1253
        *seconds = -1; /* not possible */
slouken@6448
  1254
    }
slouken@6448
  1255
slouken@6448
  1256
    if (percent) {
gabomdq@7663
  1257
        GET_INT_EXTRA(level, "level") /* == BatteryManager.EXTRA_LEVEL (API 5) */
gabomdq@7663
  1258
        GET_INT_EXTRA(scale, "scale") /* == BatteryManager.EXTRA_SCALE (API 5) */
slouken@6448
  1259
        if ((level == -1) || (scale == -1)) {
ewing@7501
  1260
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1261
            return -1;
slouken@6448
  1262
        }
slouken@6448
  1263
        *percent = level * 100 / scale;
slouken@6448
  1264
    }
slouken@6448
  1265
ewing@7501
  1266
    (*env)->DeleteLocalRef(env, intent);
slouken@6448
  1267
ewing@7501
  1268
    LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1269
    return 0;
slouken@6448
  1270
}
slouken@6448
  1271
philipp@7786
  1272
/* returns number of found touch devices as return value and ids in parameter ids */
philipp@7786
  1273
int Android_JNI_GetTouchDeviceIds(int **ids) {
philipp@7786
  1274
    JNIEnv *env = Android_JNI_GetEnv();
philipp@7786
  1275
    jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */
philipp@7786
  1276
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "inputGetInputDeviceIds", "(I)[I");
philipp@7786
  1277
    jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, mid, sources);
philipp@7786
  1278
    int number = 0;
philipp@7786
  1279
    *ids = NULL;
philipp@7786
  1280
    if (array) {
philipp@7786
  1281
        number = (int) (*env)->GetArrayLength(env, array);
philipp@7786
  1282
        if (0 < number) {
philipp@7786
  1283
            jint* elements = (*env)->GetIntArrayElements(env, array, NULL);
philipp@7786
  1284
            if (elements) {
philipp@7786
  1285
                int i;
slouken@7863
  1286
                *ids = SDL_malloc(number * sizeof (**ids));
philipp@7786
  1287
                for (i = 0; i < number; ++i) { /* not assuming sizeof (jint) == sizeof (int) */
slouken@7863
  1288
                    (*ids)[i] = elements[i];
philipp@7786
  1289
                }
philipp@7786
  1290
                (*env)->ReleaseIntArrayElements(env, array, elements, JNI_ABORT);
philipp@7786
  1291
            }
philipp@7786
  1292
        }
philipp@7786
  1293
        (*env)->DeleteLocalRef(env, array);
philipp@7786
  1294
    }
philipp@7786
  1295
    return number;
philipp@7786
  1296
}
philipp@7786
  1297
gabomdq@8057
  1298
void Android_JNI_PollInputDevices()
gabomdq@7907
  1299
{
gabomdq@8057
  1300
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@8057
  1301
    (*env)->CallStaticVoidMethod(env, mActivityClass, midPollInputDevices);    
gabomdq@7907
  1302
}
gabomdq@7907
  1303
gabomdq@7663
  1304
/* sends message to be handled on the UI event dispatch thread */
ewing@7501
  1305
int Android_JNI_SendMessage(int command, int param)
slouken@6392
  1306
{
slouken@6392
  1307
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6392
  1308
    if (!env) {
slouken@6392
  1309
        return -1;
slouken@6392
  1310
    }
ewing@7501
  1311
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
slouken@6392
  1312
    if (!mid) {
slouken@6392
  1313
        return -1;
slouken@6392
  1314
    }
ewing@7501
  1315
    jboolean success = (*env)->CallStaticBooleanMethod(env, mActivityClass, mid, command, param);
philipp@7149
  1316
    return success ? 0 : -1;
slouken@6392
  1317
}
slouken@6392
  1318
ewing@7501
  1319
void Android_JNI_ShowTextInput(SDL_Rect *inputRect)
slouken@6555
  1320
{
slouken@6555
  1321
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6555
  1322
    if (!env) {
slouken@6654
  1323
        return;
slouken@6555
  1324
    }
slouken@6555
  1325
ewing@7501
  1326
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "showTextInput", "(IIII)Z");
slouken@6555
  1327
    if (!mid) {
slouken@6654
  1328
        return;
slouken@6555
  1329
    }
ewing@7501
  1330
    (*env)->CallStaticBooleanMethod(env, mActivityClass, mid,
slouken@6555
  1331
                               inputRect->x,
slouken@6555
  1332
                               inputRect->y,
slouken@6555
  1333
                               inputRect->w,
slouken@6555
  1334
                               inputRect->h );
slouken@6555
  1335
}
slouken@6555
  1336
ewing@7501
  1337
void Android_JNI_HideTextInput()
slouken@6555
  1338
{
gabomdq@7663
  1339
    /* has to match Activity constant */
slouken@6654
  1340
    const int COMMAND_TEXTEDIT_HIDE = 3;
slouken@6654
  1341
    Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
slouken@6654
  1342
}
slouken@6555
  1343
gabomdq@7663
  1344
/*
slouken@6630
  1345
//////////////////////////////////////////////////////////////////////////////
slouken@6630
  1346
//
slouken@6630
  1347
// Functions exposed to SDL applications in SDL_system.h
gabomdq@7663
  1348
//////////////////////////////////////////////////////////////////////////////
gabomdq@7663
  1349
*/
slouken@6630
  1350
ewing@7501
  1351
void *SDL_AndroidGetJNIEnv()
slouken@6630
  1352
{
slouken@6630
  1353
    return Android_JNI_GetEnv();
slouken@6630
  1354
}
slouken@6630
  1355
gabomdq@7095
  1356
gabomdq@7083
  1357
ewing@7501
  1358
void *SDL_AndroidGetActivity()
slouken@6630
  1359
{
gabomdq@7095
  1360
    /* See SDL_system.h for caveats on using this function. */
slouken@7191
  1361
slouken@6630
  1362
    jmethodID mid;
slouken@6630
  1363
slouken@6630
  1364
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@7083
  1365
    if (!env) {
slouken@6630
  1366
        return NULL;
slouken@6630
  1367
    }
slouken@6630
  1368
gabomdq@7663
  1369
    /* return SDLActivity.getContext(); */
ewing@7501
  1370
    mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1371
            "getContext","()Landroid/content/Context;");
ewing@7501
  1372
    return (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1373
}
slouken@6630
  1374
ewing@7501
  1375
const char * SDL_AndroidGetInternalStoragePath()
slouken@6630
  1376
{
slouken@6630
  1377
    static char *s_AndroidInternalFilesPath = NULL;
slouken@6630
  1378
slouken@6630
  1379
    if (!s_AndroidInternalFilesPath) {
ewing@7501
  1380
        struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1381
        jmethodID mid;
slouken@6630
  1382
        jobject context;
slouken@6630
  1383
        jobject fileObject;
slouken@6630
  1384
        jstring pathString;
slouken@6630
  1385
        const char *path;
slouken@6630
  1386
slouken@6630
  1387
        JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1388
        if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1389
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1390
            return NULL;
slouken@6630
  1391
        }
slouken@6630
  1392
gabomdq@7663
  1393
        /* context = SDLActivity.getContext(); */
ewing@7501
  1394
        mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1395
                "getContext","()Landroid/content/Context;");
ewing@7501
  1396
        context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1397
gabomdq@7663
  1398
        /* fileObj = context.getFilesDir(); */
ewing@7501
  1399
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
slouken@6630
  1400
                "getFilesDir", "()Ljava/io/File;");
ewing@7501
  1401
        fileObject = (*env)->CallObjectMethod(env, context, mid);
slouken@6630
  1402
        if (!fileObject) {
slouken@6630
  1403
            SDL_SetError("Couldn't get internal directory");
ewing@7501
  1404
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1405
            return NULL;
slouken@6630
  1406
        }
slouken@6630
  1407
gabomdq@7663
  1408
        /* path = fileObject.getAbsolutePath(); */
ewing@7501
  1409
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
slouken@6630
  1410
                "getAbsolutePath", "()Ljava/lang/String;");
ewing@7501
  1411
        pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
slouken@6630
  1412
ewing@7501
  1413
        path = (*env)->GetStringUTFChars(env, pathString, NULL);
slouken@6630
  1414
        s_AndroidInternalFilesPath = SDL_strdup(path);
ewing@7501
  1415
        (*env)->ReleaseStringUTFChars(env, pathString, path);
ewing@7501
  1416
ewing@7501
  1417
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1418
    }
slouken@6630
  1419
    return s_AndroidInternalFilesPath;
slouken@6630
  1420
}
slouken@6630
  1421
ewing@7501
  1422
int SDL_AndroidGetExternalStorageState()
slouken@6630
  1423
{
ewing@7501
  1424
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1425
    jmethodID mid;
slouken@6630
  1426
    jclass cls;
slouken@6630
  1427
    jstring stateString;
slouken@6630
  1428
    const char *state;
slouken@6630
  1429
    int stateFlags;
slouken@6630
  1430
slouken@6630
  1431
    JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1432
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1433
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1434
        return 0;
slouken@6630
  1435
    }
slouken@6630
  1436
ewing@7501
  1437
    cls = (*env)->FindClass(env, "android/os/Environment");
ewing@7501
  1438
    mid = (*env)->GetStaticMethodID(env, cls,
slouken@6630
  1439
            "getExternalStorageState", "()Ljava/lang/String;");
ewing@7501
  1440
    stateString = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
slouken@6630
  1441
ewing@7501
  1442
    state = (*env)->GetStringUTFChars(env, stateString, NULL);
slouken@6630
  1443
gabomdq@7663
  1444
    /* Print an info message so people debugging know the storage state */
slouken@6630
  1445
    __android_log_print(ANDROID_LOG_INFO, "SDL", "external storage state: %s", state);
slouken@6630
  1446
slouken@6630
  1447
    if (SDL_strcmp(state, "mounted") == 0) {
slouken@6630
  1448
        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ |
slouken@6630
  1449
                     SDL_ANDROID_EXTERNAL_STORAGE_WRITE;
slouken@6630
  1450
    } else if (SDL_strcmp(state, "mounted_ro") == 0) {
slouken@6630
  1451
        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ;
slouken@6630
  1452
    } else {
slouken@6630
  1453
        stateFlags = 0;
slouken@6630
  1454
    }
ewing@7501
  1455
    (*env)->ReleaseStringUTFChars(env, stateString, state);
slouken@6630
  1456
ewing@7501
  1457
    LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1458
    return stateFlags;
slouken@6630
  1459
}
slouken@6630
  1460
ewing@7501
  1461
const char * SDL_AndroidGetExternalStoragePath()
slouken@6630
  1462
{
slouken@6630
  1463
    static char *s_AndroidExternalFilesPath = NULL;
slouken@6630
  1464
slouken@6630
  1465
    if (!s_AndroidExternalFilesPath) {
ewing@7501
  1466
        struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1467
        jmethodID mid;
slouken@6630
  1468
        jobject context;
slouken@6630
  1469
        jobject fileObject;
slouken@6630
  1470
        jstring pathString;
slouken@6630
  1471
        const char *path;
slouken@6630
  1472
slouken@6630
  1473
        JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1474
        if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1475
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1476
            return NULL;
slouken@6630
  1477
        }
slouken@6630
  1478
gabomdq@7663
  1479
        /* context = SDLActivity.getContext(); */
ewing@7501
  1480
        mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1481
                "getContext","()Landroid/content/Context;");
ewing@7501
  1482
        context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1483
gabomdq@7663
  1484
        /* fileObj = context.getExternalFilesDir(); */
ewing@7501
  1485
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
slouken@6630
  1486
                "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
ewing@7501
  1487
        fileObject = (*env)->CallObjectMethod(env, context, mid, NULL);
slouken@6630
  1488
        if (!fileObject) {
slouken@6630
  1489
            SDL_SetError("Couldn't get external directory");
ewing@7501
  1490
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1491
            return NULL;
slouken@6630
  1492
        }
slouken@6630
  1493
gabomdq@7663
  1494
        /* path = fileObject.getAbsolutePath(); */
ewing@7501
  1495
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
slouken@6630
  1496
                "getAbsolutePath", "()Ljava/lang/String;");
ewing@7501
  1497
        pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
slouken@6630
  1498
ewing@7501
  1499
        path = (*env)->GetStringUTFChars(env, pathString, NULL);
slouken@6630
  1500
        s_AndroidExternalFilesPath = SDL_strdup(path);
ewing@7501
  1501
        (*env)->ReleaseStringUTFChars(env, pathString, path);
ewing@7501
  1502
ewing@7501
  1503
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1504
    }
slouken@6630
  1505
    return s_AndroidExternalFilesPath;
slouken@6630
  1506
}
slouken@6630
  1507
slouken@6044
  1508
#endif /* __ANDROID__ */
slouken@6044
  1509
slouken@4981
  1510
/* vi: set ts=4 sw=4 expandtab: */
gabomdq@7612
  1511