123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- package com.example.meinwald.ui.area;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.util.Log;
-
- import com.example.meinwald.BuildConfig;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.maps.android.SphericalUtil;
-
- import org.json.JSONArray;
- 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.io.PrintWriter;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- 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 pathFile;
- private String id;
- private List<Date> checkHistory;
- private Double area;
-
- 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 setPathFile(String pathFile) {
- this.pathFile = pathFile;
- }
-
- public Double getArea() {
- return area;
- }
-
- public void setArea(Double area) {
- this.area = area;
- }
-
- public void calculateArea()
- {
- this.area = SphericalUtil.computeArea(this.locations);
- }
-
- 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 getPathFile() {
- return pathFile;
- }
-
- public List<Date> getCheckHistory() {
- return checkHistory;
- }
-
- public void setCheckHistory(List<Date> checkHistory) {
- this.checkHistory = checkHistory;
- }
-
- public void addCheckDate(Date newDate)
- {
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "add DATE");
- }
- if(this.checkHistory == null)
- {
- this.checkHistory = new ArrayList<>();
- }
-
- this.checkHistory.add(newDate);
- }
-
- public OwnArea()
- {
- this.title = null;
- this.id = null;
- this.notice = null;
- this.pathFile = null;
- this.image = null;
- this.pathImage = null;
- this.locations = new ArrayList<>();
- this.checkHistory = new ArrayList<>();
- }
-
- public OwnArea(String jsonString)
- {
- this.checkHistory = new ArrayList<>();
- this.locations = new ArrayList<>();
-
- 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());
- }
- }
-
- String historyString = areaObject.getString("history");
- JSONArray history = new JSONArray(historyString);
- this.checkHistory = new ArrayList<>();
-
- for (int i = 0; i<history.length();i++) {
- this.checkHistory.add(new Date(history.getJSONObject(i).getLong("date")));
-
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "read location: " + this.checkHistory.get(i).toString());
- }
- }
-
- if (this.locations != null && this.locations.size()>2)
- {
- calculateArea();
- }
-
- } 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;
- }
-
- /**
- * Save OwnArea object in textfile on external storage.
- *
- * @return
- */
- public void writeToExternalStorage(Context context){
-
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "writeAreaToExternalStorage()");
- }
-
- // Find the root of the external storage.
- File root = android.os.Environment.getExternalStorageDirectory();
-
- //create file if it does not exist
- File path = new File (context.getFilesDir(), "meinWald" + File.pathSeparator + "areas");
- if(!path.exists()){
- path.mkdirs();
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "Create new text file: " + path);
- }
- }
- File file = new File(path, this.getId() + ".txt");
-
- //open file and write to it
- try {
- FileOutputStream f = new FileOutputStream(file);
- PrintWriter pw = new PrintWriter(f);
- pw.print(this.toJSONObject());
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "Json to write in file: " + this.toJSONObject().toString());
- }
- pw.close();
- f.close();
- } catch (FileNotFoundException e) {
- if (BuildConfig.DEBUG)
- {
- Log.e(getClass().getSimpleName(), "Failed write to external storage: " + e.toString());
- }
- } catch (IOException e) {
- if (BuildConfig.DEBUG)
- {
- Log.e(getClass().getSimpleName(), "Failed write to external storage: " + e.toString());
- }
- }
-
- this.pathFile = file.getPath();
- }
-
- /**
- * 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 + "areas" + File.pathSeparator + this.getId() + ".txt");
- if(!file.exists()){
-
- file.delete();
-
- if (BuildConfig.DEBUG)
- {
- Log.d(getClass().getSimpleName(), "Delete text file: " + file.getPath());
- }
- }
-
- }
-
-
- /**
- * 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();
-
- if (BuildConfig.DEBUG) {
- Log.d("OwnArea", "Save locations, locations size: " + locations.size());
- }
-
- if (this.locations != null && this.locations.size() > 0)
- {
- 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());
-
- //convert all locations
- writeObjects = new JSONArray();
-
- if (BuildConfig.DEBUG) {
- Log.d("OwnArea", "Save history, history size: " + checkHistory.size());
- }
-
- if (this.checkHistory != null && this.checkHistory.size() > 0)
- {
- if (BuildConfig.DEBUG) {
- Log.d("OwnArea", "history not empty");
- }
-
- for (Date date : this.checkHistory) {
-
- JSONObject singleHistoryObject = new JSONObject();
- singleHistoryObject.put("date", date.getTime());
-
- writeObjects.put(singleHistoryObject);
-
- if (BuildConfig.DEBUG) {
- Log.d("OwnArea", "added history object: " + singleHistoryObject.toString());
- }
- }
- }
-
- object.put("history", writeObjects.toString());
-
- if (area!=null)
- {
- object.put("area",this.area);
- }
- else
- {
- object.put("area", null);
- }
-
- return object;
-
- } catch (JSONException e) {
- if (BuildConfig.DEBUG) {
- Log.e(this.getClass().getSimpleName(),
- String.format("Error while parsing OwnArea to JSONObject. Values of OwnArea: %s", this.toString()));
- }
- return null;
- }
- }
-
- @Override
- public String toString() {
- return "OwnArea{" +
- "title='" + title + '\'' +
- ", notice='" + notice + '\'' +
- ", pathImage='" + pathImage + '\'' +
- ", pathLocations='" + pathFile + '\'' +
- ", id='" + id + '\'' +
- ", dates='" + checkHistory.size() + '\'' +
- '}';
- }
-
- /**
- * Parses last check update from milliseconds to string and formats it to "dd.MM.yyyy hh:mm".
- *
- * @return date parsed and mapped as string representation
- */
- public String parseAndFormatLastCheckUpdate() {
- SimpleDateFormat formatter = new SimpleDateFormat( "dd.MM.yyyy kk:mm");
- Calendar calendar = Calendar.getInstance();
- if (checkHistory != null && checkHistory.size()> 0)
- {
- calendar.setTimeInMillis(this.checkHistory.get(this.checkHistory.size()-1).getTime());
- }
- else
- {
- return "Keine Kontrolle vorhanden!";
- }
-
- return formatter.format(calendar.getTime());
- }
-
- public Long getLastChecked()
- {
- if (checkHistory != null && checkHistory.size()> 0)
- {
- return this.checkHistory.get(this.checkHistory.size()-1).getTime();
- }
- else
- {
- return (long) -1;
- }
- }
- }
|