Android Open Source - final_year_frontend Tournament Subscription Manager






From Project

Back to project page final_year_frontend.

License

The source code is released under:

MIT License

If you think the Android project final_year_frontend listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.james.erebus.networking;
//from   ww  w .  j  a v  a2  s.c o m
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;

import android.content.Context;
import android.util.Log;
import android.widget.Button;

import com.james.erebus.JSONJava.JSONArray;
import com.james.erebus.JSONJava.JSONException;
import com.james.erebus.JSONJava.JSONObject;
import com.james.erebus.core.Tournament;
import com.james.erebus.misc.MiscJsonHelpers;

/**
 * Child class of {@link com.james.erebus.networking.SubscriptionManager} for managing {@link com.james.erebus.core.Tournament}
 * subscriptions
 * @author james
 *
 */

public class TournamentSubscriptionManager extends SubscriptionManager {

  private static final String filename = "subbedTournaments.json";

  /**
   * Method to check if a {@link com.james.erebus.core.Tournament} is subscribed to
   * @param c The {@link android.content.Context} in which this method is invoked
   * @param t The Tournament being checked
   * @return True if the Tournament is subscribed to, false if not
   */
  public boolean isTournamentSubbed(Context c, Tournament t)
  {
    ArrayList<Tournament> tournaments = getSubbedTournaments(c);
    for(Tournament tournament : tournaments)
    {
      if(t.equalsTournament(tournament))
        return true;
    }
    return false;
  }

