Skip to content

Commit

Permalink
refactor: 使用Compose重构CrashActivity
Browse files Browse the repository at this point in the history
Change-Id: Ie3686eb553701df55a719d73b0e3ed6917cfb96e
  • Loading branch information
XayahSuSuSu committed Feb 7, 2023
1 parent fc746fa commit 4a664ab
Show file tree
Hide file tree
Showing 31 changed files with 203 additions and 326 deletions.
3 changes: 0 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ dependencies {
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

// Crash
implementation project(":crash")

// LibSU
def libsuVersion = '5.0.4'
implementation "com.github.topjohnwu.libsu:core:${libsuVersion}"
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
android:exported="false" />
<activity
android:name=".compose.ui.activity.processing.ProcessingActivity"
android:exported="false"
android:theme="@style/Theme.DataBackup" />
android:exported="false" />
<activity
android:name=".compose.ui.activity.crash.CrashActivity"
android:exported="false" />
<activity
android:name=".activity.list.AppListBackupActivity"
android:exported="false" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/xayah/databackup/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.app.Application
import android.content.Context
import com.google.android.material.color.DynamicColors
import com.topjohnwu.superuser.Shell
import com.xayah.crash.CrashHandler
import com.xayah.databackup.compose.ui.activity.crash.CrashHandler
import com.xayah.databackup.util.*
import java.io.InputStream

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.xayah.databackup.compose.ui.activity.crash

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.core.view.WindowCompat
import com.xayah.databackup.R
import com.xayah.databackup.compose.ui.activity.crash.components.CrashScaffold
import com.xayah.databackup.compose.ui.theme.DataBackupTheme
import com.xayah.databackup.util.GsonUtil
import com.xayah.materialyoufileexplorer.MaterialYouFileExplorer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.*

@ExperimentalMaterial3Api
class CrashActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)

val materialYouFileExplorer = MaterialYouFileExplorer().apply {
initialize(this@CrashActivity)
}

