From 02f56f25fb9521dd227bea9d7eb9df4c52b87b89 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Fri, 9 Sep 2016 19:27:07 +0200 Subject: [PATCH] Cleaned code, added debug log to DebugFragment of AboutActivity --- .../activity/AboutActivity.java | 31 +++++- .../activity/MainActivity.java | 7 +- .../activity/SettingsActivity.java | 5 - .../diaspora_android/data/PodUserProfile.java | 2 +- .../diaspora_android/task/GetPodsService.java | 2 +- .../task/ImageDownloadTask.java | 2 +- .../task/ProfileFetchTask.java | 2 +- .../task/StatisticsFetchTask.java | 2 +- .../dfa/diaspora_android/util/Helpers.java | 2 +- .../github/dfa/diaspora_android/util/Log.java | 99 +++++++++++++++++++ app/src/main/res/layout/fragment_debug.xml | 24 +++++ app/src/main/res/values/strings-about.xml | 1 + 12 files changed, 163 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/github/dfa/diaspora_android/util/Log.java diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java index 6ad57eae..9b5ea975 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java @@ -42,6 +42,11 @@ import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.ui.HtmlTextView; import com.github.dfa.diaspora_android.util.Helpers; +import com.github.dfa.diaspora_android.util.Log; + +import java.util.ArrayList; +import java.util.Observable; +import java.util.Observer; import butterknife.BindView; import butterknife.ButterKnife; @@ -184,8 +189,8 @@ public class AboutActivity extends AppCompatActivity { /** * Fragment that shows debug information like app version, pod version... */ - public static class DebugFragment extends Fragment { - + public static class DebugFragment extends Fragment implements Observer { + private TextView logBox; public DebugFragment() { } @@ -198,6 +203,10 @@ public class AboutActivity extends AppCompatActivity { TextView osVersion = (TextView) rootView.findViewById(R.id.fragment_debug__android_version); TextView deviceName = (TextView) rootView.findViewById(R.id.fragment_debug__device_name); TextView podDomain = (TextView) rootView.findViewById(R.id.fragment_debug__pod_domain); + logBox = (TextView) rootView.findViewById(R.id.fragment_debug__log_box); + + Log.addLogObserver(this); + update(Log.getInstance(), null); if (isAdded()) { try { @@ -218,6 +227,24 @@ public class AboutActivity extends AppCompatActivity { } return rootView; } + + @Override + public void onDestroyView() { + Log.removeLogObserver(this); + super.onDestroyView(); + } + + @Override + public void update(Observable observable, Object o) { + if(logBox != null) { + ArrayList logs = Log.getLogBuffer(); + String log = ""; + for(String s : logs) { + log = log + s+"\n"; + } + logBox.setText(log); + } + } } /** diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 517c6f1e..a60b144d 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -51,7 +51,6 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.ActionMenuView; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -82,6 +81,7 @@ import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.CustomWebViewClient; import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; import com.github.dfa.diaspora_android.util.Helpers; +import com.github.dfa.diaspora_android.util.Log; import com.github.dfa.diaspora_android.util.WebHelper; import org.json.JSONException; @@ -202,7 +202,7 @@ public class MainActivity extends AppCompatActivity boolean newWebView = (webView == null); if(newWebView) { Log.d(App.TAG, "Webview was null. Create new one."); - View webviewHolder = getLayoutInflater().inflate(R.layout.webview, null); + View webviewHolder = getLayoutInflater().inflate(R.layout.webview, this.contentLayout); webView = (ContextMenuWebView) webviewHolder.findViewById(R.id.webView); ((LinearLayout)webView.getParent()).removeView(webView); setupWebView(savedInstanceState); @@ -353,6 +353,7 @@ public class MainActivity extends AppCompatActivity } //For Android 4.1/4.2 only. DONT REMOVE + @SuppressWarnings("unused") protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { Log.d(App.TAG, "openFileChooser(ValCallback, String, String"); @@ -805,7 +806,7 @@ public class MainActivity extends AppCompatActivity if (WebHelper.isOnline(MainActivity.this)) { final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - View layout = getLayoutInflater().inflate(R.layout.dialog_search__people_tags, null); + View layout = getLayoutInflater().inflate(R.layout.dialog_search__people_tags, contentLayout); final EditText input = (EditText) layout.findViewById(R.id.dialog_search__input); final DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { @Override diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java index 7b06e780..94554385 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java @@ -26,20 +26,15 @@ import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; -import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; import android.view.MenuItem; -import android.view.View; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; -import butterknife.OnEditorAction; - /** * @author vanitas */ diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/PodUserProfile.java b/app/src/main/java/com/github/dfa/diaspora_android/data/PodUserProfile.java index 30c6a131..bbc90500 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/PodUserProfile.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/PodUserProfile.java @@ -19,7 +19,7 @@ package com.github.dfa.diaspora_android.data; import android.os.Handler; -import android.util.Log; +import com.github.dfa.diaspora_android.util.Log; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java index d9f964a8..e826c34e 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java @@ -23,7 +23,7 @@ import android.content.Intent; import android.os.AsyncTask; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; -import android.util.Log; +import com.github.dfa.diaspora_android.util.Log; import com.github.dfa.diaspora_android.App; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java index 88339993..2ebd60df 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java @@ -22,7 +22,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.support.annotation.Nullable; -import android.util.Log; +import com.github.dfa.diaspora_android.util.Log; import android.widget.ImageView; import com.github.dfa.diaspora_android.App; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java index 849078f5..536cbba1 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java @@ -20,7 +20,7 @@ package com.github.dfa.diaspora_android.task; import android.content.Context; import android.os.AsyncTask; -import android.util.Log; +import com.github.dfa.diaspora_android.util.Log; import android.webkit.CookieManager; import com.github.dfa.diaspora_android.App; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/StatisticsFetchTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/StatisticsFetchTask.java index d1d90621..1a94fc17 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/StatisticsFetchTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/StatisticsFetchTask.java @@ -20,7 +20,7 @@ package com.github.dfa.diaspora_android.task; import android.content.Context; import android.os.AsyncTask; -import android.util.Log; +import com.github.dfa.diaspora_android.util.Log; import android.webkit.CookieManager; import com.github.dfa.diaspora_android.App; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java index 1db486f2..2b296014 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java @@ -24,7 +24,7 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; -import android.util.Log; +import com.github.dfa.diaspora_android.util.Log; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/Log.java b/app/src/main/java/com/github/dfa/diaspora_android/util/Log.java new file mode 100644 index 00000000..25815072 --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/Log.java @@ -0,0 +1,99 @@ +package com.github.dfa.diaspora_android.util; + +import com.github.dfa.diaspora_android.App; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Observable; +import java.util.Observer; + +/** + * Class that saves logs eg. for later debugging. + * TODO: Differentiate log types (error/debug/info...) + * Created by vanitas on 09.09.16. + */ +public class Log extends Observable{ + public static Log instance; + private SimpleDateFormat dateFormat; + private ArrayList logBuffer; + private ArrayList observers; + + private Log() { + logBuffer = new ArrayList<>(); + dateFormat = new SimpleDateFormat("HH:mm:ss"); + observers = new ArrayList<>(); + } + + public static Log getInstance() { + if(instance == null) instance = new Log(); + return instance; + } + + private static String time() { + return getInstance().dateFormat.format(new Date())+": "; + } + + public static void d(String tag, String msg) { + Log l = getInstance(); + android.util.Log.d(tag, msg); + l.logBuffer.add(time()+msg); + l.notifyLogBufferChanged(); + } + + public static void e(String tag, String msg) { + Log l = getInstance(); + android.util.Log.e(tag, msg); + l.logBuffer.add(time()+msg); + l.notifyLogBufferChanged(); + } + + public static void i(String tag, String msg) { + Log l = getInstance(); + android.util.Log.i(tag, msg); + l.logBuffer.add(time()+msg); + l.notifyLogBufferChanged(); + } + + public static void v(String tag, String msg) { + Log l = getInstance(); + android.util.Log.v(tag, msg); + l.logBuffer.add(time()+msg); + l.notifyLogBufferChanged(); + } + + public static void w(String tag, String msg) { + Log l = getInstance(); + android.util.Log.w(tag, msg); + l.logBuffer.add(time()+msg); + l.notifyLogBufferChanged(); + } + + public static void wtf(String tag, String msg) { + Log l = getInstance(); + android.util.Log.wtf(tag, msg); + l.logBuffer.add(time()+msg); + l.notifyLogBufferChanged(); + } + + public static ArrayList getLogBuffer() { + return getInstance().logBuffer; + } + + private void notifyLogBufferChanged() { + if(observers == null) return; + for(Observer o : observers) { + if(o != null) { + o.update(this, null); + } + } + } + + public static void addLogObserver(Observer observer) { + getInstance().observers.add(observer); + } + + public static void removeLogObserver(Observer o) { + getInstance().observers.remove(o); + } +} diff --git a/app/src/main/res/layout/fragment_debug.xml b/app/src/main/res/layout/fragment_debug.xml index d28eaf35..88d9c0c5 100644 --- a/app/src/main/res/layout/fragment_debug.xml +++ b/app/src/main/res/layout/fragment_debug.xml @@ -17,6 +17,7 @@ android:layout_width="match_parent" android:layout_height="@dimen/activity_vertical_margin" /> + + + + + + + + + + + diff --git a/app/src/main/res/values/strings-about.xml b/app/src/main/res/values/strings-about.xml index 8ff9fb93..f4971f88 100644 --- a/app/src/main/res/values/strings-about.xml +++ b/app/src/main/res/values/strings-about.xml @@ -13,6 +13,7 @@ Application Device Diaspora Pod + Debug Log App Version: %1$s Package Name: %1$s Android Version: %1$s