Move source files from 'java' to 'kotlin' folder
This commit is contained in:
16
app/src/main/kotlin/com/github/gotify/log/Format.kt
Normal file
16
app/src/main/kotlin/com/github/gotify/log/Format.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import android.content.Context
|
||||
import com.hypertrack.hyperlog.LogFormat
|
||||
|
||||
internal class Format(context: Context) : LogFormat(context) {
|
||||
override fun getFormattedLogMessage(
|
||||
logLevelName: String,
|
||||
tag: String,
|
||||
message: String,
|
||||
timeStamp: String,
|
||||
senderName: String,
|
||||
osVersion: String,
|
||||
deviceUuid: String
|
||||
) = "$timeStamp $logLevelName: $message"
|
||||
}
|
||||
47
app/src/main/kotlin/com/github/gotify/log/Log.kt
Normal file
47
app/src/main/kotlin/com/github/gotify/log/Log.kt
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.hypertrack.hyperlog.HyperLog
|
||||
|
||||
internal object Log {
|
||||
private const val TAG = "gotify"
|
||||
|
||||
fun init(content: Context) {
|
||||
HyperLog.initialize(content, Format(content))
|
||||
HyperLog.setLogLevel(Log.INFO) // TODO configurable
|
||||
}
|
||||
|
||||
fun get(): String {
|
||||
val logs = HyperLog.getDeviceLogsAsStringList(false)
|
||||
return logs.takeLast(200).reversed().joinToString("\n")
|
||||
}
|
||||
|
||||
fun e(message: String) {
|
||||
HyperLog.e(TAG, message)
|
||||
}
|
||||
|
||||
fun e(message: String, e: Throwable) {
|
||||
HyperLog.e(TAG, "$message\n${Log.getStackTraceString(e)}")
|
||||
}
|
||||
|
||||
fun i(message: String) {
|
||||
HyperLog.i(TAG, message)
|
||||
}
|
||||
|
||||
fun i(message: String, e: Throwable) {
|
||||
HyperLog.i(TAG, "$message\n${Log.getStackTraceString(e)}")
|
||||
}
|
||||
|
||||
fun w(message: String) {
|
||||
HyperLog.w(TAG, message)
|
||||
}
|
||||
|
||||
fun w(message: String, e: Throwable) {
|
||||
HyperLog.w(TAG, "$message\n${Log.getStackTraceString(e)}")
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
HyperLog.deleteLogs()
|
||||
}
|
||||
}
|
||||
82
app/src/main/kotlin/com/github/gotify/log/LogsActivity.kt
Normal file
82
app/src/main/kotlin/com/github/gotify/log/LogsActivity.kt
Normal file
@@ -0,0 +1,82 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.github.gotify.R
|
||||
import com.github.gotify.Utils
|
||||
import com.github.gotify.Utils.launchCoroutine
|
||||
import com.github.gotify.databinding.ActivityLogsBinding
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
internal class LogsActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityLogsBinding
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityLogsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
Log.i("Entering ${javaClass.simpleName}")
|
||||
updateLogs()
|
||||
setSupportActionBar(binding.appBarDrawer.toolbar)
|
||||
val actionBar = supportActionBar
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true)
|
||||
actionBar.setDisplayShowCustomEnabled(true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLogs() {
|
||||
launchCoroutine {
|
||||
val log = Log.get()
|
||||
withContext(Dispatchers.Main) {
|
||||
val content = binding.logContent
|
||||
if (content.selectionStart == content.selectionEnd) {
|
||||
content.text = log
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isDestroyed) {
|
||||
handler.postDelayed({ updateLogs() }, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.logs_action, menu)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
finish()
|
||||
true
|
||||
}
|
||||
R.id.action_delete_logs -> {
|
||||
Log.clear()
|
||||
binding.logContent.text = null
|
||||
true
|
||||
}
|
||||
R.id.action_copy_logs -> {
|
||||
val content = binding.logContent
|
||||
val clipboardManager =
|
||||
getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clipData = ClipData.newPlainText("GotifyLog", content.text.toString())
|
||||
clipboardManager.setPrimaryClip(clipData)
|
||||
Utils.showSnackBar(this, getString(R.string.logs_copied))
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import com.github.gotify.log.Log.e
|
||||
|
||||
internal object UncaughtExceptionHandler {
|
||||
fun registerCurrentThread() {
|
||||
Thread.setDefaultUncaughtExceptionHandler { _, e: Throwable? ->
|
||||
e("uncaught exception", e!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user