Android Open Source - final_year_frontend Add Match Subscription Task






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;
/*  www.j  av a 2 s  .com*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.message.BasicNameValuePair;


import android.app.AlertDialog;
import android.util.Log;
import android.widget.Button;

/**
 * A {@link java.util.TimerTask} for adding a {@link com.james.erebus.core.Match} subscription to the server
 * @author james
 *
 */

public class AddMatchSubscriptionTask extends TimerTask{

  private static String regId;
  private static String matchEntryId;
  private static boolean success;
  private static Button b;
  private static int failures;
  
  private static ArrayList<AlertDialog> dialogs = new ArrayList<AlertDialog>();

  @SuppressWarnings({ "serial", "deprecation" })
  @Override
  public void run() {
    try {
      success = MiscNetworkingHelpers.postInformationToServer(regId, "subscriptions.json", 
          new ArrayList<BasicNameValuePair>() {{ 
            add(new BasicNameValuePair("subscription[device_registration_id]", regId));
            add(new BasicNameValuePair("subscription[model_id]", matchEntryId));
            add(new BasicNameValuePair("subscription[model_type]", "MatchEntry"));
          }});
      if(success)
      {
        
        //MatchSubscriptionManager msm = new MatchSubscriptionManager();
        //msm.subToMatch(AddMatchSubscriptionTask.match, AddMatchSubscriptionTask.context, AddMatchSubscriptionTask.b);
        for(AlertDialog retryDialog : AddMatchSubscriptionTask.dialogs)
        {
          retryDialog.dismiss();
        }
        MiscNetworkingHelpers.handler.post(new Runnable() {

          @Override
          public void run() {
            AddMatchSubscriptionTask.b.setText("Subscribed");
            AddMatchSubscriptionTask.b.setEnabled(true);
            AddMatchSubscriptionTask.b.setClickable(true);
          }  });
      }
    } 
    catch(HttpHostConnectException e)
    {
      if(failures < 2)
      {
        Log.e("AddMatchSubscriptionTask", "Failed to add match, re-adding...");
        AddMatchSubscriptionTask task = new AddMatchSubscriptionTask();
        Timer t = new Timer("AddMatchSubscriptionTimer");
        Date d = Calendar.getInstance().getTime();
        d.setSeconds(d.getSeconds() + 10);
        t.schedule(task, d);
        MiscNetworkingHelpers.handler.post(new Runnable() {

          @Override
          public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(b.getContext());
            builder.setMessage("No connection to the server - retrying")
            .setTitle("Connection error");
            AlertDialog dialog = builder.create();
            dialogs.add(dialog);
            dialog.show();
          }  });
        failures++;
      }
      else
      {
        
        MiscNetworkingHelpers.handler.post(new Runnable() {

          @Override
          public void run() {
            b.setEnabled(true);
            b.setClickable(true);
            AlertDialog.Builder builder = new AlertDialog.Builder(b.getContext());
            builder.setMessage("No connection to the server - please check your wireless is on and connected to a network")
            .setTitle("Connection error");
            AlertDialog dialog = builder.create();
            for(AlertDialog retryDialog : dialogs)
            {
              retryDialog.dismiss();
            }
            dialog.show();
          }  });
        failures = 0;
      }

    } catch(IOException e)
    {
      MiscNetworkingHelpers.handler.post(new Runnable() {

        @Override
        public void run() {
          b.setClickable(true);
          b.setEnabled(true);
        }  });
      Log.e("AddMatchSubscriptionTask", "io exception happened :(");
      e.printStackTrace();
    }
    catch (Exception e) {
      MiscNetworkingHelpers.handler.post(new Runnable() {

        @Override
        public void run() {
          b.setClickable(true);
          b.setEnabled(true);
        }  });
      Log.e("AddMatchSubscriptionTask", "other exception happened :(");
      e.printStackTrace();
    }
  }

  /**
   * Sets the {@link android.widget.Button} reference to be used
   * @param b The Button reference to set the Button field to
   */
  public void setButton(Button b)
  {
    AddMatchSubscriptionTask.b = b;    
  }

  /**
   * Sets the registration id
   * @param regId The String to set the registration id to
   */
  public void setRegId(String regId)
  {
    AddMatchSubscriptionTask.regId = regId;
  }

  /**
   * Sets the match id that is being subscribed to
   * @param matchEntryId The String to set the match id to
   */
  public void setMatchEntryId(String matchEntryId)
  {
    AddMatchSubscriptionTask.matchEntryId = matchEntryId;
  }

}




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