1515 */
1616
1717#include " app/src/invites/android/invites_android_helper.h"
18+
1819#include < assert.h>
1920#include < string.h>
21+
2022#include " app/invites_resources.h"
2123#include " app/src/invites/receiver_interface.h"
2224#include " app/src/invites/sender_receiver_interface.h"
@@ -31,27 +33,16 @@ Mutex AndroidHelper::init_mutex_; // NOLINT
3133int AndroidHelper::initialize_count_ = 0 ;
3234
3335METHOD_LOOKUP_DEFINITION (
34- invite, " com/google/firebase/invites/internal/cpp/AppInviteNativeWrapper" ,
35- INVITE_METHODS)
36+ dynamic_links_native_wrapper,
37+ " com/google/firebase/dynamiclinks/internal/cpp/DynamicLinksNativeWrapper" ,
38+ DYNAMIC_LINKS_NATIVE_WRAPPER_METHODS)
3639
3740extern " C" {
3841
3942JNIEXPORT void JNICALL
40- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_receivedInviteCallback (
41- JNIEnv* env, jclass clazz, jlong data_ptr, jstring invitation_id_java,
42- jstring deep_link_url_java, jint result, jstring error_string_java);
43- JNIEXPORT void JNICALL
44- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_convertedInviteCallback (
45- JNIEnv* env, jclass clazz, jlong data_ptr, jstring invitation_id_java,
43+ Java_com_google_firebase_dynamiclinks_internal_cpp_DynamicLinksNativeWrapper_receivedDynamicLinkCallback (
44+ JNIEnv* env, jclass clazz, jlong data_ptr, jstring deep_link_url_java,
4645 jint result, jstring error_string_java);
47- JNIEXPORT void JNICALL
48- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_sentInviteCallback (
49- JNIEnv* env, jclass clazz, jlong data_ptr,
50- jobjectArray invitation_ids_array, jint result, jstring error_string_java);
51- JNIEXPORT void JNICALL
52- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_connectionFailedCallback (
53- JNIEnv* env, jclass clazz, jlong data_ptr, jint error_code,
54- jstring error_message);
5546
5647} // extern "C"
5748
@@ -68,20 +59,10 @@ AndroidHelper::AndroidHelper(const ::firebase::App& app,
6859 }
6960
7061 static const JNINativeMethod kNativeMethods [] = {
71- {" receivedInviteCallback" ,
72- " (JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)V" ,
73- reinterpret_cast <void *>(
74- &Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_receivedInviteCallback)}, // NOLINT
75- {" convertedInviteCallback" ,
62+ {" receivedDynamicLinkCallback" ,
7663 " (JLjava/lang/String;ILjava/lang/String;)V" ,
7764 reinterpret_cast <void *>(
78- &Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_convertedInviteCallback)}, // NOLINT
79- {" sentInviteCallback" , " (J[Ljava/lang/String;ILjava/lang/String;)V" ,
80- reinterpret_cast <void *>(
81- &Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_sentInviteCallback)}, // NOLINT
82- {" connectionFailedCallback" , " (JILjava/lang/String;)V" ,
83- reinterpret_cast <void *>(
84- &Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_connectionFailedCallback)}, // NOLINT
65+ &Java_com_google_firebase_dynamiclinks_internal_cpp_DynamicLinksNativeWrapper_receivedDynamicLinkCallback)} // NOLINT
8566 };
8667 const std::vector<util::EmbeddedFile> embedded_files =
8768 util::CacheEmbeddedFiles (
@@ -90,16 +71,15 @@ AndroidHelper::AndroidHelper(const ::firebase::App& app,
9071 firebase_invites::invites_resources_filename,
9172 firebase_invites::invites_resources_data,
9273 firebase_invites::invites_resources_size));
93- if (!(invite::CacheClassFromFiles (env, app_->activity (),
94- &embedded_files) &&
95- // Prepare to instantiate the AppInviteNativeWrapper Java class,
96- // which
97- // we will use to talk to the Firebase Invites Java library. Note
98- // that
99- // we need global references to everything because, again, we may be
100- // running from different threads.
101- invite::CacheMethodIds (env, app_->activity ()) &&
102- invite::RegisterNatives (
74+ if (!(dynamic_links_native_wrapper::CacheClassFromFiles (
75+ env, app_->activity (), &embedded_files) &&
76+ // Prepare to instantiate the DynamicLinksNativeWrapper Java class,
77+ // which we will use to talk to the Firebase Dynamic Links Java
78+ // library. Note that we need global references to everything
79+ // because, again, we may be running from different threads.
80+ dynamic_links_native_wrapper::CacheMethodIds (env,
81+ app_->activity ()) &&
82+ dynamic_links_native_wrapper::RegisterNatives (
10383 env, kNativeMethods ,
10484 sizeof (kNativeMethods ) / sizeof (kNativeMethods [0 ])))) {
10585 util::Terminate (env);
@@ -109,7 +89,7 @@ AndroidHelper::AndroidHelper(const ::firebase::App& app,
10989 }
11090 initialize_count_++;
11191 }
112- // Actually create the AppInviteNativeWrapper object now.
92+ // Actually create the DynamicLinksNativeWrapper object now.
11393 CreateWrapperObject (sender_receiver);
11494}
11595
@@ -118,7 +98,7 @@ AndroidHelper::~AndroidHelper() {
11898 if (app_ == nullptr ) return ;
11999
120100 // Ensure that no further JNI callbacks refer to deleted instances.
121- CallMethod (invite ::kDiscardNativePointer );
101+ CallMethod (dynamic_links_native_wrapper ::kDiscardNativePointer );
122102
123103 JNIEnv* env = app_->GetJNIEnv ();
124104 env->DeleteGlobalRef (wrapper_obj_);
@@ -129,7 +109,7 @@ AndroidHelper::~AndroidHelper() {
129109 initialize_count_--;
130110 if (initialize_count_ == 0 ) {
131111 util::Terminate (env);
132- invite ::ReleaseClass (env);
112+ dynamic_links_native_wrapper ::ReleaseClass (env);
133113 }
134114 }
135115 app_ = nullptr ;
@@ -138,59 +118,64 @@ AndroidHelper::~AndroidHelper() {
138118void AndroidHelper::CreateWrapperObject (
139119 SenderReceiverInterface* sender_receiver) {
140120 JNIEnv* env = app_->GetJNIEnv ();
141- jobject obj = env->NewObject (
142- invite::g_class, invite::GetMethodId (invite::kConstructor ),
143- reinterpret_cast <jlong>(sender_receiver), app_->activity (), nullptr );
121+ jobject obj = env->NewObject (dynamic_links_native_wrapper::g_class,
122+ dynamic_links_native_wrapper::GetMethodId (
123+ dynamic_links_native_wrapper::kConstructor ),
124+ reinterpret_cast <jlong>(sender_receiver),
125+ app_->activity (), nullptr );
144126 CheckJNIException ();
145127 wrapper_obj_ = env->NewGlobalRef (obj);
146128 env->DeleteLocalRef (obj);
147129}
148130
149- bool AndroidHelper::CallBooleanMethod (invite::Method method) {
131+ bool AndroidHelper::CallBooleanMethod (
132+ dynamic_links_native_wrapper::Method method) {
150133 JNIEnv* env = app_->GetJNIEnv ();
151- jboolean result =
152- env-> CallBooleanMethod ( wrapper_obj (), invite ::GetMethodId (method));
134+ jboolean result = env-> CallBooleanMethod (
135+ wrapper_obj (), dynamic_links_native_wrapper ::GetMethodId (method));
153136 CheckJNIException ();
154137 return (result != JNI_FALSE);
155138}
156139
157- bool AndroidHelper::CallBooleanMethodString (invite::Method method,
158- const char * strparam) {
140+ bool AndroidHelper::CallBooleanMethodString (
141+ dynamic_links_native_wrapper::Method method, const char * strparam) {
159142 JNIEnv* env = app_->GetJNIEnv ();
160143 jstring param = env->NewStringUTF (strparam);
161- jboolean result =
162- env-> CallBooleanMethod ( wrapper_obj (), invite ::GetMethodId (method), param);
144+ jboolean result = env-> CallBooleanMethod (
145+ wrapper_obj (), dynamic_links_native_wrapper ::GetMethodId (method), param);
163146 CheckJNIException ();
164147 env->DeleteLocalRef (param);
165148
166149 return (result != JNI_FALSE);
167150}
168151
169- int AndroidHelper::CallIntMethodString (invite::Method method,
170- const char * strparam) {
152+ int AndroidHelper::CallIntMethodString (
153+ dynamic_links_native_wrapper::Method method, const char * strparam) {
171154 JNIEnv* env = app_->GetJNIEnv ();
172155 jstring param = env->NewStringUTF (strparam);
173- jint result =
174- env-> CallBooleanMethod ( wrapper_obj (), invite ::GetMethodId (method), param);
156+ jint result = env-> CallBooleanMethod (
157+ wrapper_obj (), dynamic_links_native_wrapper ::GetMethodId (method), param);
175158 CheckJNIException ();
176159 env->DeleteLocalRef (param);
177160
178161 return result;
179162}
180163
181- void AndroidHelper::CallMethod (invite ::Method method) {
164+ void AndroidHelper::CallMethod (dynamic_links_native_wrapper ::Method method) {
182165 JNIEnv* env = app_->GetJNIEnv ();
183- env->CallVoidMethod (wrapper_obj (), invite::GetMethodId (method));
166+ env->CallVoidMethod (wrapper_obj (),
167+ dynamic_links_native_wrapper::GetMethodId (method));
184168 CheckJNIException ();
185169}
186170
187- void AndroidHelper::CallMethodStringString (invite::Method method,
188- const char * strparam1,
189- const char * strparam2) {
171+ void AndroidHelper::CallMethodStringString (
172+ dynamic_links_native_wrapper::Method method, const char * strparam1,
173+ const char * strparam2) {
190174 JNIEnv* env = app_->GetJNIEnv ();
191175 jstring param1 = env->NewStringUTF (strparam1);
192176 jstring param2 = env->NewStringUTF (strparam2);
193- env->CallVoidMethod (wrapper_obj (), invite::GetMethodId (method), param1,
177+ env->CallVoidMethod (wrapper_obj (),
178+ dynamic_links_native_wrapper::GetMethodId (method), param1,
194179 param2);
195180 CheckJNIException ();
196181
@@ -232,90 +217,23 @@ void AndroidHelper::ConnectionFailedCallback(int error_code) {
232217
233218extern " C" {
234219
235- // A function that receives the callback from the Java side. The
236- // "data_ptr" parameter is actually a pointer to our the AndroidHelper
237- // instance, so we can call the proper ConnectionFailed method.
238- JNIEXPORT void JNICALL
239- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_connectionFailedCallback (
240- JNIEnv* env, jclass clazz, jlong data_ptr, jint error_code,
241- jstring error_message) {
242- if (data_ptr == 0 ) return ; // test call only
243-
244- // Be careful - we are in a different thread now. No JNI calls are allowed
245- // except on the JNIEnv we were passed, and we have to take care changing the
246- // InvitesReceiverInternal data -- anything we touch needs a lock.
247- firebase::invites::internal::AndroidHelper* instance =
248- reinterpret_cast <firebase::invites::internal::AndroidHelper*>(data_ptr);
249-
250- instance->ConnectionFailedCallback (error_code);
251- }
252-
253- // A function that receives the callback from the Java side. The
254- // "data_ptr" parameter is actually a pointer to our instance of
255- // InviteSenderInternalAndroid, so we can call the proper
256- // SentInviteCallback method.
257-
258- JNIEXPORT void JNICALL
259- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_sentInviteCallback (
260- JNIEnv* env, jclass clazz, jlong data_ptr,
261- jobjectArray invitation_ids_array, jint result, jstring error_string_java) {
262- if (data_ptr == 0 ) return ; // Test call to ensure JNI is hooked up.
263-
264- // Be careful - we are in a different thread now. No JNI calls are allowed
265- // except on the JNIEnv we were passed, and we have to take care changing the
266- // AppInvitesSenderInternal data -- anything we touch needs a lock.
267- std::vector<std::string> invitation_ids;
268- std::string error_string;
269- if (result == 0 ) {
270- if (invitation_ids_array != nullptr ) {
271- size_t num_ids =
272- static_cast <size_t >(env->GetArrayLength (invitation_ids_array));
273- invitation_ids.reserve (num_ids);
274- for (int i = 0 ; i < num_ids; i++) {
275- jstring id = static_cast <jstring>(
276- env->GetObjectArrayElement (invitation_ids_array, i));
277- const char * chars = env->GetStringUTFChars (id, nullptr );
278- invitation_ids.push_back (std::string (chars));
279- env->ReleaseStringUTFChars (id, chars);
280- env->DeleteLocalRef (id);
281- }
282- }
283- } else {
284- // result != 0
285- if (error_string_java != nullptr ) {
286- const char * chars = env->GetStringUTFChars (error_string_java, nullptr );
287- error_string = chars;
288- env->ReleaseStringUTFChars (error_string_java, chars);
289- }
290- }
291- firebase::invites::internal::SenderReceiverInterface* sender_receiver =
292- reinterpret_cast <firebase::invites::internal::SenderReceiverInterface*>(
293- data_ptr);
294- sender_receiver->SentInviteCallback (invitation_ids, result, error_string);
295- }
296-
297220// A function that receives the callback from the Java side. The
298221// "data_ptr" parameter is actually a pointer to our instance of
299222// InviteReceiverInternalAndroid, so we can call the proper
300223// ReceivedInviteCallback method.
301224JNIEXPORT void JNICALL
302- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_receivedInviteCallback (
303- JNIEnv* env, jclass clazz, jlong data_ptr, jstring invitation_id_java ,
304- jstring deep_link_url_java, jint result, jstring error_string_java) {
225+ Java_com_google_firebase_dynamiclinks_internal_cpp_DynamicLinksNativeWrapper_receivedDynamicLinkCallback (
226+ JNIEnv* env, jclass clazz, jlong data_ptr, jstring deep_link_url_java ,
227+ jint result, jstring error_string_java) {
305228 if (data_ptr == 0 ) return ; // test call only
306229
307230 // Be careful - we are in a different thread now. No JNI calls are allowed
308231 // except on the JNIEnv we were passed, and we have to take care changing the
309232 // InvitesReceiverInternal data -- anything we touch needs a lock.
310- std::string invitation_id;
233+ std::string invitation_id; // Will remain empty.
311234 std::string deep_link_url;
312235 std::string error_string;
313236 if (result == 0 ) {
314- if (invitation_id_java != nullptr ) {
315- const char * chars = env->GetStringUTFChars (invitation_id_java, nullptr );
316- invitation_id = chars;
317- env->ReleaseStringUTFChars (invitation_id_java, chars);
318- }
319237 if (deep_link_url_java != nullptr ) {
320238 const char * chars = env->GetStringUTFChars (deep_link_url_java, nullptr );
321239 deep_link_url = chars;
@@ -339,43 +257,6 @@ Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_receivedInv
339257 result, error_string);
340258}
341259
342- // A function that receives the callback from the Java side. The
343- // "data_ptr" parameter is actually a pointer to our instance of
344- // InviteReceiverInternalAndroid, so we can call the proper
345- // ConvertedInviteCallback method.
346- JNIEXPORT void JNICALL
347- Java_com_google_firebase_invites_internal_cpp_AppInviteNativeWrapper_convertedInviteCallback (
348- JNIEnv* env, jclass clazz, jlong data_ptr, jstring invitation_id_java,
349- jint result, jstring error_string_java) {
350- if (data_ptr == 0 ) return ; // test call only
351-
352- // Be careful - we are in a different thread now. No JNI calls are allowed
353- // except on the JNIEnv we were passed, and we have to take care changing the
354- // InvitesReceiverInternal data -- anything we touch needs a lock.
355- std::string invitation_id;
356- std::string error_string;
357- if (result == 0 ) {
358- if (invitation_id_java != nullptr ) {
359- const char * chars = env->GetStringUTFChars (invitation_id_java, nullptr );
360- invitation_id = chars;
361- env->ReleaseStringUTFChars (invitation_id_java, chars);
362- }
363- } else {
364- // result != 0
365- if (error_string_java != nullptr ) {
366- const char * chars = env->GetStringUTFChars (error_string_java, nullptr );
367- error_string = chars;
368- env->ReleaseStringUTFChars (error_string_java, chars);
369- }
370- }
371-
372- firebase::invites::internal::SenderReceiverInterface* sender_receiver =
373- reinterpret_cast <firebase::invites::internal::SenderReceiverInterface*>(
374- data_ptr);
375-
376- sender_receiver->ConvertedInviteCallback (invitation_id, result, error_string);
377- }
378-
379260} // extern "C"
380261
381262} // namespace internal
0 commit comments