package com.example.meinwald.ui.task; import android.content.Context; import android.graphics.Bitmap; import android.location.Location; import android.media.Image; import android.util.Log; import com.example.meinwald.BuildConfig; import com.google.android.gms.maps.model.LatLng; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; public class OwnTask { private String title; private String notice; private Location location; private Bitmap image; private String pathImage; private String id; public OwnTask(String JSONString){ try { JSONObject taskJSON = new JSONObject(JSONString); this.title = taskJSON.getString("title"); this.notice = taskJSON.getString("notice"); this.location = new Location(""); this.location.setLatitude(taskJSON.getDouble("lat")); this.location.setLongitude(taskJSON.getDouble("lng")); this.pathImage = taskJSON.getString("pathImage"); this.id = taskJSON.getString("taskID"); } catch (JSONException e) { if (BuildConfig.DEBUG) { Log.e(this.getClass().getSimpleName(), "taskJSON: " + JSONString); e.printStackTrace(); } } } public OwnTask(JSONObject taskJSON){ try { if (BuildConfig.DEBUG) { Log.d(this.getClass().getSimpleName(), "JSONObject: " + taskJSON.toString()); } this.title = taskJSON.getString("title"); this.notice = taskJSON.getString("notice"); this.location = new Location(""); this.location.setLatitude(taskJSON.getDouble("lat")); this.location.setLongitude(taskJSON.getDouble("lng")); this.id = taskJSON.getString("taskID"); this.pathImage = taskJSON.getString("taskPATHIMAGE"); } catch (JSONException e) { if (BuildConfig.DEBUG) { Log.e(this.getClass().getSimpleName(), "taskJSON: " + taskJSON.toString()); Log.e(this.getClass().getSimpleName(), "taskJSON Error: " + e.toString()); e.printStackTrace(); } } } public OwnTask() { this.title = null; this.notice = null; this.location = null; this.image = null; this.pathImage = null; this.id = null; } public OwnTask(String title, String notice, Location location, Bitmap image, String pathImage) { this.title = title; this.notice = notice; this.location = location; this.image = image; this.pathImage = pathImage; this.id = generateTaskID(title); } public String getTitle() { return title; } public String getNotice() { return notice; } public Location getLocation() { return location; } public Bitmap getImage() { return image; } public String getPathImage() { return pathImage; } public String getId() { return id; } public void setTitle(String title) { this.id = generateTaskID(title); this.title = title; } public void setNotice(String notice) { this.notice = notice; } public void setLocation(Location location) { this.location = location; } public void setImage(Bitmap image) { this.image = image; } public void setPathImage(String pathImage) { this.pathImage = pathImage; } public String saveImageToExternalStorage(Context context){ File path = null; try { path = new File(context.getFilesDir(), "meinWald" + File.separator + "Images"); if(!path.exists()){ path.mkdirs(); } if (BuildConfig.DEBUG) { Log.d(this.getClass().getSimpleName(), "Image file path: " + path); } File outFile = new File(path, this.id + ".jpeg"); if (!outFile.exists()) { FileOutputStream outputStream = new FileOutputStream(outFile); this.image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); } } catch (FileNotFoundException e) { Log.e(this.getClass().getSimpleName(), "Saving received message failed with", e); } catch (IOException e) { Log.e(this.getClass().getSimpleName(), "Saving received message failed with", e); } return path.toString(); } /** * Delete OwnArea object textfile from external storage. */ public void deleteFromExternalStorage(Context context) { if (BuildConfig.DEBUG) { Log.d(getClass().getSimpleName(), "deleteFromExternalStorage()"); } // Find the root of the external storage. File root = android.os.Environment.getExternalStorageDirectory(); //delete file if exists File file = new File(context.getFilesDir(), "meinWald" + File.pathSeparator + "Images" + File.pathSeparator + this.getId() + ".txt"); if(!file.exists()){ file.delete(); if (BuildConfig.DEBUG) { Log.d(getClass().getSimpleName(), "Delete image file: " + file.getPath()); } } } @Override public String toString() { return "OwnTask{" + "title='" + title + '\'' + ", notice='" + notice + '\'' + ", location=" + location + '\'' + ", pathImage=" + pathImage + '\'' + ", id=" + id + '}'; } private String generateTaskID(String title) { String id = title.replaceAll(" ", "_") + "_" + new Date().getTime(); if (BuildConfig.DEBUG) { Log.d(getClass().getSimpleName(), "new ID: " + id); } return id; } /** * Returns task as JSONObject. * * @return the mapped object or null, if parsing fails */ public JSONObject toJSONObject() { JSONObject object = new JSONObject(); try { object.put("title",this.title); object.put("notice",this.notice); object.put("taskPATHIMAGE",this.pathImage); object.put("taskID",this.id); if (this.location != null) { object.put("lat",this.location.getLatitude()); object.put("lng",this.location.getLongitude()); } if (BuildConfig.DEBUG) { Log.d(this.getClass().getSimpleName(), "JSONObject: " + object.toString()); Log.d(this.getClass().getSimpleName(), "Task: " + this.toString()); Log.d(this.getClass().getSimpleName(), "path: " + this.getPathImage()); Log.d(this.getClass().getSimpleName(), "id: " + this.getId()); } return object; } catch (JSONException e) { if (BuildConfig.DEBUG) { Log.e(this.getClass().getSimpleName(), String.format("Error while parsing OwnTask to JSONObject. Values of OwnTask: %s", this.toString())); } return null; } } }