Improve logcat activity

This commit is contained in:
Victor Shcherb 2020-01-07 17:27:12 +01:00
parent 59d3c16c6d
commit a8070e7a88
2 changed files with 53 additions and 52 deletions

View file

@ -21,6 +21,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- The next 2 seem necessary only for Android < v4.2 (to initialize BT SCO) --> <!-- The next 2 seem necessary only for Android < v4.2 (to initialize BT SCO) -->
<uses-permission android:name="android.permission.BROADCAST_STICKY" android:maxSdkVersion="18" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="18" /> <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="18" />

View file

@ -1,12 +1,9 @@
package net.osmand.plus.development; package net.osmand.plus.development;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.Gravity; import android.view.Gravity;
@ -17,7 +14,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -34,23 +30,27 @@ import java.io.InputStreamReader;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class LogcatActivity extends ActionBarProgressActivity { public class LogcatActivity extends ActionBarProgressActivity {
public static final String LOGCAT_PATH = "logcat.log"; public static final String LOGCAT_PATH = "logcat.log";
public static int MAX_BUFFER_LOG = 1000;
public static int MAX_BUFFER_LOG = 10000;
private static final int SHARE_ID = 0; private static final int SHARE_ID = 0;
private static final int LEVEL_ID = 1;
private static final Log log = PlatformUtil.getLog(LogcatActivity.class); private static final Log log = PlatformUtil.getLog(LogcatActivity.class);
private LogcatAsyncTask logcatAsyncTask; private LogcatAsyncTask logcatAsyncTask;
private Map<Integer, String> logsMap; private List<String> logs = new ArrayList<>();
private LogcatAdapter adapter; private LogcatAdapter adapter;
private String[] LEVELS = {"D", "I", "W", "E"};
private int filterLevel = 1;
private RecyclerView recyclerView;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -65,26 +65,18 @@ public class LogcatActivity extends ActionBarProgressActivity {
supportActionBar.setElevation(5.0f); supportActionBar.setElevation(5.0f);
} }
logsMap = new LinkedHashMap<Integer, String>() {
@Override
protected boolean removeEldestEntry(Entry<Integer, String> eldest) {
return size() > MAX_BUFFER_LOG;
}
};
adapter = new LogcatAdapter(); adapter = new LogcatAdapter();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
int colorResId = AndroidUtils.resolveAttribute(app, R.attr.divider_color_basic); // int colorResId = AndroidUtils.resolveAttribute(app, R.attr.divider_color_basic);
if (colorResId != 0) { // if (colorResId != 0) {
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); // DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(new ColorDrawable(ContextCompat.getColor(app, colorResId))); // dividerItemDecoration.setDrawable(new ColorDrawable(ContextCompat.getColor(app, colorResId)));
//
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); // recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
} // }
} }
@Override @Override
@ -101,13 +93,24 @@ public class LogcatActivity extends ActionBarProgressActivity {
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(0, SHARE_ID, 0, R.string.shared_string_export); MenuItem share = menu.add(0, SHARE_ID, 0, R.string.shared_string_export);
item.setIcon(R.drawable.ic_action_gshare_dark); share.setIcon(R.drawable.ic_action_gshare_dark);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); share.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItem level = menu.add(0, LEVEL_ID, 0, "");
level.setTitle(getFilterLevel());
level.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu); return super.onCreateOptionsMenu(menu);
} }
@NonNull
private String getFilterLevel() {
return "*:" + LEVELS[this.filterLevel];
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId(); int itemId = item.getItemId();
@ -115,6 +118,17 @@ public class LogcatActivity extends ActionBarProgressActivity {
case android.R.id.home: case android.R.id.home:
finish(); finish();
return true; return true;
case LEVEL_ID:
this.filterLevel++;
if(this.filterLevel >= LEVELS.length) {
this.filterLevel = 0;
}
item.setTitle(getFilterLevel());
stopLogcatAsyncTask();
logs.clear();
adapter.notifyDataSetChanged();
startLogcatAsyncTask();
return true;
case SHARE_ID: case SHARE_ID:
startSaveLogsAsyncTask(); startSaveLogsAsyncTask();
return true; return true;
@ -123,24 +137,14 @@ public class LogcatActivity extends ActionBarProgressActivity {
return false; return false;
} }
private void onNewLogEntry(int index, String logEntry) {
logsMap.put(index, logEntry);
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.setLogs(logsMap.values());
}
});
}
private void startSaveLogsAsyncTask() { private void startSaveLogsAsyncTask() {
SaveLogsAsyncTask saveLogsAsyncTask = new SaveLogsAsyncTask(this, logsMap.values()); SaveLogsAsyncTask saveLogsAsyncTask = new SaveLogsAsyncTask(this, logs);
saveLogsAsyncTask.execute(); saveLogsAsyncTask.execute();
} }
private void startLogcatAsyncTask() { private void startLogcatAsyncTask() {
logcatAsyncTask = new LogcatAsyncTask(this, "*:E"); logcatAsyncTask = new LogcatAsyncTask(this, getFilterLevel());
logcatAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); logcatAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@ -153,8 +157,6 @@ public class LogcatActivity extends ActionBarProgressActivity {
private class LogcatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private class LogcatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> logs = new ArrayList<>();
@NonNull @NonNull
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
@ -184,13 +186,6 @@ public class LogcatActivity extends ActionBarProgressActivity {
return logs.get(position); return logs.get(position);
} }
public void setLogs(Collection<String> logs) {
this.logs.clear();
if (logs != null && !logs.isEmpty()) {
this.logs.addAll(logs);
}
notifyDataSetChanged();
}
private class LogViewHolder extends RecyclerView.ViewHolder { private class LogViewHolder extends RecyclerView.ViewHolder {
@ -260,7 +255,6 @@ public class LogcatActivity extends ActionBarProgressActivity {
private Process processLogcat; private Process processLogcat;
private WeakReference<LogcatActivity> logcatActivity; private WeakReference<LogcatActivity> logcatActivity;
private String filterLevel; private String filterLevel;
private int index = 0;
LogcatAsyncTask(LogcatActivity logcatActivity, String filterLevel) { LogcatAsyncTask(LogcatActivity logcatActivity, String filterLevel) {
this.logcatActivity = new WeakReference<>(logcatActivity); this.logcatActivity = new WeakReference<>(logcatActivity);
@ -277,7 +271,7 @@ public class LogcatActivity extends ActionBarProgressActivity {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processLogcat.getInputStream())); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processLogcat.getInputStream()));
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null && logcatActivity.get() != null) {
if (isCancelled()) { if (isCancelled()) {
break; break;
} }
@ -298,8 +292,14 @@ public class LogcatActivity extends ActionBarProgressActivity {
if (values.length > 0 && !isCancelled()) { if (values.length > 0 && !isCancelled()) {
LogcatActivity activity = logcatActivity.get(); LogcatActivity activity = logcatActivity.get();
if (activity != null) { if (activity != null) {
activity.onNewLogEntry(index, values[0]); boolean autoscroll = !activity.recyclerView.canScrollVertically(1);
index++; for(String s : values) {
activity.logs.add(s);
}
activity.adapter.notifyDataSetChanged();
if(autoscroll) {
activity.recyclerView.scrollToPosition(activity.logs.size() - 1);
}
} }
} }
} }