-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteSelectActivity.java
601 lines (544 loc) · 24.9 KB
/
RouteSelectActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package com.github.gidabotapp.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import com.github.gidabotapp.R;
import com.github.gidabotapp.data.AppNavPhase;
import com.github.gidabotapp.domain.Floor;
import com.github.gidabotapp.domain.MapPosition;
import com.github.gidabotapp.domain.Room;
import com.github.gidabotapp.domain.Way;
import com.github.gidabotapp.viewmodel.MapViewModel;
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.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Tile;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
public class RouteSelectActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap map;
private HashMap<Floor, TileOverlay> tileOverlays;
private HashMap<Floor, Marker> robotMarkers;
private Marker markerOrigin, markerDest;
private MapViewModel viewModel;
private Button publishBtn, cancelBtn;
private AutoCompleteTextView act_origin, act_destination, act_floor;
public RouteSelectActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_select);
viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(MapViewModel.class);
viewModel.getToastLD().observe(this, new Observer<String>() {
@Override
public void onChanged(String message) {
View bottom = findViewById(R.id.mapFragment);
final Snackbar snackbar = Snackbar.make(bottom,message,Snackbar.LENGTH_INDEFINITE);
snackbar.setActionTextColor(ContextCompat.getColor(getApplicationContext(), R.color.primaryTextColor));
snackbar.setAction(R.string.accept_btn, new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
snackbar.show();
}
});
viewModel.getAlertLD().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer stringResId) {
if(stringResId == R.string.empty){
return;
}
if(stringResId == R.string.origin_reached_msg){
showNextGoalAlert();
}
else if(stringResId == R.string.destination_reached_msg){
showRouteEndAlert();
}
else if(stringResId == R.string.WAIT_ROBOT){
if(viewModel.emptyRoute()){
return;
}
int floorCode = viewModel.getCurrentGoalFloor().getFloorCode();
int pendingRequests = viewModel.getGoalFloorPending();
String message = String.format(getString(stringResId), floorCode, pendingRequests);
showAlert(message);
}
else {
String message = getString(stringResId);
showAlert(message);
}
}
});
for(final Floor floor: Floor.values()){
viewModel.getPositionLD(floor).observe(this, new Observer<MapPosition>() {
@Override
public void onChanged(MapPosition position) {
updateRobotMarker(floor, position);
}
});
}
viewModel.getAllRoomsLD().observe(this, new Observer<List<Room>>() {
@Override
public void onChanged(List<Room> rooms) {
ArrayAdapter<Room> adapterAllRooms = new ArrayAdapter<>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, rooms);
act_destination.setAdapter(adapterAllRooms);
}
});
viewModel.getAppNavPhaseLD().observe(this, new Observer<AppNavPhase>() {
@Override
public void onChanged(AppNavPhase phase) {
updateButtonsLock(phase);
}
});
publishBtn = findViewById(R.id.publishBtn);
publishBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewModel.isLiftNeeded()){
showLiftAlert();
return;
}
viewModel.publishOrigin(null);
}
});
cancelBtn = findViewById(R.id.cancelBtn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewModel.publishCancel();
}
});
FloatingActionButton locateRobotBtn = findViewById(R.id.locateRobotBtn);
locateRobotBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCameraOnRobot();
}
});
act_origin = findViewById(R.id.act_non);
act_origin.setThreshold(1);
act_origin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Hide Keyboard
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
Room origin = (Room) parent.getItemAtPosition(position);
viewModel.selectOrigin(origin);
updateOriginMarker(origin);
}
});
act_destination = findViewById(R.id.act_nora);
act_destination.setThreshold(1);
act_destination.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Hide Keyboard
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
Room destination = (Room) parent.getItemAtPosition(position);
viewModel.selectDestination(destination);
updateDestMarker(destination);
}
});
act_floor = findViewById(R.id.act_floor);
final ArrayAdapter<String> floorAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, Floor.getFloorCodeList());
act_floor.setAdapter(floorAdapter);
act_floor.setText(floorAdapter.getItem(0),false);
act_floor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Floor currentFloor = Floor.values()[position];
viewModel.selectFloor(currentFloor);
if(markerOrigin != null){
markerOrigin.setVisible(false);
}
if(markerDest != null){
boolean visible = viewModel.destOnCurrentFloor();
markerDest.setVisible(visible);
}
}
});
viewModel.getCurrentFloorRoomsLD().observe(this, new Observer<List<Room>>() {
@Override
public void onChanged(List<Room> rooms) {
ArrayAdapter<Room> adapterFloorRooms = new ArrayAdapter<>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, rooms);
act_origin.setAdapter(adapterFloorRooms);
act_origin.setText("");
viewModel.selectOrigin(null);
}
});
viewModel.getCurrentFloorLD().observe(this, new Observer<Floor>() {
@Override
public void onChanged(Floor floor) {
showTiles(floor);
showRobotMarker(floor);
}
});
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
assert mapFragment != null;
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
final int MAX_MAP_ZOOM = 3;
this.map = map;
map.setMapType(GoogleMap.MAP_TYPE_NONE);
map.setMaxZoomPreference(MAX_MAP_ZOOM);
// Set camera bounds: Horizontal scroll ends with map
LatLng SOUTHWEST_BOUND = new LatLng(-65,-110);
LatLng NORTHEAST_BOUND = new LatLng(+65,+110);
LatLngBounds bounds = new LatLngBounds(SOUTHWEST_BOUND,NORTHEAST_BOUND);
map.setLatLngBoundsForCameraTarget(bounds);
// Set custom onClickListener for all markers, so that it only shows Marker's information (if it has any),
// and hides Google's default buttons, which we don't want.
final Marker[] lastOpened = {null};
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// Check if there is an open info window
if (lastOpened[0] != null) {
// Close the info window
lastOpened[0].hideInfoWindow();
// Is the marker the same marker that was already open
if (lastOpened[0].equals(marker)) {
// Nullify the lastOpened object
lastOpened[0] = null;
// Return so that the info window isn't opened again
return true;
}
}
// Open the info window for the marker
marker.showInfoWindow();
// Re-assign the last opened such that we can close it later
lastOpened[0] = marker;
// Event was handled by our code, so do not launch default behaviour.
return true;
}
});
// Initialize robot marker on starting floor
showRobotMarker(Floor.getStartingFloor());
// Initialize tiles on starting floor
showTiles(Floor.getStartingFloor());
}
@Override
public void onDestroy() {
super.onDestroy();
// Remove all (Mutable)LiveData observers
viewModel.getCurrentFloorLD().removeObservers(this);
viewModel.getToastLD().removeObservers(this);
viewModel.getAlertLD().removeObservers(this);
for(Floor floor: Floor.values()){
viewModel.getPositionLD(floor).removeObservers(this);
}
viewModel.getCurrentFloorRoomsLD().removeObservers(this);
// Shut down robot query Node (qNode)
viewModel.shutdownNode();
}
// Update origin's marker position.
// Origin is always on current floor, so always sets it visible.
private void updateOriginMarker(Room origin) {
Floor currentFloor = viewModel.getCurrentFloorLD().getValue();
assert currentFloor != null;
LatLng position = origin.getPosition().toLatLng(currentFloor);
// If null, instantiate it with proper icon
if(markerOrigin == null){
BitmapDescriptor icon = bitmapDescriptorFromVector(this, R.drawable.ic_origin);
markerOrigin = map.addMarker(new MarkerOptions()
.icon(icon)
.position(position)
);
}
else { // Already instantiated, so just update position and set it visible.
markerOrigin.setPosition(position);
markerOrigin.setVisible(true);
}
}
// Updates destination's marker.
// Destination is not always on current floor, so depending on that set it visible or not.
private void updateDestMarker(Room destination) {
Floor destFloor = Floor.getFromDouble(destination.getFloor());
LatLng position = destination.getPosition().toLatLng(destFloor);
// If null, instantiate it with proper icon
if(markerDest == null){
BitmapDescriptor icon = bitmapDescriptorFromVector(this, R.drawable.ic_destination);
markerDest = map.addMarker(new MarkerOptions()
.icon(icon)
.position(position)
.visible(true)
);
}
else { // Already instantiated, so update position
markerDest.setPosition(position);
}
// if currentFloor = destinationFloor then set it visible
boolean visible = viewModel.destOnCurrentFloor(); // true if dest.floor = currentFloor
markerDest.setVisible(visible);
}
// Temporarily disabled, it generates quite a lot of bugs
private void updateButtonsLock(AppNavPhase phase) {
// if (phase == AppNavPhase.WAITING_USER_INPUT){
// act_origin.setEnabled(true);
// act_destination.setEnabled(true);
// act_floor.setEnabled(true);
// publishBtn.setEnabled(true);
// cancelBtn.setEnabled(false);
// }
// else {
// act_origin.setEnabled(false);
// act_destination.setEnabled(false);
// act_floor.setEnabled(false);
// publishBtn.setEnabled(false);
// cancelBtn.setEnabled(true);
// }
}
// Shows standard alert. One accept button that dismisses the alert
private void showAlert(String msg) {
MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.alert_title))
.setMessage(msg)
.setPositiveButton(R.string.accept_btn, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
// Shows next goal alert. Two buttons that decide whether the route continues or stops.
private void showNextGoalAlert(){
String message = String.format(getString(R.string.origin_reached_msg),viewModel.getDestination());
MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(this)
.setTitle(R.string.alert_title)
.setMessage(message)
// Accept button --> publish destination
.setPositiveButton(R.string.accept_btn, new DialogInterface.OnClickListener() { // Ados
@Override
public void onClick(DialogInterface dialog, int which) {
// If it is a multilevel request, show lift alert, which will publish destination depending on the choice
if(viewModel.isLiftNeeded()){
showLiftAlert();
return;
}
// Otherwise it is a single floor request, so publish destination with Null Way
viewModel.publishDestination(null);
}
})
// Cancel button --> reset appNavPhase (phase == WAIT_USER_INPUT) and dismiss alert
.setNegativeButton(R.string.cancel_btn, new DialogInterface.OnClickListener() { // Ezeztatu
@Override
public void onClick(DialogInterface dialog, int which) {
viewModel.resetAppNavPhase();
dialog.dismiss();
}
})
// Cancel event (tap out) --> reset appNavPhase (phase == WAIT_USER_INPUT)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
viewModel.resetAppNavPhase();
}
});
dialog.show();
}
// Show end route alert and reset AppNavPhase
private void showRouteEndAlert() {
String message = getString(R.string.destination_reached_msg); // Iritsi zara zure helburura
MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(this)
.setTitle(R.string.alert_title) // Informazioa
.setMessage(message)
.setPositiveButton(R.string.accept_btn, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
viewModel.resetAppNavPhase();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
viewModel.resetAppNavPhase();
}
});
dialog.show();
}
// Shows a dialog to choose between Lift and Stairs
private void showLiftAlert(){
String lift = getString(Way.LIFT.getResourceId());
String stairs = getString(Way.STAIRS.getResourceId());
String[] values = new String[]{lift, stairs};
MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(this)
.setTitle(R.string.lift_stairs_question)
.setItems(values, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Way chosenWay = Way.values()[which];
// if road has not started, current goal is origin
if(viewModel.getAppNavPhaseLD().getValue() == AppNavPhase.WAITING_USER_INPUT){
viewModel.publishOrigin(chosenWay);
}
// if road has already started, current goal is destination
if(viewModel.getAppNavPhaseLD().getValue() == AppNavPhase.REACHING_ORIGIN){
viewModel.publishDestination(chosenWay);
}
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
viewModel.resetAppNavPhase();
}
})
;
dialog.show();
}
// Centers camera on current floor's robot
private void setCameraOnRobot() {
Floor currentFloor = viewModel.getCurrentFloorLD().getValue();
map.animateCamera(CameraUpdateFactory.newLatLng(robotMarkers.get(currentFloor).getPosition()));
}
// Shows tiles in current floor. If tiles hashmap is null, instantiate it with all floors' tiles.
private void showTiles(Floor currentFloor) {
if (map == null){
return;
}
if(tileOverlays == null) {
tileOverlays = initTileOverlays();
}
for(Floor floor: tileOverlays.keySet()){
if(floor == currentFloor){
tileOverlays.get(floor).setVisible(true);
continue;
}
tileOverlays.get(floor).setVisible(false);
}
}
// Shows marker of robot in given floor. If markers hashmap is null, instantiate it with all robot's markers.
private void showRobotMarker(Floor currentFloor) {
if(map == null){
return;
}
if(robotMarkers == null){
robotMarkers = initRobotMarkers();
}
for(Floor floor : robotMarkers.keySet()){
if(floor == currentFloor){
robotMarkers.get(floor).setVisible(true);
robotMarkers.get(floor).showInfoWindow();
continue;
}
robotMarkers.get(floor).setVisible(false);
}
}
// Updates marker of robot in given floor. If markers hashmap is null, instantiate it with all robot's markers.
private void updateRobotMarker(Floor currentFloor, MapPosition position) {
if(map == null){
return;
}
if(robotMarkers == null){
robotMarkers = initRobotMarkers();
}
LatLng currentLatLng = position.toLatLng(currentFloor);
robotMarkers.get(currentFloor).setPosition(currentLatLng);
}
// Instantiate Robot Markers' hashmap with all robot icons
private HashMap<Floor,Marker> initRobotMarkers() {
return new HashMap<Floor, Marker>(){{
for(Floor floor: Floor.values()){
Marker marker = map.addMarker(new MarkerOptions()
.title(floor.getRobotNameLong())
.icon(BitmapDescriptorFactory.fromResource(floor.getRobotIconRes()))
.position(floor.getStartLatLng())
.zIndex(1.0f)
);
put(floor,marker);
}
}};
}
// Gets a TileProvider for given floor.
// Tiles are loaded from assets/map_tiles
private TileProvider getFloorTileProvider(final Floor currentFloor){
TileProvider floorTileProvider;
floorTileProvider = new TileProvider() {
final String FLOOR_MAP_URL_FORMAT =
"map_tiles/floor_%d/%d/tile_%d_%d.png";
final int TILE_SIZE_DP = 256;
@Override
public Tile getTile(int x, int y, int zoom) {
String s = String.format(Locale.US, FLOOR_MAP_URL_FORMAT, currentFloor.getFloorCode(), zoom, x, y);
try {
InputStream is = getApplication().getAssets().open(s);
Bitmap bitmap = BitmapFactory.decodeStream(is);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return new Tile(TILE_SIZE_DP, TILE_SIZE_DP, stream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
return floorTileProvider;
}
// Initializes Tile Overlays hashmap.
private HashMap<Floor,TileOverlay> initTileOverlays(){
return new HashMap<Floor, TileOverlay>(){
{
for (Floor floor : Floor.values()) {
// Take a tileProvider for current floor
TileProvider provider = getFloorTileProvider(floor);
// add TileOverlay to map with generated TileProvider
TileOverlay overlay = map.addTileOverlay(new TileOverlayOptions().tileProvider(provider));
// Put tileOverlay into hashmap
put(floor, overlay);
}
}};
}
// Provides a bitmapDescriptor for given drawable resource.
// Needed to draw Origin and Destination markers from vector assets.
private BitmapDescriptor bitmapDescriptorFromVector(Context context, int vectorResId) {
Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
assert vectorDrawable != null;
final int WIDTH = vectorDrawable.getIntrinsicWidth() * 2;
final int HEIGHT = vectorDrawable.getIntrinsicHeight() * 2;
vectorDrawable.setBounds(0, 0, WIDTH, HEIGHT);
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
}