From bbdb71792fad8ac1c70e45a18b4618beb9dec9cb Mon Sep 17 00:00:00 2001 From: snickersun Date: Tue, 12 Jan 2021 10:13:24 +0800 Subject: [PATCH] =?utf8?q?AntiMonitorService=20=E6=B7=BB=E5=8A=A0=20InputE?= =?utf8?q?ventReader=20=E6=9C=8D=E5=8A=A1=E3=80=82=20=E9=80=9A=E8=BF=87?= =?utf8?q?=E9=85=8D=E7=BD=AE=E4=B8=8B=E9=9D=A2=E5=8F=98=E9=87=8F=EF=BC=8C?= =?utf8?q?=E6=8E=A7=E5=88=B6=E4=BD=A9=E6=88=B4=E5=92=8C=E5=89=AA=E6=96=AD?= =?utf8?q?=E6=A3=80=E6=B5=8B=E8=AE=BE=E5=A4=87=E7=9A=84=E6=8C=87=E5=AE=9A?= =?utf8?q?=E3=80=82=20persist.sys.AntiInputName=20persist.sys.CutInputName?= =?utf8?q?=20=E6=B3=A8=E6=84=8FBoardConfig.mk=E4=B8=AD=E7=9A=84=E9=85=8D?= =?utf8?q?=E7=BD=AE=E4=BB=8D=E7=84=B6=E7=94=9F=E6=95=88=EF=BC=8C=E6=B3=A8?= =?utf8?q?=E6=84=8F=E4=B8=A4=E4=B8=AA=E4=B8=8D=E8=A6=81=E5=86=B2=E7=AA=81?= =?utf8?q?=E3=80=82=20BOARD=5FHAVE=5FPLS=20:=3D=20antikey?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../support/service/AntiMonitorService.java | 152 ++++++++--- .../support/service/InputEventReader.java | 256 ++++++++++++++++++ 2 files changed, 367 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/com/enqualcomm/support/service/InputEventReader.java diff --git a/app/src/main/java/com/enqualcomm/support/service/AntiMonitorService.java b/app/src/main/java/com/enqualcomm/support/service/AntiMonitorService.java index 99f904f1..3aa3f90e 100644 --- a/app/src/main/java/com/enqualcomm/support/service/AntiMonitorService.java +++ b/app/src/main/java/com/enqualcomm/support/service/AntiMonitorService.java @@ -31,14 +31,16 @@ import rx.schedulers.Schedulers; import static com.enqualcomm.support.Constants.ACTION_ANTI_OFF; import static com.enqualcomm.support.Constants.ACTION_ANTI_OFF0; import static com.enqualcomm.support.Constants.ACTION_CHECK_ANTIOFF_EVENT; +import static com.enqualcomm.support.Constants.ACTION_CHECK_CUTOFF_EVENT; import static com.enqualcomm.support.Constants.KEY_ANTI_EVENT; import static com.enqualcomm.support.Constants.KEY_ANTI_TXT; import static java.lang.Thread.sleep; -public class AntiMonitorService extends AbstractInvokeHandlerService { +public class AntiMonitorService extends AbstractInvokeHandlerService implements InputEventReader.InputEventReceiver{ + private InputEventReader m_inputEventReader=null; private static final String TAG = "AntiMonitorService"; Context mContext; @@ -58,9 +60,31 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { private static int ant_status_event_reported = 0; // 0 表示无消息, 1表示有消息需要发送, 大于1表示timeout时间内又有新消息,可以忽略不计 private static long anti_event_receivedTime = 0; private static int last_send_antoff_event = -1; + private static int current_cut_status = 12; //2 佩戴, 大于12 表示脱腕 + private static int cut_status_event_reported = 0; // 0 表示无消息, 1表示有消息需要发送, 大于1表示timeout时间内又有新消息,可以忽略不计 + private static long cut_event_receivedTime = 0; + private static int last_send_cut_event = -1; private Handler mHandler = null; PowerManager.WakeLock mWakeLock = null; + @Override + public void onInputEventRecevied(int type, int event) { + if(event==2 || event == 12){ + current_cut_status = event; + cut_status_event_reported++; + ICLogger.i("近距离 " + current_cut_status + " input event " + cut_status_event_reported + " times !"); + cut_event_receivedTime = System.currentTimeMillis(); + reset_timer_cut(); + + }else { + current_anti_status = event; + ant_status_event_reported++; + ICLogger.i("近距离 " + current_anti_status + " input event " + ant_status_event_reported + " times !"); + anti_event_receivedTime = System.currentTimeMillis(); + reset_timer(); + } + } + private static class InnerHandler extends Handler { @@ -101,6 +125,8 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { // mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, AntiMonitorService.class.getSimpleName()); mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, AntiMonitorService.class.getSimpleName()); } + + m_inputEventReader=new InputEventReader(this); } private void play_notify() { @@ -179,7 +205,7 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { mHandler.removeCallbacks(mUpdateTimeFlagTask); }; - private void send_antiof_cutoff_event() { + private void send_antiof_event() { if (last_send_antoff_event == current_anti_status) { ICLogger.i("佩戴状态未变,skip!"); return; @@ -189,43 +215,6 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { //current_anti_status == 0 表示佩戴 ==10 表示脱腕 switch (current_anti_status) { - case STATUS_LOCK_ON: - //锁定 - { - if (Config.needBroadcastCutoffAlarm) { - // play_notify(); - send_cutoff_event_broadcast(0); - } - mRuntime.Cut_off_flag = 0; - if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_LOCK_WATCH)) { - //发送消息定位报警消息 - ICLogger.i("triggerSOSLocationProcess "); - LocationClient.getInstance().triggerSOSLocationProcess(); - } - - - } - break; - case STATUS_CUT_OFF: { - //非法解锁 - if (Config.needBroadcastCutoffAlarm) { - - send_cutoff_event_broadcast(1); - } - mRuntime.Cut_off_flag = 1; - if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_CUTOFF)) { - ICLogger.i("triggerSOSLocationProcess "); - LocationClient.getInstance().triggerSOSLocationProcess(); -// StringMessage msg = protocolAPIHelper.getWarningMSG(mRuntime); -// Deliver.sendLocationMSG(msg); //消息很重要,所以利用带buffer的对列来发送! - } -// try { -// sleep(1000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } - } - break; case STATUS_WEARED: //佩戴 { @@ -278,7 +267,60 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { last_send_antoff_event = current_anti_status; ICLogger.i("AntiOff Timeout! send antioff event " + current_anti_status); } + private void send_cutoff_event() { + if (last_send_cut_event == current_cut_status) { + ICLogger.i("剪断状态未变,skip!"); + return; + } + + //current_anti_status == 2 表示表带 锁定 , ==12 表示表带非法解锁 + //current_anti_status == 0 表示佩戴 ==10 表示脱腕 + + switch (current_cut_status) { + case STATUS_LOCK_ON: + //锁定 + { + if (Config.needBroadcastCutoffAlarm) { + // play_notify(); + send_cutoff_event_broadcast(0); + } + mRuntime.Cut_off_flag = 0; + if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_LOCK_WATCH)) { + //发送消息定位报警消息 + ICLogger.i("triggerSOSLocationProcess "); + LocationClient.getInstance().triggerSOSLocationProcess(); + } + + + } + break; + case STATUS_CUT_OFF: { + //非法解锁 + if (Config.needBroadcastCutoffAlarm) { + + send_cutoff_event_broadcast(1); + } + mRuntime.Cut_off_flag = 1; + if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_CUTOFF)) { + ICLogger.i("triggerSOSLocationProcess "); + LocationClient.getInstance().triggerSOSLocationProcess(); +// StringMessage msg = protocolAPIHelper.getWarningMSG(mRuntime); +// Deliver.sendLocationMSG(msg); //消息很重要,所以利用带buffer的对列来发送! + } +// try { +// sleep(1000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + } + break; + default: + break; + } + last_send_cut_event = current_cut_status; + ICLogger.i("CutOff Timeout! send cutoff event " + current_cut_status); + } public boolean CheckAntiByPoling() { //检查是否佩戴。佩戴返回true //send_antiof_event(); @@ -295,7 +337,7 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { ); if ((currenttime - anti_event_receivedTime) >= 2000) { - send_antiof_cutoff_event(); + send_antiof_event(); ant_status_event_reported = 0; return 0; @@ -307,10 +349,31 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { } } + public int check_cut_event() { + + long currenttime = System.currentTimeMillis(); + ICLogger.i("cut_event_receivedTime in " + (currenttime - cut_event_receivedTime) + + "ms ago ,cut_event_count_when_start_timer " + cut_status_event_reported + ); + + if ((currenttime - cut_event_receivedTime) >= 2000) { + send_cutoff_event(); + + cut_status_event_reported = 0; + return 0; + } else { + long restTimeNeedDelay = (2000 + cut_event_receivedTime - currenttime); + ICLogger.i("Still need delay " + restTimeNeedDelay + "ms to check cut_event again!"); + setCutCheckAlarm((int) restTimeNeedDelay); + return 1; + } + } public void setAntiCheckAlarm(int ms) { AlarmHelper.setRealTimeAlarmInMicorSeconds(mRuntime.getContextProxy().context, ms, ACTION_CHECK_ANTIOFF_EVENT); } - + public void setCutCheckAlarm(int ms) { + AlarmHelper.setRealTimeAlarmInMicorSeconds(mRuntime.getContextProxy().context, ms, ACTION_CHECK_CUTOFF_EVENT); + } private void reset_timer() { if (1 == ant_status_event_reported) //出现 ant_status_event_reported=0表示 有消息到来,需要开始, @@ -319,7 +382,14 @@ public class AntiMonitorService extends AbstractInvokeHandlerService { setAntiCheckAlarm(2000); } } + private void reset_timer_cut() { + if (1 == cut_status_event_reported) //出现 ant_status_event_reported=0表示 有消息到来,需要开始, + { + // antioff_timer.schedule(antioff_timeout_handler,1000); //两秒后发送消息 + setCutCheckAlarm(2000); + } + } /** * 启动传感器。 */ diff --git a/app/src/main/java/com/enqualcomm/support/service/InputEventReader.java b/app/src/main/java/com/enqualcomm/support/service/InputEventReader.java new file mode 100644 index 00000000..596114de --- /dev/null +++ b/app/src/main/java/com/enqualcomm/support/service/InputEventReader.java @@ -0,0 +1,256 @@ +package com.enqualcomm.support.service; + +import com.enqualcomm.support.utils.ICLogger; +import com.enqualcomm.support.utils.ThreadPool; +import com.enqualcomm.support.utils.Util; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.InputStream; + +public class InputEventReader { + public static final int EVENT_TYPE_ANTI=0; + public static final int EVENT_TYPE_CUT=0; + public static final int EVENT_ANTI_OFF=10; + public static final int EVENT_WEARED=1; + public static final int EVENT_CUT_OFF=12; + public static final int EVENT_LOCKED=2; + + /*** + *循环读取设备的name 匹配对应的设备名,确定是否制定设备 + * /sys/class/input/input0/name + * /sys/class/input/input1/name + * ... + * /sys/class/input/inputX/name + */ + private static final String SYS_INPUT_BASE="/sys/class/input/input"; + private static final String EVENT_PATH_BASE="/dev/input/event"; + /*** + * 读取/sys/class/input/inputX/name 对应的名称 符合 antiDevNames 即是 佩戴检测事件节点 + * 检测对应 /dev/input/eventX 节点可以获取佩戴事件 + */ + private static final String antiDevNames= Util.getSystemProperties("persist.sys.AntiInputName",""); + /*** + * 读取/sys/class/input/inputX/name 对应的名称 符合 cutDevNames 即是 剪断检测事件节点 + * 检测对应 /dev/input/eventX 节点可以获取剪断事件 + */ + private static final String cutDevNames= Util.getSystemProperties("persist.sys.CutInputName",""); + + + private String getInputDevNameByID(int id){ + byte buffer[]=new byte[255]; + String ret=""; + String input=SYS_INPUT_BASE+id+"/name"; + try { + FileInputStream fis=new FileInputStream(input); + int len=fis.read(buffer); + if (len > 0){ + ret=new String(buffer,0,len); + } + } catch (Exception e) { + e.printStackTrace(); + } + ICLogger.i("getInputDevNameByID "+input+ " "+ret); + return ret.trim(); + } + + private int get_DevID_By_Name(String name){ + int devid=-1; + for ( int i=0;i< 10;i++){ + if(getInputDevNameByID(i).startsWith(name.trim())) { + devid = i; + break; + } + } + return devid; + } + + + + public int getEventFromData(boolean isAntiEvent,byte[] eventMsgs){ + /*** + * 4d3bfc5fd1ab0a0001008e00010000004d3bfc5fd1ab0a0001008e00000000004d3bfc5fd1ab0a000000000000000000 + * + * 4d3bfc5fd1ab0a00 timestamp 8bytes + * 0100 eventtype 2bytes + * 8e00 eventcode 2bytes + * 01000000 value 4bytes + * 4d3bfc5fd1ab0a00 timestamp + * 0100 eventtype + * 8e00 eventcode + * 00000000 value + * 4d3bfc5fd1ab0a00 timestamp + * 0000 eventtype + * 0000 eventcode + * 00000000 value + * + * 0001 008e 00000001 + * 0001 008e 00000000 + * 0000 0000 00000000 + */ + int ret=0; + byte int_bytes[]=new byte[4]; + if(eventMsgs.length==48){ + int eventtype,eventvalue; + long eventcode; + for(int i=0;i-1){ + + try{ + String antipath=EVENT_PATH_BASE+devid; + ICLogger.i("open AntiEventPATH "+antipath); + FileInputStream br=new FileInputStream(antipath); + byte buf[]=new byte[50]; + int len=0; + br.available() + while(true){ + len=br.read(buf); + if(len>0) { + byte t[]=new byte[len]; + for(int i=0;i-1){ + + try{ + String cutpath=EVENT_PATH_BASE+devid; + ICLogger.i("open CutEventPATH "+cutpath); + FileInputStream br=new FileInputStream(cutpath); + byte buf[]=new byte[50]; + int len=0; + while(true){ + len=br.read(buf); + if(len>0) { + byte t[]=new byte[len]; + for(int i=0;i