|
|
|
@ -86,6 +86,7 @@ import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
|
public class MicroPhotoService extends Service {
|
|
|
|
|
public static final String TAG = "MPLOG";
|
|
|
|
@ -101,6 +102,10 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
public final static int MSG_WHAT_MAX = 1000;
|
|
|
|
|
|
|
|
|
|
public final static int BROADCAST_REQUEST_CODE_HEARTBEAT = 1;
|
|
|
|
|
public final static int BROADCAST_REQUEST_CODE_TAKING_PHOTO = 2;
|
|
|
|
|
public final static int BROADCAST_REQUEST_CODE_GPS = 2;
|
|
|
|
|
|
|
|
|
|
public static final int NOTIFICATION_ID_FOREGROUND_SERVICE = 8466503;
|
|
|
|
|
public static final String ACTION_MSG_BROADCAST = "ACT_MSG_BROADCAST";
|
|
|
|
|
|
|
|
|
@ -157,6 +162,8 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
private ServiceHandler mHander = null;
|
|
|
|
|
private Messenger mMessenger = null;
|
|
|
|
|
|
|
|
|
|
private static AtomicInteger mPendingIntentFeed = new AtomicInteger();
|
|
|
|
|
|
|
|
|
|
private String mModelName = null;
|
|
|
|
|
|
|
|
|
|
public static boolean isRunning = false;
|
|
|
|
@ -199,7 +206,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onTrimMemory(int level) {
|
|
|
|
|
Log.w(TAG, "onTrimMemory level=" + level);
|
|
|
|
|
infoLog("Event onTrimMemory level=" + level);
|
|
|
|
|
if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
|
|
|
|
|
// Clear the caches. Note all pending requests will be removed too.
|
|
|
|
|
final Context context = getApplicationContext();
|
|
|
|
@ -318,6 +325,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
getApplicationContext().registerReceiver(mNetworkChangedReceiver, filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
@ -327,6 +335,8 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
enableGps(true);
|
|
|
|
|
requestPosition();
|
|
|
|
|
}
|
|
|
|
@ -416,17 +426,24 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
if (TextUtils.equals(ACTION_HEARTBEAT, action)) {
|
|
|
|
|
Log.i(TAG, "HB Timer Fired ACTION=" + action);
|
|
|
|
|
long ts = System.currentTimeMillis();
|
|
|
|
|
long expectedHbTime = intent.getLongExtra("HeartbeatTime", ts);
|
|
|
|
|
|
|
|
|
|
infoLog("HB Timer Fired ACTION=" + action + " ExpTS=" + Long.toString(expectedHbTime));
|
|
|
|
|
Runnable runnable = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
mService.sendHeartbeat(mService.mNativeHandle, mService.getSignalLevel());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Thread th = new Thread(runnable);
|
|
|
|
|
th.start();
|
|
|
|
|
|
|
|
|
|
mService.registerHeartbeatTimer(mService.mHeartbeatDuration);
|
|
|
|
|
long nextHbTime = expectedHbTime + mService.mHeartbeatDuration;
|
|
|
|
|
while (nextHbTime <= ts) {
|
|
|
|
|
nextHbTime += mService.mHeartbeatDuration;
|
|
|
|
|
}
|
|
|
|
|
long timeout = (expectedHbTime != 0) ? (nextHbTime - System.currentTimeMillis()) : mService.mHeartbeatDuration;
|
|
|
|
|
mService.registerHeartbeatTimer(timeout);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
@ -449,11 +466,18 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
for (int idx = 0; idx < cnt; idx++) {
|
|
|
|
|
long val = intent.getLongExtra(EXTRA_PARAM_SCHEDULE + idx, 0);
|
|
|
|
|
|
|
|
|
|
int channel = (int) ((val & 0xFF0000L) >> 16);
|
|
|
|
|
int preset = (int) ((val & 0xFF00L) >> 8);
|
|
|
|
|
boolean photoOrVideo = ((val & 0xFFL) == 0);
|
|
|
|
|
int channel = (int) ((val & 0xFFFF000L) >> 12);
|
|
|
|
|
int preset = (int) ((val & 0xFF0L) >> 4);
|
|
|
|
|
boolean photoOrVideo = ((val & 0xFL) == 0);
|
|
|
|
|
|
|
|
|
|
Log.i(TAG, "PhotoTimer Fired: CH=" + channel + " PR=" + preset);
|
|
|
|
|
if (channel >= 256)
|
|
|
|
|
{
|
|
|
|
|
infoLog("SERIAL Timer Fired: CH=" + (channel - 256) + " PR=" + preset);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
infoLog("IMG Timer Fired: CH=" + channel + " PR=" + preset);
|
|
|
|
|
}
|
|
|
|
|
mService.notifyToTakePhoto(mService.mNativeHandle, channel, preset, ts, photoOrVideo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -626,7 +650,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
nextPhotoTime *= 1000;
|
|
|
|
|
if (nextPhotoTime > ts) {
|
|
|
|
|
mHeartbeatDuration = duration;
|
|
|
|
|
registerHeartbeatTimer((int) ((nextPhotoTime - ts) % duration) + 999);
|
|
|
|
|
registerHeartbeatTimer((int) ((nextPhotoTime - ts) % duration));
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
mHeartbeatDuration = duration;
|
|
|
|
@ -638,15 +662,20 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerHeartbeatTimer(long timeout) {
|
|
|
|
|
private void registerHeartbeatTimer(long timeoutMs) {
|
|
|
|
|
|
|
|
|
|
// 创建延迟意图
|
|
|
|
|
long triggerTime = System.currentTimeMillis() + timeout;
|
|
|
|
|
long triggerTime = System.currentTimeMillis() + timeoutMs;
|
|
|
|
|
triggerTime -= (triggerTime % 1000);
|
|
|
|
|
Intent alarmIntent = new Intent();
|
|
|
|
|
alarmIntent.setAction(ACTION_HEARTBEAT);
|
|
|
|
|
alarmIntent.putExtra("HeartbeatDuration", mHeartbeatDuration);
|
|
|
|
|
alarmIntent.putExtra("HeartbeatTime", triggerTime);
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, BROADCAST_REQUEST_CODE_HEARTBEAT, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
|
|
Date date = new Date(triggerTime);
|
|
|
|
|
String dateStr = (String) DateFormat.format("kk:mm:ss", date);
|
|
|
|
|
infoLog( "HB Reg " + Long.toString(triggerTime) + " at " + dateStr);
|
|
|
|
|
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
|
|
|
|
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
|
|
|
|
@ -661,10 +690,20 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
intent.putExtra(EXTRA_PARAM_SCHEDULES, cnt);
|
|
|
|
|
StringBuilder channelStr = new StringBuilder();
|
|
|
|
|
long val = 0;
|
|
|
|
|
long channel = 0;
|
|
|
|
|
for (int idx = 0; idx < cnt; idx++) {
|
|
|
|
|
val = schedules.get(idx).longValue();
|
|
|
|
|
channel = ((val & 0XFFFF000) >> 12);
|
|
|
|
|
intent.putExtra(EXTRA_PARAM_SCHEDULE + idx, schedules.get(idx).longValue());
|
|
|
|
|
channelStr.append("(" + ((val & 0XFF0000) >> 16) + "-" + Long.toString (((val & 0XFF00) >> 8), 16).toUpperCase() + ") ");
|
|
|
|
|
if (channel > 0xFF)
|
|
|
|
|
{
|
|
|
|
|
channel &= 0xFF;
|
|
|
|
|
channelStr.append("(" + channel + "-" + Long.toString (((val & 0XFF0) >> 4), 16).toUpperCase() + "/SERIAL) ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
channelStr.append("(" + channel + "-" + Long.toString (((val & 0XFF0) >> 4), 16).toUpperCase() + "/IMG) ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intent.putExtra(EXTRA_PARAM_TIME, scheduleTime);
|
|
|
|
@ -675,7 +714,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
|
|
|
|
|
localBroadcastManager.sendBroadcast(intent);
|
|
|
|
|
} else {
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, BROADCAST_REQUEST_CODE_TAKING_PHOTO, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
|
|
|
|
|
try {
|
|
|
|
@ -684,12 +723,12 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
|
|
|
|
|
|
|
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
|
Date date = new Date(currentTimeMillis + timeout);
|
|
|
|
|
String dateStr = (String) DateFormat.format("MM-dd kk:mm:ss", date);
|
|
|
|
|
Log.d(TAG, "PhotoTimer Reg: " + dateStr + " currentTimeMillis=" + currentTimeMillis + " timeout=" + timeout + " CH-PR=" + channelStr.toString());
|
|
|
|
|
|
|
|
|
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
|
|
|
|
|
infoLog( "Timer Reg: " + dateStr + " TS=" + currentTimeMillis + " Timeout=" + timeout + " " + channelStr.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -982,7 +1021,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
alarmIntent.setPackage(context.getPackageName());
|
|
|
|
|
alarmIntent.setAction(ACTION_STOP);
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, alarmIntent, 0);
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), mPendingIntentFeed.getAndIncrement(), alarmIntent, 0);
|
|
|
|
|
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(ALARM_SERVICE);
|
|
|
|
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 100, pendingIntent);
|
|
|
|
@ -1384,7 +1423,7 @@ public class MicroPhotoService extends Service {
|
|
|
|
|
|
|
|
|
|
Intent intent = new Intent();
|
|
|
|
|
intent.setAction(ACTION_GPS_TIMEOUT);
|
|
|
|
|
mPreviousGpsTimer = PendingIntent.getBroadcast(this, 0, intent, 0);
|
|
|
|
|
mPreviousGpsTimer = PendingIntent.getBroadcast(this, mPendingIntentFeed.getAndIncrement(), intent, 0);
|
|
|
|
|
|
|
|
|
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + mGpsTimeout, mPreviousGpsTimer);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|