/
LineFragment.java
1060 lines (892 loc) · 40.8 KB
/
LineFragment.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Aaron Hastings
*
* Copyright 2015-2018 Aaron Hastings
*
* This file is part of Luas at a Glance.
*
* Luas at a Glance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Luas at a Glance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Luas at a Glance. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thecosmicfrog.luasataglance.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import org.thecosmicfrog.luasataglance.R;
import org.thecosmicfrog.luasataglance.api.ApiMethods;
import org.thecosmicfrog.luasataglance.api.ApiTimes;
import org.thecosmicfrog.luasataglance.object.EnglishGaeilgeMap;
import org.thecosmicfrog.luasataglance.object.NotifyTimesMap;
import org.thecosmicfrog.luasataglance.object.StopForecast;
import org.thecosmicfrog.luasataglance.object.StopNameIdMap;
import org.thecosmicfrog.luasataglance.util.Constant;
import org.thecosmicfrog.luasataglance.util.Preferences;
import org.thecosmicfrog.luasataglance.util.Settings;
import org.thecosmicfrog.luasataglance.util.StopForecastUtil;
import org.thecosmicfrog.luasataglance.view.SpinnerCardView;
import org.thecosmicfrog.luasataglance.view.StatusCardView;
import org.thecosmicfrog.luasataglance.view.StopForecastCardView;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class LineFragment extends Fragment {
private final String LOG_TAG = LineFragment.class.getSimpleName();
private static int resLayoutFragmentLine;
private static int resMenuLine;
private static int resProgressBar;
private static int resSpinnerCardView;
private static int resStatusCardView;
private static int resSwipeRefreshLayout;
private static int resScrollView;
private static int resInboundStopForecastCardView;
private static int resOutboundStopForecastCardView;
private static int resArrayStopsRedLine;
private static int resArrayStopsGreenLine;
private static StopNameIdMap mapStopNameId;
private static String localeDefault;
private View rootView = null;
private Menu menu;
private TabLayout tabLayout;
private ProgressBar progressBar;
private SpinnerCardView spinnerCardView;
private SwipeRefreshLayout swipeRefreshLayout;
private ScrollView scrollView;
private StatusCardView statusCardView;
private StopForecastCardView inboundStopForecastCardView;
private StopForecastCardView outboundStopForecastCardView;
private boolean isInitialised;
private TimerTask timerTaskReload;
private boolean shouldAutoReload = false;
private String line;
private boolean isVisibleToUser = false;
public LineFragment() {
/* Required empty public constructor. */
}
public static LineFragment newInstance(String line) {
LineFragment lineFragment = new LineFragment();
Bundle bundle = new Bundle();
bundle.putInt(Constant.RES_ARRAY_STOPS_RED_LINE, R.array.array_stops_redline);
bundle.putInt(Constant.RES_ARRAY_STOPS_GREEN_LINE, R.array.array_stops_greenline);
switch (line) {
case Constant.RED_LINE:
bundle.putString(Constant.LINE, Constant.RED_LINE);
bundle.putInt(Constant.RES_LAYOUT_FRAGMENT_LINE, R.layout.fragment_redline);
bundle.putInt(Constant.RES_MENU_LINE, R.menu.menu_red_line);
bundle.putInt(Constant.RES_PROGRESSBAR, R.id.redline_progressbar);
bundle.putInt(Constant.RES_SPINNER_CARDVIEW, R.id.redline_spinner_card_view);
bundle.putInt(Constant.RES_STATUS_CARDVIEW, R.id.redline_statuscardview);
bundle.putInt(Constant.RES_SWIPEREFRESHLAYOUT, R.id.redline_swiperefreshlayout);
bundle.putInt(Constant.RES_SCROLLVIEW, R.id.redline_scrollview);
bundle.putInt(
Constant.RES_INBOUND_STOPFORECASTCARDVIEW,
R.id.redline_inbound_stopforecastcardview
);
bundle.putInt(
Constant.RES_OUTBOUND_STOPFORECASTCARDVIEW,
R.id.redline_outbound_stopforecastcardview
);
break;
case Constant.GREEN_LINE:
bundle.putString(Constant.LINE, Constant.GREEN_LINE);
bundle.putInt(Constant.RES_LAYOUT_FRAGMENT_LINE, R.layout.fragment_greenline);
bundle.putInt(Constant.RES_MENU_LINE, R.menu.menu_green_line);
bundle.putInt(Constant.RES_PROGRESSBAR, R.id.greenline_progressbar);
bundle.putInt(Constant.RES_SPINNER_CARDVIEW, R.id.greenline_spinner_card_view);
bundle.putInt(Constant.RES_STATUS_CARDVIEW, R.id.greenline_statuscardview);
bundle.putInt(Constant.RES_SWIPEREFRESHLAYOUT, R.id.greenline_swiperefreshlayout);
bundle.putInt(Constant.RES_SCROLLVIEW, R.id.greenline_scrollview);
bundle.putInt(
Constant.RES_INBOUND_STOPFORECASTCARDVIEW,
R.id.greenline_inbound_stopforecastcardview
);
bundle.putInt(
Constant.RES_OUTBOUND_STOPFORECASTCARDVIEW,
R.id.greenline_outbound_stopforecastcardview
);
break;
default:
/* If for some reason the line doesn't make sense. */
Log.wtf(LineFragment.class.getSimpleName(), "Invalid line specified.");
}
lineFragment.setArguments(bundle);
return lineFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFragmentVars();
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment. */
rootView = inflater.inflate(resLayoutFragmentLine, container, false);
/* Initialise correct locale. */
localeDefault = Locale.getDefault().toString();
/* Instantiate a new StopNameIdMap. */
mapStopNameId = new StopNameIdMap(localeDefault);
if (isAdded()) {
isInitialised = initFragment();
}
/*
* If an Intent did not bring us to this Activity and there is a stop name saved in shared
* preferences, load that stop.
* This provides persistence to the app across shutdowns.
*/
if (!getActivity().getIntent().hasExtra(Constant.STOP_NAME)) {
if (Preferences.selectedStopName(getContext(), Constant.NO_LINE) != null) {
String stopName = Preferences.selectedStopName(getContext(), Constant.NO_LINE);
setTabAndSpinner(stopName);
}
}
return rootView;
}
@Override
public void onPause() {
super.onPause();
/* Stop the auto-reload TimerTask. */
timerTaskReload.cancel();
}
@Override
public void onResume() {
final String INTENT_EXTRA_ACTIVITY_TO_OPEN = "activityToOpen";
super.onResume();
/* Remove Favourites tutorial if it has been completed once already. */
if (line.equals(Constant.RED_LINE)
&& Preferences.hasRunOnce(getContext(), Constant.TUTORIAL_FAVOURITES)) {
StopForecastUtil.displayTutorial(
rootView,
Constant.RED_LINE,
Constant.TUTORIAL_FAVOURITES,
false
);
}
if (isAdded()) {
/*
* If a Favourite stop brought us to this Activity, load that stop's forecast.
* If a tapped notification brought us to this Activity, load the forecast for the stop
* sent with that Intent.
* If the previous cases are not matched, and the user has selected a default stop, load
* the forecast for that.
*/
if (getActivity().getIntent().hasExtra(Constant.STOP_NAME)) {
String stopName = getActivity().getIntent().getStringExtra(Constant.STOP_NAME);
/*
* Track whether or not the tab and spinner has been set. If it has, clear the Extra
* so it doesn't break the Default Stop setting.
*/
boolean hasSetTabAndSpinner = setTabAndSpinner(stopName);
if (hasSetTabAndSpinner) {
getActivity().getIntent().removeExtra(Constant.STOP_NAME);
}
} else if (getActivity().getIntent().hasExtra(Constant.NOTIFY_STOP_NAME)) {
/*
* Track whether or not the tab and spinner has been set. If it has, clear the Extra
* so it doesn't break the Default Stop setting.
*/
boolean hasSetTabAndSpinner =
setTabAndSpinner(
getActivity().getIntent().getStringExtra(Constant.NOTIFY_STOP_NAME)
);
if (hasSetTabAndSpinner) {
getActivity().getIntent().removeExtra(Constant.NOTIFY_STOP_NAME);
}
} else if (getActivity().getIntent().hasExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN)) {
activityRouter(
getActivity().getIntent().getStringExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN)
);
/* Clear the Extra to avoid opening the same Activity on every start. */
getActivity().getIntent().removeExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN);
} else if (!Preferences.defaultStopName(getContext()).equals(getString(R.string.none))
&& Preferences.defaultStopName(getContext()) != null) {
setTabAndSpinner(Preferences.defaultStopName(getContext()));
}
/* Display tutorial for selecting a stop, if required. */
StopForecastUtil.displayTutorial(rootView, line, Constant.TUTORIAL_SELECT_STOP, true);
/*
* Reload stop forecast.
* Induce 10 second delay if app is launching from cold start (timerTaskReload == null)
* in order to prevent two HTTP requests in rapid succession.
*/
if (timerTaskReload == null) {
autoReloadStopForecast(10000);
} else {
autoReloadStopForecast(0);
}
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
this.isVisibleToUser = isVisibleToUser;
if (isInitialised) {
/* If the Spinner's selected item is "Select a stop...", get out of here. */
if (spinnerCardView.getSpinnerStops().getSelectedItemPosition() == 0) {
Log.i(LOG_TAG, "Spinner selected item is \"Select a stop...\"");
return;
}
/* When this tab is visible to the user, load a stop forecast. */
if (isVisibleToUser) {
if (spinnerCardView.getSpinnerStops().getSelectedItem() != null) {
String stopName =
spinnerCardView.getSpinnerStops().getSelectedItem().toString();
Preferences.saveSelectedStopName(getContext(), Constant.NO_LINE, stopName);
loadStopForecast(stopName);
shouldAutoReload = true;
} else {
Log.w(LOG_TAG, "Spinner selected item is null.");
}
} else {
shouldAutoReload = false;
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
/* Set the menu to a class variable for easy manipulation. */
this.menu = menu;
/* Inflate the menu; this adds items to the action bar if it is present. */
inflater.inflate(resMenuLine, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Settings.getSettings(getContext(), item);
return super.onOptionsItemSelected(item);
}
/**
* Initialise local variables for this Fragment instance.
*/
private void initFragmentVars() {
resArrayStopsRedLine = getArguments().getInt(Constant.RES_ARRAY_STOPS_RED_LINE);
resArrayStopsGreenLine = getArguments().getInt(Constant.RES_ARRAY_STOPS_GREEN_LINE);
line = getArguments().getString(Constant.LINE);
resLayoutFragmentLine = getArguments().getInt(Constant.RES_LAYOUT_FRAGMENT_LINE);
resMenuLine = getArguments().getInt(Constant.RES_MENU_LINE);
resProgressBar = getArguments().getInt(Constant.RES_PROGRESSBAR);
resSpinnerCardView = getArguments().getInt(Constant.RES_SPINNER_CARDVIEW);
resStatusCardView = getArguments().getInt(Constant.RES_STATUS_CARDVIEW);
resSwipeRefreshLayout = getArguments().getInt(Constant.RES_SWIPEREFRESHLAYOUT);
resScrollView = getArguments().getInt(Constant.RES_SCROLLVIEW);
resInboundStopForecastCardView =
getArguments().getInt(Constant.RES_INBOUND_STOPFORECASTCARDVIEW);
resOutboundStopForecastCardView =
getArguments().getInt(Constant.RES_OUTBOUND_STOPFORECASTCARDVIEW);
}
/**
* Initialise Fragment and its views.
*/
private boolean initFragment() {
tabLayout = (TabLayout) getActivity().findViewById(R.id.tablayout);
progressBar = (ProgressBar) rootView.findViewById(resProgressBar);
setIsLoading(false);
/* Set up Spinner and onItemSelectedListener. */
spinnerCardView =
(SpinnerCardView) rootView.findViewById(resSpinnerCardView);
spinnerCardView.setLine(line);
spinnerCardView.getSpinnerStops().setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
/*
* If the Spinner's selected item is "Select a stop...", we don't need to
* do anything. Just clear the stop forecast and get out of here.
*/
if (position == 0) {
shouldAutoReload = false;
swipeRefreshLayout.setEnabled(false);
clearStopForecast();
return;
} else {
swipeRefreshLayout.setEnabled(true);
}
shouldAutoReload = true;
/* Hide the select stop tutorial, if it is visible. */
StopForecastUtil.displayTutorial(
rootView,
line,
Constant.TUTORIAL_SELECT_STOP,
false
);
/* Show the notifications tutorial. */
StopForecastUtil.displayTutorial(
rootView,
line,
Constant.TUTORIAL_NOTIFICATIONS,
true
);
/*
* Get the stop name from the current position of the Spinner, save it to
* SharedPreferences, then load a stop forecast with it.
*/
String selectedStopName =
spinnerCardView
.getSpinnerStops().getItemAtPosition(position).toString();
loadStopForecast(selectedStopName);
if (isVisibleToUser) {
Preferences.saveSelectedStopName(
getContext(),
line,
selectedStopName
);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
/* Set up Status CardView. */
statusCardView =
(StatusCardView) rootView.findViewById(resStatusCardView);
/* Set up SwipeRefreshLayout. */
swipeRefreshLayout =
(SwipeRefreshLayout) rootView.findViewById(resSwipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
/* Start by clearing the currently-displayed stop forecast. */
clearStopForecast();
/* Start the refresh animation. */
swipeRefreshLayout.setRefreshing(true);
loadStopForecast(
Preferences.selectedStopName(getContext(), line)
);
}
}
);
scrollView = (ScrollView) rootView.findViewById(resScrollView);
/* Set up stop forecast CardViews. */
inboundStopForecastCardView =
(StopForecastCardView) rootView.findViewById(
resInboundStopForecastCardView
);
inboundStopForecastCardView.setStopForecastDirection(
getString(R.string.inbound)
);
outboundStopForecastCardView =
(StopForecastCardView) rootView.findViewById(
resOutboundStopForecastCardView
);
outboundStopForecastCardView.setStopForecastDirection(
getString(R.string.outbound)
);
/* Set up onClickListeners for stop forecasts in both tabs. */
initStopForecastOnClickListeners();
return true;
}
/**
* Initialise OnClickListeners for a stop forecast.
*/
private void initStopForecastOnClickListeners() {
TableRow[] tableRowInboundStops = inboundStopForecastCardView.getTableRowStops();
TableRow[] tableRowOutboundStops = outboundStopForecastCardView.getTableRowStops();
for (int i = 0; i < 5; i++) {
final int index = i;
tableRowInboundStops[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNotifyTimeDialog(
spinnerCardView.getSpinnerStops().getSelectedItem().toString(),
inboundStopForecastCardView.getTextViewStopTimes(),
index
);
}
});
tableRowOutboundStops[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNotifyTimeDialog(
spinnerCardView.getSpinnerStops().getSelectedItem().toString(),
outboundStopForecastCardView.getTextViewStopTimes(),
index
);
}
});
}
}
/**
* Utility method to open an Activity based on a passed tag value.
* @param activityToOpen Value of Activity to open.
*/
private void activityRouter(String activityToOpen) {
Log.i(LOG_TAG, "Intent received to open Activity.");
switch (activityToOpen) {
case Constant.REMOTEMESSAGE_VALUE_ACTIVITY_FARES:
Log.i(LOG_TAG, "Routing to Activity: " + Constant.CLASS_FARES_ACTIVITY);
startActivity(
new Intent(getContext(), Constant.CLASS_FARES_ACTIVITY)
);
break;
case Constant.REMOTEMESSAGE_VALUE_ACTIVITY_FAVOURITES:
Log.i(LOG_TAG, "Routing to Activity: " + Constant.CLASS_FAVOURITES_ACTIVITY);
startActivity(
new Intent(getContext(), Constant.CLASS_FAVOURITES_ACTIVITY)
);
break;
case Constant.REMOTEMESSAGE_VALUE_ACTIVITY_MAIN:
/* We're already in MainActivity. Nothing to do here. */
Log.i(LOG_TAG, "Already on MainActivity. Not routing anywhere.");
break;
case Constant.REMOTEMESSAGE_VALUE_ACTIVITY_MAPS:
Log.i(LOG_TAG, "Routing to Activity: " + Constant.CLASS_MAPS_ACTIVITY);
startActivity(
new Intent(getContext(), Constant.CLASS_MAPS_ACTIVITY)
);
break;
case Constant.REMOTEMESSAGE_VALUE_ACTIVITY_NEWS:
Log.i(LOG_TAG, "Routing to Activity: " + Constant.CLASS_NEWS_ACTIVITY);
startActivity(
new Intent(getContext(), Constant.CLASS_NEWS_ACTIVITY)
);
break;
case Constant.REMOTEMESSAGE_VALUE_ACTIVITY_SETTINGS:
Log.i(LOG_TAG, "Routing to Activity: " + Constant.CLASS_SETTINGS_ACTIVITY);
startActivity(
new Intent(getContext(), Constant.CLASS_SETTINGS_ACTIVITY)
);
break;
default:
/*
* We should have never gotten to this point, as NotificationUtil should
* pass MainActivity as its default case.
*/
Log.wtf(
LOG_TAG,
"activityToOpen key does not correspond to any known value."
);
}
}
/**
* Show dialog for choosing notification times.
* @param stopName Stop name to notify for.
* @param textViewStopTimes Array of TextViews for times in a stop forecast.
* @param index Index representing which specific tram to notify for.
*/
private void showNotifyTimeDialog(String stopName, TextView[] textViewStopTimes, int index) {
String localeDefault = Locale.getDefault().toString();
String notifyStopTimeStr = textViewStopTimes[index].getText().toString();
NotifyTimesMap mapNotifyTimes = new NotifyTimesMap(localeDefault, Constant.STOP_FORECAST);
if (notifyStopTimeStr.equals(""))
return;
if (notifyStopTimeStr.matches(
getString(R.string.due) + "|" + "1 .*|2 .*")) {
Toast.makeText(
getContext(),
getString(R.string.cannot_schedule_notification),
Toast.LENGTH_LONG
).show();
return;
}
/*
* When the user opens the notification dialog as part of the tutorial, scroll back up to
* the top so that the next tutorial is definitely visible. This should only ever run once.
*/
if (!Preferences.hasRunOnce(rootView.getContext(), Constant.TUTORIAL_NOTIFICATIONS)) {
if (scrollView != null) {
scrollView.setScrollY(0);
}
}
Preferences.saveHasRunOnce(rootView.getContext(), Constant.TUTORIAL_NOTIFICATIONS, true);
/* We're done with the notifications tutorial. Hide it. */
StopForecastUtil.displayTutorial(rootView, line, Constant.TUTORIAL_NOTIFICATIONS, false);
/* Then, display the final tutorial. */
StopForecastUtil.displayTutorial(rootView, line, Constant.TUTORIAL_FAVOURITES, true);
Preferences.saveNotifyStopName(
getContext(),
stopName
);
Preferences.saveNotifyStopTimeExpected(
getContext(),
mapNotifyTimes.get(notifyStopTimeStr)
);
getContext().startActivity(
new Intent(
getContext(),
NotifyTimeActivity.class
)
);
}
/**
* Make progress bar animate or not.
* @param loading Whether or not progress bar should animate.
*/
private void setIsLoading(final boolean loading) {
if (isAdded()) {
/*
* Only run if Fragment is attached to Activity. Without this check, the app is liable
* to crash when the screen is rotated many times in a given period of time.
*/
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (loading) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.INVISIBLE);
}
}
});
}
}
/**
* Set the current tab and the position of the Spinner.
*/
private boolean setTabAndSpinner(String stopName) {
String[] arrayStopsRedLine = getResources().getStringArray(
resArrayStopsRedLine
);
String[] arrayStopsGreenLine = getResources().getStringArray(
resArrayStopsGreenLine
);
List<String> listStopsRedLine = Arrays.asList(arrayStopsRedLine);
List<String> listStopsGreenLine = Arrays.asList(arrayStopsGreenLine);
List<String> listStopsThisLine = null;
int indexOtherLine = -1;
/* Initialise some variables specific to this Fragment (i.e. Red Line or Green Line). */
switch (line) {
case Constant.RED_LINE:
listStopsThisLine = listStopsRedLine;
indexOtherLine = 1;
break;
case Constant.GREEN_LINE:
listStopsThisLine = listStopsGreenLine;
indexOtherLine = 0;
break;
default:
/* If for some reason the line doesn't make sense. */
Log.wtf(LOG_TAG, "Invalid line specified.");
}
/* Safety check. */
if (listStopsThisLine == null) {
Log.e(LOG_TAG, "List of stops for this line is null.");
return false;
}
/*
* If the List of stops representing this Fragment contains the requested stop name, set the
* Spinner to that stop.
* Otherwise, switch to the other tab and load the last-loaded stop in the previous tab.
*/
if (listStopsThisLine.contains(stopName)) {
spinnerCardView.setSelection(stopName);
return true;
} else {
TabLayout.Tab tab = tabLayout.getTabAt(indexOtherLine);
if (tab != null) {
tab.select();
}
spinnerCardView.setSelection(Preferences.selectedStopName(getContext(), line));
return false;
}
}
/**
* Clear stop forecast.
*/
private void clearStopForecast() {
inboundStopForecastCardView.clearStopForecast();
outboundStopForecastCardView.clearStopForecast();
}
/**
* Automatically reload the stop forecast after a defined period.
* @param delayTimeMillis The delay (ms) before starting the timer.
*/
public void autoReloadStopForecast(int delayTimeMillis) {
final int RELOAD_TIME_MILLIS = 10000;
timerTaskReload = new TimerTask() {
@Override
public void run() {
/* Check Fragment is attached to Activity to avoid NullPointerExceptions. */
if (isAdded()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (shouldAutoReload) {
loadStopForecast(
Preferences.selectedStopName(
getContext(),
line
)
);
}
}
});
}
}
};
/* Schedule the auto-reload task to run. */
new Timer().schedule(timerTaskReload, delayTimeMillis, RELOAD_TIME_MILLIS);
}
/**
* Load the stop forecast for a particular stop.
* @param stopName The stop for which to load a stop forecast.
*/
private void loadStopForecast(String stopName) {
final String API_URL = "https://api.thecosmicfrog.org/cgi-bin";
final String API_ACTION = "times";
final String API_VER = "2";
setIsLoading(true);
/*
* Prepare Retrofit API call.
*/
final RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
ApiMethods methods = restAdapter.create(ApiMethods.class);
Callback<ApiTimes> callback = new Callback<ApiTimes>() {
@Override
public void success(ApiTimes apiTimes, Response response) {
/* Check Fragment is attached to Activity to avoid NullPointerExceptions. */
if (isAdded()) {
/* If the server returned times. */
if (apiTimes != null) {
/* Then create a stop forecast with this data. */
StopForecast stopForecast = StopForecastUtil.createStopForecast(apiTimes);
clearStopForecast();
/* Update the stop forecast. */
updateStopForecast(stopForecast);
/* Stop the refresh animations. */
setIsLoading(false);
swipeRefreshLayout.setRefreshing(false);
}
}
}
@Override
public void failure(RetrofitError retrofitError) {
Log.e(LOG_TAG, "Failure during call to server.");
/*
* If we get a message or a response from the server, there's likely an issue with
* the client request or the server's response itself.
*/
if (retrofitError.getMessage() != null) {
Log.e(LOG_TAG, "Message: " + retrofitError.getMessage());
}
if (retrofitError.getResponse() != null) {
if (retrofitError.getResponse().getUrl() != null) {
Log.e(LOG_TAG, "Response: " + retrofitError.getResponse().getUrl());
}
Log.e(LOG_TAG, "Status: " + Integer.toString(retrofitError.getResponse().getStatus()));
if (retrofitError.getResponse().getHeaders() != null) {
Log.e(LOG_TAG, "Headers: " + retrofitError.getResponse().getHeaders().toString());
}
if (retrofitError.getResponse().getBody() != null) {
Log.e(LOG_TAG, "Body: " + retrofitError.getResponse().getBody().toString());
}
if (retrofitError.getResponse().getReason() != null) {
Log.e(LOG_TAG, "Reason: " + retrofitError.getResponse().getReason());
}
}
/*
* If we don't receive a message or response, we can still get an idea of what's
* going on by getting the "kind" of error.
*/
if (retrofitError.getKind() != null) {
Log.e(LOG_TAG, "Kind: " + retrofitError.getKind().toString());
}
}
};
/* Call API and get stop forecast from server. */
methods.getStopForecast(
API_ACTION,
API_VER,
mapStopNameId.get(stopName),
callback
);
}
/**
* Draw stop forecast to screen.
* @param stopForecast StopForecast object containing data for requested stop.
*/
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private void updateStopForecast(StopForecast stopForecast) {
final String GAEILGE = "ga";
final String DUE = "DUE";
EnglishGaeilgeMap mapEnglishGaeilge = new EnglishGaeilgeMap();
String min = " " + getString(R.string.min);
String mins = " " + getString(R.string.mins);
String minOrMins;
/* If a valid stop forecast exists... */
if (stopForecast != null) {
String status;
boolean operatingNormally = false;
if (stopForecast.getStopForecastStatusDirectionInbound().getOperatingNormally()
&& stopForecast.getStopForecastStatusDirectionOutbound().getOperatingNormally()) {
operatingNormally = true;
}
if (localeDefault.startsWith(GAEILGE)) {
status = getString(R.string.message_success);
} else {
status = stopForecast.getMessage();
}
if (operatingNormally) {
/*
* No error message on server. Change the message title TextView to
* green and set a default success message.
*/
statusCardView.setStatus(status);
statusCardView.setStatusColor(R.color.message_success);
/* Change the alerts image to the default white image. */
MainActivity.getImageViewBottomNavAlerts().setImageResource(
R.drawable.ic_error_alerts
);
/* Change the color of the Alerts TextView to white (default). */
MainActivity.getTextViewBottomNavAlerts().setTextColor(
ContextCompat.getColor(getContext(), android.R.color.white)
);
} else {
if (status.equals("")) {
/*
* If the server returns no status message, the Luas RTPI system is
* probably down.
*/
statusCardView.setStatus(
getString(R.string.message_no_status)
);
} else {
/* Set the error message from the server. */
statusCardView.setStatus(status);
}
/* Change the color of the message title TextView to red. */
statusCardView.setStatusColor(R.color.message_error);
/* Change the Alerts image to the red version. */
MainActivity.getImageViewBottomNavAlerts().setImageResource(
R.drawable.ic_error_alerts_red
);
/* Change the color of the Alerts TextView to red. */
MainActivity.getTextViewBottomNavAlerts().setTextColor(
ContextCompat.getColor(getContext(), R.color.message_error)
);
}
/*
* Pull in all trams from the StopForecast, but only display up to five
* inbound and outbound trams.
*/
if (stopForecast.getInboundTrams() != null) {
if (stopForecast.getInboundTrams().size() == 0) {
inboundStopForecastCardView.setNoTramsForecast();
} else {
String inboundTram;
for (int i = 0; i < stopForecast.getInboundTrams().size(); i++) {
String dueMinutes =
stopForecast.getInboundTrams().get(i).getDueMinutes();
if (i < 5) {
if (localeDefault.startsWith(GAEILGE)) {
inboundTram = mapEnglishGaeilge.get(
stopForecast.getInboundTrams()
.get(i)
.getDestination()
);
} else {
inboundTram = stopForecast.getInboundTrams()
.get(i)
.getDestination();
}
if (dueMinutes.equalsIgnoreCase(DUE)) {
if (localeDefault.startsWith(GAEILGE)) {
dueMinutes = mapEnglishGaeilge.get(dueMinutes);
}
minOrMins = "";
} else if (Integer.parseInt(dueMinutes) > 1) {
minOrMins = mins;
} else {
minOrMins = min;
}
inboundStopForecastCardView.setStopNames(
i,
inboundTram
);
inboundStopForecastCardView.setStopTimes(
i,
dueMinutes + minOrMins
);
}
}
}
}