Skip to content

Commit

Permalink
Bugly test
Browse files Browse the repository at this point in the history
  • Loading branch information
lulululbj committed Nov 19, 2019
1 parent c451b65 commit b1ef0ad
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dependencies {
implementation 'com.afollestad.material-dialogs:' + versions.material_dialogs_input
implementation 'de.psdev.licensesdialog:licensesdialog:2.1.0'
implementation 'com.github.sharish:ShimmerRecyclerView:v1.3'

implementation 'com.tencent.bugly:crashreport:3.1.0'
// module
implementation project(':Parser')

Expand Down
6 changes: 5 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,8 @@
-dontnote com.google.android.material.**
-dontwarn androidx.**

-keep class android.util.TypedValue {*;}
-keep class android.util.TypedValue {*;}

# bugly
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>

<application
android:name=".App"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/luyao/box/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package luyao.box

import android.app.Application
import android.content.Context
import com.tencent.bugly.crashreport.CrashReport
import luyao.util.ktx.Ktx
import luyao.util.ktx.ext.showLog
import kotlin.properties.Delegates
Expand All @@ -21,5 +22,6 @@ class App : Application() {
CONTEXT = applicationContext
showLog = BuildConfig.DEBUG
Ktx.watchAppLife = false
CrashReport.initCrashReport(applicationContext, "ecba9e150d", BuildConfig.DEBUG)
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/luyao/box/adapter/LogcatAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package luyao.box.adapter

import android.graphics.Color
import android.util.Log
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import luyao.box.R
import luyao.box.bean.LogcatBean

class LogcatAdapter(layoutResId: Int = R.layout.item_logcat) :
BaseQuickAdapter<LogcatBean, BaseViewHolder>(layoutResId) {
override fun convert(helper: BaseViewHolder, item: LogcatBean) {
helper.setText(R.id.logcatText, item.logString)

if (item.level == Log.ERROR) {
helper.setTextColor(R.id.logcatText, Color.RED)
}else{
helper.setTextColor(R.id.logcatText, Color.BLACK)

}
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/luyao/box/bean/LogcatBean.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package luyao.box.bean

import android.util.Log

data class LogcatBean(val logString: String) {

val level: Int
get() {
when {
logString.contains("V/") -> return Log.VERBOSE
logString.contains("D/") -> return Log.DEBUG
logString.contains("I/") -> return Log.INFO
logString.contains("W/") -> return Log.WARN
logString.contains("E/") -> return Log.ERROR
logString.contains("A/") -> return Log.ASSERT
}
return Log.VERBOSE
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/luyao/box/service/LogcatService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package luyao.box.service

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import luyao.box.bean.HistoryBean
import luyao.box.bean.LogcatBean
import luyao.box.ui.logcat.LogcatUtil
import luyao.box.util.FloatWindowManager

class LogcatService : Service() {

lateinit var mListen : (LogcatBean) -> Unit


inner class LogBinder : Binder(){
fun getService():LogcatService{
return this@LogcatService
}
}

override fun onBind(intent: Intent?): IBinder? {
return LogBinder()
}

override fun onCreate() {
super.onCreate()
start()
}

private fun start() {
FloatWindowManager.show()
FloatWindowManager.clearView()
CoroutineScope(Dispatchers.Main).launch {
LogcatUtil.start()
for (logcat in LogcatUtil.channel) {
mListen.invoke(logcat)
FloatWindowManager.addItem(HistoryBean("",logcat.logString))
}
}
}

fun logListener(listener : (LogcatBean) -> Unit){
this.mListen=listener
}


}
5 changes: 4 additions & 1 deletion app/src/main/java/luyao/box/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.google.android.material.navigation.NavigationView
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import kotlinx.android.synthetic.main.title_layout.*
Expand Down Expand Up @@ -72,7 +73,9 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
mainAdapter.setOnItemClickListener { _, _, position ->
when (position) {
mainAdapter.data.size - 1 -> toast("Coming soon !")
0 -> startKtxActivity<AppManagerActivity>(value = AppManagerActivity.REVERSE to true)
0 ->
CrashReport.testJavaCrash()
// startKtxActivity<AppManagerActivity>(value = AppManagerActivity.REVERSE to true)
2 -> startKtxActivity<AppManagerActivity>(value = AppManagerActivity.REVERSE to false)
else -> startActivity(mainAdapter.data[position].clazz)
}
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/luyao/box/ui/logcat/LogcatUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package luyao.box.ui.logcat

import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import luyao.box.bean.LogcatBean
import java.io.BufferedReader
import java.io.InputStreamReader

object LogcatUtil {

val channel = Channel<LogcatBean>()
val logList = ArrayList<LogcatBean>()
var keyWords = ""
var level = Log.VERBOSE


fun start() {
CoroutineScope(Dispatchers.IO).launch {
Runtime.getRuntime().exec("logcat -c") // 清除日志
val process = Runtime.getRuntime().exec("logcat -v time")
val inputStream = process.inputStream
val inputStreamReader = InputStreamReader(inputStream)
val reader = BufferedReader(inputStreamReader)

var logcat: String? = ""
do {
logcat = reader.readLine()
val log = LogcatBean(logcat)
if (check(log)) {
logList.add(log)
channel.send(log)
}
} while (logcat != null)
}
}

fun check(log: LogcatBean): Boolean {
var flag = true
if (keyWords != "" && !log.logString.contains(keyWords, false)) flag = false
if (level!=Log.VERBOSE && log.level != level) flag = false
return flag
}
}

0 comments on commit b1ef0ad

Please sign in to comment.