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

143 lines
4.8 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import static androidx.room.ForeignKey.CASCADE;
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityOperation.TABLE_NAME,
foreignKeys = {
2018-08-09 20:45:42 +00:00
@ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
2018-08-08 06:55:47 +00:00
@ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
2018-08-02 13:33:06 +00:00
},
indices = {
2018-08-10 09:45:36 +00:00
@Index(value = {"folder"}),
2018-08-02 13:33:06 +00:00
@Index(value = {"message"})
}
)
public class EntityOperation {
static final String TABLE_NAME = "operation";
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
2018-08-09 20:45:42 +00:00
public Long folder;
@NonNull
2018-08-02 13:33:06 +00:00
public Long message;
@NonNull
public String name;
@NonNull
2018-08-02 13:33:06 +00:00
public String args;
@NonNull
public Long created;
2018-08-02 13:33:06 +00:00
public static final String SEEN = "seen";
public static final String ADD = "add";
public static final String MOVE = "move";
public static final String DELETE = "delete";
public static final String SEND = "send";
2018-09-05 07:23:51 +00:00
public static final String HEADERS = "headers";
2018-09-15 07:22:42 +00:00
public static final String BODY = "body";
public static final String ATTACHMENT = "attachment";
2018-09-07 15:12:43 +00:00
public static final String FLAG = "flag";
2018-08-02 13:33:06 +00:00
private static List<Intent> queue = new ArrayList<>();
2018-08-09 20:45:42 +00:00
static void queue(DB db, EntityMessage message, String name) {
2018-08-02 13:33:06 +00:00
JSONArray jsonArray = new JSONArray();
2018-08-09 20:45:42 +00:00
queue(db, message, name, jsonArray);
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
static void queue(DB db, EntityMessage message, String name, Object value) {
2018-08-02 13:33:06 +00:00
JSONArray jsonArray = new JSONArray();
jsonArray.put(value);
2018-08-09 20:45:42 +00:00
queue(db, message, name, jsonArray);
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
static void queue(DB db, EntityMessage message, String name, Object value1, Object value2) {
JSONArray jsonArray = new JSONArray();
jsonArray.put(value1);
jsonArray.put(value2);
queue(db, message, name, jsonArray);
}
2018-08-02 13:33:06 +00:00
2018-08-09 20:45:42 +00:00
private static void queue(DB db, EntityMessage message, String name, JSONArray jsonArray) {
2018-08-02 13:33:06 +00:00
EntityOperation operation = new EntityOperation();
2018-08-09 20:45:42 +00:00
operation.folder = message.folder;
2018-08-02 13:33:06 +00:00
operation.message = message.id;
operation.name = name;
operation.args = jsonArray.toString();
operation.created = new Date().getTime();
2018-08-09 20:45:42 +00:00
operation.id = db.operation().insertOperation(operation);
2018-08-02 13:33:06 +00:00
Intent intent = new Intent();
2018-08-11 14:04:06 +00:00
intent.setType("account/" + (SEND.equals(name) ? "outbox" : message.account));
2018-08-11 08:37:56 +00:00
intent.setAction(ServiceSynchronize.ACTION_PROCESS_OPERATIONS);
2018-08-08 06:55:47 +00:00
intent.putExtra("folder", message.folder);
synchronized (queue) {
queue.add(intent);
}
2018-08-09 20:45:42 +00:00
Log.i(Helper.TAG, "Queued op=" + operation.id + "/" + operation.name +
" msg=" + message.folder + "/" + operation.message +
" args=" + operation.args);
}
2018-08-02 13:33:06 +00:00
public static void process(Context context) {
// Processing needs to be done after committing to the database
2018-08-02 13:33:06 +00:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
synchronized (queue) {
for (Intent intent : queue)
lbm.sendBroadcast(intent);
queue.clear();
}
2018-08-02 13:33:06 +00:00
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityOperation) {
EntityOperation other = (EntityOperation) obj;
return (this.folder.equals(other.folder) &&
this.message.equals(other.message) &&
this.name.equals(other.name) &&
this.args.equals(other.args));
} else
return false;
}
2018-08-02 13:33:06 +00:00
}