Android Open Source - final_year_frontend Match Activity






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.core;
/*from  w  w  w  .  ja  va2  s  .  c  om*/
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Timer;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.james.erebus.JSONJava.JSONArray;
import com.james.erebus.JSONJava.JSONException;
import com.james.erebus.JSONJava.JSONObject;
import com.james.erebus.misc.AppConsts;
import com.james.erebus.misc.MiscJsonHelpers;
import com.james.erebus.networking.GetMatchesTask;
import com.james.erebus.networking.MatchRetriever;
import com.james.erebus.networking.MatchSubscriptionManager;

/**
 * The java file for the Match activity, which is the screen that shows all of the matches currently available 
 * @author james
 *
 */

public class MatchActivity extends Activity implements MatchPreferencesFragment.NoticeDialogListener, OnClickListener {

  private static ArrayList<TournyMatchOptions> selectedItems;
  private JSONArray matches;
  private LinearLayout layout;


  
  public void confirmPrefs(View v) {
    DialogFragment newFragment = new MatchPreferencesFragment();
    newFragment.show(getFragmentManager(), "");
  }

  @Override
  public void onDialogPositiveClick(DialogFragment dialog) {
    // User touched the dialog's positive button
    setSelectedItems(MatchPreferencesFragment.getSelectedItems());
    try {
      displayMatches();
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onDialogNegativeClick(DialogFragment dialog) {
    // User touched the dialog's negative button
    //Do nothing
  }


  /**
   * 
   * @return The {@link java.util.ArrayList} of selected items from the Fragment
   */
  public static ArrayList<TournyMatchOptions> getSelectedItems()
  {
    return selectedItems;
  }

  /**
   * 
   * @param items The list of items to set the selectedItems field to
   */
  private void setSelectedItems( ArrayList<TournyMatchOptions> items)
  {
    selectedItems = items;
  }

  /**
   * Method called when the free text search button is pressed
   * @param v The current {@link android.view.View}
   * @throws URISyntaxException
   */
  public void freeTextSearch(View v) throws URISyntaxException
  {
    EditText et = (EditText) findViewById(com.james.erebus.R.id.searchTextMatches);
    ArrayList<String> searchWords = new ArrayList<String>(Arrays.asList(et.getText().toString().split(" ")));
    ArrayList<Button> buttons = new ArrayList<Button>();
    //final Context matchButtonContext = this;
    for(int i = 0; i < matches.length(); i++)
    {
      try {
        JSONObject obj = matches.getJSONObject(i);
        final String values = MiscJsonHelpers.getValuesFromJsonObject(obj);
        for(String s : searchWords)
        {
          if(values.contains(s))
          {
            Button newButton = new Button(this); //construct a button
            if(obj.getString("parentTournament").length() != 0) //some if/elses for setting the text
              newButton.setText(obj.getString("player1") + " vs " + obj.getString("player2") + ": " + obj.getString("parentTournament") + " (" + obj.getString("status") + ")");
            else
              newButton.setText(obj.getString("player1") + " vs " + obj.getString("player2") + "(" + obj.getString("status") + ")");
            newButton.setOnClickListener(this);
            newButton.setTag(obj);
            buttons.add(newButton);
          }
        }
      } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    layout.removeAllViews();
    for(Button newButton : buttons)
    {
      newButton.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
      layout.addView(newButton);
    }
  }

  /**
   * Method called when the refresh button is pressed
   * @param v The current {@link android.view.View}
   */
  public void refresh(View v)
  {
    getMatches(true);
    try {
      displayMatches();
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public void onResume()
  {
    AppConsts.currentActivity = this;
    super.onResume();
  }


  /**
   * Retrieves the list of {@link com.james.erebus.core.Match matches} 
   * @param forceRefresh This parameter controls whether the method goes to the cache or not
   */
  private void getMatches(boolean forceRefresh)
  {
    MatchRetriever m = new MatchRetriever();
    if(forceRefresh)
      try {
        matches = m.forceRetrieveFromServer(m.getURI(), m.getMatchesFilename());
      } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    else
      matches = m.retrieve(m.getURI(), m.getMatchesFilename());
  }

  /**
   * Displays the matches on the activity screen
   * @throws JSONException
   */
  private void displayMatches() throws JSONException
  {
    layout  = (LinearLayout) findViewById(com.james.erebus.R.id.matchButtonsLayout);
    if(matches == null)
    {
      TextView tv = new TextView(this);
      tv.setText("The app was unable to retrieve information from the server: please check you have a" +
          " valid internet connection, then try again.");
      layout.addView(tv);
      return;
    }

    ArrayList<TournyMatchOptions> matchOptions = getSelectedItems(); //get the filters that were selected
    layout.removeAllViews();
    ArrayList<Button> matchButtons = new ArrayList<Button>();
    for(int i = 0; i < matches.length(); i++) //for each match
    {
      JSONObject obj = (JSONObject) matches.get(i); //construct a json object for it
      Match match = MiscJsonHelpers.jsonToMatch(obj);
      //final String values = MiscJsonHelpers.getValuesFromJsonObject(obj);
      Button newButton = new Button(this); //construct a button
      if(obj.getString("parentTournament").length() != 0) //some if/elses for setting the text
        newButton.setText(obj.getString("player1") + " vs " + obj.getString("player2") + ": " + obj.getString("parentTournament") + " (" + obj.getString("status") + ")");
      else
        newButton.setText(obj.getString("player1") + " vs " + obj.getString("player2") + "(" + obj.getString("status") + ")");

      newButton.setOnClickListener(this);
      newButton.setTag(obj);
      MatchSubscriptionManager msm = new MatchSubscriptionManager();
      boolean toBeAdded = true;
      if(matchOptions == null) //if the user hasnt clicked the filter button yet
      {
        matchButtons.add(newButton);
        continue;
      }
      if(matchOptions.isEmpty()) // if the user deselected all of the filters
      {
        matchButtons.add(newButton);
        continue;
      }
      if(!match.getStatus().equals("future") && matchOptions.contains(TournyMatchOptions.future))
      {
        toBeAdded = false;
      }
      //if the user only wants matches in the past
      if(!match.getStatus().equals("past") && matchOptions.contains(TournyMatchOptions.past))
      {
        toBeAdded = false;
      }
      //if the user only wants matches that are ongoing 
      if(!match.getStatus().equals("ongoing") && matchOptions.contains(TournyMatchOptions.ongoing))
      {
        toBeAdded = false;
      }
      if(!msm.isMatchSubbed(this, match) && matchOptions.contains(TournyMatchOptions.subbed)) // if it's in the list, it's subbed to
      {
        toBeAdded = false;
      }
      if(msm.isMatchSubbed(this, match) && matchOptions.contains(TournyMatchOptions.unsubbed))  // if it's not in the list, it's not subbed to
      {
        toBeAdded = false;
      }
      if(toBeAdded)
        matchButtons.add(newButton);
    }
    for(Button newButton : matchButtons)
    {
      newButton.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
      layout.addView(newButton);
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
    super.onCreate(savedInstanceState);
    setContentView(com.james.erebus.R.layout.activity_match);
    this.setTitle("Matches");
    getMatches(false);
    try {
      displayMatches();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    long thirtyMinutesInMillis = 18000000;
    //long fiveminutesinmillis = 300000; //used for testing purposes
    long delayAndTimer = thirtyMinutesInMillis;
    GetMatchesTask task = new GetMatchesTask();
    Timer timer = new Timer("GetMatchesTimer");
    timer.schedule(task, delayAndTimer, delayAndTimer);
  }

  @Override
  public void onClick(View v) {
    JSONObject values = (JSONObject)v.getTag();
    Intent intent = new Intent(this, MatchButtonActivity.class);
    intent.putExtra("com.james.erebus.MatchButtonActivity.dataValues", values);
    startActivity(intent);
  }
}




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