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.

139 lines
4.6 KiB

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