From 2531c88f2c8a992aa0c40c9f96e88099dc94139e Mon Sep 17 00:00:00 2001 From: steckbrief Date: Fri, 20 Nov 2015 22:41:30 +0100 Subject: Implements FS#67: Introduce central logging class to use log prefix, new activity to show logcat output and button to copy contents --- .../logcat/adapters/LogCatArrayAdapter.java | 122 +++++++++++++++++++++ .../android/logcat/tasks/ReadLogCatAsyncTask.java | 67 +++++++++++ .../android/logcat/ui/LogCatOutputActivity.java | 27 +++++ .../logcat/ui/LogCatOutputCopyOnClickListener.java | 42 +++++++ 4 files changed, 258 insertions(+) create mode 100644 src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java create mode 100644 src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java create mode 100644 src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputActivity.java create mode 100644 src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputCopyOnClickListener.java (limited to 'src/main/java/de/thedevstack/android/logcat') diff --git a/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java b/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java new file mode 100644 index 00000000..c8a96ea3 --- /dev/null +++ b/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java @@ -0,0 +1,122 @@ +package de.thedevstack.android.logcat.adapters; + +import android.content.Context; +import android.widget.ArrayAdapter; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Created by tzur on 20.11.2015. + */ +public class LogCatArrayAdapter extends ArrayAdapter { + private ArrayList logcatItems = new ArrayList<>(); + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a TextView to use when + */ + public LogCatArrayAdapter(Context context, int resource) { + super(context, resource); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a layout to use when + * instantiating views. + * @param textViewResourceId The id of the TextView within the layout resource to be populated + */ + public LogCatArrayAdapter(Context context, int resource, int textViewResourceId) { + super(context, resource, textViewResourceId); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a TextView to use when + * instantiating views. + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, String[] objects) { + super(context, resource, objects); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a layout to use when + * instantiating views. + * @param textViewResourceId The id of the TextView within the layout resource to be populated + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) { + super(context, resource, textViewResourceId, objects); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a TextView to use when + * instantiating views. + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a layout to use when + * instantiating views. + * @param textViewResourceId The id of the TextView within the layout resource to be populated + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, int textViewResourceId, List objects) { + super(context, resource, textViewResourceId, objects); + } + + @Override + public void add(String object) { + super.add(object); + logcatItems.add(object); + } + + @Override + public void addAll(Collection collection) { + super.addAll(collection); + logcatItems.addAll(collection); + } + + @Override + public void addAll(String... items) { + super.addAll(items); + Collections.addAll(logcatItems, items); + } + + @Override + public void clear() { + super.clear(); + logcatItems.clear(); + } + + @Override + public void remove(String object) { + super.remove(object); + logcatItems.remove(object); + } + + public ArrayList getItems() { + return this.logcatItems; + } +} diff --git a/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java b/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java new file mode 100644 index 00000000..b9fa450b --- /dev/null +++ b/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java @@ -0,0 +1,67 @@ +package de.thedevstack.android.logcat.tasks; + +import android.os.AsyncTask; +import de.thedevstack.conversationsplus.utils.Logging; +import android.widget.ArrayAdapter; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +import de.thedevstack.conversationsplus.utils.StreamUtil; + +/** + * Created by tzur on 20.11.2015. + */ +public class ReadLogCatAsyncTask extends AsyncTask { + private final ArrayAdapter arrayAdapter; + private static final String[] LOG_CAT_CMD = { "logcat", "-d", "-v", "time"}; + private static final String[] WHITE_LIST = { "thedevstack.", "E/", "W/" }; + private static final String[] BLACK_LIST = { "D/TextLayoutCache" }; + + public ReadLogCatAsyncTask(ArrayAdapter arrayAdapter) { + this.arrayAdapter = arrayAdapter; + } + @Override + protected String[] doInBackground(Void... params) { + ArrayList logCatOutput = new ArrayList<>(); + BufferedReader bufferedReader = null; + try { + Process process = Runtime.getRuntime().exec(ReadLogCatAsyncTask.LOG_CAT_CMD); + bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + logCatOutput.add(line); + } + bufferedReader.close(); + } catch (IOException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } finally { + StreamUtil.close(bufferedReader); + } + logCatOutput.trimToSize(); + return logCatOutput.toArray(new String[0]); + } + + @Override + protected void onPostExecute(String[] items) { + this.arrayAdapter.clear(); + if (null != items && items.length > 0) { + for (String item : items) { + if (!filter(item)) { + this.arrayAdapter.add(item); + } + } + } + } + + protected boolean filter(String item) { + for (String whiteListed : ReadLogCatAsyncTask.WHITE_LIST) { + if (item.contains(whiteListed)) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputActivity.java b/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputActivity.java new file mode 100644 index 00000000..bb49c54d --- /dev/null +++ b/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputActivity.java @@ -0,0 +1,27 @@ +package de.thedevstack.android.logcat.ui; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.Button; +import android.widget.ListView; + +import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; +import de.thedevstack.android.logcat.tasks.ReadLogCatAsyncTask; +import de.thedevstack.conversationsplus.R; + +/** + * Created by tzur on 07.10.2015. + */ +public class LogCatOutputActivity extends Activity { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_logcatoutput); + ListView lv = (ListView)findViewById(R.id.actLogInfoOutput); + LogCatArrayAdapter logCatOutputAdapter = new LogCatArrayAdapter(this, R.layout.list_item_logcatoutput); + lv.setAdapter(logCatOutputAdapter); + new ReadLogCatAsyncTask(logCatOutputAdapter).execute(); + Button copyButton = (Button) findViewById(R.id.actLogOutputCopyButton); + copyButton.setOnClickListener(new LogCatOutputCopyOnClickListener(this, logCatOutputAdapter)); + } +} diff --git a/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputCopyOnClickListener.java b/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputCopyOnClickListener.java new file mode 100644 index 00000000..da82fc7f --- /dev/null +++ b/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputCopyOnClickListener.java @@ -0,0 +1,42 @@ +package de.thedevstack.android.logcat.ui; + +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.view.View; +import android.widget.Toast; + +import java.util.ArrayList; + +import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; +import de.thedevstack.conversationsplus.R; + +/** + * Created by tzur on 20.11.2015. + */ +public class LogCatOutputCopyOnClickListener implements View.OnClickListener { + private final LogCatArrayAdapter logCatOutputAdapter; + private final Context context; + + public LogCatOutputCopyOnClickListener(Context context, LogCatArrayAdapter logCatOutputAdapter) { + this.logCatOutputAdapter = logCatOutputAdapter; + this.context = context; + } + + @Override + public void onClick(View v) { + ArrayList items = this.logCatOutputAdapter.getItems(); + if (null != items && !items.isEmpty()) { + StringBuilder sb = new StringBuilder(); + for (String item : items) { + sb.append(item); + sb.append("\n"); + } + ClipboardManager clipboard = (ClipboardManager) ConversationsPlusApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText("c+logcat", sb.toString()); + clipboard.setPrimaryClip(clip); + Toast.makeText(this.context, R.string.cplus_copied_to_clipboard, Toast.LENGTH_LONG).show(); + } + } +} -- cgit v1.2.3