hjw
2023-04-20 b2dd8ca6ea52926382dabd0914efcc6b552267d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package com.jwipc.nodka_reboot_under.utils;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.spirit.AlarmPowerItem;
import android.hardware.spirit.AlarmPowerManager;
import android.jwipc.spirit.SpiritAlarmManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.os.Build;
import android.os.PowerManager;
import android.os.RemoteException;
import android.text.format.DateFormat;
import android.util.Log;
 
import com.jwipc.nodka_reboot_under.MyApplication;
import com.nodka.api.NodkaAPI;
import com.nodka.api.NodkaNative;
 
 
public class Utils {
 
   Context context = null;
   PowerManager pm;
   
   SharedPreferences sharedPreferences;
   Editor editor;
   
   public String shapre = "shapre";
   
   long watch_dog_timer = 10 * 60 * 1000;
   long watch_dog_timer_STOP = 30 * 60 * 1000;
   
   String get_broadcast_time = "get_broadcast_time";
   String get_broadcast_ALIVE_or_STOP = "get_broadcast_ALIVE_or_STOP";
   
   public String set_time_reboot_h = "set_time_reboot_h";
   public String set_time_reboot_m = "set_time_reboot_m";
   public String set_time_reboot_s = "set_time_reboot_s";
   public String set_time_reboot_done = "set_time_reboot_done";
   public String set_time_reboot_repeat = "set_time_reboot_repeat";
   public String repeat_alarm_poweron_time = "repeat_alarm_poweron_time";
   public String repeat_alarm_poweroff_time = "repeat_alarm_poweroff_time";
   
   public final String MCUEnableWdt = "/sys/class/minix-mcu/MCUEnableWdt";
   public final String MCUWdtFeed = "/sys/class/minix-mcu/MCUWdtFeed";
   public final String MCUWdtTimeout = "/sys/class/minix-mcu/MCUWdtTimeout";
   
   public static boolean is_set_time_reboot = true;
   
   String is_android_watchdog = "is_android_watchdog";
   String is_check_poweroff_alarm_repeat = "is_check_poweroff_alarm_repeat";
   
   Thread thread_WatchDog = null;
   Thread thread_set_time_reboot = null;
   Thread thread_STOP_WATCH_DOG = null;
   Thread thread_AlarmPower_Repeat_Poweroff = null;
 
   
 
   public Utils(Context context) {
       super();
       this.context = context;
       
       pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
       
       sharedPreferences = context.getSharedPreferences(shapre, context.MODE_PRIVATE);
       editor = sharedPreferences.edit();
   }
   
