package com.example.meinwald.ui.area; import android.graphics.Bitmap; import android.location.Location; 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.sql.Time; import java.util.Date; import java.util.List; public class OwnArea { private String title; private String notice; private List locations; private Bitmap image; private String pathImage; private String pathLocations; private String id; private Date time; public void setImage(Bitmap image) { this.image = image; } public void setTitle(String title) { this.title = title; this.id = generateAreaID(title); } public void setNotice(String notice) { this.notice = notice; } public void setLocations(List locations) { this.locations = locations; } public void setPathImage(String pathImage) { this.pathImage = pathImage; } public void setPathLocations(String pathLocations) { this.pathLocations = pathLocations; } public String getNotice() { return notice; } public String getTitle() { return title; } public Bitmap getImage() { return image; } public List getLocations() { return locations; } public String getId() { return id; } public String getPathImage() { return pathImage; } public String getPathLocations() { return pathLocations; } public OwnArea() { } public OwnArea(String jsonString){ try { JSONObject areaObject = new JSONObject(jsonString); this.title = areaObject.getString("title"); this.notice = areaObject.getString("description"); this.id = areaObject.getString("id"); this.time = new Date(areaObject.getString("time")); } catch (JSONException e) { if (BuildConfig.DEBUG) { Log.e(this.getClass().getSimpleName(), "earthquakeJSON " + jsonString); e.printStackTrace(); } } } private String generateAreaID(String title) { String id = title.replaceAll(" ", "_") + "_" + new Date().getTime(); if (BuildConfig.DEBUG) { Log.d(getClass().getSimpleName(), "new ID: " + id); } return id; } /** * Returns OwnArea 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("description",this.notice); object.put("id",this.id); if (this.time != null) { object.put("time", this.time); } else { object.put("time", new Date().getTime()); } JSONObject locations = new JSONObject(); Long count = Long.valueOf(0); //convert all locations for (LatLng location: this.locations) { JSONObject locationObject = new JSONObject(); locationObject.put("Lat", location.latitude); locationObject.put("Lng", location.longitude); locations.put("location"+count, locationObject.toString()); } object.put("locations", locations.toString()); return object; } catch (JSONException e) { if (BuildConfig.DEBUG) { Log.e(this.getClass().getSimpleName(), String.format("Error while parsing epiColorDto to JSONObject. Values of epiColorDto: %s", this.toString())); } return null; } } @Override public String toString() { return "OwnArea{" + "title='" + title + '\'' + ", notice='" + notice + '\'' + ", pathImage='" + pathImage + '\'' + ", pathLocations='" + pathLocations + '\'' + ", id='" + id + '\'' + '}'; } }