Skip to content

Commit

Permalink
Using Thread-Safe CopyOnWriteArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard20181 committed Nov 14, 2022
1 parent dbe7503 commit ec5c791
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.idea/misc.xml
.DS_Store
/build
/captures
Expand All @@ -17,4 +18,4 @@
local.properties
/app/releaseHasController/
/app/debug/
/app/release/
/app/release/
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ dependencies {
ksp 'com.highcapable.yukihookapi:ksp-xposed:1.1.4'
implementation "com.microsoft.appcenter:appcenter-analytics:4.4.5"
implementation "com.microsoft.appcenter:appcenter-crashes:4.4.5"
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.google.code.gson:gson:2.10'
implementation 'com.github.duanhong169:drawabletoolbox:1.0.7'
implementation "com.github.topjohnwu.libsu:core:3.1.2"
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.highcapable.yukihookapi.hook.log.loggerI
import com.highcapable.yukihookapi.hook.log.loggerW
import com.highcapable.yukihookapi.hook.type.android.BundleClass
import com.highcapable.yukihookapi.hook.type.android.MessageClass
import java.util.concurrent.CopyOnWriteArrayList

object FrameworkHooker : YukiBaseHooker() {

Expand Down Expand Up @@ -88,7 +89,7 @@ object FrameworkHooker : YukiBaseHooker() {
private var mutedErrorsIfRestartApps = HashSet<String>()

/** 已记录的 APP 异常信息数组 */
private var appErrorsRecords = ArrayList<AppErrorsInfoBean>()
private var appErrorsRecords = CopyOnWriteArrayList<AppErrorsInfoBean>()

/** 注册生命周期 */
private fun registerLifecycle() {
Expand All @@ -98,7 +99,7 @@ object FrameworkHooker : YukiBaseHooker() {
/** 刷新模块 Resources 缓存 */
registerReceiver(Intent.ACTION_LOCALE_CHANGED) { _, _ -> refreshModuleAppResources() }
/** 启动时从本地获取异常记录 */
onCreate { appErrorsRecords = ConfigData.getResolverString(ConfigData.APP_ERRORS_DATA).toEntity() ?: arrayListOf() }
onCreate { appErrorsRecords = ConfigData.getResolverString(ConfigData.APP_ERRORS_DATA).toEntity() ?: CopyOnWriteArrayList() }
}
FrameworkTool.Host.with(instance = this) {
onOpenAppUsedFramework {
Expand All @@ -111,7 +112,7 @@ object FrameworkHooker : YukiBaseHooker() {
AppErrorsInfoBean.createEmpty()
}
}
onPushAppErrorsInfoData { appErrorsRecords }
onPushAppErrorsInfoData { appErrorsRecords.toMutableList() }
onRemoveAppErrorsInfoData {
loggerI(msg = "Removed app errors info data for package \"${it.packageName}\"")
appErrorsRecords.remove(it)
Expand Down Expand Up @@ -178,10 +179,14 @@ object FrameworkHooker : YukiBaseHooker() {
}

/** 保存异常记录到本地 */
private fun saveAllAppErrorsRecords() = newThread {
runCatching {
ConfigData.putResolverString(ConfigData.APP_ERRORS_DATA, appErrorsRecords.toJson())
}.onFailure {}
private fun saveAllAppErrorsRecords() = newThread { ConfigData.putResolverString(ConfigData.APP_ERRORS_DATA, appErrorsRecords.toJson()) }

/** 由于使用广播与界面进行通信,大于 200 条日志会超过可发送上限而失败 **/
private fun limitMaxAppErrorsRecords() {
if (appErrorsRecords.size > 200) {
appErrorsRecords.removeLast()
limitMaxAppErrorsRecords()
}
}

override fun onHook() {
Expand Down Expand Up @@ -360,20 +365,16 @@ object FrameworkHooker : YukiBaseHooker() {

/** 当前 APP 信息 */
val appInfo = ProcessRecordClass.toClass().field { name = "info" }.get(proc).cast<ApplicationInfo>()
/** 启动新线程延迟防止方法执行顺序在前导致无法正确获取数据 */
newThread {
/** 延迟 50ms */
Thread.sleep(50)
/** 添加当前异常信息到第一位 */
appErrorsRecords.add(
0, AppErrorsInfoBean.clone(pid, appInfo?.packageName, appUserIdRecords[pid], args().last().cast())
)
loggerI(msg = "Received crash application data --pid $pid")
}

/** 添加当前异常信息到第一位 */
appErrorsRecords.add(0, AppErrorsInfoBean.clone(pid, appInfo?.packageName, appUserIdRecords[pid], args().last().cast()))
loggerI(msg = "Received crash application data --pid $pid")
limitMaxAppErrorsRecords()

/** 保存异常记录到本地 */
saveAllAppErrorsRecords()
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object FrameworkTool {
private val CALL_APP_ERRORS_DATA_REMOVE = ChannelData<AppErrorsInfoBean>("call_app_errors_data_remove")
private val CALL_APP_LIST_DATA_GET_RESULT = ChannelData<ArrayList<AppInfoBean>>("call_app_info_list_data_get_result")
private val CALL_APP_ERROR_DATA_GET_RESULT = ChannelData<AppErrorsInfoBean>("call_app_error_data_get_result")
private val CALL_APP_ERRORS_DATA_GET_RESULT = ChannelData<ArrayList<AppErrorsInfoBean>>("call_app_errors_data_get_result")
private val CALL_APP_ERRORS_DATA_GET_RESULT = ChannelData<MutableList<AppErrorsInfoBean>>("call_app_errors_data_get_result")
private val CALL_MUTED_ERRORS_APP_DATA_GET_RESULT = ChannelData<ArrayList<MutedErrorsAppBean>>("call_muted_app_errors_data_get_result")
private val CALL_UNMUTE_ERRORS_APP_DATA = ChannelData<MutedErrorsAppBean>("call_unmute_errors_app_data")
private val CALL_MUTED_ERRORS_IF_UNLOCK = ChannelData<String>("call_muted_errors_if_unlock")
Expand Down Expand Up @@ -102,7 +102,7 @@ object FrameworkTool {
* 监听发送 APP 异常信息数组
* @param result 回调数据
*/
fun onPushAppErrorsInfoData(result: () -> ArrayList<AppErrorsInfoBean>) {
fun onPushAppErrorsInfoData(result: () -> MutableList<AppErrorsInfoBean>) {
instance?.dataChannel?.with { wait(CALL_APP_ERRORS_DATA_GET) { put(CALL_APP_ERRORS_DATA_GET_RESULT, result()) } }
}

Expand Down Expand Up @@ -265,7 +265,7 @@ object FrameworkTool {
*/
fun fetchAppErrorsInfoData(context: Context, result: (ArrayList<AppErrorsInfoBean>) -> Unit) {
context.dataChannel(SYSTEM_FRAMEWORK_NAME).with {
wait(CALL_APP_ERRORS_DATA_GET_RESULT) { result(it) }
wait(CALL_APP_ERRORS_DATA_GET_RESULT) { result(it as ArrayList<AppErrorsInfoBean>) }
put(CALL_APP_ERRORS_DATA_GET)
}
}
Expand Down Expand Up @@ -370,4 +370,4 @@ object FrameworkTool {
put(CALL_APP_LIST_DATA_GET, appFilters)
}
}
}
}

0 comments on commit ec5c791

Please # to comment.