diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/AboutPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/AboutPreferences.java
index 69cff9dc..59347966 100644
--- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/AboutPreferences.java
+++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/AboutPreferences.java
@@ -15,14 +15,79 @@
*/
package com.health.openscale.gui.preferences;
+import android.content.Intent;
+import android.net.Uri;
import android.os.Bundle;
+import android.preference.Preference;
import android.preference.PreferenceFragment;
+import android.util.Log;
import com.health.openscale.BuildConfig;
import com.health.openscale.R;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import timber.log.Timber;
+
+import static android.app.Activity.RESULT_OK;
+
public class AboutPreferences extends PreferenceFragment {
private static final String KEY_APP_VERSION = "pref_app_version";
+ private static final String KEY_DEBUG_LOG = "debug_log";
+
+ private static final int DEBUG_LOG_REQUEST = 100;
+
+ class FileDebugTree extends Timber.DebugTree {
+ PrintWriter writer;
+ DateFormat format;
+
+ FileDebugTree(OutputStream output) {
+ writer = new PrintWriter(output, true);
+ format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+ }
+
+ void close() {
+ writer.close();
+ }
+
+ private String priorityToString(int priority) {
+ switch (priority) {
+ case Log.ASSERT:
+ return "Assert";
+ case Log.ERROR:
+ return "Error";
+ case Log.WARN:
+ return "Warning";
+ case Log.INFO:
+ return "Info";
+ case Log.DEBUG:
+ return "Debug";
+ case Log.VERBOSE:
+ return "Verbose";
+ }
+ return String.format("Unknown (%d)", priority);
+ }
+
+ @Override
+ protected void log(int priority, String tag, String message, Throwable t) {
+ writer.printf("%s %s %s: %s\n",
+ format.format(new Date()), priorityToString(priority), tag, message);
+ }
+ }
+
+ private FileDebugTree getEnabledFileDebugTree() {
+ for (Timber.Tree tree : Timber.forest()) {
+ if (tree instanceof FileDebugTree) {
+ return (FileDebugTree) tree;
+ }
+ }
+ return null;
+ }
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -30,6 +95,59 @@ public class AboutPreferences extends PreferenceFragment {
addPreferencesFromResource(R.xml.about_preferences);
- findPreference(KEY_APP_VERSION).setSummary("v" + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")");
+ findPreference(KEY_APP_VERSION).setSummary(
+ String.format("v%s (%d)", BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE));
+
+ Preference debugLog = findPreference(KEY_DEBUG_LOG);
+ debugLog.setSummary(getEnabledFileDebugTree() != null
+ ? R.string.info_is_enable : R.string.info_is_not_enable);
+ debugLog.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ FileDebugTree tree = getEnabledFileDebugTree();
+ if (tree != null) {
+ Timber.d("Debug log disabled");
+ tree.close();
+ Timber.uproot(tree);
+ preference.setSummary(R.string.info_is_not_enable);
+ return true;
+ }
+
+ DateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
+ String fileName = String.format("openScale_%s.txt", format.format(new Date()));
+
+ Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
+ intent.setType("text/plain");
+ intent.putExtra(Intent.EXTRA_TITLE, fileName);
+
+ startActivityForResult(intent, DEBUG_LOG_REQUEST);
+
+ return true;
+ }
+ });
+ }
+
+ private void startLogTo(Uri uri) {
+ try {
+ OutputStream output = getActivity().getContentResolver().openOutputStream(uri);
+ Timber.plant(new FileDebugTree(output));
+ findPreference(KEY_DEBUG_LOG).setSummary(R.string.info_is_enable);
+ Timber.d("Debug log enabled (%s v%s (%d))",
+ getResources().getString(R.string.app_name),
+ BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE);
+ }
+ catch (IOException ex) {
+ Timber.e(ex, "Failed to open debug log %s", uri.toString());
+ }
+ }
+
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+
+ if (requestCode == DEBUG_LOG_REQUEST && resultCode == RESULT_OK && data != null) {
+ startLogTo(data.getData());
+ }
}
}
diff --git a/android_app/app/src/main/res/values/strings.xml b/android_app/app/src/main/res/values/strings.xml
index 2cac8051..537eedbb 100644
--- a/android_app/app/src/main/res/values/strings.xml
+++ b/android_app/app/src/main/res/values/strings.xml
@@ -217,4 +217,6 @@
monthly
Is your scale not supported?
Click here to help add support for it
+ Development
+ Save debug log to file
diff --git a/android_app/app/src/main/res/xml/about_preferences.xml b/android_app/app/src/main/res/xml/about_preferences.xml
index 45d17852..fbfd33e6 100644
--- a/android_app/app/src/main/res/xml/about_preferences.xml
+++ b/android_app/app/src/main/res/xml/about_preferences.xml
@@ -22,4 +22,10 @@
android:selectable="false"
android:title="@string/label_license"
android:summary="GPLv3"/>
+
+
+
\ No newline at end of file