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

266 lines
9.8 KiB
Java

package com.safemobile.safedispatch;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.safemobile.lib.AppParams;
import com.safemobile.lib.HistPos;
import com.safemobile.lib.OperationCodes;
import com.safemobile.lib.SM;
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;
public class HistoryActivity extends AppCompatActivity implements OnMapReadyCallback {
public Bundle savedInstanceState;
private TabLayoutActivity parentTab;
private GoogleMap googleMap;
private Spinner spinnerVehicle;
private Button displayButton;
private Date startDate, endDate;
private final Handler myHandler = new Handler();
private GoogleMapsInfoBubble infoBubble;
private final ArrayList<Vehicle> allVehicle = new ArrayList<>();
private final ArrayList<String> allVehicleNames = new ArrayList<>();
private final SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd yyyy");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
setContentView(R.layout.tabhistory);
parentTab = (TabLayoutActivity) getParent();
parentTab.setHistoryActivity(this);
Locale locale = new Locale(AppParams.LANGUAGETMP);
Locale.setDefault(locale);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null)
mapFragment.getMapAsync(this);
spinnerVehicle = findViewById(R.id.spinnerVehicle);
getVehicles();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, allVehicleNames);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerVehicle.setAdapter(adapter);
spinnerVehicle.setSelection(0);
changeMapType();
changeTraffic();
displayButton = findViewById(R.id.buttonDisplay);
displayButton.setOnClickListener(view -> {
googleMap.clear();
if (!AppParams.DEMO) {
parentTab.setDropAllData(false);
parentTab.setFirstHistoryData(true);
parentTab.clearHistoryMessageList();
parentTab.clearHistoryPositionList();
// request history
parentTab.executeNetworkStuff(new String[] {OperationCodes.GetHistoryPositions + "", allVehicle.get(spinnerVehicle.getSelectedItemPosition()).sc_id + "",
(startDate.getTime()/ 1000L) + "", (endDate.getTime()/ 1000L) + ""});
} else {
parentTab.demoPositionsList();
displayHistory(parentTab.getDemoPositions());
}
});
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);
}
});
setDate();
}
private void displayHistory(ArrayList<HistPos> positions) {
LatLng latLng = null;
PolylineOptions polylineOptions = new PolylineOptions();
BitmapDescriptor markerIcon = BitmapDescriptorFactory.fromResource(R.drawable.history_pin);
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);
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(markerIcon)
.title(String.valueOf(i)));
}
}
googleMap.addPolyline(polylineOptions);
if (latLng != null)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14));
}
@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();
}
private void setDate() {
Calendar calendar = Calendar.getInstance();
endDate = calendar.getTime();
calendar.add(Calendar.DATE, -1);
startDate = calendar.getTime();
TextView textViewStartDate = findViewById(R.id.textViewStartDate);
TextView textViewEndDate = findViewById(R.id.textViewEndDate);
textViewStartDate.setText(sdf.format(startDate));
textViewEndDate.setText(sdf.format(endDate));
textViewStartDate.setOnClickListener(view -> openDialog(textViewStartDate, true));
textViewEndDate.setOnClickListener(view -> openDialog(textViewEndDate, false));
}
private void openDialog(TextView textView, Boolean isStartDate) {
Date date = startDate;
if (!isStartDate)
date = endDate;
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();
}
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() {
ImageView changeMapType = findViewById(R.id.changeMapType);
changeMapType.setOnClickListener(view -> {
if (googleMap.getMapType() == GoogleMap.MAP_TYPE_SATELLITE) {
changeMapType.setImageResource(R.drawable.satellite);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
changeMapType.setImageResource(R.drawable.map);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
}
public void getVehicles() {
try {
allVehicle.clear();
allVehicleNames.clear();
for (Vehicle vehicle : parentTab.getAllVehicle()) {
allVehicleNames.add(vehicle.name);
allVehicle.add(vehicle);
}
} catch (Exception ignored) { }
}
final Runnable UpdateMapResults = new Runnable() {
public void run() {
Log.v("updateMap", parentTab.getHistoryPositionList().toString());
SM.Debug("Do the Display");
infoBubble.setHistoryPositions(parentTab.getHistoryPositionList());
displayHistory(parentTab.getHistoryPositionList());
}
};
public void UpdateMap() {
myHandler.post(UpdateMapResults);
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");
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
this.googleMap = googleMap;
infoBubble = new GoogleMapsInfoBubble(getLayoutInflater(), this);
this.googleMap.setInfoWindowAdapter(infoBubble);
this.googleMap.getUiSettings().setMapToolbarEnabled(false);
parentTab.demoPositionsList();
displayButton.performClick();
}
}