1
0
mirror of https://github.com/balkian/Carrousel-Android.git synced 2024-12-23 05:28:13 +00:00
Carrousel-Android/src/com/onirica/carrousel/Configuration.java

206 lines
6.6 KiB
Java
Raw Normal View History

2011-03-09 21:41:31 +00:00
package com.onirica.carrousel;
2011-03-09 22:33:35 +00:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
2011-03-09 22:33:35 +00:00
2011-03-09 21:41:31 +00:00
import android.app.Activity;
2011-03-09 22:56:52 +00:00
import android.app.ListActivity;
2011-03-09 22:41:02 +00:00
import android.app.ProgressDialog;
import android.content.ComponentName;
2011-03-09 23:33:46 +00:00
import android.content.Context;
2011-03-09 22:41:02 +00:00
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
2011-03-09 21:41:31 +00:00
import android.os.Bundle;
import android.os.IBinder;
2011-03-09 23:33:46 +00:00
import android.view.View;
import android.view.View.OnClickListener;
2011-03-09 23:33:46 +00:00
import android.view.ViewGroup;
2011-03-09 22:56:52 +00:00
import android.widget.ArrayAdapter;
2011-03-09 23:33:46 +00:00
import android.widget.BaseAdapter;
import android.widget.Button;
2011-03-09 23:33:46 +00:00
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
2011-03-09 21:41:31 +00:00
2011-03-09 22:56:52 +00:00
public class Configuration extends ListActivity {
private Results mResults;
2011-03-09 22:33:35 +00:00
private Intent intent;
private ServiceConnection conn;
2011-03-09 22:41:02 +00:00
private ProgressDialog progressDialog;
private HashSet<String> subscribedMatches = new HashSet<String>();
private Button mSubscribeButton;
2011-03-09 21:41:31 +00:00
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2011-03-09 22:33:35 +00:00
intent = new Intent(getBaseContext(), Results.class);
startService(intent);
2011-03-09 23:05:11 +00:00
setContentView(R.layout.main);
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
mResults = ((Results.LocalBinder)service).getService();
2011-03-10 00:28:48 +00:00
subscribedMatches = mResults.getSubscriptions();
if (subscribedMatches == null) {
subscribedMatches = new HashSet<String>();
} else
mSubscribeButton.setEnabled(true);
2011-03-09 22:33:35 +00:00
populateMatches();
2011-03-09 22:41:02 +00:00
progressDialog.dismiss();
}
@Override
public void onServiceDisconnected(ComponentName className) {
mResults = null;
2011-03-09 22:33:35 +00:00
stopService(intent);
finish();
}
};
bindService(intent, conn, BIND_AUTO_CREATE);
2011-03-09 22:41:02 +00:00
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Retrieving match list");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
//Log.i(TAG, "dialog cancel has been invoked");
stopService(intent);
finish();
}
});
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();
2011-03-09 21:41:31 +00:00
}
private void quit() {
unbindService(conn);
stopService(intent);
finish();
}
2011-03-09 22:33:35 +00:00
private void populateMatches() {
HashMap<String, Match> matches = mResults.getMatches();
MatchAdapter adapter = new MatchAdapter((Match[])matches.values().toArray(new Match[0]));
2011-03-09 23:33:46 +00:00
setListAdapter(adapter);
}
private class MatchView extends LinearLayout {
private TextView mTv;
private CheckBox mCb;
private CompoundButton.OnCheckedChangeListener mListener = null;
2011-03-10 00:28:48 +00:00
public MatchView(Context context, String text, boolean isChecked) {
2011-03-09 23:33:46 +00:00
super(context);
this.setOrientation(HORIZONTAL);
mTv = new TextView(context);
mTv.setText(text);
mCb = new CheckBox(context);
2011-03-10 00:28:48 +00:00
setChecked(isChecked);
2011-03-09 23:33:46 +00:00
mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mListener != null) {
mListener.onCheckedChanged(buttonView, isChecked); }
}
});
this.addView(mTv, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
this.addView(mCb, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0));
}
public void setText(String text) {
mTv.setText(text);
}
2011-03-10 00:28:48 +00:00
public void setChecked(boolean isChecked) {
mCb.setChecked(isChecked);
}
2011-03-09 23:33:46 +00:00
public void setOnMatchCheckedChanged(CompoundButton.OnCheckedChangeListener listener) {
mListener = listener;
}
}
private class MatchAdapter extends BaseAdapter {
private Match mMatches[];
public MatchAdapter(Match matches[]) {
super();
mMatches = matches;
}
@Override
public int getCount() {
return mMatches.length;
}
@Override
public Object getItem(int pos) {
return (Object)mMatches[pos];
}
@Override
public long getItemId(int pos) {
return pos;
}
2011-03-10 00:28:48 +00:00
@Override
2011-03-09 23:33:46 +00:00
public View getView(int pos, View convertView, ViewGroup parent) {
MatchView v;
2011-03-10 00:28:48 +00:00
Match match = mMatches[pos];
boolean isSubscribed = subscribedMatches.contains(match.getId());
2011-03-09 23:33:46 +00:00
if (convertView == null) {
2011-03-10 00:28:48 +00:00
; v = new MatchView(parent.getContext(), match.toString(), isSubscribed);
2011-03-09 23:33:46 +00:00
v.setOnMatchCheckedChanged(new OnMatchCheckedListener(pos));
} else {
v = (MatchView)convertView;
2011-03-10 00:28:48 +00:00
v.setText(match.toString());
// This is tricky: We are reusing the view for a different match.
// We need to update the view checkbox state and we use setChecked,
// but this would trigger the previous event listener, clearing
// the model of the previous item shown with this view.
v.setOnMatchCheckedChanged(null);
v.setChecked(isSubscribed);
v.setOnMatchCheckedChanged(new OnMatchCheckedListener(pos));
2011-03-09 23:33:46 +00:00
}
return v;
}
private class OnMatchCheckedListener implements CompoundButton.OnCheckedChangeListener {
private int mPosition;
OnMatchCheckedListener(int position){
mPosition = position;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Match m = (Match)getItem(mPosition);
if (m != null) {
if (isChecked)
subscribedMatches.add(mMatches[mPosition].getId());
2011-03-10 00:28:48 +00:00
else
subscribedMatches.remove(mMatches[mPosition].getId());
mSubscribeButton.setEnabled(!subscribedMatches.isEmpty());
}
2011-03-09 23:33:46 +00:00
}
}
2011-03-09 22:33:35 +00:00
}
2011-03-09 21:41:31 +00:00
}