From 08244e218a3cd08bb1abd1be31776510eee5da6a Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sun, 7 Jul 2019 21:21:12 +0200 Subject: implements FS#282: In-App Logcat View; introduces piratx application implementation to access the appcontext in a static way --- build.gradle | 1 + libs/thedevstacklogcat/.gitignore | 1 + libs/thedevstacklogcat/build.gradle | 21 +++ .../android/logcat/ApplicationTest.java | 13 ++ .../thedevstacklogcat/src/main/AndroidManifest.xml | 4 + .../de/thedevstack/android/logcat/Logging.java | 148 +++++++++++++++++++++ .../logcat/adapters/LogCatArrayAdapter.java | 126 ++++++++++++++++++ .../android/logcat/tasks/ReadLogCatAsyncTask.java | 132 ++++++++++++++++++ .../src/main/res/values/strings.xml | 3 + settings.gradle | 1 + src/main/AndroidManifest.xml | 6 +- src/main/java/de/pixart/messenger/Config.java | 2 +- src/main/res/xml/preferences.xml | 3 + .../de/thedevstack/piratx/PiratXApplication.java | 38 ++++++ .../listeners/LogCatOutputCopyOnClickListener.java | 41 ++++++ .../ui/preferences/LogCatOutputActivity.java | 56 ++++++++ .../ui/preferences/LogInformationPreference.java | 29 ++++ .../de/thedevstack/piratx/utils/ClipboardUtil.java | 47 +++++++ src/piratx/res/layout/activity_logcatoutput.xml | 19 +++ src/piratx/res/layout/list_item_logcatoutput.xml | 7 + src/piratx/res/values-de/strings.xml | 10 ++ src/piratx/res/values/strings.xml | 10 ++ 22 files changed, 716 insertions(+), 2 deletions(-) create mode 100644 libs/thedevstacklogcat/.gitignore create mode 100644 libs/thedevstacklogcat/build.gradle create mode 100644 libs/thedevstacklogcat/src/androidTest/java/de/thedevstack/android/logcat/ApplicationTest.java create mode 100644 libs/thedevstacklogcat/src/main/AndroidManifest.xml create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java create mode 100644 libs/thedevstacklogcat/src/main/res/values/strings.xml create mode 100644 src/piratx/java/de/thedevstack/piratx/PiratXApplication.java create mode 100644 src/piratx/java/de/thedevstack/piratx/ui/listeners/LogCatOutputCopyOnClickListener.java create mode 100644 src/piratx/java/de/thedevstack/piratx/ui/preferences/LogCatOutputActivity.java create mode 100644 src/piratx/java/de/thedevstack/piratx/ui/preferences/LogInformationPreference.java create mode 100644 src/piratx/java/de/thedevstack/piratx/utils/ClipboardUtil.java create mode 100644 src/piratx/res/layout/activity_logcatoutput.xml create mode 100644 src/piratx/res/layout/list_item_logcatoutput.xml create mode 100644 src/piratx/res/values-de/strings.xml create mode 100644 src/piratx/res/values/strings.xml diff --git a/build.gradle b/build.gradle index e23dfc0ee..bbb57ba28 100644 --- a/build.gradle +++ b/build.gradle @@ -81,6 +81,7 @@ dependencies { implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0' implementation 'com.google.guava:guava:27.1-android' + implementation project(':libs:thedevstacklogcat') } ext { diff --git a/libs/thedevstacklogcat/.gitignore b/libs/thedevstacklogcat/.gitignore new file mode 100644 index 000000000..796b96d1c --- /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 000000000..e0c2fab53 --- /dev/null +++ b/libs/thedevstacklogcat/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 28 + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + } + } +} + +dependencies { + implementation 'com.android.support:appcompat-v7:28.0.0' +} 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 000000000..2e381ee7b --- /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; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + 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 000000000..594487086 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + 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 000000000..6af7a70e7 --- /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 thedevstack. + * @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 000000000..eb4efc983 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java @@ -0,0 +1,126 @@ +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); + } + + /** + * Returns an unmodifiable copy of the log cat entries. + * @return UnmodifiableList of logcat entries. + */ + public List getItems() { + return Collections.unmodifiableList(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 000000000..e16009ee3 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java @@ -0,0 +1,132 @@ +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; +import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; + +/** + * Task to read the logcat of the App. + * The command logcat -d -v time 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: + *
    + *
  • {@value Logging#LOG_TAG_PREFIX}
  • + *
  • E/ - for every error message
  • + *
  • W/ - for every warning message
  • + *
+ */ +public class ReadLogCatAsyncTask extends AsyncTask { + /** + * The array adapter to publish the log messages to. + */ + private final LogCatArrayAdapter 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(LogCatArrayAdapter 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 logCatOutput = new ArrayList<>(); + BufferedReader bufferedReader = null; + BufferedReader errorReader = null; + try { + Process process = Runtime.getRuntime().exec(ReadLogCatAsyncTask.LOG_CAT_CMD); + bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + logCatOutput.add(line); + } + + String errorLine = ""; + StringBuilder sb = new StringBuilder(); + while ((errorLine = errorReader.readLine()) != null) { + sb.append(errorLine); + sb.append('\n'); + } + int exitValue = process.waitFor(); + + Logging.d("ReadLogCat", "Logcat command returned with exitValue '" + exitValue + "'."); + + String errorMessage = sb.toString(); + if (0 != exitValue && !errorMessage.isEmpty()) { + Logging.e("ReadLogCat", errorMessage); + logCatOutput.add(errorMessage); + } + } catch (IOException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } catch (InterruptedException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } finally { + if (null != bufferedReader) { + try { + bufferedReader.close(); + } catch (IOException e) { + } + } + if (null != errorReader) { + try { + errorReader.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 true if the string should be filtered (removed from the list) false 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/res/values/strings.xml b/libs/thedevstacklogcat/src/main/res/values/strings.xml new file mode 100644 index 000000000..45f5e7fd0 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Copy + diff --git a/settings.gradle b/settings.gradle index 13cce3507..8072becfa 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include ':libs:android-transcoder' include ':libs:xmpp-addr' include ':libs:fullscreenvideoview' +include ':libs:thedevstacklogcat' rootProject.name = 'PiratX' diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 217f90d1e..fb75c6d69 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -48,7 +48,7 @@ android:required="false" /> + diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index 7ead13955..0e2d2b4b5 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -40,7 +40,7 @@ public final class Config { } - public static final String LOGTAG = "Pix-Art_Messenger"; + public static final String LOGTAG = "thedevstack.piratx"; public static final Jid BUG_REPORTS = Jid.of("piratx+bugs@conference.thedevstack.de"); diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index d8dd6c89e..580889d8f 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -465,5 +465,8 @@ android:icon="?attr/ic_settings_about" android:summary="@string/pref_about_conversations_summary" android:title="@string/title_activity_about" /> + diff --git a/src/piratx/java/de/thedevstack/piratx/PiratXApplication.java b/src/piratx/java/de/thedevstack/piratx/PiratXApplication.java new file mode 100644 index 000000000..f4d008d52 --- /dev/null +++ b/src/piratx/java/de/thedevstack/piratx/PiratXApplication.java @@ -0,0 +1,38 @@ +package de.thedevstack.piratx; + +import android.content.Context; +import android.support.multidex.MultiDexApplication; + +/** + * + */ +public class PiratXApplication extends MultiDexApplication { + /** + * Application instance for static access + */ + private static PiratXApplication instance; + + /** + * Initializes the application and saves its instance. + */ + public void onCreate() { + super.onCreate(); + PiratXApplication.instance = this; + } + + /** + * Returns the instance of the application + * @return this application instance + */ + public static PiratXApplication getInstance() { + return PiratXApplication.instance; + } + + /** + * Returns the application's context. + * @return Context the application's context + */ + public static Context getAppContext() { + return PiratXApplication.instance.getApplicationContext(); + } +} diff --git a/src/piratx/java/de/thedevstack/piratx/ui/listeners/LogCatOutputCopyOnClickListener.java b/src/piratx/java/de/thedevstack/piratx/ui/listeners/LogCatOutputCopyOnClickListener.java new file mode 100644 index 000000000..8699535ba --- /dev/null +++ b/src/piratx/java/de/thedevstack/piratx/ui/listeners/LogCatOutputCopyOnClickListener.java @@ -0,0 +1,41 @@ +package de.thedevstack.piratx.ui.listeners; + +import android.view.View; + +import java.util.List; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; +import de.thedevstack.piratx.utils.ClipboardUtil; + +/** + * OnClickListener to copy logcat entries from LogCatArrayAdapter to clipboard. + */ +public class LogCatOutputCopyOnClickListener implements View.OnClickListener { + private final LogCatArrayAdapter logCatOutputAdapter; + + public LogCatOutputCopyOnClickListener(LogCatArrayAdapter logCatOutputAdapter) { + this.logCatOutputAdapter = logCatOutputAdapter; + } + + /** + * Copies the entries of LogCatArrayAdapter separated by a new line to the clipboard. + * + * @param v The view that was clicked. + */ + @Override + public void onClick(View v) { + Logging.d("copylogcat", "Start Copying log cat"); + List items = this.logCatOutputAdapter.getItems(); + String textToCopy = null; + if (null != items && !items.isEmpty()) { + StringBuilder sb = new StringBuilder(); + for (String item : items) { + sb.append(item); + sb.append("\n"); + } + textToCopy = sb.toString(); + } + ClipboardUtil.copyToClipboard("c+logcat", textToCopy); + } +} diff --git a/src/piratx/java/de/thedevstack/piratx/ui/preferences/LogCatOutputActivity.java b/src/piratx/java/de/thedevstack/piratx/ui/preferences/LogCatOutputActivity.java new file mode 100644 index 000000000..13ff6a383 --- /dev/null +++ b/src/piratx/java/de/thedevstack/piratx/ui/preferences/LogCatOutputActivity.java @@ -0,0 +1,56 @@ +package de.thedevstack.piratx.ui.preferences; + +import android.app.Activity; +import android.os.Bundle; +import android.view.ContextMenu; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.ListView; + +import de.pixart.messenger.R; +import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; +import de.thedevstack.android.logcat.tasks.ReadLogCatAsyncTask; +import de.thedevstack.piratx.ui.listeners.LogCatOutputCopyOnClickListener; +import de.thedevstack.piratx.utils.ClipboardUtil; + +/** + * Activity to display the logcat output. + */ +public class LogCatOutputActivity extends Activity { + /** + * List adapter containing the logcat entries. + */ + private LogCatArrayAdapter logCatArrayAdapter; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_logcatoutput); + ListView lv = findViewById(R.id.actLogInfoOutput); + this.logCatArrayAdapter = new LogCatArrayAdapter(this, R.layout.list_item_logcatoutput); + lv.setAdapter(this.logCatArrayAdapter); + new ReadLogCatAsyncTask(this.logCatArrayAdapter).execute(); + Button copyButton = findViewById(R.id.actLogOutputCopyButton); + copyButton.setOnClickListener(new LogCatOutputCopyOnClickListener(this.logCatArrayAdapter)); + registerForContextMenu(lv); + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + menu.add(0, 123456789, 0, R.string.piratx_copy_item); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + if (123456789 == item.getItemId()) { + AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); + String itemText = this.logCatArrayAdapter.getItems().get(info.position); + ClipboardUtil.copyToClipboard(itemText); + return true; + } + return super.onContextItemSelected(item); + } +} diff --git a/src/piratx/java/de/thedevstack/piratx/ui/preferences/LogInformationPreference.java b/src/piratx/java/de/thedevstack/piratx/ui/preferences/LogInformationPreference.java new file mode 100644 index 000000000..6b381d9c2 --- /dev/null +++ b/src/piratx/java/de/thedevstack/piratx/ui/preferences/LogInformationPreference.java @@ -0,0 +1,29 @@ +package de.thedevstack.piratx.ui.preferences; + +import android.content.Context; +import android.content.Intent; +import android.preference.Preference; +import android.util.AttributeSet; + +/** + * + */ +public class LogInformationPreference extends Preference { + public LogInformationPreference(final Context context, final AttributeSet attrs, final int defStyle) { + super(context, attrs, defStyle); + } + + public LogInformationPreference(final Context context, final AttributeSet attrs) { + super(context, attrs); + } + public LogInformationPreference(Context context) { + super(context); + } + + @Override + protected void onClick() { + super.onClick(); + final Intent intent = new Intent(getContext(), LogCatOutputActivity.class); + getContext().startActivity(intent); + } +} diff --git a/src/piratx/java/de/thedevstack/piratx/utils/ClipboardUtil.java b/src/piratx/java/de/thedevstack/piratx/utils/ClipboardUtil.java new file mode 100644 index 000000000..623eeb5ba --- /dev/null +++ b/src/piratx/java/de/thedevstack/piratx/utils/ClipboardUtil.java @@ -0,0 +1,47 @@ +package de.thedevstack.piratx.utils; + +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.widget.Toast; + +import de.pixart.messenger.R; +import de.thedevstack.piratx.PiratXApplication; + +/** + * Util class to work with the Clipboard. + */ +public final class ClipboardUtil { + private static final String CLIPBOARD_LABEL = "piratx+clipboard"; + + /** + * Copies a text to the clipboard. + * @param clipboardLabel the label to show to a user to allow identifying the text in clipboard. + * @param text the text to copy + */ + public static void copyToClipboard(String clipboardLabel, String text) { + Context context = PiratXApplication.getAppContext(); + if (null != text && !text.isEmpty()) { + String label = (null == clipboardLabel) ? CLIPBOARD_LABEL : clipboardLabel; + ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText(label, text); + clipboard.setPrimaryClip(clip); + Toast.makeText(context, R.string.piratx_copied_to_clipboard, Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(context, R.string.piratx_not_copied_to_clipboard_empty, Toast.LENGTH_LONG).show(); + } + + } + + /** + * Copies a text to the clipboard. + * @param text the text to copy + */ + public static void copyToClipboard(String text) { + copyToClipboard(CLIPBOARD_LABEL, text); + } + + private ClipboardUtil() { + // helper class - avoid instantiation + } +} diff --git a/src/piratx/res/layout/activity_logcatoutput.xml b/src/piratx/res/layout/activity_logcatoutput.xml new file mode 100644 index 000000000..76c9227e4 --- /dev/null +++ b/src/piratx/res/layout/activity_logcatoutput.xml @@ -0,0 +1,19 @@ + + + +