MultiSmsSender/src/com/hectorone/multismssender/PhoneNumberSelection.java

230 lines
6.5 KiB
Java
Raw Normal View History

2010-12-12 02:36:52 +01:00
package com.hectorone.multismssender;
2009-10-22 14:26:02 +02:00
import java.util.HashSet;
2010-12-12 02:36:52 +01:00
2009-10-22 14:26:02 +02:00
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
2009-10-22 14:26:02 +02:00
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
2009-10-22 14:26:02 +02:00
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
public class PhoneNumberSelection extends ListActivity
{
PhoneDataListAdapter mAdpater;
2010-01-22 15:21:33 +01:00
2009-10-22 14:26:02 +02:00
HashSet<String> mSelectedSet;
private static final int INSERT_ID = Menu.FIRST;
private static final int SELECT_ALL_ID = Menu.FIRST + 1;
private static final int DESELECT_ALL_ID = Menu.FIRST + 2;
2009-10-22 14:26:02 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.number_list);
2010-01-22 15:21:33 +01:00
String[] selected;
selected = savedInstanceState != null ? savedInstanceState.getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST ) : null;
2009-10-22 14:26:02 +02:00
2010-01-22 15:21:33 +01:00
if (selected == null) {
2009-10-22 14:26:02 +02:00
Bundle extras = getIntent().getExtras();
2010-01-22 15:21:33 +01:00
selected = extras != null ? extras.getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST ) : null;
2009-10-22 14:26:02 +02:00
}
mSelectedSet = new HashSet<String>();
2010-01-22 15:21:33 +01:00
for (int i = 0; i < selected.length; i++) {
mSelectedSet.add(selected[i].trim());
2009-10-22 14:26:02 +02:00
}
fillData();
Button ok = (Button) findViewById(R.id.okContacts);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
2010-01-22 15:21:33 +01:00
String [] numbers = new String[mSelectedSet.size()];
mSelectedSet.toArray(numbers);
2009-10-22 14:26:02 +02:00
Bundle bundle = new Bundle();
bundle.putStringArray(MultiSmsSender.PARAM_NUMBERS_LIST, numbers);
2010-01-22 15:21:33 +01:00
bundle.putBoolean(MultiSmsSender.PARAM_FLUSH, true);
2009-10-22 14:26:02 +02:00
i.putExtras(bundle);
setResult(RESULT_OK, i);
finish();
2010-01-22 15:21:33 +01:00
2009-10-22 14:26:02 +02:00
}
});
}
private void fillData() {
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Data.MIMETYPE, Phone.NUMBER, Phone.TYPE, Phone.LABEL, Contacts.DISPLAY_NAME},
Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
null, Contacts.DISPLAY_NAME);
2009-10-22 14:26:02 +02:00
startManagingCursor(c);
mAdpater = new PhoneDataListAdapter(this, R.layout.number_row, c, new String[] {
Contacts.DISPLAY_NAME, Phone.NUMBER
2009-10-22 14:26:02 +02:00
}, new int[] {R.id.name, R.id.phone}, mSelectedSet);
/*Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME);
startManagingCursor(c);
mAdpater = new PhoneDataListAdapter(this, R.layout.number_row, c, new String[] {
Phones.NAME, Phones.NUMBER
}, new int[] {R.id.name, R.id.phone}, mSelectedSet);*/
2009-10-22 14:26:02 +02:00
setListAdapter(mAdpater);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//menu.add(0, INSERT_ID,0, R.string.add_group);
2009-10-22 14:26:02 +02:00
menu.add(0, SELECT_ALL_ID, 0, R.string.select_all);
menu.add(0, DESELECT_ALL_ID, 0, R.string.deselect_all);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
display_group_list();
return true;
case SELECT_ALL_ID:
/*Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME);
2009-10-22 14:26:02 +02:00
int numberIdx = c.getColumnIndex(Phones.NUMBER);
*/
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Data.MIMETYPE, Phone.NUMBER, Phone.TYPE, Phone.LABEL, Contacts.DISPLAY_NAME},
Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
null, null);
int numberIdx = c.getColumnIndex(Phone.NUMBER);
startManagingCursor(c);
2009-10-22 14:26:02 +02:00
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
mSelectedSet.add(c.getString(numberIdx));
c.moveToNext();
}
fillData();
return true;
case DESELECT_ALL_ID:
mSelectedSet = new HashSet<String>();
fillData();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
public void display_group_list(){
Intent i = new Intent(this, SelectGroupActivity.class);
startActivityForResult(i, MultiSmsSender.ACTIVITY_ADD_GROUP);
}
2010-01-22 15:21:33 +01:00
/*@Override
2009-10-22 14:26:02 +02:00
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
l.getItemAtPosition(position);
2010-01-22 15:21:33 +01:00
}*/
2009-10-22 14:26:02 +02:00
/*@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArray(MultiSmsSender.PARAM_NUMBERS_LIST, (String[] )mAdpater.selected.toArray());
}
@Override
protected void onPause() {
super.onPause();
}*/
public class PhoneDataListAdapter extends SimpleCursorAdapter{
int nameidx;
int numberidx;
Context mContext;
public PhoneDataListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, HashSet<String> selected) {
super(context, layout, c, from, to);
nameidx = c.getColumnIndex(Contacts.DISPLAY_NAME);
numberidx = c.getColumnIndex(Phone.NUMBER);
2009-10-22 14:26:02 +02:00
mContext = context;
2010-01-22 15:21:33 +01:00
2009-10-22 14:26:02 +02:00
}
public View getView(int position, View convertView, ViewGroup parent) {
Cursor c = getCursor();
c.moveToPosition(position);
String name = c.getString(nameidx);
String number = c.getString(numberidx);
ContactRow contact;
if (convertView == null) {
contact = new ContactRow(mContext);
}else {
contact = (ContactRow) convertView;
}
2010-01-22 15:21:33 +01:00
contact.display(name, number, mSelectedSet.contains(number));
2009-10-22 14:26:02 +02:00
contact.mSelectedCheckBox.setOnClickListener(new addNumberToSelectedClickListener(contact));
return contact;
}
}
private class addNumberToSelectedClickListener implements OnClickListener{
ContactRow contact;
public addNumberToSelectedClickListener(ContactRow contact) {
this.contact = contact;
}
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()) {
mSelectedSet.add(contact.mNumberView.getText().toString());
}else {
mSelectedSet.remove(contact.mNumberView.getText().toString());
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(intent != null) {
String [] numbers = intent.getExtras().getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST);
for (int i = 0; i < numbers.length; i++) {
mSelectedSet.add(numbers[i]);
}
2010-01-22 15:21:33 +01:00
2009-10-22 14:26:02 +02:00
fillData();
}
}
}