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

184 lines
5.7 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-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;
public MatchView(Context context, String text) {
super(context);
this.setOrientation(HORIZONTAL);
mTv = new TextView(context);
mTv.setText(text);
mCb = new CheckBox(context);
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);
}
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;
}
public View getView(int pos, View convertView, ViewGroup parent) {
MatchView v;
if (convertView == null) {
v = new MatchView(parent.getContext(), mMatches[pos].toString());
v.setOnMatchCheckedChanged(new OnMatchCheckedListener(pos));
} else {
v = (MatchView)convertView;
v.setText(mMatches[pos].toString());
}
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.remove(mMatches[mPosition].getId());
else
subscribedMatches.add(mMatches[mPosition].getId());
}
2011-03-09 23:33:46 +00:00
}
}
2011-03-09 22:33:35 +00:00
}
2011-03-09 21:41:31 +00:00
}