From b61c7e3940a34a068160366c4b3e2e148e5e9b8d Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Fri, 17 Nov 2023 15:07:34 +0100 Subject: [PATCH] feat(crashlytics): support `isEnabled()` on Android --- packages/crashlytics/README.md | 2 +- .../crashlytics/FirebaseCrashlytics.java | 48 ++++++++++++++++++- .../FirebaseCrashlyticsPlugin.java | 13 ++++- .../FirebaseCrashlyticsTests.swift | 9 ---- packages/crashlytics/src/definitions.ts | 2 +- 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/packages/crashlytics/README.md b/packages/crashlytics/README.md index 9fa9abdd..1cbd3e8d 100644 --- a/packages/crashlytics/README.md +++ b/packages/crashlytics/README.md @@ -232,7 +232,7 @@ isEnabled() => Promise Returns whether or not automatic data collection is enabled. -Only available for iOS. +Only available for Android and iOS. **Returns:** Promise<IsEnabledResult> diff --git a/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlytics.java b/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlytics.java index 4a2aee0b..778d577d 100644 --- a/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlytics.java +++ b/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlytics.java @@ -1,14 +1,27 @@ package io.capawesome.capacitorjs.plugins.firebase.crashlytics; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.os.Bundle; + +import androidx.annotation.Nullable; import com.getcapacitor.JSArray; +import com.getcapacitor.Logger; import com.getcapacitor.PluginCall; import org.json.JSONException; public class FirebaseCrashlytics { + private static final String KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED = "crashlytics_auto_collection_enabled"; + private static final String KEY_CRASHLYTICS_SHARED_PREFS = "com.google.firebase.crashlytics"; + private final com.google.firebase.crashlytics.FirebaseCrashlytics crashlyticsInstance; + private final FirebaseCrashlyticsPlugin plugin; - FirebaseCrashlytics() { + FirebaseCrashlytics(FirebaseCrashlyticsPlugin plugin) { + this.plugin = plugin; crashlyticsInstance = com.google.firebase.crashlytics.FirebaseCrashlytics.getInstance(); } @@ -50,6 +63,18 @@ public void setEnabled(Boolean enabled) { crashlyticsInstance.setCrashlyticsCollectionEnabled(enabled); } + public boolean isEnabled() { + boolean enabled; + SharedPreferences sharedPreferences = this.getFirebaseCrashlyticsSharedPreferences(); + if (sharedPreferences.contains(FirebaseCrashlytics.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED)) { + enabled = sharedPreferences.getBoolean(FirebaseCrashlytics.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, true); + } else { + Bundle metaData = this.getApplicationMetaData(); + enabled = metaData.getBoolean(FirebaseCrashlytics.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, true); + } + return enabled; + } + public boolean didCrashOnPreviousExecution() { return crashlyticsInstance.didCrashOnPreviousExecution(); } @@ -67,6 +92,27 @@ public void recordException(String message, JSArray stacktrace) { crashlyticsInstance.recordException(throwable); } + private SharedPreferences getFirebaseCrashlyticsSharedPreferences() { + Context context = this.plugin.getActivity().getApplicationContext(); + return context.getSharedPreferences(FirebaseCrashlytics.KEY_CRASHLYTICS_SHARED_PREFS, Context.MODE_PRIVATE); + } + + @Nullable + private Bundle getApplicationMetaData() { + Context context = this.plugin.getActivity().getApplicationContext(); + try { + PackageManager packageManager = context.getPackageManager(); + if (packageManager == null) { + return null; + } + ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); + return applicationInfo.metaData; + } catch (PackageManager.NameNotFoundException exception) { + // Ignore + } + return null; + } + private JavaScriptException getJavaScriptException(String message, JSArray stacktrace) { if (stacktrace == null) { return new JavaScriptException(message); diff --git a/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java b/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java index 1c3bcdbd..42901a30 100644 --- a/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java +++ b/packages/crashlytics/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/crashlytics/FirebaseCrashlyticsPlugin.java @@ -21,7 +21,7 @@ public class FirebaseCrashlyticsPlugin extends Plugin { @Override public void load() { - implementation = new FirebaseCrashlytics(); + implementation = new FirebaseCrashlytics(this); } @PluginMethod @@ -107,7 +107,16 @@ public void setEnabled(PluginCall call) { @PluginMethod public void isEnabled(PluginCall call) { - call.unimplemented("Not implemented on Android."); + try { + boolean enabled = implementation.isEnabled(); + + JSObject result = new JSObject(); + result.put("enabled", enabled); + call.resolve(result); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } } @PluginMethod diff --git a/packages/crashlytics/ios/PluginTests/FirebaseCrashlyticsTests.swift b/packages/crashlytics/ios/PluginTests/FirebaseCrashlyticsTests.swift index d1b71d5d..0991365f 100644 --- a/packages/crashlytics/ios/PluginTests/FirebaseCrashlyticsTests.swift +++ b/packages/crashlytics/ios/PluginTests/FirebaseCrashlyticsTests.swift @@ -2,15 +2,6 @@ import XCTest @testable import Plugin class FirebaseCrashlyticsTests: XCTestCase { - override func setUp() { - super.setUp() - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } func testEcho() { // This is an example of a functional test case for a plugin. diff --git a/packages/crashlytics/src/definitions.ts b/packages/crashlytics/src/definitions.ts index 73da89b7..f7713fe3 100644 --- a/packages/crashlytics/src/definitions.ts +++ b/packages/crashlytics/src/definitions.ts @@ -43,7 +43,7 @@ export interface FirebaseCrashlyticsPlugin { /** * Returns whether or not automatic data collection is enabled. * - * Only available for iOS. + * Only available for Android and iOS. * * @since 0.1.0 */