diff options
Diffstat (limited to 'libs/thedevstacklogcat')
11 files changed, 495 insertions, 0 deletions
diff --git a/libs/thedevstacklogcat/.gitignore b/libs/thedevstacklogcat/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/libs/thedevstacklogcat/.gitignore @@ -0,0 +1 @@ +/build diff --git a/libs/thedevstacklogcat/build.gradle b/libs/thedevstacklogcat/build.gradle new file mode 100644 index 00000000..27db48ea --- /dev/null +++ b/libs/thedevstacklogcat/build.gradle @@ -0,0 +1,25 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 22 + buildToolsVersion "22.0.1" + + defaultConfig { + minSdkVersion 14 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:22.2.1' +} diff --git a/libs/thedevstacklogcat/proguard-rules.pro b/libs/thedevstacklogcat/proguard-rules.pro new file mode 100644 index 00000000..077398be --- /dev/null +++ b/libs/thedevstacklogcat/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\tzur\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/libs/thedevstacklogcat/src/androidTest/java/de/thedevstack/android/logcat/ApplicationTest.java b/libs/thedevstacklogcat/src/androidTest/java/de/thedevstack/android/logcat/ApplicationTest.java new file mode 100644 index 00000000..2e381ee7 --- /dev/null +++ b/libs/thedevstacklogcat/src/androidTest/java/de/thedevstack/android/logcat/ApplicationTest.java @@ -0,0 +1,13 @@ +package de.thedevstack.android.logcat; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> + */ +public class ApplicationTest extends ApplicationTestCase<Application> { + public ApplicationTest() { + super(Application.class); + } +}
\ No newline at end of file diff --git a/libs/thedevstacklogcat/src/main/AndroidManifest.xml b/libs/thedevstacklogcat/src/main/AndroidManifest.xml new file mode 100644 index 00000000..59448708 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="de.thedevstack.android.logcat"> + +</manifest> diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java new file mode 100644 index 00000000..6af7a70e --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java @@ -0,0 +1,148 @@ +package de.thedevstack.android.logcat; + +import android.util.Log; + +/** + * Utility class to prefix every log tag. + * This can be used for better filtering in the log cat output activity. + */ +public class Logging { + /** + * The prefix for every log tag. + */ + protected static String LOG_TAG_PREFIX = "thedevstack."; + + /** + * Changes the default log tag prefix. + * The default value is <code>thedevstack.</code> + * @param logTagPrefix the new log tag prefix to use + */ + public static void initLogTagPrefix(String logTagPrefix) { + if (null != logTagPrefix) { + LOG_TAG_PREFIX = logTagPrefix; + } + } + + /** + * Returns the current log tag prefix. + * @return value of Logging.LOG_TAG_PREFIX + */ + public static String getLogTagPrefix() { + return LOG_TAG_PREFIX; + } + + /** + * Send a {@link Log#VERBOSE} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int v(String tag, String msg) { + return Log.v(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#VERBOSE} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int v(String tag, String msg, Throwable tr) { + return Log.v(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /** + * Send a {@link Log#DEBUG} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int d(String tag, String msg) { + return Log.d(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#DEBUG} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int d(String tag, String msg, Throwable tr) { + return Log.d(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /** + * Send an {@link Log#INFO} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int i(String tag, String msg) { + return Log.i(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#INFO} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int i(String tag, String msg, Throwable tr) { + return Log.i(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /** + * Send a {@link Log#WARN} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int w(String tag, String msg) { + return Log.w(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#WARN} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int w(String tag, String msg, Throwable tr) { + return Log.w(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /* + * Send a {@link #WARN} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param tr An exception to log + */ + public static int w(String tag, Throwable tr) { + return Log.w(Logging.LOG_TAG_PREFIX + tag, Log.getStackTraceString(tr)); + } + + /** + * Send an {@link Log#ERROR} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int e(String tag, String msg) { + return Log.e(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#ERROR} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int e(String tag, String msg, Throwable tr) { + return Log.e(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } +} diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java new file mode 100644 index 00000000..c8a96ea3 --- /dev/null +++ b/libs/thedevstacklogcat/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<String> { + private ArrayList<String> 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<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, List<String> objects) { + super(context, resource, textViewResourceId, objects); + } + + @Override + public void add(String object) { + super.add(object); + logcatItems.add(object); + } + + @Override + public void addAll(Collection<? extends String> 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<String> getItems() { + return this.logcatItems; + } +} diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java new file mode 100644 index 00000000..1136faf8 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java @@ -0,0 +1,105 @@ +package de.thedevstack.android.logcat.tasks; + +import android.os.AsyncTask; +import android.widget.ArrayAdapter; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +import de.thedevstack.android.logcat.Logging; + +/** + * Task to read the logcat of the App. + * The command <code>logcat -d -v time</code> is used to load the logs. + * This reader uses a white list to restrict the messages to display, otherwise it might be flooded with useless log messages. + * The white list checks if a log messages contains one of the following strings: + * <ul> + * <li>{@value Logging#LOG_TAG_PREFIX}</li> + * <li><code>E/</code> - for every error message</li> + * <li><code>W/</code> - for every warning message</li> + * </ul> + */ +public class ReadLogCatAsyncTask extends AsyncTask<Void, Void, String[]> { + /** + * The array adapter to publish the log messages to. + */ + private final ArrayAdapter<String> arrayAdapter; + /** + * The command to execute logcat. + */ + private static final String[] LOG_CAT_CMD = { "logcat", "-d", "-v", "time"}; + /** + * The white list to filter log messages. + */ + private static final String[] WHITE_LIST = { Logging.getLogTagPrefix(), "E/", "W/" }; + + /** + * Initializes the Task with the array adapter to publish the log messages to. + * @param arrayAdapter the array adapter + */ + public ReadLogCatAsyncTask(ArrayAdapter<String> arrayAdapter) { + this.arrayAdapter = arrayAdapter; + } + + /** + * Executes the logcat command, reads the output of the command and returns all log messages. + * @param params no params will be passed. (interface compliance) + * @return the array of log messages + */ + @Override + protected String[] doInBackground(Void... params) { + ArrayList<String> 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); + } + } catch (IOException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } finally { + if (null != bufferedReader) { + try { + bufferedReader.close(); + } catch (IOException e) { + } + } + } + logCatOutput.trimToSize(); + return logCatOutput.toArray(new String[0]); + } + + /** + * Clears the array adapter and adds the filtered log messages. + * @param items all log messages + */ + @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); + } + } + } + } + + /** + * Checks whether a log message contains a white listed string or not. + * @param item the item to filter + * @return <code>true</code> if the string should be filtered (removed from the list) <code>false</code> otherwise. + */ + protected boolean filter(String item) { + for (String whiteListed : ReadLogCatAsyncTask.WHITE_LIST) { + if (item.contains(whiteListed)) { + return false; + } + } + return true; + } +} diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputCopyOnClickListener.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/ui/LogCatOutputCopyOnClickListener.java new file mode 100644 index 00000000..6568a2c5 --- /dev/null +++ b/libs/thedevstacklogcat/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; + +/** + * Created by tzur on 20.11.2015. + */ +public class LogCatOutputCopyOnClickListener implements View.OnClickListener { + private final LogCatArrayAdapter logCatOutputAdapter; + private final Context context; + private final int resIdForToast; + + public LogCatOutputCopyOnClickListener(Context context, LogCatArrayAdapter logCatOutputAdapter, int resIdForToast) { + this.logCatOutputAdapter = logCatOutputAdapter; + this.context = context; + this.resIdForToast = resIdForToast; + } + + @Override + public void onClick(View v) { + ArrayList<String> 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) this.context.getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText("c+logcat", sb.toString()); + clipboard.setPrimaryClip(clip); + Toast.makeText(this.context, this.context.getText(this.resIdForToast), Toast.LENGTH_LONG).show(); + } + } +} diff --git a/libs/thedevstacklogcat/src/main/res/values/strings.xml b/libs/thedevstacklogcat/src/main/res/values/strings.xml new file mode 100644 index 00000000..45f5e7fd --- /dev/null +++ b/libs/thedevstacklogcat/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ +<resources> + <string name="thedevstack_logcat_copy">Copy</string> +</resources> diff --git a/libs/thedevstacklogcat/src/test/java/de/thedevstack/android/logcat/ExampleUnitTest.java b/libs/thedevstacklogcat/src/test/java/de/thedevstack/android/logcat/ExampleUnitTest.java new file mode 100644 index 00000000..1d2bc545 --- /dev/null +++ b/libs/thedevstacklogcat/src/test/java/de/thedevstack/android/logcat/ExampleUnitTest.java @@ -0,0 +1,15 @@ +package de.thedevstack.android.logcat; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * To work on unit tests, switch the Test Artifact in the Build Variants view. + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() throws Exception { + assertEquals(4, 2 + 2); + } +}
\ No newline at end of file |