  /**
   * 
   * @param c The {@link android.content.Context} in which this method is invoked
   * @return an {@link java.util.ArrayList} of subscribed {@link com.james.erebus.core.Tournament tournaments}
   */
  public ArrayList<Tournament> getSubbedTournaments(Context c)
  {
    JSONArray ja = readSubbed(c, "subbedTournaments.json");
    if(ja != null)
    {
      ArrayList<Tournament> tournaments = new ArrayList<Tournament>();
      for(int i = 0; i < ja.length(); i++)
      {
        try {
          tournaments.add(MiscJsonHelpers.jsonToTournament((JSONObject) ja.get(i)));
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
      return tournaments;
    }
    else
    {
      return new ArrayList<Tournament>();
    }
  }

  /**
   * Subscribes to a {@link com.james.erebus.core.Tournament} both internally and also calls
   * the method to send this subscription to the server
   * @param t The Tournament to be subscribed to
   * @param c The {@link android.content.Context} in which this method is invoked
   * @param b The {@link android.widget.Button} which was pressed to invoke this method
   * @return True if the Tournament was subscribed to successfully, false if not
   * @throws IOException
   * @throws JSONException
   */
  public boolean subToTournament(Tournament t, Context c, Button b) throws IOException, JSONException
  {
    JSONArray ja = readSubbed(c, "subbedTournaments.json");
    if(ja != null)
    {
      ArrayList<Tournament> subbedTournaments = new ArrayList<Tournament>();
      for(int i = 0; i < ja.length(); i++)
      {
        try {
          subbedTournaments.add(MiscJsonHelpers.jsonToTournament((JSONObject) ja.get(i)));
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
      for(int i = 0; i < subbedTournaments.size(); i++)
      {
        if(subbedTournaments.get(i).equalsTournament(t))
        {
          return false;
        }
      }
    }
    else
      ja = new JSONArray();
    ja.put(MiscJsonHelpers.tournamentToJson(t));
    writeSubbed(c, ja, "subbedTournaments.json");
    addTournamentSubscriptionToServer(MiscNetworkingHelpers.regId, Integer.toString(t.getId()), b, t, c);
    return false;
  }

  /**
   * Compare
   * @param c - The context in which this method is invoked
   * @return - The ArrayList of updated tournaments if it's newer, else null
   */
  /*
  public ArrayList<Tournament> compareSubbedTournaments(Context c)
  {
    ArrayList<Tournament> subbedTournaments = getSubbedTournaments(c);
    TournamentRetriever tr = new TournamentRetriever();
    JSONArray ja = tr.retrieve(tr.getURI(), tr.getTournamentsFilename());
    if(ja == null)
      return null;
    ArrayList<Tournament> updatedTournaments = (ArrayList<Tournament>) MiscJsonHelpers.jsonTournamentArrayToTournamentList(ja);
    ArrayList<Tournament> changedTournaments = new ArrayList<Tournament>();
    JSONArray newSubbedJa = new JSONArray();
    boolean shouldAdd;
    for(Tournament t : subbedTournaments)
    {
      shouldAdd = true;
      for(Tournament t2 : updatedTournaments)
      {
        if(t.equalsTournament(t2))
        {
          if(t.isDifferentTo(t2))
          {
            Log.i("l54:tournamentsubmanager", "added changed tournament");
            changedTournaments.add(t2);
            newSubbedJa.put(MiscJsonHelpers.tournamentToJson(t2));
            shouldAdd = false;
            break;
          }
        }
      }
      if(shouldAdd)
        newSubbedJa.put(MiscJsonHelpers.tournamentToJson(t));
    }
    writeSubbed(c, newSubbedJa, filename);
    return changedTournaments;
  }
  */
  
  /**
   * Unsubscribes from a {@link com.james.erebus.core.Tournament} both internally and also calls
   * the method to send this subscription to the server
   * @param c The {@link android.content.Context} in which this method is invoked
   * @param t The Tournament to be unsubscribed from
   * @param b The {@link android.widget.Button} which was pressed to invoke this method
   * @return True if the Tournament was unsubscribed from successfully, false if not
   */
  public boolean unsubFromTournament(Context c, Tournament t, Button b)
  {
    boolean retVal = false;
    JSONArray ja = readSubbed(c, "subbedTournaments.json");

    ArrayList<Tournament> subbedTournaments = new ArrayList<Tournament>();
    for(int i = 0; i < ja.length(); i++)
    {
      try {
        subbedTournaments.add(MiscJsonHelpers.jsonToTournament((JSONObject) ja.get(i)));
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    JSONArray returnJa = new JSONArray();
    for(int i = 0; i < subbedTournaments.size(); i++)
    {
      if(subbedTournaments.get(i).equalsTournament(t))
      {
        retVal = true;
        //this is the tournament we want to remove, so don't add it
      }
      else
        returnJa.put(MiscJsonHelpers.tournamentToJson(subbedTournaments.get(i)));
    }
    try {
      removeTournamentSubscriptionToServer(MiscNetworkingHelpers.regId, Integer.toString(t.getId()), b);
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    writeSubbed(c, returnJa, "subbedTournaments.json");
    return retVal;
  }

  /**
   * Adds a {@link com.james.erebus.core.Tournament} subscription to the server
   * @param regId The registration ID for this app
   * @param tournamentEntryId The Tournament id
   * @param b The {@link android.widget.Button} that was pressed to invoke this method
   * @param tournament The Tournament being subscribed to
   * @param c The {@link android.content.Context} in which this method was invoked
   */
  private void addTournamentSubscriptionToServer(final String regId, final String tournamentEntryId, Button b, Tournament tournament, Context c)
  {
    AddTournamentSubscriptionToServerTask task = new AddTournamentSubscriptionToServerTask();
    task.setContext(c);
    task.setTournament(tournament);
    task.setRegId(regId);
    task.setButton(b);
    task.setTournamentEntryId(tournamentEntryId);
    Timer t = new Timer("AddTournamentSubscriptionToServerTimer");
    t.schedule(task, Calendar.getInstance().getTime());
  }

  /**
   * Removes a {@link com.james.erebus.core.Tournament} subscription from the server
   * @param regId The registration ID for this app
   * @param tournamentEntryId The Tournament id
   * @param b The {@link android.widget.Button} that was pressed to invoke this method
   * @throws UnknownHostException
   */
  private void removeTournamentSubscriptionToServer(final String regId, final String tournamentEntryId, Button b) throws UnknownHostException
  {
    SubscriptionRetriever sr = new SubscriptionRetriever();
    JSONArray subs = sr.forceRetrieveFromServer(sr.getURI(), sr.getSubscriptionsFilename());
    if(subs != null)
    {
      for(int i = 0; i < subs.length(); i++)
      {
        try {
          JSONObject obj = (JSONObject) subs.get(i);
          String tournamentEntryIdRetrieved = Integer.toString(obj.getInt("model_id")).toLowerCase();
          if(tournamentEntryIdRetrieved != "null")
          {
            if(tournamentEntryIdRetrieved.equals(tournamentEntryId))
            {
              RemoveTournamentSubscriptionFromServerTask task = new RemoveTournamentSubscriptionFromServerTask();
              task.setButton(b);
              task.setRegId(regId);
              task.setJsonTournamentObject(obj);
              Timer t = new Timer("RemoveTournamentSubscriptionFromServerTimer");
              t.schedule(task, Calendar.getInstance().getTime());
              Log.v("removetournysub", "set task to remove tourny sub");
            }
          }
        } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}




Java Source Code List

com.james.erebus.GCMIntentService.java
com.james.erebus.JSONJava.CDL.java
com.james.erebus.JSONJava.CookieList.java
com.james.erebus.JSONJava.Cookie.java
com.james.erebus.JSONJava.HTTPTokener.java
com.james.erebus.JSONJava.HTTP.java
com.james.erebus.JSONJava.JSONArray.java
com.james.erebus.JSONJava.JSONException.java
com.james.erebus.JSONJava.JSONML.java
com.james.erebus.JSONJava.JSONObject.java
com.james.erebus.JSONJava.JSONString.java
com.james.erebus.JSONJava.JSONStringer.java
com.james.erebus.JSONJava.JSONTokener.java
com.james.erebus.JSONJava.JSONWriter.java
com.james.erebus.JSONJava.XMLTokener.java
com.james.erebus.JSONJava.XML.java
com.james.erebus.core.C2DMRegistrationReceiver.java
com.james.erebus.core.CustomOnItemSelectedListener.java
com.james.erebus.core.MainActivity.java
com.james.erebus.core.MatchActivity.java
com.james.erebus.core.MatchButtonActivity.java
com.james.erebus.core.MatchOptions.java
com.james.erebus.core.MatchPreferencesFragment.java
com.james.erebus.core.Match.java
com.james.erebus.core.NotificationActivity.java
com.james.erebus.core.Notification.java
com.james.erebus.core.ParentPreferencesFragment.java
com.james.erebus.core.TournamentActivity.java
com.james.erebus.core.TournamentButtonActivity.java
com.james.erebus.core.TournamentFactory.java
com.james.erebus.core.TournamentPreferencesFragment.java
com.james.erebus.core.Tournament.java
com.james.erebus.core.TournyMatchOptions.java
com.james.erebus.misc.AppConsts.java
com.james.erebus.misc.MiscJsonHelpers.java
com.james.erebus.misc.misc.java
com.james.erebus.networking.AddDeviceTask.java
com.james.erebus.networking.AddMatchSubscriptionTask.java
com.james.erebus.networking.AddTournamentSubscriptionToServerTask.java
com.james.erebus.networking.GcmRegisterDeviceTask.java
com.james.erebus.networking.GetMatchesTask.java
com.james.erebus.networking.GetTournamentsTask.java
com.james.erebus.networking.MatchRetriever.java
com.james.erebus.networking.MatchSubscriptionManager.java
com.james.erebus.networking.MiscNetworkingHelpers.java
com.james.erebus.networking.NotificationManager.java
com.james.erebus.networking.RemoveMatchSubscriptionFromServerTask.java
com.james.erebus.networking.RemoveTournamentSubscriptionFromServerTask.java
com.james.erebus.networking.Retriever.java
com.james.erebus.networking.SubscriptionManager.java
com.james.erebus.networking.SubscriptionRetriever.java
com.james.erebus.networking.TournamentRetriever.java
com.james.erebus.networking.TournamentSubscriptionManager.java