File: JNIHelper.cpp

package info (click to toggle)
android-platform-development 7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 30,092 kB
  • sloc: ansic: 161,291; java: 15,681; cpp: 7,721; xml: 6,419; python: 5,456; sh: 1,748; lisp: 261; ruby: 183; asm: 132; perl: 88; makefile: 22
file content (376 lines) | stat: -rw-r--r-- 11,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <fstream>
#include <iostream>

#include "JNIHelper.h"

namespace ndk_helper
{

#define CLASS_NAME "android/app/NativeActivity"

//---------------------------------------------------------------------------
//JNI Helper functions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//Singleton
//---------------------------------------------------------------------------
JNIHelper* JNIHelper::GetInstance()
{
    static JNIHelper helper;
    return &helper;
}

//---------------------------------------------------------------------------
//Ctor
//---------------------------------------------------------------------------
JNIHelper::JNIHelper()
{
    pthread_mutex_init( &mutex_, NULL );
}

//---------------------------------------------------------------------------
//Dtor
//---------------------------------------------------------------------------
JNIHelper::~JNIHelper()
{
    pthread_mutex_lock( &mutex_ );

    JNIEnv *env;
    activity_->vm->AttachCurrentThread( &env, NULL );

    env->DeleteGlobalRef( jni_helper_java_ref_ );
    env->DeleteGlobalRef( jni_helper_java_class_ );

    activity_->vm->DetachCurrentThread();

    pthread_mutex_destroy( &mutex_ );
}

//---------------------------------------------------------------------------
//Init
//---------------------------------------------------------------------------
void JNIHelper::Init( ANativeActivity* activity,
        const char* helper_class_name )
{
    JNIHelper& helper = *GetInstance();
    pthread_mutex_lock( &helper.mutex_ );

    helper.activity_ = activity;

    JNIEnv *env;
    helper.activity_->vm->AttachCurrentThread( &env, NULL );

    //Retrieve app name
    jclass android_content_Context = env->GetObjectClass( helper.activity_->clazz );
    jmethodID midGetPackageName = env->GetMethodID( android_content_Context, "getPackageName",
            "()Ljava/lang/String;" );

    jstring packageName = (jstring) env->CallObjectMethod( helper.activity_->clazz,
            midGetPackageName );
    const char* appname = env->GetStringUTFChars( packageName, NULL );
    helper.app_name_ = std::string( appname );

    jclass cls = helper.RetrieveClass( env, helper_class_name );
    helper.jni_helper_java_class_ = (jclass) env->NewGlobalRef( cls );

    jmethodID constructor = env->GetMethodID( helper.jni_helper_java_class_, "<init>", "()V" );
    helper.jni_helper_java_ref_ = env->NewObject( helper.jni_helper_java_class_, constructor );
    helper.jni_helper_java_ref_ = env->NewGlobalRef( helper.jni_helper_java_ref_ );

    env->ReleaseStringUTFChars( packageName, appname );
    helper.activity_->vm->DetachCurrentThread();

    pthread_mutex_unlock( &helper.mutex_ );
}

//---------------------------------------------------------------------------
//readFile
//---------------------------------------------------------------------------
bool JNIHelper::ReadFile( const char* fileName,
        std::vector<uint8_t>* buffer_ref )
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized.Call init() to initialize the helper" );
        return false;
    }

    //First, try reading from externalFileDir;
    JNIEnv *env;
    jmethodID mid;

    pthread_mutex_lock( &mutex_ );
    activity_->vm->AttachCurrentThread( &env, NULL );

    jstring str_path = GetExternalFilesDirJString( env );
    const char* path = env->GetStringUTFChars( str_path, NULL );
    std::string s( path );

    if( fileName[0] != '/' )
    {
        s.append( "/" );
    }
    s.append( fileName );
    std::ifstream f( s.c_str(), std::ios::binary );

    env->ReleaseStringUTFChars( str_path, path );
    env->DeleteLocalRef( str_path );
    activity_->vm->DetachCurrentThread();

    if( f )
    {
        LOGI( "reading:%s", s.c_str() );
        f.seekg( 0, std::ifstream::end );
        int32_t fileSize = f.tellg();
        f.seekg( 0, std::ifstream::beg );
        buffer_ref->reserve( fileSize );
        buffer_ref->assign( std::istreambuf_iterator<char>( f ), std::istreambuf_iterator<char>() );
        f.close();
        pthread_mutex_unlock( &mutex_ );
        return true;
    }
    else
    {
        //Fallback to assetManager
        AAssetManager* assetManager = activity_->assetManager;
        AAsset* assetFile = AAssetManager_open( assetManager, fileName, AASSET_MODE_BUFFER );
        if( !assetFile )
        {
            pthread_mutex_unlock( &mutex_ );
            return false;
        }
        uint8_t* data = (uint8_t*) AAsset_getBuffer( assetFile );
        int32_t size = AAsset_getLength( assetFile );
        if( data == NULL )
        {
            AAsset_close( assetFile );

            LOGI( "Failed to load:%s", fileName );
            pthread_mutex_unlock( &mutex_ );
            return false;
        }

        buffer_ref->reserve( size );
        buffer_ref->assign( data, data + size );

        AAsset_close( assetFile );
        pthread_mutex_unlock( &mutex_ );
        return true;
    }
}

