/app/src/main/java/com/aurora/adroid/manager/RepoSyncManager.java

https://gitlab.com/jazielmendozacarrillo7/auroradroid · Java · 259 lines · 203 code · 38 blank · 18 comment · 19 complexity · 542724b71bff30f7e89b705cc168e7b3 MD5 · raw file

  1. /*
  2. * Aurora Droid
  3. * Copyright (C) 2019-20, Rahul Kumar Patel <whyorean@gmail.com>
  4. *
  5. * Aurora Droid is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Aurora Droid is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Aurora Droid. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.aurora.adroid.manager;
  20. import android.content.Context;
  21. import com.aurora.adroid.Constants;
  22. import com.aurora.adroid.model.StaticRepo;
  23. import com.aurora.adroid.model.RepoHeader;
  24. import com.aurora.adroid.task.DatabaseTask;
  25. import com.aurora.adroid.util.Log;
  26. import com.aurora.adroid.util.PrefUtil;
  27. import com.google.gson.Gson;
  28. import com.google.gson.reflect.TypeToken;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.lang.reflect.Type;
  32. import java.nio.charset.StandardCharsets;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. public class RepoSyncManager {
  37. private final HashMap<String, StaticRepo> repoHashMap = new HashMap<>();
  38. private final HashMap<String, StaticRepo> syncHashMap = new HashMap<>();
  39. private final HashMap<String, RepoHeader> headerHashMap = new HashMap<>();
  40. private Context context;
  41. private Gson gson;
  42. public RepoSyncManager(Context context) {
  43. this.context = context;
  44. this.gson = new Gson();
  45. this.repoHashMap.putAll(getRepoHashMap());
  46. this.syncHashMap.putAll(getSyncedHashMap());
  47. this.headerHashMap.putAll(getHeaderHashMap());
  48. }
  49. public void addToRepoMap(StaticRepo staticRepo) {
  50. synchronized (repoHashMap) {
  51. if (!repoHashMap.containsKey(staticRepo.getRepoId())) {
  52. repoHashMap.put(staticRepo.getRepoId(), staticRepo);
  53. }
  54. }
  55. }
  56. public void addDefault() {
  57. synchronized (repoHashMap) {
  58. final StaticRepo staticRepo = getDefaultFromAssets();
  59. if (staticRepo != null && !repoHashMap.containsKey(staticRepo.getRepoId())) {
  60. repoHashMap.put(staticRepo.getRepoId(), staticRepo);
  61. saveRepoMap();
  62. }
  63. }
  64. }
  65. public void addToSyncMap(StaticRepo staticRepo) {
  66. synchronized (syncHashMap) {
  67. if (!syncHashMap.containsKey(staticRepo.getRepoId())) {
  68. syncHashMap.put(staticRepo.getRepoId(), staticRepo);
  69. }
  70. saveSyncMap();
  71. }
  72. }
  73. public void addToHeaderMap(RepoHeader repoHeader) {
  74. synchronized (headerHashMap) {
  75. if (!headerHashMap.containsKey(repoHeader.getRepoId())) {
  76. headerHashMap.put(repoHeader.getRepoId(), repoHeader);
  77. }
  78. saveRepoHeaderMap();
  79. }
  80. }
  81. public void addAllToRepoMap(List<StaticRepo> staticRepoList) {
  82. synchronized (repoHashMap) {
  83. for (StaticRepo staticRepo : staticRepoList) {
  84. addToRepoMap(staticRepo);
  85. }
  86. saveRepoMap();
  87. }
  88. }
  89. public List<StaticRepo> getRepoList() {
  90. synchronized (repoHashMap) {
  91. return new ArrayList<>(repoHashMap.values());
  92. }
  93. }
  94. public List<StaticRepo> getSyncList() {
  95. synchronized (syncHashMap) {
  96. return new ArrayList<>(syncHashMap.values());
  97. }
  98. }
  99. public List<RepoHeader> getHeaderList() {
  100. synchronized (headerHashMap) {
  101. return new ArrayList<>(headerHashMap.values());
  102. }
  103. }
  104. public void updateRepoMap(List<StaticRepo> staticRepoList) {
  105. synchronized (repoHashMap) {
  106. clear();
  107. addAllToRepoMap(staticRepoList);
  108. }
  109. }
  110. public void updateSyncMap(List<StaticRepo> staticRepoList) {
  111. synchronized (syncHashMap) {
  112. final List<StaticRepo> syncedList = getSyncList();
  113. final DatabaseTask databaseTask = new DatabaseTask(context);
  114. for (StaticRepo staticRepo : syncedList) {
  115. if (!staticRepoList.contains(staticRepo)) {
  116. syncHashMap.remove(staticRepo.getRepoId());
  117. databaseTask.clearRepo(staticRepo);
  118. }
  119. }
  120. saveSyncMap();
  121. }
  122. }
  123. public void updateHeaderMap(List<StaticRepo> staticRepoList) {
  124. synchronized (headerHashMap) {
  125. final List<String> repoIdList = new ArrayList<>();
  126. final List<RepoHeader> syncedList = getHeaderList();
  127. for (StaticRepo staticRepo : staticRepoList)
  128. repoIdList.add(staticRepo.getRepoId());
  129. for (RepoHeader repoHeader : syncedList) {
  130. if (!repoIdList.contains(repoHeader.getRepoId())) {
  131. headerHashMap.remove(repoHeader.getRepoId());
  132. }
  133. }
  134. saveRepoHeaderMap();
  135. }
  136. }
  137. public void removeFromRepoMap(StaticRepo staticRepo) {
  138. synchronized (repoHashMap) {
  139. repoHashMap.remove(staticRepo.getRepoId());
  140. }
  141. }
  142. public void removeFromSyncMap(StaticRepo staticRepo) {
  143. synchronized (syncHashMap) {
  144. syncHashMap.remove(staticRepo.getRepoId());
  145. }
  146. }
  147. public boolean isAdded(StaticRepo staticRepo) {
  148. synchronized (repoHashMap) {
  149. return repoHashMap.containsKey(staticRepo.getRepoId());
  150. }
  151. }
  152. public boolean isSynced(String repoId) {
  153. synchronized (syncHashMap) {
  154. return syncHashMap.containsKey(repoId);
  155. }
  156. }
  157. public void clear() {
  158. synchronized (repoHashMap) {
  159. repoHashMap.clear();
  160. saveRepoMap();
  161. }
  162. }
  163. private void saveRepoMap() {
  164. synchronized (repoHashMap) {
  165. PrefUtil.putString(context, Constants.PREFERENCE_REPO_MAP, gson.toJson(repoHashMap));
  166. }
  167. }
  168. private void saveSyncMap() {
  169. synchronized (syncHashMap) {
  170. PrefUtil.putString(context, Constants.PREFERENCE_SYNC_MAP, gson.toJson(syncHashMap));
  171. }
  172. }
  173. private void saveRepoHeaderMap() {
  174. synchronized (headerHashMap) {
  175. PrefUtil.putString(context, Constants.PREFERENCE_REPO_HEADER_MAP, gson.toJson(headerHashMap));
  176. }
  177. }
  178. private HashMap<String, StaticRepo> getRepoHashMap() {
  179. final String rawList = PrefUtil.getString(context, Constants.PREFERENCE_REPO_MAP);
  180. final Type type = new TypeToken<HashMap<String, StaticRepo>>() {
  181. }.getType();
  182. final HashMap<String, StaticRepo> repoList = gson.fromJson(rawList, type);
  183. if (repoList == null)
  184. return new HashMap<>();
  185. else
  186. return repoList;
  187. }
  188. private HashMap<String, StaticRepo> getSyncedHashMap() {
  189. final String rawList = PrefUtil.getString(context, Constants.PREFERENCE_SYNC_MAP);
  190. final Type type = new TypeToken<HashMap<String, StaticRepo>>() {
  191. }.getType();
  192. final HashMap<String, StaticRepo> repoList = gson.fromJson(rawList, type);
  193. if (repoList == null)
  194. return new HashMap<>();
  195. else
  196. return repoList;
  197. }
  198. private HashMap<String, RepoHeader> getHeaderHashMap() {
  199. final String jsonString = PrefUtil.getString(context, Constants.PREFERENCE_REPO_HEADER_MAP);
  200. final Type type = new TypeToken<HashMap<String, RepoHeader>>() {
  201. }.getType();
  202. final HashMap<String, RepoHeader> repoHeaderList = gson.fromJson(jsonString, type);
  203. if (repoHeaderList == null || repoHeaderList.isEmpty())
  204. return new HashMap<>();
  205. else
  206. return repoHeaderList;
  207. }
  208. private StaticRepo getDefaultFromAssets() {
  209. try {
  210. final InputStream inputStream = context.getAssets().open("default.json");
  211. final byte[] bytes = new byte[inputStream.available()];
  212. inputStream.read(bytes);
  213. inputStream.close();
  214. final String rawJSON = new String(bytes, StandardCharsets.UTF_8);
  215. return gson.fromJson(rawJSON, StaticRepo.class);
  216. } catch (IOException e) {
  217. Log.e(e.getMessage());
  218. return null;
  219. }
  220. }
  221. }