Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(crashlytics): support isEnabled() on Android #491

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/crashlytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ isEnabled() => Promise<IsEnabledResult>

Returns whether or not automatic data collection is enabled.

Only available for iOS.
Only available for Android and iOS.

**Returns:** <code>Promise&lt;<a href="#isenabledresult">IsEnabledResult</a>&gt;</code>

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class FirebaseCrashlyticsPlugin extends Plugin {

@Override
public void load() {
implementation = new FirebaseCrashlytics();
implementation = new FirebaseCrashlytics(this);
}

@PluginMethod
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/crashlytics/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down