-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |