From 2fb31bcc3f9e4920891765ab8e0cf2e33227ea3e Mon Sep 17 00:00:00 2001 From: steckbrief Date: Thu, 17 Dec 2015 21:09:18 +0100 Subject: Log error in case logcat command could not be executed; Show toast in case nothing is copied --- .../android/logcat/tasks/ReadLogCatAsyncTask.java | 31 ++++++++++++++++++++-- .../logcat/ui/LogCatOutputCopyOnClickListener.java | 14 +++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'libs') 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 index 1136faf8..e16009ee 100644 --- 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 @@ -9,6 +9,7 @@ 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. @@ -25,7 +26,7 @@ public class ReadLogCatAsyncTask extends AsyncTask { /** * The array adapter to publish the log messages to. */ - private final ArrayAdapter arrayAdapter; + private final LogCatArrayAdapter arrayAdapter; /** * The command to execute logcat. */ @@ -39,7 +40,7 @@ public class ReadLogCatAsyncTask extends AsyncTask { * Initializes the Task with the array adapter to publish the log messages to. * @param arrayAdapter the array adapter */ - public ReadLogCatAsyncTask(ArrayAdapter arrayAdapter) { + public ReadLogCatAsyncTask(LogCatArrayAdapter arrayAdapter) { this.arrayAdapter = arrayAdapter; } @@ -52,15 +53,35 @@ public class ReadLogCatAsyncTask extends AsyncTask { 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 { @@ -68,6 +89,12 @@ public class ReadLogCatAsyncTask extends AsyncTask { } catch (IOException e) { } } + if (null != errorReader) { + try { + errorReader.close(); + } catch (IOException e) { + } + } } logCatOutput.trimToSize(); return logCatOutput.toArray(new String[0]); 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 index 6568a2c5..1be04c9c 100644 --- 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 @@ -8,6 +8,7 @@ import android.widget.Toast; import java.util.ArrayList; +import de.thedevstack.android.logcat.Logging; import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; /** @@ -16,16 +17,19 @@ import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; public class LogCatOutputCopyOnClickListener implements View.OnClickListener { private final LogCatArrayAdapter logCatOutputAdapter; private final Context context; - private final int resIdForToast; + private final int resIdLogcatCopied; + private final int resIdLogcatNotCopied; - public LogCatOutputCopyOnClickListener(Context context, LogCatArrayAdapter logCatOutputAdapter, int resIdForToast) { + public LogCatOutputCopyOnClickListener(Context context, LogCatArrayAdapter logCatOutputAdapter, int resIdLogcatCopied, int resIdLogcatNotCopied) { this.logCatOutputAdapter = logCatOutputAdapter; this.context = context; - this.resIdForToast = resIdForToast; + this.resIdLogcatCopied = resIdLogcatCopied; + this.resIdLogcatNotCopied = resIdLogcatNotCopied; } @Override public void onClick(View v) { + Logging.d("copylogcat", "Start Copying log cat"); ArrayList items = this.logCatOutputAdapter.getItems(); if (null != items && !items.isEmpty()) { StringBuilder sb = new StringBuilder(); @@ -36,7 +40,9 @@ public class LogCatOutputCopyOnClickListener implements View.OnClickListener { 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(); + Toast.makeText(this.context, this.context.getText(this.resIdLogcatCopied), Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(this.context, this.context.getText(this.resIdLogcatNotCopied), Toast.LENGTH_LONG).show(); } } } -- cgit v1.2.3