123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- 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;
- }
- }
- }
|