fix: console lagged with too many logs
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.stardust.autojs.runtime.api;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.stardust.autojs.runtime.api.Console;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/21.
|
||||
*/
|
||||
|
||||
public class NoOpConsole implements Console {
|
||||
@Override
|
||||
public void verbose(@Nullable Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(@Nullable Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(int level, Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(@Nullable Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(@Nullable Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(@Nullable Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void assertTrue(boolean value, @Nullable Object data, Object... options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int level, CharSequence charSequence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(CharSequence title) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,24 +2,21 @@ package com.stardust.autojs.runtime.console;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.runtime.console.StardustConsole;
|
||||
import com.stardust.enhancedfloaty.ResizableExpandableFloatyWindow;
|
||||
import com.stardust.util.SparseArrayEntries;
|
||||
|
||||
@@ -31,7 +28,7 @@ import java.util.List;
|
||||
*/
|
||||
public class ConsoleView extends FrameLayout implements StardustConsole.LogListener {
|
||||
|
||||
private static final SparseArray<Integer> COLORS = new SparseArrayEntries<Integer>()
|
||||
static final SparseArray<Integer> COLORS = new SparseArrayEntries<Integer>()
|
||||
.entry(Log.VERBOSE, 0xdfc0c0c0)
|
||||
.entry(Log.DEBUG, 0xdfffffff)
|
||||
.entry(Log.INFO, 0xff64dd17)
|
||||
@@ -40,12 +37,15 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
||||
.entry(Log.ASSERT, 0xffff534e)
|
||||
.sparseArray();
|
||||
|
||||
private static final int REFRESH_INTERVAL = 100;
|
||||
private SparseArray<Integer> mColors = COLORS;
|
||||
private StardustConsole mConsole;
|
||||
private TextView mTextView;
|
||||
private RecyclerView mLogListRecyclerView;
|
||||
private EditText mEditText;
|
||||
private ResizableExpandableFloatyWindow mWindow;
|
||||
private LinearLayout mInputContainer;
|
||||
private ScrollView mContentContainer;
|
||||
private boolean mShouldStopRefresh = false;
|
||||
private ArrayList<StardustConsole.Log> mLogs = new ArrayList<>();
|
||||
|
||||
public ConsoleView(Context context) {
|
||||
super(context);
|
||||
@@ -62,11 +62,16 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
||||
init();
|
||||
}
|
||||
|
||||
public void setColors(SparseArray<Integer> colors) {
|
||||
mColors = colors;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
inflate(getContext(), R.layout.console_view, this);
|
||||
mTextView = (TextView) findViewById(R.id.content);
|
||||
mTextView.setMovementMethod(new ScrollingMovementMethod());
|
||||
mContentContainer = (ScrollView) findViewById(R.id.content_container);
|
||||
mLogListRecyclerView = (RecyclerView) findViewById(R.id.log_list);
|
||||
LinearLayoutManager manager = new LinearLayoutManager(getContext());
|
||||
mLogListRecyclerView.setLayoutManager(manager);
|
||||
mLogListRecyclerView.setAdapter(new Adapter());
|
||||
initEditText();
|
||||
initSubmitButton();
|
||||
}
|
||||
@@ -115,68 +120,63 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
||||
|
||||
@Override
|
||||
public void onNewLog(StardustConsole.Log log) {
|
||||
log(log.level, log.content);
|
||||
|
||||
}
|
||||
|
||||
private void log(int level, CharSequence log) {
|
||||
int color = getColorForLevel(level);
|
||||
final Spannable spannable = buildColorSpannable(log, color);
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
mShouldStopRefresh = false;
|
||||
postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mTextView.append(spannable);
|
||||
mContentContainer.fullScroll(View.FOCUS_DOWN);
|
||||
mEditText.requestFocus();
|
||||
refreshLog();
|
||||
if (!mShouldStopRefresh) {
|
||||
postDelayed(this, REFRESH_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}, REFRESH_INTERVAL);
|
||||
}
|
||||
|
||||
private Spannable buildColorSpannable(CharSequence log, int color) {
|
||||
Spannable spannable = new SpannableString(log);
|
||||
spannable.setSpan(new ForegroundColorSpan(color), 0, log.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||
return spannable;
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
mShouldStopRefresh = true;
|
||||
}
|
||||
|
||||
private int getColorForLevel(int level) {
|
||||
return COLORS.get(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogClear() {
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mTextView.setText("");
|
||||
mLogs.clear();
|
||||
mLogListRecyclerView.getAdapter().notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onWindowVisibilityChanged(int visibility) {
|
||||
super.onWindowVisibilityChanged(visibility);
|
||||
if (visibility == VISIBLE) {
|
||||
refreshLog();
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshLog() {
|
||||
if (mConsole != null) {
|
||||
mTextView.setText("");
|
||||
final List<StardustConsole.Log> logs = new ArrayList<>(mConsole.getLogs());
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (StardustConsole.Log log : logs) {
|
||||
int color = getColorForLevel(log.level);
|
||||
final Spannable spannable = buildColorSpannable(log.content, color);
|
||||
mTextView.append(spannable);
|
||||
}
|
||||
mContentContainer.fullScroll(View.FOCUS_DOWN);
|
||||
mEditText.requestFocus();
|
||||
}
|
||||
});
|
||||
if (mConsole == null)
|
||||
return;
|
||||
int oldSize = mLogs.size();
|
||||
ArrayList<StardustConsole.Log> logs = mConsole.getAllLogs();
|
||||
final int size = logs.size();
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
if (oldSize >= size) {
|
||||
return;
|
||||
}
|
||||
if (oldSize == 0) {
|
||||
mLogs.addAll(logs);
|
||||
} else {
|
||||
for (int i = oldSize; i < size; i++) {
|
||||
mLogs.add(logs.get(i));
|
||||
}
|
||||
}
|
||||
mLogListRecyclerView.getAdapter().notifyItemRangeInserted(oldSize, size - 1);
|
||||
mLogListRecyclerView.scrollToPosition(size - 1);
|
||||
}
|
||||
|
||||
public void setWindow(ResizableExpandableFloatyWindow window) {
|
||||
@@ -193,4 +193,34 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
TextView textView;
|
||||
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
textView = (TextView) itemView;
|
||||
}
|
||||
}
|
||||
|
||||
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.console_view_item, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
StardustConsole.Log log = mLogs.get(position);
|
||||
holder.textView.setText(log.content);
|
||||
holder.textView.setTextColor(mColors.get(log.level));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mLogs.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.stardust.autojs.runtime.console;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.stardust.autojs.runtime.api.Console;
|
||||
import com.stardust.util.UiHandler;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/10/22.
|
||||
*/
|
||||
|
||||
public class GlobalStardustConsole extends StardustConsole {
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault());
|
||||
|
||||
private final SparseArray<Integer> mColors;
|
||||
|
||||
public GlobalStardustConsole(UiHandler uiHandler) {
|
||||
super(uiHandler);
|
||||
mColors = ConsoleView.COLORS.clone();
|
||||
mColors.put(android.util.Log.DEBUG, 0xcc000000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setConsoleView(ConsoleView consoleView) {
|
||||
super.setConsoleView(consoleView);
|
||||
consoleView.setColors(mColors);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int level, CharSequence charSequence) {
|
||||
charSequence = String.format(Locale.getDefault(), "%s/%s: %s",
|
||||
DATE_FORMAT.format(new Date()), getLevelChar(level), charSequence.toString());
|
||||
super.println(level, charSequence);
|
||||
}
|
||||
|
||||
private String getLevelChar(int level) {
|
||||
switch (level) {
|
||||
case android.util.Log.VERBOSE:
|
||||
return "V";
|
||||
case android.util.Log.DEBUG:
|
||||
return "D";
|
||||
case android.util.Log.INFO:
|
||||
return "I";
|
||||
case android.util.Log.WARN:
|
||||
return "W";
|
||||
case android.util.Log.ERROR:
|
||||
return "E";
|
||||
case android.util.Log.ASSERT:
|
||||
return "A";
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,19 +5,22 @@ import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.annotation.ScriptInterface;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.runtime.api.AbstractConsole;
|
||||
import com.stardust.autojs.runtime.api.Console;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.ResizableExpandableFloatyWindow;
|
||||
import com.stardust.util.UiHandler;
|
||||
import com.stardust.autojs.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import ezy.assist.compat.SettingsCompat;
|
||||
|
||||
@@ -27,15 +30,30 @@ import ezy.assist.compat.SettingsCompat;
|
||||
|
||||
public class StardustConsole extends AbstractConsole {
|
||||
|
||||
public static class Log {
|
||||
public static class Log implements Comparable<Log> {
|
||||
|
||||
public int id;
|
||||
public int level;
|
||||
public CharSequence content;
|
||||
public boolean newLine = false;
|
||||
|
||||
public Log(int level, CharSequence content) {
|
||||
public Log(int id, int level, CharSequence content) {
|
||||
this.id = id;
|
||||
this.level = level;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Log(int id, int level, CharSequence content, boolean newLine) {
|
||||
this.id = id;
|
||||
this.level = level;
|
||||
this.content = content;
|
||||
this.newLine = newLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NonNull Log o) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public interface LogListener {
|
||||
@@ -45,7 +63,8 @@ public class StardustConsole extends AbstractConsole {
|
||||
}
|
||||
|
||||
private final Console mGlobalConsole;
|
||||
private List<Log> mLogs = new ArrayList<>();
|
||||
private final ArrayList<Log> mLogs = new ArrayList<>();
|
||||
private AtomicInteger mIdCounter = new AtomicInteger(0);
|
||||
private ResizableExpandableFloatyWindow mFloatyWindow;
|
||||
private ConsoleFloaty mConsoleFloaty;
|
||||
private LogListener mLogListener;
|
||||
@@ -83,13 +102,13 @@ public class StardustConsole extends AbstractConsole {
|
||||
mLogListener = logListener;
|
||||
}
|
||||
|
||||
public List<Log> getLogs() {
|
||||
public ArrayList<Log> getAllLogs() {
|
||||
return mLogs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int level, CharSequence charSequence) {
|
||||
Log log = new Log(level, charSequence + "\n");
|
||||
Log log = new Log(mIdCounter.getAndIncrement(), level, charSequence, true);
|
||||
mLogs.add(log);
|
||||
if (mGlobalConsole != null) {
|
||||
mGlobalConsole.println(level, charSequence);
|
||||
@@ -102,7 +121,7 @@ public class StardustConsole extends AbstractConsole {
|
||||
|
||||
@Override
|
||||
public void write(int level, CharSequence charSequence) {
|
||||
Log log = new Log(level, charSequence);
|
||||
Log log = new Log(mIdCounter.getAndIncrement(), level, charSequence);
|
||||
mLogs.add(log);
|
||||
if (mGlobalConsole != null) {
|
||||
mGlobalConsole.print(level, charSequence);
|
||||
|
||||
@@ -6,22 +6,11 @@
|
||||
android:animateLayoutChanges="true"
|
||||
android:orientation="vertical">
|
||||
|
||||
<ScrollView
|
||||
android:id="@+id/content_container"
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/log_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:paddingLeft="3dp"
|
||||
android:textIsSelectable="true"
|
||||
android:textSize="14sp"
|
||||
tools:text="12345\n678\n9\n0"/>
|
||||
</ScrollView>
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/input_container"
|
||||
|
||||
7
autojs/src/main/res/layout/console_view_item.xml
Normal file
7
autojs/src/main/res/layout/console_view_item.xml
Normal 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="wrap_content"
|
||||
android:textSize="15sp">
|
||||
|
||||
</TextView>
|
||||
Reference in New Issue
Block a user