Skip to content

Commit 995717d

Browse files
committed
Updated RealTime, added some functionalities
1 parent 4688f70 commit 995717d

File tree

7 files changed

+88
-47
lines changed

7 files changed

+88
-47
lines changed

app/src/main/java/kevin/carrismobile/fragments/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ protected void onCreate(Bundle savedInstanceState) {
7070
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
7171
transaction.add(R.id.main_layout, routesFragment).commit();
7272
mapper.put(routesFragment, 2);
73+
initFragment(R.id.main_layout, settingsFragment, 2);
7374
initFragment(R.id.main_layout, routeDetailsFragment, 2);
7475
initFragment(R.id.main_layout, realTimeFragment, 4);
7576
initFragment(R.id.main_layout, stopsMapFragment, 3);
7677
initFragment(R.id.main_layout, stopDetailsFragment, 2);
7778
initFragment(R.id.main_layout, stopFavoritesFragment, 1);
7879
initFragment(R.id.main_layout, routeFavoritesFragment, 1);
7980
initFragment(R.id.main_layout, moovitFragment, 0);
80-
initFragment(R.id.main_layout, settingsFragment, 2);
8181
currentIndexFragment = 2;
8282
currentFragment = routesFragment;
8383
oldFragmentsList.add(routesFragment);

app/src/main/java/kevin/carrismobile/fragments/RealTimeFragment.java

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public class RealTimeFragment extends Fragment {
5858
public static MapView map;
5959
public static Activity activity;
6060
Button button;
61-
Button nextButton;
61+
public static Button nextButton;
6262
public static TextView textView;
63-
Button previousButton;
63+
public static Button previousButton;
6464
EditText editText;
6565
public static CheckBox checkBox;
6666
public BusBackgroundThread backgroundThread = new BusBackgroundThread();
@@ -94,9 +94,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
9494
checkBox = v.findViewById(R.id.checkBox);
9595
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
9696

97-
9897
map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
99-
map.setTileSource(TileSourceFactory.OpenTopo);
98+
map.setTileSource(SettingsFragment.getCurrentTileProvider());
10099

101100
map.setMultiTouchControls(true);
102101
map.getController().setCenter(new GeoPoint(38.73329737648646d, -9.14096412687648d));
@@ -181,7 +180,7 @@ public void run() {
181180
@Override
182181
public void run() {
183182
updateMarkers(pathList, map);
184-
updateBuses(busList, map, getActivity());
183+
updateBusesUI();
185184
updateTextView();
186185
updateDirectionIndex();
187186
Log.println(Log.DEBUG, "BUS DEBUG", "GUI UPDATED");
@@ -214,6 +213,7 @@ public void onClick(View view) {
214213
currentSelectedBus ++;
215214
updateTextView();
216215
updateDirectionIndex();
216+
updateBusesUI();
217217
updateMarkers(currentCarreira.getDirectionList().get(currentDirectionIndex).getPathList(), map);
218218
Log.println(Log.DEBUG, "Button", "Current Bus: " + currentSelectedBus);
219219
getActivity().runOnUiThread(new Runnable() {
@@ -222,7 +222,9 @@ public void onClick(View view) {
222222
public void run() {
223223
//textView.setText((currentSelectedBus + 1) + "/" + busList.size() + "\n" + busList.get(currentSelectedBus).getStatus());
224224
GeoPoint point = markerBusList.get(currentSelectedBus).getPosition();
225-
map.getController().animateTo(point, 16.5, 2500L);
225+
if (checkBox.isChecked()){
226+
map.getController().animateTo(point, 16.5, 2500L);
227+
}
226228
map.invalidate();
227229
}
228230
});
@@ -242,13 +244,16 @@ public void onClick(View view) {
242244
currentSelectedBus --;
243245
updateTextView();
244246
updateDirectionIndex();
247+
updateBusesUI();
245248
updateMarkers(currentCarreira.getDirectionList().get(currentDirectionIndex).getPathList(), map);
246249
Log.println(Log.DEBUG, "Button", "Current Bus: " + currentSelectedBus);
247250
getActivity().runOnUiThread(new Runnable() {
248251
@Override
249252
public void run() {
250253
GeoPoint point = markerBusList.get(currentSelectedBus).getPosition();
251-
map.getController().animateTo(point, 16.5, 2500L);
254+
if (checkBox.isChecked()){
255+
map.getController().animateTo(point, 16.5, 2500L);
256+
}
252257
map.invalidate();
253258
}
254259
});
@@ -307,6 +312,7 @@ private void updateMarkers(List<Path> pathList, MapView map){
307312
int resultBlue = Integer.valueOf(hexCode.substring(4, 6), 16);
308313
line.setColor(Color.rgb(resultRed, resultGreen, resultBlue));
309314
line.setWidth(7.5f);
315+
line.setInfoWindow(null);
310316
map.getOverlays().add(line);
311317
markerList.forEach(marker -> map.getOverlays().add(marker));
312318
}else{
@@ -322,11 +328,17 @@ public static void updateBuses(List<Bus> busList, MapView map, Activity activity
322328
map.getOverlays().remove(marker);
323329
}
324330
markerBusList.clear();
331+
int index = 0;
325332
for (Bus bus : busList){
326333
double[] coordinates = bus.getCoordinates();
327334
GeoPoint point = new GeoPoint(coordinates[0], coordinates[1]);
328335
Marker marker = new Marker(map);
329-
Drawable d = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.cm_bus_regular, null);
336+
Drawable d;
337+
if (index!=currentSelectedBus){
338+
d = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.cm_bus, null);
339+
}else{
340+
d = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.cm_bus_regular, null);
341+
}
330342
RotateDrawable d1 = new RotateDrawable();
331343
d1.setDrawable(d);
332344
d1.setFromDegrees(0f);
@@ -338,47 +350,23 @@ public static void updateBuses(List<Bus> busList, MapView map, Activity activity
338350
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);
339351
String descrption = "Vehicle Id: " + bus.getVehicleId() + "\nSpeed: " + bus.getSpeed() + "\nPattern Id: " + bus.getPattern_id();
340352
MarkerInfoWindow miw= new CustomMarkerInfoWindow(org.osmdroid.library.R.layout.bonuspack_bubble, map, "Bus " + bus.getId(), descrption);
341-
marker.setInfoWindow(miw);
353+
marker.setOnMarkerClickListener(new BusClickListener(miw));
342354
map.getOverlays().add(marker);
355+
index++;
343356
}
344357
}
345358

346359
public static void updateBusesUI(){
347360
activity.runOnUiThread(new Runnable() {
348361
@Override
349362
public void run() {
350-
updateBuses(busList, map, activity);
363+
synchronized (busList){
364+
updateBuses(busList, map, activity);
365+
}
351366
}
352367
});
353368
}
354369

355-
356-
private static Drawable getRotateDrawable(Bitmap b, float angle, Activity activity) {
357-
final BitmapDrawable drawable = new BitmapDrawable(activity.getApplicationContext().getResources(), b) {
358-
@Override
359-
public void draw(final Canvas canvas) {
360-
canvas.save();
361-
canvas.rotate(angle, b.getWidth() / 2f, b.getHeight() / 2f);
362-
super.draw(canvas);
363-
canvas.restore();
364-
}
365-
};
366-
return drawable;
367-
}
368-
369-
private static Drawable getRotateDrawable2(final Drawable d, final float angle) {
370-
final Drawable[] arD = { d };
371-
return new LayerDrawable(arD) {
372-
@Override
373-
public void draw(final Canvas canvas) {
374-
canvas.save();
375-
canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2);
376-
super.draw(canvas);
377-
canvas.restore();
378-
}
379-
};
380-
}
381-
382370
public void showBackgroundThreadDialog(){
383371
getActivity().runOnUiThread(new Runnable() {
384372
@Override
@@ -395,4 +383,29 @@ public static void updateTextView(){
395383
public MapView getMap() {
396384
return map;
397385
}
386+
387+
static class BusClickListener implements Marker.OnMarkerClickListener{
388+
MarkerInfoWindow miw;
389+
public BusClickListener(MarkerInfoWindow miw){
390+
this.miw=miw;
391+
}
392+
@Override
393+
public boolean onMarkerClick(Marker marker, MapView mapView) {
394+
int index = 0;
395+
marker.closeInfoWindow();
396+
for (Marker busMarker : markerBusList) {
397+
if (busMarker.equals(marker)){
398+
if (currentSelectedBus != index){
399+
currentSelectedBus = index + 1;
400+
previousButton.performClick();
401+
}
402+
break;
403+
}
404+
index++;
405+
}
406+
marker.setInfoWindow(miw);
407+
marker.showInfoWindow();
408+
return false;
409+
}
410+
}
398411
}

app/src/main/java/kevin/carrismobile/fragments/RouteDetailsFragment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
129129
textView.setText("Insira o número da sua Carreira\ne pressione Atualizar");
130130

131131
map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
132-
map.setTileSource(TileSourceFactory.OpenTopo);
132+
map.setTileSource(SettingsFragment.getCurrentTileProvider());
133133

134134
map.setMultiTouchControls(true);
135135
map.getController().setCenter(new GeoPoint(38.73329737648646, -9.14096412687648));
@@ -578,6 +578,7 @@ private void updateMarkers(List<Stop> stopList, MapView map){
578578
int resultBlue = Integer.valueOf(hexCode.substring(4, 6), 16);
579579
line.setColor(Color.rgb(resultRed, resultGreen, resultBlue));
580580
line.setWidth(7.5f);
581+
line.setInfoWindow(null);
581582
map.getOverlays().add(line);
582583
markerList.forEach(marker -> map.getOverlays().add(marker));
583584
}else{

app/src/main/java/kevin/carrismobile/fragments/SettingsFragment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.android.material.textfield.TextInputEditText;
2323
import com.google.gson.Gson;
2424

25+
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
2526
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
2627
import org.osmdroid.tileprovider.tilesource.bing.BingMapTileSource;
2728
import org.osmdroid.views.MapView;
@@ -40,6 +41,7 @@
4041
public class SettingsFragment extends Fragment {
4142
private SharedPreferences mPrefs;
4243
public List<String> keys;
44+
public static OnlineTileSourceBase currentTileProvider = TileSourceFactory.OpenTopo;
4345
public Switch openTopoSwitch;
4446
public Switch thunderForestSwitch;
4547
public Switch bingMapsSwitch;
@@ -98,6 +100,10 @@ public void run() {
98100
return v;
99101
}
100102

103+
public static OnlineTileSourceBase getCurrentTileProvider() {
104+
return currentTileProvider;
105+
}
106+
101107
private void setOpenTopoSwitchOnClickListener(){
102108
openTopoSwitch.setOnClickListener(new View.OnClickListener() {
103109
@Override
@@ -118,6 +124,7 @@ public void onClick(View view) {
118124
map1.setTileSource(TileSourceFactory.OpenTopo);
119125
map2.setTileSource(TileSourceFactory.OpenTopo);
120126
map3.setTileSource(TileSourceFactory.OpenTopo);
127+
currentTileProvider = TileSourceFactory.OpenTopo;
121128
map1.invalidate();
122129
map2.invalidate();
123130
map3.invalidate();
@@ -155,6 +162,8 @@ public void onClick(View view) {
155162
map1.setTileSource(bingMap);
156163
map2.setTileSource(bingMap);
157164
map3.setTileSource(bingMap);
165+
166+
currentTileProvider = bingMap;
158167
map1.invalidate();
159168
map2.invalidate();
160169
map3.invalidate();
@@ -191,6 +200,7 @@ public void onClick(View view) {
191200
map1.setTileSource(thunderForestTileSource);
192201
map2.setTileSource(thunderForestTileSource);
193202
map3.setTileSource(thunderForestTileSource);
203+
currentTileProvider = thunderForestTileSource;
194204
map1.invalidate();
195205
map2.invalidate();
196206
map3.invalidate();
@@ -239,6 +249,7 @@ public void onClick(View view) {
239249
map1.setTileSource(mapTilerSource);
240250
map2.setTileSource(mapTilerSource);
241251
map3.setTileSource(mapTilerSource);
252+
currentTileProvider = mapTilerSource;
242253
map1.invalidate();
243254
map2.invalidate();
244255
map3.invalidate();

app/src/main/java/kevin/carrismobile/fragments/StopsMapFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
7575

7676

7777
map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
78-
map.setTileSource(TileSourceFactory.OpenTopo);
78+
map.setTileSource(SettingsFragment.getCurrentTileProvider());
7979

8080
map.setMultiTouchControls(true);
8181
CompassOverlay compassOverlay = new CompassOverlay(getActivity(), map);

app/src/main/java/kevin/carrismobile/gui/BusBackgroundThread.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public void run() {
5353
}
5454
}
5555
}
56-
RealTimeFragment.busList.clear();
57-
RealTimeFragment.busList.addAll(listToAdd);
56+
synchronized (RealTimeFragment.busList){
57+
RealTimeFragment.busList.clear();
58+
RealTimeFragment.busList.addAll(listToAdd);
59+
}
5860
RealTimeFragment.updateBusesUI();
5961
RealTimeFragment.activity.runOnUiThread(new Runnable() {
6062
@Override
@@ -82,13 +84,16 @@ public void run() {
8284

8385
Log.println(Log.DEBUG,"BACKGROUND CALLER", "Call" + index);
8486
index++;
85-
List<Marker> currentBusList = RealTimeFragment.markerBusList;
86-
TimeUnit.MILLISECONDS.sleep(5000);
87-
if (!RealTimeFragment.markerBusList.equals(currentBusList)){
88-
for (Marker marker : currentBusList){
89-
RealTimeFragment.map.getOverlays().remove(marker);
87+
synchronized (RealTimeFragment.markerBusList){
88+
List<Marker> currentBusList = RealTimeFragment.markerBusList;
89+
TimeUnit.MILLISECONDS.sleep(5000);
90+
if (!RealTimeFragment.markerBusList.equals(currentBusList)){
91+
for (Marker marker : currentBusList){
92+
RealTimeFragment.map.getOverlays().remove(marker);
93+
}
9094
}
9195
}
96+
9297
Log.println(Log.DEBUG, "BACKGROUND THREAD", thread.getName() + "STARTED");
9398
lock.lock();
9499
thread.start();

0 commit comments

Comments
 (0)