import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
-
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import com.enqualcomm.support.LocationClient;
import com.enqualcomm.support.R;
import com.enqualcomm.support.bean.Runtime;
-import com.enqualcomm.support.bean.StringMessage;
import com.enqualcomm.support.service.base.AbstractInvokeHandlerService;
import com.enqualcomm.support.utils.AlarmHelper;
import com.enqualcomm.support.utils.Connection;
import com.enqualcomm.support.utils.ICLogger;
-
-import com.enqualcomm.support.utils.TimeUtil;
import com.enqualcomm.support.utils.Util;
import com.enqualcomm.support.utils.VibrateUtil;
-import com.ic.protocols.protocolAPIHelper;
+
+import java.lang.ref.WeakReference;
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.KEY_ANTI_EVENT;
import static com.enqualcomm.support.Constants.KEY_ANTI_TXT;
-import static java.lang.Thread.interrupted;
import static java.lang.Thread.sleep;
public static final int STATUS_CUT_OFF = 12; //12 表示表带非法解锁
public static final int STATUS_LOCK_ON = 2; // 2 表示表带 锁定
- private static int current_anti_status=10; //0 佩戴, 大于0 表示脱腕
- 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 Handler mHandler=null;
- PowerManager.WakeLock mWakeLock = null;
- public AntiMonitorService(){
- if (mHandler==null) {
- mHandler = new Handler() {
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- if (msg.what == 100) {
-
- ICLogger.i("release WakeLock!");
- if (mWakeLock != null && mWakeLock.isHeld()) {
- mWakeLock.release();
- ICLogger.i("release WakeLock!");
- }
-
- }else if (msg.what == 200){
- AudioService.getInstance().stop();
- }
+ private static int current_anti_status = 10; //0 佩戴, 大于0 表示脱腕
+ 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 Handler mHandler = null;
+ PowerManager.WakeLock mWakeLock = null;
+
+
+ private static class InnerHandler extends Handler {
+
+ private WeakReference<AntiMonitorService> serviceWeakReference = null;
+
+ public InnerHandler(AntiMonitorService service) {
+ serviceWeakReference = new WeakReference<>(service);
+ }
+
+ public void handleMessage(Message msg) {
+ super.handleMessage(msg);
+ AntiMonitorService antiMonitorService = serviceWeakReference.get();
+ if (antiMonitorService == null) {
+ return;
+ }
+ if (msg.what == 100) {
+
+ ICLogger.i("release WakeLock!");
+ if (antiMonitorService.mWakeLock != null && antiMonitorService.mWakeLock.isHeld()) {
+ antiMonitorService.mWakeLock.release();
+ ICLogger.i("release WakeLock!");
}
- };
+
+ } else if (msg.what == 200) {
+ AudioService.getInstance().stop();
+ }
+ }
+
+ }
+
+
+ public AntiMonitorService() {
+ if (mHandler == null) {
+ mHandler = new InnerHandler(this);
}
- if (mWakeLock==null) {
+ if (mWakeLock == null) {
PowerManager mPowerManager = (PowerManager) CoreService.getInstance().getSystemService(Context.POWER_SERVICE);
// 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());
}
}
- private void play_notify(){
+
+ private void play_notify() {
AudioService.getInstance().play(mContext.getAssets(), "antioff.mp3");
- if(mHandler!=null)
- mHandler.sendEmptyMessageDelayed(200,5000);
+ if (mHandler != null)
+ mHandler.sendEmptyMessageDelayed(200, 5000);
}
* 广播给其他应用,用于UI在客户操作
* @param antioff
*/
- private void broadcast_antioff_event_to_other_application(boolean antioff){
- Intent intent=new Intent(ACTION_ANTI_OFF0);
- if(antioff){
+ private void broadcast_antioff_event_to_other_application(boolean antioff) {
+ Intent intent = new Intent(ACTION_ANTI_OFF0);
+ if (antioff) {
+ //脱落
+ mHandler.postDelayed(mDeleteTimeFlagTask, getAntiOffDelayTime());
Util.setSystemProperties("persist.sys.ic_antioff", "true");
- Util.setSystemProperties("persist.sys.ic.antioff_time", TimeUtil.getSystemTimeyyyyMMddHHmmss());
- intent.putExtra(KEY_ANTI_EVENT,1);
- }else {
+ //Util.setSystemProperties("persist.sys.ic.antioff_time", TimeUtil.getSystemTimeyyyyMMddHHmmss());
+ intent.putExtra(KEY_ANTI_EVENT, 1);
+ } else {
+ //佩戴
+ mHandler.removeCallbacks(mDeleteTimeFlagTask);
+ //佩戴的时候开始计时,5分钟后修改佩戴够长的标记值(脱落了会干掉这个任务)
+ mHandler.postDelayed(mUpdateTimeFlagTask, getWearEnoughTime());
Util.setSystemProperties("persist.sys.ic_antioff", "false");
- Util.setSystemProperties("persist.sys.ic.weared_time", TimeUtil.getSystemTimeyyyyMMddHHmmss());
- intent.putExtra(KEY_ANTI_EVENT,0);
+ //Util.setSystemProperties("persist.sys.ic.weared_time", TimeUtil.getSystemTimeyyyyMMddHHmmss());
+ intent.putExtra(KEY_ANTI_EVENT, 0);
}
mContext.sendBroadcast(intent);
}
- private void send_antiof_cutoff_event(){
- if (last_send_antoff_event==current_anti_status){
- ICLogger.i("佩戴状态未变,skip!");
- return;
+ //5分钟
+ public static final int DEFAULT_WEAR_ENOUGH_TIME = 5 * 60 * 1000;
+ //30秒
+ public static final int DEFAULT_RESET_WEAR_TIME = 30 * 1000;
+
+
+ private int getWearEnoughTime() {
+ //佩戴多久为佩戴够长时间了
+ return Util.getSystemPropertiesInt("persist.sys.ic_anti_wear_enough_time",
+ DEFAULT_WEAR_ENOUGH_TIME);
+ }
+
+ private int getAntiOffDelayTime() {
+ return Util.getSystemPropertiesInt("persist.sys.ic_anti_off_delay_time",
+ DEFAULT_RESET_WEAR_TIME);
+ }
+
+ /**
+ * 这个任务用于更新是否佩戴够久的标记量
+ */
+ private final Runnable mUpdateTimeFlagTask = () -> {
+ //如果叠加够5分钟了,就去修改标记量,如果脱落了超过30秒,这个任务就会被干掉。并且会修改成false
+ Util.setSystemProperties("persist.sys.ic_anti_wear_enough", "true");
+ };
+
+ /**
+ * 这个任务是延时30秒执行,就会干掉mUpdateTimeFlagTask。如果佩戴上了这个任务就会被干掉。
+ */
+ private final Runnable mDeleteTimeFlagTask = () -> {
+ //如果脱落了超过30秒,就会干掉mUpdateTimeFlagTask。并且会修改成false
+ Util.setSystemProperties("persist.sys.ic_anti_wear_enough", "false");
+ mHandler.removeCallbacks(mUpdateTimeFlagTask);
+ };
+
+ private void send_antiof_cutoff_event() {
+
+ if (last_send_antoff_event == current_anti_status) {
+ ICLogger.i("佩戴状态未变,skip!");
+ return;
}
//current_anti_status == 2 表示表带 锁定 , ==12 表示表带非法解锁
//current_anti_status == 0 表示佩戴 ==10 表示脱腕
- switch(current_anti_status){
+ switch (current_anti_status) {
case STATUS_LOCK_ON:
//锁定
{
send_cutoff_event_broadcast(0);
}
mRuntime.Cut_off_flag = 0;
- if(mRuntime.setAlarmType(Runtime.AlarmType.ALARM_LOCK_WATCH)) {
+ if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_LOCK_WATCH)) {
//发送消息定位报警消息
ICLogger.i("triggerSOSLocationProcess ");
LocationClient.getInstance().triggerSOSLocationProcess();
}
- break;
- case STATUS_CUT_OFF:
- {
+ break;
+ case STATUS_CUT_OFF: {
//非法解锁
- if(Config.needBroadcastCutoffAlarm) {
+ if (Config.needBroadcastCutoffAlarm) {
send_cutoff_event_broadcast(1);
}
- mRuntime.Cut_off_flag=1;
- if(mRuntime.setAlarmType(Runtime.AlarmType.ALARM_CUTOFF)) {
+ mRuntime.Cut_off_flag = 1;
+ if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_CUTOFF)) {
ICLogger.i("triggerSOSLocationProcess ");
LocationClient.getInstance().triggerSOSLocationProcess();
// StringMessage msg = protocolAPIHelper.getWarningMSG(mRuntime);
// e.printStackTrace();
// }
}
- break;
+ break;
case STATUS_WEARED:
//佩戴
{
broadcast_antioff_event_to_other_application(false);
- if(mRuntime.setAlarmType(Runtime.AlarmType.ALARM_WEARED)) {
+ if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_WEARED)) {
ICLogger.i("triggerSOSLocationProcess ");
LocationClient.getInstance().triggerSOSLocationProcess();
// StringMessage msg = protocolAPIHelper.getWarningMSG(mRuntime);
// e.printStackTrace();
// }
}
- break;
- case STATUS_ANTI_OFF:
- {
+ break;
+ case STATUS_ANTI_OFF: {
//脱腕
- if(Config.needBroadcastAntiAlarm) {
+ if (Config.needBroadcastAntiAlarm) {
send_anti_event_broadcast(1);
}
- mRuntime.Anti_off_flag=1;
+ mRuntime.Anti_off_flag = 1;
broadcast_antioff_event_to_other_application(true);
- if(mRuntime.setAlarmType(Runtime.AlarmType.ALARM_ANTIOFF)) {
+ if (mRuntime.setAlarmType(Runtime.AlarmType.ALARM_ANTIOFF)) {
ICLogger.i("triggerSOSLocationProcess ");
LocationClient.getInstance().triggerSOSLocationProcess();
// StringMessage msg = protocolAPIHelper.getWarningMSG(mRuntime);
// e.printStackTrace();
// }
}
- break;
+ break;
default:
break;
}
- last_send_antoff_event=current_anti_status;
- ICLogger.i("AntiOff Timeout! send antioff event "+current_anti_status);
+ last_send_antoff_event = current_anti_status;
+ ICLogger.i("AntiOff Timeout! send antioff event " + current_anti_status);
}
- public boolean CheckAntiByPoling(){
+ public boolean CheckAntiByPoling() {
//检查是否佩戴。佩戴返回true
//send_antiof_event();
return false;
//return 0 表示check 结束, 1表示需要继续check 由coreService中 ACTION_CHECK_ANTIOFF_EVENT进行
- public int check_ant_event(){
+ public int check_ant_event() {
- long currenttime=System.currentTimeMillis();
- ICLogger.i("anti_event_receivedTime in "+ (currenttime - anti_event_receivedTime)
- +"ms ago ,anti_event_count_when_start_timer "+ant_status_event_reported
+ long currenttime = System.currentTimeMillis();
+ ICLogger.i("anti_event_receivedTime in " + (currenttime - anti_event_receivedTime)
+ + "ms ago ,anti_event_count_when_start_timer " + ant_status_event_reported
);
if ((currenttime - anti_event_receivedTime) >= 2000) {
}
}
- public void setAntiCheckAlarm(int ms){
- AlarmHelper.setRealTimeAlarmInMicorSeconds(mRuntime.getContextProxy().context,ms,ACTION_CHECK_ANTIOFF_EVENT);
+ public void setAntiCheckAlarm(int ms) {
+ AlarmHelper.setRealTimeAlarmInMicorSeconds(mRuntime.getContextProxy().context, ms, ACTION_CHECK_ANTIOFF_EVENT);
}
- private void reset_timer(){
- if (1==ant_status_event_reported) //出现 ant_status_event_reported=0表示 有消息到来,需要开始,
+ private void reset_timer() {
+
+ if (1 == ant_status_event_reported) //出现 ant_status_event_reported=0表示 有消息到来,需要开始,
{
// antioff_timer.schedule(antioff_timeout_handler,1000); //两秒后发送消息
- setAntiCheckAlarm(2000);
+ setAntiCheckAlarm(2000);
}
}
+
/**
* 启动传感器。
*/
if (!isRegister) {
ICLogger.i("Proximity 设备不支持2");
//throw new UnsupportedOperationException("设备不支持");
- return ;
+ return;
}
}
-
- public void send_cutoff_event_broadcast(int cutoff){
- ICLogger.i("send_cutoff_event_broadcast "+cutoff);
- Intent intent=new Intent();
+ public void send_cutoff_event_broadcast(int cutoff) {
+ ICLogger.i("send_cutoff_event_broadcast " + cutoff);
+ Intent intent = new Intent();
intent.setAction(ACTION_ANTI_OFF);
- intent.putExtra(KEY_ANTI_EVENT,cutoff);
- if (cutoff==0) {
+ intent.putExtra(KEY_ANTI_EVENT, cutoff);
+ if (cutoff == 0) {
if (Config.A9_noBroadCastActionStrip) {
if (mRuntime.needReportCutoff() == true) {
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_lock_success));
}
- }else if (Config.L004_BroadCastActionStrip) {
+ } else if (Config.L004_BroadCastActionStrip) {
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_wristband_on));
//下面消息是为了把锁定窗口关掉
intent.putExtra(KEY_ANTI_TXT, "");
mContext.sendBroadcast(intent);
- }else if (Config.C5_noBroadCastActionStrip) {
+ } else if (Config.C5_noBroadCastActionStrip) {
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_lock_success));
- }else{
+ } else {
intent.putExtra(KEY_ANTI_TXT, "");
mContext.sendBroadcast(intent);
}
- }else{
+ } else {
if (Config.A9_noBroadCastActionStrip) {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
+ ICLogger.e(e.getMessage(), e);
}//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_Illegal_unlock));
}
- }else if (Config.L004_BroadCastActionStrip) {
+ } else if (Config.L004_BroadCastActionStrip) {
play_notify();
VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
+ ICLogger.e(e.getMessage(), e);
}//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
intent.putExtra(KEY_ANTI_TXT, mContext.getResources().getString(R.string.alarm_wristband_off));
mContext.sendBroadcast(intent);
- }else if (Config.C5_noBroadCastActionStrip) {
+ } else if (Config.C5_noBroadCastActionStrip) {
- play_notify();
- VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
- screen_on();
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
- }//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
- Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_Illegal_unlock));
+ play_notify();
+ VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
+ screen_on();
+ try {
+ sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ ICLogger.e(e.getMessage(), e);
+ }//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
+ Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_Illegal_unlock));
- }else{
+ } else {
play_notify();
VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
screen_on();
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
+ ICLogger.e(e.getMessage(), e);
}//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
- if (mRuntime.getNeedAntioff()){
+ if (mRuntime.getNeedAntioff()) {
intent.putExtra(KEY_ANTI_TXT, mContext.getResources().getString(R.string.alarm_wristband_off));
mContext.sendBroadcast(intent);
}
}
}
- public void send_anti_event_broadcast(int antioff){
- ICLogger.i("send_anti_event_broadcast "+antioff);
- Intent intent=new Intent();
+ public void send_anti_event_broadcast(int antioff) {
+
+ ICLogger.i("send_anti_event_broadcast " + antioff);
+ Intent intent = new Intent();
intent.setAction(ACTION_ANTI_OFF);
- intent.putExtra(KEY_ANTI_EVENT,antioff);
- if (antioff==0) {
- mRuntime.isAntioff=false;
+ intent.putExtra(KEY_ANTI_EVENT, antioff);
+ if (antioff == 0) {
+ mRuntime.isAntioff = false;
if (Config.A9_noBroadCastActionStrip) {
if (mRuntime.needReportCutoff() == true) {
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_lock_success));
}
- }else if (Config.L004_BroadCastActionStrip) {
+ } else if (Config.L004_BroadCastActionStrip) {
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_wristband_on));
//下面消息是为了把锁定窗口关掉
intent.putExtra(KEY_ANTI_TXT, "");
mContext.sendBroadcast(intent);
- }else if (Config.C5_noBroadCastActionStrip) {
+ } else if (Config.C5_noBroadCastActionStrip) {
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_lock_success));
- }else{
+ } else {
intent.putExtra(KEY_ANTI_TXT, "");
mContext.sendBroadcast(intent);
}
- }else{
- mRuntime.isAntioff=true;
+ } else {
+ mRuntime.isAntioff = true;
if (Config.A9_noBroadCastActionStrip) {
if (mRuntime.needReportCutoff() == true) {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
+ ICLogger.e(e.getMessage(), e);
}//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_Illegal_unlock));
}
- }else if (Config.L004_BroadCastActionStrip) {
+ } else if (Config.L004_BroadCastActionStrip) {
play_notify();
VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
screen_on();
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
+ ICLogger.e(e.getMessage(), e);
}//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
intent.putExtra(KEY_ANTI_TXT, mContext.getResources().getString(R.string.alarm_wristband_off));
mContext.sendBroadcast(intent);
- }else if (Config.C5_noBroadCastActionStrip) {
+ } else if (Config.C5_noBroadCastActionStrip) {
- play_notify();
- VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
- screen_on();
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
- }//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
- Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_Illegal_unlock));
+ play_notify();
+ VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
+ screen_on();
+ try {
+ sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ ICLogger.e(e.getMessage(), e);
+ }//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
+ Util.ShowToast(mRuntime, mContext.getResources().getString(R.string.alarm_Illegal_unlock));
- }else{
+ } else {
play_notify();
VibrateUtil.getInstance(mContext).VibrateOnOnce(true);
screen_on();
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
+ ICLogger.e(e.getMessage(), e);
}//delay一会儿再发消息,以屏没有来得急亮起,免界面不响应。
- if (mRuntime.getNeedAntioff()){
+ if (mRuntime.getNeedAntioff()) {
intent.putExtra(KEY_ANTI_TXT, mContext.getResources().getString(R.string.alarm_wristband_off));
mContext.sendBroadcast(intent);
}
}
- private void screen_on(){
- mWakeLock.acquire(5*1000);//亮屏5秒
- mHandler.sendEmptyMessageDelayed(100,2000);
+ private void screen_on() {
+ mWakeLock.acquire(5 * 1000);//亮屏5秒
+ mHandler.sendEmptyMessageDelayed(100, 2000);
}
+
private SensorEventListener mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
- current_anti_status=(int)sensorEvent.values[0];
+ current_anti_status = (int) sensorEvent.values[0];
ant_status_event_reported++;
- ICLogger.i("近距离 "+current_anti_status+" get event "+ant_status_event_reported+" times !");
- anti_event_receivedTime=System.currentTimeMillis();
+ ICLogger.i("近距离 " + current_anti_status + " get event " + ant_status_event_reported + " times !");
+ anti_event_receivedTime = System.currentTimeMillis();
reset_timer();
}
@Override
public void onAttach(Connection paramConnection, Runtime paramRuntime) {
- mRuntime=paramRuntime;
- mContext=mRuntime.getContextProxy().context;
- mConnection=paramConnection;
- if(Config.UsePolingCheckWeared==false) {
+ mRuntime = paramRuntime;
+ mContext = mRuntime.getContextProxy().context;
+ mConnection = paramConnection;
+ if (Config.UsePolingCheckWeared == false) {
ICLogger.i("启用中断式佩戴检测!");
startSensor();
- }else{
+ } else {
ICLogger.i("启用循环式佩戴检测!");
}
}
@Override
public void onDetach(Connection paramConnection, Runtime paramRuntime) {
- if(Config.UsePolingCheckWeared==false) {
+ if (Config.UsePolingCheckWeared == false) {
mSensorManager.unregisterListener(mSensorEventListener);
}
}