|
| 1 | +/** |
| 2 | + * Adapted from [fcitx5-android/LogActivity.kt](https://github.com/fcitx5-android/fcitx5-android/blob/e44c1c7/app/src/main/java/org/fcitx/fcitx5/android/ui/main/LogActivity.kt) |
| 3 | + */ |
| 4 | +package com.osfans.trime.settings |
| 5 | + |
| 6 | +import android.os.Bundle |
| 7 | +import android.view.View |
| 8 | +import androidx.activity.result.ActivityResultLauncher |
| 9 | +import androidx.activity.result.contract.ActivityResultContracts |
| 10 | +import androidx.appcompat.app.AlertDialog |
| 11 | +import androidx.appcompat.app.AppCompatActivity |
| 12 | +import androidx.lifecycle.lifecycleScope |
| 13 | +import cat.ereza.customactivityoncrash.CustomActivityOnCrash |
| 14 | +import com.osfans.trime.BuildConfig |
| 15 | +import com.osfans.trime.R |
| 16 | +import com.osfans.trime.TrimeApplication |
| 17 | +import com.osfans.trime.databinding.ActivityLogBinding |
| 18 | +import com.osfans.trime.settings.components.LogView |
| 19 | +import com.osfans.trime.util.DeviceInfo |
| 20 | +import com.osfans.trime.util.Logcat |
| 21 | +import com.osfans.trime.util.bindOnNotNull |
| 22 | +import com.osfans.trime.util.iso8601UTCDateTime |
| 23 | +import com.osfans.trime.util.toast |
| 24 | +import kotlinx.coroutines.Dispatchers |
| 25 | +import kotlinx.coroutines.NonCancellable |
| 26 | +import kotlinx.coroutines.launch |
| 27 | +import java.io.OutputStreamWriter |
| 28 | + |
| 29 | +class LogActivity : AppCompatActivity() { |
| 30 | + |
| 31 | + private lateinit var launcher: ActivityResultLauncher<String> |
| 32 | + private lateinit var logView: LogView |
| 33 | + |
| 34 | + private fun registerLauncher() { |
| 35 | + launcher = registerForActivityResult(ActivityResultContracts.CreateDocument()) { uri -> |
| 36 | + lifecycleScope.launch(NonCancellable + Dispatchers.IO) { |
| 37 | + runCatching { |
| 38 | + if (uri != null) |
| 39 | + contentResolver.openOutputStream(uri)?.let { OutputStreamWriter(it) } |
| 40 | + else null |
| 41 | + }.bindOnNotNull { x -> |
| 42 | + x.use { |
| 43 | + logView |
| 44 | + .currentLog |
| 45 | + .let { log -> |
| 46 | + runCatching { |
| 47 | + it.write("--------- Build Info\n") |
| 48 | + it.write("${BuildConfig.BUILD_INFO}\n") |
| 49 | + it.write("--------- Device Info\n") |
| 50 | + it.write(DeviceInfo.get(this@LogActivity)) |
| 51 | + it.write(log.toString()) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }?.toast() |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 61 | + super.onCreate(savedInstanceState) |
| 62 | + val binding = ActivityLogBinding.inflate(layoutInflater) |
| 63 | + setContentView(binding.root) |
| 64 | + with(binding) { |
| 65 | + setSupportActionBar(toolbar.toolbar) |
| 66 | + this@LogActivity.logView = logView |
| 67 | + logView.setLogcat( |
| 68 | + if (CustomActivityOnCrash.getConfigFromIntent(intent) == null) { |
| 69 | + supportActionBar!!.apply { |
| 70 | + setDisplayHomeAsUpEnabled(true) |
| 71 | + setTitle(R.string.real_time_logs) |
| 72 | + } |
| 73 | + Logcat() |
| 74 | + } else { |
| 75 | + supportActionBar!!.setTitle(R.string.crash_logs) |
| 76 | + clearButton.visibility = View.GONE |
| 77 | + AlertDialog.Builder(this@LogActivity) |
| 78 | + .setTitle(R.string.app_crash) |
| 79 | + .setMessage(R.string.app_crash_message) |
| 80 | + .setPositiveButton(android.R.string.ok) { _, _ -> } |
| 81 | + .show() |
| 82 | + Logcat(TrimeApplication.getLastPid()) |
| 83 | + } |
| 84 | + ) |
| 85 | + clearButton.setOnClickListener { |
| 86 | + logView.clear() |
| 87 | + } |
| 88 | + exportButton.setOnClickListener { |
| 89 | + launcher.launch("$packageName-${iso8601UTCDateTime()}.txt") |
| 90 | + } |
| 91 | + } |
| 92 | + registerLauncher() |
| 93 | + } |
| 94 | +} |
0 commit comments