diff --git a/app/src/main/java/com/linsheng/FATJS/activitys/FloatingButton.java b/app/src/main/java/com/linsheng/FATJS/activitys/FloatingButton.java index c7d9d9a..45ed170 100644 --- a/app/src/main/java/com/linsheng/FATJS/activitys/FloatingButton.java +++ b/app/src/main/java/com/linsheng/FATJS/activitys/FloatingButton.java @@ -1,6 +1,7 @@ package com.linsheng.FATJS.activitys; import static com.linsheng.FATJS.config.GlobalVariableHolder.*; +import static com.linsheng.FATJS.node.AccUtils.isAccessibilityServiceOn; import static com.linsheng.FATJS.node.AccUtils.moveFloatWindow; import static com.linsheng.FATJS.node.AccUtils.printLogMsg; @@ -16,6 +17,7 @@ import android.os.Build; import android.os.Handler; import android.os.IBinder; +import android.provider.Settings; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; @@ -31,6 +33,7 @@ import com.linsheng.FATJS.node.TaskBase; import com.linsheng.FATJS.utils.ExceptionUtil; +import com.linsheng.FATJS.utils.StringUtils; import com.linsheng.FATJS.utils.VibratorUtil; public class FloatingButton extends Service { @@ -39,6 +42,7 @@ public class FloatingButton extends Service { private LinearLayout ll; ViewGroup.LayoutParams txtParameters; ViewGroup.LayoutParams llParameters; + TextView smallCircle2; private final int btn_w = (mWidth / 8); private int btn_h = (mWidth / 8); @@ -187,11 +191,15 @@ private void splitCircles(WindowManager.LayoutParams parameters) { animator1.start(); // 创建第二个小圆 - TextView smallCircle2 = new TextView(context); - if (isStop) { + smallCircle2 = new TextView(context); + if (!isRunning) { smallCircle2.setText("开始"); - } else { - smallCircle2.setText("暂停"); + }else { + if (isStop) { + smallCircle2.setText("开始"); + } else { + smallCircle2.setText("暂停"); + } } smallCircle2.setTextSize((float) (text_size + 2)); smallCircle2.setGravity(Gravity.CENTER); @@ -323,6 +331,9 @@ private void splitCircles(WindowManager.LayoutParams parameters) { printLogMsg("开始运行...", 0); smallCircle2.setText("暂停"); } + }else { + // 开始执行选中的脚本 + testMethodPre(); } }); smallLL3.setOnClickListener((v) -> { @@ -483,6 +494,22 @@ public boolean performClick() { @RequiresApi(api = Build.VERSION_CODES.O) private void testMethodPre() { + if (StringUtils.isEmpty(checkedFileName)) { + printLogMsg("未选中脚本", 0); + Toast.makeText(context, "未选中脚本", Toast.LENGTH_SHORT).show(); + return; + } + if (!checkedFileName.endsWith(".js")) { + printLogMsg(checkedFileName + " is not a js file", 0); + Toast.makeText(context, checkedFileName + " is not a js file", Toast.LENGTH_SHORT).show(); + return; + } + if (!isAccessibilityServiceOn()){ + printLogMsg("请开启无障碍服务", 0); + Toast.makeText(context, "请开启无障碍服务", Toast.LENGTH_SHORT).show(); + mainActivity.startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)); + return; + } // 判断是否有任务正在执行 if (isRunning) { killThread = true; @@ -490,6 +517,8 @@ private void testMethodPre() { Toast.makeText(context, "有任务正在执行", Toast.LENGTH_SHORT).show(); return; } + printLogMsg("开始运行...", 0); + smallCircle2.setText("暂停"); new Thread(() -> { try { printLogMsg("w => " + mWidth + ", h => " + mHeight); @@ -550,8 +579,9 @@ private void btnClick() { private void testMethod() { // 将测试的动作写到这里,点击悬浮窗的 打开 按钮,就可以执行 TaskBase taskDemo = new TaskBase(); - @SuppressLint("SdCardPath") String script_path = "/sdcard/FATJS_DIR/dev_script.js"; - printLogMsg("script_path => " + script_path, 0); + printLogMsg("run script " + checkedFileName, 0); + @SuppressLint("SdCardPath") String script_path = "/sdcard/FATJS_DIR/" + checkedFileName; + // printLogMsg("script_path => " + script_path, 0); taskDemo.initJavet(script_path); } } \ No newline at end of file diff --git a/app/src/main/java/com/linsheng/FATJS/activitys/FloatingWindow.java b/app/src/main/java/com/linsheng/FATJS/activitys/FloatingWindow.java index 6609c45..f638dcb 100644 --- a/app/src/main/java/com/linsheng/FATJS/activitys/FloatingWindow.java +++ b/app/src/main/java/com/linsheng/FATJS/activitys/FloatingWindow.java @@ -97,6 +97,10 @@ public void onReceive(Context context, Intent intent) { wm.updateViewLayout(sv, parameters); windowAutoScroll(); + } else if ("screen_log_clear".equals(intent.getStringExtra("msg"))) { + // 清空悬浮窗日志 + ll.removeAllViews(); + ll.addView(createText("日志面板log")); } else { // 日志相关 GlobalVariableHolder.broadcast_map.put("msg", false); diff --git a/app/src/main/java/com/linsheng/FATJS/aione_editor/ScriptsActivity.java b/app/src/main/java/com/linsheng/FATJS/aione_editor/ScriptsActivity.java index 541ec43..3fecb9e 100644 --- a/app/src/main/java/com/linsheng/FATJS/aione_editor/ScriptsActivity.java +++ b/app/src/main/java/com/linsheng/FATJS/aione_editor/ScriptsActivity.java @@ -11,6 +11,7 @@ import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageButton; +import android.widget.RadioButton; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; diff --git a/app/src/main/java/com/linsheng/FATJS/config/GlobalVariableHolder.java b/app/src/main/java/com/linsheng/FATJS/config/GlobalVariableHolder.java index b013502..0b5708f 100644 --- a/app/src/main/java/com/linsheng/FATJS/config/GlobalVariableHolder.java +++ b/app/src/main/java/com/linsheng/FATJS/config/GlobalVariableHolder.java @@ -24,6 +24,7 @@ public class GlobalVariableHolder { public static MainActivity mainActivity = null; // ll public static LinearLayout ll = null; + public static String checkedFileName = ""; // 当前选中的脚本文件名称 public volatile static HashMap broadcast_map = new HashMap() {{ put("jumpUid", false); diff --git a/app/src/main/java/com/linsheng/FATJS/node/AccUtils.java b/app/src/main/java/com/linsheng/FATJS/node/AccUtils.java index 7420c56..372f407 100644 --- a/app/src/main/java/com/linsheng/FATJS/node/AccUtils.java +++ b/app/src/main/java/com/linsheng/FATJS/node/AccUtils.java @@ -5,18 +5,18 @@ import android.accessibilityservice.AccessibilityService; import android.accessibilityservice.GestureDescription; import android.annotation.SuppressLint; -import android.app.ActivityManager; import android.content.ClipData; import android.content.ClipboardManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Path; import android.graphics.Rect; import android.os.Build; +import android.provider.Settings; +import android.text.TextUtils; import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; @@ -402,6 +402,31 @@ public static float[] getPoint(AccessibilityNodeInfo nodeInfo) { return null; } + // 判断本程序的无障碍服务是否已经开启 + public static Boolean isAccessibilityServiceOn() { + try{ + String packageName = context.getPackageName(); + String service = packageName + "/" + packageName + ".MyAccessibilityService"; + int enabled = Settings.Secure.getInt(GlobalVariableHolder.context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED); + TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':'); + if (enabled == 1) { + String settingValue = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); + if (settingValue != null) { + splitter.setString(settingValue); + while (splitter.hasNext()) { + String accessibilityService = splitter.next(); + if (accessibilityService.equals(service)) { + return true; + } + } + } + } + }catch (Exception ex){ + ex.printStackTrace(); + return false; + } + return false; + } /* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */ /* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */ /* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */ diff --git a/app/src/main/java/com/linsheng/FATJS/node/TaskBase.java b/app/src/main/java/com/linsheng/FATJS/node/TaskBase.java index 7df5c85..2469f36 100644 --- a/app/src/main/java/com/linsheng/FATJS/node/TaskBase.java +++ b/app/src/main/java/com/linsheng/FATJS/node/TaskBase.java @@ -106,7 +106,12 @@ public void _fullScreenLog() { moveFloatWindow("全屏"); }catch (Exception e) {} } - public void _clearLog() {} + public void _clearLog() { + Intent intent = new Intent(); + intent.setAction("com.msg"); + intent.putExtra("msg", "screen_log_clear"); + context.sendBroadcast(intent); + } public void _clip(String text) { clip(text); } diff --git a/app/src/main/java/com/linsheng/FATJS/ui/home/HomeFragment.java b/app/src/main/java/com/linsheng/FATJS/ui/home/HomeFragment.java index 2a82f64..942b7c3 100644 --- a/app/src/main/java/com/linsheng/FATJS/ui/home/HomeFragment.java +++ b/app/src/main/java/com/linsheng/FATJS/ui/home/HomeFragment.java @@ -1,13 +1,11 @@ package com.linsheng.FATJS.ui.home; -import static com.linsheng.FATJS.config.GlobalVariableHolder.PATH; -import static com.linsheng.FATJS.config.GlobalVariableHolder.context; -import static com.linsheng.FATJS.config.GlobalVariableHolder.isRunning; -import static com.linsheng.FATJS.config.GlobalVariableHolder.isStop; -import static com.linsheng.FATJS.config.GlobalVariableHolder.killThread; +import static com.linsheng.FATJS.config.GlobalVariableHolder.*; +import static com.linsheng.FATJS.node.AccUtils.isAccessibilityServiceOn; import static com.linsheng.FATJS.node.AccUtils.moveFloatWindow; import static com.linsheng.FATJS.node.AccUtils.printLogMsg; +import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; @@ -23,6 +21,7 @@ import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageButton; +import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; @@ -51,9 +50,11 @@ public class HomeFragment extends Fragment { private RecyclerView recyclerView; - private RecyclerView.Adapter myRecyclerAdapter; + private MyRecyclerAdapter myRecyclerAdapter; private ArrayList list = new ArrayList<>(); private FragmentHomeBinding binding; + private int checkedPosition = -1; + private boolean __isOpenFloatWin = false; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -70,7 +71,14 @@ public View onCreateView(@NonNull LayoutInflater inflater, recyclerView.setItemAnimator(new DefaultItemAnimator()); myRecyclerAdapter = new MyRecyclerAdapter(list); recyclerView.setAdapter(myRecyclerAdapter); + + printLogMsg("HomeFragment onCreateView", 0); getFileList(1); + // 恢复选中状态 + int checkedPosition = myRecyclerAdapter.getCheckedPosition(); + if (checkedPosition != -1) { + myRecyclerAdapter.notifyItemChanged(checkedPosition); + } final TextView textView = binding.textHome; homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); @@ -82,6 +90,11 @@ public void onStart() { super.onStart(); printLogMsg("HomeFragment onStart", 0); getFileList(0); + // 备份记录悬浮窗是否打开 + __isOpenFloatWin = isOpenFloatWin; + if (DEV_MODE && __isOpenFloatWin) { + moveFloatWindow("隐藏"); + } } public View aboutBtn; @@ -104,6 +117,24 @@ public void btnClick() { startActivity(new Intent(getActivity(), EditorActivity.class)); } + @Override + public void onResume() { + super.onResume(); + printLogMsg("HomeFragment onResume", 0); + } + + @Override + public void onPause() { + super.onPause(); + printLogMsg("HomeFragment onPause", 0); + // 保存选中状态 + int checkedPosition = myRecyclerAdapter.getCheckedPosition(); + myRecyclerAdapter.saveCheckedPosition(checkedPosition); + if (DEV_MODE && __isOpenFloatWin) { + moveFloatWindow("打开"); + } + } + @Override public void onDestroyView() { super.onDestroyView(); @@ -112,13 +143,14 @@ public void onDestroyView() { private class MyRecyclerAdapter extends RecyclerView.Adapter { private ArrayList list; - public class MyViewHolder extends RecyclerView.ViewHolder { + public RadioButton radio_button; public TextView file_name; public ImageButton rename_script, delete_script, run_script; public MyViewHolder(View view) { super(view); + radio_button = view.findViewById(R.id.radio_button); file_name = view.findViewById(R.id.file_name); run_script = view.findViewById(R.id.run_script); delete_script = view.findViewById(R.id.delete_script); @@ -138,9 +170,24 @@ public MyRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int v } @Override - public void onBindViewHolder(MyRecyclerAdapter.MyViewHolder holder, int position) { + public void onBindViewHolder(MyRecyclerAdapter.MyViewHolder holder, @SuppressLint("RecyclerView") int position) { final String name = list.get(position); holder.file_name.setText(name); + + holder.radio_button.setChecked(position == checkedPosition); + holder.radio_button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (checkedPosition != position) { + notifyItemChanged(checkedPosition); + checkedPosition = position; + checkedFileName = name; + notifyItemChanged(checkedPosition); + printLogMsg("已选中 " + checkedFileName, 0); + Toast.makeText(context, "已选中 " + checkedFileName, Toast.LENGTH_SHORT).show(); + } + } + }); holder.file_name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -173,13 +220,24 @@ public void onClick(View view) { }); } + // 保存选中状态 + public void saveCheckedPosition(int position) { + checkedPosition = position; + } + + // 恢复选中状态 + public int getCheckedPosition() { + return checkedPosition; + } + @Override public int getItemCount() { return list.size(); } } - private void getFileList(int show) { + @SuppressLint("NotifyDataSetChanged") + private void getFileList(int show) { // 获取并刷新脚本文件列表 list.clear(); File f = new File(EditorActivity.scripts_path); if (!f.exists()) { @@ -261,6 +319,7 @@ private void deleteScript(String name) { private void runScript(String name) { String script_path = Environment.getExternalStorageDirectory() + PATH + name; if (!isAccessibilityServiceOn()){ + printLogMsg("请开启无障碍服务", 0); Toast.makeText(context, "请开启无障碍服务", Toast.LENGTH_SHORT).show(); startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)); return; @@ -291,32 +350,6 @@ public void run() { thread.start(); } - // 判断本程序的无障碍服务是否已经开启 - public Boolean isAccessibilityServiceOn() { - try{ - String packageName = context.getPackageName(); - String service = packageName + "/" + packageName + ".MyAccessibilityService"; - int enabled = Settings.Secure.getInt(GlobalVariableHolder.context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED); - TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':'); - if (enabled == 1) { - String settingValue = Settings.Secure.getString(GlobalVariableHolder.context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); - if (settingValue != null) { - splitter.setString(settingValue); - while (splitter.hasNext()) { - String accessibilityService = splitter.next(); - if (accessibilityService.equals(service)) { - return true; - } - } - } - } - }catch (Exception ex){ - ex.printStackTrace(); - return false; - } - return false; - } - private void shareScript(final String mpath) { if (mpath == "") return; Intent shareIntent = new Intent(); diff --git a/app/src/main/res/layout/recycler_item.xml b/app/src/main/res/layout/recycler_item.xml index 9064443..51943ac 100644 --- a/app/src/main/res/layout/recycler_item.xml +++ b/app/src/main/res/layout/recycler_item.xml @@ -9,6 +9,14 @@ android:gravity="center" android:orientation="horizontal"> + +