Skip to content

Commit

Permalink
1.增加脚本选中功能
Browse files Browse the repository at this point in the history
2.优化DEV_MODE
3.增加清空悬浮窗日志
  • Loading branch information
1754048656 committed Dec 15, 2023
1 parent 72c3353 commit a4f4f93
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 44 deletions.
42 changes: 36 additions & 6 deletions app/src/main/java/com/linsheng/FATJS/activitys/FloatingButton.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -323,6 +331,9 @@ private void splitCircles(WindowManager.LayoutParams parameters) {
printLogMsg("开始运行...", 0);
smallCircle2.setText("暂停");
}
}else {
// 开始执行选中的脚本
testMethodPre();
}
});
smallLL3.setOnClickListener((v) -> {
Expand Down Expand Up @@ -483,13 +494,31 @@ 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;
printLogMsg("有任务正在执行", 0);
Toast.makeText(context, "有任务正在执行", Toast.LENGTH_SHORT).show();
return;
}
printLogMsg("开始运行...", 0);
smallCircle2.setText("暂停");
new Thread(() -> {
try {
printLogMsg("w => " + mWidth + ", h => " + mHeight);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Boolean> broadcast_map = new HashMap<String, Boolean>() {{
put("jumpUid", false);
Expand Down
29 changes: 27 additions & 2 deletions app/src/main/java/com/linsheng/FATJS/node/AccUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
/* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */
/* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */
/* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/linsheng/FATJS/node/TaskBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
103 changes: 68 additions & 35 deletions app/src/main/java/com/linsheng/FATJS/ui/home/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -51,9 +50,11 @@

public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter myRecyclerAdapter;
private MyRecyclerAdapter myRecyclerAdapter;
private ArrayList<String> list = new ArrayList<>();
private FragmentHomeBinding binding;
private int checkedPosition = -1;
private boolean __isOpenFloatWin = false;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -112,13 +143,14 @@ public void onDestroyView() {

private class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private ArrayList<String> 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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit a4f4f93

Please sign in to comment.