Skip to content

Commit a4f4f93

Browse files
committed
1.增加脚本选中功能
2.优化DEV_MODE 3.增加清空悬浮窗日志
1 parent 72c3353 commit a4f4f93

File tree

8 files changed

+151
-44
lines changed

8 files changed

+151
-44
lines changed

app/src/main/java/com/linsheng/FATJS/activitys/FloatingButton.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.linsheng.FATJS.activitys;
22

33
import static com.linsheng.FATJS.config.GlobalVariableHolder.*;
4+
import static com.linsheng.FATJS.node.AccUtils.isAccessibilityServiceOn;
45
import static com.linsheng.FATJS.node.AccUtils.moveFloatWindow;
56
import static com.linsheng.FATJS.node.AccUtils.printLogMsg;
67

@@ -16,6 +17,7 @@
1617
import android.os.Build;
1718
import android.os.Handler;
1819
import android.os.IBinder;
20+
import android.provider.Settings;
1921
import android.util.Log;
2022
import android.view.Gravity;
2123
import android.view.MotionEvent;
@@ -31,6 +33,7 @@
3133

3234
import com.linsheng.FATJS.node.TaskBase;
3335
import com.linsheng.FATJS.utils.ExceptionUtil;
36+
import com.linsheng.FATJS.utils.StringUtils;
3437
import com.linsheng.FATJS.utils.VibratorUtil;
3538

