Android Open Source - final_year_frontend G C M Intent Service






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;
//w  ww. ja va 2  s.  c  o m

import java.util.Calendar;
import java.util.Timer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import com.google.android.gcm.GCMBaseIntentService;
import com.james.erebus.core.Match;
import com.james.erebus.core.Notification;
import com.james.erebus.core.Tournament;
import com.james.erebus.misc.AppConsts;
import com.james.erebus.networking.AddDeviceTask;
import com.james.erebus.networking.MiscNetworkingHelpers;
import com.james.erebus.networking.NotificationManager;

import android.content.Context;
import android.content.Intent;
import android.util.Log;


/**
 * 
 * @author james
 * This class provides methods to deal with GCM messaging and registering all of the methods in this class are
 * triggered by events and are never called by code in the project
 */
public class GCMIntentService extends GCMBaseIntentService{

  public GCMIntentService()
  {
    super("585651294813");
  }

  @Override
  protected void onError(Context context, String errorId) {
    Log.v("onError", "error!");

  }


  /**
   * Used to convert a GCM notification to a {@link com.james.erebus.core.Match}
   * @param contents The contents of the GCM notification
   * @return A match built from the contents of the GCM notification
   */
  private Match convertGcmNotificationToMatch(String contents)
  {
    String[] values = contents.split(",\"");
    Match m = new Match();
    for(int i = 0; i < values.length; i++)
    {
      String value = values[i];
      
      Pattern patt = Pattern.compile(".*\\{");
      Matcher matcher = patt.matcher(value);
      value = matcher.replaceAll("");;
      patt = Pattern.compile("\\}.*");
      matcher = patt.matcher(value);
      value = matcher.replaceAll("");
      String[] keyvalue = value.split("\":");
      patt = Pattern.compile("\"");
      matcher = patt.matcher(keyvalue[0]);
      keyvalue[0] = matcher.replaceAll("");
      matcher = patt.matcher(keyvalue[1]);
      keyvalue[1] = matcher.replaceAll("");
      
      if(keyvalue[0].equals("date"))
        m.setDate(keyvalue[1]);
      if(keyvalue[0].equals("id"))
        m.setId(Integer.parseInt(keyvalue[1]));
      if(keyvalue[0].equals("links"))
        m.setLinks(keyvalue[1]);  
      if(keyvalue[0].equals("parentTournament"))
        m.setParentTourny(keyvalue[1]);
      if(keyvalue[0].equals("player1"))
        m.setPlayer1(keyvalue[1]);
      if(keyvalue[0].equals("player2"))
        m.setPlayer2(keyvalue[1]);
      if(keyvalue[0].equals("status"))
        m.setStatus(keyvalue[1]);
      if(keyvalue[0].equals("time"))
        m.setTime(keyvalue[1]);    
    }
    Log.v("match", m.toString());
    return m;
  }

  /**
   * 
   * @param contents the contents of the notification
   * @return A {@link com.james.erebus.core.Tournament} built from the contents of the notification
   */
  private Tournament convertGcmNotificationToTournament(String contents)
  {
    String[] values = contents.split(",\"");
    Tournament t = new Tournament();
    for(int i = 0; i < values.length; i++)
    {
      String value = values[i];
      Pattern patt = Pattern.compile(".*\\{");
      Matcher m = patt.matcher(value);
      value = m.replaceAll("");;
      patt = Pattern.compile("\\}.*");
      m = patt.matcher(value);
      value = m.replaceAll("");
      String[] keyvalue = value.split("\":");
      patt = Pattern.compile("\"");
      m = patt.matcher(keyvalue[0]);
      keyvalue[0] = m.replaceAll("");
      m = patt.matcher(keyvalue[1]);
      keyvalue[1] = m.replaceAll("");
      if(keyvalue[0].equals("entry_reqs"))
        t.setEntryReqs(keyvalue[1]);
      if(keyvalue[0].equals("format"))
        t.setFormat(keyvalue[1]);
      if(keyvalue[0].equals("future"))
        t.setFuture(keyvalue[1]);  
      if(keyvalue[0].equals("id"))
        t.setId(Integer.parseInt(keyvalue[1]));
      if(keyvalue[0].equals("links"))
        t.setLinks(keyvalue[1]);
      if(keyvalue[0].equals("location"))
        t.setLocation(keyvalue[1]);
      if(keyvalue[0].equals("name"))
        t.setName(keyvalue[1]);
      if(keyvalue[0].equals("ongoing"))
        t.setOngoing(keyvalue[1]);
      if(keyvalue[0].equals("past"))
        t.setPast(keyvalue[1]);
      if(keyvalue[0].equals("prizes"))
        t.setPrizes(keyvalue[1]);
      if(keyvalue[0].equals("sponsor"))
        t.setSponsor(keyvalue[1]);
      if(keyvalue[0].equals("start_date"))
        t.setStartDate(keyvalue[1]);      
    }
    Log.v("tournament", t.toString());
    return t;
  }

  @Override
  protected void onMessage(Context context, Intent intent) {
    String message = intent.getExtras().toString();
    Log.v("onMessage", "content: " + message);
    
    if(message.contains("new_match_data_available"))
    {
      Match m = convertGcmNotificationToMatch(message);
      Log.v("converted match", m.toString());
      NotificationManager.addNotification(new Notification("The match " + m.getPlayer1() + " vs " + m.getPlayer2() + 
          " has changed. Press 'view' to view it, or 'clear' to clear this notification", m));
    }
    else if(message.contains("new_tournament_data_available"))
    {
      Tournament t = convertGcmNotificationToTournament(message);
      Log.v("converted tourny", t.toString());
      NotificationManager.addNotification(new Notification("The tournament " + t.getName() + " has changed. " +
          "Press 'view' to view it, or 'clear' to clear this notification", t));
    }
  }

  @Override
  protected void onUnregistered(Context context, String regId) {
    Log.v("onUnregistered", "unregistered now!");

  }

  @Override
  protected void onRegistered(Context context, String regId) {
    //send reg id to server
    Log.v("onRegistered", "send regId to server");
    addDeviceToServer(regId);
    MiscNetworkingHelpers.regId = regId;
  }


  private void addDeviceToServer(final String regId)
  {
    AddDeviceTask task = new AddDeviceTask();
    AddDeviceTask.setContext(AppConsts.currentActivity);
    task.setRegId(regId);
    Timer t = new Timer("AddDeviceTimer");
    t.schedule(task, Calendar.getInstance().getTime());
  }
}




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