From a61ca71221bfa128a6fc6a22cda8e55077c9b3f9 Mon Sep 17 00:00:00 2001 From: androidacy-user Date: Mon, 12 Dec 2022 11:48:01 -0500 Subject: [PATCH] Fix sentry + arch specific builds Signed-off-by: androidacy-user --- app/build.gradle | 22 +++++ .../java/com/fox2code/mmm/MainActivity.java | 60 +++++-------- .../mmm/settings/SettingsActivity.java | 2 +- .../com/fox2code/mmm/sentry/SentryMain.java | 87 ++++--------------- 4 files changed, 64 insertions(+), 107 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index bcee40d..9e02207 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,6 +33,28 @@ android { signingConfig signingConfigs.release } + splits { + + // Configures multiple APKs based on ABI. + abi { + + // Enables building multiple APKs per ABI. + enable true + + // By default all ABIs are included, so use reset() and include to specify that we only + // want APKs for x86 and x86_64. + + // Resets the list of ABIs that Gradle should create APKs for to none. + reset() + + // Specifies a list of ABIs that Gradle should create APKs for. + include "x86", "x86_64", "armeabi-v7a", "arm64-v8a" + + // Specifies that we do not want to also generate a universal APK that includes all ABIs. + universalApk true + } + } + buildTypes { release { minifyEnabled true diff --git a/app/src/main/java/com/fox2code/mmm/MainActivity.java b/app/src/main/java/com/fox2code/mmm/MainActivity.java index 57bcc17..8795840 100644 --- a/app/src/main/java/com/fox2code/mmm/MainActivity.java +++ b/app/src/main/java/com/fox2code/mmm/MainActivity.java @@ -59,13 +59,10 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import eightbitlab.com.blurview.BlurView; -import io.sentry.Sentry; public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRefreshListener, SearchView.OnQueryTextListener, SearchView.OnCloseListener, OverScrollManager.OverScrollHelper { private static final String TAG = "MainActivity"; @@ -258,8 +255,10 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe if (MainApplication.isCrashReportingEnabled()) { SharedPreferences preferences = getSharedPreferences("sentry", MODE_PRIVATE); String lastExitReason = preferences.getString("lastExitReason", ""); + if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Last Exit Reason: " + lastExitReason); if (lastExitReason.equals("crash")) { String lastEventId = preferences.getString("lastEventId", ""); + if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Last Event ID: " + lastEventId); if (!lastEventId.equals("")) { // Three edit texts for the user to enter their email, name and a description of the issue EditText email = new EditText(this); @@ -299,47 +298,31 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Authorization", "Bearer " + BuildConfig.SENTRY_TOKEN); - connection.setDoOutput(true); - connection.setDoInput(true); - connection.setChunkedStreamingMode(0); - connection.connect(); - // Write the JSON data to the output stream - OutputStream outputStream = connection.getOutputStream(); - // Name and email are optional, so if they are empty, set them to - // Anonymous and Anonymous respectively + // Setups the JSON body String nameString = name.getText().toString(); String emailString = email.getText().toString(); if (nameString.equals("")) nameString = "Anonymous"; if (emailString.equals("")) emailString = "Anonymous"; - String finalNameString = nameString; - String finalEmailString = emailString; - outputStream.write(new JSONObject() {{ - put("event_id", lastEventId); - put("name", finalNameString); - put("email", finalEmailString); - put("comments", description.getText().toString()); - }}.toString().getBytes()); - outputStream.flush(); - outputStream.close(); - // Read the response - InputStream inputStream = connection.getInputStream(); - byte[] buffer = new byte[1024]; - int length; - StringBuilder stringBuilder = new StringBuilder(); - while ((length = inputStream.read(buffer)) != -1) { - stringBuilder.append(new String(buffer, 0, length)); + JSONObject body = new JSONObject(); + body.put("event_id", lastEventId); + body.put("name", nameString); + body.put("email", emailString); + body.put("comments", description.getText().toString()); + // Send the request + connection.setDoOutput(true); + connection.getOutputStream().write(body.toString().getBytes()); + connection.connect(); + // For debug builds, log the response code and response body + if (BuildConfig.DEBUG) { + Log.d("NoodleDebug", "Response Code: " + connection.getResponseCode()); } - inputStream.close(); - connection.disconnect(); - if (BuildConfig.DEBUG) Log.d("Sentry", stringBuilder.toString()); - // Valid responses will be a json object with a key "id" - JSONObject jsonObject = new JSONObject(stringBuilder.toString()); - if (jsonObject.has("id")) { - // Show a toast to the user to confirm that the feedback has been sent + // Check if the request was successful + if (connection.getResponseCode() == 200) { runOnUiThread(() -> Toast.makeText(this, R.string.sentry_dialogue_success, Toast.LENGTH_LONG).show()); } else { - // Show a toast to the user to confirm that the feedback has not been sent - runOnUiThread(() -> Toast.makeText(this, R.string.sentry_dialogue_failed_toast, Toast.LENGTH_LONG).show()); + runOnUiThread(() -> Toast.makeText(this, + R.string.sentry_dialogue_failed_toast, + Toast.LENGTH_LONG).show()); } } catch (IOException | JSONException ignored) { // Show a toast if the user feedback could not be submitted @@ -348,7 +331,8 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe }).start(); }).setNegativeButton(R.string.cancel, (dialog, which) -> { preferences.edit().remove("lastEventId").apply(); - Sentry.captureMessage("User has ignored the crash"); + preferences.edit().putString("lastExitReason", "").apply(); + Log.w(TAG, "User cancelled sentry dialog"); }).show(); } } diff --git a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java index 2fcba7f..05304c7 100644 --- a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java +++ b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java @@ -330,7 +330,7 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { } else { findPreference("pref_crash").setOnPreferenceClickListener(preference -> { // Hard crash the app - throw new RuntimeException("This is a test crash"); + throw new Error("This is a test crash"); }); } if (InstallerInitializer.peekMagiskVersion() < Constants.MAGISK_VER_CODE_INSTALL_COMMAND || !MainApplication.isDeveloper()) { diff --git a/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java b/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java index 90fc3b9..ba6b121 100644 --- a/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java +++ b/app/src/sentry/java/com/fox2code/mmm/sentry/SentryMain.java @@ -1,28 +1,18 @@ package com.fox2code.mmm.sentry; -import static io.sentry.TypeCheckHint.SENTRY_TYPE_CHECK_HINT; - import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import android.net.Uri; -import android.util.Log; -import com.fox2code.mmm.BuildConfig; import com.fox2code.mmm.MainApplication; import com.fox2code.mmm.androidacy.AndroidacyUtil; -import java.io.IOException; -import java.io.Writer; import java.util.Objects; -import io.sentry.JsonObjectWriter; -import io.sentry.NoOpLogger; import io.sentry.Sentry; import io.sentry.android.core.SentryAndroid; import io.sentry.android.fragment.FragmentLifecycleIntegration; -import io.sentry.hints.DiskFlushNotification; -import io.sentry.protocol.SentryId; public class SentryMain { public static final boolean IS_SENTRY_INSTALLED = true; @@ -34,6 +24,16 @@ public class SentryMain { */ @SuppressLint({"RestrictedApi", "UnspecifiedImmutableFlag"}) public static void initialize(final MainApplication mainApplication) { + Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { + SharedPreferences.Editor editor = mainApplication.getSharedPreferences( + "sentry", Context.MODE_PRIVATE).edit(); + editor.putString("lastExitReason", "crash"); + editor.apply(); + // If we just let the default uncaught exception handler handle the + // exception, the app will hang and never close. + // So we need to kill the app ourselves. + System.exit(1); + }); SentryAndroid.init(mainApplication, options -> { // If crash reporting is disabled, stop here. if (!MainApplication.isCrashReportingEnabled()) { @@ -55,54 +55,15 @@ public class SentryMain { // Add a callback that will be used before the event is sent to Sentry. // With this callback, you can modify the event or, when returning null, also discard the event. options.setBeforeSend((event, hint) -> { - if (BuildConfig.DEBUG) { // Debug sentry events for debug. - StringBuilder stringBuilder = new StringBuilder("Sentry report debug: "); - try { - event.serialize(new JsonObjectWriter(new Writer() { - @Override - public void write(char[] cbuf) { - stringBuilder.append(cbuf); - } - - @Override - public void write(String str) { - stringBuilder.append(str); - } - - @Override - public void write(char[] chars, int i, int i1) { - stringBuilder.append(chars, i, i1); - } - - @Override - public void write(String str, int off, int len) { - stringBuilder.append(str, off, len); - } - - @Override - public void flush() { - } - - @Override - public void close() { - } - }, 4), NoOpLogger.getInstance()); - } catch (IOException ignored) { - } - Log.i(TAG, stringBuilder.toString()); - } - if (MainApplication.isCrashReportingEnabled()) { - // Save lastEventId to private shared preferences - SharedPreferences sharedPreferences = mainApplication.getSharedPreferences("sentry", Context.MODE_PRIVATE); - sharedPreferences.edit().putString("lastEventId", - Objects.requireNonNull(event.getEventId()).toString()).apply(); - return event; - } else { - // We need to do this to avoid crash delay on crash when the event is dropped - DiskFlushNotification diskFlushNotification = hint.getAs(SENTRY_TYPE_CHECK_HINT, DiskFlushNotification.class); - if (diskFlushNotification != null) diskFlushNotification.markFlushed(); - return null; - } + // Save lastEventId to private shared preferences + SharedPreferences sharedPreferences = MainApplication.getINSTANCE().getSharedPreferences( + "sentry", + Context.MODE_PRIVATE); + String lastEventId = Objects.requireNonNull(event.getEventId()).toString(); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("lastEventId", lastEventId); + editor.apply(); + return event; }); // Filter breadrcrumb content from crash report. options.setBeforeBreadcrumb((breadcrumb, hint) -> { @@ -114,16 +75,6 @@ public class SentryMain { } return breadcrumb; }); - // On uncaught exception, set the lastEventId in private sentry preferences - Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { - SentryId lastEventId = Sentry.captureException(throwable); - SharedPreferences.Editor editor = mainApplication.getSharedPreferences( - "sentry", Context.MODE_PRIVATE).edit(); - editor.putString("lastExitReason", "crash"); - editor.apply(); - // Kill the app - System.exit(2); - }); } }); }