Browse Source

feat: View logs in-app

Linked to inside Settings.
Peter Cai 2 years ago
parent
commit
22eff6e92a

+ 4 - 0
app-common/src/main/AndroidManifest.xml

@@ -21,6 +21,10 @@
             android:label="@string/profile_download"
             android:theme="@style/Theme.AppCompat.Translucent" />
 
+        <activity
+            android:name="im.angry.openeuicc.ui.LogsActivity"
+            android:label="@string/pref_advanced_logs" />
+
         <activity
             android:name="com.journeyapps.barcodescanner.CaptureActivity"
             android:screenOrientation="fullSensor"

+ 64 - 0
app-common/src/main/java/im/angry/openeuicc/ui/LogsActivity.kt

@@ -0,0 +1,64 @@
+package im.angry.openeuicc.ui
+
+import android.os.Bundle
+import android.view.View
+import android.widget.ScrollView
+import android.widget.TextView
+import androidx.appcompat.app.AppCompatActivity
+import androidx.lifecycle.lifecycleScope
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
+import im.angry.openeuicc.common.R
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+class LogsActivity : AppCompatActivity() {
+    private lateinit var swipeRefresh: SwipeRefreshLayout
+    private lateinit var scrollView: ScrollView
+    private lateinit var logText: TextView
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_logs)
+        setSupportActionBar(findViewById(R.id.toolbar))
+        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
+
+        swipeRefresh = findViewById(R.id.swipe_refresh)
+        scrollView = findViewById(R.id.scroll_view)
+        logText = findViewById(R.id.log_text)
+
+        swipeRefresh.setOnRefreshListener {
+            lifecycleScope.launch {
+                reload()
+            }
+        }
+    }
+
+    override fun onStart() {
+        super.onStart()
+        lifecycleScope.launch {
+            reload()
+        }
+    }
+
+    private suspend fun reload() = withContext(Dispatchers.Main) {
+        swipeRefresh.isRefreshing = true
+
+        val logStr = withContext(Dispatchers.IO) {
+            try {
+                Runtime.getRuntime().exec("logcat -t 1024").inputStream.readBytes()
+                    .decodeToString()
+            } catch (_: Exception) {
+                ""
+            }
+        }
+
+        logText.text = logStr
+
+        swipeRefresh.isRefreshing = false
+
+        scrollView.post {
+            scrollView.fullScroll(View.FOCUS_DOWN)
+        }
+    }
+}

+ 6 - 0
app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt

@@ -27,6 +27,12 @@ class SettingsFragment: PreferenceFragmentCompat() {
                 true
             }
 
+        findPreference<Preference>("pref_advanced_logs")
+            ?.setOnPreferenceClickListener {
+                startActivity(Intent(requireContext(), LogsActivity::class.java))
+                true
+            }
+
         findPreference<CheckBoxPreference>("pref_notifications_download")
             ?.bindBooleanFlow(preferenceRepository.notificationDownloadFlow, PreferenceKeys.NOTIFICATION_DOWNLOAD)
 

+ 47 - 0
app-common/src/main/res/layout/activity_logs.xml

@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+
+    <com.google.android.material.appbar.MaterialToolbar
+        android:id="@+id/toolbar"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintWidth_percent="1" />
+
+    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
+        android:id="@+id/swipe_refresh"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        app:layout_constraintTop_toBottomOf="@id/toolbar"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintBottom_toBottomOf="parent">
+
+        <ScrollView
+            android:id="@+id/scroll_view"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent">
+
+            <TextView
+                android:id="@+id/log_text"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:padding="10dp"
+                android:textIsSelectable="true"
+                android:focusable="true"
+                android:textSize="10sp"
+                android:fontFamily="monospace"
+                android:lineSpacingMultiplier="1.1"
+                android:longClickable="true"
+                tools:ignore="SmallSp" />
+
+        </ScrollView>
+
+    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 3 - 0
app-common/src/main/res/values/strings.xml

@@ -58,6 +58,9 @@
     <string name="pref_notifications_enable_desc">Send notifications for <i>enabling</i> profiles\nNote that this type of notification is unreliable.</string>
     <string name="pref_notifications_disable">Disabling</string>
     <string name="pref_notifications_disable_desc">Send notifications for <i>disabling</i> profiles\nNote that this type of notification is unreliable.</string>
+    <string name="pref_advanced">Advanced</string>
+    <string name="pref_advanced_logs">Logs</string>
+    <string name="pref_advanced_logs_desc">View recent debug logs of the application</string>
     <string name="pref_info">Info</string>
     <string name="pref_info_app_version">App Version</string>
     <string name="pref_info_source_code">Source Code</string>

+ 10 - 0
app-common/src/main/res/xml/pref_settings.xml

@@ -26,6 +26,16 @@
             app:key="pref_notifications_disable" />
     </im.angry.openeuicc.ui.preference.LongSummaryPreferenceCategory>
 
+    <PreferenceCategory
+        app:title="@string/pref_advanced"
+        app:iconSpaceReserved="false">
+        <Preference
+            app:key="pref_advanced_logs"
+            app:iconSpaceReserved="false"
+            app:title="@string/pref_advanced_logs"
+            app:summary="@string/pref_advanced_logs_desc" />
+    </PreferenceCategory>
+
     <PreferenceCategory
         app:title="@string/pref_info"
         app:iconSpaceReserved="false">