diff --git a/app/src/main/java/com/fankes/tsbattery/utils/factory/DialogBuilderFactory.kt b/app/src/main/java/com/fankes/tsbattery/utils/factory/DialogBuilderFactory.kt
index 32b103b..25295f1 100644
--- a/app/src/main/java/com/fankes/tsbattery/utils/factory/DialogBuilderFactory.kt
+++ b/app/src/main/java/com/fankes/tsbattery/utils/factory/DialogBuilderFactory.kt
@@ -72,8 +72,8 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
init {
if (isUsingAndroidX)
- runCatching { instanceAndroidX = MaterialAlertDialogBuilder(context) }
- else runCatching {
+ runInSafe { instanceAndroidX = MaterialAlertDialogBuilder(context) }
+ else runInSafe {
instanceAndroid = android.app.AlertDialog.Builder(
context,
if (isUseBlackTheme) android.R.style.Theme_Material_Dialog else android.R.style.Theme_Material_Light_Dialog
@@ -84,8 +84,8 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
/** 设置对话框不可关闭 */
fun noCancelable() {
if (isUsingAndroidX)
- runCatching { instanceAndroidX?.setCancelable(false) }
- else runCatching { instanceAndroid?.setCancelable(false) }
+ runInSafe { instanceAndroidX?.setCancelable(false) }
+ else runInSafe { instanceAndroid?.setCancelable(false) }
}
/** 设置对话框标题 */
@@ -93,8 +93,8 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
get() = ""
set(value) {
if (isUsingAndroidX)
- runCatching { instanceAndroidX?.setTitle(value) }
- else runCatching { instanceAndroid?.setTitle(value) }
+ runInSafe { instanceAndroidX?.setTitle(value) }
+ else runInSafe { instanceAndroid?.setTitle(value) }
}
/** 设置对话框消息内容 */
@@ -102,8 +102,8 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
get() = ""
set(value) {
if (isUsingAndroidX)
- runCatching { instanceAndroidX?.setMessage(value) }
- else runCatching { instanceAndroid?.setMessage(value) }
+ runInSafe { instanceAndroidX?.setMessage(value) }
+ else runInSafe { instanceAndroid?.setMessage(value) }
}
/** 设置进度条对话框消息内容 */
@@ -144,8 +144,8 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
*/
fun confirmButton(text: String = "确定", it: () -> Unit = {}) {
if (isUsingAndroidX)
- runCatching { instanceAndroidX?.setPositiveButton(text) { _, _ -> it() } }
- else runCatching { instanceAndroid?.setPositiveButton(text) { _, _ -> it() } }
+ runInSafe { instanceAndroidX?.setPositiveButton(text) { _, _ -> it() } }
+ else runInSafe { instanceAndroid?.setPositiveButton(text) { _, _ -> it() } }
}
/**
@@ -155,8 +155,8 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
*/
fun cancelButton(text: String = "取消", it: () -> Unit = {}) {
if (isUsingAndroidX)
- runCatching { instanceAndroidX?.setNegativeButton(text) { _, _ -> it() } }
- else runCatching { instanceAndroid?.setNegativeButton(text) { _, _ -> it() } }
+ runInSafe { instanceAndroidX?.setNegativeButton(text) { _, _ -> it() } }
+ else runInSafe { instanceAndroid?.setNegativeButton(text) { _, _ -> it() } }
}
/**
@@ -166,21 +166,21 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
*/
fun neutralButton(text: String = "更多", it: () -> Unit = {}) {
if (isUsingAndroidX)
- runCatching { instanceAndroidX?.setNeutralButton(text) { _, _ -> it() } }
- else runCatching { instanceAndroid?.setNeutralButton(text) { _, _ -> it() } }
+ runInSafe { instanceAndroidX?.setNeutralButton(text) { _, _ -> it() } }
+ else runInSafe { instanceAndroid?.setNeutralButton(text) { _, _ -> it() } }
}
/** 取消对话框 */
fun cancel() = dialogInstance?.cancel()
/** 显示对话框 */
- internal fun show() {
- if (isUsingAndroidX) runCatching {
+ internal fun show() =
+ if (isUsingAndroidX) runInSafe {
instanceAndroidX?.create()?.apply {
customLayoutView?.let { setView(it) }
dialogInstance = this
}?.show()
- } else runCatching {
+ } else runInSafe {
instanceAndroid?.create()?.apply {
customLayoutView?.let { setView(it) }
window?.setBackgroundDrawable(
@@ -196,5 +196,4 @@ class DialogBuilder(val context: Context, private val isUseBlackTheme: Boolean)
dialogInstance = this
}?.show()
}
- }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/fankes/tsbattery/utils/factory/ExceptionFactory.kt b/app/src/main/java/com/fankes/tsbattery/utils/factory/ExceptionFactory.kt
new file mode 100644
index 0000000..dc2258e
--- /dev/null
+++ b/app/src/main/java/com/fankes/tsbattery/utils/factory/ExceptionFactory.kt
@@ -0,0 +1,82 @@
+/*
+ * TSBattery - A new way to save your battery avoid cancer apps hacker it.
+ * Copyright (C) 2019-2022 Fankes Studio(qzmmcn@163.com)
+ * https://github.com/fankes/TSBattery
+ *
+ * This software is non-free but opensource software: you can redistribute it
+ * and/or modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and eula along with this software. If not, see
+ *
+ *
+ * This file is Created by fankes on 2022/3/13.
+ */
+@file:Suppress("unused")
+
+package com.fankes.tsbattery.utils.factory
+
+import com.highcapable.yukihookapi.hook.log.loggerE
+
+/**
+ * 忽略异常返回值
+ * @param result 回调 - 如果异常为空
+ * @return [T] 发生异常时返回设定值否则返回正常值
+ */
+inline fun safeOfNull(result: () -> T): T? = safeOf(default = null, result)
+
+/**
+ * 忽略异常返回值
+ * @param result 回调 - 如果异常为 false
+ * @return [Boolean] 发生异常时返回设定值否则返回正常值
+ */
+inline fun safeOfFalse(result: () -> Boolean) = safeOf(default = false, result)
+
+/**
+ * 忽略异常返回值
+ * @param result 回调 - 如果异常为 true
+ * @return [Boolean] 发生异常时返回设定值否则返回正常值
+ */
+inline fun safeOfTrue(result: () -> Boolean) = safeOf(default = true, result)
+
+/**
+ * 忽略异常返回值
+ * @param result 回调 - 如果异常为 false
+ * @return [String] 发生异常时返回设定值否则返回正常值
+ */
+inline fun safeOfNothing(result: () -> String) = safeOf(default = "", result)
+
+/**
+ * 忽略异常返回值
+ * @param result 回调 - 如果异常为 false
+ * @return [Int] 发生异常时返回设定值否则返回正常值
+ */
+inline fun safeOfNan(result: () -> Int) = safeOf(default = 0, result)
+
+/**
+ * 忽略异常返回值
+ * @param default 异常返回值
+ * @param result 正常回调值
+ * @return [T] 发生异常时返回设定值否则返回正常值
+ */
+inline fun safeOf(default: T, result: () -> T) = try {
+ result()
+} catch (_: Throwable) {
+ default
+}
+
+/**
+ * 忽略异常运行
+ * @param msg 出错输出的消息 - 默认为空
+ * @param block 正常回调
+ */
+inline fun T.runInSafe(msg: String = "", block: () -> Unit) {
+ runCatching(block).onFailure { if (msg.isNotBlank()) loggerE(msg = msg, e = it) }
+}
\ No newline at end of file