Skip to content

Commit c42c2a2

Browse files
tweaks to loading
also fix crash reporting Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent f5874a9 commit c42c2a2

7 files changed

Lines changed: 37 additions & 51 deletions

File tree

app/src/main/kotlin/com/fox2code/mmm/CrashHandler.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class CrashHandler : FoxActivity() {
5353
}
5454
// force sentry to send all events
5555
Sentry.flush(2000)
56-
val lastEventId = MainApplication.getSharedPreferences("sentry")?.getString("lastEventId", "")
56+
var lastEventId = intent.getStringExtra("lastEventId")
57+
// if event id is all zeros, set it to "". test this by matching the regex ^0+$ (all zeros)
58+
if (lastEventId?.matches("^0+$".toRegex()) == true) {
59+
lastEventId = ""
60+
}
5761
Timber.d(
5862
"CrashHandler.onCreate: lastEventId=%s, crashReportingEnabled=%s",
5963
lastEventId,
@@ -65,9 +69,11 @@ class CrashHandler : FoxActivity() {
6569
val email = findViewById<EditText>(R.id.feedback_email)
6670
val description = findViewById<EditText>(R.id.feedback_message)
6771
val submit = findViewById<View>(R.id.feedback_submit)
68-
if (lastEventId == "" && crashReportingEnabled) {
72+
if (lastEventId.isNullOrEmpty() && crashReportingEnabled) {
6973
// if lastEventId is null, hide the feedback button
7074
Timber.d("CrashHandler.onCreate: lastEventId is null but crash reporting is enabled. This may indicate a bug in the crash reporting system.")
75+
submit.visibility = View.GONE
76+
findViewById<MaterialTextView>(R.id.feedback_text).setText(R.string.no_sentry_id)
7177
} else {
7278
// if lastEventId is not null, enable the feedback name, email, message, and submit button
7379
email.isEnabled = true
@@ -135,7 +141,7 @@ class CrashHandler : FoxActivity() {
135141
finish()
136142
startActivity(packageManager.getLaunchIntentForPackage(packageName))
137143
}
138-
} else {
144+
} else if (!crashReportingEnabled) {
139145
// set feedback_text to "Crash reporting is disabled"
140146
(findViewById<View>(R.id.feedback_text) as MaterialTextView).setText(R.string.sentry_enable_nag)
141147
submit.setOnClickListener { _: View? ->

app/src/main/kotlin/com/fox2code/mmm/MainApplication.kt

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import java.text.SimpleDateFormat
5454
import java.util.Date
5555
import java.util.Random
5656
import kotlin.math.abs
57-
import kotlin.math.max
5857

5958
@Suppress("unused", "MemberVisibilityCanBePrivate")
6059
class MainApplication : FoxApplication(), Configuration.Provider {
@@ -129,7 +128,7 @@ class MainApplication : FoxApplication(), Configuration.Provider {
129128
markwonThemeContext = FoxThemeWrapper(this, managerThemeResId)
130129
contextThemeWrapper = markwonThemeContext
131130
}
132-
val markwon =
131+
this.markwon =
133132
Markwon.builder(contextThemeWrapper!!).usePlugin(HtmlPlugin.create()).usePlugin(
134133
ImagesPlugin.create().addSchemeHandler(
135134
OkHttpNetworkSchemeHandler.create(
@@ -337,6 +336,7 @@ class MainApplication : FoxApplication(), Configuration.Provider {
337336
Timber.w("ANDROIDACY_CLIENT_ID is empty, disabling AndroidacyRepoData 1")
338337
editor.apply()
339338
}
339+
getMarkwon()
340340
}
341341

342342
private val intent: Intent?
@@ -538,44 +538,12 @@ class MainApplication : FoxApplication(), Configuration.Provider {
538538
val mContext: Context? = INSTANCE
539539
name += "x"
540540
if (mSharedPrefs == null) {
541-
Timber.d("Creating shared prefs map")
542541
mSharedPrefs = HashMap()
543542
}
544-
/*
545-
this part is only here because with added encryption, parts of code that were previously calling this over and over again or on each invocation of a method are causing performance issues.
546-
*/
547-
if (BuildConfig.DEBUG) {
548-
// get file, function, and line number
549-
val stackTrace = Thread.currentThread().stackTrace
550-
// get the caller of this method
551-
val caller = stackTrace[3]
552-
Timber.d(
553-
"Shared prefs file: %s, caller: %s:%d in %s",
554-
name,
555-
caller.fileName,
556-
caller.lineNumber,
557-
caller.methodName
558-
)
559-
// add the caller to an array. if the last 3 callers are the same, then we are in a loop, log at error level
560-
callers.add(name + ":" + caller.lineNumber + ":" + caller.methodName)
561-
// get the last 3 callers
562-
val last3: List<String> = callers.subList(max(callers.size - 3, 0), callers.size)
563-
// if the last 3 callers are the same, then we are in a loop, log at error level
564-
if (((last3.size == 3) && last3[0] == last3[1]) && last3[1] == last3[2]) {
565-
Timber.e(
566-
"Shared prefs loop detected. File: %s, caller: %s:%d",
567-
name,
568-
caller.methodName,
569-
caller.lineNumber
570-
)
571-
}
572-
}
573543
if (mSharedPrefs!!.containsKey(name)) {
574-
Timber.d("Returning cached shared prefs")
575544
return mSharedPrefs!![name] as SharedPreferences?
576545
}
577546
return try {
578-
Timber.d("Creating encrypted shared prefs")
579547
val masterKey =
580548
MasterKey.Builder(mContext!!).setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
581549
.build()
@@ -594,6 +562,10 @@ class MainApplication : FoxApplication(), Configuration.Provider {
594562
}
595563
}
596564

565+
fun clearCachedSharedPrefs() {
566+
mSharedPrefs = null
567+
}
568+
597569
fun checkSecret(intent: Intent?): Boolean {
598570
return intent != null && intent.getLongExtra("secret", secret.inv()) == secret
599571
}

app/src/main/kotlin/com/fox2code/mmm/markdown/MarkdownActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class MarkdownActivity : FoxActivity() {
108108
Timber.i("Done!")
109109
runOnUiThread {
110110
footer?.minimumHeight = this.navigationBarHeight
111-
MainApplication.INSTANCE!!.markwon!!.setMarkdown(
111+
MainApplication.INSTANCE!!.markwon?.setMarkdown(
112112
textView,
113113
MarkdownUrlLinker.urlLinkify(markdown)
114114
)

app/src/main/kotlin/com/fox2code/mmm/repo/RepoManager.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ class RepoManager private constructor(mainApplication: MainApplication) : SyncMa
207207
Timber.e(e)
208208
}
209209
updatedModules++
210-
updateListener.update(STEP1 + STEP2 / (if (moduleToUpdate != 0) moduleToUpdate else 1) * updatedModules)
210+
// update the update listener with step1 + computed step 2 (which is updated modules / total modules / total repos)
211+
updateListener.update(
212+
STEP1 + (STEP2 / (moduleToUpdate / updatedModules) / repoDatas.size)
213+
)
211214
}
212215
for (repoModule in repoUpdaters[i]!!.toApply()!!) {
213216
if (repoModule.moduleInfo.flags and ModuleInfo.FLAG_METADATA_INVALID == 0) {
@@ -268,11 +271,11 @@ class RepoManager private constructor(mainApplication: MainApplication) : SyncMa
268271
}
269272
repoLastErrorName = repoUpdaters[i]!!.repoData.name
270273
}
271-
updateListener.update(STEP1 + STEP2 + STEP3 / repoDatas.size * (i + 1))
274+
updateListener.update(STEP1 + (STEP2 * (i / repoUpdaters.size)))
272275
}
273276
}
274277
Timber.i("Got " + modules.size + " modules!")
275-
updateListener.update(1.0)
278+
updateListener.update(STEP1 + STEP2 + STEP3)
276279
}
277280

278281
fun updateEnabledStates() {

app/src/main/kotlin/com/fox2code/mmm/utils/io/net/Http.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.fox2code.mmm.installer.InstallerInitializer.Companion.peekMagiskPath
2929
import com.fox2code.mmm.installer.InstallerInitializer.Companion.peekMagiskVersion
3030
import com.fox2code.mmm.utils.io.Files.Companion.makeBuffer
3131
import com.google.net.cronet.okhttptransport.CronetInterceptor
32+
import io.sentry.android.okhttp.SentryOkHttpInterceptor
3233
import okhttp3.Cache
3334
import okhttp3.Dns
3435
import okhttp3.HttpUrl.*
@@ -351,6 +352,9 @@ enum class Http {;
351352
httpclientBuilder.addInterceptor(loggingInterceptor)
352353
}
353354

355+
// add sentry interceptor
356+
httpclientBuilder.addInterceptor(SentryOkHttpInterceptor())
357+
354358
// Add cronet interceptor
355359
// init cronet
356360
try {

app/src/main/kotlin/com/fox2code/mmm/utils/sentry/SentryMain.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package com.fox2code.mmm.utils.sentry
66

77
import android.annotation.SuppressLint
8-
import android.content.Context
98
import android.content.Intent
109
import android.content.SharedPreferences
1110
import android.net.Uri
@@ -25,13 +24,15 @@ import io.sentry.android.core.SentryAndroid
2524
import io.sentry.android.core.SentryAndroidOptions
2625
import io.sentry.android.fragment.FragmentLifecycleIntegration
2726
import io.sentry.android.timber.SentryTimberIntegration
27+
import io.sentry.protocol.SentryId
2828
import org.matomo.sdk.extra.TrackHelper
2929
import timber.log.Timber
3030

3131
object SentryMain {
3232
const val IS_SENTRY_INSTALLED = true
3333
private var isCrashing = false
3434
private var isSentryEnabled = false
35+
private var crashExceptionId: SentryId? = null
3536

3637
/**
3738
* Initialize Sentry
@@ -42,6 +43,7 @@ object SentryMain {
4243
fun initialize(mainApplication: MainApplication) {
4344
Thread.setDefaultUncaughtExceptionHandler { _: Thread?, throwable: Throwable ->
4445
isCrashing = true
46+
MainApplication.clearCachedSharedPrefs()
4547
TrackHelper.track().exception(throwable).with(MainApplication.INSTANCE!!.tracker)
4648
// open crash handler and exit
4749
val intent = Intent(mainApplication, CrashHandler::class.java)
@@ -55,6 +57,12 @@ object SentryMain {
5557
intent.putExtra("crashReportingEnabled", isSentryEnabled)
5658
// add isCrashing to intent
5759
intent.putExtra("isCrashing", isCrashing)
60+
// add crashExceptionId to intent
61+
if (crashExceptionId != null) {
62+
intent.putExtra("lastEventId", crashExceptionId!!.toString())
63+
} else {
64+
intent.putExtra("lastEventId", "")
65+
}
5866
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
5967
Timber.e("Starting crash handler")
6068
mainApplication.startActivity(intent)
@@ -119,15 +127,7 @@ object SentryMain {
119127
if (!isSentryEnabled) {
120128
return@BeforeSendCallback null
121129
}
122-
// store eventid in prefs
123-
val editor =
124-
MainApplication.INSTANCE!!.getSharedPreferences(
125-
"sentry",
126-
Context.MODE_PRIVATE
127-
)
128-
.edit()
129-
editor.putString("lastEventId", event!!.eventId.toString())
130-
editor.commit() // commit so we immediately cache if crashing
130+
crashExceptionId = event?.eventId
131131
event
132132
}
133133
// Filter breadcrumb content from crash report.

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,5 @@
387387
<string name="error_opening_notes">Error in opening module notes. Logs may have the reason.</string>
388388
<string name="submit_feedback">Submit feedback</string>
389389
<string name="ssl_error">Potential SSL interception detected.</string>
390+
<string name="no_sentry_id">Unable to sumbit feedback - no event ID from Sentry. The last event may not have sent.</string>
390391
</resources>

0 commit comments

Comments
 (0)