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

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