1
0
Fork 0

it is now possible to copy the debug logs by clicking them

This commit is contained in:
Dimo Karaivanov 2023-08-24 11:54:20 +03:00
parent 312a6734d5
commit bd675be3a2

View file

@ -1,5 +1,10 @@
package io.github.sspanak.tt9.preferences.screens;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import androidx.preference.Preference;
import androidx.preference.SwitchPreferenceCompat;
@ -10,11 +15,13 @@ import java.io.InputStreamReader;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.preferences.PreferencesActivity;
import io.github.sspanak.tt9.ui.UI;
public class DebugScreen extends BaseScreenFragment {
private static final String DEBUG_LOGS_SWITCH = "pref_enable_debug_logs";
private static final String SYSTEM_LOGS_SWITCH = "pref_enable_system_logs";
private static final String LOGS_CONTAINER = "debug_logs_container";
private final static String LOG_TAG = "DebugScreen";
private final static String DEBUG_LOGS_SWITCH = "pref_enable_debug_logs";
private final static String SYSTEM_LOGS_SWITCH = "pref_enable_system_logs";
private final static String LOGS_CONTAINER = "debug_logs_container";
public DebugScreen() { init(); }
public DebugScreen(PreferencesActivity activity) { init(activity); }
@ -26,6 +33,7 @@ public class DebugScreen extends BaseScreenFragment {
protected void onCreate() {
initLogMessagesSwitch();
initSystemLogsSwitch();
enableLogsCopy();
SwitchPreferenceCompat systemLogs = findPreference(SYSTEM_LOGS_SWITCH);
boolean includeSystemLogs = systemLogs != null && systemLogs.isChecked();
@ -35,7 +43,7 @@ public class DebugScreen extends BaseScreenFragment {
private void initLogMessagesSwitch() {
SwitchPreferenceCompat msgSwitch = findPreference(DEBUG_LOGS_SWITCH);
if (msgSwitch == null) {
Logger.w("DebugScreen", "Debug logs switch not found.");
Logger.w(LOG_TAG, "Debug logs switch not found.");
return;
}
@ -49,7 +57,7 @@ public class DebugScreen extends BaseScreenFragment {
private void initSystemLogsSwitch() {
SwitchPreferenceCompat systemLogs = findPreference(SYSTEM_LOGS_SWITCH);
if (systemLogs == null) {
Logger.w("DebugScreen", "System logs switch not found.");
Logger.w(LOG_TAG, "System logs switch not found.");
return;
}
@ -62,7 +70,7 @@ public class DebugScreen extends BaseScreenFragment {
private void printLogs(boolean includeSystemLogs) {
Preference logsContainer = findPreference(LOGS_CONTAINER);
if (logsContainer == null) {
Logger.w("DebugScreen", "Logs container not found. Cannot print logs");
Logger.w(LOG_TAG, "Logs container not found. Cannot print logs");
return;
}
@ -88,4 +96,26 @@ public class DebugScreen extends BaseScreenFragment {
logsContainer.setSummary(log.toString());
}
private void enableLogsCopy() {
if (activity == null) {
Logger.w(LOG_TAG, "Activity is missing. Copying the logs will not be possible.");
return;
}
Preference logsContainer = findPreference(LOGS_CONTAINER);
if (logsContainer == null) {
Logger.w(LOG_TAG, "Logs container not found. Copying the logs will not be possible.");
return;
}
ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
logsContainer.setOnPreferenceClickListener((Preference p) -> {
clipboard.setPrimaryClip(ClipData.newPlainText("TT9 debug log", p.getSummary()));
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) {
UI.toast(activity, "Logs copied.");
}
return true;
});
}
}