[stacktrace] Save stacktrace when crash

This commit is contained in:
kyori19 2019-01-16 23:23:35 +09:00
parent d17fd8a03b
commit 4619f14cb1
5 changed files with 86 additions and 1 deletions

View File

@ -20,8 +20,8 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.util.NotificationHelper
import net.accelf.yuito.CustomUncaughtExceptionHandler
import javax.inject.Inject
class SplashActivity : AppCompatActivity(), Injectable {
@ -32,6 +32,9 @@ class SplashActivity : AppCompatActivity(), Injectable {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val customUncaughtExceptionHandler = CustomUncaughtExceptionHandler(applicationContext)
Thread.setDefaultUncaughtExceptionHandler(customUncaughtExceptionHandler)
/** delete old notification channels */
NotificationHelper.deleteLegacyNotificationChannels(this, accountManager)

View File

@ -18,6 +18,7 @@ package com.keylesspalace.tusky.fragment.preference
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.keylesspalace.tusky.ComposeActivity
import com.keylesspalace.tusky.PreferencesActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.util.ThemeUtils
@ -73,6 +74,8 @@ class PreferencesFragment : PreferenceFragmentCompat() {
val botDrawable = botIndicatorPreference.context.getDrawable(R.drawable.ic_bot_24dp)
ThemeUtils.setDrawableTint(context, botDrawable, R.attr.toolbar_icon_tint)
botIndicatorPreference.icon = botDrawable
updateStackTracePreference()
}
override fun onResume() {
@ -105,6 +108,36 @@ class PreferencesFragment : PreferenceFragmentCompat() {
}
private fun updateStackTracePreference() {
val stackTraceCategory = requirePreference("stackTraceCategory")
val sharedPreferences = preferenceManager.sharedPreferences
val stackTrace = sharedPreferences.getString("stack_trace", null)
if (stackTrace.isNullOrEmpty()) {
preferenceScreen.removePreference(stackTraceCategory)
} else {
val sendCrashReportPreference = requirePreference("sendCrashReport")
sendCrashReportPreference.setOnPreferenceClickListener {
activity?.let { activity ->
val intent = ComposeActivity.IntentBuilder()
.tootText("@ars42525@odakyu.app $stackTrace".substring(0, 400))
.contentWarning("Yuito StackTrace")
.build(activity)
activity.startActivity(intent)
sharedPreferences.edit()
.remove("stack_trace")
.apply()
}
true
}
val stackTracePreference = requirePreference("stackTrace")
stackTracePreference.summary = stackTrace
}
}
companion object {
fun newInstance(): PreferencesFragment {
return PreferencesFragment()

View File

@ -0,0 +1,34 @@
package net.accelf.yuito;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.io.PrintWriter;
import java.io.StringWriter;
public class CustomUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Context context;
private Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;
public CustomUncaughtExceptionHandler(Context context) {
this.context = context;
mDefaultUncaughtExceptionHandler = Thread
.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable e) {
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String stackTrace = stringWriter.toString();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.edit().putString("stack_trace", stackTrace).apply();
mDefaultUncaughtExceptionHandler.uncaughtException(thread, e);
}
}

View File

@ -224,6 +224,8 @@
<string name="pref_title_experimental">Experimental Settings</string>
<string name="pref_title_experimental_viewpager_offscreen">Increase offscreenPageLimit of ViewPager</string>
<string name="pref_title_experimental_htl_streaming">Use home timeline streaming</string>
<string name="pref_title_stacktrace">Stacktrace</string>
<string name="pref_title_stacktrace_send">Send crash report to developer</string>
<string name="app_them_dark">Dark</string>
<string name="app_theme_light">Light</string>

View File

@ -125,4 +125,17 @@
android:key="useHTLStream"
android:title="@string/pref_title_experimental_htl_streaming" />
</PreferenceCategory>
<PreferenceCategory
android:key="stackTraceCategory"
android:title="@string/pref_title_stacktrace">
<Preference
android:key="sendCrashReport"
android:title="@string/pref_title_stacktrace_send" />
<Preference
android:key="stackTrace"
android:selectable="false" />
</PreferenceCategory>
</androidx.preference.PreferenceScreen>