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.

140 lines
4.7 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. NetGuard 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 NetGuard. 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 ATTACHMENT = "attachment";
  61. public static final String HEADERS = "headers";
  62. private static List<Intent> queue = new ArrayList<>();
  63. static void queue(DB db, EntityMessage message, String name) {
  64. JSONArray jsonArray = new JSONArray();
  65. queue(db, message, name, jsonArray);
  66. }
  67. static void queue(DB db, EntityMessage message, String name, Object value) {
  68. JSONArray jsonArray = new JSONArray();
  69. jsonArray.put(value);
  70. queue(db, message, name, jsonArray);
  71. }
  72. static void queue(DB db, EntityMessage message, String name, Object value1, Object value2) {
  73. JSONArray jsonArray = new JSONArray();
  74. jsonArray.put(value1);
  75. jsonArray.put(value2);
  76. queue(db, message, name, jsonArray);
  77. }
  78. private static void queue(DB db, EntityMessage message, String name, JSONArray jsonArray) {
  79. EntityOperation operation = new EntityOperation();
  80. operation.folder = message.folder;
  81. operation.message = message.id;
  82. operation.name = name;
  83. operation.args = jsonArray.toString();
  84. operation.created = new Date().getTime();
  85. operation.id = db.operation().insertOperation(operation);
  86. Intent intent = new Intent();
  87. intent.setType("account/" + (SEND.equals(name) ? "outbox" : message.account));
  88. intent.setAction(ServiceSynchronize.ACTION_PROCESS_OPERATIONS);
  89. intent.putExtra("folder", message.folder);
  90. synchronized (queue) {
  91. queue.add(intent);
  92. }
  93. Log.i(Helper.TAG, "Queued op=" + operation.id + "/" + operation.name +
  94. " msg=" + message.folder + "/" + operation.message +
  95. " args=" + operation.args);
  96. }
  97. public static void process(Context context) {
  98. // Processing needs to be done after committing to the database
  99. LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
  100. synchronized (queue) {
  101. for (Intent intent : queue)
  102. lbm.sendBroadcast(intent);
  103. queue.clear();
  104. }
  105. }
  106. @Override
  107. public boolean equals(Object obj) {
  108. if (obj instanceof EntityOperation) {
  109. EntityOperation other = (EntityOperation) obj;
  110. return (this.folder.equals(other.folder) &&
  111. this.message.equals(other.message) &&
  112. this.name.equals(other.name) &&
  113. this.args.equals(other.args));
  114. } else
  115. return false;
  116. }
  117. }