From 13255a260faaafaf106beb471352b13707ad166c Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 18 Sep 2019 11:52:46 +0300 Subject: [PATCH] Add ability to share logcat file --- .../net/osmand/plus/OsmandApplication.java | 8 +- .../plus/development/LogcatActivity.java | 91 +++++++++++++++++-- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index e08c1b1c5c..ab45459701 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -1009,9 +1009,13 @@ public class OsmandApplication extends MultiDexApplication { } public void sendCrashLog() { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"crash@osmand.net"}); File file = getAppPath(OsmandApplication.EXCEPTION_PATH); + sendCrashLog(file); + } + + public void sendCrashLog(File file) { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"crash@osmand.net"}); intent.putExtra(Intent.EXTRA_STREAM, AndroidUtils.getUriForFile(this, file)); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setType("vnd.android.cursor.dir/email"); diff --git a/OsmAnd/src/net/osmand/plus/development/LogcatActivity.java b/OsmAnd/src/net/osmand/plus/development/LogcatActivity.java index 18c790b765..5b72bbdac7 100644 --- a/OsmAnd/src/net/osmand/plus/development/LogcatActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/LogcatActivity.java @@ -9,7 +9,9 @@ import android.support.v7.app.ActionBar; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.view.Gravity; import android.view.LayoutInflater; +import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -19,11 +21,14 @@ import net.osmand.AndroidUtils; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.activities.OsmandActionBarActivity; +import net.osmand.plus.activities.ActionBarProgressActivity; import org.apache.commons.logging.Log; import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.lang.ref.WeakReference; @@ -33,10 +38,14 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -public class LogcatActivity extends OsmandActionBarActivity { +public class LogcatActivity extends ActionBarProgressActivity { + + public static final String LOGCAT_PATH = "logcat.log"; public static int MAX_BUFFER_LOG = 1000; + private static final int SHARE_ID = 0; + private static final Log log = PlatformUtil.getLog(LogcatActivity.class); private LogcatAsyncTask logcatAsyncTask; @@ -90,6 +99,15 @@ public class LogcatActivity extends OsmandActionBarActivity { stopLogcatAsyncTask(); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItem item = menu.add(0, SHARE_ID, 0, R.string.shared_string_export); + item.setIcon(R.drawable.ic_action_gshare_dark); + item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); + + return super.onCreateOptionsMenu(menu); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); @@ -97,6 +115,9 @@ public class LogcatActivity extends OsmandActionBarActivity { case android.R.id.home: finish(); return true; + case SHARE_ID: + startSaveLogsAsyncTask(); + return true; } return false; @@ -113,12 +134,17 @@ public class LogcatActivity extends OsmandActionBarActivity { }); } - public void startLogcatAsyncTask() { + private void startSaveLogsAsyncTask() { + SaveLogsAsyncTask saveLogsAsyncTask = new SaveLogsAsyncTask(this, logsMap.values()); + saveLogsAsyncTask.execute(); + } + + private void startLogcatAsyncTask() { logcatAsyncTask = new LogcatAsyncTask(this, "*:E"); logcatAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - public void stopLogcatAsyncTask() { + private void stopLogcatAsyncTask() { if (logcatAsyncTask != null && logcatAsyncTask.getStatus() == AsyncTask.Status.RUNNING) { logcatAsyncTask.cancel(false); logcatAsyncTask.stopLogging(); @@ -133,7 +159,8 @@ public class LogcatActivity extends OsmandActionBarActivity { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext()); - View itemView = inflater.inflate(R.layout.bottom_sheet_item_description_long, viewGroup, false); + TextView itemView = (TextView) inflater.inflate(R.layout.bottom_sheet_item_description_long, viewGroup, false); + itemView.setGravity(Gravity.CENTER_VERTICAL); return new LogViewHolder(itemView); } @@ -176,6 +203,58 @@ public class LogcatActivity extends OsmandActionBarActivity { } } + public static class SaveLogsAsyncTask extends AsyncTask { + + private WeakReference logcatActivity; + private Collection logs; + + SaveLogsAsyncTask(LogcatActivity logcatActivity, Collection logs) { + this.logcatActivity = new WeakReference<>(logcatActivity); + this.logs = logs; + } + + @Override + protected void onPreExecute() { + LogcatActivity activity = logcatActivity.get(); + if (activity != null) { + activity.setSupportProgressBarIndeterminateVisibility(true); + } + } + + @Override + protected File doInBackground(Void... voids) { + File file = logcatActivity.get().getMyApplication().getAppPath(LOGCAT_PATH); + try { + if (file.exists()) { + file.delete(); + } + StringBuilder stringBuilder = new StringBuilder(); + for (String log : logs) { + stringBuilder.append(log); + stringBuilder.append("\n"); + } + if (file.getParentFile().canWrite()) { + BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); + writer.write(stringBuilder.toString()); + writer.close(); + } + } catch (Exception e) { + log.error(e); + } + + return file; + } + + @Override + protected void onPostExecute(File file) { + LogcatActivity activity = logcatActivity.get(); + if (activity != null) { + activity.setSupportProgressBarIndeterminateVisibility(false); + activity.getMyApplication().sendCrashLog(file); + } + } + } + public static class LogcatAsyncTask extends AsyncTask { private Process processLogcat; @@ -192,7 +271,7 @@ public class LogcatActivity extends OsmandActionBarActivity { protected Void doInBackground(Void... voids) { try { String filter = String.valueOf(android.os.Process.myPid()); - String[] command = {"logcat", filterLevel, " | grep " + filter, "-T", String.valueOf(MAX_BUFFER_LOG)}; + String[] command = {"logcat", filterLevel, "--pid=" + filter, "-T", String.valueOf(MAX_BUFFER_LOG)}; processLogcat = Runtime.getRuntime().exec(command); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processLogcat.getInputStream()));