diff options
author | steckbrief <steckbrief@chefmail.de> | 2019-07-07 21:21:12 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2019-07-07 21:21:12 +0200 |
commit | 08244e218a3cd08bb1abd1be31776510eee5da6a (patch) | |
tree | b73568cc711268eeaa1b7485c63ec53a2efcbd02 /src/piratx/java/de | |
parent | 77fb7577ee2a637abf6ff151f3b87a273aff7877 (diff) |
implements FS#282: In-App Logcat View; introduces piratx application implementation to access the appcontext in a static way
Diffstat (limited to '')
5 files changed, 211 insertions, 0 deletions
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<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); + } +} 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 + } +} |