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.

OwnArea.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package com.example.meinwald.ui.area;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.util.Log;
  5. import com.example.meinwald.BuildConfig;
  6. import com.google.android.gms.maps.model.LatLng;
  7. import com.google.maps.android.SphericalUtil;
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.io.PrintWriter;
  16. import java.text.SimpleDateFormat;
  17. import java.util.ArrayList;
  18. import java.util.Calendar;
  19. import java.util.Date;
  20. import java.util.List;
  21. public class OwnArea {
  22. private String title;
  23. private String notice;
  24. private List<LatLng> locations;
  25. private Bitmap image;
  26. private String pathImage;
  27. private String pathFile;
  28. private String id;
  29. private List<Date> checkHistory;
  30. private Double area;
  31. public void setImage(Bitmap image) {
  32. this.image = image;
  33. }
  34. public void setTitle(String title) {
  35. this.title = title;
  36. this.id = generateAreaID(title);
  37. }
  38. public void setNotice(String notice) {
  39. this.notice = notice;
  40. }
  41. public void setLocations(List<LatLng> locations) {
  42. this.locations = locations;
  43. }
  44. public void setPathImage(String pathImage) {
  45. this.pathImage = pathImage;
  46. }
  47. public void setPathFile(String pathFile) {
  48. this.pathFile = pathFile;
  49. }
  50. public Double getArea() {
  51. return area;
  52. }
  53. public void setArea(Double area) {
  54. this.area = area;
  55. }
  56. public void calculateArea()
  57. {
  58. this.area = SphericalUtil.computeArea(this.locations);
  59. }
  60. public String getNotice() {
  61. return notice;
  62. }
  63. public String getTitle() {
  64. return title;
  65. }
  66. public Bitmap getImage() {
  67. return image;
  68. }
  69. public List<LatLng> getLocations() {
  70. return locations;
  71. }
  72. public String getId() {
  73. return id;
  74. }
  75. public String getPathImage() {
  76. return pathImage;
  77. }
  78. public String getPathFile() {
  79. return pathFile;
  80. }
  81. public List<Date> getCheckHistory() {
  82. return checkHistory;
  83. }
  84. public void setCheckHistory(List<Date> checkHistory) {
  85. this.checkHistory = checkHistory;
  86. }
  87. public void addCheckDate(Date newDate)
  88. {
  89. if (BuildConfig.DEBUG)
  90. {
  91. Log.d(getClass().getSimpleName(), "add DATE");
  92. }
  93. if(this.checkHistory == null)
  94. {
  95. this.checkHistory = new ArrayList<>();
  96. }
  97. this.checkHistory.add(newDate);
  98. }
  99. public OwnArea()
  100. {
  101. this.title = null;
  102. this.id = null;
  103. this.notice = null;
  104. this.pathFile = null;
  105. this.image = null;
  106. this.pathImage = null;
  107. this.locations = new ArrayList<>();
  108. this.checkHistory = new ArrayList<>();
  109. }
  110. public OwnArea(String jsonString)
  111. {
  112. this.checkHistory = new ArrayList<>();
  113. this.locations = new ArrayList<>();
  114. try {
  115. JSONObject areaObject = new JSONObject(jsonString);
  116. this.title = areaObject.getString("title");
  117. this.notice = areaObject.getString("description");
  118. this.id = areaObject.getString("id");
  119. String locationsString = areaObject.getString("locations");
  120. JSONArray locations = new JSONArray(locationsString);
  121. this.locations = new ArrayList<>();
  122. for (int i = 0; i<locations.length();i++) {
  123. this.locations.add(new LatLng((float) (locations.getJSONObject(i).getDouble("Lat")), (float) (locations.getJSONObject(i).getDouble("Lng"))));
  124. if (BuildConfig.DEBUG)
  125. {
  126. Log.d(getClass().getSimpleName(), "read location: " + this.locations.get(i).toString());
  127. }
  128. }
  129. String historyString = areaObject.getString("history");
  130. JSONArray history = new JSONArray(historyString);
  131. this.checkHistory = new ArrayList<>();
  132. for (int i = 0; i<history.length();i++) {
  133. this.checkHistory.add(new Date(history.getJSONObject(i).getLong("date")));
  134. if (BuildConfig.DEBUG)
  135. {
  136. Log.d(getClass().getSimpleName(), "read location: " + this.checkHistory.get(i).toString());
  137. }
  138. }
  139. if (this.locations != null && this.locations.size()>2)
  140. {
  141. calculateArea();
  142. }
  143. } catch (JSONException e) {
  144. if (BuildConfig.DEBUG) {
  145. Log.e(this.getClass().getSimpleName(), "area json: " + jsonString);
  146. Log.e(this.getClass().getSimpleName(), e.toString());
  147. e.printStackTrace();
  148. }
  149. }
  150. }
  151. private String generateAreaID(String title)
  152. {
  153. String id = title.replaceAll(" ", "_") + "_" + new Date().getTime();
  154. if (BuildConfig.DEBUG)
  155. {
  156. Log.d(getClass().getSimpleName(), "new ID: " + id);
  157. }
  158. return id;
  159. }
  160. /**
  161. * Save OwnArea object in textfile on external storage.
  162. *
  163. * @return
  164. */
  165. public void writeToExternalStorage(Context context){
  166. if (BuildConfig.DEBUG)
  167. {
  168. Log.d(getClass().getSimpleName(), "writeAreaToExternalStorage()");
  169. }
  170. // Find the root of the external storage.
  171. File root = android.os.Environment.getExternalStorageDirectory();
  172. //create file if it does not exist
  173. File path = new File (context.getFilesDir(), "meinWald" + File.pathSeparator + "areas");
  174. if(!path.exists()){
  175. path.mkdirs();
  176. if (BuildConfig.DEBUG)
  177. {
  178. Log.d(getClass().getSimpleName(), "Create new text file: " + path);
  179. }
  180. }
  181. File file = new File(path, this.getId() + ".txt");
  182. //open file and write to it
  183. try {
  184. FileOutputStream f = new FileOutputStream(file);
  185. PrintWriter pw = new PrintWriter(f);
  186. pw.print(this.toJSONObject());
  187. if (BuildConfig.DEBUG)
  188. {
  189. Log.d(getClass().getSimpleName(), "Json to write in file: " + this.toJSONObject().toString());
  190. }
  191. pw.close();
  192. f.close();
  193. } catch (FileNotFoundException e) {
  194. if (BuildConfig.DEBUG)
  195. {
  196. Log.e(getClass().getSimpleName(), "Failed write to external storage: " + e.toString());
  197. }
  198. } catch (IOException e) {
  199. if (BuildConfig.DEBUG)
  200. {
  201. Log.e(getClass().getSimpleName(), "Failed write to external storage: " + e.toString());
  202. }
  203. }
  204. this.pathFile = file.getPath();
  205. }
  206. /**
  207. * Delete OwnArea object textfile from external storage.
  208. */
  209. public void deleteFromExternalStorage(Context context)
  210. {
  211. if (BuildConfig.DEBUG)
  212. {
  213. Log.d(getClass().getSimpleName(), "deleteFromExternalStorage()");
  214. }
  215. // Find the root of the external storage.
  216. File root = android.os.Environment.getExternalStorageDirectory();
  217. //delete file if exists
  218. File file = new File(context.getFilesDir(), "meinWald" + File.pathSeparator + "areas" + File.pathSeparator + this.getId() + ".txt");
  219. if(!file.exists()){
  220. file.delete();
  221. if (BuildConfig.DEBUG)
  222. {
  223. Log.d(getClass().getSimpleName(), "Delete text file: " + file.getPath());
  224. }
  225. }
  226. }
  227. /**
  228. * Returns OwnArea as JSONObject.
  229. *
  230. * @return the mapped object or null, if parsing fails
  231. */
  232. public JSONObject toJSONObject() {
  233. JSONObject object = new JSONObject();
  234. try {
  235. object.put("title",this.title);
  236. object.put("description",this.notice);
  237. object.put("id",this.id);
  238. //convert all locations
  239. JSONArray writeObjects = new JSONArray();
  240. if (BuildConfig.DEBUG) {
  241. Log.d("OwnArea", "Save locations, locations size: " + locations.size());
  242. }
  243. if (this.locations != null && this.locations.size() > 0)
  244. {
  245. for (LatLng location: this.locations) {
  246. JSONObject singleLocationObject = new JSONObject();
  247. singleLocationObject.put("Lat", location.latitude);
  248. singleLocationObject.put("Lng", location.longitude);
  249. writeObjects.put(singleLocationObject);
  250. if (BuildConfig.DEBUG) {
  251. Log.d("OwnArea", "added location object: " + singleLocationObject.toString());
  252. }
  253. }
  254. }
  255. object.put("locations", writeObjects.toString());
  256. //convert all locations
  257. writeObjects = new JSONArray();
  258. if (BuildConfig.DEBUG) {
  259. Log.d("OwnArea", "Save history, history size: " + checkHistory.size());
  260. }
  261. if (this.checkHistory != null && this.checkHistory.size() > 0)
  262. {
  263. if (BuildConfig.DEBUG) {
  264. Log.d("OwnArea", "history not empty");
  265. }
  266. for (Date date : this.checkHistory) {
  267. JSONObject singleHistoryObject = new JSONObject();
  268. singleHistoryObject.put("date", date.getTime());
  269. writeObjects.put(singleHistoryObject);
  270. if (BuildConfig.DEBUG) {
  271. Log.d("OwnArea", "added history object: " + singleHistoryObject.toString());
  272. }
  273. }
  274. }
  275. object.put("history", writeObjects.toString());
  276. if (area!=null)
  277. {
  278. object.put("area",this.area);
  279. }
  280. else
  281. {
  282. object.put("area", null);
  283. }
  284. return object;
  285. } catch (JSONException e) {
  286. if (BuildConfig.DEBUG) {
  287. Log.e(this.getClass().getSimpleName(),
  288. String.format("Error while parsing OwnArea to JSONObject. Values of OwnArea: %s", this.toString()));
  289. }
  290. return null;
  291. }
  292. }
  293. @Override
  294. public String toString() {
  295. return "OwnArea{" +
  296. "title='" + title + '\'' +
  297. ", notice='" + notice + '\'' +
  298. ", pathImage='" + pathImage + '\'' +
  299. ", pathLocations='" + pathFile + '\'' +
  300. ", id='" + id + '\'' +
  301. ", dates='" + checkHistory.size() + '\'' +
  302. '}';
  303. }
  304. /**
  305. * Parses last check update from milliseconds to string and formats it to "dd.MM.yyyy hh:mm".
  306. *
  307. * @return date parsed and mapped as string representation
  308. */
  309. public String parseAndFormatLastCheckUpdate() {
  310. SimpleDateFormat formatter = new SimpleDateFormat( "dd.MM.yyyy kk:mm");
  311. Calendar calendar = Calendar.getInstance();
  312. if (checkHistory != null && checkHistory.size()> 0)
  313. {
  314. calendar.setTimeInMillis(this.checkHistory.get(this.checkHistory.size()-1).getTime());
  315. }
  316. else
  317. {
  318. return "Keine Kontrolle vorhanden!";
  319. }
  320. return formatter.format(calendar.getTime());
  321. }
  322. public Long getLastChecked()
  323. {
  324. if (checkHistory != null && checkHistory.size()> 0)
  325. {
  326. return this.checkHistory.get(this.checkHistory.size()-1).getTime();
  327. }
  328. else
  329. {
  330. return (long) -1;
  331. }
  332. }
  333. }