Skip to content

Commit 0f1ac4c

Browse files
authored
added a new content provider that writes app start profiling options to a config file to make the SDK think it was already initialized (sentry-demos#136)
1 parent 49da0f9 commit 0f1ac4c

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

app/src/main/AndroidManifest.xml

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@
9191
android:authorities="${applicationId}.thirdpartycontentprovider"
9292
android:name=".ThirdPartyContentProvider" />
9393

94+
<provider
95+
android:exported="false"
96+
android:authorities="${applicationId}.initcontentprovider"
97+
android:initOrder="9999"
98+
android:name=".InitContentProvider" />
99+
94100

95101
</application>
96102

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.example.vu.android;
2+
3+
import static io.sentry.Sentry.APP_START_PROFILING_CONFIG_FILE_NAME;
4+
5+
import android.annotation.SuppressLint;
6+
import android.content.ContentProvider;
7+
import android.content.ContentValues;
8+
import android.content.Context;
9+
import android.database.Cursor;
10+
import android.net.Uri;
11+
12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
15+
import org.jetbrains.annotations.NotNull;
16+
17+
import java.io.BufferedWriter;
18+
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
import java.io.OutputStreamWriter;
23+
import java.io.Writer;
24+
import java.nio.charset.StandardCharsets;
25+
26+
import io.sentry.SentryAppStartProfilingOptions;
27+
import io.sentry.SentryOptions;
28+
29+
public class InitContentProvider extends ContentProvider {
30+
31+
@Override
32+
public boolean onCreate() {
33+
34+
final Context context = getContext();
35+
if (context == null) {
36+
return true;
37+
}
38+
39+
final @NotNull SentryOptions options = createOptions(context);
40+
final @NotNull SentryAppStartProfilingOptions appStartProfilingOptions = createAppStartOptions(options);
41+
// Manually create all folders required by the SDK to work
42+
final @NotNull File cacheDir = new File(context.getCacheDir(), "sentry");
43+
final @NotNull File configFile = new File(cacheDir, APP_START_PROFILING_CONFIG_FILE_NAME);
44+
cacheDir.mkdirs();
45+
new File(options.getProfilingTracesDirPath()).mkdirs();
46+
47+
// Write option to config file to make the SDK think it was already initialized.
48+
try (final OutputStream outputStream = new FileOutputStream(configFile);
49+
final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
50+
options.getSerializer().serialize(appStartProfilingOptions, writer);
51+
} catch (IOException e) {
52+
throw new RuntimeException(e);
53+
}
54+
return true;
55+
}
56+
57+
@SuppressLint("VisibleForTests")
58+
private SentryOptions createOptions(final @NotNull Context context) {
59+
SentryOptions options = new SentryOptions();
60+
options.setDsn("https://[email protected]/1801383");
61+
options.setCacheDirPath(new File(context.getCacheDir(), "sentry").getAbsolutePath());
62+
return options;
63+
}
64+
65+
@SuppressLint("VisibleForTests")
66+
private SentryAppStartProfilingOptions createAppStartOptions(SentryOptions options) {
67+
SentryAppStartProfilingOptions appStartProfilingOptions = new SentryAppStartProfilingOptions();
68+
appStartProfilingOptions.setProfileSampled(true);
69+
appStartProfilingOptions.setTraceSampled(true);
70+
appStartProfilingOptions.setProfileSampleRate(1.0);
71+
appStartProfilingOptions.setTraceSampleRate(1.0);
72+
appStartProfilingOptions.setProfilingTracesDirPath(options.getProfilingTracesDirPath());
73+
appStartProfilingOptions.setProfilingEnabled(true);
74+
appStartProfilingOptions.setProfilingTracesHz(options.getProfilingTracesHz());
75+
return appStartProfilingOptions;
76+
}
77+
78+
@Nullable
79+
@Override
80+
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
81+
return null;
82+
}
83+
84+
@Nullable
85+
@Override
86+
public String getType(@NonNull Uri uri) {
87+
return null;
88+
}
89+
90+
@Nullable
91+
@Override
92+
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
93+
return null;
94+
}
95+
96+
@Override
97+
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
98+
return 0;
99+
}
100+
101+
@Override
102+
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
103+
return 0;
104+
}
105+
}

0 commit comments

Comments
 (0)