FairEmail/app/src/main/java/eu/faircode/email/ServiceUI.java

523 lines
17 KiB
Java
Raw Normal View History

2019-02-27 15:05:15 +00:00
package eu.faircode.email;
2019-05-04 20:49:22 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
2019-02-27 15:05:15 +00:00
import android.app.IntentService;
2019-05-21 13:55:10 +00:00
import android.app.NotificationManager;
import android.content.Context;
2019-02-27 15:05:15 +00:00
import android.content.Intent;
2019-05-06 08:33:29 +00:00
import android.content.SharedPreferences;
2019-08-24 13:00:14 +00:00
import android.os.Bundle;
2019-02-27 15:05:15 +00:00
import androidx.annotation.Nullable;
2019-08-24 13:00:14 +00:00
import androidx.core.app.RemoteInput;
2019-05-06 08:33:29 +00:00
import androidx.preference.PreferenceManager;
import org.json.JSONException;
2020-02-20 09:35:01 +00:00
import java.io.File;
2019-08-24 13:00:14 +00:00
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
2019-08-24 13:00:14 +00:00
import java.util.Date;
2020-08-15 05:59:36 +00:00
import java.util.HashMap;
2019-05-06 08:33:29 +00:00
import java.util.List;
2020-08-15 05:59:36 +00:00
import java.util.Map;
2019-08-24 13:00:14 +00:00
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2019-02-27 15:05:15 +00:00
public class ServiceUI extends IntentService {
2019-03-14 14:57:24 +00:00
static final int PI_CLEAR = 1;
2019-05-05 20:28:07 +00:00
static final int PI_TRASH = 2;
2019-09-26 19:28:05 +00:00
static final int PI_JUNK = 3;
static final int PI_ARCHIVE = 4;
2019-10-23 10:51:20 +00:00
static final int PI_MOVE = 5;
static final int PI_REPLY_DIRECT = 6;
static final int PI_FLAG = 7;
static final int PI_SEEN = 8;
2021-04-12 12:59:38 +00:00
static final int PI_HIDE = 9;
static final int PI_SNOOZE = 10;
static final int PI_IGNORED = 11;
2020-06-07 06:08:17 +00:00
2019-02-27 15:05:15 +00:00
public ServiceUI() {
this(ServiceUI.class.getName());
}
public ServiceUI(String name) {
super(name);
}
2019-02-27 16:48:03 +00:00
@Override
public void onCreate() {
Log.i("Service UI create");
super.onCreate();
}
@Override
public void onDestroy() {
Log.i("Service UI destroy");
super.onDestroy();
}
2019-02-27 15:05:15 +00:00
@Override
protected void onHandleIntent(@Nullable Intent intent) {
2019-03-02 08:16:27 +00:00
// Under certain circumstances, a background app is placed on a temporary whitelist for several minutes
// - Executing a PendingIntent from a notification.
// https://developer.android.com/about/versions/oreo/background#services
2019-02-27 16:48:03 +00:00
Log.i("Service UI intent=" + intent);
2019-07-25 08:43:07 +00:00
Log.logExtras(intent);
2019-02-27 16:48:03 +00:00
2019-02-27 15:05:15 +00:00
if (intent == null)
return;
String action = intent.getAction();
if (action == null)
return;
2019-08-24 13:00:14 +00:00
try {
String[] parts = action.split(":");
long id = (parts.length > 1 ? Long.parseLong(parts[1]) : -1);
2020-04-24 15:43:10 +00:00
long group = intent.getLongExtra("group", -1);
2019-08-24 13:00:14 +00:00
switch (parts[0]) {
case "clear":
2019-09-01 09:32:18 +00:00
onClear(id);
2019-08-24 13:00:14 +00:00
break;
case "trash":
cancel(group, id);
2019-09-26 19:28:05 +00:00
onMove(id, EntityFolder.TRASH);
break;
case "junk":
cancel(group, id);
onJunk(id);
2019-08-24 13:00:14 +00:00
break;
case "archive":
cancel(group, id);
2019-09-26 19:28:05 +00:00
onMove(id, EntityFolder.ARCHIVE);
2019-08-24 13:00:14 +00:00
break;
2019-10-23 10:51:20 +00:00
case "move":
cancel(group, id);
onMove(id);
break;
2019-08-24 13:00:14 +00:00
case "reply":
cancel(group, id);
onSeen(id);
2020-04-24 08:36:14 +00:00
onReplyDirect(id, intent);
2019-08-24 13:00:14 +00:00
break;
case "flag":
cancel(group, id);
onFlag(id);
break;
case "seen":
cancel(group, id);
onSeen(id);
break;
2021-04-12 12:59:38 +00:00
case "hide":
cancel(group, id);
onHide(id);
break;
2019-09-24 07:34:43 +00:00
case "snooze":
cancel(group, id);
onSnooze(id);
break;
2019-08-24 13:00:14 +00:00
case "ignore":
onIgnore(id);
2019-08-24 13:00:14 +00:00
break;
2019-09-24 07:34:43 +00:00
case "wakeup":
2021-03-30 07:12:36 +00:00
// ignore
2019-08-24 13:00:14 +00:00
break;
2019-11-23 10:39:34 +00:00
case "sync":
2021-09-02 20:33:04 +00:00
onSync(id, -1L, false);
break;
case "widget":
onWidget(intent, (int) id);
break;
2021-02-22 19:11:34 +00:00
case "exists":
2021-03-30 07:36:51 +00:00
// ignore
2021-02-22 19:11:34 +00:00
break;
2019-08-24 13:00:14 +00:00
default:
throw new IllegalArgumentException("Unknown UI action: " + parts[0]);
}
2020-08-15 05:59:36 +00:00
Map<String, String> crumb = new HashMap<>();
crumb.put("action", action);
Log.breadcrumb("serviceui", crumb);
ServiceSynchronize.eval(this, "ui/" + action);
2019-08-24 13:00:14 +00:00
} catch (Throwable ex) {
Log.e(ex);
2019-02-27 15:05:15 +00:00
}
}
2019-09-01 09:32:18 +00:00
private void onClear(long group) {
2021-07-22 07:36:49 +00:00
// Group
// < 0: folder
// = 0: unified
// > 0: account
2019-09-01 09:32:18 +00:00
DB db = DB.getInstance(this);
2020-06-15 10:34:25 +00:00
int cleared;
if (group < 0)
2021-09-12 06:14:33 +00:00
cleared = db.message().ignoreAll(null, -group, null);
2020-06-15 10:34:25 +00:00
else
2021-09-12 06:14:33 +00:00
cleared = db.message().ignoreAll(group == 0 ? null : group, null, null);
2021-08-17 12:21:19 +00:00
EntityLog.log(this, EntityLog.Type.Notification,
"Notify clear group=" + group + " cleared=" + cleared);
2019-02-27 15:05:15 +00:00
}
2020-04-24 15:43:10 +00:00
private void cancel(long group, long id) {
// https://issuetracker.google.com/issues/159152393
2019-05-21 13:55:10 +00:00
String tag = "unseen." + group + ":" + id;
2021-07-15 16:36:39 +00:00
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(tag, NotificationHelper.NOTIFICATION_TAGGED);
2019-05-21 13:55:10 +00:00
}
2019-09-26 19:28:05 +00:00
private void onMove(long id, String folderType) {
2019-02-27 15:05:15 +00:00
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
2019-09-24 07:34:43 +00:00
if (message == null)
return;
2019-10-23 10:51:20 +00:00
EntityFolder folder = db.folder().getFolderByType(message.account, folderType);
if (folder == null)
return;
EntityOperation.queue(this, message, EntityOperation.MOVE, folder.id);
2019-10-23 10:51:20 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
private void onMove(long id) {
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
EntityAccount account = db.account().getAccount(message.account);
if (account == null || account.move_to == null)
return;
EntityOperation.queue(this, message, EntityOperation.MOVE, account.move_to);
2019-02-27 15:05:15 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
private void onJunk(long id) throws JSONException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean block_sender = prefs.getBoolean("notify_block_sender", false);
2020-04-19 14:57:40 +00:00
List<String> whitelist = EmailProvider.getDomainNames(this);
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
if (junk == null)
return;
EntityOperation.queue(this, message, EntityOperation.MOVE, junk.id);
if (block_sender) {
2020-04-19 14:57:40 +00:00
EntityRule rule = EntityRule.blockSender(this, message, junk, false, whitelist);
if (rule != null)
rule.id = db.rule().insertRule(rule);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2019-02-27 15:05:15 +00:00
}
2019-08-24 13:00:14 +00:00
private void onReplyDirect(long id, Intent intent) throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean prefix_once = prefs.getBoolean("prefix_once", true);
boolean plain_only = prefs.getBoolean("plain_only", false);
2021-01-25 13:49:56 +00:00
DB db = DB.getInstance(this);
EntityMessage ref = db.message().getMessage(id);
if (ref == null)
throw new IllegalArgumentException("message not found");
EntityIdentity identity = db.identity().getIdentity(ref.identity);
if (identity == null)
throw new IllegalArgumentException("identity not found");
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null)
throw new IllegalArgumentException("outbox not found");
String subject = (ref.subject == null ? "" : ref.subject);
2021-06-10 13:48:17 +00:00
if (prefix_once)
subject = EntityMessage.collapsePrefixes(this, ref.language, subject, false);
2021-01-25 13:49:56 +00:00
2019-08-24 13:00:14 +00:00
Bundle results = RemoteInput.getResultsFromIntent(intent);
2020-03-26 14:25:44 +00:00
String body = results.getString("text");
if (body != null)
body = "<p>" + body.replaceAll("\\r?\\n", "<br>") + "</p>";
2019-08-24 13:00:14 +00:00
2021-01-19 10:57:59 +00:00
String text = HtmlHelper.getFullText(body);
2021-01-25 13:49:56 +00:00
String language = HtmlHelper.getLanguage(this, ref.subject, text);
2021-01-19 11:18:03 +00:00
String preview = HtmlHelper.getPreview(text);
2020-11-16 18:06:55 +00:00
2019-08-24 13:00:14 +00:00
try {
db.beginTransaction();
EntityMessage reply = new EntityMessage();
reply.account = identity.account;
reply.folder = outbox.id;
reply.identity = identity.id;
reply.msgid = EntityMessage.generateMessageId();
reply.inreplyto = ref.msgid;
reply.thread = ref.thread;
reply.to = ref.from;
reply.from = new Address[]{new InternetAddress(identity.email, identity.name, StandardCharsets.UTF_8.name())};
2019-08-24 13:00:14 +00:00
reply.subject = getString(R.string.title_subject_reply, subject);
reply.received = new Date().getTime();
reply.seen = true;
reply.ui_seen = true;
reply.id = db.message().insertMessage(reply);
2020-02-20 09:35:01 +00:00
File file = reply.getFile(this);
2020-03-26 14:25:44 +00:00
Helper.writeText(file, body);
2020-02-20 09:35:01 +00:00
2019-08-24 13:00:14 +00:00
db.message().setMessageContent(reply.id,
true,
2020-11-16 18:06:55 +00:00
language,
2019-08-24 13:00:14 +00:00
plain_only || ref.plain_only,
2020-11-16 18:06:55 +00:00
preview,
2019-08-24 13:00:14 +00:00
null);
EntityOperation.queue(this, reply, EntityOperation.SEND);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2019-12-07 19:32:58 +00:00
ServiceSend.start(this);
2019-08-24 13:00:14 +00:00
}
2019-05-06 08:33:29 +00:00
private void onFlag(long id) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean threading = prefs.getBoolean("threading", true);
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
2019-09-24 07:34:43 +00:00
if (message == null)
return;
List<EntityMessage> messages = db.message().getMessagesByThread(
2020-02-24 17:33:22 +00:00
message.account, message.thread, threading ? null : id, message.folder);
2019-09-24 07:34:43 +00:00
for (EntityMessage threaded : messages) {
EntityOperation.queue(this, threaded, EntityOperation.FLAG, true);
EntityOperation.queue(this, threaded, EntityOperation.SEEN, true);
2019-05-06 08:33:29 +00:00
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
2019-05-05 20:28:07 +00:00
private void onSeen(long id) {
2019-02-27 15:05:15 +00:00
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
2019-09-24 07:34:43 +00:00
if (message == null)
return;
EntityOperation.queue(this, message, EntityOperation.SEEN, true);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
2021-04-12 12:59:38 +00:00
private void onHide(long id) {
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
db.message().setMessageSnoozed(message.id, Long.MAX_VALUE);
db.message().setMessageUiIgnored(message.id, true);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
2019-09-24 07:34:43 +00:00
private void onSnooze(long id) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-10-06 19:01:01 +00:00
int notify_snooze_duration = prefs.getInt("default_snooze", 1);
2019-09-24 07:34:43 +00:00
2019-10-06 19:01:01 +00:00
long wakeup = new Date().getTime() + notify_snooze_duration * 3600 * 1000L;
2019-09-24 07:34:43 +00:00
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
db.message().setMessageSnoozed(id, wakeup);
2019-11-04 08:00:14 +00:00
db.message().setMessageUiIgnored(message.id, true);
2019-09-24 07:34:43 +00:00
EntityMessage.snooze(this, id, wakeup);
2019-02-27 15:05:15 +00:00
db.setTransactionSuccessful();
2019-09-24 07:34:43 +00:00
2019-02-27 15:05:15 +00:00
} finally {
db.endTransaction();
}
}
private void onIgnore(long id) {
EntityMessage message;
2020-04-03 07:39:48 +00:00
EntityFolder folder;
2019-02-27 15:05:15 +00:00
DB db = DB.getInstance(this);
try {
db.beginTransaction();
message = db.message().getMessage(id);
2019-09-24 07:34:43 +00:00
if (message == null)
return;
2020-04-03 07:39:48 +00:00
folder = db.folder().getFolder(message.folder);
if (folder == null)
return;
2019-09-24 07:34:43 +00:00
db.message().setMessageUiIgnored(message.id, true);
2019-02-27 15:05:15 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
2021-09-02 20:33:04 +00:00
private void onSync(long aid, long fid, boolean unified) {
DB db = DB.getInstance(this);
try {
db.beginTransaction();
2020-01-22 20:39:38 +00:00
List<EntityAccount> accounts = db.account().getPollAccounts(aid < 0 ? null : aid);
for (EntityAccount account : accounts) {
List<EntityFolder> folders;
if (fid < 0)
folders = db.folder().getSynchronizingFolders(account.id);
else
folders = Arrays.asList(db.folder().getFolder(fid));
2020-01-22 20:39:38 +00:00
if (folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(this));
for (EntityFolder folder : folders)
2021-09-02 20:33:04 +00:00
if (!unified || folder.unified)
EntityOperation.sync(this, folder.id, true);
2020-01-22 20:39:38 +00:00
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
private void onWidget(Intent intent, int appWidgetId) {
long aid = intent.getLongExtra("account", -1L);
long fid = intent.getLongExtra("folder", -1L);
2021-09-02 20:33:04 +00:00
onSync(aid, fid, fid < 0);
}
static void sync(Context context, Long account) {
2021-06-30 10:57:15 +00:00
try {
Intent sync = new Intent(context, ServiceUI.class)
.setAction(account == null ? "sync" : "sync:" + account);
context.startService(sync);
2021-06-30 10:57:15 +00:00
} catch (Throwable ex) {
Log.e(ex);
/*
java.lang.IllegalStateException: Not allowed to start service Intent { act=sync cmp=eu.faircode.email/.ServiceUI }: app is in background uid UidRecord{ac095c9 u0a94 CEM bg:+9d20h57m50s144ms idle change:cached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)
at android.app.ContextImpl.startService(ContextImpl.java:1670)
at android.content.ContextWrapper.startService(ContextWrapper.java:720)
at eu.faircode.email.ServiceUI.sync(ServiceUI:487)
at eu.faircode.email.ServiceTileUnseen.onClick(ServiceTileUnseen:103)
at android.service.quicksettings.TileService$H.handleMessage(TileService.java:449)
*/
}
}
static void ignore(Context context, long id) {
try {
Intent ignore = new Intent(context, ServiceUI.class)
.setAction("ignore:" + id);
context.startService(ignore);
} catch (Throwable ex) {
Log.e(ex);
}
}
2019-02-27 15:05:15 +00:00
}