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.

OwnTask.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.example.meinwald.ui.task;
  2. import android.graphics.Bitmap;
  3. import android.location.Location;
  4. import android.media.Image;
  5. import android.util.Log;
  6. import com.example.meinwald.BuildConfig;
  7. import com.google.android.gms.maps.model.LatLng;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10. import java.util.Date;
  11. public class OwnTask
  12. {
  13. private String title;
  14. private String notice;
  15. private Location location;
  16. private Bitmap image;
  17. private String pathImage;
  18. private String id;
  19. public OwnTask(String JSONString){
  20. try {
  21. JSONObject taskJSON = new JSONObject(JSONString);
  22. this.title = taskJSON.getString("title");
  23. this.notice = taskJSON.getString("notice");
  24. this.location = new Location("");
  25. this.location.setLatitude(taskJSON.getDouble("lat"));
  26. this.location.setLongitude(taskJSON.getDouble("lng"));
  27. this.pathImage = taskJSON.getString("pathImage");
  28. this.id = taskJSON.getString("taskID");
  29. } catch (JSONException e) {
  30. if (BuildConfig.DEBUG) {
  31. Log.e(this.getClass().getSimpleName(), "taskJSON: " + JSONString);
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. public OwnTask(JSONObject taskJSON){
  37. try {
  38. if (BuildConfig.DEBUG) {
  39. Log.d(this.getClass().getSimpleName(), "JSONObject: " + taskJSON.toString());
  40. }
  41. this.title = taskJSON.getString("title");
  42. this.notice = taskJSON.getString("notice");
  43. this.location = new Location("");
  44. this.location.setLatitude(taskJSON.getDouble("lat"));
  45. this.location.setLongitude(taskJSON.getDouble("lng"));
  46. this.id = taskJSON.getString("taskID");
  47. this.pathImage = taskJSON.getString("taskPATHIMAGE");
  48. } catch (JSONException e) {
  49. if (BuildConfig.DEBUG) {
  50. Log.e(this.getClass().getSimpleName(), "taskJSON: " + taskJSON.toString());
  51. Log.e(this.getClass().getSimpleName(), "taskJSON Error: " + e.toString());
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. public OwnTask()
  57. {
  58. this.title = null;
  59. this.notice = null;
  60. this.location = null;
  61. this.image = null;
  62. this.pathImage = null;
  63. this.id = null;
  64. }
  65. public OwnTask(String title, String notice, Location location, Bitmap image, String pathImage)
  66. {
  67. this.title = title;
  68. this.notice = notice;
  69. this.location = location;
  70. this.image = image;
  71. this.pathImage = pathImage;
  72. this.id = generateTaskID(title);
  73. }
  74. public String getTitle() {
  75. return title;
  76. }
  77. public String getNotice() {
  78. return notice;
  79. }
  80. public Location getLocation() {
  81. return location;
  82. }
  83. public Bitmap getImage() {
  84. return image;
  85. }
  86. public String getPathImage() { return pathImage; }
  87. public String getId() { return id; }
  88. public void setTitle(String title)
  89. {
  90. this.id = generateTaskID(title);
  91. this.title = title;
  92. }
  93. public void setNotice(String notice) {
  94. this.notice = notice;
  95. }
  96. public void setLocation(Location location) {
  97. this.location = location;
  98. }
  99. public void setImage(Bitmap image) {
  100. this.image = image;
  101. }
  102. public void setPathImage(String pathImage) { this.pathImage = pathImage; }
  103. @Override
  104. public String toString() {
  105. return "OwnTask{" +
  106. "title='" + title + '\'' +
  107. ", notice='" + notice + '\'' +
  108. ", location=" + location + '\'' +
  109. ", pathImage=" + pathImage + '\'' +
  110. ", id=" + id +
  111. '}';
  112. }
  113. private String generateTaskID(String title)
  114. {
  115. String id = title.replaceAll(" ", "_") + "_" + new Date().getTime();
  116. if (BuildConfig.DEBUG)
  117. {
  118. Log.d(getClass().getSimpleName(), "new ID: " + id);
  119. }
  120. return id;
  121. }
  122. /**
  123. * Returns task as JSONObject.
  124. *
  125. * @return the mapped object or null, if parsing fails
  126. */
  127. public JSONObject toJSONObject() {
  128. JSONObject object = new JSONObject();
  129. try {
  130. object.put("title",this.title);
  131. object.put("notice",this.notice);
  132. object.put("taskPATHIMAGE",this.pathImage);
  133. object.put("taskID",this.id);
  134. if (this.location != null)
  135. {
  136. object.put("lat",this.location.getLatitude());
  137. object.put("lng",this.location.getLongitude());
  138. }
  139. if (BuildConfig.DEBUG) {
  140. Log.d(this.getClass().getSimpleName(), "JSONObject: " + object.toString());
  141. Log.d(this.getClass().getSimpleName(), "Task: " + this.toString());
  142. Log.d(this.getClass().getSimpleName(), "path: " + this.getPathImage());
  143. Log.d(this.getClass().getSimpleName(), "id: " + this.getId());
  144. }
  145. return object;
  146. } catch (JSONException e) {
  147. if (BuildConfig.DEBUG) {
  148. Log.e(this.getClass().getSimpleName(), String.format("Error while parsing OwnTask to JSONObject. Values of OwnTask: %s", this.toString()));
  149. }
  150. return null;
  151. }
  152. }
  153. }