1st version that works
This commit is contained in:
203
libSafeMobile/src/main/java/com/safemobile/lib/Position.java
Normal file
203
libSafeMobile/src/main/java/com/safemobile/lib/Position.java
Normal file
@ -0,0 +1,203 @@
|
||||
package com.safemobile.lib;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by Adi on 8/11/2017.
|
||||
*/
|
||||
|
||||
public class Position implements Parcelable, Comparable<Position>{
|
||||
// region private fields
|
||||
public double latitude;
|
||||
public double longitude;
|
||||
public Date positionTime;
|
||||
public double speed;
|
||||
public String address;
|
||||
public float accuracy;
|
||||
//endregion
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public Position()
|
||||
{
|
||||
latitude = 0;
|
||||
longitude = 0;
|
||||
positionTime = new Date(0);
|
||||
speed = 0;
|
||||
address = "";
|
||||
accuracy = 0;
|
||||
}
|
||||
|
||||
public Position(String json)
|
||||
{
|
||||
try {
|
||||
JSONObject posObj = new JSONObject(json);
|
||||
speed = posObj.isNull("speed")
|
||||
? 0 : posObj.getDouble("speed");
|
||||
latitude = posObj.isNull("latitude")
|
||||
? 0 : posObj.getDouble("latitude");
|
||||
longitude = posObj.isNull("longitude")
|
||||
? 0 : posObj.getDouble("longitude");
|
||||
address = posObj.isNull("address")
|
||||
? "" : posObj.getString("address");
|
||||
|
||||
accuracy = posObj.isNull("accuracy")
|
||||
? 0 : (float) posObj.getDouble("accuracy");
|
||||
|
||||
String posTime = posObj.isNull("positionTime")
|
||||
? "" : posObj.getString("positionTime");
|
||||
if(posTime.length() > 0)
|
||||
{
|
||||
//May 16, 2018 12:01:47
|
||||
try
|
||||
{
|
||||
String timeFormat = "MMMMM d, yyyy HH:mm:ss";
|
||||
DateFormat format = new SimpleDateFormat(timeFormat, Locale.ENGLISH);
|
||||
positionTime = format.parse(posTime);
|
||||
}
|
||||
catch (ParseException e)
|
||||
{
|
||||
String timeFormat = "MMM d, yyyy HH:mm:ss";
|
||||
DateFormat format = new SimpleDateFormat(timeFormat, Locale.ENGLISH);
|
||||
positionTime = format.parse(posTime);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
positionTime = new Date(0);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
positionTime = new Date(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Position(double latitude, double longitude)
|
||||
{
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
positionTime = new Date(0);
|
||||
speed = 0;
|
||||
address = "";
|
||||
accuracy = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor from Parcel, reads back fields IN THE ORDER they were
|
||||
* written
|
||||
*/
|
||||
public Position(Parcel parcel){
|
||||
latitude = parcel.readDouble();
|
||||
longitude = parcel.readDouble();
|
||||
positionTime = new Date(parcel.readLong());
|
||||
speed = parcel.readDouble();
|
||||
address = parcel.readString();
|
||||
accuracy = parcel.readFloat();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method from Parcelable class, left as auto-generated
|
||||
* @return A default value
|
||||
*/
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method to create the Parcel object from the main class that
|
||||
* will be passed when a Parcelable object is used
|
||||
* @param dest Parcel object in which all the object values will
|
||||
* be added
|
||||
* @param flags Additional flags about how the object should be
|
||||
* written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.
|
||||
*/
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeDouble(latitude);
|
||||
dest.writeDouble(longitude);
|
||||
dest.writeLong(positionTime.getTime());
|
||||
dest.writeDouble(speed);
|
||||
dest.writeString(address);
|
||||
dest.writeFloat(accuracy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static field used to regenerate object, individually or as arrays
|
||||
*/
|
||||
public static final Parcelable.Creator<Position> CREATOR = new Parcelable.Creator<Position>() {
|
||||
public Position createFromParcel(Parcel in) {
|
||||
return new Position(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position[] newArray(int size) {
|
||||
return new Position[size];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Override of the method that returns a string representation of the object which
|
||||
* contains all the useful information about the object
|
||||
* @return String representation of the object
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Latitude: " + latitude + " | "
|
||||
+ "Longitude: " + longitude + " | "
|
||||
+ "Speed: " + speed + " | "
|
||||
+ "PositionTime: " + (positionTime!= null ? positionTime.toString() : "null" ) + " | "
|
||||
+ "Address " + (address!= null ? address.toString() : "null" )
|
||||
+ "Accuracy " + accuracy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NonNull Position o) {
|
||||
return (latitude == o.latitude
|
||||
&& longitude == o.longitude
|
||||
) ? 0 : 1;
|
||||
}
|
||||
|
||||
// Overriding equals() to compare two Complex objects
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
Position pos = (Position) o;
|
||||
|
||||
return compareTo(pos) == 0;
|
||||
}
|
||||
|
||||
|
||||
// just omitted null checks
|
||||
@Override
|
||||
public int hashCode() {
|
||||
double res = latitude * longitude * 100000;
|
||||
int hash = (int)(latitude );
|
||||
hash = (int)(longitude) * hash;
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
|
||||
public String toJson()
|
||||
{
|
||||
return new Gson().toJson(this);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user