-package com.enqualcomm.support.utils;\r
-\r
-import android.content.BroadcastReceiver;\r
-import android.content.Context;\r
-import android.content.ContextWrapper;\r
-import android.content.Intent;\r
-import android.content.IntentFilter;\r
-import android.os.BatteryManager;\r
-import android.os.Build;\r
-import android.os.Handler;\r
-import android.os.Message;\r
-import android.os.PowerManager;\r
-\r
-import com.enqualcomm.support.Config;\r
-import com.enqualcomm.support.CoreService;\r
-import com.enqualcomm.support.LocationClient;\r
-import com.enqualcomm.support.service.AntiMonitorService;\r
-import com.enqualcomm.support.service.AudioService;\r
-import com.enqualcomm.support.service.Deliver;\r
-import com.ic.protocols.protocolAPIHelper;\r
-import com.enqualcomm.support.bean.Runtime;\r
-\r
-//import static android.content.Context.BATTERY_SERVICE;\r
-import static android.os.BatteryManager.BATTERY_STATUS_CHARGING;\r
-import static android.os.BatteryManager.BATTERY_STATUS_DISCHARGING;\r
-import static android.os.BatteryManager.BATTERY_STATUS_FULL;\r
-import static android.os.BatteryManager.BATTERY_STATUS_NOT_CHARGING;\r
-import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;\r
-import static android.os.BatteryManager.EXTRA_LEVEL;\r
-import static android.os.BatteryManager.EXTRA_SCALE;\r
-import static android.os.BatteryManager.EXTRA_STATUS;\r
-import static android.os.BatteryManager.EXTRA_VOLTAGE;\r
-import static com.enqualcomm.support.Constants.ACTION_BATTERYLOW;\r
-import static com.enqualcomm.support.Constants.KEY_BATTERY_EVENT;\r
-\r
-public class BatteryUtil {\r
- private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() {\r
- public void onReceive(Context param1Context, Intent intent) {\r
- if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {\r
- int extra_status = intent.getIntExtra(EXTRA_STATUS , BATTERY_STATUS_UNKNOWN );\r
- StringBuilder msg=new StringBuilder("EXTRA_STATUS = ");\r
- switch (extra_status){\r
- case BATTERY_STATUS_CHARGING:\r
- msg.append("BATTERY_STATUS_CHARGING ");\r
- break;\r
- case BATTERY_STATUS_UNKNOWN:\r
- msg.append("BATTERY_STATUS_UNKNOWN ");\r
- break;\r
- case BATTERY_STATUS_DISCHARGING:\r
- msg.append("BATTERY_STATUS_DISCHARGING ");\r
- break;\r
- case BATTERY_STATUS_FULL:\r
- msg.append("BATTERY_STATUS_FULL ");\r
- if (Config.isL03Tracker){\r
- LedUtil.setRedLedOn(false);\r
- LedUtil.setBlueLedOn(true);\r
- }\r
- break;\r
- case BATTERY_STATUS_NOT_CHARGING:\r
- msg.append("BATTERY_STATUS_NOT_CHARGING ");\r
- break;\r
-\r
- }\r
- msg.append("; ");\r
- if (extra_status == BATTERY_STATUS_CHARGING) {\r
- if(mRuntime.getBatterychanging()==false){\r
- ICLogger.i("Send BatteryCharging event!");\r
- connection.send(protocolAPIHelper.getChargingMSG(1));\r
- }\r
- mRuntime.setBatterychanging(true);\r
- mRuntime.setBatteryLow(false);\r
- } else {\r
- if(mRuntime.getBatterychanging()==true){\r
- //send\r
- ICLogger.i("Send no BatteryCharging event!");\r
- connection.send(protocolAPIHelper.getChargingMSG(0));\r
- }\r
- mRuntime.setBatterychanging(false);\r
- }\r
- int batLevel = intent.getIntExtra(EXTRA_LEVEL, 0) * 100 / intent.getIntExtra(EXTRA_SCALE, 100);\r
- // 当前电池电压\r
- int batteryVolt = intent.getIntExtra(EXTRA_VOLTAGE, -1);\r
- mRuntime.setBatteryVoltage(batteryVolt);\r
- msg.append("BatteryLevel "+batLevel + " BatteryVotage "+batteryVolt+"; ");\r
- if (Config.LOG_TOFILE)ICLogger.logToFile("Battery",msg.toString());\r
- ICLogger.i(msg.toString());\r
- if (batLevel >= 100) {\r
- mRuntime.setBattery(String.format("%03d",100));\r
- mRuntime.setBattery(100);\r
- mRuntime.setHaveBatteryChange(false);\r
- mRuntime.setBatteryLow(false);\r
- } else if (batLevel<100 && batLevel >= 25) {\r
- mRuntime.setBattery(String.format("%03d",batLevel));\r
- mRuntime.setBattery(batLevel);\r
- mRuntime.setHaveBatteryChange(false);\r
- mRuntime.setBatteryLow(false);\r
- } else if (batLevel<25 && batLevel > 15) {\r
- mRuntime.setBattery(String.format("%03d",batLevel));\r
- mRuntime.setBattery(batLevel);\r
- mRuntime.setHaveBatteryChange(true);\r
-\r
- } else if (batLevel<=15 && batLevel > 5) {\r
-\r
-\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 15%!");\r
- checkAndSendBatteryEvent(param1Context,mRuntime);\r
- mRuntime.setBattery(String.format("%03d",batLevel));\r
- mRuntime.setBattery(batLevel);\r
- mRuntime.setHaveBatteryChange(true);\r
- mRuntime.setBatteryLow(true);\r
- } else {\r
-\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 5% !");\r
- checkAndSendBatteryEvent(param1Context,mRuntime);\r
- mRuntime.setBattery(String.format("%03d",batLevel));\r
- mRuntime.setBattery(batLevel);\r
- mRuntime.setHaveBatteryChange(true);\r
- mRuntime.setBatteryLow(true);\r
- }\r
-\r
- }else if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {\r
- // mRuntime.setBatteryLow(true);\r
- // if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow !");\r
- // mRuntime.setAlarmType(false,mRuntime.getBatteryLow(),false,false);\r
- //trigger AP10 报警定位\r
- // connection.send(protocolAPIHelper.genStringMessage("AP10",protocolAPIHelper.getWarningMSG(mRuntime)));\r
-\r
- }else if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())){\r
- ICLogger.i("Battery OKAY !");\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","Battery OKAY !");\r
- send_batterylow_event_broadcast(param1Context,0);\r
- }else if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())){\r
- ICLogger.i("ACTION_POWER_CONNECTED !");\r
- mRuntime.setPowerConnected(true);\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","ACTION_POWER_CONNECTED!");\r
- if (Config.isL03Tracker){\r
- LedUtil.setRedLedOn(true);\r
- }\r
-\r
- }else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())){\r
- ICLogger.i("ACTION_POWER_DISCONNECTED!");\r
- mRuntime.setPowerConnected(false);\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","ACTION_POWER_DISCONNECTED !");\r
-\r
- if (Config.isL03Tracker){\r
- LedUtil.setRedLedOn(false);\r
- }\r
- }\r
- }\r
- };\r
- private Handler mHandler=null;\r
- PowerManager.WakeLock mWakeLock = null;\r
-\r
- private static void play_waning_notify(Context context){\r
- AudioService.getInstance().play(context.getAssets(), "warning.mp3");\r
- }\r
-\r
- public static void checkAndSendBatteryEvent(Context context, Runtime runtime){\r
-\r
- //没有充电,上次电池level > 15% , 上次电池并且不是 低电状态,则报警\r
- if (runtime.isPowerConnected()==false && runtime.getBatterylevel()<= 10 && runtime.getBatterylevel()> 5 && runtime.getBatteryLow()==false) {\r
- Intent intent=new Intent();\r
- intent.setAction(ACTION_BATTERYLOW);\r
- intent.putExtra(KEY_BATTERY_EVENT,1);\r
- context.sendBroadcast(intent);\r
- play_waning_notify(context);\r
- runtime.setAlarmType(Runtime.AlarmType.ALARM_LOWBATTERY);\r
- //trigger AP10 报警定位\r
-// Deliver.sendLocationMSG(protocolAPIHelper.getWarningMSG(runtime));\r
- ICLogger.i("triggerSOSLocationProcess ");\r
- LocationClient.getInstance().triggerSOSLocationProcess();\r
- }else if (runtime.isPowerConnected()==false && runtime.getBatterylevel()<= 5 ) {\r
- //没有充电,上次电池level > 15% , 上次电池并且不是 电池低电状态,则报警\r
- Intent intent=new Intent();\r
- intent.setAction(ACTION_BATTERYLOW);\r
- intent.putExtra(KEY_BATTERY_EVENT,1);\r
- context.sendBroadcast(intent);\r
- play_waning_notify(context);\r
- runtime.setAlarmType(Runtime.AlarmType.ALARM_LOWBATTERY);\r
- //trigger AP10 报警定位\r
-// Deliver.sendLocationMSG( protocolAPIHelper.getWarningMSG(runtime));\r
- ICLogger.i("triggerSOSLocationProcess ");\r
- LocationClient.getInstance().triggerSOSLocationProcess();\r
- }\r
- }\r
- private void send_batterylow_event_broadcast(Context context,int batlow){\r
- mHandler.post(new Runnable() {\r
- @Override\r
- public void run() {\r
- Intent intent=new Intent();\r
- intent.setAction(ACTION_BATTERYLOW);\r
- intent.putExtra(KEY_BATTERY_EVENT,batlow);\r
- context.sendBroadcast(intent);\r
- }\r
- });\r
- }\r
- private StringBuffer mBuffer;\r
-\r
- Runtime mRuntime;\r
- Connection connection;\r
- public BatteryUtil(Runtime paramRuntime,Connection _connection) {\r
- this.mRuntime = paramRuntime;\r
- connection = _connection;\r
-\r
- if (mHandler==null) {\r
- mHandler = new Handler() {\r
- public void handleMessage(Message msg) {\r
- super.handleMessage(msg);\r
- if (msg.what == 100) {\r
- if (mWakeLock != null && mWakeLock.isHeld()) {\r
- mWakeLock.release();\r
- }\r
- }else if (msg.what == 200){\r
- AudioService.getInstance().stop();\r
- }\r
- }\r
- };\r
- }\r
- if (mWakeLock==null) {\r
- PowerManager mPowerManager = (PowerManager) CoreService.getInstance().getSystemService(Context.POWER_SERVICE);\r
- mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, AntiMonitorService.class.getSimpleName());\r
- }\r
-\r
- }\r
- \r
- public void registerBroadReceiver() {\r
- IntentFilter intentFilter = new IntentFilter();\r
- intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);\r
- intentFilter.addAction(Intent.ACTION_BATTERY_LOW);\r
- intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);\r
- intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);\r
- intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);\r
- (this.mRuntime.getContextProxy()).context.registerReceiver(this.batteryChangedReceiver, intentFilter);\r
- }\r
-\r
- public int getBatteryLevel(Context context) {\r
-// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {\r
-// BatteryManager batteryManager = (BatteryManager)context.getSystemService(BATTERY_SERVICE);\r
-// return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);\r
-// } else {\r
- Intent intent = new ContextWrapper(context.getApplicationContext()).registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));\r
- int batLevel = (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) /\r
- intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);\r
-\r
- if( batLevel>15) {\r
-\r
-\r
- }else if (batLevel<=15 && batLevel > 5) {\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 15%!");\r
- checkAndSendBatteryEvent(context,mRuntime);\r
- mRuntime.setBattery(String.format("%03d",batLevel));\r
- mRuntime.setBattery(batLevel);\r
- mRuntime.setHaveBatteryChange(true);\r
- mRuntime.setBatteryLow(true);\r
- } else {\r
- if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 5% !");\r
- checkAndSendBatteryEvent(context,mRuntime);\r
- mRuntime.setBattery(String.format("%03d",batLevel));\r
- mRuntime.setBattery(batLevel);\r
- mRuntime.setHaveBatteryChange(true);\r
- mRuntime.setBatteryLow(true);\r
- }\r
- return batLevel;\r
- }\r
-\r
-\r
- public boolean isPowerConnected(Context context){\r
-\r
- IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);\r
- Intent batteryStatus = context.registerReceiver(null, ifilter);\r
-//你可以读到充电状态,如果在充电,可以读到是usb还是交流电\r
-\r
-// 是否在充电\r
- int status = batteryStatus.getIntExtra(EXTRA_STATUS, -1);\r
- boolean isCharging = (status == BATTERY_STATUS_CHARGING) ;//|| status == BatteryManager.BATTERY_STATUS_FULL;\r
- boolean isCharging2 = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;\r
- ICLogger.d("isCharging = " +isCharging+" isCharging2 "+ isCharging2 );\r
- return isCharging2;\r
- }\r
-\r
- public void unregisterBroadReceiver() { (this.mRuntime.getContextProxy()).context.unregisterReceiver(this.batteryChangedReceiver); }\r
-}\r
-\r
-\r
+package com.enqualcomm.support.utils;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.ContextWrapper;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.BatteryManager;
+import android.os.Build;
+import android.os.Handler;
+import android.os.Message;
+import android.os.PowerManager;
+
+import com.enqualcomm.support.Config;
+import com.enqualcomm.support.CoreService;
+import com.enqualcomm.support.LocationClient;
+import com.enqualcomm.support.service.AntiMonitorService;
+import com.enqualcomm.support.service.AudioService;
+import com.enqualcomm.support.service.Deliver;
+import com.ic.protocols.protocolAPIHelper;
+import com.enqualcomm.support.bean.Runtime;
+
+//import static android.content.Context.BATTERY_SERVICE;
+import static android.os.BatteryManager.BATTERY_STATUS_CHARGING;
+import static android.os.BatteryManager.BATTERY_STATUS_DISCHARGING;
+import static android.os.BatteryManager.BATTERY_STATUS_FULL;
+import static android.os.BatteryManager.BATTERY_STATUS_NOT_CHARGING;
+import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
+import static android.os.BatteryManager.EXTRA_LEVEL;
+import static android.os.BatteryManager.EXTRA_SCALE;
+import static android.os.BatteryManager.EXTRA_STATUS;
+import static android.os.BatteryManager.EXTRA_VOLTAGE;
+import static com.enqualcomm.support.Constants.ACTION_BATTERYLOW;
+import static com.enqualcomm.support.Constants.KEY_BATTERY_EVENT;
+
+public class BatteryUtil {
+ private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() {
+ public void onReceive(Context param1Context, Intent intent) {
+ if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
+ int extra_status = intent.getIntExtra(EXTRA_STATUS , BATTERY_STATUS_UNKNOWN );
+ StringBuilder msg=new StringBuilder("EXTRA_STATUS = ");
+ switch (extra_status){
+ case BATTERY_STATUS_CHARGING:
+ msg.append("BATTERY_STATUS_CHARGING ");
+ break;
+ case BATTERY_STATUS_UNKNOWN:
+ msg.append("BATTERY_STATUS_UNKNOWN ");
+ break;
+ case BATTERY_STATUS_DISCHARGING:
+ msg.append("BATTERY_STATUS_DISCHARGING ");
+ break;
+ case BATTERY_STATUS_FULL:
+ msg.append("BATTERY_STATUS_FULL ");
+ if (Config.isL03Tracker){
+ LedUtil.setRedLedOn(false);
+ LedUtil.setBlueLedOn(true);
+ }
+ break;
+ case BATTERY_STATUS_NOT_CHARGING:
+ msg.append("BATTERY_STATUS_NOT_CHARGING ");
+ break;
+
+ }
+ msg.append("; ");
+ if (extra_status == BATTERY_STATUS_CHARGING) {
+ if(mRuntime.getBatterychanging()==false){
+ ICLogger.i("Send BatteryCharging event!");
+ connection.send(protocolAPIHelper.getChargingMSG(1));
+ }
+ mRuntime.setBatterychanging(true);
+ mRuntime.setBatteryLow(false);
+ } else {
+ if(mRuntime.getBatterychanging()==true){
+ //send
+ ICLogger.i("Send no BatteryCharging event!");
+ connection.send(protocolAPIHelper.getChargingMSG(0));
+ }
+ mRuntime.setBatterychanging(false);
+ }
+ int batLevel = intent.getIntExtra(EXTRA_LEVEL, 0) * 100 / intent.getIntExtra(EXTRA_SCALE, 100);
+ // 当前电池电压
+ int batteryVolt = intent.getIntExtra(EXTRA_VOLTAGE, -1);
+ mRuntime.setBatteryVoltage(batteryVolt);
+ msg.append("BatteryLevel "+batLevel + " BatteryVotage "+batteryVolt+"; ");
+ if (Config.LOG_TOFILE)ICLogger.logToFile("Battery",msg.toString());
+ ICLogger.i(msg.toString());
+ if (batLevel >= 100) {
+ mRuntime.setBattery(String.format("%03d",100));
+ mRuntime.setBattery(100);
+ mRuntime.setHaveBatteryChange(false);
+ mRuntime.setBatteryLow(false);
+ } else if (batLevel<100 && batLevel >= 25) {
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(false);
+ mRuntime.setBatteryLow(false);
+ } else if (batLevel<25 && batLevel > 15) {
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(true);
+ } else if (batLevel<=15 && batLevel > 10) {
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 15%!");
+ checkAndSendBatteryEvent(param1Context,mRuntime);
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(true);
+ mRuntime.setBatteryLow(true);
+ } else if (batLevel<=10 && batLevel > 5) {
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 10%!");
+ checkAndSendBatteryEvent(param1Context,mRuntime);
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(true);
+ mRuntime.setBatteryLow(true);
+ } else {
+
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 5% !");
+ checkAndSendBatteryEvent(param1Context,mRuntime);
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(true);
+ mRuntime.setBatteryLow(true);
+ }
+
+ }else if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {
+ // mRuntime.setBatteryLow(true);
+ // if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow !");
+ // mRuntime.setAlarmType(false,mRuntime.getBatteryLow(),false,false);
+ //trigger AP10 报警定位
+ // connection.send(protocolAPIHelper.genStringMessage("AP10",protocolAPIHelper.getWarningMSG(mRuntime)));
+
+ }else if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())){
+ ICLogger.i("Battery OKAY !");
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","Battery OKAY !");
+ send_batterylow_event_broadcast(param1Context,0);
+ }else if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())){
+ ICLogger.i("ACTION_POWER_CONNECTED !");
+ mRuntime.setPowerConnected(true);
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","ACTION_POWER_CONNECTED!");
+ if (Config.isL03Tracker){
+ LedUtil.setRedLedOn(true);
+ }
+
+ }else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())){
+ ICLogger.i("ACTION_POWER_DISCONNECTED!");
+ mRuntime.setPowerConnected(false);
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","ACTION_POWER_DISCONNECTED !");
+
+ if (Config.isL03Tracker){
+ LedUtil.setRedLedOn(false);
+ }
+ }
+ }
+ };
+ private Handler mHandler=null;
+ PowerManager.WakeLock mWakeLock = null;
+
+ private static void play_waning_notify(Context context){
+ AudioService.getInstance().play(context.getAssets(), "warning.mp3");
+ }
+
+ public static void checkAndSendBatteryEvent(Context context, Runtime runtime){
+
+ //没有充电,上次电池level > 15% , 上次电池并且不是 低电状态,则报警
+ if (runtime.isPowerConnected()==false && runtime.getBatterylevel()<= 15 && runtime.getBatterylevel()> 10 && runtime.getBatteryLow()==false) {
+ Intent intent = new Intent();
+ intent.setAction(ACTION_BATTERYLOW);
+ intent.putExtra(KEY_BATTERY_EVENT, 1);
+ context.sendBroadcast(intent);
+ play_waning_notify(context);
+ runtime.setAlarmType(Runtime.AlarmType.ALARM_LOWBATTERY);
+ //trigger AP10 报警定位
+// Deliver.sendLocationMSG(protocolAPIHelper.getWarningMSG(runtime));
+ ICLogger.i("triggerSOSLocationProcess ");
+ LocationClient.getInstance().triggerSOSLocationProcess();
+ }else if (runtime.isPowerConnected()==false && runtime.getBatterylevel()<= 10 && runtime.getBatterylevel()> 5 && runtime.getBatteryLow()==false) {
+ //百分之10以内的还不能正常报数,需要后面再优化
+ Intent intent=new Intent();
+ intent.setAction(ACTION_BATTERYLOW);
+ intent.putExtra(KEY_BATTERY_EVENT,1);
+ context.sendBroadcast(intent);
+ play_waning_notify(context);
+ runtime.setAlarmType(Runtime.AlarmType.ALARM_LOWBATTERY);
+ //trigger AP10 报警定位
+// Deliver.sendLocationMSG(protocolAPIHelper.getWarningMSG(runtime));
+ ICLogger.i("triggerSOSLocationProcess ");
+ LocationClient.getInstance().triggerSOSLocationProcess();
+ }else if (runtime.isPowerConnected()==false && runtime.getBatterylevel()<= 5 ) {
+ //没有充电,上次电池level > 15% , 上次电池并且不是 电池低电状态,则报警
+ Intent intent=new Intent();
+ intent.setAction(ACTION_BATTERYLOW);
+ intent.putExtra(KEY_BATTERY_EVENT,1);
+ context.sendBroadcast(intent);
+ play_waning_notify(context);
+ runtime.setAlarmType(Runtime.AlarmType.ALARM_LOWBATTERY);
+ //trigger AP10 报警定位
+// Deliver.sendLocationMSG( protocolAPIHelper.getWarningMSG(runtime));
+ ICLogger.i("triggerSOSLocationProcess ");
+ LocationClient.getInstance().triggerSOSLocationProcess();
+ }
+ }
+ private void send_batterylow_event_broadcast(Context context,int batlow){
+ mHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ Intent intent=new Intent();
+ intent.setAction(ACTION_BATTERYLOW);
+ intent.putExtra(KEY_BATTERY_EVENT,batlow);
+ context.sendBroadcast(intent);
+ }
+ });
+ }
+ private StringBuffer mBuffer;
+
+ Runtime mRuntime;
+ Connection connection;
+ public BatteryUtil(Runtime paramRuntime,Connection _connection) {
+ this.mRuntime = paramRuntime;
+ connection = _connection;
+
+ if (mHandler==null) {
+ mHandler = new Handler() {
+ public void handleMessage(Message msg) {
+ super.handleMessage(msg);
+ if (msg.what == 100) {
+ if (mWakeLock != null && mWakeLock.isHeld()) {
+ mWakeLock.release();
+ }
+ }else if (msg.what == 200){
+ AudioService.getInstance().stop();
+ }
+ }
+ };
+ }
+ 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());
+ }
+
+ }
+
+ public void registerBroadReceiver() {
+ IntentFilter intentFilter = new IntentFilter();
+ intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
+ intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
+ intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
+ intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
+ intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
+ (this.mRuntime.getContextProxy()).context.registerReceiver(this.batteryChangedReceiver, intentFilter);
+ }
+
+ public int getBatteryLevel(Context context) {
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+// 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));
+ int batLevel = (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) /
+ intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
+
+ if( batLevel>15) {
+
+
+ }else if (batLevel<=15 && batLevel > 5) {
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 15%!");
+ checkAndSendBatteryEvent(context,mRuntime);
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(true);
+ mRuntime.setBatteryLow(true);
+ } else {
+ if (Config.LOG_TOFILE) ICLogger.logToFile("Battery","BatteryLow less than 5% !");
+ checkAndSendBatteryEvent(context,mRuntime);
+ mRuntime.setBattery(String.format("%03d",batLevel));
+ mRuntime.setBattery(batLevel);
+ mRuntime.setHaveBatteryChange(true);
+ mRuntime.setBatteryLow(true);
+ }
+ return batLevel;
+ }
+
+
+ public boolean isPowerConnected(Context context){
+
+ IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
+ Intent batteryStatus = context.registerReceiver(null, ifilter);
+//你可以读到充电状态,如果在充电,可以读到是usb还是交流电
+
+// 是否在充电
+ int status = batteryStatus.getIntExtra(EXTRA_STATUS, -1);
+ boolean isCharging = (status == BATTERY_STATUS_CHARGING) ;//|| status == BatteryManager.BATTERY_STATUS_FULL;
+ boolean isCharging2 = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
+ ICLogger.d("isCharging = " +isCharging+" isCharging2 "+ isCharging2 );
+ return isCharging2;
+ }
+
+ public void unregisterBroadReceiver() { (this.mRuntime.getContextProxy()).context.unregisterReceiver(this.batteryChangedReceiver); }
+}
+
+