safedispatch-mobile/safeDispatch/src/main/java/com/safemobile/safedispatch/GoogleMapsInfoBubble.java

128 lines
4.6 KiB
Java
Raw Normal View History

2022-03-24 09:18:49 +00:00
package com.safemobile.safedispatch;
2022-03-16 08:54:41 +00:00
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;
2022-03-21 09:50:52 +00:00
import com.safemobile.lib.HistPos;
2022-03-16 08:54:41 +00:00
import com.safemobile.lib.SuperVehicle;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
2022-03-21 09:50:52 +00:00
import java.util.ArrayList;
2022-03-16 08:54:41 +00:00
import java.util.Date;
import java.util.Hashtable;
import java.util.Locale;
public class GoogleMapsInfoBubble implements GoogleMap.InfoWindowAdapter {
private final String TAG = GoogleMapsInfoBubble.class.getName();
private final View mWindow;
2022-03-21 09:50:52 +00:00
private Hashtable<Long, SuperVehicle> superVehHash;
2022-03-16 08:54:41 +00:00
private final Context context;
2022-03-21 09:50:52 +00:00
private ArrayList<HistPos> histPosList;
private boolean isLiveTab = true;
2022-03-16 08:54:41 +00:00
public GoogleMapsInfoBubble(LayoutInflater layoutInflater, Context context, Hashtable<Long, SuperVehicle> vehicles) {
this.context = context;
this.superVehHash = vehicles;
mWindow = layoutInflater.inflate(R.layout.map_marker_info_bubble, null);
}
2022-03-21 09:50:52 +00:00
public GoogleMapsInfoBubble(LayoutInflater layoutInflater, Context context) {
isLiveTab = false;
this.context = context;
mWindow = layoutInflater.inflate(R.layout.map_marker_info_bubble, null);
}
public void setHistoryPositions(ArrayList<HistPos> positions) {
this.histPosList = positions;
}
2022-03-16 08:54:41 +00:00
@Override
public View getInfoWindow(@NonNull Marker marker) {
render(marker, mWindow);
return mWindow;
}
@Override
public View getInfoContents(@NonNull Marker marker) {
render(marker, mWindow);
return mWindow;
}
private void render(Marker marker, View view) {
long key = 0;
2022-03-21 09:50:52 +00:00
int position = 0;
if (isLiveTab) {
try {
key = Long.parseLong(marker.getTitle());
} catch (Exception ex) {
Log.v(TAG, "Unable to parse Google Maps Info Bubble title");
}
} else {
try {
2022-04-07 10:31:23 +00:00
position = Integer.parseInt(marker.getTitle());
2022-03-21 09:50:52 +00:00
} catch (Exception e) {
Log.v(TAG, "Unable to parse Google Maps Info Bubble title on History");
}
2022-03-16 08:54:41 +00:00
}
2022-03-21 09:50:52 +00:00
2022-03-16 08:54:41 +00:00
RelativeLayout rlMapInfoBubbleInfo = view.findViewById(R.id.rlMapInfoBubbleInfo);
TextView tvUnitName = view.findViewById(R.id.tvUnitName);
TextView tvGPSLocation = view.findViewById(R.id.tvGPSLocation);
TextView tvTimeAgo = view.findViewById(R.id.tvTimeAgo);
TextView tvSpeed = view.findViewById(R.id.tvSpeed);
TextView tvStreetView = view.findViewById(R.id.tvStreetView);
ImageView streetView = view.findViewById(R.id.streetView);
rlMapInfoBubbleInfo.setVisibility(View.VISIBLE);
boolean isMilitaryTime = false;
2022-03-21 09:50:52 +00:00
Date positionTime;
String speed, address, gpsLocation, name = "";
if (isLiveTab) {
SuperVehicle vehicle = superVehHash.get(key);
positionTime = new Date(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(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) + "]";
}
2022-03-16 08:54:41 +00:00
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));
tvStreetView.setText(address);
streetView.setVisibility(address.length() > 0 ? View.VISIBLE : View.GONE);
tvStreetView.setVisibility(address.length() > 0 ? View.VISIBLE : View.GONE);
2022-03-21 09:50:52 +00:00
if (!isLiveTab)
tvUnitName.setVisibility(View.GONE);
else
tvUnitName.setText(name);
tvSpeed.setText(speed);
tvGPSLocation.setText(gpsLocation);
2022-03-16 08:54:41 +00:00
}
}