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 runningService = (ArrayList) 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; } }