123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- 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.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
-
- import java.sql.Time;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- public class OwnArea {
-
- private String title;
- private String notice;
- private List<LatLng> locations;
- private Bitmap image;
- private String pathImage;
- private String pathLocations;
- private String id;
-
- 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<LatLng> 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<LatLng> 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");
-
- String locationsString = areaObject.getString("locations");
- JSONArray locations = new JSONArray(locationsString);
- this.locations = new ArrayList<>();
-
- for (int i = 0; i<locations.length();i++) {
- this.locations.add(new LatLng((float) (locations.getJSONObject(i).getDouble("Lat")), (float) (locations.getJSONObject(i).getDouble("Lng"))));
-
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "read location: " + this.locations.get(i).toString());
- }
- }
-
- } catch (JSONException e) {
- if (BuildConfig.DEBUG) {
- Log.e(this.getClass().getSimpleName(), "area json: " + jsonString);
- Log.e(this.getClass().getSimpleName(), e.toString());
- 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);
-
- //convert all locations
- JSONArray writeObjects = new JSONArray();
-
- for (LatLng location: this.locations) {
-
- JSONObject singleLocationObject = new JSONObject();
- singleLocationObject.put("Lat", location.latitude);
- singleLocationObject.put("Lng", location.longitude);
-
- writeObjects.put(singleLocationObject);
-
- if (BuildConfig.DEBUG) {
- Log.d("OwnArea", "added location object: " + singleLocationObject.toString());
- }
- }
-
- object.put("locations", writeObjects.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 + '\'' +
- '}';
- }
- }
|