3639
public class FloatingButton extends Service {
@@ -39,6 +42,7 @@ public class FloatingButton extends Service {
3942
private LinearLayout ll;
4043
ViewGroup.LayoutParams txtParameters;
4144
ViewGroup.LayoutParams llParameters;
45+
TextView smallCircle2;
4246
private final int btn_w = (mWidth / 8);
4347
private int btn_h = (mWidth / 8);
4448

@@ -187,11 +191,15 @@ private void splitCircles(WindowManager.LayoutParams parameters) {
187191
animator1.start();
188192

189193
// 创建第二个小圆
190-
TextView smallCircle2 = new TextView(context);
191-
if (isStop) {
194+
smallCircle2 = new TextView(context);
195+
if (!isRunning) {
192196
smallCircle2.setText("开始");
193-
} else {
194-
smallCircle2.setText("暂停");
197+
}else {
198+
if (isStop) {
199+
smallCircle2.setText("开始");
200+
} else {
201+
smallCircle2.setText("暂停");
202+
}
195203
}
196204
smallCircle2.setTextSize((float) (text_size + 2));
197205
smallCircle2.setGravity(Gravity.CENTER);
@@ -323,6 +331,9 @@ private void splitCircles(WindowManager.LayoutParams parameters) {
323331
printLogMsg("开始运行...", 0);
324332
smallCircle2.setText("暂停");
325333
}
334+
}else {
335+
// 开始执行选中的脚本
336+
testMethodPre();
326337
}
327338
});
328339
smallLL3.setOnClickListener((v) -> {
@@ -483,13 +494,31 @@ public boolean performClick() {
483494

484495
@RequiresApi(api = Build.VERSION_CODES.O)
485496
private void testMethodPre() {
497+
if (StringUtils.isEmpty(checkedFileName)) {
498+
printLogMsg("未选中脚本", 0);
499+
Toast.makeText(context, "未选中脚本", Toast.LENGTH_SHORT).show();
500+
return;
501+
}
502+
if (!checkedFileName.endsWith(".js")) {
503+
printLogMsg(checkedFileName + " is not a js file", 0);
504+
Toast.makeText(context, checkedFileName + " is not a js file", Toast.LENGTH_SHORT).show();
505+
return;
506+
}
507+
if (!isAccessibilityServiceOn()){
508+
printLogMsg("请开启无障碍服务", 0);
509+
Toast.makeText(context, "请开启无障碍服务", Toast.LENGTH_SHORT).show();
510+
mainActivity.startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
511+
return;
512+
}
486513
// 判断是否有任务正在执行
487514
if (isRunning) {
488515
killThread = true;
489516
printLogMsg("有任务正在执行", 0);
490517
Toast.makeText(context, "有任务正在执行", Toast.LENGTH_SHORT).show();
491518
return;
492519
}
520+
printLogMsg("开始运行...", 0);
521+
smallCircle2.setText("暂停");
493522
new Thread(() -> {
494523
try {
495524
printLogMsg("w => " + mWidth + ", h => " + mHeight);
@@ -550,8 +579,9 @@ private void btnClick() {
550579
private void testMethod() {
551580
// 将测试的动作写到这里,点击悬浮窗的 打开 按钮,就可以执行
552581
TaskBase taskDemo = new TaskBase();
553-
@SuppressLint("SdCardPath") String script_path = "/sdcard/FATJS_DIR/dev_script.js";
554-
printLogMsg("script_path => " + script_path, 0);
582+
printLogMsg("run script " + checkedFileName, 0);
583+
@SuppressLint("SdCardPath") String script_path = "/sdcard/FATJS_DIR/" + checkedFileName;
584+
// printLogMsg("script_path => " + script_path, 0);
555585
taskDemo.initJavet(script_path);
556586
}
557587
}

app/src/main/java/com/linsheng/FATJS/activitys/FloatingWindow.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public void onReceive(Context context, Intent intent) {
9797

9898
wm.updateViewLayout(sv, parameters);
9999
windowAutoScroll();
100+
} else if ("screen_log_clear".equals(intent.getStringExtra("msg"))) {
101+
// 清空悬浮窗日志
102+
ll.removeAllViews();
103+
ll.addView(createText("日志面板log"));
100104
} else {
101105
// 日志相关
102106
GlobalVariableHolder.broadcast_map.put("msg", false);

app/src/main/java/com/linsheng/FATJS/aione_editor/ScriptsActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.view.ViewGroup;
1212
import android.widget.EditText;
1313
import android.widget.ImageButton;
14+
import android.widget.RadioButton;
1415
import android.widget.Spinner;
1516
import android.widget.TextView;
1617
import android.widget.Toast;

app/src/main/java/com/linsheng/FATJS/config/GlobalVariableHolder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class GlobalVariableHolder {
2424
public static MainActivity mainActivity = null;
2525
// ll
2626
public static LinearLayout ll = null;
27+
public static String checkedFileName = ""; // 当前选中的脚本文件名称
2728

2829
public volatile static HashMap<String, Boolean> broadcast_map = new HashMap<String, Boolean>() {{
2930
put("jumpUid", false);

app/src/main/java/com/linsheng/FATJS/node/AccUtils.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
import android.accessibilityservice.AccessibilityService;
66
import android.accessibilityservice.GestureDescription;
77
import android.annotation.SuppressLint;
8-
import android.app.ActivityManager;
98
import android.content.ClipData;
109
import android.content.ClipboardManager;
1110
import android.content.ComponentName;
1211
import android.content.Context;
1312
import android.content.Intent;
14-
import android.content.pm.ActivityInfo;
1513
import android.content.pm.PackageManager;
1614
import android.content.pm.ResolveInfo;
1715
import android.graphics.Path;
1816
import android.graphics.Rect;
1917
import android.os.Build;
18+
import android.provider.Settings;
19+
import android.text.TextUtils;
2020
import android.util.Log;
2121
import android.view.accessibility.AccessibilityEvent;
2222
import android.view.accessibility.AccessibilityNodeInfo;
@@ -402,6 +402,31 @@ public static float[] getPoint(AccessibilityNodeInfo nodeInfo) {
402402
return null;
403403
}
404404

405+
// 判断本程序的无障碍服务是否已经开启
406+
public static Boolean isAccessibilityServiceOn() {
407+
try{
408+
String packageName = context.getPackageName();
409+
String service = packageName + "/" + packageName + ".MyAccessibilityService";
410+
int enabled = Settings.Secure.getInt(GlobalVariableHolder.context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
411+
TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':');
412+
if (enabled == 1) {
413+
String settingValue = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
414+
if (settingValue != null) {
415+
splitter.setString(settingValue);
416+
while (splitter.hasNext()) {
417+
String accessibilityService = splitter.next();
418+
if (accessibilityService.equals(service)) {
419+
return true;
420+
}
421+
}
422+
}
423+
}
424+
}catch (Exception ex){
425+
ex.printStackTrace();
426+
return false;
427+
}
428+
return false;
429+
}
405430
/* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */
406431
/* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */
407432
/* ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇以下方法将逐渐被弃用⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ */

app/src/main/java/com/linsheng/FATJS/node/TaskBase.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ public void _fullScreenLog() {
106106
moveFloatWindow("全屏");
107107
}catch (Exception e) {}
108108
}
109-
public void _clearLog() {}
109+
public void _clearLog() {
110+
Intent intent = new Intent();
111+
intent.setAction("com.msg");
112+
intent.putExtra("msg", "screen_log_clear");
113+
context.sendBroadcast(intent);
114+
}
110115
public void _clip(String text) {
111116
clip(text);
112117
}

app/src/main/java/com/linsheng/FATJS/ui/home/HomeFragment.java

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.linsheng.FATJS.ui.home;
22

3-
import static com.linsheng.FATJS.config.GlobalVariableHolder.PATH;
4-
import static com.linsheng.FATJS.config.GlobalVariableHolder.context;
5-
import static com.linsheng.FATJS.config.GlobalVariableHolder.isRunning;
6-
import static com.linsheng.FATJS.config.GlobalVariableHolder.isStop;
7-
import static com.linsheng.FATJS.config.GlobalVariableHolder.killThread;
3+
import static com.linsheng.FATJS.config.GlobalVariableHolder.*;
4+
import static com.linsheng.FATJS.node.AccUtils.isAccessibilityServiceOn;
85
import static com.linsheng.FATJS.node.AccUtils.moveFloatWindow;
96
import static com.linsheng.FATJS.node.AccUtils.printLogMsg;
107

8+
import android.annotation.SuppressLint;
119
import android.app.AlertDialog;
1210
import android.content.DialogInterface;
1311
import android.content.Intent;
@@ -23,6 +21,7 @@
2321
import android.view.ViewGroup;
2422
import android.widget.EditText;
2523
import android.widget.ImageButton;
24+
import android.widget.RadioButton;
2625
import android.widget.TextView;
2726
import android.widget.Toast;
2827

@@ -51,9 +50,11 @@
5150

5251
public class HomeFragment extends Fragment {
5352
private RecyclerView recyclerView;
54-
private RecyclerView.Adapter myRecyclerAdapter;
53+
private MyRecyclerAdapter myRecyclerAdapter;
5554
private ArrayList<String> list = new ArrayList<>();
5655
private FragmentHomeBinding binding;
56+
private int checkedPosition = -1;
57+
private boolean __isOpenFloatWin = false;
5758

5859
public View onCreateView(@NonNull LayoutInflater inflater,
5960
ViewGroup container, Bundle savedInstanceState) {
@@ -70,7 +71,14 @@ public View onCreateView(@NonNull LayoutInflater inflater,
7071
recyclerView.setItemAnimator(new DefaultItemAnimator());
7172
myRecyclerAdapter = new MyRecyclerAdapter(list);
7273
recyclerView.setAdapter(myRecyclerAdapter);
74+
75+
printLogMsg("HomeFragment onCreateView", 0);
7376
getFileList(1);
77+
// 恢复选中状态
78+
int checkedPosition = myRecyclerAdapter.getCheckedPosition();
79+
if (checkedPosition != -1) {
80+
myRecyclerAdapter.notifyItemChanged(checkedPosition);
81+
}
7482

7583
final TextView textView = binding.textHome;
7684
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
@@ -82,6 +90,11 @@ public void onStart() {
8290
super.onStart();
8391
printLogMsg("HomeFragment onStart", 0);
8492
getFileList(0);
93+
// 备份记录悬浮窗是否打开
94+
__isOpenFloatWin = isOpenFloatWin;
95+
if (DEV_MODE && __isOpenFloatWin) {
96+
moveFloatWindow("隐藏");
97+
}
8598
}
8699

87100
public View aboutBtn;
@@ -104,6 +117,24 @@ public void btnClick() {
104117
startActivity(new Intent(getActivity(), EditorActivity.class));
105118
}
106119

120+
@Override
121+
public void onResume() {
122+
super.onResume();
123+
printLogMsg("HomeFragment onResume", 0);
124+
}
125+
126+
@Override
127+
public void onPause() {
128+
super.onPause();
129+
printLogMsg("HomeFragment onPause", 0);
130+
// 保存选中状态
131+
int checkedPosition = myRecyclerAdapter.getCheckedPosition();
132+
myRecyclerAdapter.saveCheckedPosition(checkedPosition);
133+
if (DEV_MODE && __isOpenFloatWin) {
134+
moveFloatWindow("打开");
135+
}
136+
}
137+
107138
@Override
108139
public void onDestroyView() {
109140
super.onDestroyView();
@@ -112,13 +143,14 @@ public void onDestroyView() {
112143

113144
private class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
114145
private ArrayList<String> list;
115-
116146
public class MyViewHolder extends RecyclerView.ViewHolder {
147+
public RadioButton radio_button;
117148
public TextView file_name;
118149
public ImageButton rename_script, delete_script, run_script;
119150

120151
public MyViewHolder(View view) {
121152
super(view);
153+
radio_button = view.findViewById(R.id.radio_button);
122154
file_name = view.findViewById(R.id.file_name);
123155
run_script = view.findViewById(R.id.run_script);
124156
delete_script = view.findViewById(R.id.delete_script);
@@ -138,9 +170,24 @@ public MyRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int v
138170
}
139171

140172
@Override
141-
public void onBindViewHolder(MyRecyclerAdapter.MyViewHolder holder, int position) {
173+
public void onBindViewHolder(MyRecyclerAdapter.MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
142174
final String name = list.get(position);
143175
holder.file_name.setText(name);
176+
177+
holder.radio_button.setChecked(position == checkedPosition);
178+
holder.radio_button.setOnClickListener(new View.OnClickListener() {
179+
@Override
180+
public void onClick(View view) {
181+
if (checkedPosition != position) {
182+
notifyItemChanged(checkedPosition);
183+
checkedPosition = position;
184+
checkedFileName = name;
185+
notifyItemChanged(checkedPosition);
186+
printLogMsg("已选中 " + checkedFileName, 0);
187+
Toast.makeText(context, "已选中 " + checkedFileName, Toast.LENGTH_SHORT).show();
188+
}
189+
}
190+
});
144191
holder.file_name.setOnClickListener(new View.OnClickListener() {
145192
@Override
146193
public void onClick(View view) {
@@ -173,13 +220,24 @@ public void onClick(View view) {
173220
});
174221
}
175222

223+
// 保存选中状态
224+
public void saveCheckedPosition(int position) {
225+
checkedPosition = position;
226+
}
227+
228+
// 恢复选中状态
229+
public int getCheckedPosition() {
230+
return checkedPosition;
231+
}
232+
176233
@Override
177234
public int getItemCount() {
178235
return list.size();
179236
}
180237
}
181238

182-
private void getFileList(int show) {
239+
@SuppressLint("NotifyDataSetChanged")
240+
private void getFileList(int show) { // 获取并刷新脚本文件列表
183241
list.clear();
184242
File f = new File(EditorActivity.scripts_path);
185243
if (!f.exists()) {
@@ -261,6 +319,7 @@ private void deleteScript(String name) {
261319
private void runScript(String name) {
262320
String script_path = Environment.getExternalStorageDirectory() + PATH + name;
263321
if (!isAccessibilityServiceOn()){
322+
printLogMsg("请开启无障碍服务", 0);
264323
Toast.makeText(context, "请开启无障碍服务", Toast.LENGTH_SHORT).show();
265324
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
266325
return;
@@ -291,32 +350,6 @@ public void run() {
291350
thread.start();
292351
}
293352

294-
// 判断本程序的无障碍服务是否已经开启
295-
public Boolean isAccessibilityServiceOn() {
296-
try{
297-
String packageName = context.getPackageName();
298-
String service = packageName + "/" + packageName + ".MyAccessibilityService";
299-
int enabled = Settings.Secure.getInt(GlobalVariableHolder.context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
300-
TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':');
301-
if (enabled == 1) {
302-
String settingValue = Settings.Secure.getString(GlobalVariableHolder.context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
303-
if (settingValue != null) {
304-
splitter.setString(settingValue);
305-
while (splitter.hasNext()) {
306-
String accessibilityService = splitter.next();
307-
if (accessibilityService.equals(service)) {
308-
return true;
309-
}
310-
}
311-
}
312-
}
313-
}catch (Exception ex){
314-
ex.printStackTrace();
315-
return false;
316-
}
317-
return false;
318-
}
319-
320353
private void shareScript(final String mpath) {
321354
if (mpath == "") return;
322355
Intent shareIntent = new Intent();

0 commit comments

Comments
 (0)