Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.8 KiB

6 years ago
  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. FairEmail is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.util.Log;
  19. import org.json.JSONArray;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. import androidx.annotation.NonNull;
  24. import androidx.localbroadcastmanager.content.LocalBroadcastManager;
  25. import androidx.room.Entity;
  26. import androidx.room.ForeignKey;
  27. import androidx.room.Index;
  28. import androidx.room.PrimaryKey;
  29. import static androidx.room.ForeignKey.CASCADE;
  30. @Entity(
  31. tableName = EntityOperation.TABLE_NAME,
  32. foreignKeys = {
  33. @ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
  34. @ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
  35. },
  36. indices = {
  37. @Index(value = {"folder"}),
  38. @Index(value = {"message"})
  39. }
  40. )
  41. public class EntityOperation {
  42. static final String TABLE_NAME = "operation";
  43. @PrimaryKey(autoGenerate = true)
  44. public Long id;
  45. @NonNull
  46. public Long folder;
  47. @NonNull
  48. public Long message;
  49. @NonNull
  50. public String name;
  51. @NonNull
  52. public String args;
  53. @NonNull
  54. public Long created;
  55. public static final String SEEN = "seen";
  56. public static final String ADD = "add";
  57. public static final String MOVE = "move";
  58. public static final String DELETE = "delete";
  59. public static final String SEND = "send";
  60. public static final String HEADERS = "headers";
  61. public static final String BODY = "body";
  62. public static final String ATTACHMENT = "attachment";
  63. public static final String FLAG = "flag";
  64. private static List<Intent> queue = new ArrayList<>();
  65. static void queue(DB db, EntityMessage message, String name) {
  66. JSONArray jsonArray = new JSONArray();
  67. queue(db, message, name, jsonArray);
  68. }
  69. static void queue(DB db, EntityMessage message, String name, Object value) {
  70. JSONArray jsonArray = new JSONArray();
  71. jsonArray.put(value);
  72. queue(db, message, name, jsonArray);
  73. }
  74. static void queue(DB db, EntityMessage message, String name, Object value1, Object value2) {
  75. JSONArray jsonArray = new JSONArray();
  76. jsonArray.put(value1);
  77. jsonArray.put(value2);
  78. queue(db, message, name, jsonArray);
  79. }
  80. private static void queue(DB db, EntityMessage message, String name, JSONArray jsonArray) {
  81. EntityOperation operation = new EntityOperation();
  82. operation.folder = message.folder;
  83. operation.message = message.id;
  84. operation.name = name;
  85. operation.args = jsonArray.toString();
  86. operation.created = new Date().getTime();
  87. operation.id = db.operation().insertOperation(operation);
  88. Intent intent = new Intent();
  89. intent.setType("account/" + (SEND.equals(name) ? "outbox" : message.account));
  90. intent.setAction(ServiceSynchronize.ACTION_PROCESS_OPERATIONS);
  91. intent.putExtra("folder", message.folder);
  92. synchronized (queue) {
  93. queue.add(intent);
  94. }
  95. Log.i(Helper.TAG, "Queued op=" + operation.id + "/" + operation.name +
  96. " msg=" + message.folder + "/" + operation.message +
  97. " args=" + operation.args);
  98. }
  99. public static void process(Context context) {
  100. // Processing needs to be done after committing to the database
  101. LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
  102. synchronized (queue) {
  103. for (Intent intent : queue)
  104. lbm.sendBroadcast(intent);
  105. queue.clear();
  106. }
  107. }
  108. @Override
  109. public boolean equals(Object obj) {
  110. if (obj instanceof EntityOperation) {
  111. EntityOperation other = (EntityOperation) obj;
  112. return (this.folder.equals(other.folder) &&
  113. this.message.equals(other.message) &&
  114. this.name.equals(other.name) &&
  115. this.args.equals(other.args));
  116. } else
  117. return false;
  118. }
  119. }