17
17
18
18
#include < android/log.h>
19
19
#include < android_native_app_glue.h>
20
- #include < pthread.h>
21
20
#include < cassert>
21
+ #include < pthread.h>
22
22
23
- #include " main.h" // NOLINT
23
+ #include " main.h" // NOLINT
24
24
25
25
// This implementation is derived from http://github.com/google/fplutil
26
26
27
- extern " C" int common_main (int argc, const char * argv[]);
27
+ extern " C" int common_main (int argc, const char * argv[]);
28
28
29
- static struct android_app * g_app_state = nullptr ;
29
+ static struct android_app * g_app_state = nullptr ;
30
30
static bool g_destroy_requested = false ;
31
31
static bool g_started = false ;
32
32
static bool g_restarted = false ;
33
33
static pthread_mutex_t g_started_mutex;
34
34
35
35
// Handle state changes from via native app glue.
36
- static void OnAppCmd (struct android_app * app, int32_t cmd) {
36
+ static void OnAppCmd (struct android_app * app, int32_t cmd) {
37
37
g_destroy_requested |= cmd == APP_CMD_DESTROY;
38
38
}
39
39
40
40
// Process events pending on the main thread.
41
41
// Returns true when the app receives an event requesting exit.
42
42
bool ProcessEvents (int msec) {
43
- struct android_poll_source * source = nullptr ;
43
+ struct android_poll_source * source = nullptr ;
44
44
int events;
45
45
int looperId = ALooper_pollAll (msec, nullptr , &events,
46
- reinterpret_cast <void **>(&source));
46
+ reinterpret_cast <void **>(&source));
47
47
if (looperId >= 0 && source) {
48
48
source->process (g_app_state, source);
49
49
}
@@ -57,7 +57,7 @@ jobject GetActivity() { return g_app_state->activity->clazz; }
57
57
jobject GetWindowContext () { return g_app_state->activity ->clazz ; }
58
58
59
59
// Find a class, attempting to load the class if it's not found.
60
- jclass FindClass (JNIEnv* env, jobject activity_object, const char * class_name) {
60
+ jclass FindClass (JNIEnv * env, jobject activity_object, const char * class_name) {
61
61
jclass class_object = env->FindClass (class_name);
62
62
if (env->ExceptionCheck ()) {
63
63
env->ExceptionClear ();
@@ -93,22 +93,21 @@ jclass FindClass(JNIEnv* env, jobject activity_object, const char* class_name) {
93
93
94
94
// Vars that we need available for appending text to the log window:
95
95
class LoggingUtilsData {
96
- public:
96
+ public:
97
97
LoggingUtilsData ()
98
- : logging_utils_class_(nullptr ),
99
- logging_utils_add_log_text_ (0 ),
98
+ : logging_utils_class_(nullptr ), logging_utils_add_log_text_(0 ),
100
99
logging_utils_init_log_window_ (0 ) {}
101
100
102
101
~LoggingUtilsData () {
103
- JNIEnv* env = GetJniEnv ();
102
+ JNIEnv * env = GetJniEnv ();
104
103
assert (env);
105
104
if (logging_utils_class_) {
106
105
env->DeleteGlobalRef (logging_utils_class_);
107
106
}
108
107
}
109
108
110
109
void Init () {
111
- JNIEnv* env = GetJniEnv ();
110
+ JNIEnv * env = GetJniEnv ();
112
111
assert (env);
113
112
114
113
jclass logging_utils_class = FindClass (
@@ -130,27 +129,28 @@ class LoggingUtilsData {
130
129
logging_utils_init_log_window_, GetActivity ());
131
130
}
132
131
133
- void AppendText (const char * text) {
134
- if (logging_utils_class_ == 0 ) return ; // haven't been initted yet
135
- JNIEnv* env = GetJniEnv ();
132
+ void AppendText (const char *text) {
133
+ if (logging_utils_class_ == 0 )
134
+ return ; // haven't been initted yet
135
+ JNIEnv *env = GetJniEnv ();
136
136
assert (env);
137
137
jstring text_string = env->NewStringUTF (text);
138
138
env->CallStaticVoidMethod (logging_utils_class_, logging_utils_add_log_text_,
139
139
text_string);
140
140
env->DeleteLocalRef (text_string);
141
141
}
142
142
143
- private:
143
+ private:
144
144
jclass logging_utils_class_;
145
145
jmethodID logging_utils_add_log_text_;
146
146
jmethodID logging_utils_init_log_window_;
147
147
};
148
148
149
- LoggingUtilsData* g_logging_utils_data;
149
+ LoggingUtilsData * g_logging_utils_data;
150
150
151
151
// Checks if a JNI exception has happened, and if so, logs it to the console.
152
152
void CheckJNIException () {
153
- JNIEnv* env = GetJniEnv ();
153
+ JNIEnv * env = GetJniEnv ();
154
154
if (env->ExceptionCheck ()) {
155
155
// Get the exception text.
156
156
jthrowable exception = env->ExceptionOccurred ();
@@ -161,7 +161,7 @@ void CheckJNIException() {
161
161
jmethodID toString =
162
162
env->GetMethodID (object_class, " toString" , " ()Ljava/lang/String;" );
163
163
jstring s = (jstring)env->CallObjectMethod (exception , toString);
164
- const char * exception_text = env->GetStringUTFChars (s, nullptr );
164
+ const char * exception_text = env->GetStringUTFChars (s, nullptr );
165
165
166
166
// Log the exception text.
167
167
__android_log_print (ANDROID_LOG_INFO, FIREBASE_TESTAPP_NAME,
@@ -182,7 +182,7 @@ void CheckJNIException() {
182
182
}
183
183
184
184
// Log a message that can be viewed in "adb logcat".
185
- void LogMessage (const char * format, ...) {
185
+ void LogMessage (const char * format, ...) {
186
186
static const int kLineBufferSize = 100 ;
187
187
char buffer[kLineBufferSize + 2 ];
188
188
@@ -201,15 +201,15 @@ void LogMessage(const char* format, ...) {
201
201
}
202
202
203
203
// Get the JNI environment.
204
- JNIEnv* GetJniEnv () {
205
- JavaVM* vm = g_app_state->activity ->vm ;
206
- JNIEnv* env;
204
+ JNIEnv * GetJniEnv () {
205
+ JavaVM * vm = g_app_state->activity ->vm ;
206
+ JNIEnv * env;
207
207
jint result = vm->AttachCurrentThread (&env, nullptr );
208
208
return result == JNI_OK ? env : nullptr ;
209
209
}
210
210
211
211
// Execute common_main(), flush pending events and finish the activity.
212
- extern " C" void android_main (struct android_app * state) {
212
+ extern " C" void android_main (struct android_app * state) {
213
213
// native_app_glue spawns a new thread, calling android_main() when the
214
214
// activity onStart() or onRestart() methods are called. This code handles
215
215
// the case where we're re-entering this method on a different thread by
@@ -236,17 +236,18 @@ extern "C" void android_main(struct android_app* state) {
236
236
g_logging_utils_data->Init ();
237
237
238
238
// Execute cross platform entry point.
239
- static const char * argv[] = {FIREBASE_TESTAPP_NAME};
239
+ static const char * argv[] = {FIREBASE_TESTAPP_NAME};
240
240
int return_value = common_main (1 , argv);
241
- (void )return_value; // Ignore the return value.
241
+ (void )return_value; // Ignore the return value.
242
242
ProcessEvents (10 );
243
243
244
244
// Clean up logging display.
245
245
delete g_logging_utils_data;
246
246
g_logging_utils_data = nullptr ;
247
247
248
248
// Finish the activity.
249
- if (!g_restarted) ANativeActivity_finish (state->activity );
249
+ if (!g_restarted)
250
+ ANativeActivity_finish (state->activity );
250
251
251
252
g_app_state->activity ->vm ->DetachCurrentThread ();
252
253
g_started = false ;
0 commit comments