From eb126b2743d89e4590ac25eff5c063810fd31efb Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Fri, 15 Jul 2022 17:47:12 +0200 Subject: [PATCH 1/2] wip: android --- .../analytics/FirebaseAnalyticsPlugin.java | 92 ++++-- .../firebase/app/FirebaseAppPlugin.java | 32 +- .../FirebaseAuthenticationPlugin.java | 296 ++++++++++++------ .../FirebaseCrashlyticsPlugin.java | 122 +++++--- .../messaging/FirebaseMessagingPlugin.java | 156 +++++---- .../FirebasePerformancePlugin.java | 118 ++++--- 6 files changed, 514 insertions(+), 302 deletions(-) diff --git a/packages/analytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/analytics/FirebaseAnalyticsPlugin.java b/packages/analytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/analytics/FirebaseAnalyticsPlugin.java index 304d9b8f..2d56839f 100644 --- a/packages/analytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/analytics/FirebaseAnalyticsPlugin.java +++ b/packages/analytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/analytics/FirebaseAnalyticsPlugin.java @@ -21,59 +21,83 @@ public void load() { @PluginMethod public void setUserId(PluginCall call) { - String userId = call.getString("userId", null); - implementation.setUserId(userId); - call.resolve(); + try { + String userId = call.getString("userId", null); + implementation.setUserId(userId); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void setUserProperty(PluginCall call) { - String key = call.getString("key"); - if (key == null) { - call.reject(ERROR_KEY_MISSING); - return; + try { + String key = call.getString("key"); + if (key == null) { + call.reject(ERROR_KEY_MISSING); + return; + } + String value = call.getString("value", null); + implementation.setUserProperty(key, value); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - String value = call.getString("value", null); - implementation.setUserProperty(key, value); - call.resolve(); } @PluginMethod public void setCurrentScreen(PluginCall call) { - String screenName = call.getString("screenName", null); - String screenClassOverride = call.getString("screenClassOverride", null); - implementation.setCurrentScreen(screenName, screenClassOverride); - call.resolve(); + try { + String screenName = call.getString("screenName", null); + String screenClassOverride = call.getString("screenClassOverride", null); + implementation.setCurrentScreen(screenName, screenClassOverride); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void logEvent(PluginCall call) { - String name = call.getString("name"); - if (name == null) { - call.reject(ERROR_NAME_MISSING); - return; + try { + String name = call.getString("name"); + if (name == null) { + call.reject(ERROR_NAME_MISSING); + return; + } + JSObject params = call.getObject("params"); + implementation.logEvent(name, params); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - JSObject params = call.getObject("params"); - implementation.logEvent(name, params); - call.resolve(); } @PluginMethod public void setSessionTimeoutDuration(PluginCall call) { - Long duration = call.getLong("duration", 1800000L); - implementation.setSessionTimeoutDuration(duration); - call.resolve(); + try { + Long duration = call.getLong("duration", 1800000L); + implementation.setSessionTimeoutDuration(duration); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void setEnabled(PluginCall call) { - Boolean enabled = call.getBoolean("enabled"); - if (enabled == null) { - call.reject(ERROR_ENABLED_MISSING); - return; + try { + Boolean enabled = call.getBoolean("enabled"); + if (enabled == null) { + call.reject(ERROR_ENABLED_MISSING); + return; + } + implementation.setEnabled(enabled); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.setEnabled(enabled); - call.resolve(); } @PluginMethod @@ -83,7 +107,11 @@ public void isEnabled(PluginCall call) { @PluginMethod public void resetAnalyticsData(PluginCall call) { - implementation.resetAnalyticsData(); - call.resolve(); + try { + implementation.resetAnalyticsData(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } } diff --git a/packages/app/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/app/FirebaseAppPlugin.java b/packages/app/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/app/FirebaseAppPlugin.java index 67d3a67f..7dfbf906 100644 --- a/packages/app/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/app/FirebaseAppPlugin.java +++ b/packages/app/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/app/FirebaseAppPlugin.java @@ -19,21 +19,29 @@ public void load() { @PluginMethod public void getName(PluginCall call) { - JSObject ret = new JSObject(); - ret.put("name", firebaseAppInstance.getName()); - call.resolve(ret); + try { + JSObject ret = new JSObject(); + ret.put("name", firebaseAppInstance.getName()); + call.resolve(ret); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void getOptions(PluginCall call) { - FirebaseOptions options = firebaseAppInstance.getOptions(); - JSObject ret = new JSObject(); - ret.put("apiKey", options.getApiKey()); - ret.put("applicationId", options.getApplicationId()); - ret.put("databaseUrl", options.getDatabaseUrl()); - ret.put("gcmSenderId", options.getGcmSenderId()); - ret.put("projectId", options.getProjectId()); - ret.put("storageBucket", options.getStorageBucket()); - call.resolve(ret); + try { + FirebaseOptions options = firebaseAppInstance.getOptions(); + JSObject ret = new JSObject(); + ret.put("apiKey", options.getApiKey()); + ret.put("applicationId", options.getApplicationId()); + ret.put("databaseUrl", options.getDatabaseUrl()); + ret.put("gcmSenderId", options.getGcmSenderId()); + ret.put("projectId", options.getProjectId()); + ret.put("storageBucket", options.getStorageBucket()); + call.resolve(ret); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } } diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java index 8445226a..2f45c49a 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java @@ -41,209 +41,305 @@ public void load() { @PluginMethod public void applyActionCode(PluginCall call) { - String oobCode = call.getString("oobCode"); - if (oobCode == null) { - call.reject(ERROR_OOB_CODE_MISSING); - return; + try { + String oobCode = call.getString("oobCode"); + if (oobCode == null) { + call.reject(ERROR_OOB_CODE_MISSING); + return; + } + implementation.applyActionCode(oobCode, () -> call.resolve()); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.applyActionCode(oobCode, () -> call.resolve()); } @PluginMethod public void createUserWithEmailAndPassword(PluginCall call) { - implementation.createUserWithEmailAndPassword(call); + try { + implementation.createUserWithEmailAndPassword(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void confirmPasswordReset(PluginCall call) { - String oobCode = call.getString("oobCode"); - if (oobCode == null) { - call.reject(ERROR_OOB_CODE_MISSING); - return; - } - String newPassword = call.getString("newPassword"); - if (newPassword == null) { - call.reject(ERROR_NEW_PASSWORD_MISSING); - return; + try { + String oobCode = call.getString("oobCode"); + if (oobCode == null) { + call.reject(ERROR_OOB_CODE_MISSING); + return; + } + String newPassword = call.getString("newPassword"); + if (newPassword == null) { + call.reject(ERROR_NEW_PASSWORD_MISSING); + return; + } + implementation.confirmPasswordReset(oobCode, newPassword, () -> call.resolve()); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.confirmPasswordReset(oobCode, newPassword, () -> call.resolve()); } @PluginMethod public void getCurrentUser(PluginCall call) { - FirebaseUser user = implementation.getCurrentUser(); - JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user); - JSObject result = new JSObject(); - result.put("user", userResult); - call.resolve(result); + try { + FirebaseUser user = implementation.getCurrentUser(); + JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user); + JSObject result = new JSObject(); + result.put("user", userResult); + call.resolve(result); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void getIdToken(PluginCall call) { - Boolean forceRefresh = call.getBoolean("forceRefresh", false); - - implementation.getIdToken( - forceRefresh, - new GetIdTokenResultCallback() { - @Override - public void success(String token) { - JSObject result = new JSObject(); - result.put("token", token); - call.resolve(result); - } - - @Override - public void error(String message) { - call.reject(message); + try { + Boolean forceRefresh = call.getBoolean("forceRefresh", false); + + implementation.getIdToken( + forceRefresh, + new GetIdTokenResultCallback() { + @Override + public void success(String token) { + JSObject result = new JSObject(); + result.put("token", token); + call.resolve(result); + } + + @Override + public void error(String message) { + call.reject(message); + } } - } - ); + ); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void sendEmailVerification(PluginCall call) { - FirebaseUser user = implementation.getCurrentUser(); - if (user == null) { - call.reject(ERROR_NO_USER_SIGNED_IN); - return; + try { + FirebaseUser user = implementation.getCurrentUser(); + if (user == null) { + call.reject(ERROR_NO_USER_SIGNED_IN); + return; + } + implementation.sendEmailVerification(user, () -> call.resolve()); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.sendEmailVerification(user, () -> call.resolve()); } @PluginMethod public void sendPasswordResetEmail(PluginCall call) { - String email = call.getString("email"); - if (email == null) { - call.reject(ERROR_EMAIL_MISSING); - return; + try { + String email = call.getString("email"); + if (email == null) { + call.reject(ERROR_EMAIL_MISSING); + return; + } + implementation.sendPasswordResetEmail(email, () -> call.resolve()); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.sendPasswordResetEmail(email, () -> call.resolve()); } @PluginMethod public void setLanguageCode(PluginCall call) { - String languageCode = call.getString("languageCode", ""); + try { + String languageCode = call.getString("languageCode", ""); - implementation.setLanguageCode(languageCode); - call.resolve(); + implementation.setLanguageCode(languageCode); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithApple(PluginCall call) { - implementation.signInWithApple(call); + try { + implementation.signInWithApple(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithCustomToken(PluginCall call) { - implementation.signInWithCustomToken(call); + try { + implementation.signInWithCustomToken(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithEmailAndPassword(PluginCall call) { - implementation.signInWithEmailAndPassword(call); + try { + implementation.signInWithEmailAndPassword(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithFacebook(PluginCall call) { - implementation.signInWithFacebook(call); + try { + implementation.signInWithFacebook(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithGithub(PluginCall call) { - implementation.signInWithGithub(call); + try { + implementation.signInWithGithub(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithGoogle(PluginCall call) { - implementation.signInWithGoogle(call); + try { + implementation.signInWithGoogle(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithMicrosoft(PluginCall call) { - implementation.signInWithMicrosoft(call); + try { + implementation.signInWithMicrosoft(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithPhoneNumber(PluginCall call) { - String phoneNumber = call.getString("phoneNumber"); - String verificationId = call.getString("verificationId"); - String verificationCode = call.getString("verificationCode"); + try { + String phoneNumber = call.getString("phoneNumber"); + String verificationId = call.getString("verificationId"); + String verificationCode = call.getString("verificationCode"); + + if (phoneNumber == null && (verificationId == null || verificationCode == null)) { + call.reject(ERROR_PHONE_NUMBER_SMS_CODE_MISSING); + return; + } - if (phoneNumber == null && (verificationId == null || verificationCode == null)) { - call.reject(ERROR_PHONE_NUMBER_SMS_CODE_MISSING); - return; + implementation.signInWithPhoneNumber(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - - implementation.signInWithPhoneNumber(call); } @PluginMethod public void signInWithPlayGames(PluginCall call) { - implementation.signInWithPlayGames(call); + try { + implementation.signInWithPlayGames(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithTwitter(PluginCall call) { - implementation.signInWithTwitter(call); + try { + implementation.signInWithTwitter(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signInWithYahoo(PluginCall call) { - implementation.signInWithYahoo(call); + try { + implementation.signInWithYahoo(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void signOut(PluginCall call) { - implementation.signOut(call); + try { + implementation.signOut(call); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void updateEmail(PluginCall call) { - String newEmail = call.getString("newEmail"); - if (newEmail == null) { - call.reject(ERROR_NEW_EMAIL_MISSING); - return; - } - FirebaseUser user = implementation.getCurrentUser(); - if (user == null) { - call.reject(ERROR_NO_USER_SIGNED_IN); - return; + try { + String newEmail = call.getString("newEmail"); + if (newEmail == null) { + call.reject(ERROR_NEW_EMAIL_MISSING); + return; + } + FirebaseUser user = implementation.getCurrentUser(); + if (user == null) { + call.reject(ERROR_NO_USER_SIGNED_IN); + return; + } + implementation.updateEmail(user, newEmail, () -> call.resolve()); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.updateEmail(user, newEmail, () -> call.resolve()); } @PluginMethod public void updatePassword(PluginCall call) { - String newPassword = call.getString("newPassword"); - if (newPassword == null) { - call.reject(ERROR_NEW_PASSWORD_MISSING); - return; - } - FirebaseUser user = implementation.getCurrentUser(); - if (user == null) { - call.reject(ERROR_NO_USER_SIGNED_IN); - return; + try { + String newPassword = call.getString("newPassword"); + if (newPassword == null) { + call.reject(ERROR_NEW_PASSWORD_MISSING); + return; + } + FirebaseUser user = implementation.getCurrentUser(); + if (user == null) { + call.reject(ERROR_NO_USER_SIGNED_IN); + return; + } + implementation.updatePassword(user, newPassword, () -> call.resolve()); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.updatePassword(user, newPassword, () -> call.resolve()); } @PluginMethod public void useAppLanguage(PluginCall call) { - implementation.useAppLanguage(); - call.resolve(); + try { + implementation.useAppLanguage(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void useEmulator(PluginCall call) { - String host = call.getString("host"); - if (host == null) { - call.reject(ERROR_HOST_MISSING); - return; - } - int port = call.getInt("port", 9099); + try { + String host = call.getString("host"); + if (host == null) { + call.reject(ERROR_HOST_MISSING); + return; + } + int port = call.getInt("port", 9099); - implementation.useEmulator(host, port); - call.resolve(); + implementation.useEmulator(host, port); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @Override diff --git a/packages/crashlytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java b/packages/crashlytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java index 57b6bb03..b11145c4 100644 --- a/packages/crashlytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java +++ b/packages/crashlytics/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java @@ -34,52 +34,68 @@ public void crash(PluginCall call) { @PluginMethod public void setCustomKey(PluginCall call) { - String key = call.getString("key"); - if (key == null) { - call.reject(ERROR_KEY_MISSING); - return; - } - boolean hasValue = call.hasOption("value"); - if (!hasValue) { - call.reject(ERROR_VALUE_MISSING); - return; + try { + String key = call.getString("key"); + if (key == null) { + call.reject(ERROR_KEY_MISSING); + return; + } + boolean hasValue = call.hasOption("value"); + if (!hasValue) { + call.reject(ERROR_VALUE_MISSING); + return; + } + String type = call.getString("type", "string"); + implementation.setCustomKey(key, type, call); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - String type = call.getString("type", "string"); - implementation.setCustomKey(key, type, call); - call.resolve(); } @PluginMethod public void setUserId(PluginCall call) { - String userId = call.getString("userId"); - if (userId == null) { - call.reject(ERROR_USERID_MISSING); - return; + try { + String userId = call.getString("userId"); + if (userId == null) { + call.reject(ERROR_USERID_MISSING); + return; + } + implementation.setUserId(userId); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.setUserId(userId); - call.resolve(); } @PluginMethod public void log(PluginCall call) { - String message = call.getString("message"); - if (message == null) { - call.reject(ERROR_MESSAGE_MISSING); - return; + try { + String message = call.getString("message"); + if (message == null) { + call.reject(ERROR_MESSAGE_MISSING); + return; + } + implementation.log(message); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.log(message); - call.resolve(); } @PluginMethod public void setEnabled(PluginCall call) { - Boolean enabled = call.getBoolean("enabled"); - if (enabled == null) { - call.reject(ERROR_ENABLED_MISSING); - return; + try { + Boolean enabled = call.getBoolean("enabled"); + if (enabled == null) { + call.reject(ERROR_ENABLED_MISSING); + return; + } + implementation.setEnabled(enabled); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.setEnabled(enabled); - call.resolve(); } @PluginMethod @@ -89,33 +105,49 @@ public void isEnabled(PluginCall call) { @PluginMethod public void didCrashOnPreviousExecution(PluginCall call) { - boolean crashed = implementation.didCrashOnPreviousExecution(); - JSObject ret = new JSObject(); - ret.put("crashed", crashed); - call.resolve(ret); + try { + boolean crashed = implementation.didCrashOnPreviousExecution(); + JSObject ret = new JSObject(); + ret.put("crashed", crashed); + call.resolve(ret); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void sendUnsentReports(PluginCall call) { - implementation.sendUnsentReports(); - call.resolve(); + try { + implementation.sendUnsentReports(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void deleteUnsentReports(PluginCall call) { - implementation.deleteUnsentReports(); - call.resolve(); + try { + implementation.deleteUnsentReports(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void recordException(PluginCall call) { - String message = call.getString("message"); - if (message == null) { - call.reject(ERROR_MESSAGE_MISSING); - return; - } + try { + String message = call.getString("message"); + if (message == null) { + call.reject(ERROR_MESSAGE_MISSING); + return; + } - implementation.recordException(message); - call.resolve(); + implementation.recordException(message); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } } diff --git a/packages/messaging/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/messaging/FirebaseMessagingPlugin.java b/packages/messaging/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/messaging/FirebaseMessagingPlugin.java index b4bd5fca..aa427a60 100644 --- a/packages/messaging/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/messaging/FirebaseMessagingPlugin.java +++ b/packages/messaging/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/messaging/FirebaseMessagingPlugin.java @@ -85,102 +85,130 @@ public void isSupported(PluginCall call) { @PluginMethod public void getToken(PluginCall call) { - implementation.getToken( - new GetTokenResultCallback() { - @Override - public void success(String token) { - handleTokenReceived(token); - JSObject result = new JSObject(); - result.put("token", token); - call.resolve(result); - } - - @Override - public void error(String message) { - call.reject(message); + try { + implementation.getToken( + new GetTokenResultCallback() { + @Override + public void success(String token) { + handleTokenReceived(token); + JSObject result = new JSObject(); + result.put("token", token); + call.resolve(result); + } + + @Override + public void error(String message) { + call.reject(message); + } } - } - ); + ); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void deleteToken(PluginCall call) { - implementation.deleteToken(); - call.resolve(); + try { + implementation.deleteToken(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void getDeliveredNotifications(PluginCall call) { - JSArray notificationsResult = new JSArray(); - StatusBarNotification[] activeNotifications = implementation.getDeliveredNotifications(); - for (StatusBarNotification activeNotification : activeNotifications) { - JSObject notificationResult = FirebaseMessagingHelper.createNotificationResult(activeNotification); - notificationsResult.put(notificationResult); - } + try { + JSArray notificationsResult = new JSArray(); + StatusBarNotification[] activeNotifications = implementation.getDeliveredNotifications(); + for (StatusBarNotification activeNotification : activeNotifications) { + JSObject notificationResult = FirebaseMessagingHelper.createNotificationResult(activeNotification); + notificationsResult.put(notificationResult); + } - JSObject result = new JSObject(); - result.put("notifications", notificationsResult); - call.resolve(result); + JSObject result = new JSObject(); + result.put("notifications", notificationsResult); + call.resolve(result); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void removeDeliveredNotifications(PluginCall call) { - JSArray notifications = call.getArray("notifications"); - if (notifications == null) { - call.reject(ERROR_NOTIFICATIONS_MISSING); - return; - } - - List tags = new ArrayList<>(); - List ids = new ArrayList<>(); try { - for (Object item : notifications.toList()) { - if (item instanceof JSONObject) { - JSObject notification = JSObject.fromJSONObject((JSONObject) item); - String tag = notification.getString("tag", ""); - tags.add(tag); - String id = notification.getString("id", ""); - ids.add(id); - } else { - call.reject(ERROR_NOTIFICATIONS_INVALID); - return; + JSArray notifications = call.getArray("notifications"); + if (notifications == null) { + call.reject(ERROR_NOTIFICATIONS_MISSING); + return; + } + + List tags = new ArrayList<>(); + List ids = new ArrayList<>(); + try { + for (Object item : notifications.toList()) { + if (item instanceof JSONObject) { + JSObject notification = JSObject.fromJSONObject((JSONObject) item); + String tag = notification.getString("tag", ""); + tags.add(tag); + String id = notification.getString("id", ""); + ids.add(id); + } else { + call.reject(ERROR_NOTIFICATIONS_INVALID); + return; + } } + } catch (JSONException e) { + call.reject(ERROR_NOTIFICATIONS_INVALID); + return; } - } catch (JSONException e) { - call.reject(ERROR_NOTIFICATIONS_INVALID); - return; - } - implementation.removeDeliveredNotifications(tags, ids); - call.resolve(); + implementation.removeDeliveredNotifications(tags, ids); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void removeAllDeliveredNotifications(PluginCall call) { - implementation.removeAllDeliveredNotifications(); - call.resolve(); + try { + implementation.removeAllDeliveredNotifications(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } @PluginMethod public void subscribeToTopic(PluginCall call) { - String topic = call.getString("topic"); - if (topic == null) { - call.reject(ERROR_TOPIC_MISSING); - return; + try { + String topic = call.getString("topic"); + if (topic == null) { + call.reject(ERROR_TOPIC_MISSING); + return; + } + implementation.subscribeToTopic(topic); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.subscribeToTopic(topic); - call.resolve(); } @PluginMethod public void unsubscribeFromTopic(PluginCall call) { - String topic = call.getString("topic"); - if (topic == null) { - call.reject(ERROR_TOPIC_MISSING); - return; + try { + String topic = call.getString("topic"); + if (topic == null) { + call.reject(ERROR_TOPIC_MISSING); + return; + } + implementation.unsubscribeFromTopic(topic); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.unsubscribeFromTopic(topic); - call.resolve(); } private void handleTokenReceived(@NonNull String token) { diff --git a/packages/performance/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java b/packages/performance/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java index f92e6dd5..f24b3915 100644 --- a/packages/performance/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java +++ b/packages/performance/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java @@ -19,74 +19,94 @@ public class FirebasePerformancePlugin extends Plugin { @PluginMethod public void startTrace(PluginCall call) { - String traceName = call.getString("traceName"); - if (traceName == null) { - call.reject(ERROR_TRACE_NAME_MISSING); - return; + try { + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(ERROR_TRACE_NAME_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); + if (trace != null) { + call.reject(ERROR_TRACE_NAME_ALREADY_ASSIGNED); + return; + } + implementation.startTrace(traceName); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - Trace trace = implementation.getTraceByName(traceName); - if (trace != null) { - call.reject(ERROR_TRACE_NAME_ALREADY_ASSIGNED); - return; - } - implementation.startTrace(traceName); - call.resolve(); } @PluginMethod public void stopTrace(PluginCall call) { - String traceName = call.getString("traceName"); - if (traceName == null) { - call.reject(ERROR_TRACE_NAME_MISSING); - return; - } - Trace trace = implementation.getTraceByName(traceName); - if (trace == null) { - call.reject(ERROR_TRACE_NOT_FOUND); - return; + try { + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(ERROR_TRACE_NAME_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + implementation.stopTrace(traceName); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.stopTrace(traceName); - call.resolve(); } @PluginMethod public void incrementMetric(PluginCall call) { - String traceName = call.getString("traceName"); - String metricName = call.getString("metricName"); - Integer incrementBy = call.getInt("incrementBy", 1); - if (traceName == null) { - call.reject(ERROR_TRACE_NAME_MISSING); - return; - } - if (metricName == null) { - call.reject(ERROR_METRIC_NAME_MISSING); - return; + try { + String traceName = call.getString("traceName"); + String metricName = call.getString("metricName"); + Integer incrementBy = call.getInt("incrementBy", 1); + if (traceName == null) { + call.reject(ERROR_TRACE_NAME_MISSING); + return; + } + if (metricName == null) { + call.reject(ERROR_METRIC_NAME_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + implementation.incrementMetric(traceName, metricName, incrementBy); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - Trace trace = implementation.getTraceByName(traceName); - if (trace == null) { - call.reject(ERROR_TRACE_NOT_FOUND); - return; - } - implementation.incrementMetric(traceName, metricName, incrementBy); - call.resolve(); } @PluginMethod public void setPerformanceCollectionEnabled(PluginCall call) { - Boolean enabled = call.getBoolean("enabled"); - if (enabled == null) { - call.reject(ERROR_ENABLED_MISSING); - return; + try { + Boolean enabled = call.getBoolean("enabled"); + if (enabled == null) { + call.reject(ERROR_ENABLED_MISSING); + return; + } + implementation.setPerformanceCollectionEnabled(enabled); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); } - implementation.setPerformanceCollectionEnabled(enabled); - call.resolve(); } @PluginMethod public void isPerformanceCollectionEnabled(PluginCall call) { - Boolean enabled = implementation.isPerformanceCollectionEnabled(); - JSObject result = new JSObject(); - result.put("enabled", enabled); - call.resolve(result); + try { + Boolean enabled = implementation.isPerformanceCollectionEnabled(); + JSObject result = new JSObject(); + result.put("enabled", enabled); + call.resolve(result); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } } } From 4e5f59d9261f769d854485d386912ac151fd7118 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Fri, 15 Jul 2022 17:54:40 +0200 Subject: [PATCH 2/2] docs: add changeset --- .changeset/wise-rivers-reflect.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .changeset/wise-rivers-reflect.md diff --git a/.changeset/wise-rivers-reflect.md b/.changeset/wise-rivers-reflect.md new file mode 100644 index 00000000..aaf7fc5e --- /dev/null +++ b/.changeset/wise-rivers-reflect.md @@ -0,0 +1,10 @@ +--- +"@capacitor-firebase/analytics": patch +"@capacitor-firebase/app": patch +"@capacitor-firebase/authentication": patch +"@capacitor-firebase/crashlytics": patch +"@capacitor-firebase/messaging": patch +"@capacitor-firebase/performance": patch +--- + +fix(android): catch all native errors and pass to the webview