From 8af585e12e4de4d20eb6649abf072fa542b2b7c7 Mon Sep 17 00:00:00 2001 From: androidacy-user Date: Fri, 30 Jun 2023 21:49:45 -0400 Subject: [PATCH] fix a crash or two can't catch em all Signed-off-by: androidacy-user --- app/build.gradle.kts | 9 +- .../kotlin/com/fox2code/mmm/CrashHandler.kt | 40 ++++---- .../mmm/installer/InstallerActivity.kt | 5 +- .../fox2code/mmm/utils/sentry/SentryMain.kt | 30 +++--- .../res/layout/activity_crash_handler.xml | 91 ++++++++++--------- app/src/main/res/values/strings.xml | 1 + 6 files changed, 83 insertions(+), 93 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 75afeb4..30c64fd 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -473,14 +473,7 @@ dependencies { // Chromium cronet from androidacy implementation("org.chromium.net:cronet-embedded:113.5672.61") - // protobuf - fixes a crash on some devices - // implementation("com.google.protobuf:protobuf-javalite:3.22.2") - - // google guava, maybe fix a bug - implementation("com.google.guava:guava:32.0.1-jre") - - - val libsuVersion = "5.0.5" + val libsuVersion = "5.1.0" // The core module that provides APIs to a shell implementation("com.github.topjohnwu.libsu:core:${libsuVersion}") diff --git a/app/src/main/kotlin/com/fox2code/mmm/CrashHandler.kt b/app/src/main/kotlin/com/fox2code/mmm/CrashHandler.kt index 99efc43..4e4596d 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/CrashHandler.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/CrashHandler.kt @@ -51,26 +51,32 @@ class CrashHandler : FoxActivity() { stacktrace = stacktrace.replace(",", "\n ") crashDetails.text = getString(R.string.crash_full_stacktrace, stacktrace) } - val lastEventId = intent.getStringExtra("lastEventId") + // force sentry to send all events + Sentry.flush(2000) + val lastEventId = MainApplication.getSharedPreferences("sentry")?.getString("lastEventId", "") Timber.d( "CrashHandler.onCreate: lastEventId=%s, crashReportingEnabled=%s", lastEventId, crashReportingEnabled ) - if (lastEventId == null && crashReportingEnabled) { + + // get name, email, and message fields + val name = findViewById(R.id.feedback_name) + val email = findViewById(R.id.feedback_email) + val description = findViewById(R.id.feedback_message) + val submit = findViewById(R.id.feedback_submit) + if (lastEventId == "" && crashReportingEnabled) { // if lastEventId is null, hide the feedback button - findViewById(R.id.feedback).visibility = View.GONE Timber.d("CrashHandler.onCreate: lastEventId is null but crash reporting is enabled. This may indicate a bug in the crash reporting system.") } else { - // if lastEventId is not null, show the feedback button - findViewById(R.id.feedback).visibility = View.VISIBLE + // if lastEventId is not null, enable the feedback name, email, message, and submit button + email.isEnabled = true + name.isEnabled = true + description.isEnabled = true + submit.isEnabled = true } // disable feedback if sentry is disabled if (crashReportingEnabled && lastEventId != null) { - // get name, email, and message fields - val name = findViewById(R.id.feedback_name) - val email = findViewById(R.id.feedback_email) - val description = findViewById(R.id.feedback_message) // get submit button findViewById(R.id.feedback_submit).setOnClickListener { _: View? -> // require the feedback_message, rest is optional @@ -84,12 +90,10 @@ class CrashHandler : FoxActivity() { arrayOf(if (name.text.toString() == "") "Anonymous" else name.text.toString()) val emailString = arrayOf(if (email.text.toString() == "") "Anonymous" else email.text.toString()) - // get sentryException passed in intent - @Suppress("NAME_SHADOWING") val lastEventId = Sentry.getLastEventId() Thread { try { val userFeedback = - UserFeedback(SentryId(lastEventId.toString())) + UserFeedback(SentryId(lastEventId)) // Setups the JSON body if (nameString[0] == "") nameString[0] = "Anonymous" if (emailString[0] == "") emailString[0] = "Anonymous" @@ -132,19 +136,9 @@ class CrashHandler : FoxActivity() { startActivity(packageManager.getLaunchIntentForPackage(packageName)) } } else { - // disable feedback if sentry is disabled - findViewById(R.id.feedback_name).isEnabled = false - findViewById(R.id.feedback_email).isEnabled = false - findViewById(R.id.feedback_message).isEnabled = false - // fade out all the fields - findViewById(R.id.feedback_name).alpha = 0.5f - findViewById(R.id.feedback_email).alpha = 0.5f - findViewById(R.id.feedback_message).alpha = 0.5f - // fade out the submit button - findViewById(R.id.feedback_submit).alpha = 0.5f // set feedback_text to "Crash reporting is disabled" (findViewById(R.id.feedback_text) as MaterialTextView).setText(R.string.sentry_enable_nag) - findViewById(R.id.feedback_submit).setOnClickListener { _: View? -> + submit.setOnClickListener { _: View? -> Toast.makeText( this, R.string.sentry_dialogue_disabled, Toast.LENGTH_LONG ).show() diff --git a/app/src/main/kotlin/com/fox2code/mmm/installer/InstallerActivity.kt b/app/src/main/kotlin/com/fox2code/mmm/installer/InstallerActivity.kt index 5d9e37b..760f0e0 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/installer/InstallerActivity.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/installer/InstallerActivity.kt @@ -52,7 +52,6 @@ import com.fox2code.mmm.utils.io.net.Http import com.fox2code.mmm.utils.sentry.SentryBreadcrumb import com.fox2code.mmm.utils.sentry.SentryMain import com.google.android.material.bottomnavigation.BottomNavigationItemView -import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.progressindicator.LinearProgressIndicator import com.topjohnwu.superuser.CallbackList @@ -206,7 +205,7 @@ class InstallerActivity : FoxActivity() { ) var errMessage = "Failed to download module zip" // Set this to the error message if it's a HTTP error - var rawModule: ByteArray? = ByteArray(0) + var rawModule: ByteArray? try { Timber.i( "%s%s", if (urlMode) "Downloading: " else "Loading: ", AndroidacyUtil.hideToken( @@ -239,7 +238,7 @@ class InstallerActivity : FoxActivity() { } } if (canceled) return@Runnable - fixJavaZipHax(rawModule!!) + fixJavaZipHax(rawModule) // checks to make sure zip is not a source archive, and if it is, unzips the folder within, switches to it, and zips up the contents of it fixSourceArchiveShit(rawModule) var noPatch = false diff --git a/app/src/main/kotlin/com/fox2code/mmm/utils/sentry/SentryMain.kt b/app/src/main/kotlin/com/fox2code/mmm/utils/sentry/SentryMain.kt index ecd9319..b80e0f0 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/utils/sentry/SentryMain.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/utils/sentry/SentryMain.kt @@ -10,6 +10,7 @@ import android.content.Intent import android.content.SharedPreferences import android.net.Uri import android.os.Process +import com.fox2code.mmm.BuildConfig import com.fox2code.mmm.CrashHandler import com.fox2code.mmm.MainApplication import com.fox2code.mmm.androidacy.AndroidacyUtil.Companion.hideToken @@ -37,32 +38,17 @@ object SentryMain { * Sentry is used for crash reporting and performance monitoring. */ @JvmStatic - @SuppressLint("RestrictedApi", "UnspecifiedImmutableFlag") + @SuppressLint("RestrictedApi", "UnspecifiedImmutableFlag", "ApplySharedPref") fun initialize(mainApplication: MainApplication) { Thread.setDefaultUncaughtExceptionHandler { _: Thread?, throwable: Throwable -> isCrashing = true TrackHelper.track().exception(throwable).with(MainApplication.INSTANCE!!.tracker) - val editor = - MainApplication.INSTANCE!!.getSharedPreferences("sentry", Context.MODE_PRIVATE) - .edit() - editor.putString("lastExitReason", "crash") - editor.putLong("lastExitTime", System.currentTimeMillis()) - editor.putString("lastExitReason", "crash") - editor.putString("lastExitId", Sentry.getLastEventId().toString()) - editor.apply() - Timber.e( - "Uncaught exception with sentry ID %s and stacktrace %s", - Sentry.getLastEventId(), - throwable.stackTrace - ) // open crash handler and exit val intent = Intent(mainApplication, CrashHandler::class.java) // pass the entire exception to the crash handler intent.putExtra("exception", throwable) // add stacktrace as string intent.putExtra("stacktrace", throwable.stackTrace) - // put lastEventId in intent (get from preferences) - intent.putExtra("lastEventId", Sentry.getLastEventId().toString()) // serialize Sentry.captureException and pass it to the crash handler intent.putExtra("sentryException", throwable) // pass crashReportingEnabled to crash handler @@ -127,11 +113,21 @@ object SentryMain { options.isEnableUncaughtExceptionHandler = true // 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.environment = BuildConfig.BUILD_TYPE options.beforeSend = BeforeSendCallback { event: SentryEvent?, _: Hint? -> // in the rare event that crash reporting has been disabled since we started the app, we don't want to send the crash report - if (!isSentryEnabled || isCrashing) { + if (!isSentryEnabled) { return@BeforeSendCallback null } + // store eventid in prefs + val editor = + MainApplication.INSTANCE!!.getSharedPreferences( + "sentry", + Context.MODE_PRIVATE + ) + .edit() + editor.putString("lastEventId", event!!.eventId.toString()) + editor.commit() // commit so we immediately cache if crashing event } // Filter breadcrumb content from crash report. diff --git a/app/src/main/res/layout/activity_crash_handler.xml b/app/src/main/res/layout/activity_crash_handler.xml index 79a513e..5fe8339 100644 --- a/app/src/main/res/layout/activity_crash_handler.xml +++ b/app/src/main/res/layout/activity_crash_handler.xml @@ -107,6 +107,7 @@ @@ -125,57 +127,62 @@ - - + + + + + + + - - - - - - - - - - + + + + + + + + + + + + + - - - - - - + android:layout_margin="10dp" + android:text="@string/reset_app" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f58a26b..592e8c0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -385,4 +385,5 @@ Download latest Saved logs successfully Error in opening module notes. Logs may have the reason. + Submit feedback