All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
AndroidNativeState.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include <string>
7 #include <jni.h>
8 #include <android/asset_manager.h>
9 #include <android/native_window.h>
10 #include <pthread.h>
11 
12 
14 {
16  : javaMainThread(0)
17  , vm(NULL)
18  , mainThreadEnv(NULL)
19  , assetManagerGlobalRef(NULL)
20  , assetManager(NULL)
21  , window(NULL)
22  , activity(NULL)
23  , activityClass(NULL)
24  , deviceDpi(0)
25  , deviceModel(NULL)
26  , versionName("")
27  , versionCode(0)
28  , classLoader(NULL)
29  , classLoaderClass(NULL)
30  , classLoaderLoadClassMethod(0)
31  , m_nativeMessageRunner(NULL)
32  { }
33 
34  pthread_t javaMainThread;
35 
36  JavaVM* vm; //the global vm pointer
37  JNIEnv* mainThreadEnv; //only use this from the main thread! if attaching, use the **env from the attachment method
38 
39  //cached pointers to asset manager and main window
40  jobject assetManagerGlobalRef;
41  AAssetManager* assetManager;
42  ANativeWindow* window;
43 
44  //main entry point activity instance and class
45  jobject activity;
46  jclass activityClass;
47 
48  // device Dpi
49  jfloat deviceDpi;
50 
51  const char* deviceModel;
52 
53  std::string versionName;
54  int versionCode;
55 
56  //cached class loader descriptions - use these to load an arbitrary application class
57  jobject classLoader;
58  jclass classLoaderClass;
59  jmethodID classLoaderLoadClassMethod;
60 
61  //assuming an attached thread, and a valid JNIEnv, this method loads an application class
62  jclass LoadClass(JNIEnv* env, jstring strClassName)
63  {
64  return (jclass)env->CallObjectMethod(classLoader, classLoaderLoadClassMethod, strClassName);
65  }
66 
67  void DetermineDeviceModel(JNIEnv* env)
68  {
69  jclass buildClass = env->FindClass("android/os/Build");
70  jfieldID modelMethod = env->GetStaticFieldID(buildClass, "MODEL", "Ljava/lang/String;");
71  jstring deviceNameString = (jstring)env->GetStaticObjectField(buildClass, modelMethod);
72  deviceModel = env->GetStringUTFChars(deviceNameString, NULL);
73  }
74 
75  void SetNativeMessageRunner(jobject nativeMessageRunner)
76  {
77  m_nativeMessageRunner = nativeMessageRunner;
78  }
79 
80  jobject GetNativeMessageRunner()
81  {
82  return m_nativeMessageRunner;
83  }
84 
85 private:
86  jobject m_nativeMessageRunner;
87 };
88 
90 {
91  const AndroidNativeState& nativeState;
92  bool shouldBeAttached;
93 
94 public:
95  JNIEnv* envForThread;
96 
98  :nativeState(nativeState)
99  {
100  int result = nativeState.vm->GetEnv((void **) &envForThread, JNI_VERSION_1_6);
101  shouldBeAttached = (result == JNI_EDETACHED);
102 
103  if(shouldBeAttached)
104  {
105  int result = nativeState.vm->AttachCurrentThread(&envForThread, NULL);
106  Eegeo_ASSERT(result == 0);
107  }
108  }
109 
111  {
112  if(shouldBeAttached)
113  {
114  nativeState.vm->DetachCurrentThread();
115  }
116  }
117 };