From 19f80d915a5c15d585f0c421f25211acd333f0db Mon Sep 17 00:00:00 2001 From: snickersun Date: Sat, 15 Feb 2020 01:34:36 +0800 Subject: [PATCH] =?utf8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E5=92=8C=E6=B7=BB?= =?utf8?q?=E5=8A=A0=E7=9F=AD=E4=BF=A1=E5=91=BD=E4=BB=A4=E5=8F=8Abroadcast?= =?utf8?q?=E6=B5=8B=E8=AF=95=E5=AF=B9=E5=BA=94=E5=91=BD=E4=BB=A4=EF=BC=8C?= =?utf8?q?=E4=BB=8A=E6=97=A5=E4=B8=BB=E8=A6=81=E5=A2=9E=E5=8A=A0=E4=BA=86a?= =?utf8?q?pn=E5=92=8Clocation=E5=91=BD=E4=BB=A4=E3=80=82=E5=85=B7=E4=BD=93?= =?utf8?q?=E5=91=BD=E4=BB=A4=E5=A6=82=E4=B8=8B=EF=BC=9A=202=E3=80=81?= =?utf8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E4=BD=8D=E9=80=BB=E8=BE=91=EF=BC=8C?= =?utf8?q?=E5=A2=9E=E5=8A=A0Custom=20Mode=EF=BC=885=EF=BC=89=EF=BC=8C?= =?utf8?q?=E5=AE=9A=E4=BD=8D=E9=97=B4=E9=9A=94=E4=BB=A5=E7=A7=92=E4=B8=BA?= =?utf8?q?=E5=8D=95=E4=BD=8D=EF=BC=8C=E6=9C=80=E4=BD=8E30=E7=A7=92?= =?utf8?q?=E3=80=82=E4=BD=8E=E4=BA=8E1=E5=88=86=E9=92=9F=E5=AE=9A=E4=BD=8D?= =?utf8?q?=E7=9A=84=E8=AF=9D=EF=BC=8CGPS=E5=B0=B1=E4=B8=8D=E4=BC=9A?= =?utf8?q?=E5=85=B3=E9=97=AD=E4=BA=86=E3=80=82=20am=20broadcast=20-a=20com?= =?utf8?q?.enqualcomm.support.SMSCMD=20--es=20smsCmd=20"#status#"=20am=20b?= =?utf8?q?roadcast=20-a=20com.enqualcomm.support.SMSCMD=20--es=20smsCmd=20?= =?utf8?q?"#deviceinfo#"=20am=20broadcast=20-a=20com.enqualcomm.support.SM?= =?utf8?q?SCMD=20--es=20smsCmd=20"#apn#=3Dcurrent#"=20am=20broadcast=20-a?= =?utf8?q?=20com.enqualcomm.support.SMSCMD=20--es=20smsCmd=20"#apn#=3D460,?= =?utf8?q?07,CMNET,cmnet,,#"=20am=20broadcast=20-a=20com.enqualcomm.suppor?= =?utf8?q?t.SMSCMD=20--es=20smsCmd=20"#imei:reset"=20am=20broadcast=20-a?= =?utf8?q?=20com.enqualcomm.support.SMSCMD=20--es=20smsCmd=20"#imei:curren?= =?utf8?q?t"=20am=20broadcast=20-a=20com.enqualcomm.support.SMSCMD=20--es?= =?utf8?q?=20smsCmd=20"#imei:123456789012345"=20am=20broadcast=20-a=20com.?= =?utf8?q?enqualcomm.support.SMSCMD=20--es=20smsCmd=20"#setlocation#=3Dres?= =?utf8?q?et#"=20am=20broadcast=20-a=20com.enqualcomm.support.SMSCMD=20--e?= =?utf8?q?s=20smsCmd=20"#setlocation#=3D5,30#"=20am=20broadcast=20-a=20com?= =?utf8?q?.enqualcomm.support.SMSCMD=20--es=20smsCmd=20"#ip#=3Dreset#"=20a?= =?utf8?q?m=20broadcast=20-a=20com.enqualcomm.support.SMSCMD=20--es=20smsC?= =?utf8?q?md=20"#ip#=3Dcurrent#"=20am=20broadcast=20-a=20com.enqualcomm.su?= =?utf8?q?pport.SMSCMD=20--es=20smsCmd=20"#ip#=3D192.168.0.1:8080#"=20am?= =?utf8?q?=20broadcast=20-a=20com.enqualcomm.support.SMSCMD=20--es=20smsCm?= =?utf8?q?d=20"#ip#=3D192.168.0.1,8080#"?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 3 + .../com/enqualcomm/support/CoreService.java | 134 ++---------- .../com/enqualcomm/support/ICSmsManager.java | 196 +++++++++++++----- .../enqualcomm/support/LocationClient.java | 10 +- .../com/enqualcomm/support/NMEAListener.java | 78 ++++--- .../enqualcomm/support/bean/ICLocation.java | 4 +- .../com/enqualcomm/support/utils/APNUtil.java | 127 ++++++++++++ .../enqualcomm/support/utils/AlarmHelper.java | 24 ++- .../enqualcomm/support/utils/BatteryUtil.java | 17 +- .../java/com/sina/util/dnscache/DNSCache.java | 2 +- 10 files changed, 391 insertions(+), 204 deletions(-) create mode 100644 app/src/main/java/com/enqualcomm/support/utils/APNUtil.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6d430603..bb6d0e91 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -106,6 +106,9 @@ + + + diff --git a/app/src/main/java/com/enqualcomm/support/CoreService.java b/app/src/main/java/com/enqualcomm/support/CoreService.java index eba3437c..4da9a78c 100644 --- a/app/src/main/java/com/enqualcomm/support/CoreService.java +++ b/app/src/main/java/com/enqualcomm/support/CoreService.java @@ -176,10 +176,9 @@ public class CoreService extends Service { Logger.i(stringBuilder2.toString()); if (ACTION_SEND_EMEGENCY_LOCATION_START.equals(param1Intent.getAction())){ - runtime.getLocationInstance().setWorkingMod(CUSTOM_MOD); - runtime.getLocationInstance().setLocation_cycle(1); + runtime.getLocationInstance().setWorkingMod(EMERGNECY_MOD); runtime.setPpgTestCycle(1); - setLocationAlarm(CUSTOM_MOD); + setLocationAlarm(runtime.getLocationInstance().getWorkingMod()); setHeartRateDetectAlarm(runtime.getPpgTestCycle()); setEmegencyLocationSTOPAlarm(5); }else if (ACTION_LOCAL_SOS_ACTION.endsWith(param1Intent.getAction())){ @@ -194,9 +193,8 @@ public class CoreService extends Service { connection.send(protocolAPIHelper.genStringMessage("AP10",protocolAPIHelper.AP10(runtime))); //开启紧急定位模式,GPS 优先1分钟一次定位,持续5分钟 - runtime.getLocationInstance().setWorkingMod(CUSTOM_MOD); - runtime.getLocationInstance().setLocation_cycle(1); - setLocationAlarm(CUSTOM_MOD); + runtime.getLocationInstance().setWorkingMod(EMERGNECY_MOD); + setLocationAlarm(runtime.getLocationInstance().getWorkingMod()); //SOS模式,心率测试1分钟一次,持续5分钟 runtime.setPpgTestCycle(1); setHeartRateDetectAlarm(runtime.getPpgTestCycle()); @@ -441,7 +439,7 @@ public class CoreService extends Service { @Override public void onCreate() { -// Logger.i("CoreService--onCreate002"); + log("初始化网络协议服务"); mProtocolServer=this; @@ -530,15 +528,17 @@ public class CoreService extends Service { this.mSignalUtil = SimSignalUtil.getInstance(this); //初始化 电池电量监控工具(内部注册接收电池变化) - batteryUtil = new BatteryUtil(this.runtime); - batteryUtil.registerBroadReceiver(); - + batteryUtil = new BatteryUtil(this.runtime,connection); //保证发送心跳时提前获取BatLevel数据 int batlevel=batteryUtil.getBatteryLevel(this); runtime.setBattery(String.format("%03d",batlevel)); Logger.i("Battery get from api "+batlevel); + //api直接获取batterylevel取消注册 receiver, 需要重新注册一下。 + + batteryUtil.registerBroadReceiver(); + //设置计步清零时间。 setCleanStepCountAlarm(); @@ -692,13 +692,6 @@ public class CoreService extends Service { }else if(ACTION_SEND_EMEGENCY_LOCATION_STOP.equals(paramIntent.getAction())){ //stop Emegency location Stop Config.loadModCycleSettings(runtime); //回复之前的 工作模式即可 - }else if (ACTION_BATTERY_LOW_MSG.equals(paramIntent.getAction())){ - - runtime.setAlarmType(false,runtime.getBatteryLow(),false,false); - //trigger AP10 报警定位 - CoreService.this.locationClient.triggerSOSLocationProcess(); - - return Service.START_STICKY; } this.invokeHandlerServiceChain.processUserRequest(paramIntent, this.connection, this.runtime); } @@ -755,21 +748,21 @@ public class CoreService extends Service { switch(mod){ case EMERGNECY_MOD: case REALTIME_MOD: - cycle=1; + cycle=60; break; case NORMAL_MOD: - cycle=15; + cycle=15*60; break; case LOWPOWER_MOD: - cycle=60; + cycle=60*60; break; case CUSTOM_MOD: cycle=runtime.getLocationInstance().getLocation_cycle(); break; } - AlarmHelper.setRealTimeAlarm(runtime.getContextProxy().context,cycle, ACTION_LOCATION_ALARM); - sb.append(" setLocationAlarm "+ACTION_LOCATION_ALARM+" mod "+mod+", cycle "+cycle); + AlarmHelper.setRealTimeAlarmInSeconds(runtime.getContextProxy().context,cycle, ACTION_LOCATION_ALARM); + sb.append(" setLocationAlarm "+ACTION_LOCATION_ALARM+" mod "+mod+", cycle in Seconds "+cycle); locationClient.triggerLocationProcess(); Logger.i(sb.toString()); @@ -833,32 +826,14 @@ public class CoreService extends Service { } } - private void LogToast(String msg) { -// if (msg!=null && msg.length()>0 && toast!=null) -// toast.setText(msg).show(); - } - - private static final String TAG = "ICSrv." + CoreService.class.getSimpleName(); - private static boolean mLowPowerMode = false; - private static boolean mCallStateIsIdle = true; // 非通话活动 - private TelephonyManager telManager; private Timer mTimer_retry_connect=null; - private WifiManager mWifiManager=null; - - private static Runtime mDevStat=null; -// private protocolAPIHelper mProtocolAPI=null; - private void log(String msg) { Logger.log(TAG,msg); } - private void log(String funcName, String msg) { - Logger.log(TAG,funcName,msg); - } - public static CoreService getInstance(){ return mProtocolServer; } @@ -927,10 +902,7 @@ public class CoreService extends Service { @Override public void onDestroy() { super.onDestroy(); - try { - unregisterReceiver(mIntentReceiver); - } catch (Exception ignored) { - } + saveStepCount(); this.connectionServiceChain.processDetach(this.connection, this.runtime); this.invokeHandlerServiceChain.processDetach(this.connection, this.runtime); @@ -940,6 +912,7 @@ public class CoreService extends Service { unregisterReceiver(this.screenOnReceiver); unregisterReceiver(this.AtCallbackBroadcastReceiver); batteryUtil.unregisterBroadReceiver(); + smsManager.unregisterBroadReceiver(); } @@ -947,77 +920,4 @@ public class CoreService extends Service { this.connection.close(); } - private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - boolean lastIsLowPowerMode = isLowPowerMode(); - log("mIntentReceiver,onReceive", "START"); - log("mIntentReceiver,onReceive", "action: " + action - + ", mCallStateIsIdle: " + mCallStateIsIdle); - - setLowPowerMode(false); - if (!inCall() && action != null) { // 非通话活动 - boolean currLowPowerMode = false; - if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { - int rawLevel = intent.getIntExtra("level", -1); - int scale = intent.getIntExtra("scale", -1); - int status = intent.getIntExtra("status", -1); - int level = -1; // percentage, or -1 for unknown - - if (status != BatteryManager.BATTERY_STATUS_CHARGING) { // 非充电状态 - if (rawLevel >= 0 && scale > 0) { - level = (rawLevel * 100) / scale; - currLowPowerMode = isLowPower(level); - log("mIntentReceiver,onReceive", "level: " + level); - } - } - } else if (action.equals(Intent.ACTION_BATTERY_LOW)) { - log("mIntentReceiver,onReceive", "ACTION_BATTERY_LOW"); - currLowPowerMode = true; - } - - // 只在状态改变时激活桌面 - boolean needActivateLauncher = (currLowPowerMode != lastIsLowPowerMode); - log("mIntentReceiver,onReceive", - "lastIsLowPowerMode:" + lastIsLowPowerMode + - "currLowPowerMode:" + currLowPowerMode + - ", needActivateLauncher:" + needActivateLauncher); - - setLowPowerMode(currLowPowerMode); - - - if (needActivateLauncher) { - log("低电量,进入协议省电模式!"); - } - - } - log("mIntentReceiver,onReceive", "END"); - } - }; - - public boolean isLowPowerMode() { - log("isLowPowerMode", "" + mLowPowerMode); - return mLowPowerMode; - } - - /** - * 根据电量,判断是否是低电量 - * - * @param level - * @return - */ - public static boolean isLowPower(int level) { - return (level <= 15); - } - - public void setLowPowerMode(boolean v) { - log("setLowPowerMode", "" + v); - mLowPowerMode = v; - } - - private boolean inCall() { -// return telManager.getCallState() != TelephonyManager.CALL_STATE_IDLE; - return false; - } } diff --git a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java index a4224503..34060840 100644 --- a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java +++ b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java @@ -15,6 +15,7 @@ import android.telephony.SmsMessage; import android.widget.Toast; import com.enqualcomm.support.bean.Runtime; +import com.enqualcomm.support.utils.APNUtil; import com.enqualcomm.support.utils.DeviceInfoUtil; import com.enqualcomm.support.utils.Logger; import com.enqualcomm.support.utils.ServiceUtil; @@ -26,24 +27,18 @@ import chestnut.utils.PhoneUtils; public class ICSmsManager { - private static String mNumber; + private static String ACTION_SMSCMD="com.enqualcomm.support.SMSCMD"; private static long preTimeMillis; - private Uri SMS_INBOX = Uri.parse("content://sms/inbox"); private Context mContext = null; Runtime runtime; -// ArrayList smsInfoList; ICSmsManager.SmsReceiver smsReceiver; -// ArrayList source; public ICSmsManager( Runtime _runtime) { -// this.source = src; this.runtime = _runtime; this.mContext = runtime.getContextProxy().context; this.smsReceiver = new ICSmsManager.SmsReceiver(); - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); - intentFilter.setPriority(2147483647); - this.mContext.registerReceiver(this.smsReceiver, intentFilter); + + registerBroadReceiver(); } public void sendSMS(String phonenumber, String msg) { @@ -53,7 +48,9 @@ public class ICSmsManager { while(msgit.hasNext()) { String message= (String)msgit.next(); Logger.i("reply message to "+phonenumber+", "+message); - smsManager.sendTextMessage(phonenumber, (String)null,message, (PendingIntent)null, (PendingIntent)null); + if (phonenumber!=null && !phonenumber.isEmpty()) { + smsManager.sendTextMessage(phonenumber, (String) null, message, (PendingIntent) null, (PendingIntent) null); + } } } @@ -61,7 +58,33 @@ public class ICSmsManager { return true; } + public void registerBroadReceiver() { + + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); + intentFilter.setPriority(2147483647); + this.mContext.registerReceiver(this.smsReceiver, intentFilter); + intentFilter = new IntentFilter(); + intentFilter.addAction(ACTION_SMSCMD); + (this.runtime.getContextProxy()).context.registerReceiver(this.mSMSCMD_BroadcastReceiver, intentFilter); + } + + public void unregisterBroadReceiver() { + (this.runtime.getContextProxy()).context.unregisterReceiver(this.mSMSCMD_BroadcastReceiver); + (this.runtime.getContextProxy()).context.unregisterReceiver(this.smsReceiver); + } + + private final BroadcastReceiver mSMSCMD_BroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (ACTION_SMSCMD.equals(action)) { + String cmd = intent.getStringExtra("smsCmd"); + handleSMS_CMD("", cmd); + } + } + }; //1:短信设置服务器 //#ip#=198.11.183.28,5088# //回复 ip set ok @@ -81,28 +104,77 @@ public class ICSmsManager { //SW: L02_20200208_V1 public void handleSMS_CMD(String phonenumber, String sms){ if (isAdminNumber(phonenumber)) { - if (sms.startsWith("#deviceinfo#")) { + + if (sms==null || sms.isEmpty()) return ; + + if (sms.startsWith("#apn#=")&& sms.endsWith("#")){ + String[] tmp=sms.split("#"); + if(tmp.length==3) { + String[] apnvalues= tmp[2].replace("=","").split(","); + if (apnvalues.length==4){ + String mcc=apnvalues[0]; + String mnc=apnvalues[1]; + String apnName=apnvalues[2]; + String apn=apnvalues[3]; + String user=""; + String password=""; + if(APNUtil.addVPN(mcc,mnc,apnName,apn,user,password,mContext)==true){ + sendSMS(phonenumber,"apn add success!"); + }else{ + sendSMS(phonenumber,"apn add failed!"); + } + }else if (apnvalues.length==6) { + String mcc=apnvalues[0]; + String mnc=apnvalues[1]; + String apnName=apnvalues[2]; + String apn=apnvalues[3]; + String user=apnvalues[4]; + String password=apnvalues[5]; + if(APNUtil.addVPN(mcc,mnc,apnName,apn,user,password,mContext)==true){ + sendSMS(phonenumber,"apn add success!"); + }else{ + sendSMS(phonenumber,"apn add failed!"); + } + }else if (apnvalues.length==1){ + if (apnvalues[0].equals("current")) { + + String apnstr = APNUtil.getCurrentAPN(mContext); + sendSMS(phonenumber, apnstr); + }else{ + sendSMS(phonenumber,"apn cmd format error!"); + } + } else{ + sendSMS(phonenumber,"apn cmd format error!"); + } + }else { + + sendSMS(phonenumber,"apn cmd format error!"); + + } + }else if (sms.startsWith("#deviceinfo#")) { Logger.i("SMS "+sms+" reply "+DeviceInfoUtil.getShortDeviceInfo(runtime)); sendSMS(phonenumber,DeviceInfoUtil.getShortDeviceInfo(runtime)); }else if (sms.startsWith("#status#")) { - String reply="IMEI="+runtime.IMEI()+"\nSW:"+DeviceInfoUtil.getVersionCode(runtime.getContextProxy().context)+" "+DeviceInfoUtil.getSWVersionDisplayName(); + String reply="IMEI="+runtime.IMEI()+"\nSW:"+DeviceInfoUtil.getVersionCode(mContext)+" "+DeviceInfoUtil.getSWVersionDisplayName(); Logger.i("SMS "+sms+" reply \n"+reply); sendSMS(phonenumber,reply); }else if (sms.startsWith("#imei:")) { String imei=sms.substring(6); Logger.i("SMS "+sms+" imei="+imei); if (imei.length()==15 ) { - Config.setIMEI(runtime.getContextProxy().context, imei); + Config.setIMEI(mContext, imei); runtime.setImeiAndCid(imei, ""); sendSMS(phonenumber, "set IMEI " + imei + " sucess !"); CoreService.getInstance().disconnect_server(); CoreService.getInstance().ReConnectToServer(); }else if (imei.equals("reset")) { - Config.setIMEI(runtime.getContextProxy().context, ""); - runtime.setImeiAndCid(PhoneUtils.getIMEI(runtime.getContextProxy().context), ""); - sendSMS(phonenumber, "reset IMEI to " + PhoneUtils.getIMEI(runtime.getContextProxy().context) + " sucess !"); + Config.setIMEI(mContext, ""); + runtime.setImeiAndCid(PhoneUtils.getIMEI(mContext), ""); + sendSMS(phonenumber, "reset IMEI to " + PhoneUtils.getIMEI(mContext) + " sucess !"); CoreService.getInstance().disconnect_server(); CoreService.getInstance().ReConnectToServer(); + }else if (imei.equals("current")) { + sendSMS(phonenumber, "Current IMEI " + runtime.IMEI()); }else{ sendSMS(phonenumber, "set IMEI " + imei + " failed ! len!=15"); } @@ -110,54 +182,84 @@ public class ICSmsManager { String tmp[]=sms.split("="); String reply=""; Logger.i(sms+" len "+tmp.length); - if (tmp.length==2) - { - //分析 以 , 为分界的格式 - String[] ipstr = tmp[1].split(","); - if (ipstr[0].equals("reset#")) { - Config.setProtocolServerHost(runtime.getContextProxy().context, Runtime.DefaultHost); - Config.setProtocolServerAddr(runtime.getContextProxy().context, "", Runtime.DefaultPort); + if (tmp.length==2) { + if (tmp[1].equals("reset#")) { + Config.setProtocolServerHost(mContext, Runtime.DefaultHost); + Config.setProtocolServerAddr(mContext, "", Runtime.DefaultPort); reply = "reset ServerIP to " + Runtime.DefaultHost + ":" + Runtime.DefaultPort + " success! "; Config.loadServerSettings(runtime); CoreService.getInstance().disconnect_server(); CoreService.getInstance().ReConnectToServer(); - }else if (ipstr.length==2){ - String ip=ipstr[0]; - int port=Integer.valueOf(ipstr[1].replace("#","")); - Config.setProtocolServerHost(runtime.getContextProxy().context,ip); - Config.setProtocolServerAddr(runtime.getContextProxy().context,"",port); - reply = sms+" success! "; - Config.loadServerSettings(runtime); - CoreService.getInstance().disconnect_server(); - CoreService.getInstance().ReConnectToServer(); - }else{ - //分析以 : 为分界的格式 - ipstr = tmp[1].split(":"); - - if (ipstr[0].equals("reset#")) { - Config.setProtocolServerHost(runtime.getContextProxy().context, Runtime.DefaultHost); - Config.setProtocolServerAddr(runtime.getContextProxy().context, "", Runtime.DefaultPort); - reply = "reset ServerIP to " + Runtime.DefaultHost + ":" + Runtime.DefaultPort + " success! "; - Config.loadServerSettings(runtime); - CoreService.getInstance().disconnect_server(); - CoreService.getInstance().ReConnectToServer(); - } else if (ipstr.length == 2) { + } else if (tmp[1].equals("current#")) { + reply = "current serverAddress:" + runtime.getHost() + ":" + runtime.getPort(); + } else { + //分析 以 , 为分界的格式 + String[] ipstr = tmp[1].split(","); + if (ipstr.length == 2) { String ip = ipstr[0]; int port = Integer.valueOf(ipstr[1].replace("#", "")); - Config.setProtocolServerHost(runtime.getContextProxy().context, ip); - Config.setProtocolServerAddr(runtime.getContextProxy().context, "", port); + Config.setProtocolServerHost(mContext, ip); + Config.setProtocolServerAddr(mContext, "", port); reply = sms + " success! "; Config.loadServerSettings(runtime); CoreService.getInstance().disconnect_server(); CoreService.getInstance().ReConnectToServer(); } else { - reply = sms + " falied! "; + //分析以 : 为分界的格式 + ipstr = tmp[1].split(":"); + if (ipstr.length == 2) { + String ip = ipstr[0]; + int port = Integer.valueOf(ipstr[1].replace("#", "")); + Config.setProtocolServerHost(mContext, ip); + Config.setProtocolServerAddr(mContext, "", port); + reply = sms + " success! "; + Config.loadServerSettings(runtime); + CoreService.getInstance().disconnect_server(); + CoreService.getInstance().ReConnectToServer(); + } else { + reply = sms + " falied! "; + } } } }else{ reply = sms + " falied! "; } sendSMS(phonenumber, reply); + }else if (sms.startsWith("#setlocation#=")&& sms.endsWith("#")){ + String tmp[]=sms.split("="); + if (tmp.length==2){ + if (tmp[1].equals("reset#")){ + Config.loadModCycleSettings(runtime);//恢复初始设置 + sendSMS(phonenumber, "reset to Location Mode="+runtime.getLocationInstance().getWorkingMod()+", Cycle="+runtime.getLocationInstance().getLocation_cycle()); + }else { + String[] t=tmp[1].replace("#","").split(","); + if (t.length==2){ + int mod=0; + int cycle=0; + try { + mod = Integer.valueOf(t[0]); + cycle = Integer.valueOf(t[1]); + }catch (Exception e){ + e.printStackTrace(); + } + if(mod<6&& mod>0 && cycle>=30 ){ + runtime.getLocationInstance().setLocation_cycle(cycle); + runtime.getLocationInstance().setWorkingMod(mod); + CoreService.getInstance().setLocationAlarm(runtime.getLocationInstance().getWorkingMod()); + sendSMS(phonenumber, "set to Location Mode="+runtime.getLocationInstance().getWorkingMod()+", Cycle="+runtime.getLocationInstance().getLocation_cycle()); + }else{ + sendSMS(phonenumber, "set to Location Failed ,value range should be 1<= Mode <=5 and cycle >=30!"); + } + + }else{ + sendSMS(phonenumber, "setlocation format error!"); + } + } + + }else{ + sendSMS(phonenumber, "setlocation format error!"); + } + } } } diff --git a/app/src/main/java/com/enqualcomm/support/LocationClient.java b/app/src/main/java/com/enqualcomm/support/LocationClient.java index d4294395..bc05c38a 100644 --- a/app/src/main/java/com/enqualcomm/support/LocationClient.java +++ b/app/src/main/java/com/enqualcomm/support/LocationClient.java @@ -283,9 +283,10 @@ public class LocationClient { Logger.i("lbs_ok "+lbs_ok+" waitAndSendLocation wifi_ok " + wifi_ok+ " gps_ok "+gps_ok + " mode "+mode); switch(mode){ + case ICLocation.CUSTOM_MOD: case ICLocation.EMERGNECY_MOD: case ICLocation.REALTIME_MOD: - case ICLocation.CUSTOM_MOD: + //GPS 优先的话多给些时间(10秒) if (checktimes> 50) timeout=true; @@ -441,9 +442,14 @@ public void updateCellInfo() { updateCellInfo(); startWifiScan(); switch(mod){ + case ICLocation.CUSTOM_MOD: + if(runtime.getLocationInstance().getLocation_cycle()<60){ + nmeaListener.setNoStop(true); + }else{ + nmeaListener.setNoStop(false); + } case ICLocation.EMERGNECY_MOD: case ICLocation.REALTIME_MOD: - case ICLocation.CUSTOM_MOD: startGpsLocate(); break; } diff --git a/app/src/main/java/com/enqualcomm/support/NMEAListener.java b/app/src/main/java/com/enqualcomm/support/NMEAListener.java index 11113eaa..1379b860 100644 --- a/app/src/main/java/com/enqualcomm/support/NMEAListener.java +++ b/app/src/main/java/com/enqualcomm/support/NMEAListener.java @@ -33,13 +33,22 @@ public class NMEAListener implements NmeaListener, LocationListener { private double longitude; private double latitude; + private boolean noStop=false; + private int SearchTimeout = 120; private int noSignalCount=0; private int searchCout=0; private GPS_CURRENT_STATUS gpsMode; int numberOfSatellites; Runtime runtime; - + + + public void setNoStop(boolean nostop){ + noStop=nostop; + if (noStop==false){ + stopListen(); + } + } public NMEAListener(LocationManager paramLocationManager, Runtime paramRuntime) { this.locationManager = paramLocationManager; this.runtime = paramRuntime; @@ -136,16 +145,20 @@ public class NMEAListener implements NmeaListener, LocationListener { }; + private final int MSG_WHAT_GPS_FIXED=300; + private final int MSG_WHAT_GPS_TIMEOUT=100; + private final int MSG_WHAT_GPS_NOSTAR=200; + private final int MSG_WHAT_GPS_FIXED_NO_STOP=400; Handler temHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message message) { - if( message.what== 100|| message.what==200){ + if( message.what== MSG_WHAT_GPS_TIMEOUT|| message.what==MSG_WHAT_GPS_NOSTAR){ //no start //do nothing - }else if (message.what==300){ + }else if (message.what==MSG_WHAT_GPS_FIXED){ //fixed /* 只有当前GPS已经定位,且location不为空才给 runtime赋值GPS坐标。 @@ -153,10 +166,13 @@ public class NMEAListener implements NmeaListener, LocationListener { */ runtime.getLocationInstance().setLocationStat(ICLocation.LOCATION_STAT.LOCATION_FIXED); + }else if (message.what== MSG_WHAT_GPS_FIXED_NO_STOP){ + runtime.getLocationInstance().setLocationStat(ICLocation.LOCATION_STAT.LOCATION_FIXED); + return true; } NMEAListener.this.stopListen(); - return false; + return true; } }); private void get3MaxSnrSatellite(GpsSatellite gst){ @@ -225,14 +241,14 @@ public class NMEAListener implements NmeaListener, LocationListener { gpsMode=GPS_CURRENT_STATUS.GPS_TIMEOUT; runtime.getLocationInstance().setLocationStat(ICLocation.LOCATION_STAT.LOCATION_GPS_TIEMOUT); - stop(100); + stop(MSG_WHAT_GPS_TIMEOUT); } if (noSignalCount> 10){ sb2.append(" 搜索"+searchCout+"次,超过"+noSignalCount+"次没有收到星,退出GPS定位"); gpsMode=GPS_CURRENT_STATUS.GPS_NOSINGAL; runtime.getLocationInstance().setLocationStat(ICLocation.LOCATION_STAT.LOCATION_GPS_TIEMOUT); - stop(200); + stop(MSG_WHAT_GPS_NOSTAR); } if (Config.LOG_TOFILE) FileOutputUtil.fileOutput(this.runtime.getCacheDir(),"GPS",sb2.toString()); @@ -258,12 +274,16 @@ public class NMEAListener implements NmeaListener, LocationListener { } private void delaystop(){ + if (noStop){ + NMEAListener.this.temHandler.sendEmptyMessageDelayed(MSG_WHAT_GPS_FIXED_NO_STOP, 3000L); + }else { /* 只有当前GPS已经定位,且location不为空才给 runtime赋值GPS坐标。 延迟三秒后才设置runtime的定位标志,以便稳定坐标值。 */ - //发送停止消息 - NMEAListener.this.temHandler.sendEmptyMessageDelayed(300,3000L); + //发送停止消息 + NMEAListener.this.temHandler.sendEmptyMessageDelayed(MSG_WHAT_GPS_FIXED, 3000L); + } } boolean isFrist = true; @@ -348,28 +368,32 @@ public class NMEAListener implements NmeaListener, LocationListener { public boolean startListen() { - Criteria criteria = new Criteria(); - criteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置为最大精度 - criteria.setAltitudeRequired(false);//不要求海拔信息 - criteria.setBearingRequired(false);// 不要求方位信息 - criteria.setCostAllowed(true);//是否允许付费 - criteria.setPowerRequirement(Criteria.POWER_LOW);// 对电量的要求 - - String provider = locationManager.getBestProvider(criteria,true); - - Logger.i("GPS StartListen with Provider "+provider); - this.locationManager.requestLocationUpdates(provider, 0L, 0.0F, this); - this.listening = true; - noSignalCount=0; - longitude=0; - latitude=0; - searchCout=0; - gpsMode = GPS_CURRENT_STATUS.GPS_LISTENING; - locationManager.addGpsStatusListener(statusListener);//监听搜星状态 + if (this.listening == false) { + Criteria criteria = new Criteria(); + criteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置为最大精度 + criteria.setAltitudeRequired(false);//不要求海拔信息 + criteria.setBearingRequired(false);// 不要求方位信息 + criteria.setCostAllowed(true);//是否允许付费 + criteria.setPowerRequirement(Criteria.POWER_LOW);// 对电量的要求 + + String provider = locationManager.getBestProvider(criteria, true); + + Logger.i("GPS StartListen with Provider " + provider); + this.locationManager.requestLocationUpdates(provider, 0L, 0.0F, this); + this.listening = true; + noSignalCount = 0; + longitude = 0; + latitude = 0; + searchCout = 0; + gpsMode = GPS_CURRENT_STATUS.GPS_LISTENING; + locationManager.addGpsStatusListener(statusListener);//监听搜星状态 // this.locationManager.addNmeaListener(this); //做个热启动 - return gpsHotStart(); + return gpsHotStart(); + }else{ + return true; + } } diff --git a/app/src/main/java/com/enqualcomm/support/bean/ICLocation.java b/app/src/main/java/com/enqualcomm/support/bean/ICLocation.java index bb0addef..05daadb7 100644 --- a/app/src/main/java/com/enqualcomm/support/bean/ICLocation.java +++ b/app/src/main/java/com/enqualcomm/support/bean/ICLocation.java @@ -31,8 +31,8 @@ public class ICLocation { public static final int NORMAL_MOD=1; //location rate 1 time per 15 minutes public static final int LOWPOWER_MOD=2; // location rate 1 time per hour - public static final int EMERGNECY_MOD=3; //location rate 1 time per minutes - public static final int REALTIME_MOD=4; //location rate 1 time per minutes + public static final int REALTIME_MOD=3; //location rate 1 time per minutes + public static final int EMERGNECY_MOD=4; //location rate 1 time per minutes work 5 minutes public static final int CUSTOM_MOD=5; //location rate 1 time per minutes diff --git a/app/src/main/java/com/enqualcomm/support/utils/APNUtil.java b/app/src/main/java/com/enqualcomm/support/utils/APNUtil.java new file mode 100644 index 00000000..e283f4af --- /dev/null +++ b/app/src/main/java/com/enqualcomm/support/utils/APNUtil.java @@ -0,0 +1,127 @@ +package com.enqualcomm.support.utils; + +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.telephony.TelephonyManager; +import android.widget.Toast; + +public class APNUtil { + + /** + * 当前连接APN + * */ + private static Uri APN_URI_NOW =Uri.parse("content://telephony/carriers/current"); + /** + * 所有APN + * */ + private static Uri APN_URI = Uri.parse("content://telephony/carriers"); + + private static Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); + + public static String getCurrentAPN(Context context){ + ContentResolver resolver = context.getContentResolver(); + Cursor c = resolver.query(APN_URI_NOW, null, null, null, null); //从apn列表中查找apn名称为myapn的apn信息 + String APNSTR=""; + if (c != null && c.moveToNext()) { + int id = c.getShort(c.getColumnIndex("_id")); //获取该apn的id信息 + String mcc=c.getString(c.getColumnIndex("mcc")); + String mnc=c.getString(c.getColumnIndex("mnc")); + String name=c.getString(c.getColumnIndex("name")); + String apnname=c.getString(c.getColumnIndex("apn")); + String user=c.getString(c.getColumnIndex("user")); + String pwd=c.getString(c.getColumnIndex("password")); + APNSTR="Current APN "+id+","+mcc+","+mnc+","+name+","+apnname+","+user+","+pwd; + Logger.i(APNSTR); + } else { + Logger.i("APN不存在"); + return "APN 不存在"; + } + return APNSTR; + } + public static boolean addVPN(String apnName, String apn, String mcc,String mnc , String user,String passwd,Context context) { + + if (checkAPN(mcc,mnc,apn,context)==true){ + Logger.i("VPN 已经存在,不需要添加!"); + return false; + } + //添加新的APN到系统 + int id= AddNewApn(apnName,apn,mcc,mnc,user,passwd,context); + + //添加后设置为当前指定APN + SetAPN(id,context); + + + boolean isAddSuccess = checkAPN(mcc,mnc,apn,context); + if (isAddSuccess) { + Logger.i("APN 添加成功!"); + return true; +// Toast.makeText(NewAPNActivity.this, "新增APN成功", Toast.LENGTH_SHORT).show(); + } else { + Logger.i("APN 添加失败!"); + return false; +// Toast.makeText(NewAPNActivity.this, "新增APN失败", Toast.LENGTH_SHORT).show(); + } + } + // 设置接入点 + public static void SetAPN(int id,Context context) { + ContentResolver resolver = context.getContentResolver(); + ContentValues values = new ContentValues(); + values.put("apn_id", id); + resolver.update(CURRENT_APN_URI, values, null, null); + } + public static boolean checkAPN(String mcc,String mnc,String apn,Context context) { + // 检查系统已经写入的APN + Cursor cr = context.getContentResolver().query(APN_URI, null, null, null, null); + while (cr != null && cr.moveToNext()) { + if(cr.getString(cr.getColumnIndex("apn")).equals(apn) + && cr.getString(cr.getColumnIndex("mcc")).equals(mcc) + && cr.getString(cr.getColumnIndex("mnc")).equals(mnc)){ + return true; + } + } + return false; + } + + + public static int AddNewApn(String apnName,String apn, String mcc,String mnc , String user,String passwd, Context context) { + Logger.i("添加一个新的apn ==> "+apnName+", mcc="+mcc+", mnc="+mnc+", user="+user+", password="+passwd); + int apnId = -1; + ContentResolver resolver = context.getContentResolver(); + ContentValues values = new ContentValues(); + values.put("name", apnName); //apn中文描述 + values.put("apn", apn); //apn名称 + values.put("type", "default"); + values.put("numeric", mcc+mnc); + values.put("mcc", mcc); + values.put("mnc", mnc); + values.put("proxy", ""); + values.put("port", ""); + values.put("mmsproxy", ""); + values.put("mmsport", ""); + values.put("user", user); + values.put("server", ""); + values.put("password", passwd); + values.put("mmsc", ""); + Cursor c = null; +// 获取新添加的apn的ID + try { + Uri newRow = resolver.insert(APN_URI, values); + if (newRow != null) { + c = resolver.query(newRow, null, null, null, null); + int idindex = c.getColumnIndex("_id"); + c.moveToFirst(); + apnId = idindex; + } + } catch (Exception e) { + e.printStackTrace(); + } + if (c != null) + c.close(); + return apnId; + } + + +} diff --git a/app/src/main/java/com/enqualcomm/support/utils/AlarmHelper.java b/app/src/main/java/com/enqualcomm/support/utils/AlarmHelper.java index 2ce56df9..74204990 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/AlarmHelper.java +++ b/app/src/main/java/com/enqualcomm/support/utils/AlarmHelper.java @@ -93,7 +93,29 @@ public class AlarmHelper { } alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, l + (minutes * 60 * 1000), pendingIntent); } - + + public static void setRealTimeAlarmInSeconds(Context context, int seconds, String actionname) { + AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + Intent intent = new Intent(context, com.enqualcomm.support.CoreService.class); + intent.setAction(actionname); + PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + alarmManager.cancel(pendingIntent); + long l = SystemClock.elapsedRealtime(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("SDK_INT="); + stringBuilder.append(Build.VERSION.SDK_INT); + stringBuilder.append(",m=,"); + stringBuilder.append(23); + stringBuilder.append(",kitkat="); + stringBuilder.append(19); + Logger.i(stringBuilder.toString()); + if (Build.VERSION.SDK_INT >= 19) { + alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, l + (seconds * 1000), pendingIntent); + return; + } + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, l + (seconds * 1000), pendingIntent); + } + public static void setTimeAlarm(Context context, int hour, int minutes, String paramString) { AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, com.enqualcomm.support.CoreService.class); diff --git a/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java b/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java index 4704db9e..385ea12f 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java +++ b/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java @@ -11,6 +11,7 @@ import android.os.Build; import com.enqualcomm.support.Config; import com.enqualcomm.support.CoreService; +import com.enqualcomm.support.adapter.protocolAPIHelper; import com.enqualcomm.support.bean.Runtime; import static android.content.Context.BATTERY_SERVICE; @@ -87,10 +88,10 @@ public class BatteryUtil { BatteryUtil.this.mRuntime.setBatteryLow(true); Logger.i("lowbattery finish video_end_call"); + mRuntime.setAlarmType(false,mRuntime.getBatteryLow(),false,false); + //trigger AP10 报警定位 + connection.send(protocolAPIHelper.genStringMessage("AP10",protocolAPIHelper.AP10(mRuntime))); - ComponentName componentName = new ComponentName("com.enqualcomm.support", "com.enqualcomm.support.CoreService"); - Intent intent = (new Intent(ACTION_BATTERY_LOW_MSG)).setComponent(componentName); - (mRuntime.getContextProxy()).context.startService(intent); } } }; @@ -98,8 +99,11 @@ public class BatteryUtil { private StringBuffer mBuffer; Runtime mRuntime; - - public BatteryUtil(Runtime paramRuntime) { this.mRuntime = paramRuntime; } + Connection connection; + public BatteryUtil(Runtime paramRuntime,Connection _connection) { + this.mRuntime = paramRuntime; + connection = _connection; + } public void registerBroadReceiver() { this.mBuffer = new StringBuffer(); @@ -114,8 +118,7 @@ public class BatteryUtil { BatteryManager batteryManager = (BatteryManager)context.getSystemService(BATTERY_SERVICE); return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); } else { - Intent intent = new ContextWrapper(context.getApplicationContext()). - registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + Intent intent = new ContextWrapper(context.getApplicationContext()).registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); return (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); } diff --git a/dNSCache/src/main/java/com/sina/util/dnscache/DNSCache.java b/dNSCache/src/main/java/com/sina/util/dnscache/DNSCache.java index 798c0d62..b1f82613 100644 --- a/dNSCache/src/main/java/com/sina/util/dnscache/DNSCache.java +++ b/dNSCache/src/main/java/com/sina/util/dnscache/DNSCache.java @@ -68,7 +68,7 @@ public class DNSCache { scoreManager = new ScoreManager(); dnsManager = new DnsManager(); speedtestManager = new SpeedtestManager(); - startTimer(); +// startTimer(); } public static DNSCache getInstance() { -- 2.17.1