30
30
import android .os .SystemClock ;
31
31
import android .support .v4 .app .Fragment ;
32
32
import android .support .v4 .app .FragmentManager ;
33
- import java .text .DateFormat ;
34
33
import android .util .Log ;
35
34
import android .view .Display ;
36
35
import android .view .LayoutInflater ;
62
61
import com .google .android .gms .maps .model .MarkerOptions ;
63
62
import com .google .android .gms .maps .model .Polyline ;
64
63
import com .google .android .gms .maps .model .PolylineOptions ;
65
- import com .imgtec .flow .Flow ;
66
64
import com .imgtec .flow .MessagingEvent ;
67
65
import com .imgtec .flow .client .users .DataStore ;
68
66
import com .imgtec .flow .client .users .DataStoreItem ;
74
72
import com .imgtec .hobbyist .flow .AsyncMessage ;
75
73
import com .imgtec .hobbyist .flow .AsyncMessageListener ;
76
74
import com .imgtec .hobbyist .flow .DevicePresenceListener ;
77
- import com .imgtec .hobbyist .flow .FlowEntities ;
78
75
import com .imgtec .hobbyist .flow .FlowHelper ;
79
76
import com .imgtec .hobbyist .flow .GPSReading ;
80
77
import com .imgtec .hobbyist .flow .Geofence ;
96
93
import java .text .SimpleDateFormat ;
97
94
import java .util .ArrayList ;
98
95
import java .util .Arrays ;
99
- import java .util .Collections ;
100
96
import java .util .Comparator ;
101
97
import java .util .Date ;
102
98
import java .util .HashMap ;
@@ -200,6 +196,7 @@ public class InteractiveModeFragment extends NDListeningFragment implements
200
196
201
197
private int saveReadingPeriod ;
202
198
private int maximumReadings ;
199
+ private Timer updateTimer ;
203
200
204
201
205
202
public static InteractiveModeFragment newInstance () {
@@ -231,10 +228,12 @@ public void onDestroyView() {
231
228
// because we add a fragment in the XML and InteractiveModeFragment is recreated
232
229
// each time we need to remove the map fragment
233
230
Fragment mapFragment = fm .findFragmentById (R .id .mapFragment );
234
- if (mapFragment != null ) {
231
+ try {
232
+ if (mapFragment != null ) {
235
233
fm .beginTransaction ().remove (mapFragment ).commit ();
236
- }
237
-
234
+ }
235
+ } catch (IllegalStateException e )
236
+ {}
238
237
super .onDestroyView ();
239
238
}
240
239
@@ -317,13 +316,30 @@ public void run() {
317
316
318
317
}
319
318
});
319
+
320
+ updateTimer = new Timer ();
321
+ updateTimer .scheduleAtFixedRate (new TimerTask () {
322
+ @ Override
323
+ public void run () {
324
+ fetchWiFireLocation ();
325
+ fetchWiFireLocationHistory (0 );
326
+ }
327
+ }, 0 , 30000 );
320
328
}
321
329
322
330
323
331
@ Override
324
332
public void onPause () {
325
333
flowHelper .removeDevicePresenceListener (this );
326
334
connectionReceiver .unregister (appContext );
335
+ updateTimer = new Timer ();
336
+ updateTimer .scheduleAtFixedRate (new TimerTask () {
337
+ @ Override
338
+ public void run () {
339
+ fetchWiFireLocation ();
340
+ fetchWiFireLocationHistory (0 );
341
+ }
342
+ }, 0 , 30000 );
327
343
super .onPause ();
328
344
}
329
345
@@ -602,13 +618,14 @@ public void onStart() {
602
618
super .onStart ();
603
619
locationClient .connect ();
604
620
fetchWiFireLocationHistory (INITIAL_LOCATIONS_LOAD_EXECUTOR_ID );
605
- new Timer ().scheduleAtFixedRate (new TimerTask () {
606
- @ Override
607
- public void run () {
608
- fetchWiFireLocation ();
609
- fetchWiFireLocationHistory (0 );
610
- }
611
- }, 0 , 30000 );
621
+ /* updateTimer = new Timer();
622
+ updateTimer.scheduleAtFixedRate(new TimerTask() {
623
+ @Override
624
+ public void run() {
625
+ fetchWiFireLocation();
626
+ fetchWiFireLocationHistory(0);
627
+ }
628
+ }, 0, 30000);*/
612
629
//fetchWiFireLocation();
613
630
}
614
631
@@ -650,7 +667,7 @@ public void onCommandResponse(AsyncMessage response) {
650
667
String code = response .getNode ("responsecode" );
651
668
String params = response .getNode ("responseparams" );
652
669
653
- Log .v ("CommandResponseHandler. onCommandResponse" , "response->responseparams = \" " + params + "\" " );
670
+ Log .v ("onCommandResponse" , "response->responseparams = \" " + params + "\" " );
654
671
if (code .equals ("OK" )){
655
672
try {
656
673
GPSReading currentLocation = new GPSReading (params );
@@ -660,11 +677,11 @@ public void onCommandResponse(AsyncMessage response) {
660
677
showToast ("Updated WiFire location" , Toast .LENGTH_SHORT );
661
678
662
679
} catch (XmlPullParserException | IOException | ParseException e ) {
663
- Log .e ("CommandResponseHandler. onCommandResponse" , "Error parsing GPS reading" , e );
680
+ Log .e ("onCommandResponse" , "Error parsing GPS reading" , e );
664
681
e .printStackTrace ();
665
682
}
666
683
} else {
667
- Log .e ("CommandResponseHandler. onCommandResponse" , "Got response code" + code );
684
+ Log .e ("onCommandResponse" , "Got response code" + code );
668
685
}
669
686
670
687
}
@@ -724,8 +741,8 @@ public void execute() {
724
741
flowHelper .postAsyncMessage (message );
725
742
726
743
waitingCommands .put (message .getRequestId (), onResponse );
727
- Log .v ("InteractiveModeFragment. sendCommand" , "Sent command \" " + command + "\" . Request ID: " + message .getRequestId ());
728
- Log .v ("InteractiveModeFragment. sendCommand" , message .buildXml ());
744
+ Log .v ("sendCommand" , "Sent command \" " + command + "\" . Request ID: " + message .getRequestId ());
745
+ Log .v ("sendCommand" , message .buildXml ());
729
746
730
747
731
748
}
@@ -785,7 +802,9 @@ public void run() {
785
802
partialSplinePoints = new ArrayList <>();
786
803
CatmullRomSpline c = new CatmullRomSpline (points , REPLAY_PATH_SUBSTEPS );
787
804
788
- animatePath (c .iterator (), 0 , LatLngBounds .builder ());
805
+ LatLngBounds .Builder builder = LatLngBounds .builder ();
806
+ if (!points .isEmpty ()) builder .include (points .get (0 ));
807
+ animatePath (c .iterator (), 0 , builder );
789
808
}
790
809
}
791
810
@@ -834,10 +853,10 @@ public List<GPSReading> submit() {
834
853
sdf .setTimeZone (TimeZone .getTimeZone ("UTC" ));
835
854
String timeAgo = sdf .format (new Date (System .currentTimeMillis () - (maximumReadings * saveReadingPeriod )));
836
855
837
- Log .v ("fetchWiFireLocationHistory " , "gpsReadingDatastore.getItemsByQuery(\" @gpsreadingtime >= '\" + " + timeAgo + "+ \" '\" )" );
856
+ Log .v ("fetchWiFi...History " , "gpsReadingDatastore.getItemsByQuery(\" @gpsreadingtime >= '\" + " + timeAgo + "+ \" '\" )" );
838
857
DataStoreItems gpsReadingDatastoreItems = gpsReadingDatastore .getItemsByQuery ("@gpsreadingtime >= '" + timeAgo + "'" );
839
858
840
- Log .v ("fetchWiFireLocationHistory " , "Got " + gpsReadingDatastoreItems .size () + " entries from location history" );
859
+ Log .v ("fetchWiFi...History " , "Got " + gpsReadingDatastoreItems .size () + " entries from location history" );
841
860
842
861
List <GPSReading > gpsReadings = new ArrayList <>(Math .min (maximumReadings , gpsReadingDatastoreItems .size ()));
843
862
int skip = 0 ;
@@ -849,6 +868,7 @@ public List<GPSReading> submit() {
849
868
}
850
869
}
851
870
871
+ Log .v ("fetchWiFi...History" , "Down to " + gpsReadings .size () + " / " + maximumReadings + " entries from location history" );
852
872
853
873
854
874
return gpsReadings ;
@@ -1037,32 +1057,31 @@ public void onAsyncMessageResponse(MessagingEvent.AsyncMessageResponse response)
1037
1057
}
1038
1058
}
1039
1059
if (!success ){
1060
+ updateTimer .cancel ();
1040
1061
ActivitiesAndFragmentsHelper .showFragmentChangeDialog (
1041
1062
R .string .wifire_connectivity_problems ,
1042
1063
R .string .back_to_connected_devices ,
1043
1064
(FlowActivity ) activity ,
1044
1065
SimpleFragmentFactory .createFragment (ConnectedDevicesFragment .TAG ));
1045
1066
}
1046
1067
//online = success;
1047
- Log .v ("InteractiveModeFragment. onAsyncMessageResponse" , response .name () + " - " + response + " :: " + responseText );
1068
+ Log .v ("onAsyncMessageResponse" , response .name () + " - " + response + " :: " + responseText );
1048
1069
}
1049
1070
1050
1071
@ Override
1051
1072
public void onTextMessageReceived (AsyncMessage msg ) {
1052
- Log .v ("InteractiveModeFragment. onTextMessageReceived" , msg .buildXml ());
1073
+ Log .v ("onTextMessageReceived" , msg .buildXml ());
1053
1074
}
1054
1075
1055
1076
@ Override
1056
1077
public void onCommandMessageReceived (AsyncMessage msg ) {
1057
- Log .v ("InteractiveModeFragment.onCommandMessageReceived" , msg .buildXml ());
1058
1078
}
1059
1079
1060
1080
@ Override
1061
1081
public void onCommandRXMessageReceived (AsyncMessage msg ) {
1062
- Log .v ("InteractiveModeFragment.onCommandRXMessageReceived" , "ID: " + msg .getNode ("clientid" ) + " - " + msg .buildXml ());
1063
1082
CommandResponseHandler resp = waitingCommands .get (msg .getNode ("clientid" ));
1064
1083
if (resp == null ) {
1065
- Log .w ("InteractiveModeFragment.onCommandRXMessageReceived " , "No response found for message " + msg .getNode ("clientid" ));
1084
+ Log .w ("onCommandRXMe...ved " , "No response found for message " + msg .getNode ("clientid" ));
1066
1085
} else {
1067
1086
waitingCommands .remove (msg .getNode ("clientid" ));
1068
1087
if (getActivity () != null ) resp .onCommandResponse (msg );
@@ -1077,6 +1096,7 @@ public void onCommandRXMessageReceived(AsyncMessage msg) {
1077
1096
@ Override
1078
1097
public void onDevicePresenceChangeListener (boolean isConnected ) {
1079
1098
if (!isConnected ) {
1099
+ updateTimer .cancel ();
1080
1100
showConnectedDevicesFragmentDialog (SimpleFragmentFactory .createFragment (ConnectedDevicesFragment .TAG ));
1081
1101
}
1082
1102
}
0 commit comments