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

295 lines
11 KiB
Java
Raw Normal View History

2022-03-24 09:18:49 +00:00
package com.safemobile.safedispatch;
2022-03-14 09:53:00 +00:00
2022-03-18 13:03:34 +00:00
import androidx.annotation.NonNull;
2022-03-14 09:53:00 +00:00
import androidx.appcompat.app.AppCompatActivity;
2022-03-21 09:50:52 +00:00
import android.app.AlertDialog;
2022-03-21 09:50:52 +00:00
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
2022-03-14 09:53:00 +00:00
import android.os.Bundle;
2022-03-18 21:12:20 +00:00
import android.os.Handler;
import android.util.Log;
import android.view.View;
2022-03-17 08:18:23 +00:00
import android.widget.ArrayAdapter;
2022-03-18 21:12:20 +00:00
import android.widget.Button;
2022-03-17 08:18:23 +00:00
import android.widget.ImageView;
import android.widget.LinearLayout;
2022-03-17 08:18:23 +00:00
import android.widget.Spinner;
import android.widget.TextView;
2022-03-14 09:53:00 +00:00
2022-03-18 13:03:34 +00:00
import com.google.android.gms.maps.CameraUpdateFactory;
2022-03-17 08:18:23 +00:00
import com.google.android.gms.maps.GoogleMap;
2022-03-18 13:03:34 +00:00
import com.google.android.gms.maps.OnMapReadyCallback;
2022-03-17 08:18:23 +00:00
import com.google.android.gms.maps.SupportMapFragment;
2022-03-21 10:19:23 +00:00
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
2022-03-18 13:03:34 +00:00
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
2022-03-18 13:22:35 +00:00
import com.google.android.gms.maps.model.PolylineOptions;
2022-03-17 08:18:23 +00:00
import com.safemobile.lib.AppParams;
2022-03-18 13:22:35 +00:00
import com.safemobile.lib.HistPos;
2022-03-18 13:40:15 +00:00
import com.safemobile.lib.OperationCodes;
2022-03-14 09:53:00 +00:00
import com.safemobile.lib.SM;
2022-03-17 08:18:23 +00:00
import com.safemobile.lib.Vehicle;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
2022-03-14 09:53:00 +00:00
2022-03-18 13:03:34 +00:00
public class HistoryActivity extends AppCompatActivity implements OnMapReadyCallback {
2022-03-14 09:53:00 +00:00
public Bundle savedInstanceState;
2022-03-16 14:40:34 +00:00
private TabLayoutActivity parentTab;
2022-03-17 08:18:23 +00:00
private GoogleMap googleMap;
private Spinner spinnerVehicle;
2022-03-18 21:12:20 +00:00
private Button displayButton;
2022-03-18 13:40:15 +00:00
private Date startDate, endDate;
2022-03-18 21:12:20 +00:00
private final Handler myHandler = new Handler();
2022-03-21 09:50:52 +00:00
private GoogleMapsInfoBubble infoBubble;
2022-03-16 14:40:34 +00:00
2022-03-21 09:58:34 +00:00
private final ArrayList<Vehicle> allVehicle = new ArrayList<>();
private final ArrayList<String> allVehicleNames = new ArrayList<>();
private final SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd yyyy");
private boolean showVehicles = true;
2022-03-21 09:50:52 +00:00
2022-03-14 09:53:00 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
setContentView(R.layout.tabhistory);
2022-03-16 14:40:34 +00:00
parentTab = (TabLayoutActivity) getParent();
2022-03-24 10:55:53 +00:00
parentTab.setHistoryActivity(this);
2022-03-16 14:40:34 +00:00
2022-03-17 08:18:23 +00:00
Locale locale = new Locale(AppParams.LANGUAGETMP);
Locale.setDefault(locale);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
2022-03-21 09:58:34 +00:00
if (mapFragment != null)
mapFragment.getMapAsync(this);
2022-03-17 08:18:23 +00:00
spinnerVehicle = findViewById(R.id.spinnerVehicle);
getVehicles();
2022-03-21 09:58:34 +00:00
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, allVehicleNames);
2022-03-17 08:18:23 +00:00
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerVehicle.setAdapter(adapter);
spinnerVehicle.setSelection(0);
changeMapType();
changeTraffic();
2022-03-18 21:12:20 +00:00
displayButton = findViewById(R.id.buttonDisplay);
displayButton.setOnClickListener(view -> {
googleMap.clear();
if (!AppParams.DEMO) {
2022-03-24 10:55:53 +00:00
parentTab.setDropAllData(false);
parentTab.setFirstHistoryData(true);
parentTab.clearHistoryMessageList();
parentTab.clearHistoryPositionList();
2022-03-18 21:12:20 +00:00
// request history
parentTab.executeNetworkStuff(new String[] {OperationCodes.GetHistoryPositions + "", allVehicle.get(spinnerVehicle.getSelectedItemPosition()).sc_id + "",
(startDate.getTime()/ 1000L) + "", (endDate.getTime()/ 1000L) + ""});
} else {
parentTab.demoPositionsList();
2022-03-24 10:55:53 +00:00
displayHistory(parentTab.getDemoPositions());
2022-03-18 21:12:20 +00:00
}
});
2022-03-17 08:18:23 +00:00
2022-04-08 10:52:16 +00:00
ImageView mapType = findViewById(R.id.changeMapTypeHeader);
mapType.setOnClickListener(view -> {
if (googleMap.getMapType() != GoogleMap.MAP_TYPE_SATELLITE) {
mapType.setImageResource(R.drawable.map);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
mapType.setImageResource(R.drawable.satellite);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
LinearLayout slideLayout = findViewById(R.id.slidelayout);
LinearLayout linearLayoutVehicles = findViewById(R.id.layoutBig);
ImageView slideLayoutImageView = findViewById(R.id.slideLayoutImage);
slideLayout.setOnClickListener(v -> {
if (showVehicles) {
linearLayoutVehicles.setVisibility(View.GONE);
slideLayoutImageView.setImageResource(R.drawable.arrow_right);
showVehicles = false;
} else {
linearLayoutVehicles.setVisibility(View.VISIBLE);
slideLayoutImageView.setImageResource(R.drawable.arrow_left);
showVehicles = true;
}
});
2022-03-17 08:18:23 +00:00
setDate();
2022-03-18 13:22:35 +00:00
}
2022-04-20 11:40:12 +00:00
public void setLanguage() {
TextView labelVehicle = findViewById(R.id.labelVehicle);
TextView labelStartDate = findViewById(R.id.labelStartDate);
TextView labelEndDate = findViewById(R.id.labelEndDate);
labelVehicle.setText(R.string.vehicle);
labelStartDate.setText(R.string.startDate);
labelEndDate.setText(R.string.endDate);
displayButton.setText(R.string.display);
}
2022-03-18 21:12:20 +00:00
private void displayHistory(ArrayList<HistPos> positions) {
LatLng latLng = null;
2022-03-18 13:22:35 +00:00
PolylineOptions polylineOptions = new PolylineOptions();
2022-03-21 11:01:04 +00:00
BitmapDescriptor markerIcon = BitmapDescriptorFactory.fromResource(R.drawable.history_pin);
2022-03-21 09:50:52 +00:00
for (int i = 0; i < positions.size(); i++) {
HistPos pos = positions.get(i);
if (pos.speed > 0) {
latLng = new LatLng(pos.lat, pos.lng);
polylineOptions.add(latLng);
2022-03-21 11:01:04 +00:00
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(markerIcon)
.title(String.valueOf(i)));
2022-03-21 09:50:52 +00:00
}
2022-03-18 13:22:35 +00:00
}
2022-03-21 09:58:34 +00:00
googleMap.addPolyline(polylineOptions);
2022-03-18 21:12:20 +00:00
if (latLng != null)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14));
2022-03-17 08:18:23 +00:00
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.exit))
.setCancelable(false)
.setNeutralButton(getString(R.string.logout), (dialog, id) -> parentTab.whenBackPressed(AppParams.ActivityResult.logout))
.setPositiveButton(getString(R.string.ext), (dialog, id) -> parentTab.whenBackPressed(AppParams.ActivityResult.exit))
.setNegativeButton(getString(R.string.cancel), (dialog, id) -> dialog.cancel());
AlertDialog alert = builder.create();
alert.show();
}
2022-03-17 08:18:23 +00:00
private void setDate() {
Calendar calendar = Calendar.getInstance();
2022-04-18 10:55:35 +00:00
calendar.add(Calendar.DATE, 1);
2022-03-18 13:40:15 +00:00
endDate = calendar.getTime();
2022-04-18 10:55:35 +00:00
calendar.add(Calendar.DATE, -2);
2022-03-18 13:40:15 +00:00
startDate = calendar.getTime();
2022-03-17 08:18:23 +00:00
2022-03-21 09:50:52 +00:00
TextView textViewStartDate = findViewById(R.id.textViewStartDate);
TextView textViewEndDate = findViewById(R.id.textViewEndDate);
2022-03-17 08:18:23 +00:00
2022-03-21 09:50:52 +00:00
textViewStartDate.setText(sdf.format(startDate));
textViewEndDate.setText(sdf.format(endDate));
2022-03-17 08:18:23 +00:00
2022-03-21 09:50:52 +00:00
textViewStartDate.setOnClickListener(view -> openDialog(textViewStartDate, true));
textViewEndDate.setOnClickListener(view -> openDialog(textViewEndDate, false));
}
2022-03-17 08:18:23 +00:00
2022-03-21 09:50:52 +00:00
private void openDialog(TextView textView, Boolean isStartDate) {
Date date = startDate;
if (!isStartDate)
date = endDate;
2022-03-17 08:18:23 +00:00
2022-03-21 09:50:52 +00:00
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
DatePickerDialog.OnDateSetListener setListener = (datePicker, y, m, d) -> {
calendar.set(y, m, d);
String dateString = sdf.format(calendar.getTime());
textView.setText(dateString);
if (isStartDate)
startDate = calendar.getTime();
else
endDate = calendar.getTime();
};
Dialog dialog = new DatePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, setListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
2022-03-17 08:18:23 +00:00
}
private void changeTraffic() {
ImageView changeTrafficImageView = findViewById(R.id.changeTraffic);
changeTrafficImageView.setOnClickListener(v -> {
if (googleMap.isTrafficEnabled()) {
changeTrafficImageView.setImageResource(R.drawable.traffic_off);
googleMap.setTrafficEnabled(false);
} else {
changeTrafficImageView.setImageResource(R.drawable.traffic);
googleMap.setTrafficEnabled(true);
}
});
}
private void changeMapType() {
2022-03-21 09:58:34 +00:00
ImageView changeMapType = findViewById(R.id.changeMapType);
2022-03-17 08:18:23 +00:00
changeMapType.setOnClickListener(view -> {
if (googleMap.getMapType() == GoogleMap.MAP_TYPE_SATELLITE) {
2022-03-21 09:58:34 +00:00
changeMapType.setImageResource(R.drawable.satellite);
2022-03-17 08:18:23 +00:00
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
2022-03-21 09:58:34 +00:00
changeMapType.setImageResource(R.drawable.map);
2022-03-17 08:18:23 +00:00
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
}
public void getVehicles() {
try {
2022-03-21 09:58:34 +00:00
allVehicle.clear();
allVehicleNames.clear();
2022-03-17 08:18:23 +00:00
for (Vehicle vehicle : parentTab.getAllVehicle()) {
allVehicleNames.add(vehicle.name);
allVehicle.add(vehicle);
}
2022-03-21 09:58:34 +00:00
} catch (Exception ignored) { }
2022-03-17 08:18:23 +00:00
2022-03-14 09:53:00 +00:00
}
2022-03-18 21:12:20 +00:00
final Runnable UpdateMapResults = new Runnable() {
public void run() {
2022-03-24 10:55:53 +00:00
Log.v("updateMap", parentTab.getHistoryPositionList().toString());
2022-03-18 21:12:20 +00:00
SM.Debug("Do the Display");
2022-03-24 10:55:53 +00:00
infoBubble.setHistoryPositions(parentTab.getHistoryPositionList());
displayHistory(parentTab.getHistoryPositionList());
2022-03-18 21:12:20 +00:00
}
};
2022-03-14 09:53:00 +00:00
public void UpdateMap() {
2022-03-18 21:12:20 +00:00
myHandler.post(UpdateMapResults);
2022-03-14 09:53:00 +00:00
SM.Debug("Do the updateMAP post");
}
public void UpdateUnableDisp() {
SM.Debug("Do Cancelwindow");
}
public void UpdateCancel() {
SM.Debug("Do Cancelwindow");
}
public void UpdateNrPos(int size) {
SM.Debug("Do Cancelwindow");
}
2022-03-18 13:03:34 +00:00
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
this.googleMap = googleMap;
2022-03-21 09:50:52 +00:00
infoBubble = new GoogleMapsInfoBubble(getLayoutInflater(), this);
this.googleMap.setInfoWindowAdapter(infoBubble);
2022-04-08 13:39:53 +00:00
this.googleMap.getUiSettings().setMapToolbarEnabled(false);
2022-03-21 09:50:52 +00:00
2022-03-18 13:22:35 +00:00
parentTab.demoPositionsList();
2022-03-18 13:03:34 +00:00
}
2022-03-14 09:53:00 +00:00
}