Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit a61ca71

Browse files
Fix sentry + arch specific builds
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent edea473 commit a61ca71

File tree

4 files changed

+64
-107
lines changed

4 files changed

+64
-107
lines changed

app/build.gradle

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ android {
3333
signingConfig signingConfigs.release
3434
}
3535

36+
splits {
37+
38+
// Configures multiple APKs based on ABI.
39+
abi {
40+
41+
// Enables building multiple APKs per ABI.
42+
enable true
43+
44+
// By default all ABIs are included, so use reset() and include to specify that we only
45+
// want APKs for x86 and x86_64.
46+
47+
// Resets the list of ABIs that Gradle should create APKs for to none.
48+
reset()
49+
50+
// Specifies a list of ABIs that Gradle should create APKs for.
51+
include "x86", "x86_64", "armeabi-v7a", "arm64-v8a"
52+
53+
// Specifies that we do not want to also generate a universal APK that includes all ABIs.
54+
universalApk true
55+
}
56+
}
57+
3658
buildTypes {
3759
release {
3860
minifyEnabled true

app/src/main/java/com/fox2code/mmm/MainActivity.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@
5959
import org.json.JSONObject;
6060

6161
import java.io.IOException;
62-
import java.io.InputStream;
63-
import java.io.OutputStream;
6462
import java.net.HttpURLConnection;
6563
import java.net.URL;
6664

6765
import eightbitlab.com.blurview.BlurView;
68-
import io.sentry.Sentry;
6966

7067
public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRefreshListener, SearchView.OnQueryTextListener, SearchView.OnCloseListener, OverScrollManager.OverScrollHelper {
7168
private static final String TAG = "MainActivity";
@@ -258,8 +255,10 @@ public void commonNext() {
258255
if (MainApplication.isCrashReportingEnabled()) {
259256
SharedPreferences preferences = getSharedPreferences("sentry", MODE_PRIVATE);
260257
String lastExitReason = preferences.getString("lastExitReason", "");
258+
if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Last Exit Reason: " + lastExitReason);
261259
if (lastExitReason.equals("crash")) {
262260
String lastEventId = preferences.getString("lastEventId", "");
261+
if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Last Event ID: " + lastEventId);
263262
if (!lastEventId.equals("")) {
264263
// Three edit texts for the user to enter their email, name and a description of the issue
265264
EditText email = new EditText(this);
@@ -299,47 +298,31 @@ public void commonNext() {
299298
connection.setRequestMethod("POST");
300299
connection.setRequestProperty("Content-Type", "application/json");
301300
connection.setRequestProperty("Authorization", "Bearer " + BuildConfig.SENTRY_TOKEN);
302-
connection.setDoOutput(true);
303-
connection.setDoInput(true);
304-
connection.setChunkedStreamingMode(0);
305-
connection.connect();
306-
// Write the JSON data to the output stream
307-
OutputStream outputStream = connection.getOutputStream();
308-
// Name and email are optional, so if they are empty, set them to
309-
// Anonymous and Anonymous respectively
301+
// Setups the JSON body
310302
String nameString = name.getText().toString();
311303
String emailString = email.getText().toString();
312304
if (nameString.equals("")) nameString = "Anonymous";
313305
if (emailString.equals("")) emailString = "Anonymous";
314-
String finalNameString = nameString;
315-
String finalEmailString = emailString;
316-
outputStream.write(new JSONObject() {{
317-
put("event_id", lastEventId);
318-
put("name", finalNameString);
319-
put("email", finalEmailString);
320-
put("comments", description.getText().toString());
321-
}}.toString().getBytes());
322-
outputStream.flush();
323-
outputStream.close();
324-
// Read the response
325-
InputStream inputStream = connection.getInputStream();
326-
byte[] buffer = new byte[1024];
327-
int length;
328-
StringBuilder stringBuilder = new StringBuilder();
329-
while ((length = inputStream.read(buffer)) != -1) {
330-
stringBuilder.append(new String(buffer, 0, length));
306+
JSONObject body = new JSONObject();
307+
body.put("event_id", lastEventId);
308+
body.put("name", nameString);
309+
body.put("email", emailString);
310+
body.put("comments", description.getText().toString());
311+
// Send the request
312+
connection.setDoOutput(true);
313+
connection.getOutputStream().write(body.toString().getBytes());
314+
connection.connect();
315+
// For debug builds, log the response code and response body
316+
if (BuildConfig.DEBUG) {
317+
Log.d("NoodleDebug", "Response Code: " + connection.getResponseCode());
331318
}
332-
inputStream.close();
333-
connection.disconnect();
334-
if (BuildConfig.DEBUG) Log.d("Sentry", stringBuilder.toString());
335-
// Valid responses will be a json object with a key "id"
336-
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
337-
if (jsonObject.has("id")) {
338-
// Show a toast to the user to confirm that the feedback has been sent
319+
// Check if the request was successful
320+
if (connection.getResponseCode() == 200) {
339321
runOnUiThread(() -> Toast.makeText(this, R.string.sentry_dialogue_success, Toast.LENGTH_LONG).show());
340322
} else {
341-
// Show a toast to the user to confirm that the feedback has not been sent
342-
runOnUiThread(() -> Toast.makeText(this, R.string.sentry_dialogue_failed_toast, Toast.LENGTH_LONG).show());
323+
runOnUiThread(() -> Toast.makeText(this,
324+
R.string.sentry_dialogue_failed_toast,
325+
Toast.LENGTH_LONG).show());
343326
}
344327
} catch (IOException | JSONException ignored) {
345328
// Show a toast if the user feedback could not be submitted
@@ -348,7 +331,8 @@ public void commonNext() {
348331
}).start();
349332
}).setNegativeButton(R.string.cancel, (dialog, which) -> {
350333
preferences.edit().remove("lastEventId").apply();
351-
Sentry.captureMessage("User has ignored the crash");
334+
preferences.edit().putString("lastExitReason", "").apply();
335+
Log.w(TAG, "User cancelled sentry dialog");
352336
}).show();
353337
}
354338
}

app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
330330
} else {
331331
findPreference("pref_crash").setOnPreferenceClickListener(preference -> {
332332
// Hard crash the app
333-
throw new RuntimeException("This is a test crash");
333+
throw new Error("This is a test crash");
334334
});
335335
}
336336
if (InstallerInitializer.peekMagiskVersion() < Constants.MAGISK_VER_CODE_INSTALL_COMMAND || !MainApplication.isDeveloper()) {

app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
package com.fox2code.mmm.sentry;
22

3-
import static io.sentry.TypeCheckHint.SENTRY_TYPE_CHECK_HINT;
4-
53
import android.annotation.SuppressLint;
64
import android.content.Context;
75
import android.content.SharedPreferences;
86
import android.net.Uri;
9-
import android.util.Log;
107

11-
import com.fox2code.mmm.BuildConfig;
128
import com.fox2code.mmm.MainApplication;
139
import com.fox2code.mmm.androidacy.AndroidacyUtil;
1410

15-
import java.io.IOException;
16-
import java.io.Writer;
1711
import java.util.Objects;
1812

19-
import io.sentry.JsonObjectWriter;
20-
import io.sentry.NoOpLogger;
2113
import io.sentry.Sentry;
2214
import io.sentry.android.core.SentryAndroid;
2315
import io.sentry.android.fragment.FragmentLifecycleIntegration;
24-
import io.sentry.hints.DiskFlushNotification;
25-
import io.sentry.protocol.SentryId;
2616

2717
public class SentryMain {
2818
public static final boolean IS_SENTRY_INSTALLED = true;
@@ -34,6 +24,16 @@ public class SentryMain {
3424
*/
3525
@SuppressLint({"RestrictedApi", "UnspecifiedImmutableFlag"})
3626
public static void initialize(final MainApplication mainApplication) {
27+
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
28+
SharedPreferences.Editor editor = mainApplication.getSharedPreferences(
29+
"sentry", Context.MODE_PRIVATE).edit();
30+
editor.putString("lastExitReason", "crash");
31+
editor.apply();
32+
// If we just let the default uncaught exception handler handle the
33+
// exception, the app will hang and never close.
34+
// So we need to kill the app ourselves.
35+
System.exit(1);
36+
});
3737
SentryAndroid.init(mainApplication, options -> {
3838
// If crash reporting is disabled, stop here.
3939
if (!MainApplication.isCrashReportingEnabled()) {
@@ -55,54 +55,15 @@ public static void initialize(final MainApplication mainApplication) {
5555
// Add a callback that will be used before the event is sent to Sentry.
5656
// With this callback, you can modify the event or, when returning null, also discard the event.
5757
options.setBeforeSend((event, hint) -> {
58-
if (BuildConfig.DEBUG) { // Debug sentry events for debug.
59-
StringBuilder stringBuilder = new StringBuilder("Sentry report debug: ");
60-
try {
61-
event.serialize(new JsonObjectWriter(new Writer() {
62-
@Override
63-
public void write(char[] cbuf) {
64-
stringBuilder.append(cbuf);
65-
}
66-
67-
@Override
68-
public void write(String str) {
69-
stringBuilder.append(str);
70-
}
71-
72-
@Override
73-
public void write(char[] chars, int i, int i1) {
74-
stringBuilder.append(chars, i, i1);
75-
}
76-
77-
@Override
78-
public void write(String str, int off, int len) {
79-
stringBuilder.append(str, off, len);
80-
}
81-
82-
@Override
83-
public void flush() {
84-
}
85-
86-
@Override
87-
public void close() {
88-
}
89-
}, 4), NoOpLogger.getInstance());
90-
} catch (IOException ignored) {
91-
}
92-
Log.i(TAG, stringBuilder.toString());
93-
}
94-
if (MainApplication.isCrashReportingEnabled()) {
95-
// Save lastEventId to private shared preferences
96-
SharedPreferences sharedPreferences = mainApplication.getSharedPreferences("sentry", Context.MODE_PRIVATE);
97-
sharedPreferences.edit().putString("lastEventId",
98-
Objects.requireNonNull(event.getEventId()).toString()).apply();
99-
return event;
100-
} else {
101-
// We need to do this to avoid crash delay on crash when the event is dropped
102-
DiskFlushNotification diskFlushNotification = hint.getAs(SENTRY_TYPE_CHECK_HINT, DiskFlushNotification.class);
103-
if (diskFlushNotification != null) diskFlushNotification.markFlushed();
104-
return null;
105-
}
58+
// Save lastEventId to private shared preferences
59+
SharedPreferences sharedPreferences = MainApplication.getINSTANCE().getSharedPreferences(
60+
"sentry",
61+
Context.MODE_PRIVATE);
62+
String lastEventId = Objects.requireNonNull(event.getEventId()).toString();
63+
SharedPreferences.Editor editor = sharedPreferences.edit();
64+
editor.putString("lastEventId", lastEventId);
65+
editor.apply();
66+
return event;
10667
});
10768
// Filter breadrcrumb content from crash report.
10869
options.setBeforeBreadcrumb((breadcrumb, hint) -> {
@@ -114,16 +75,6 @@ public void close() {
11475
}
11576
return breadcrumb;
11677
});
117-
// On uncaught exception, set the lastEventId in private sentry preferences
118-
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
119-
SentryId lastEventId = Sentry.captureException(throwable);
120-
SharedPreferences.Editor editor = mainApplication.getSharedPreferences(
121-
"sentry", Context.MODE_PRIVATE).edit();
122-
editor.putString("lastExitReason", "crash");
123-
editor.apply();
124-
// Kill the app
125-
System.exit(2);
126-
});
12778
}
12879
});
12980
}

0 commit comments

Comments
 (0)