val logs = intent.getStringExtra("crashInfo") ?: "\n"
setContent {
DataBackupTheme {
CrashScaffold(logs) {
materialYouFileExplorer.apply {
isFile = false
toExplorer(this@CrashActivity) { path, _ ->
CoroutineScope(Dispatchers.IO).launch {
try {
val date = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss",
Locale.CHINA
).format(Date())
val name =
"Crash-${date.replace(" ", "-").replace(":", "-")}.txt"
GsonUtil.saveToFile(
"${path}/${name}",
logs
)
withContext(Dispatchers.Main) {
Toast.makeText(
this@CrashActivity,
getString(R.string.success),
Toast.LENGTH_SHORT
).show()
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
Toast.makeText(
this@CrashActivity,
getString(R.string.failed),
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.xayah.crash
package com.xayah.databackup.compose.ui.activity.crash

import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.os.Build
import androidx.compose.material3.ExperimentalMaterial3Api
import java.io.PrintWriter
import java.io.StringWriter
import java.io.Writer
Expand All @@ -21,7 +23,7 @@ class CrashHandler(private val mContext: Context) : Thread.UncaughtExceptionHand
try {
val that = this
mContext.applicationInfo.apply {
if (flags and ApplicationInfo.FLAG_DEBUGGABLE == 0) {
if (flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
// 获取系统默认的UncaughtException处理
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler()
// 设置该CrashHandler为程序的默认处理
Expand All @@ -36,6 +38,7 @@ class CrashHandler(private val mContext: Context) : Thread.UncaughtExceptionHand
/**
* 异常捕获
*/
@OptIn(ExperimentalMaterial3Api::class)
override fun uncaughtException(thread: Thread, throwable: Throwable) {
if (!handleException(throwable) && mDefaultHandler != null) {
// 使用系统默认的异常处理器处理
Expand Down Expand Up @@ -79,10 +82,18 @@ class CrashHandler(private val mContext: Context) : Thread.UncaughtExceptionHand
val errorMessage = writer.toString()
val stringBuilder = StringBuilder()
try {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
val date: String = simpleDateFormat.format(Date())
val date =
"Date: ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(Date())}\n"
val model = "Model: ${Build.MODEL}\n"
val abi = "ABIs: ${Build.SUPPORTED_ABIS.joinToString(separator = ", ")}\n"
val sdk = "SDK: ${Build.VERSION.SDK_INT}\n"
stringBuilder.apply {
append(date).append("\n")
append("================================\n")
append(date)
append(model)
append(abi)
append(sdk)
append("================================\n")
append(errorMessage)
}
crashInfo = stringBuilder.toString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.xayah.databackup.compose.ui.activity.crash.components

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Warning
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import com.xayah.databackup.R

@ExperimentalMaterial3Api
@Composable
fun CrashScaffold(crashInfo: String, onSaveClick: () -> Unit) {
val colorOnSurfaceVariant = MaterialTheme.colorScheme.onSurfaceVariant
val nonePadding = dimensionResource(R.dimen.padding_none)
val smallPadding = dimensionResource(R.dimen.padding_small)
val mediumPadding = dimensionResource(R.dimen.padding_medium)
val hugePadding = dimensionResource(R.dimen.padding_huge)
val iconMediumSize = dimensionResource(R.dimen.icon_medium_size)
Scaffold(
floatingActionButton = {
FloatingActionButton(
onClick = onSaveClick,
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_round_save),
contentDescription = null
)
}
},
floatingActionButtonPosition = FabPosition.Center
) { innerPadding ->
LazyColumn(
modifier = Modifier.padding(mediumPadding, nonePadding),
verticalArrangement = Arrangement.spacedBy(mediumPadding),
) {
item {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(
modifier = Modifier
.fillMaxWidth()
.height(
innerPadding.calculateTopPadding() * 2
)
)
Icon(
imageVector = Icons.Rounded.Warning,
contentDescription = null,
tint = colorOnSurfaceVariant,
modifier = Modifier
.size(iconMediumSize)
.padding(nonePadding, nonePadding, nonePadding, smallPadding)
)
Text(
text = stringResource(R.string.app_crashed),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
}
}
item {
Text(
text = crashInfo,
style = MaterialTheme.typography.labelSmall
)
}
item {
Spacer(
modifier = Modifier
.fillMaxWidth()
.height(
innerPadding.calculateBottomPadding() + hugePadding
)
)
}
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_round_save.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M5,21Q4.175,21 3.587,20.413Q3,19.825 3,19V5Q3,4.175 3.587,3.587Q4.175,3 5,3H16.175Q16.575,3 16.938,3.15Q17.3,3.3 17.575,3.575L20.425,6.425Q20.7,6.7 20.85,7.062Q21,7.425 21,7.825V19Q21,19.825 20.413,20.413Q19.825,21 19,21ZM19,7.85 L16.15,5H5Q5,5 5,5Q5,5 5,5V19Q5,19 5,19Q5,19 5,19H19Q19,19 19,19Q19,19 19,19ZM12,18Q13.25,18 14.125,17.125Q15,16.25 15,15Q15,13.75 14.125,12.875Q13.25,12 12,12Q10.75,12 9.875,12.875Q9,13.75 9,15Q9,16.25 9.875,17.125Q10.75,18 12,18ZM7,10H14Q14.425,10 14.713,9.712Q15,9.425 15,9V7Q15,6.575 14.713,6.287Q14.425,6 14,6H7Q6.575,6 6.287,6.287Q6,6.575 6,7V9Q6,9.425 6.287,9.712Q6.575,10 7,10ZM5,7.85V19Q5,19 5,19Q5,19 5,19Q5,19 5,19Q5,19 5,19V5Q5,5 5,5Q5,5 5,5Z" />
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@
<string name="restoring">Restoring</string>
<string name="read_icon">Read icon</string>
<string name="read_icon_title">Icon reading requires a lot of memory, if you experience crashes during restoring, try turning this option off</string>
<string name="app_crashed">App crashed</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@
<string name="restoring">Restoring</string>
<string name="read_icon">Read icon</string>
<string name="read_icon_title">Icon reading requires a lot of memory, if you experience crashes during restoring, try turning this option off</string>
<string name="app_crashed">App crashed</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@
<string name="restoring">恢复中</string>
<string name="read_icon">读取图标</string>
<string name="read_icon_title">图标读取需要大量的内存,如果您在恢复过程中遇到崩溃问题,请尝试关闭此选项</string>
<string name="app_crashed">应用发生崩溃</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@
<string name="restoring">恢復中</string>
<string name="read_icon">讀取圖標</string>
<string name="read_icon_title">圖標讀取需要大量的內存,如果您在恢復過程中遇到崩潰問題,請嘗試關閉此選項</string>
<string name="app_crashed">應用發生崩潰</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@
<string name="restoring">恢復中</string>
<string name="read_icon">讀取圖示</string>
<string name="read_icon_title">圖示讀取需要大量的記憶體,如果您在恢復過程中遇到崩潰問題,請嘗試關閉此選項</string>
<string name="app_crashed">應用發生崩潰</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<string name="restoring">Restoring</string>
<string name="read_icon">Read icon</string>
<string name="read_icon_title">Icon reading requires a lot of memory, if you experience crashes during restoring, try turning this option off</string>
<string name="app_crashed">App crashed</string>
<string-array name="rclone_config_type_array_key" translatable="false">
<item>FTP</item>
<item>WebDAV</item>
Expand Down
2 changes: 0 additions & 2 deletions crash/.gitignore

This file was deleted.

54 changes: 0 additions & 54 deletions crash/build.gradle

This file was deleted.

Empty file removed crash/consumer-rules.pro
Empty file.
21 changes: 0 additions & 21 deletions crash/proguard-rules.pro

This file was deleted.

Loading

0 comments on commit 4a664ab

Please sign in to comment.