Skip to content

Commit bb0a88b

Browse files
committed
added a lock to synchronize the UI access for app UI and gpg sign-in prompt
1 parent d5868d7 commit bb0a88b

4 files changed

Lines changed: 8 additions & 20 deletions

File tree

‎samples-android/ButtonClicker/jni/Application.mk‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
APP_PLATFORM := android-22
22
#APP_ABI := all
3-
APP_ABI := armeabi-v7a
4-
#APP_STL := arm64-v8a
3+
#APP_ABI := armeabi-v7a
4+
APP_STL := arm64-v8a
55

66
APP_STL := c++_static
77

‎samples-android/ButtonClicker/jni/ButtonClickerNativeActivity.cpp‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ void Engine::InitGooglePlayGameServices() {
105105
*
106106
*/
107107
void Engine::OnAuthActionStarted(gpg::AuthOperation op) {
108-
if (!initialized_resources_) return;
108+
startup_mutex_.lock();
109109
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this, op]() {
110110
EnableUI(false);
111-
authorizing_ = true;
112111
if (op == gpg::AuthOperation::SIGN_IN) {
113112
LOGI("Signing in to GPG");
114113
status_text_->SetAttribute("Text", "Signing In...");
@@ -136,16 +135,14 @@ void Engine::OnAuthActionFinished(gpg::AuthOperation op,
136135
});
137136
}
138137

139-
if (!initialized_resources_) return;
140-
141138
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this, status]() {
142139
EnableUI(true);
143-
authorizing_ = false;
144140
button_sign_in_->SetAttribute(
145141
"Text", gpg::IsSuccess(status) ? "Sign Out" : "Sign In");
146142

147143
status_text_->SetAttribute(
148144
"Text", gpg::IsSuccess(status) ? "Signed In" : "Signed Out");
145+
startup_mutex_.unlock();
149146
});
150147
}
151148

@@ -560,14 +557,12 @@ void Engine::LeaveGame() {
560557
* invoking jui_helper functions to create java UIs
561558
*/
562559
void Engine::InitUI() {
563-
// Show toast with app label
564-
565560
// The window initialization
566561
jui_helper::JUIWindow::Init(app_->activity, JUIHELPER_CLASS_NAME);
567562

563+
// Show toast with app label
568564
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([]() {
569565
if(NULL == jui_helper::JUIWindow::GetInstance()->GetContext()) {
570-
LOGE("====JUIWindow::Context is NULL, not showing Toast");
571566
return;
572567
}
573568
jui_helper::JUIToast toast(
@@ -580,9 +575,6 @@ void Engine::InitUI() {
580575
// UIs.
581576
//
582577

583-
// The window initialization
584-
// jui_helper::JUIWindow::Init(app_->activity, JUIHELPER_CLASS_NAME);
585-
586578
//
587579
// Buttons
588580
//
@@ -655,7 +647,6 @@ void Engine::InitUI() {
655647
// Init play game services
656648
InitGooglePlayGameServices();
657649

658-
if (authorizing_) EnableUI(false);
659650
return;
660651
}
661652

‎samples-android/ButtonClicker/jni/ButtonClickerNativeActivity.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ class Engine : public gpg::IRealTimeEventListener {
147147
// in OnRoomStatusChanged()
148148
int32_t score_counter_; // Score counter of local player
149149
bool playing_; // Am I playing a game?
150-
bool authorizing_; // Am I signing in to gpg service?
151150
std::string self_id_; // Local player's ID
152151
double start_time_; // Game start time
153152

154153
// synchronization primitive to synchronize
155154
// UIThread, Timer thread and gpg callback thread
156155
mutable std::mutex mutex_;
156+
mutable std::mutex startup_mutex_;
157157

158158
// Renderer of a teapot
159159
TeapotRenderer renderer_;

‎samples-android/ButtonClicker/jni/ButtonClickerNativeActivity_Engine.cpp‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
Engine::Engine()
2424
: initialized_resources_(false),
2525
has_focus_(false),
26-
authorizing_(false),
2726
app_(nullptr),
2827
dialog_(nullptr),
2928
textViewFPS_(nullptr),
@@ -61,14 +60,12 @@ void Engine::UnloadResources() { renderer_.Unload(); }
6160
*/
6261
int Engine::InitDisplay(const int32_t cmd) {
6362
if (!initialized_resources_) {
64-
while(authorizing_) {
65-
std::chrono::milliseconds d(100);
66-
std::this_thread::sleep_for(d);
67-
}
63+
startup_mutex_.lock();
6864
gl_context_->Init(app_->window);
6965
InitUI();
7066
LoadResources();
7167
initialized_resources_ = true;
68+
startup_mutex_.unlock();
7269
} else {
7370
// initialize OpenGL ES and EGL
7471
if (EGL_SUCCESS != gl_context_->Resume(app_->window)) {

0 commit comments

Comments
 (0)