|
|
|
@ -47,6 +47,8 @@ import java.util.Arrays;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
|
import javax.mail.Message;
|
|
|
|
@ -62,8 +64,11 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
|
|
|
|
|
|
|
|
|
public class ServiceSend extends LifecycleService {
|
|
|
|
|
private int lastUnsent = 0;
|
|
|
|
|
private boolean lastSuitable = false;
|
|
|
|
|
private TwoStateOwner cowner;
|
|
|
|
|
|
|
|
|
|
private static boolean booted = false;
|
|
|
|
|
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
|
|
|
|
|
|
|
|
|
private static final int IDENTITY_ERROR_AFTER = 30; // minutes
|
|
|
|
|
|
|
|
|
@ -72,107 +77,45 @@ public class ServiceSend extends LifecycleService {
|
|
|
|
|
Log.i("Service send create");
|
|
|
|
|
super.onCreate();
|
|
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
|
|
|
|
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
|
|
|
|
cm.registerNetworkCallback(builder.build(), networkCallback);
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(this);
|
|
|
|
|
cowner = new TwoStateOwner(ServiceSend.this, "send");
|
|
|
|
|
final DB db = DB.getInstance(this);
|
|
|
|
|
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
|
|
|
|
|
|
// Observe unsent count
|
|
|
|
|
db.operation().liveUnsent().observe(this, new Observer<Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onChanged(Integer unsent) {
|
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService(unsent).build());
|
|
|
|
|
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService(unsent, null).build());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
Log.i("Service send destroy");
|
|
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
cm.unregisterNetworkCallback(networkCallback);
|
|
|
|
|
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
|
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
nm.cancel(Helper.NOTIFICATION_SEND);
|
|
|
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
startForeground(Helper.NOTIFICATION_SEND, getNotificationService(null).build());
|
|
|
|
|
|
|
|
|
|
super.onStartCommand(intent, flags, startId);
|
|
|
|
|
|
|
|
|
|
return START_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NotificationCompat.Builder getNotificationService(Integer unsent) {
|
|
|
|
|
if (unsent != null)
|
|
|
|
|
lastUnsent = unsent;
|
|
|
|
|
|
|
|
|
|
// Build pending intent
|
|
|
|
|
Intent intent = new Intent(this, ActivityView.class);
|
|
|
|
|
intent.setAction("outbox");
|
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(
|
|
|
|
|
this, ActivityView.REQUEST_OUTBOX, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
|
new NotificationCompat.Builder(this, "send")
|
|
|
|
|
.setSmallIcon(R.drawable.baseline_send_24)
|
|
|
|
|
.setContentTitle(getString(R.string.title_notification_sending))
|
|
|
|
|
.setContentIntent(pi)
|
|
|
|
|
.setAutoCancel(false)
|
|
|
|
|
.setShowWhen(true)
|
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MIN)
|
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
|
|
|
|
|
|
|
|
|
if (lastUnsent > 0)
|
|
|
|
|
builder.setContentText(getResources().getQuantityString(
|
|
|
|
|
R.plurals.title_notification_unsent, lastUnsent, lastUnsent));
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
|
|
|
|
private Thread thread = null;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAvailable(Network network) {
|
|
|
|
|
Log.i("Service send available=" + network);
|
|
|
|
|
check();
|
|
|
|
|
}
|
|
|
|
|
// Observe send operations
|
|
|
|
|
db.operation().liveOperations(null).observe(cowner, new Observer<List<EntityOperation>>() {
|
|
|
|
|
private List<Long> handling = new ArrayList<>();
|
|
|
|
|
private PowerManager.WakeLock wlFolder = pm.newWakeLock(
|
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
|
|
|
|
|
Log.i("Service send network=" + network + " caps=" + caps);
|
|
|
|
|
check();
|
|
|
|
|
public void onChanged(final List<EntityOperation> operations) {
|
|
|
|
|
boolean process = false;
|
|
|
|
|
List<Long> ops = new ArrayList<>();
|
|
|
|
|
for (EntityOperation op : operations) {
|
|
|
|
|
if (!handling.contains(op.id))
|
|
|
|
|
process = true;
|
|
|
|
|
ops.add(op.id);
|
|
|
|
|
}
|
|
|
|
|
handling = ops;
|
|
|
|
|
|
|
|
|
|
private void check() {
|
|
|
|
|
if (!ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (thread != null && thread.isAlive())
|
|
|
|
|
return;
|
|
|
|
|
if (handling.size() > 0 && process) {
|
|
|
|
|
Log.i("OUTBOX operations=" + operations.size());
|
|
|
|
|
|
|
|
|
|
thread = new Thread(new Runnable() {
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(
|
|
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
|
|
|
|
|
try {
|
|
|
|
|
wl.acquire();
|
|
|
|
|
wlFolder.acquire();
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(ServiceSend.this);
|
|
|
|
|
EntityFolder outbox = db.folder().getOutbox();
|
|
|
|
|
try {
|
|
|
|
|
db.folder().setFolderError(outbox.id, null);
|
|
|
|
@ -229,9 +172,6 @@ public class ServiceSend extends LifecycleService {
|
|
|
|
|
if (!ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (db.operation().getOperations(outbox.id).size() == 0)
|
|
|
|
|
stopSelf();
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(outbox.name, ex);
|
|
|
|
|
db.folder().setFolderError(outbox.id, Helper.formatThrowable(ex, true));
|
|
|
|
@ -239,13 +179,106 @@ public class ServiceSend extends LifecycleService {
|
|
|
|
|
db.folder().setFolderState(outbox.id, null);
|
|
|
|
|
db.folder().setFolderSyncState(outbox.id, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
wl.release();
|
|
|
|
|
wlFolder.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, "send:connectivity");
|
|
|
|
|
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
|
|
|
|
|
thread.start();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operations.size() == 0)
|
|
|
|
|
stopSelf();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
|
|
|
|
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
|
|
|
|
cm.registerNetworkCallback(builder.build(), networkCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
Log.i("Service send destroy");
|
|
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
cm.unregisterNetworkCallback(networkCallback);
|
|
|
|
|
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
|
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
nm.cancel(Helper.NOTIFICATION_SEND);
|
|
|
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
startForeground(Helper.NOTIFICATION_SEND, getNotificationService(null, null).build());
|
|
|
|
|
|
|
|
|
|
super.onStartCommand(intent, flags, startId);
|
|
|
|
|
|
|
|
|
|
return START_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NotificationCompat.Builder getNotificationService(Integer unsent, Boolean suitable) {
|
|
|
|
|
if (unsent != null)
|
|
|
|
|
lastUnsent = unsent;
|
|
|
|
|
if (suitable != null)
|
|
|
|
|
lastSuitable = suitable;
|
|
|
|
|
|
|
|
|
|
// Build pending intent
|
|
|
|
|
Intent intent = new Intent(this, ActivityView.class);
|
|
|
|
|
intent.setAction("outbox");
|
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(
|
|
|
|
|
this, ActivityView.REQUEST_OUTBOX, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
|
new NotificationCompat.Builder(this, "send")
|
|
|
|
|
.setSmallIcon(R.drawable.baseline_send_24)
|
|
|
|
|
.setContentTitle(getString(R.string.title_notification_sending))
|
|
|
|
|
.setContentIntent(pi)
|
|
|
|
|
.setAutoCancel(false)
|
|
|
|
|
.setShowWhen(true)
|
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MIN)
|
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
|
|
|
|
|
|
|
|
|
if (lastUnsent > 0)
|
|
|
|
|
builder.setContentText(getResources().getQuantityString(
|
|
|
|
|
R.plurals.title_notification_unsent, lastUnsent, lastUnsent));
|
|
|
|
|
if (!lastSuitable)
|
|
|
|
|
builder.setSubText(getString(R.string.title_notification_waiting));
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAvailable(Network network) {
|
|
|
|
|
Log.i("Service send available=" + network);
|
|
|
|
|
check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
|
|
|
|
|
Log.i("Service send network=" + network + " caps=" + caps);
|
|
|
|
|
check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void check() {
|
|
|
|
|
boolean suitable = ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable();
|
|
|
|
|
Log.i("OUTBOX suitable=" + suitable);
|
|
|
|
|
|
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService(null, suitable).build());
|
|
|
|
|
|
|
|
|
|
if (suitable)
|
|
|
|
|
cowner.start();
|
|
|
|
|
else
|
|
|
|
|
cowner.stop();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|