From 5c8b85766440baf1554b0e074a5d2f4a24436aee Mon Sep 17 00:00:00 2001 From: Laurentiu Date: Fri, 26 May 2023 17:40:07 +0300 Subject: [PATCH] format timestamp as time ago --- .../com/safemobile/services/TCPService.java | 26 +-------- .../safedispatch/GoogleMapsInfoBubble.java | 58 ++++++++++++++++--- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java b/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java index eb83182..e19c89d 100644 --- a/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java +++ b/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java @@ -16,9 +16,9 @@ import android.widget.Toast; public class TCPService extends Service { private int[] startModes = {START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT}; - private int mStartMode = startModes[0]; // indicates how to behave if the service is killed + private int mStartMode = startModes[0]; // indicates how to behave if the service is killed private IBinder mBinder = new TCPBinder(); // interface for clients that bind - private boolean mAllowRebind = true; // indicates whether onRebind should be used + private boolean mAllowRebind = true; // indicates whether onRebind should be used private TCPhandler tcp = null; private TCPmsgParser tcpParser = null; @@ -114,27 +114,7 @@ public class TCPService extends Service { /** restart the TCP Connection after the connection parameters had been changed */ public void recreateTCPConnection() { - /* - Thread t = new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(1000*2); - } catch (InterruptedException e) { - e.printStackTrace(); - } - */ - // create a tcp connection - int port = 13589; - try { - port = Integer.parseInt(AppParams.PORT); - } - catch(Exception ex) { } - tcp = new TCPhandler(getApplicationContext(), AppParams.IP, port); - /* } - }); - t.start(); - */ + recreateTCPConnection(AppParams.IP, AppParams.PORT); } public void recreateTCPConnection(String _ip, String _port) diff --git a/safeDispatch/src/main/java/com/safemobile/safedispatch/GoogleMapsInfoBubble.java b/safeDispatch/src/main/java/com/safemobile/safedispatch/GoogleMapsInfoBubble.java index ab19f8b..3384f86 100644 --- a/safeDispatch/src/main/java/com/safemobile/safedispatch/GoogleMapsInfoBubble.java +++ b/safeDispatch/src/main/java/com/safemobile/safedispatch/GoogleMapsInfoBubble.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.Hashtable; import java.util.Locale; +import java.util.concurrent.TimeUnit; public class GoogleMapsInfoBubble implements GoogleMap.InfoWindowAdapter { @@ -90,28 +91,38 @@ public class GoogleMapsInfoBubble implements GoogleMap.InfoWindowAdapter { boolean isMilitaryTime = false; Date positionTime; + long timeGMT = 0; String speed, address, gpsLocation, name = ""; if (isLiveTab) { SuperVehicle vehicle = superVehHash.get(key); - positionTime = new Date((new Date()).getTime() - vehicle.timeGMT); + + //positionTime = new Date((new Date()).getTime() - vehicle.timeGMT); + + timeGMT = vehicle.timeGMT; address = vehicle.Address != null ? vehicle.Address : ""; name = vehicle.name; speed = String.format(context.getResources().getString(R.string.speedMph), vehicle.speed); gpsLocation = "[" + String.format("%.4f", vehicle.lat) + "," + String.format("%.4f",vehicle.lng) + "]"; } else { HistPos histPos = histPosList.get(position); - positionTime = new Date((new Date()).getTime() - histPos.timeGMT); + + //positionTime = new Date((new Date()).getTime() - histPos.timeGMT); + timeGMT = histPos.timeGMT; address = histPos.Address != null ? histPos.Address : ""; speed = String.format(context.getResources().getString(R.string.speedMph), histPos.speed); gpsLocation = "[" + String.format("%.4f", histPos.lat) + "," + String.format("%.4f",histPos.lng) + "]"; } - String timeFormat = isMilitaryTime - ? "HH:mm:ss dd.MMM.yyyy" - : "hh:mm:ss a dd.MMM.yyy"; + //String timeFormat = isMilitaryTime + // ? "HH:mm:ss dd.MMM.yyyy" + // : "hh:mm:ss a dd.MMM.yyy"; + + //DateFormat format = new SimpleDateFormat(timeFormat, Locale.ENGLISH); + //tvTimeAgo.setText(format.format(positionTime)); + + DateFormat format = new SimpleDateFormat("hh:mm:ss", Locale.ENGLISH); + tvTimeAgo.setText( getDateTimeAgo(timeGMT * 1000) + " [" + format.format(new Date(timeGMT*1000) ) + "]"); - DateFormat format = new SimpleDateFormat(timeFormat, Locale.ENGLISH); - tvTimeAgo.setText(format.format(positionTime)); tvStreetView.setText(address); streetView.setVisibility(address.length() > 0 ? View.VISIBLE : View.GONE); tvStreetView.setVisibility(address.length() > 0 ? View.VISIBLE : View.GONE); @@ -123,5 +134,38 @@ public class GoogleMapsInfoBubble implements GoogleMap.InfoWindowAdapter { tvSpeed.setText(speed); tvGPSLocation.setText(gpsLocation); } + + /// + /// Get a text representation for the date time representing + /// the difference in minutes/hours/days from now + /// + /// DateTime value which needs to be converted + /// String 'Ago' representation fot he date + public static String getDateTimeAgo(long time) + { + String timeValue = ""; + + long diffInMs = (new Date().getTime() - time); + + long secondsDifference = TimeUnit.MILLISECONDS.toSeconds(diffInMs);; + + if (secondsDifference < 60) + timeValue = "less than 1 minute"; + else if (secondsDifference < 3600) + timeValue = (secondsDifference / 60) + " minute" + ((secondsDifference / 60) > 1 ? "s" : "") + " ago"; + else if (secondsDifference < 7200) + timeValue = "1 hour ago"; + else if (secondsDifference < 86400) + { + timeValue = (secondsDifference / 3600) + " hours ago"; + } + else + { + timeValue = (secondsDifference / 86400) + " days ago"; + } + + return timeValue; + } + }