Skip to content

Commit c9b19be

Browse files
committed
Add file logging toggle and clear button in Settings
1 parent 6abc3b6 commit c9b19be

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/com/bpmct/trmnl_nook_simple_touch/ApiPrefs.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ApiPrefs {
1010
private static final String KEY_API_BASE_URL = "api_base_url";
1111
private static final String DEFAULT_API_BASE_URL = "https://usetrmnl.com/api";
1212
private static final String KEY_ALLOW_SLEEP = "allow_sleep";
13+
private static final String KEY_FILE_LOGGING = "file_logging";
1314
private static final String SCREENSAVER_PATH = "/media/screensavers/TRMNL/display.png";
1415

1516
public static boolean hasCredentials(Context context) {
@@ -98,6 +99,16 @@ public static void setAllowSleep(Context context, boolean allow) {
9899
.putBoolean(KEY_ALLOW_SLEEP, allow).commit();
99100
}
100101

102+
public static boolean isFileLoggingEnabled(Context context) {
103+
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
104+
return prefs.getBoolean(KEY_FILE_LOGGING, false);
105+
}
106+
107+
public static void setFileLoggingEnabled(Context context, boolean enabled) {
108+
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit()
109+
.putBoolean(KEY_FILE_LOGGING, enabled).commit();
110+
}
111+
101112
/** File path for screensaver image (hardcoded for NOOK). */
102113
public static String getScreensaverPath() {
103114
return SCREENSAVER_PATH;

src/com/bpmct/trmnl_nook_simple_touch/DisplayActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public class DisplayActivity extends Activity {
100100
protected void onCreate(Bundle savedInstanceState) {
101101
super.onCreate(savedInstanceState);
102102

103+
// Initialize file logging from saved preference
104+
FileLogger.setEnabled(ApiPrefs.isFileLoggingEnabled(this));
105+
103106
// NOOK Simple Touch is API 7 (no nav bar); keep this deterministic.
104107
getWindow().setFlags(
105108
WindowManager.LayoutParams.FLAG_FULLSCREEN,

src/com/bpmct/trmnl_nook_simple_touch/FileLogger.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,22 @@ public class FileLogger {
1515
private static final long MAX_SIZE = 512 * 1024; // 512KB max
1616
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
1717

18+
private static volatile boolean enabled = false;
19+
20+
public static void setEnabled(boolean e) { enabled = e; }
21+
public static boolean isEnabled() { return enabled; }
22+
23+
public static void clear() {
24+
try {
25+
File f = new File(LOG_PATH);
26+
if (f.exists()) f.delete();
27+
File old = new File(LOG_PATH + ".old");
28+
if (old.exists()) old.delete();
29+
} catch (Throwable t) { /* ignore */ }
30+
}
31+
1832
public static synchronized void log(String tag, String level, String msg) {
33+
if (!enabled) return;
1934
try {
2035
File f = new File(LOG_PATH);
2136
// Rotate if too large

src/com/bpmct/trmnl_nook_simple_touch/SettingsActivity.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class SettingsActivity extends Activity {
1717
private static final int APP_ROTATION_DEGREES = 90;
1818
private TextView statusView;
1919
private CheckBox allowSleepCheck;
20+
private CheckBox fileLoggingCheck;
2021
protected void onCreate(Bundle savedInstanceState) {
2122
super.onCreate(savedInstanceState);
2223

@@ -79,6 +80,34 @@ protected void onCreate(Bundle savedInstanceState) {
7980
ViewGroup.LayoutParams.WRAP_CONTENT,
8081
ViewGroup.LayoutParams.WRAP_CONTENT));
8182

83+
TextView loggingLabel = new TextView(this);
84+
loggingLabel.setText("Logging");
85+
loggingLabel.setTextSize(14);
86+
loggingLabel.setTextColor(0xFF000000);
87+
LinearLayout.LayoutParams loggingLabelParams = new LinearLayout.LayoutParams(
88+
ViewGroup.LayoutParams.WRAP_CONTENT,
89+
ViewGroup.LayoutParams.WRAP_CONTENT);
90+
loggingLabelParams.topMargin = 20;
91+
inner.addView(loggingLabel, loggingLabelParams);
92+
93+
fileLoggingCheck = new CheckBox(this);
94+
fileLoggingCheck.setText("Capture logs to file");
95+
fileLoggingCheck.setTextColor(0xFF000000);
96+
fileLoggingCheck.setChecked(ApiPrefs.isFileLoggingEnabled(this));
97+
inner.addView(fileLoggingCheck, new LinearLayout.LayoutParams(
98+
ViewGroup.LayoutParams.WRAP_CONTENT,
99+
ViewGroup.LayoutParams.WRAP_CONTENT));
100+
101+
Button clearLogsButton = new Button(this);
102+
clearLogsButton.setText("Clear Logs");
103+
clearLogsButton.setTextColor(0xFF000000);
104+
clearLogsButton.setOnClickListener(new View.OnClickListener() {
105+
public void onClick(View v) {
106+
FileLogger.clear();
107+
}
108+
});
109+
inner.addView(clearLogsButton);
110+
82111
LinearLayout actions = new LinearLayout(this);
83112
actions.setOrientation(LinearLayout.HORIZONTAL);
84113
LinearLayout.LayoutParams actionsParams = new LinearLayout.LayoutParams(
@@ -134,6 +163,7 @@ protected void onResume() {
134163
statusView.setText(ApiPrefs.hasCredentials(this) ? "Saved" : "Missing");
135164
}
136165
if (allowSleepCheck != null) allowSleepCheck.setChecked(ApiPrefs.isAllowSleep(this));
166+
if (fileLoggingCheck != null) fileLoggingCheck.setChecked(ApiPrefs.isFileLoggingEnabled(this));
137167
}
138168

139169
protected void onPause() {
@@ -143,5 +173,10 @@ protected void onPause() {
143173

144174
private void saveDisplayPrefs() {
145175
if (allowSleepCheck != null) ApiPrefs.setAllowSleep(this, allowSleepCheck.isChecked());
176+
if (fileLoggingCheck != null) {
177+
boolean enabled = fileLoggingCheck.isChecked();
178+
ApiPrefs.setFileLoggingEnabled(this, enabled);
179+
FileLogger.setEnabled(enabled);
180+
}
146181
}
147182
}

0 commit comments

Comments
 (0)