Skip to content

Commit 9568a06

Browse files
author
Simon Pinfold
committedFeb 17, 2015
Improved sending of messages once the WiFire is offline
1 parent e0516ec commit 9568a06

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed
 

‎app/src/main/java/com/imgtec/hobbyist/fragments/menu/InteractiveModeFragment.java

+47-27
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import android.os.SystemClock;
3131
import android.support.v4.app.Fragment;
3232
import android.support.v4.app.FragmentManager;
33-
import java.text.DateFormat;
3433
import android.util.Log;
3534
import android.view.Display;
3635
import android.view.LayoutInflater;
@@ -62,7 +61,6 @@
6261
import com.google.android.gms.maps.model.MarkerOptions;
6362
import com.google.android.gms.maps.model.Polyline;
6463
import com.google.android.gms.maps.model.PolylineOptions;
65-
import com.imgtec.flow.Flow;
6664
import com.imgtec.flow.MessagingEvent;
6765
import com.imgtec.flow.client.users.DataStore;
6866
import com.imgtec.flow.client.users.DataStoreItem;
@@ -74,7 +72,6 @@
7472
import com.imgtec.hobbyist.flow.AsyncMessage;
7573
import com.imgtec.hobbyist.flow.AsyncMessageListener;
7674
import com.imgtec.hobbyist.flow.DevicePresenceListener;
77-
import com.imgtec.hobbyist.flow.FlowEntities;
7875
import com.imgtec.hobbyist.flow.FlowHelper;
7976
import com.imgtec.hobbyist.flow.GPSReading;
8077
import com.imgtec.hobbyist.flow.Geofence;
@@ -96,7 +93,6 @@
9693
import java.text.SimpleDateFormat;
9794
import java.util.ArrayList;
9895
import java.util.Arrays;
99-
import java.util.Collections;
10096
import java.util.Comparator;
10197
import java.util.Date;
10298
import java.util.HashMap;
@@ -200,6 +196,7 @@ public class InteractiveModeFragment extends NDListeningFragment implements
200196

201197
private int saveReadingPeriod;
202198
private int maximumReadings;
199+
private Timer updateTimer;
203200

204201

205202
public static InteractiveModeFragment newInstance() {
@@ -231,10 +228,12 @@ public void onDestroyView() {
231228
// because we add a fragment in the XML and InteractiveModeFragment is recreated
232229
// each time we need to remove the map fragment
233230
Fragment mapFragment = fm.findFragmentById(R.id.mapFragment);
234-
if (mapFragment != null) {
231+
try {
232+
if (mapFragment != null) {
235233
fm.beginTransaction().remove(mapFragment).commit();
236-
}
237-
234+
}
235+
} catch (IllegalStateException e)
236+
{}
238237
super.onDestroyView();
239238
}
240239

@@ -317,13 +316,30 @@ public void run() {
317316

318317
}
319318
});
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);
320328
}
321329

322330

323331
@Override
324332
public void onPause() {
325333
flowHelper.removeDevicePresenceListener(this);
326334
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);
327343
super.onPause();
328344
}
329345

@@ -602,13 +618,14 @@ public void onStart() {
602618
super.onStart();
603619
locationClient.connect();
604620
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);*/
612629
//fetchWiFireLocation();
613630
}
614631

@@ -650,7 +667,7 @@ public void onCommandResponse(AsyncMessage response) {
650667
String code = response.getNode("responsecode");
651668
String params = response.getNode("responseparams");
652669

653-
Log.v("CommandResponseHandler.onCommandResponse", "response->responseparams = \"" + params + "\"");
670+
Log.v("onCommandResponse", "response->responseparams = \"" + params + "\"");
654671
if (code.equals("OK")){
655672
try {
656673
GPSReading currentLocation = new GPSReading(params);
@@ -660,11 +677,11 @@ public void onCommandResponse(AsyncMessage response) {
660677
showToast("Updated WiFire location", Toast.LENGTH_SHORT);
661678

662679
} catch (XmlPullParserException | IOException | ParseException e) {
663-
Log.e("CommandResponseHandler.onCommandResponse", "Error parsing GPS reading", e);
680+
Log.e("onCommandResponse", "Error parsing GPS reading", e);
664681
e.printStackTrace();
665682
}
666683
} else {
667-
Log.e("CommandResponseHandler.onCommandResponse", "Got response code" + code);
684+
Log.e("onCommandResponse", "Got response code" + code);
668685
}
669686

670687
}
@@ -724,8 +741,8 @@ public void execute() {
724741
flowHelper.postAsyncMessage(message);
725742

726743
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());
729746

730747

731748
}
@@ -785,7 +802,9 @@ public void run() {
785802
partialSplinePoints = new ArrayList<>();
786803
CatmullRomSpline c = new CatmullRomSpline(points, REPLAY_PATH_SUBSTEPS);
787804

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);
789808
}
790809
}
791810

