@ -42,6 +42,10 @@ import com.xypower.mpmaster.sms.SmsSendReceiver;
import org.json.JSONObject ;
import org.json.JSONObject ;
import java.io.File ;
import java.io.File ;
import java.util.Arrays ;
import java.util.Calendar ;
import java.util.List ;
import java.util.TimeZone ;
import java.util.logging.FileHandler ;
import java.util.logging.FileHandler ;
import java.util.logging.Level ;
import java.util.logging.Level ;
import java.util.logging.Logger ;
import java.util.logging.Logger ;
@ -83,6 +87,7 @@ public class MpMasterService extends Service {
public static final int CONNECTED = 10 ;
public static final int CONNECTED = 10 ;
public static final int NOT_CONNECTED = 0 ;
public static final int NOT_CONNECTED = 0 ;
}
}
private static int mStateService = STATE_SERVICE . NOT_CONNECTED ;
private static int mStateService = STATE_SERVICE . NOT_CONNECTED ;
private boolean mMntnMode = false ;
private boolean mMntnMode = false ;
@ -120,6 +125,7 @@ public class MpMasterService extends Service {
public MpMasterService ( ) {
public MpMasterService ( ) {
}
}
@Override
@Override
public IBinder onBind ( Intent intent ) {
public IBinder onBind ( Intent intent ) {
// TODO: Return the communication channel to the service.
// TODO: Return the communication channel to the service.
@ -226,7 +232,7 @@ public class MpMasterService extends Service {
mSmsSnedReceiver = new SmsSendReceiver ( ) ;
mSmsSnedReceiver = new SmsSendReceiver ( ) ;
IntentFilter intentFilter ;
IntentFilter intentFilter ;
intentFilter = new IntentFilter ( SimUtil . SMS_SEND_ACTION ) ;
intentFilter = new IntentFilter ( SimUtil . SMS_SEND_ACTION ) ;
registerReceiver ( mSmsSnedReceiver , intentFilter ) ;
registerReceiver ( mSmsSnedReceiver , intentFilter ) ;
// AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
@ -294,8 +300,7 @@ public class MpMasterService extends Service {
}
}
private void loadIccid ( ) {
private void loadIccid ( ) {
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . LOLLIPOP_MR1 )
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . LOLLIPOP_MR1 ) {
{
try {
try {
SubscriptionManager sm = SubscriptionManager . from ( this ) ;
SubscriptionManager sm = SubscriptionManager . from ( this ) ;
List < SubscriptionInfo > sis = sm . getActiveSubscriptionInfoList ( ) ;
List < SubscriptionInfo > sis = sm . getActiveSubscriptionInfoList ( ) ;
@ -410,8 +415,7 @@ public class MpMasterService extends Service {
MicroPhotoContext . saveMasterConfig ( getApplicationContext ( ) , masterConfig ) ;
MicroPhotoContext . saveMasterConfig ( getApplicationContext ( ) , masterConfig ) ;
logger . warning ( "MNTN Mode Changed from " + convertSwitch ( oldMntnMode ) + " to " + convertSwitch ( mntnMode )
logger . warning ( "MNTN Mode Changed from " + convertSwitch ( oldMntnMode ) + " to " + convertSwitch ( mntnMode ) + " Quick Heartbeat from" + convertSwitch ( oldQuickHbMode ) + " to " + convertSwitch ( quickHbMode ) ) ;
+ " Quick Heartbeat from" + convertSwitch ( oldQuickHbMode ) + " to " + convertSwitch ( quickHbMode ) ) ;
}
}
if ( oldQuickHbMode ! = quickHbMode ) {
if ( oldQuickHbMode ! = quickHbMode ) {
@ -442,12 +446,15 @@ public class MpMasterService extends Service {
public static class AlarmReceiver extends BroadcastReceiver {
public static class AlarmReceiver extends BroadcastReceiver {
private MpMasterService mService ;
private MpMasterService mService ;
public AlarmReceiver ( ) {
public AlarmReceiver ( ) {
mService = null ;
mService = null ;
}
}
public AlarmReceiver ( MpMasterService service ) {
public AlarmReceiver ( MpMasterService service ) {
mService = service ;
mService = service ;
}
}
public void onReceive ( Context context , Intent intent ) {
public void onReceive ( Context context , Intent intent ) {
String action = intent . getAction ( ) ;
String action = intent . getAction ( ) ;
if ( TextUtils . equals ( ACTION_HEARTBEAT , action ) ) {
if ( TextUtils . equals ( ACTION_HEARTBEAT , action ) ) {
@ -480,24 +487,15 @@ public class MpMasterService extends Service {
String cmd = intent . getStringExtra ( "cmd" ) ;
String cmd = intent . getStringExtra ( "cmd" ) ;
String msg = intent . getStringExtra ( "msg" ) ;
String msg = intent . getStringExtra ( "msg" ) ;
// Log.e("_otg_","cmd="+cmd);
// Log.e("_otg_","cmd="+cmd);
if ( "write" . equals ( cmd ) )
if ( "write" . equals ( cmd ) ) {
{
// int progress = Integer.parseInt(msg);
// int progress = Integer.parseInt(msg);
}
} else if ( "update" . equals ( cmd ) ) {
else if ( "update" . equals ( cmd ) )
{
// int progress = Integer.parseInt(msg);
// int progress = Integer.parseInt(msg);
}
} else if ( "info" . equals ( cmd ) ) {
else if ( "info" . equals ( cmd ) )
{
}
} else if ( "error" . equals ( cmd ) ) {
else if ( "error" . equals ( cmd ) )
{
mService . logger . warning ( "UPD OTA Failed" ) ;
mService . logger . warning ( "UPD OTA Failed" ) ;
}
} else if ( "success" . equals ( cmd ) ) {
else if ( "success" . equals ( cmd ) )
{
//confirm to reboot device ??
//confirm to reboot device ??
mService . logger . warning ( "UPD OTA Succeeded, will RESET dev" ) ;
mService . logger . warning ( "UPD OTA Succeeded, will RESET dev" ) ;
Handler handler = new Handler ( ) ;
Handler handler = new Handler ( ) ;
@ -528,11 +526,45 @@ public class MpMasterService extends Service {
long timeout = mHeartbeatDuration ;
long timeout = mHeartbeatDuration ;
if ( mMntnMode & & mQuickHbMode ) {
if ( mMntnMode & & mQuickHbMode ) {
timeout = mQuickHeartbeatDuration ;
timeout = mQuickHeartbeatDuration ;
registerHeartbeatTimer ( SystemClock . elapsedRealtime ( ) + timeout * 1000 ) ;
} else {
} else {
long closestTime = 0 ;
if ( mAbsHeartbeatTimes ! = null & & mAbsHeartbeatTimes . length > 0 ) {
long currentTimeMillis = System . currentTimeMillis ( ) ;
long heartTimeMills = currentTimeMillis + timeout * 1000 ;
Arrays . sort ( mAbsHeartbeatTimes ) ;
for ( int i = 0 ; i < mAbsHeartbeatTimes . length ; i + + ) {
int mAbsHeartbeatTime = mAbsHeartbeatTimes [ i ] ;
int hour = mAbsHeartbeatTime / 3600 ;
int leftsecond = mAbsHeartbeatTime % 3600 ;
int minute = leftsecond / 3600 ;
if ( hour > = 24 ) {
return ;
}
//设置提醒时间
Calendar calendar = Calendar . getInstance ( ) ;
//这里时区需要设置一下, 不然会有8个小时的时间差
calendar . setTimeZone ( TimeZone . getTimeZone ( "GMT+8" ) ) ;
//设置每天为13: 50点提醒
calendar . set ( Calendar . HOUR_OF_DAY , hour ) ;
calendar . set ( Calendar . MINUTE , minute ) ;
calendar . set ( Calendar . SECOND , 0 ) ;
calendar . set ( Calendar . MILLISECOND , 0 ) ;
long selecttime = calendar . getTimeInMillis ( ) ;
if ( selecttime > currentTimeMillis & & selecttime < heartTimeMills ) {
closestTime = selecttime ;
break ;
} else {
if ( i = = ( mAbsHeartbeatTimes . length - 1 ) ) {
closestTime = heartTimeMills ;
}
}
}
}
if ( closestTime > 0 ) {
registerAbsHeartbeatTimer ( closestTime ) ;
}
}
}
registerHeartbeatTimer ( SystemClock . elapsedRealtime ( ) + timeout * 1000 ) ;
}
}
private void registerHeartbeatTimer ( long triggerTime ) {
private void registerHeartbeatTimer ( long triggerTime ) {
@ -554,6 +586,25 @@ public class MpMasterService extends Service {
alarmManager . setExactAndAllowWhileIdle ( AlarmManager . ELAPSED_REALTIME_WAKEUP , triggerTime , pendingIntent ) ;
alarmManager . setExactAndAllowWhileIdle ( AlarmManager . ELAPSED_REALTIME_WAKEUP , triggerTime , pendingIntent ) ;
}
}
private void registerAbsHeartbeatTimer ( long triggerTime ) {
AlarmManager alarmManager = ( AlarmManager ) getSystemService ( ALARM_SERVICE ) ;
if ( mPreviousHB ! = null ) {
try {
alarmManager . cancel ( mPreviousHB ) ;
} catch ( Exception ex ) {
}
mPreviousHeartbeatTime = 0 ;
}
Intent alarmIntent = new Intent ( ) ;
alarmIntent . setAction ( ACTION_HEARTBEAT ) ;
PendingIntent pendingIntent = PendingIntent . getBroadcast ( this , 0 , alarmIntent , 0 ) ;
mPreviousHB = pendingIntent ;
mPreviousHeartbeatTime = triggerTime ;
alarmManager . setExactAndAllowWhileIdle ( AlarmManager . RTC_WAKEUP , triggerTime , pendingIntent ) ;
}
@Override
@Override
public int onStartCommand ( Intent intent , int flags , int startId ) {
public int onStartCommand ( Intent intent , int flags , int startId ) {
@ -601,22 +652,20 @@ public class MpMasterService extends Service {
private void connect ( ) {
private void connect ( ) {
// after 10 seconds its connected
// after 10 seconds its connected
mHander . postDelayed (
mHander . postDelayed ( new Runnable ( ) {
new Runnable ( ) {
public void run ( ) {
public void run ( ) {
Log . d ( TAG , "Bluetooth Low Energy device is connected!!" ) ;
Log . d ( TAG , "Bluetooth Low Energy device is connected!!" ) ;
Toast . makeText ( getApplicationContext ( ) , "Connected!" , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( getApplicationContext ( ) , "Connected!" , Toast . LENGTH_SHORT ) . show ( ) ;
mStateService = STATE_SERVICE . CONNECTED ;
mStateService = STATE_SERVICE . CONNECTED ;
startForeground ( NOTIFICATION_ID_FOREGROUND_SERVICE , prepareNotification ( ) ) ;
startForeground ( NOTIFICATION_ID_FOREGROUND_SERVICE , prepareNotification ( ) ) ;
}
}
} , 10000 ) ;
} , 10000 ) ;
}
}
private Notification prepareNotification ( ) {
private Notification prepareNotification ( ) {
// handle build version above android oreo
// handle build version above android oreo
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . O & &
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . O & & mNotificationManager . getNotificationChannel ( FOREGROUND_CHANNEL_ID ) = = null ) {
mNotificationManager . getNotificationChannel ( FOREGROUND_CHANNEL_ID ) = = null ) {
CharSequence name = getString ( R . string . text_name_notification ) ;
CharSequence name = getString ( R . string . text_name_notification ) ;
int importance = NotificationManager . IMPORTANCE_DEFAULT ;
int importance = NotificationManager . IMPORTANCE_DEFAULT ;
NotificationChannel channel = new NotificationChannel ( FOREGROUND_CHANNEL_ID , name , importance ) ;
NotificationChannel channel = new NotificationChannel ( FOREGROUND_CHANNEL_ID , name , importance ) ;
@ -661,14 +710,7 @@ public class MpMasterService extends Service {
} else {
} else {
notificationBuilder = new NotificationCompat . Builder ( this ) ;
notificationBuilder = new NotificationCompat . Builder ( this ) ;
}
}
notificationBuilder
notificationBuilder . setContent ( remoteViews ) . setSmallIcon ( R . mipmap . ic_launcher ) . setCategory ( NotificationCompat . CATEGORY_SERVICE ) . setOnlyAlertOnce ( true ) . setOngoing ( true ) . setAutoCancel ( true ) . setContentIntent ( pendingIntent ) ;
. setContent ( remoteViews )
. setSmallIcon ( R . mipmap . ic_launcher )
. setCategory ( NotificationCompat . CATEGORY_SERVICE )
. setOnlyAlertOnce ( true )
. setOngoing ( true )
. setAutoCancel ( true )
. setContentIntent ( pendingIntent ) ;
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . LOLLIPOP ) {
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . LOLLIPOP ) {
notificationBuilder . setVisibility ( NotificationCompat . VISIBILITY_PUBLIC ) ;
notificationBuilder . setVisibility ( NotificationCompat . VISIBILITY_PUBLIC ) ;
@ -734,7 +776,7 @@ public class MpMasterService extends Service {
File file = new File ( tempPath ) ;
File file = new File ( tempPath ) ;
file . mkdirs ( ) ;
file . mkdirs ( ) ;
final String filePath = tempPath + File . separator + "mp.apk" ;
final String filePath = tempPath + File . separator + "mp.apk" ;
Thread th = new Thread ( new Runnable ( ) {
Thread th = new Thread ( new Runnable ( ) {
@Override
@Override
public void run ( ) {
public void run ( ) {
@ -767,8 +809,7 @@ public class MpMasterService extends Service {
Intent intent = getApplicationContext ( ) . registerReceiver ( null , intentFilter ) ;
Intent intent = getApplicationContext ( ) . registerReceiver ( null , intentFilter ) ;
int batteryStatus = intent . getIntExtra ( BatteryManager . EXTRA_STATUS , - 1 ) ;
int batteryStatus = intent . getIntExtra ( BatteryManager . EXTRA_STATUS , - 1 ) ;
int isCahrging = ( ( batteryStatus = = BatteryManager . BATTERY_STATUS_CHARGING ) | |
int isCahrging = ( ( batteryStatus = = BatteryManager . BATTERY_STATUS_CHARGING ) | | ( batteryStatus = = BatteryManager . BATTERY_STATUS_FULL ) ) ? 1 : 0 ;
( batteryStatus = = BatteryManager . BATTERY_STATUS_FULL ) ) ? 1 : 0 ;
int level = intent . getIntExtra ( "level" , 0 ) ; ///电池剩余电量
int level = intent . getIntExtra ( "level" , 0 ) ; ///电池剩余电量
int scale = intent . getIntExtra ( "scale" , 0 ) ; ///获取电池满电量数值
int scale = intent . getIntExtra ( "scale" , 0 ) ; ///获取电池满电量数值
@ -785,15 +826,15 @@ public class MpMasterService extends Service {
int bc = manager . getIntProperty ( BatteryManager . BATTERY_PROPERTY_CURRENT_NOW ) ;
int bc = manager . getIntProperty ( BatteryManager . BATTERY_PROPERTY_CURRENT_NOW ) ;
level = manager . getIntProperty ( BatteryManager . BATTERY_PROPERTY_CAPACITY ) ;
level = manager . getIntProperty ( BatteryManager . BATTERY_PROPERTY_CAPACITY ) ;
float bcaVal = ( bca < 0 ) ? ( ( - bca ) / 1000000000 ) : ( bca / 1000000000 ) ;
float bcaVal = ( bca < 0 ) ? ( ( - bca ) / 1000000000 ) : ( bca / 1000000000 ) ;
sb . append ( "&BC=" + Float . toString ( bcaVal ) ) ;
sb . append ( "&BC=" + Float . toString ( bcaVal ) ) ;
sb . append ( "&BV=" + Float . toString ( ( ( float ) bv ) / 1000 ) ) ;
sb . append ( "&BV=" + Float . toString ( ( ( float ) bv ) / 1000 ) ) ;
sb . append ( "&BP=" + level ) ;
sb . append ( "&BP=" + level ) ;
sb . append ( "&BS=" + scale ) ;
sb . append ( "&BS=" + scale ) ;
sb . append ( "&CS=" + isCahrging ) ;
sb . append ( "&CS=" + isCahrging ) ;
ConnectivityManager cm = ( ConnectivityManager ) getApplicationContext ( ) . getSystemService ( Context . CONNECTIVITY_SERVICE ) ;
ConnectivityManager cm = ( ConnectivityManager ) getApplicationContext ( ) . getSystemService ( Context . CONNECTIVITY_SERVICE ) ;
boolean isMetered = cm . isActiveNetworkMetered ( ) ;
boolean isMetered = cm . isActiveNetworkMetered ( ) ;
sb . append ( "&NS=" + ( isMetered ? "1" : "0" ) ) ;
sb . append ( "&NS=" + ( isMetered ? "1" : "0" ) ) ;
@ -811,7 +852,7 @@ public class MpMasterService extends Service {
}
}
if ( isXyPlatform ) {
if ( isXyPlatform ) {
java . text . DecimalFormat fmt = new java . text . DecimalFormat ( "0.0" ) ;
java . text . DecimalFormat fmt = new java . text . DecimalFormat ( "0.0" ) ;
double val = SysApi . getChargingVoltage ( ) / 200.0 ;
double val = SysApi . getChargingVoltage ( ) / 200.0 ;
sb . append ( "&CV=" + fmt . format ( val ) ) ; // ChargeVol *5/1000
sb . append ( "&CV=" + fmt . format ( val ) ) ; // ChargeVol *5/1000
@ -875,9 +916,10 @@ public class MpMasterService extends Service {
}
}
public native static int getInt ( int cmd ) ;
public native static int getInt ( int cmd ) ;
public native static int [ ] getStats ( long ts ) ;
public native static int [ ] getStats ( long ts ) ;
////////////////////////GPS////////////////////
////////////////////////GPS////////////////////
}
}