@@ -34,8 +34,8 @@ namespace ndk_helper {
3434 * Singleton
3535 */
3636JNIHelper *JNIHelper::GetInstance () {
37- static JNIHelper helper ;
38- return &helper ;
37+ static JNIHelper* pHelper = new JNIHelper () ;
38+ return pHelper ;
3939}
4040
4141/*
@@ -73,14 +73,14 @@ void JNIHelper::Init(ANativeActivity *activity, const char *helper_class_name) {
7373 jmethodID midGetPackageName = env->GetMethodID (
7474 android_content_Context, " getPackageName" , " ()Ljava/lang/String;" );
7575
76- jstring packageName = ( jstring)
77- env->CallObjectMethod (helper.activity_ ->clazz , midGetPackageName);
76+ jstring packageName = static_cast < jstring>(
77+ env->CallObjectMethod (helper.activity_ ->clazz , midGetPackageName)) ;
7878 const char *appname = env->GetStringUTFChars (packageName, NULL );
7979 helper.app_bunlde_name_ = std::string (appname);
8080
8181 // Instantiate JNIHelper class
8282 jclass cls = helper.RetrieveClass (env, helper_class_name);
83- helper.jni_helper_java_class_ = ( jclass) env->NewGlobalRef (cls);
83+ helper.jni_helper_java_class_ = static_cast < jclass>( env->NewGlobalRef (cls) );
8484
8585 jmethodID constructor =
8686 env->GetMethodID (helper.jni_helper_java_class_ , " <init>" ,
@@ -91,8 +91,8 @@ void JNIHelper::Init(ANativeActivity *activity, const char *helper_class_name) {
9191 helper.jni_helper_java_ref_ = env->NewGlobalRef (helper.jni_helper_java_ref_ );
9292
9393 // Get app label
94- jstring labelName = ( jstring)
95- helper.CallObjectMethod (" getApplicationName" , " ()Ljava/lang/String;" );
94+ jstring labelName = static_cast < jstring>(
95+ helper.CallObjectMethod (" getApplicationName" , " ()Ljava/lang/String;" )) ;
9696 const char *label = env->GetStringUTFChars (labelName, NULL );
9797 helper.app_label_ = std::string (label);
9898
@@ -158,9 +158,9 @@ bool JNIHelper::ReadFile(const char *fileName,
158158 if (f) {
159159 LOGI (" reading:%s" , s.c_str ());
160160 f.seekg (0 , std::ifstream::end);
161- int32_t fileSize = f.tellg ();
161+ long long int fileSize = f.tellg ();
162162 f.seekg (0 , std::ifstream::beg);
163- buffer_ref->reserve (fileSize);
163+ buffer_ref->reserve (static_cast < unsigned long >( fileSize) );
164164 buffer_ref->assign (std::istreambuf_iterator<char >(f),
165165 std::istreambuf_iterator<char >());
166166 f.close ();
@@ -173,8 +173,8 @@ bool JNIHelper::ReadFile(const char *fileName,
173173 if (!assetFile) {
174174 return false ;
175175 }
176- uint8_t *data = ( uint8_t *) AAsset_getBuffer (assetFile);
177- int32_t size = AAsset_getLength (assetFile);
176+ const uint8_t *data = static_cast < const uint8_t *>( AAsset_getBuffer (assetFile) );
177+ size_t size = static_cast < size_t >( AAsset_getLength (assetFile) );
178178 if (data == NULL ) {
179179 AAsset_close (assetFile);
180180
@@ -250,11 +250,11 @@ uint32_t JNIHelper::LoadTexture(const char *file_name, int32_t *outWidth,
250250 int32_t height = env->GetIntField (out, fidHeight);
251251 if (!ret) {
252252 glDeleteTextures (1 , &tex);
253- tex = - 1 ;
253+ tex = 0xffff ;
254254 LOGI (" Texture load failed %s" , file_name);
255255 }
256256 LOGI (" Loaded texture original size:%dx%d alpha:%d" , width, height,
257- ( int32_t ) alpha);
257+ static_cast < int32_t >( alpha) );
258258 if (outWidth != NULL ) {
259259 *outWidth = width;
260260 }
@@ -287,16 +287,16 @@ std::string JNIHelper::ConvertString(const char *str, const char *encode) {
287287 JNIEnv *env = AttachCurrentThread ();
288288 env->PushLocalFrame (16 );
289289
290- int32_t iLength = strlen (( const char *) str);
290+ jsize iLength = static_cast <jsize>( strlen (str) );
291291
292292 jbyteArray array = env->NewByteArray (iLength);
293- env->SetByteArrayRegion (array, 0 , iLength, ( const signed char *) str);
293+ env->SetByteArrayRegion (array, 0 , iLength, reinterpret_cast < const jbyte*>( str) );
294294
295295 jstring strEncode = env->NewStringUTF (encode);
296296
297297 jclass cls = env->FindClass (" java/lang/String" );
298298 jmethodID ctor = env->GetMethodID (cls, " <init>" , " ([BLjava/lang/String;)V" );
299- jstring object = ( jstring) env->NewObject (cls, ctor, array, strEncode);
299+ jstring object = static_cast < jstring>( env->NewObject (cls, ctor, array, strEncode) );
300300
301301 const char *cparam = env->GetStringUTFChars (object, NULL );
302302
@@ -332,8 +332,8 @@ std::string JNIHelper::GetStringResource(const std::string& resourceName)
332332 JNIEnv *env = AttachCurrentThread ();
333333 jstring name = env->NewStringUTF (resourceName.c_str ());
334334
335- jstring ret = ( jstring)
336- CallObjectMethod (" getStringResource" , " (Ljava/lang/String;)Ljava/lang/String;" , name);
335+ jstring ret = static_cast < jstring>(
336+ CallObjectMethod (" getStringResource" , " (Ljava/lang/String;)Ljava/lang/String;" , name)) ;
337337
338338 const char *resource = env->GetStringUTFChars (ret, NULL );
339339 std::string s = std::string (resource);
@@ -389,7 +389,7 @@ jclass JNIHelper::RetrieveClass(JNIEnv *jni, const char *class_name) {
389389
390390 jstring str_class_name = jni->NewStringUTF (class_name);
391391 jclass class_retrieved =
392- ( jclass) jni->CallObjectMethod (cls, find_class, str_class_name);
392+ static_cast < jclass>( jni->CallObjectMethod (cls, find_class, str_class_name) );
393393 jni->DeleteLocalRef (str_class_name);
394394 jni->DeleteLocalRef (activity_class);
395395 jni->DeleteLocalRef (class_loader);
@@ -411,7 +411,7 @@ jstring JNIHelper::GetExternalFilesDirJString(JNIEnv *env) {
411411 jclass cls_File = env->FindClass (" java/io/File" );
412412 jmethodID mid_getPath =
413413 env->GetMethodID (cls_File, " getPath" , " ()Ljava/lang/String;" );
414- jstring obj_Path = ( jstring) env->CallObjectMethod (obj_File, mid_getPath);
414+ jstring obj_Path = static_cast < jstring>( env->CallObjectMethod (obj_File, mid_getPath) );
415415
416416 return obj_Path;
417417}
@@ -622,15 +622,17 @@ void JNIHelper::RunOnUiThread(std::function<void()> callback) {
622622
623623 // Allocate temporary function object to be passed around
624624 std::function<void ()> *pCallback = new std::function<void ()>(callback);
625- env->CallVoidMethod (jni_helper_java_ref_, mid, ( int64_t ) pCallback);
625+ env->CallVoidMethod (jni_helper_java_ref_, mid, reinterpret_cast < int64_t >( pCallback) );
626626}
627627
628628// This JNI function is invoked from UIThread asynchronously
629629extern " C" {
630630JNIEXPORT void
631631Java_com_sample_helper_NDKHelper_RunOnUiThreadHandler (JNIEnv *env, jobject thiz,
632632 int64_t pointer) {
633- std::function<void ()> *pCallback = (std::function<void ()> *)pointer;
633+ #pragma unused (env)
634+ #pragma unused (thiz)
635+ std::function<void ()> *pCallback = reinterpret_cast < std::function<void ()> *>(pointer);
634636 (*pCallback)();
635637
636638 // Deleting temporary object
0 commit comments