11package 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 ;
85import static com .linsheng .FATJS .node .AccUtils .moveFloatWindow ;
96import static com .linsheng .FATJS .node .AccUtils .printLogMsg ;
107
8+ import android .annotation .SuppressLint ;
119import android .app .AlertDialog ;
1210import android .content .DialogInterface ;
1311import android .content .Intent ;
2321import android .view .ViewGroup ;
2422import android .widget .EditText ;
2523import android .widget .ImageButton ;
24+ import android .widget .RadioButton ;
2625import android .widget .TextView ;
2726import android .widget .Toast ;
2827
5150
5251public 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