std::string JNIHelper::GetExternalFilesDir()
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" );
        return std::string( "" );
    }

    pthread_mutex_lock( &mutex_ );

    //First, try reading from externalFileDir;
    JNIEnv *env;
    jmethodID mid;

    activity_->vm->AttachCurrentThread( &env, NULL );

    jstring strPath = GetExternalFilesDirJString( env );
    const char* path = env->GetStringUTFChars( strPath, NULL );
    std::string s( path );

    env->ReleaseStringUTFChars( strPath, path );
    env->DeleteLocalRef( strPath );
    activity_->vm->DetachCurrentThread();

    pthread_mutex_unlock( &mutex_ );
    return s;
}

uint32_t JNIHelper::LoadTexture( const char* file_name )
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" );
        return 0;
    }

    JNIEnv *env;
    jmethodID mid;

    pthread_mutex_lock( &mutex_ );
    activity_->vm->AttachCurrentThread( &env, NULL );

    jstring name = env->NewStringUTF( file_name );

    GLuint tex;
    glGenTextures( 1, &tex );
    glBindTexture( GL_TEXTURE_2D, tex );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    mid = env->GetMethodID( jni_helper_java_class_, "loadTexture", "(Ljava/lang/String;)Z" );
    jboolean ret = env->CallBooleanMethod( jni_helper_java_ref_, mid, name );
    if( !ret )
    {
        glDeleteTextures( 1, &tex );
        tex = -1;
        LOGI( "Texture load failed %s", file_name );
    }

    //Generate mipmap
    glGenerateMipmap( GL_TEXTURE_2D );

    env->DeleteLocalRef( name );
    activity_->vm->DetachCurrentThread();
    pthread_mutex_unlock( &mutex_ );

    return tex;

}

std::string JNIHelper::ConvertString( const char* str,
        const char* encode )
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" );
        return std::string( "" );
    }

    JNIEnv *env;

    pthread_mutex_lock( &mutex_ );
    activity_->vm->AttachCurrentThread( &env, NULL );

    int32_t iLength = strlen( (const char*) str );

    jbyteArray array = env->NewByteArray( iLength );
    env->SetByteArrayRegion( array, 0, iLength, (const signed char*) str );

    jstring strEncode = env->NewStringUTF( encode );

    jclass cls = env->FindClass( "java/lang/String" );
    jmethodID ctor = env->GetMethodID( cls, "<init>", "([BLjava/lang/String;)V" );
    jstring object = (jstring) env->NewObject( cls, ctor, array, strEncode );

    const char *cparam = env->GetStringUTFChars( object, NULL );

    std::string s = std::string( cparam );

    env->ReleaseStringUTFChars( object, cparam );
    env->DeleteLocalRef( strEncode );
    env->DeleteLocalRef( object );
    activity_->vm->DetachCurrentThread();
    pthread_mutex_unlock( &mutex_ );

    return s;
}

//---------------------------------------------------------------------------
//Audio helpers
//---------------------------------------------------------------------------
int32_t JNIHelper::GetNativeAudioBufferSize()
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" );
        return 0;
    }

    JNIEnv *env;
    jmethodID mid;

    pthread_mutex_lock( &mutex_ );
    activity_->vm->AttachCurrentThread( &env, NULL );

    mid = env->GetMethodID( jni_helper_java_class_, "getNativeAudioBufferSize", "()I" );
    int32_t i = env->CallIntMethod( jni_helper_java_ref_, mid );
    activity_->vm->DetachCurrentThread();
    pthread_mutex_unlock( &mutex_ );

    return i;
}

int32_t JNIHelper::GetNativeAudioSampleRate()
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" );
        return 0;
    }

    JNIEnv *env;
    jmethodID mid;

    pthread_mutex_lock( &mutex_ );
    activity_->vm->AttachCurrentThread( &env, NULL );

    mid = env->GetMethodID( jni_helper_java_class_, "getNativeAudioSampleRate", "()I" );
    int32_t i = env->CallIntMethod( jni_helper_java_ref_, mid );
    activity_->vm->DetachCurrentThread();
    pthread_mutex_unlock( &mutex_ );

    return i;
}

//---------------------------------------------------------------------------
//Misc implementations
//---------------------------------------------------------------------------
jclass JNIHelper::RetrieveClass( JNIEnv *jni,
        const char* class_name )
{
    jclass activity_class = jni->FindClass( CLASS_NAME );
    jmethodID get_class_loader = jni->GetMethodID( activity_class, "getClassLoader",
            "()Ljava/lang/ClassLoader;" );
    jobject cls = jni->CallObjectMethod( activity_->clazz, get_class_loader );
    jclass class_loader = jni->FindClass( "java/lang/ClassLoader" );
    jmethodID find_class = jni->GetMethodID( class_loader, "loadClass",
            "(Ljava/lang/String;)Ljava/lang/Class;" );

    jstring str_class_name = jni->NewStringUTF( class_name );
    jclass class_retrieved = (jclass) jni->CallObjectMethod( cls, find_class, str_class_name );
    jni->DeleteLocalRef( str_class_name );
    return class_retrieved;
}

jstring JNIHelper::GetExternalFilesDirJString( JNIEnv *env )
{
    if( activity_ == NULL )
    {
        LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" );
        return NULL;
    }

    // Invoking getExternalFilesDir() java API
    jclass cls_Env = env->FindClass( CLASS_NAME );
    jmethodID mid = env->GetMethodID( cls_Env, "getExternalFilesDir",
            "(Ljava/lang/String;)Ljava/io/File;" );
    jobject obj_File = env->CallObjectMethod( activity_->clazz, mid, NULL );
    jclass cls_File = env->FindClass( "java/io/File" );
    jmethodID mid_getPath = env->GetMethodID( cls_File, "getPath", "()Ljava/lang/String;" );
    jstring obj_Path = (jstring) env->CallObjectMethod( obj_File, mid_getPath );

    return obj_Path;
}

} //namespace ndkHelper