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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.example.meinwald.ui.area;
  2. import android.graphics.Bitmap;
  3. import android.location.Location;
  4. import android.util.Log;
  5. import com.example.meinwald.BuildConfig;
  6. import com.google.android.gms.maps.model.LatLng;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import java.sql.Time;
  10. import java.util.Date;
  11. import java.util.List;
  12. public class OwnArea {
  13. private String title;
  14. private String notice;
  15. private List<LatLng> locations;
  16. private Bitmap image;
  17. private String pathImage;
  18. private String pathLocations;
  19. private String id;
  20. private Date time;
  21. public void setImage(Bitmap image) {
  22. this.image = image;
  23. }
  24. public void setTitle(String title) {
  25. this.title = title;
  26. this.id = generateAreaID(title);
  27. }
  28. public void setNotice(String notice) {
  29. this.notice = notice;
  30. }
  31. public void setLocations(List<LatLng> locations) {
  32. this.locations = locations;
  33. }
  34. public void setPathImage(String pathImage) {
  35. this.pathImage = pathImage;
  36. }
  37. public void setPathLocations(String pathLocations) {
  38. this.pathLocations = pathLocations;
  39. }
  40. public String getNotice() {
  41. return notice;
  42. }
  43. public String getTitle() {
  44. return title;
  45. }
  46. public Bitmap getImage() {
  47. return image;
  48. }
  49. public List<LatLng> getLocations() {
  50. return locations;
  51. }
  52. public String getId() {
  53. return id;
  54. }
  55. public String getPathImage() {
  56. return pathImage;
  57. }
  58. public String getPathLocations() {
  59. return pathLocations;
  60. }
  61. public OwnArea()
  62. {
  63. }
  64. public OwnArea(String jsonString){
  65. try {
  66. JSONObject areaObject = new JSONObject(jsonString);
  67. this.title = areaObject.getString("title");
  68. this.notice = areaObject.getString("description");
  69. this.id = areaObject.getString("id");
  70. this.time = new Date(areaObject.getString("time"));
  71. } catch (JSONException e) {
  72. if (BuildConfig.DEBUG) {
  73. Log.e(this.getClass().getSimpleName(), "earthquakeJSON " + jsonString);
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. private String generateAreaID(String title)
  79. {
  80. String id = title.replaceAll(" ", "_") + "_" + new Date().getTime();
  81. if (BuildConfig.DEBUG)
  82. {
  83. Log.d(getClass().getSimpleName(), "new ID: " + id);
  84. }
  85. return id;
  86. }
  87. /**
  88. * Returns OwnArea as JSONObject.
  89. *
  90. * @return the mapped object or null, if parsing fails
  91. */
  92. public JSONObject toJSONObject() {
  93. JSONObject object = new JSONObject();
  94. try {
  95. object.put("title",this.title);
  96. object.put("description",this.notice);
  97. object.put("id",this.id);
  98. if (this.time != null)
  99. {
  100. object.put("time", this.time);
  101. }
  102. else
  103. {
  104. object.put("time", new Date().getTime());
  105. }
  106. JSONObject locations = new JSONObject();
  107. Long count = Long.valueOf(0);
  108. //convert all locations
  109. for (LatLng location: this.locations)
  110. {
  111. JSONObject locationObject = new JSONObject();
  112. locationObject.put("Lat", location.latitude);
  113. locationObject.put("Lng", location.longitude);
  114. locations.put("location"+count, locationObject.toString());
  115. }
  116. object.put("locations", locations.toString());
  117. return object;
  118. } catch (JSONException e) {
  119. if (BuildConfig.DEBUG) {
  120. Log.e(this.getClass().getSimpleName(),
  121. String.format("Error while parsing epiColorDto to JSONObject. Values of epiColorDto: %s", this.toString()));
  122. }
  123. return null;
  124. }
  125. }
  126. @Override
  127. public String toString() {
  128. return "OwnArea{" +
  129. "title='" + title + '\'' +
  130. ", notice='" + notice + '\'' +
  131. ", pathImage='" + pathImage + '\'' +
  132. ", pathLocations='" + pathLocations + '\'' +
  133. ", id='" + id + '\'' +
  134. '}';
  135. }
  136. }