|
25 | 25 | #include "app/src/include/firebase/internal/platform.h" |
26 | 26 |
|
27 | 27 | #if FIREBASE_PLATFORM_ANDROID |
| 28 | +#include "app/src/google_play_services/availability_android.h" |
28 | 29 | #include "app/src/include/google_play_services/availability.h" |
| 30 | +#include "app/src/util_android.h" |
29 | 31 | #endif // FIREBASE_PLATFORM_ANDROID |
30 | 32 | #include "app/src/include/firebase/internal/mutex.h" |
31 | 33 | #include "app/src/log.h" |
@@ -77,32 +79,49 @@ static void PerformInitialize(ModuleInitializerData* data) { |
77 | 79 |
|
78 | 80 | #if FIREBASE_PLATFORM_ANDROID |
79 | 81 | if (init_result == kInitResultFailedMissingDependency) { |
80 | | - // On Android, we need to update or activate Google Play services |
81 | | - // before we can initialize this Firebase module. |
82 | | - LogWarning("Google Play services unavailable, trying to fix."); |
83 | | - |
84 | | - Future<void> make_available = google_play_services::MakeAvailable( |
85 | | - data->app->GetJNIEnv(), data->app->activity()); |
86 | | - |
87 | | - make_available.OnCompletion( |
88 | | - [](const Future<void>& result, void* ptr) { |
89 | | - ModuleInitializerData* data = |
90 | | - reinterpret_cast<ModuleInitializerData*>(ptr); |
91 | | - if (result.status() == kFutureStatusComplete) { |
92 | | - if (result.error() == 0) { |
93 | | - LogInfo("Google Play services now available, continuing."); |
94 | | - PerformInitialize(data); |
95 | | - } else { |
96 | | - LogError("Google Play services still unavailable."); |
97 | | - int num_remaining = data->init_fns.size() - data->init_fn_idx; |
98 | | - data->future_impl.Complete( |
99 | | - data->future_handle_init, num_remaining, |
100 | | - "Unable to initialize due to missing Google Play services " |
101 | | - "dependency."); |
| 82 | + // Note: If Initialize here succeeds, google_play_services::Terminate |
| 83 | + // is called in the OnCompletion handler below. Note that these |
| 84 | + // are reference-counted so it's safe to init/terminate an extra time.. |
| 85 | + if (google_play_services::Initialize(data->app->GetJNIEnv(), |
| 86 | + data->app->activity())) { |
| 87 | + // On Android, we need to update or activate Google Play services |
| 88 | + // before we can initialize this Firebase module. |
| 89 | + LogWarning("Google Play services unavailable, trying to fix."); |
| 90 | + |
| 91 | + Future<void> make_available = google_play_services::MakeAvailable( |
| 92 | + data->app->GetJNIEnv(), data->app->activity()); |
| 93 | + |
| 94 | + make_available.OnCompletion( |
| 95 | + [](const Future<void>& result, void* ptr) { |
| 96 | + ModuleInitializerData* data = |
| 97 | + reinterpret_cast<ModuleInitializerData*>(ptr); |
| 98 | + if (result.status() == kFutureStatusComplete) { |
| 99 | + if (result.error() == 0) { |
| 100 | + LogInfo("Google Play services now available, continuing."); |
| 101 | + PerformInitialize(data); |
| 102 | + google_play_services::Terminate(data->app->GetJNIEnv()); |
| 103 | + } else { |
| 104 | + LogError("Google Play services still unavailable."); |
| 105 | + int num_remaining = data->init_fns.size() - data->init_fn_idx; |
| 106 | + data->future_impl.Complete(data->future_handle_init, |
| 107 | + num_remaining, |
| 108 | + "Unable to initialize due to " |
| 109 | + "missing Google Play services " |
| 110 | + "dependency."); |
| 111 | + google_play_services::Terminate(util::GetJNIEnvFromApp()); |
| 112 | + } |
102 | 113 | } |
103 | | - } |
104 | | - }, |
105 | | - data); |
| 114 | + }, |
| 115 | + data); |
| 116 | + } else { |
| 117 | + int num_remaining = data->init_fns.size() - data->init_fn_idx; |
| 118 | + data->future_impl.Complete( |
| 119 | + data->future_handle_init, num_remaining, |
| 120 | + "Could not run Google Play services update due to app " |
| 121 | + "misconfiguration. Please add " |
| 122 | + "com.google.android.gms:play-services-base as an Android " |
| 123 | + "dependency to enable this functionality."); |
| 124 | + } |
106 | 125 | } |
107 | 126 | #else // !FIREBASE_PLATFORM_ANDROID |
108 | 127 | // Outside of Android, we shouldn't get kInitResultFailedMissingDependency. |
|
0 commit comments