From 1300f8b5178a2426a9e662419198c99be0544de4 Mon Sep 17 00:00:00 2001 From: sspanak Date: Wed, 4 Sep 2024 18:04:27 +0300 Subject: [PATCH] reorganized the Debug screen and moved the log messages to a separate screen --- app/src/main/AndroidManifest.xml | 14 +++- .../sspanak/tt9/preferences/HelpActivity.java | 49 ++------------ .../sspanak/tt9/preferences/LogsActivity.java | 19 ++++++ .../screens/debug/DebugScreen.java | 35 ---------- .../preferences/settings/SettingsHacks.java | 4 ++ .../sspanak/tt9/ui/WebViewActivity.java | 53 +++++++++++++++ app/src/main/res/xml/prefs_screen_debug.xml | 67 ++++++++++--------- 7 files changed, 129 insertions(+), 112 deletions(-) create mode 100644 app/src/main/java/io/github/sspanak/tt9/preferences/LogsActivity.java create mode 100644 app/src/main/java/io/github/sspanak/tt9/ui/WebViewActivity.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ea39b483..117b3668 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ @@ -54,6 +54,16 @@ + + + + + + + 30 the WebView does not load the entire String with .loadData(), - // so we need to do this weird shit. - // The "app:" prefix is mandatory, otherwise the anchor links do not work. - // Reference: https://developer.android.com/develop/ui/views/layout/webapps/webview - String html = getHelpHtml(); - String encodedHtml = "app:" + Base64.encodeToString(html.getBytes(), Base64.NO_PADDING); - container.loadDataWithBaseURL(encodedHtml, html, "text/html", "UTF-8", null); - - setContentView(container); - } - - private String getHelpHtml() { + protected String getText() { AssetManager assets = getAssets(); try { InputStream stream = assets.open("help.html"); diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/LogsActivity.java b/app/src/main/java/io/github/sspanak/tt9/preferences/LogsActivity.java new file mode 100644 index 00000000..d7d89944 --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/LogsActivity.java @@ -0,0 +1,19 @@ +package io.github.sspanak.tt9.preferences; + +import io.github.sspanak.tt9.db.customWords.LogcatExporter; +import io.github.sspanak.tt9.preferences.settings.SettingsStore; +import io.github.sspanak.tt9.ui.WebViewActivity; + +public class LogsActivity extends WebViewActivity { + @Override + protected String getMimeType() { + return "text/plain"; + } + + @Override + protected String getText() { + boolean includeSystemLogs = new SettingsStore(this).getSystemLogs(); + String logs = LogcatExporter.getLogs(includeSystemLogs).replace("\n", "\n\n"); + return logs.isEmpty() ? "No Logs" : logs; + } +} diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/screens/debug/DebugScreen.java b/app/src/main/java/io/github/sspanak/tt9/preferences/screens/debug/DebugScreen.java index 566a8bb4..2cbb9958 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/screens/debug/DebugScreen.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/screens/debug/DebugScreen.java @@ -1,9 +1,6 @@ package io.github.sspanak.tt9.preferences.screens.debug; -import androidx.preference.SwitchPreferenceCompat; - import io.github.sspanak.tt9.R; -import io.github.sspanak.tt9.db.customWords.LogcatExporter; import io.github.sspanak.tt9.hacks.DeviceInfo; import io.github.sspanak.tt9.preferences.PreferencesActivity; import io.github.sspanak.tt9.preferences.items.ItemText; @@ -13,10 +10,6 @@ public class DebugScreen extends BaseScreenFragment { public static final String NAME = "Debug"; private static final String DEVICE_INFO_CONTAINER = "pref_device_info"; - private static final String SYSTEM_LOGS_SWITCH = "pref_enable_system_logs"; - - - private ItemText logsContainer; public DebugScreen() { init(); } public DebugScreen(PreferencesActivity activity) { init(activity); } @@ -32,35 +25,7 @@ public class DebugScreen extends BaseScreenFragment { (new ItemText(activity, findPreference(DEVICE_INFO_CONTAINER))).populate(new DeviceInfo().toString()).enableClickHandler(); (new ItemExportLogcat(findPreference(ItemExportLogcat.NAME), activity)).enableClickHandler(); (new ItemDemoMode(findPreference(ItemDemoMode.NAME), activity)).populate().enableClickHandler(); - initSystemLogsSwitch(); - - SwitchPreferenceCompat systemLogs = findPreference(SYSTEM_LOGS_SWITCH); - boolean includeSystemLogs = systemLogs != null && systemLogs.isChecked(); - printLogs(includeSystemLogs); resetFontSize(false); } - - private void initSystemLogsSwitch() { - SwitchPreferenceCompat systemLogs = findPreference(SYSTEM_LOGS_SWITCH); - if (systemLogs != null) { - systemLogs.setOnPreferenceChangeListener((p, newValue) -> { - printLogs((boolean) newValue); - return true; - }); - } - } - - private void printLogs(boolean includeSystemLogs) { - if (logsContainer == null) { - logsContainer = new ItemText(activity, findPreference("debug_logs_container")); - logsContainer.enableClickHandler(); - } - - String logs = LogcatExporter.getLogs(includeSystemLogs).replace("\n", "\n\n"); - if (logs.isEmpty()) { - logs = "No Logs"; - } - logsContainer.populate(logs); - } } diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsHacks.java b/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsHacks.java index e01f7924..43042fbd 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsHacks.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsHacks.java @@ -62,4 +62,8 @@ class SettingsHacks extends BaseSettings { defaultTime = DeviceInfo.isQinF21() ? 20 : defaultTime; return getStringifiedInt("pref_key_pad_debounce_time", defaultTime); } + + public boolean getSystemLogs() { + return prefs.getBoolean("pref_enable_system_logs", false); + } } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/WebViewActivity.java b/app/src/main/java/io/github/sspanak/tt9/ui/WebViewActivity.java new file mode 100644 index 00000000..4af8c25b --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/ui/WebViewActivity.java @@ -0,0 +1,53 @@ +package io.github.sspanak.tt9.ui; + +import android.os.Bundle; +import android.util.Base64; +import android.webkit.WebView; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; + +abstract public class WebViewActivity extends AppCompatActivity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + buildLayout(); + } + + @Override + public boolean onSupportNavigateUp() { + finish(); + return true; + } + + private void buildLayout() { + enableBackButton(); + setContent(); + } + + private void enableBackButton() { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayShowHomeEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(true); + } + } + + private void setContent() { + WebView container = new WebView(this); + + // On API > 30 the WebView does not load the entire String with .loadData(), + // so we need to do this weird shit. + // The "app:" prefix is mandatory, otherwise the anchor links do not work. + // Reference: https://developer.android.com/develop/ui/views/layout/webapps/webview + String text = getText(); + String encodedHtml = "app:" + Base64.encodeToString(text.getBytes(), Base64.NO_PADDING); + container.loadDataWithBaseURL(encodedHtml, text, getMimeType(), "UTF-8", null); + + setContentView(container); + } + + abstract protected String getText(); + abstract protected String getMimeType(); +} diff --git a/app/src/main/res/xml/prefs_screen_debug.xml b/app/src/main/res/xml/prefs_screen_debug.xml index 60f2874c..1b7f3dd0 100644 --- a/app/src/main/res/xml/prefs_screen_debug.xml +++ b/app/src/main/res/xml/prefs_screen_debug.xml @@ -1,43 +1,48 @@ - + + + android:fragment="io.github.sspanak.tt9.preferences.screens.UsageStatsScreen" + android:key="pref_slow_queries" + android:title="@string/pref_category_usage_stats" /> - + + - + + - + + - + - + - - - - - + + +