AutoJsPro

AutoJs : 控制台只显示指定级别的日志

2021-06-23  本文已影响0人  幽默的林先生

使用场景
控制台只显示指定级别的日志

但是日志文件里面显示所有日志

Autojs版本
8.7.7-0

Get知识点
停止其他脚本

导入java类

获取控制台实例

获取日志实例

隐藏控制台输入框和按钮

遍历控制台view

重写控制台adapter

自定义控制台recycleView的布局

日志级别

退出事件

设置控制台大小和位置
  1. 停止其他脚本

  engines.all().map((ScriptEngine) => {

        if (engines.myEngine().toString() !== ScriptEngine.toString()) {

            ScriptEngine.forceStop();

        }

    });

  1. 导入java类
    importClass(Packages.androidx.recyclerview.widget.RecyclerView);
    importClass(Packages.androidx.recyclerview.widget.LinearLayoutManager);
    importClass(android.graphics.drawable.BitmapDrawable);
    importClass(Packages.androidx.recyclerview.widget.DividerItemDecoration);
    importClass(android.graphics.BitmapFactory);
    importClass(android.graphics.Paint);
    importClass(android.graphics.Color);
    importClass(android.widget.LinearLayout);
  1. 获取控制台需要处理的view
let mConsoleView = getConsoleWindow();
let parent = mConsoleView.parent;
var inputView = parent.findViewById(context.getResources().getIdentifier("input", "id", context.getPackageName()));
var buttonView = parent.findViewById(context.getResources().getIdentifier("submit", "id", context.getPackageName()));
var consoleView = parent.findViewById(context.getResources().getIdentifier("console", "id", context.getPackageName()));
  1. 隐藏输入框和按钮
ui.run(function () {
  inputView.setVisibility(8);
  buttonView.setVisibility(8);
});
  1. 获取控制台悬浮窗实例
function getConsoleWindow() {
  var mConsole = runtime.console;
  let field = mConsole.class.superclass.getDeclaredField("mConsoleFloaty");
  field.setAccessible(true);
  mConsoleFloaty = field.get(mConsole);
  mConsoleView = mConsoleFloaty.getExpandedView();
  return mConsoleView;
}
  1. 修改控制台标题
let r = filterView(mConsoleView);
function filterView(view, arr) {
  arr = arr || [];
  if (view instanceof android.view.ViewGroup) {
    arr.push(view);
    let childCount = view.childCount;
    for (var i = 0; i < childCount; i++) {
      let chileView = view.getChildAt(i);
      filterView(chileView, arr);
    }
  } else {
    arr.push(view);
  }
  return arr;
}
  1. 修改控制台标题
r[3].setText("幽默的林先生");
  1. 控制台recycleview的子布局
let boxXml = (
  <TextView
    id="content"
    layout_width="match_parent"
    layout_height="wrap_content"
    textIsSelectable="true"
    textSize="19sp"
    margin="6"
  ></TextView>
);
  1. 重写控制台adapter
ui.run(function () {
  let recycleview = r[num];
  function createAdapter(mLogEntries) {
    return RecyclerView.Adapter({
      onCreateViewHolder: function (parent, viewType) {
        // 视图创建
        let view;
        let holder;
        view = ui.inflate(boxXml, parent, false);
        holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
        return holder;
      },
      onBindViewHolder: function (holder, position) {
        // 数据绑定
        let logEntry = mLogEntries.get(position);
        holder.itemView.setTextColor(colors.parseColor("#0000ff"));
        holder.itemView.setText(logEntry.content);
        let level = 5;
        显示指定级别的日志(logEntry, holder, level);
      },
      getItemCount: function () {
        return mLogEntries.size();
      },
    });
  }
  let newAdapter = createAdapter(mLogEntries);
  recycleview.setAdapter(newAdapter);
});
  1. 日志级别
static final SparseArray<Integer> COLORS = new SparseArrayEntries<Integer>()
.entry(Log.VERBOSE, 0xdfc0c0c0)  
.entry(Log.DEBUG, 0xdfffffff) 
.entry(Log.INFO, 0xff64dd17) 
.entry(Log.WARN, 0xff2962ff)  
.entry(Log.ERROR, 0xffd50000) 
.sparseArray();
上一篇下一篇

猜你喜欢

热点阅读