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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.example.meinwald.ui.task;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.location.Location;
  5. import android.media.Image;
  6. import android.util.Log;
  7. import com.example.meinwald.BuildConfig;
  8. import com.google.android.gms.maps.model.LatLng;
  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.util.Date;
  16. public class OwnTask
  17. {
  18. private String title;
  19. private String notice;
  20. private Location location;
  21. private Bitmap image;
  22. private String pathImage;
  23. private String id;
  24. public OwnTask(String JSONString){
  25. try {
  26. JSONObject taskJSON = new JSONObject(JSONString);
  27. this.title = taskJSON.getString("title");
  28. this.notice = taskJSON.getString("notice");
  29. this.location = new Location("");
  30. this.location.setLatitude(taskJSON.getDouble("lat"));
  31. this.location.setLongitude(taskJSON.getDouble("lng"));
  32. this.pathImage = taskJSON.getString("pathImage");
  33. this.id = taskJSON.getString("taskID");
  34. } catch (JSONException e) {
  35. if (BuildConfig.DEBUG) {
  36. Log.e(this.getClass().getSimpleName(), "taskJSON: " + JSONString);
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. public OwnTask(JSONObject taskJSON){
  42. try {
  43. if (BuildConfig.DEBUG) {
  44. Log.d(this.getClass().getSimpleName(), "JSONObject: " + taskJSON.toString());
  45. }
  46. this.title = taskJSON.getString("title");
  47. this.notice = taskJSON.getString("notice");
  48. this.location = new Location("");
  49. this.location.setLatitude(taskJSON.getDouble("lat"));
  50. this.location.setLongitude(taskJSON.getDouble("lng"));
  51. this.id = taskJSON.getString("taskID");
  52. this.pathImage = taskJSON.getString("taskPATHIMAGE");
  53. } catch (JSONException e) {
  54. if (BuildConfig.DEBUG) {
  55. Log.e(this.getClass().getSimpleName(), "taskJSON: " + taskJSON.toString());
  56. Log.e(this.getClass().getSimpleName(), "taskJSON Error: " + e.toString());
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. public OwnTask()
  62. {
  63. this.title = null;
  64. this.notice = null;
  65. this.location = null;
  66. this.image = null;
  67. this.pathImage = null;
  68. this.id = null;
  69. }
  70. public OwnTask(String title, String notice, Location location, Bitmap image, String pathImage)
  71. {
  72. this.title = title;
  73. this.notice = notice;
  74. this.location = location;
  75. this.image = image;
  76. this.pathImage = pathImage;
  77. this.id = generateTaskID(title);
  78. }
  79. public String getTitle() {
  80. return title;
  81. }
  82. public String getNotice() {
  83. return notice;
  84. }
  85. public Location getLocation() {
  86. return location;
  87. }
  88. public Bitmap getImage() {
  89. return image;
  90. }
  91. public String getPathImage() { return pathImage; }
  92. public String getId() { return id; }
  93. public void setTitle(String title)
  94. {
  95. this.id = generateTaskID(title);
  96. this.title = title;
  97. }
  98. public void setNotice(String notice) {
  99. this.notice = notice;
  100. }
  101. public void setLocation(Location location) {
  102. this.location = location;
  103. }
  104. public void setImage(Bitmap image) {
  105. this.image = image;
  106. }
  107. public void setPathImage(String pathImage) { this.pathImage = pathImage; }
  108. public String saveImageToExternalStorage(Context context){
  109. File path = null;
  110. try {
  111. path = new File(context.getFilesDir(), "meinWald" + File.separator + "Images");
  112. if(!path.exists()){
  113. path.mkdirs();
  114. }
  115. if (BuildConfig.DEBUG) {
  116. Log.d(this.getClass().getSimpleName(), "Image file path: " + path);
  117. }
  118. File outFile = new File(path, this.id + ".jpeg");
  119. if (!outFile.exists())
  120. {
  121. FileOutputStream outputStream = new FileOutputStream(outFile);
  122. this.image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
  123. outputStream.close();
  124. }
  125. } catch (FileNotFoundException e) {
  126. Log.e(this.getClass().getSimpleName(), "Saving received message failed with", e);
  127. } catch (IOException e) {
  128. Log.e(this.getClass().getSimpleName(), "Saving received message failed with", e);
  129. }
  130. return path.toString();
  131. }
  132. /**
  133. * Delete OwnArea object textfile from external storage.
  134. */
  135. public void deleteFromExternalStorage(Context context)
  136. {
  137. if (BuildConfig.DEBUG)
  138. {
  139. Log.d(getClass().getSimpleName(), "deleteFromExternalStorage()");
  140. }
  141. // Find the root of the external storage.
  142. File root = android.os.Environment.getExternalStorageDirectory();
  143. //delete file if exists
  144. File file = new File(context.getFilesDir(), "meinWald" + File.pathSeparator + "Images" + File.pathSeparator + this.getId() + ".txt");
  145. if(!file.exists()){
  146. file.delete();
  147. if (BuildConfig.DEBUG)
  148. {
  149. Log.d(getClass().getSimpleName(), "Delete image file: " + file.getPath());
  150. }
  151. }
  152. }
  153. @Override
  154. public String toString() {
  155. return "OwnTask{" +
  156. "title='" + title + '\'' +
  157. ", notice='" + notice + '\'' +
  158. ", location=" + location + '\'' +
  159. ", pathImage=" + pathImage + '\'' +
  160. ", id=" + id +
  161. '}';
  162. }
  163. private String generateTaskID(String title)
  164. {
  165. String id = title.replaceAll(" ", "_") + "_" + new Date().getTime();
  166. if (BuildConfig.DEBUG)
  167. {
  168. Log.d(getClass().getSimpleName(), "new ID: " + id);
  169. }
  170. return id;
  171. }
  172. /**
  173. * Returns task as JSONObject.
  174. *
  175. * @return the mapped object or null, if parsing fails
  176. */
  177. public JSONObject toJSONObject() {
  178. JSONObject object = new JSONObject();
  179. try {
  180. object.put("title",this.title);
  181. object.put("notice",this.notice);
  182. object.put("taskPATHIMAGE",this.pathImage);
  183. object.put("taskID",this.id);
  184. if (this.location != null)
  185. {
  186. object.put("lat",this.location.getLatitude());
  187. object.put("lng",this.location.getLongitude());
  188. }
  189. if (BuildConfig.DEBUG) {
  190. Log.d(this.getClass().getSimpleName(), "JSONObject: " + object.toString());
  191. Log.d(this.getClass().getSimpleName(), "Task: " + this.toString());
  192. Log.d(this.getClass().getSimpleName(), "path: " + this.getPathImage());
  193. Log.d(this.getClass().getSimpleName(), "id: " + this.getId());
  194. }
  195. return object;
  196. } catch (JSONException e) {
  197. if (BuildConfig.DEBUG) {
  198. Log.e(this.getClass().getSimpleName(), String.format("Error while parsing OwnTask to JSONObject. Values of OwnTask: %s", this.toString()));
  199. }
  200. return null;
  201. }
  202. }
  203. }