Moved exists to send service

pull/197/head
M66B 4 years ago
parent e98dafdf3f
commit 90af49d770

@ -1705,10 +1705,10 @@ class Core {
else if (imessages == null || imessages.length == 0) {
long next = new Date().getTime() + EXISTS_RETRY_DELAY;
Intent intent = new Intent(context, ServiceUI.class);
Intent intent = new Intent(context, ServiceSend.class);
intent.setAction("exists:" + message.id);
PendingIntent piExists = PendingIntentCompat.getService(
context, ServiceUI.PI_EXISTS, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piExists = PendingIntentCompat.getForegroundService(
context, ServiceSend.PI_EXISTS, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piExists); // inexact

@ -75,10 +75,12 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
private static ExecutorService executor = Helper.getBackgroundExecutor(1, "send");
private static final int PI_SEND = 1;
private static final int RETRY_MAX = 3;
private static final int CONNECTIVITY_DELAY = 5000; // milliseconds
static final int PI_SEND = 1;
static final int PI_EXISTS = 2;
@Override
public void onCreate() {
EntityLog.log(this, "Service send create");
@ -193,6 +195,24 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
Log.i("Send intent=" + intent);
Log.logExtras(intent);
if (intent == null)
return START_STICKY;
String action = intent.getAction();
if (action == null)
return START_STICKY;
String[] parts = action.split(":");
switch (parts[0]) {
case "exists":
onExists(intent);
break;
}
return START_STICKY;
}
@ -727,6 +747,38 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
}
}
private void onExists(Intent intent) {
String action = intent.getAction();
long id = Long.parseLong(action.split(":")[1]);
executor.submit(new Runnable() {
@Override
public void run() {
try {
DB db = DB.getInstance(ServiceSend.this);
try {
db.beginTransaction();
// Message could have been deleted in the meantime
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
EntityOperation.queue(ServiceSend.this, message, EntityOperation.EXISTS, true);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
static void boot(final Context context) {
executor.submit(new Runnable() {
@Override

@ -57,9 +57,6 @@ public class ServiceUI extends IntentService {
static final int PI_SNOOZE = 9;
static final int PI_IGNORED = 10;
static final int PI_THREAD = 11;
static final int PI_WAKEUP = 12;
static final int PI_EXISTS = 15;
public ServiceUI() {
this(ServiceUI.class.getName());
@ -161,7 +158,7 @@ public class ServiceUI extends IntentService {
break;
case "exists":
onExists(id);
// ignore
break;
default:
@ -462,25 +459,6 @@ public class ServiceUI extends IntentService {
}
}
private void onExists(long id) {
DB db = DB.getInstance(this);
try {
db.beginTransaction();
// Message could have been deleted in the meantime
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
EntityOperation.queue(this, message, EntityOperation.EXISTS, true);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
static void sync(Context context, Long account) {
context.startService(new Intent(context, ServiceUI.class)
.setAction(account == null ? "sync" : "sync:" + account));

Loading…
Cancel
Save