implements FS#282: In-App Logcat View; introduces piratx application implementation to access the appcontext in a static way

This commit is contained in:
steckbrief 2019-07-07 21:21:12 +02:00
parent 77fb7577ee
commit 08244e218a
22 changed files with 716 additions and 2 deletions

View file

@ -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 {

1
libs/thedevstacklogcat/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build

View file

@ -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'
}

View file

@ -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);
}
}

View file

@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.thedevstack.android.logcat">
</manifest>

View file

@ -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));
}
}

View file

@ -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<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);
}
/**
* Returns an unmodifiable copy of the log cat entries.
* @return UnmodifiableList of logcat entries.
*/
public List<String> getItems() {
return Collections.unmodifiableList(this.logcatItems);
}
}

View file

@ -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 <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 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<String> 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 <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;
}
}

View file

@ -0,0 +1,3 @@
<resources>
<string name="thedevstack_logcat_copy">Copy</string>
</resources>

View file

@ -1,4 +1,5 @@
include ':libs:android-transcoder'
include ':libs:xmpp-addr'
include ':libs:fullscreenvideoview'
include ':libs:thedevstacklogcat'
rootProject.name = 'PiratX'

View file

@ -48,7 +48,7 @@
android:required="false" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:name="de.thedevstack.piratx.PiratXApplication"
android:allowBackup="false"
android:appCategory="social"
android:icon="@drawable/ic_launcher"
@ -315,6 +315,10 @@
<activity
android:name=".ui.ChannelDiscoveryActivity"
android:label="@string/discover_channels" />
<activity
android:name="de.thedevstack.piratx.ui.preferences.LogCatOutputActivity"
android:label="@string/piratx_title_activity_loginformation"
android:launchMode="singleTask" />
<service android:name=".services.ExportBackupService" />
<service android:name=".services.ImportBackupService" />

View file

@ -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");

View file

@ -465,5 +465,8 @@
android:icon="?attr/ic_settings_about"
android:summary="@string/pref_about_conversations_summary"
android:title="@string/title_activity_about" />
<de.thedevstack.piratx.ui.preferences.LogInformationPreference
android:summary="@string/pref_show_logcat_summary"
android:title="@string/pref_show_logcat_title"/>
</PreferenceScreen>

View file

@ -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();
}
}

View file

@ -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<String> 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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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
}
}

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/piratx_copy_to_clipboard"
android:id="@+id/actLogOutputCopyButton" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/actLogInfoOutput"
android:scrollbars="vertical"
android:textSize="12sp"
android:textIsSelectable="true"/>
</LinearLayout>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listItemLogcatoutputText"
android:scrollbars="vertical"
android:textSize="12sp"/>

View file

@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<resources>
<string name="pref_show_logcat_title">Zeige logcat-Ausgabe</string>
<string name="pref_show_logcat_summary">Zeigt die Ausgabe von logcat an. Hilfreich für die Fehlersuche.</string>
<string name="piratx_title_activity_loginformation">Logausgaben</string>
<string name="piratx_copy_item">Zeile kopieren</string>
<string name="piratx_copy_to_clipboard">Kopieren</string>
<string name="piratx_copied_to_clipboard">In Zwischenablage kopiert</string>
<string name="piratx_not_copied_to_clipboard_empty">Nichts zu kopieren.</string>
</resources>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="pref_show_logcat_title">Show logcat output</string>
<string name="pref_show_logcat_summary">Shows the output of logcat. This is useful for debugging.</string>
<string name="piratx_title_activity_loginformation">Log information</string>
<string name="piratx_copy_item">Copy item</string>
<string name="piratx_copy_to_clipboard">Copy to clipboard</string>
<string name="piratx_copied_to_clipboard">Copied to clipboard</string>
<string name="piratx_not_copied_to_clipboard_empty">Nothing to copy.</string>
</resources>