   public void shut_Down()
   {
       try {
           Class<?> ServiceManager = Class.forName("android.os.ServiceManager");
           Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
           Object oRemoteService = getService.invoke(null,Context.POWER_SERVICE);
           Class<?> cStub = Class.forName("android.os.IPowerManager$Stub");
           Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
           Object oIPowerManager = asInterface.invoke(null, oRemoteService);
           
           if(android.os.Build.VERSION.SDK_INT < 24)
           {
               Method shutdown =  oIPowerManager.getClass().getMethod("shutdown",boolean.class,boolean.class);
               shutdown.invoke(oIPowerManager,false,true);
           }
           else
           {
               Method shutdown =  oIPowerManager.getClass().getMethod("shutdown",boolean.class,java.lang.String.class,boolean.class);
               shutdown.invoke(oIPowerManager,false,null,true);
           }
       } catch (ClassNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (NoSuchMethodException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IllegalArgumentException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (InvocationTargetException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } 
   }
   
   
   public void reboot()
   {
       pm.reboot(null);
   }
   
   
   public void thread_WatchDog()
   {
       
       if(thread_WatchDog == null)
       {
           thread_WatchDog = new Thread(new Runnable() {
               
               @Override
               public void run() {
                   // TODO Auto-generated method stub
                   
                   while (true) {
                       
                       if(sharedPreferences.getString(get_broadcast_ALIVE_or_STOP, "").equals("ALIVE"))
                       {
                           if(System.currentTimeMillis() - sharedPreferences.getLong(get_broadcast_time, 0) > watch_dog_timer)
                           {
                               thread_STOP_APK_FEED();
                           }
                       }
                       else if(sharedPreferences.getString(get_broadcast_ALIVE_or_STOP, "").equals("STOP"))
                       {
                           if(System.currentTimeMillis() - sharedPreferences.getLong(get_broadcast_time, 0) > watch_dog_timer_STOP)
                           {
                               thread_STOP_APK_FEED();
                           }
                       }
                       
                       
                       
                       try {
                           Thread.sleep(1000);
                       } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }
                       
                   }
                   
               }
           });
       }
       
       
       if(!thread_WatchDog.isAlive() || thread_WatchDog.isInterrupted())
       {
           thread_WatchDog.start();
       }
   }
   
   
   public void set_time_reboot(String power_off_time, boolean active, String repeat)
   {
       if(!active)
       {
           is_set_time_reboot = false;
           
           if(thread_set_time_reboot != null)
           {
               thread_set_time_reboot.interrupt();
               thread_set_time_reboot = null;
           }
           
           editor.clear();
           editor.commit();
           
           return;
       }
       
       is_set_time_reboot = true;
       
       final int[] hms_time = string_to_time(power_off_time);
       
       if(hms_time.length == 3)
       {
           if(sharedPreferences.getInt(set_time_reboot_h, -1) == hms_time[0])
           {
               if(sharedPreferences.getInt(set_time_reboot_m, -1) == hms_time[1])
               {
                   if(sharedPreferences.getInt(set_time_reboot_s, -1) == hms_time[2])
                   {
                       return;
                   }
               }
           }
           
           editor.putBoolean(set_time_reboot_done, false);
           editor.putInt(set_time_reboot_h, hms_time[0]);
           editor.putInt(set_time_reboot_m, hms_time[1]);
           editor.putInt(set_time_reboot_s, hms_time[2]);
           editor.putString(set_time_reboot_repeat, repeat);
           editor.commit();
           
           set_time_reboot_by_xml();
       }
       else if(!repeat.equals(""))
       {
           if(sharedPreferences.getInt(set_time_reboot_h, 0) != 0)
           {
               editor.putString(set_time_reboot_repeat, repeat);
               editor.commit();
           }
       }
       
   }
   
   
   public void set_alarmPower(String power_off_time, String power_on_time, boolean active, String repeat)
   {
       final int[] hms_off = string_to_time(power_off_time);
       
       if(hms_off.length != 3)
       {
           return;
       }
       
       final int[] hms_on = string_to_time(power_on_time);
       
       if(hms_on.length != 3)
       {
           return;
       }
       
       if (isSpiritVersion()) {
           new AlarmPowerManager().removeAllalarmPower();
           setFileState("0", "/sys/class/minix-rtc/MCURTCDATA");
       } else {
           MyApplication.getApplication().getNodkaAPI().alarm_removeAllalarmPower();
           setFileState("0", "/sys/class/minix-rtc/minix_RTC_DATA");
       }
 
       editor.putString(repeat_alarm_poweron_time, "");
       editor.putString(repeat_alarm_poweroff_time, "");
       editor.commit();
 
       if(active)
       {
           if(repeat.equals("true"))
           {
               if (isSpiritVersion()) {
                   new SpiritAlarmManager().setSpiritAlarmWithDay(1, hms_on[0], hms_on[1]);
                   setFileState("1", "/sys/class/minix-rtc/MCURTCDATA");
               } else {
                   MyApplication.getApplication().getNodkaAPI().alarm_setSpiritAlarmWithDay(1, hms_on[0], hms_on[1]);
                   setFileState("1", "/sys/class/minix-rtc/minix_RTC_DATA");
               }
 
               editor.putString(repeat_alarm_poweron_time, hms_on[0]+":"+hms_on[1]+":"+0);
               editor.putString(repeat_alarm_poweroff_time, hms_off[0]+":"+hms_off[1]+":"+0);
               editor.commit();
               
               thread_AlarmPower_Repeat_Poweroff();
               
           }
           else
           {
               editor.putString(repeat_alarm_poweron_time, "");
               editor.putString(repeat_alarm_poweroff_time, "");
               editor.commit();
               
               Calendar calendar = Calendar.getInstance();
 
               if (isSpiritVersion()) {
                   // 改接口 setSpiritAlarmWithDay()
                   new AlarmPowerManager().CreatOneAlarmPower(calendar.get(Calendar.YEAR)+"",
                           calendar.get(Calendar.MONTH)+1+"",
                           calendar.get(Calendar.DAY_OF_MONTH)+"",
                           hms_on[0]+"",
                           hms_on[1]+"",
                           hms_on[2]+"",
                           hms_off[0]+"",
                           hms_off[1]+"",
                           hms_off[2]+"",
                           hms_on[0]>12? "0":"1",
                           hms_off[0]>12? "0":"1");
 
                   new AlarmPowerManager().updateAlarmPowerActive(0, active+"");
               } else {
                   MyApplication.getApplication().getNodkaAPI().alarm_creatOneAlarmPower(calendar.get(Calendar.YEAR)+"",
                           calendar.get(Calendar.MONTH)+1+"",
                           calendar.get(Calendar.DAY_OF_MONTH)+"",
                           hms_on[0]+"",
                           hms_on[1]+"",
                           hms_on[2]+"",
                           hms_off[0]+"",
                           hms_off[1]+"",
                           hms_off[2]+"",
                           hms_on[0]>12? "0":"1",
                           hms_off[0]>12? "0":"1");
 
                   MyApplication.getApplication().getNodkaAPI().alarm_updateAlarmPowerActive(0, active+"");
               }
           }
       }
   }
   
   
   private int[] string_to_time(String string)
   {
       if(string.contains(":"))
       {
           String[] hms = string.split(":");
           
           if(hms.length == 3)
           {
               int[] time = new int[3];
               
               time[0] = new Integer(hms[0]);
               time[1] = new Integer(hms[1]);
               time[2] = new Integer(hms[2]);
               
               return time;
           }
       }
       
       int[] tt = new int[1];        
       return tt;
   }
   
   
   public boolean get_time_reboot_isdone()
   {
       boolean isdone = false;
       
       isdone = sharedPreferences.getBoolean(set_time_reboot_done, false);
       
       return isdone;
   }
   
   
   public boolean get_time_reboot_isRepeat()
   {
       boolean isRepeat = false;
       
       isRepeat = sharedPreferences.getString(set_time_reboot_repeat, "false").equals("true");
       
       return isRepeat;
   }
   
   
   public void set_time_reboot_by_xml()
   {
       if(thread_set_time_reboot == null)
       {
           thread_set_time_reboot = new Thread(new Runnable() {
               
               @Override
               public void run() {
                   // TODO Auto-generated method stub
                   
                   while(is_set_time_reboot)
                   {
                       int now_hour = -1, now_minute = -1, now_second = -1;
                       
                       Calendar calendar = Calendar.getInstance();
                       
                       now_minute = calendar.get(Calendar.MINUTE);
                       now_second = calendar.get(Calendar.SECOND);
                       now_hour = calendar.get(Calendar.HOUR_OF_DAY);
                       
                       
                       if(now_hour == sharedPreferences.getInt(set_time_reboot_h, -1) && 
                           now_minute == sharedPreferences.getInt(set_time_reboot_m, -1) && 
                           now_second == sharedPreferences.getInt(set_time_reboot_s, -1) )
                       {
                           editor.putBoolean(set_time_reboot_done, true);
                           
                           if(sharedPreferences.getString(set_time_reboot_repeat, "false").equals("false"))
                           {
                               editor.putInt(set_time_reboot_h, -1);
                               editor.putInt(set_time_reboot_m, -1);
                               editor.putInt(set_time_reboot_s, -1);
                           }
                           
                           editor.commit();
                           
                           exe_cmd("reboot");
                       }
                       
                       try {
                           Thread.sleep(800);
                       } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }
                   }
               }
           });
       }
       
       if(!thread_set_time_reboot.isAlive() || thread_set_time_reboot.isInterrupted())
       {
           thread_set_time_reboot.start();
       }
   }
   
   
   public Boolean isServiceRunning(Context context, String ServiceName) {
        if (("").equals(ServiceName) || ServiceName == null)
            return false;
        
        ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager.getRunningServices(Integer.MAX_VALUE);
        
        for (int i = 0; i < runningService.size(); i++) 
        {
            if (runningService.get(i).service.getClassName().toString().equals(ServiceName)) 
            {
                return true;
            }
        }
        return false;
    }
   
   
   public void set_broadcast_time(long time)
   {
       if(editor != null)
       {
           editor.putLong(get_broadcast_time, time);
           editor.commit();
       }
   }
   
   
   public void set_NDJ_APP_ALIVE_or_STOP(String intent_action)
   {
       if(editor != null)
       {
           if(intent_action.equals("android.intent.action.NDJ_APP_ALIVE"))
           {
               editor.putString(get_broadcast_ALIVE_or_STOP, "ALIVE");
               editor.commit();
           }
           else if(intent_action.equals("android.intent.action.NDJ_APP_STOP"))
           {
               editor.putString(get_broadcast_ALIVE_or_STOP, "STOP");
               editor.commit();
           }
           
       }
   }
   
   
   public String get_String_From_SharedPreferences(String key)
   {
       String s = sharedPreferences.getString(key, "");
       
       return s;
   }
   
   
   public void remove_String_From_SharedPreferences(String key)
   {
       editor.putString(key, "");
       editor.commit();
   }
   
   
   public void remove_int_From_SharedPreferences(String key)
   {
       editor.putInt(key, -1);
       editor.commit();
   }
   
   
   public String get_int_From_SharedPreferences(String key)
   {
       String s = "";
       int a = sharedPreferences.getInt(key, -1);
       
       if(a == -1)
           s = "";
       else
           s = a+"";
       
       return s;
   }
   
   
   public void stop_watch_dog_apk_feed()
   {
       if(new File(MCUEnableWdt).exists())
           setFileState("2", MCUEnableWdt);
       
       editor.putBoolean(is_android_watchdog, true);
       editor.commit();
       
       if(thread_STOP_WATCH_DOG == null)
       {
           thread_STOP_WATCH_DOG = new Thread(new Runnable() {
               
               @Override
               public void run() {
                   // TODO Auto-generated method stub
                   
                   while (sharedPreferences.getBoolean(is_android_watchdog, true)) {
                       try {
                           if(new File(MCUWdtFeed).exists())
                           {
                               setFileState("1", MCUWdtFeed);
                           }
                           
                           Thread.sleep(1000);
                       } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }
                       
                   }
                   
               }
           });
       }
       
       
       if(!thread_STOP_WATCH_DOG.isAlive() || thread_STOP_WATCH_DOG.isInterrupted())
       {
           thread_STOP_WATCH_DOG.start();
       }
   }
   
   
   public void setFileState(String state, String file) {
        FileWriter fw;
        
        try {
            fw = new FileWriter(file);
            fw.write(state);
            fw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
   }
   
   
   public void thread_STOP_APK_FEED()
   {
       if(thread_STOP_WATCH_DOG != null)
       {
           editor.putBoolean(is_android_watchdog, false);
           editor.commit();
           
           if(thread_STOP_WATCH_DOG.isAlive() && !thread_STOP_WATCH_DOG.isInterrupted())
           {
               thread_STOP_WATCH_DOG.interrupt();
           }
       }
       else
       {
           editor.putBoolean(is_android_watchdog, false);
           editor.commit();
       }
   }
   
   
   public void thread_AlarmPower_Repeat_Poweroff()
   {        
       if(sharedPreferences.getString(repeat_alarm_poweroff_time, "").equals(""))
           return;
       
       if(thread_AlarmPower_Repeat_Poweroff == null)
       {
           thread_AlarmPower_Repeat_Poweroff = new Thread(new Runnable() {
               
               @Override
               public void run() {
                   // TODO Auto-generated method stub
                   
                   while (sharedPreferences.getBoolean(is_check_poweroff_alarm_repeat, true)) {
                       try {
                           
                           int now_hour = 0;
                           int now_minute = 0;
                           
                           Calendar calendar = Calendar.getInstance();
                           now_minute = calendar.get(Calendar.MINUTE);
                           
                           if(DateFormat.is24HourFormat(context))
                           {                                
                               now_hour = calendar.get(Calendar.HOUR_OF_DAY);
                           }
                           else
                           {
                               if(calendar.get(Calendar.AM_PM) == Calendar.PM)
                               {
                                   now_hour = calendar.get(Calendar.HOUR) + 12;
                                   
                                   if(now_hour == 24)
                                       now_hour = 0;
                               }
                               else
                               {
                                   now_hour = calendar.get(Calendar.HOUR);
                               }
                               
                           }
                           
                           
                                                       
                           if((now_hour+":"+now_minute+":"+"0").equals(sharedPreferences.getString(repeat_alarm_poweroff_time, "")))
                           {
                               exe_cmd("reboot -p");
                           }
                           
                           Thread.sleep(800);
                       } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }
                       
                   }
                   
               }
           });
       }
       
       if(!thread_AlarmPower_Repeat_Poweroff.isAlive() || thread_AlarmPower_Repeat_Poweroff.isInterrupted())
       {
           thread_AlarmPower_Repeat_Poweroff.start();
       }
   }
   
   
   public String exe_cmd(String cmd)
   {
       String re = "";
       try {
           Process process = Runtime.getRuntime().exec(cmd);
           
           BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
           StringBuffer mStringBuffer = new StringBuffer();
           String line = "";
           while((line = mBufferedReader.readLine())!=null)
           {
               mStringBuffer.append(line);
           }
           
           re = mStringBuffer.toString();
           
           process.waitFor();
           process.exitValue();
           
           return re;
           
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       
       return re;
   }
   
   public boolean isSpiritVersion() {
       return Build.VERSION.SDK_INT <= 25;
   }
 
 
}