From a1b30d37e9d602073d303a2407310c0bba324ecd Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Thu, 10 Mar 2011 01:04:54 +0100 Subject: [PATCH] I12: Enviar lista de partidos subscritos --- src/com/onirica/carrousel/Configuration.java | 50 +++++++++++++++++--- src/com/onirica/carrousel/Match.java | 11 ++++- src/com/onirica/carrousel/Results.java | 34 +++++++++---- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/src/com/onirica/carrousel/Configuration.java b/src/com/onirica/carrousel/Configuration.java index 78b5100..0e432a1 100644 --- a/src/com/onirica/carrousel/Configuration.java +++ b/src/com/onirica/carrousel/Configuration.java @@ -1,6 +1,8 @@ package com.onirica.carrousel; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import android.app.Activity; import android.app.ListActivity; @@ -13,9 +15,11 @@ import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; +import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; +import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; @@ -24,7 +28,11 @@ import android.widget.TextView; public class Configuration extends ListActivity { private Results mResults; private Intent intent; + private ServiceConnection conn; private ProgressDialog progressDialog; + private HashSet subscribedMatches = new HashSet(); + private Button mSubscribeButton; + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { @@ -32,7 +40,7 @@ public class Configuration extends ListActivity { intent = new Intent(getBaseContext(), Results.class); startService(intent); setContentView(R.layout.main); - ServiceConnection conn = new ServiceConnection() { + conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { mResults = ((Results.LocalBinder)service).getService(); @@ -60,12 +68,36 @@ public class Configuration extends ListActivity { } }); progressDialog.show(); + Button b = (Button) findViewById(R.id.quit); + b.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + stopService(intent); + quit(); + } + }); + + mSubscribeButton = (Button) findViewById(R.id.subscribe); + mSubscribeButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + mResults.updateSubscriptions(subscribedMatches); + close(); + } + }); } + private void close() { + unbindService(conn); + finish(); + } + + private void quit() { + unbindService(conn); + stopService(intent); + finish(); + } + private void populateMatches() { - ArrayList matches = mResults.getMatches(); - Match[] ms = new Match[matches.size()]; - matches.toArray(ms); - MatchAdapter adapter = new MatchAdapter(ms); + HashMap matches = mResults.getMatches(); + MatchAdapter adapter = new MatchAdapter((Match[])matches.values().toArray(new Match[0])); setListAdapter(adapter); } @@ -138,7 +170,13 @@ public class Configuration extends ListActivity { } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - // Do the real stuff here. + Match m = (Match)getItem(mPosition); + if (m != null) { + if (isChecked) + subscribedMatches.remove(mMatches[mPosition].getId()); + else + subscribedMatches.add(mMatches[mPosition].getId()); + } } } diff --git a/src/com/onirica/carrousel/Match.java b/src/com/onirica/carrousel/Match.java index 7d87cfe..6790fc3 100644 --- a/src/com/onirica/carrousel/Match.java +++ b/src/com/onirica/carrousel/Match.java @@ -25,9 +25,11 @@ public class Match { public String visitorTeam; public State state; public int min; + public String id; public ArrayList goals; - public Match(String localTeam, String visitorTeam) { + public Match(String id, String localTeam, String visitorTeam) { + this.id = id; this.localTeam = localTeam; this.visitorTeam = visitorTeam; state = State.MATCH_NO_STARTED; @@ -35,7 +37,8 @@ public class Match { goals = new ArrayList(); } - public Match(String localTeam, String visitorTeam, State state, int min) { + public Match(String id, String localTeam, String visitorTeam, State state, int min) { + this.id = id; this.localTeam = localTeam; this.visitorTeam = visitorTeam; this.state = state; @@ -67,4 +70,8 @@ public class Match { return localTeam + " " + getLocalGoals() + " - " + getVisitorGoals() + " " + visitorTeam; } + public String getId() { + return id; + } + } diff --git a/src/com/onirica/carrousel/Results.java b/src/com/onirica/carrousel/Results.java index 611af6b..210b01d 100644 --- a/src/com/onirica/carrousel/Results.java +++ b/src/com/onirica/carrousel/Results.java @@ -1,6 +1,8 @@ package com.onirica.carrousel; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.Timer; import java.util.TimerTask; @@ -11,6 +13,16 @@ import android.os.IBinder; public class Results extends Service { private final IBinder mBinder = new LocalBinder(); + private HashMap mMatches = new HashMap(); + private HashSet mSubscriptions = null; + + public HashMap getMatches() { + return mMatches; + } + + public void updateSubscriptions(HashSet subscriptions) { + mSubscriptions = subscriptions; + } public class LocalBinder extends Binder { Results getService() { @@ -19,6 +31,8 @@ public class Results extends Service { } @Override public IBinder onBind(Intent arg0) { + if (mMatches.isEmpty()) + mMatches = retrieveMatches(); return mBinder; } @Override @@ -28,22 +42,24 @@ public class Results extends Service { timer.scheduleAtFixedRate(task, 0, 60000); } - public ArrayList getMatches() { - ArrayList matches = new ArrayList(); + private HashMap retrieveMatches() { + HashMap matches = new HashMap(); - Match match = new Match("Deportivo", "Atelico de Madrid"); - matches.add(match); + Match match = new Match("bbva1", "Deportivo", "Atelico de Madrid"); + matches.put(match.getId(), match); - match = new Match("Betis", "Sevilla"); - matches.add(match); + match = new Match("bbva2", "Betis", "Sevilla"); + matches.put(match.getId(), match); - match = new Match("Madrid", "Barcelona", Match.State.MATCH_FIRST_ROUND, 13); + match = new Match("bbva3", "Oviedo", "Sporting", Match.State.MATCH_SECOND_ROUND, 84); + + match = new Match("bbva13", "Madrid", "Barcelona", Match.State.MATCH_FIRST_ROUND, 13); Match.Goal goal = match.new Goal(12, true, "Cristiano Ronaldo"); match.addGoal(goal); - matches.add(match); + matches.put(match.getId(), match); return matches; - } +} private class pollingTask extends TimerTask { @Override