Ask server if race is tracked and allow map display if tracked

This commit is contained in:
Bennet Brunsen committed 2015-07-16 16:56:29 +02:00
1 parent c287b20c17
commit 9055c6cb5c
3 files changed
+120

No files matched your search

@@ -72,4 +72,7 @@ public class AppConstants {
public final static String INTENT_ACTION_SHOW_SUMMARY_CONTENT = PACKAGE_NAME + ".action.show.summary";
public final static String INTENT_ACTION_SHOW_PROTEST = PACKAGE_NAME + ".action.show.protest";
public final static String INTENT_ACTION_IS_TRACKING = PACKAGE_NAME + "action.is.tracking";
public final static String INTENT_ACTION_IS_TRACKING_EXTRA = PACKAGE_NAME + "action.is.tracking.extra";
}
@@ -2,10 +2,15 @@ package com.sap.sailing.racecommittee.app.ui.fragments.raceinfo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -40,6 +45,7 @@ import com.sap.sailing.racecommittee.app.ui.utils.OnRaceUpdatedListener;
import com.sap.sailing.racecommittee.app.ui.views.CompassView;
import com.sap.sailing.racecommittee.app.ui.views.CompassView.CompassDirectionListener;
import com.sap.sailing.racecommittee.app.utils.ThemeHelper;
import com.sap.sailing.racecommittee.app.utils.WindHelper;
import com.sap.sse.common.Util;
import com.sap.sse.common.impl.MillisecondsTimePoint;
@@ -125,6 +131,10 @@ public class WindFragment extends BaseFragment
// refresh ui
mRefreshUIRunnable = createUIRefreshRunnable();
//register receiver to be notified if race is tracked
IntentFilter filter = new IntentFilter(AppConstants.INTENT_ACTION_IS_TRACKING);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(new IsTrackedReceiver(), filter);
}
private Runnable createUIRefreshRunnable() {
@@ -278,6 +288,8 @@ public class WindFragment extends BaseFragment
});
}
if (mContentMapShow != null) {
// disable functionality at first. will be enabled after contacting the server if race is tracked
mContentMapShow.setEnabled(false);
mContentMapShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@@ -409,6 +421,8 @@ public class WindFragment extends BaseFragment
tearUpPositionPoller();
sendIntent(AppConstants.INTENT_ACTION_TIME_HIDE);
// Contact server and ask if race is tracked and map is allowed to show.
WindHelper.isTrackedRace(getActivity(), getRace());
}
@Override
@@ -496,4 +510,13 @@ public class WindFragment extends BaseFragment
preferences.setWindSpeed(wind.getKnots());
}
private class IsTrackedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mContentMapShow.setEnabled(intent.getBooleanExtra(AppConstants.INTENT_ACTION_IS_TRACKING_EXTRA, false));
}
}
}
@@ -0,0 +1,94 @@
package com.sap.sailing.racecommittee.app.utils;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.sap.sailing.android.shared.data.http.HttpGetRequest;
import com.sap.sailing.android.shared.util.NetworkHelper;
import com.sap.sailing.racecommittee.app.AppConstants;
import com.sap.sailing.racecommittee.app.AppPreferences;
import com.sap.sailing.racecommittee.app.data.DataManager;
import com.sap.sailing.racecommittee.app.data.DataStore;
import com.sap.sailing.racecommittee.app.domain.ManagedRace;
import com.sap.sailing.racecommittee.app.domain.impl.FleetIdentifierImpl;
import com.sap.sse.common.Util;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.MalformedURLException;
import java.net.URL;
public class WindHelper {
public static void isTrackedRace(final Context context, final ManagedRace race){
try {
Util.Triple<String, String, String> triple = FleetIdentifierImpl.unescape(race.getId());
StringBuilder builder = new StringBuilder();
builder.append(getBaseUrl(context));
builder.append("/sailingserver/api/v1/events/");
builder.append(getEventId(context));
builder.append("/racestates");
builder.append("?filterByLeaderboard=" + triple.getA());
URL serverUrl = new URL(builder.toString());
HttpGetRequest request = new HttpGetRequest(serverUrl, context);
NetworkHelper.getInstance(context).executeHttpJsonRequestAsnchronously(request, new NetworkHelper.NetworkHelperSuccessListener() {
@Override
public void performAction(JSONObject response) {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(context);
Intent notifyTrackedIntent = new Intent();
notifyTrackedIntent.putExtra(AppConstants.INTENT_ACTION_IS_TRACKING_EXTRA, checkResponseForTracking(response, race));
notifyTrackedIntent.setAction(AppConstants.INTENT_ACTION_IS_TRACKING);
manager.sendBroadcast(notifyTrackedIntent);
}
}, new NetworkHelper.NetworkHelperFailureListener() {
@Override
public void performAction(NetworkHelper.NetworkHelperError e) {
// TODO: Error handling
}
});
}
catch (MalformedURLException e){
// TODO: Error handling
}
}
private static String getBaseUrl(Context context){
return AppPreferences.on(context).getServerBaseURL();
}
private static String getEventId(Context context){
DataStore dataStore = DataManager.create(context).getDataStore();
return "" + dataStore.getEventUUID();
}
private static boolean checkResponseForTracking(JSONObject response, ManagedRace race){
boolean isTracked = false;
Util.Triple<String, String, String> triple = FleetIdentifierImpl.unescape(race.getId());
try {
JSONArray raceStates = response.getJSONArray("raceStates");
for(int index = 0; index < raceStates.length(); index++){
JSONObject raceState = raceStates.getJSONObject(index);
String leaderboardName = raceState.getString("leaderboardName");
String fleetName = raceState.getString("fleetName");
String raceName = raceState.getString("raceName");
boolean matchingLeaderboardName = leaderboardName != null && leaderboardName.equals(triple.getA());
boolean matchingFleetName = fleetName != null && fleetName.equals(triple.getC());
boolean matchingRaceName = raceName != null && raceName.equals(race.getRaceName());
if (matchingLeaderboardName && matchingFleetName && matchingRaceName){
String trackedRaceId = raceState.getString("trackedRaceId");
if (trackedRaceId != null){
isTracked = true;
break;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
// TODO: Error handling e.g. logging
}
return isTracked;
}
}