@@ -834,10 +853,10 @@ public List<GPSReading> submit() {
834853
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
835854
String timeAgo = sdf.format(new Date(System.currentTimeMillis() - (maximumReadings * saveReadingPeriod)));
836855

837-
Log.v("fetchWiFireLocationHistory", "gpsReadingDatastore.getItemsByQuery(\"@gpsreadingtime >= '\" + " + timeAgo + "+ \"'\")");
856+
Log.v("fetchWiFi...History", "gpsReadingDatastore.getItemsByQuery(\"@gpsreadingtime >= '\" + " + timeAgo + "+ \"'\")");
838857
DataStoreItems gpsReadingDatastoreItems = gpsReadingDatastore.getItemsByQuery("@gpsreadingtime >= '" + timeAgo + "'");
839858

840-
Log.v("fetchWiFireLocationHistory", "Got " + gpsReadingDatastoreItems.size() + " entries from location history");
859+
Log.v("fetchWiFi...History", "Got " + gpsReadingDatastoreItems.size() + " entries from location history");
841860

842861
List<GPSReading> gpsReadings = new ArrayList<>(Math.min(maximumReadings, gpsReadingDatastoreItems.size()));
843862
int skip = 0;
@@ -849,6 +868,7 @@ public List<GPSReading> submit() {
849868
}
850869
}
851870

871+
Log.v("fetchWiFi...History", "Down to " + gpsReadings.size() + " / " + maximumReadings + " entries from location history");
852872

853873

854874
return gpsReadings;
@@ -1037,32 +1057,31 @@ public void onAsyncMessageResponse(MessagingEvent.AsyncMessageResponse response)
10371057
}
10381058
}
10391059
if (!success){
1060+
updateTimer.cancel();
10401061
ActivitiesAndFragmentsHelper.showFragmentChangeDialog(
10411062
R.string.wifire_connectivity_problems,
10421063
R.string.back_to_connected_devices,
10431064
(FlowActivity) activity,
10441065
SimpleFragmentFactory.createFragment(ConnectedDevicesFragment.TAG));
10451066
}
10461067
//online = success;
1047-
Log.v("InteractiveModeFragment.onAsyncMessageResponse", response.name() + " - " + response + " :: " + responseText);
1068+
Log.v("onAsyncMessageResponse", response.name() + " - " + response + " :: " + responseText);
10481069
}
10491070

10501071
@Override
10511072
public void onTextMessageReceived(AsyncMessage msg) {
1052-
Log.v("InteractiveModeFragment.onTextMessageReceived", msg.buildXml());
1073+
Log.v("onTextMessageReceived", msg.buildXml());
10531074
}
10541075

10551076
@Override
10561077
public void onCommandMessageReceived(AsyncMessage msg) {
1057-
Log.v("InteractiveModeFragment.onCommandMessageReceived", msg.buildXml());
10581078
}
10591079

10601080
@Override
10611081
public void onCommandRXMessageReceived(AsyncMessage msg) {
1062-
Log.v("InteractiveModeFragment.onCommandRXMessageReceived", "ID: " + msg.getNode("clientid") + " - " + msg.buildXml());
10631082
CommandResponseHandler resp = waitingCommands.get(msg.getNode("clientid"));
10641083
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"));
10661085
} else {
10671086
waitingCommands.remove(msg.getNode("clientid"));
10681087
if (getActivity() != null) resp.onCommandResponse(msg);
@@ -1077,6 +1096,7 @@ public void onCommandRXMessageReceived(AsyncMessage msg) {
10771096
@Override
10781097
public void onDevicePresenceChangeListener(boolean isConnected) {
10791098
if (!isConnected) {
1099+
updateTimer.cancel();
10801100
showConnectedDevicesFragmentDialog(SimpleFragmentFactory.createFragment(ConnectedDevicesFragment.TAG));
10811101
}
10821102
}

0 commit comments

Comments
 (0)
Please sign in to comment.