Skip to content

Commit

Permalink
feat(telemetry): Update sentry from 3.x to 7.x
Browse files Browse the repository at this point in the history
Telemetry is still not included in GitHub build, and disabled by default on other stores.
  • Loading branch information
PerfectSlayer committed May 12, 2024
1 parent 54d31fc commit a23c74b
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 48 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ dependencies {
if (keyStoreDefined) {
implementation project(':sentrystub')
} else {
implementation 'io.sentry:sentry-android:3.1.0'
implementation platform('io.sentry:sentry-bom:7.8.0')
implementation('io.sentry:sentry-android')
implementation('io.sentry:sentry-android-fragment')
implementation('io.sentry:sentry-android-timber')
}

// Root related
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
android:value="false" />
<meta-data
android:name="io.sentry.dsn"
android:value="https://[email protected]/1331667" />
android:value="https://[email protected]/1331667" />
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void bindTelemetryPrefAction() {
Preference enableTelemetryPref = findPreference(getString(R.string.pref_enable_telemetry_key));
assert enableTelemetryPref != null : PREFERENCE_NOT_FOUND;
enableTelemetryPref.setOnPreferenceChangeListener((preference, newValue) -> {
SentryLog.setEnabled(getContext(), (boolean) newValue);
SentryLog.setEnabled(requireActivity().getApplication(), (boolean) newValue);
return true;
});
if (SentryLog.isStub()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void bindSupport() {
private void bindTelemetry() {
this.binding.telemetryCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
PreferenceHelper.setTelemetryEnabled(requireContext(), isChecked);
SentryLog.setEnabled(getContext(), isChecked);
SentryLog.setEnabled(requireActivity().getApplication(), isChecked);
});
}

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/adaway/util/log/ApplicationLog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.adaway.util.log;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;

Expand All @@ -25,16 +26,15 @@ private ApplicationLog() {
/**
* Initialize application logging.
*
* @param context The application context.
* @param application The application instance.
*/
public static void init(Context context) {
if (isApplicationDebuggable(context) || PreferenceHelper.getDebugEnabled(context)) {
public static void init(Application application) {
if (isApplicationDebuggable(application) || PreferenceHelper.getDebugEnabled(application)) {
Shell.enableVerboseLogging = true;
Timber.plant(new Timber.DebugTree());
} else {
Shell.enableVerboseLogging = false;
SentryLog.init(context);
Timber.plant(new SentryLog.SentryTree());
SentryLog.init(application);
}
}

Expand Down
43 changes: 16 additions & 27 deletions app/src/main/java/org/adaway/util/log/SentryLog.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package org.adaway.util.log;

import android.content.Context;
import android.util.Log;
import static io.sentry.SentryLevel.ERROR;
import static io.sentry.SentryLevel.INFO;

import android.app.Application;

import org.adaway.helper.PreferenceHelper;
import org.jetbrains.annotations.Nullable;

import io.sentry.Breadcrumb;
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.android.core.SentryAndroid;
import timber.log.Timber;

import static io.sentry.SentryLevel.INFO;

import androidx.annotation.NonNull;
import io.sentry.android.fragment.FragmentLifecycleIntegration;
import io.sentry.android.timber.SentryTimberIntegration;

/**
* This class is a helper to initialize and configuration Sentry.
Expand All @@ -32,22 +29,25 @@ private SentryLog() {
/**
* Initialize Sentry logging client according user preferences.
*
* @param context The application context.
* @param application The application instance.
*/
public static void init(Context context) {
setEnabled(context, PreferenceHelper.getTelemetryEnabled(context));
public static void init(Application application) {
setEnabled(application, PreferenceHelper.getTelemetryEnabled(application));
}

/**
* Initialize Sentry logging client.
*
* @param context The application context.
* @param application The application instance.
* @param enabled Whether the application is allowed to send events to Sentry or not.
*/
public static void setEnabled(Context context, boolean enabled) {
public static void setEnabled(Application application, boolean enabled) {
if (enabled) {
// Initialize sentry client manually
SentryAndroid.init(context);
// Initialize sentry client manually and bind it to logging
SentryAndroid.init(application, options -> {
options.addIntegration(new SentryTimberIntegration(ERROR, INFO));
options.addIntegration(new FragmentLifecycleIntegration(application, true, false));
});
}
}

Expand Down Expand Up @@ -78,15 +78,4 @@ public static boolean isStub() {
return false;
}
}

static class SentryTree extends Timber.Tree {
@Override
protected void log(int priority, @Nullable String tag, @NonNull String message, @Nullable Throwable throwable) {
if (priority == Log.WARN) {
Sentry.captureMessage(message, SentryLevel.WARNING);
} else if (priority == Log.ERROR) {
Sentry.captureMessage(message, SentryLevel.ERROR);
}
}
}
}
4 changes: 4 additions & 0 deletions sentrystub/src/main/java/io/sentry/Integration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.sentry;

public interface Integration {
}
19 changes: 8 additions & 11 deletions sentrystub/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
public class Sentry {
public static final boolean STUB = true;

public static void capture(String message) {
// Stub
}

public static void captureMessage(String msg, SentryLevel info) {
// Stub
}

public static void captureException(Throwable tr, String msg) {
public static void configureScope(ScopeCallback callback) {
// Stub
}

public static void configureScope(ScopeCallback callback) {
// Stub
public interface OptionsConfiguration<T extends SentryOptions> {
/**
* configure the options
*
* @param options the options
*/
void configure(T options);
}
}
7 changes: 7 additions & 0 deletions sentrystub/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.sentry;

public class SentryOptions {
public void addIntegration(Integration integration) {
// Stub
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import android.content.Context;

import io.sentry.Sentry;
import io.sentry.SentryOptions;

public class SentryAndroid {
public static void init(Context context) {
public static void init(Context context, Sentry.OptionsConfiguration<SentryOptions> options) {
// Stub
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.sentry.android.fragment;

import android.app.Application;

import io.sentry.Integration;

public class FragmentLifecycleIntegration implements Integration {
public FragmentLifecycleIntegration(Application application, boolean enableFragmentLifecycleBreadcrumbs, boolean enableAutoFragmentLifecycleTracing ) {
// Stub
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.sentry.android.timber;

import io.sentry.Integration;
import io.sentry.SentryLevel;

public class SentryTimberIntegration implements Integration {
public SentryTimberIntegration(SentryLevel minEventLevel, SentryLevel minBreadcrumbLevel) {
// Stub
}
}

0 comments on commit a23c74b

Please sign in to comment.