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

211 lines
5.8 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;
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.LinearLayout;
2009-10-22 14:26:02 +02:00
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
2009-10-22 14:26:02 +02:00
2012-11-06 10:35:01 +01:00
public class PhoneNumberSelection extends ListActivity {
2009-10-22 14:26:02 +02:00
PhoneDataListAdapter mAdpater;
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
HashSet<String> mSelectedSet;
2012-11-06 10:35:01 +01:00
private static final int INSERT_ID = Menu.FIRST;
private static final int SELECT_ALL_ID = Menu.FIRST + 1;
2009-10-22 14:26:02 +02:00
private static final int DESELECT_ALL_ID = Menu.FIRST + 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.number_list);
2010-01-22 15:21:33 +01:00
String[] selected;
2012-11-06 10:35:01 +01:00
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();
2012-11-06 10:35:01 +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) {
2012-11-06 10:35:01 +01:00
Intent i = new Intent();
String[] numbers = new String[mSelectedSet.size()];
2010-01-22 15:21:33 +01:00
mSelectedSet.toArray(numbers);
2012-11-06 10:35:01 +01: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);
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
setResult(RESULT_OK, i);
finish();
}
});
}
private void fillData() {
2012-11-06 10:35:01 +01:00
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);
2012-11-06 10:35:01 +01:00
mAdpater = new PhoneDataListAdapter(this, R.layout.number_row, c,
new String[] { Contacts.DISPLAY_NAME, Phone.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, 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) {
2012-11-06 10:35:01 +01:00
switch (item.getItemId()) {
2009-10-22 14:26:02 +02:00
case INSERT_ID:
display_group_list();
return true;
case SELECT_ALL_ID:
2012-11-06 10:35:01 +01:00
/*
* Cursor c = getContentResolver().query(Phones.CONTENT_URI, null,
* null, null, Phones.NAME); 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;
2012-11-06 10:35:01 +01:00
}
2009-10-22 14:26:02 +02:00
return super.onMenuItemSelected(featureId, item);
}
2012-11-06 10:35:01 +01:00
public void display_group_list() {
2009-10-22 14:26:02 +02:00
Intent i = new Intent(this, SelectGroupActivity.class);
startActivityForResult(i, MultiSmsSender.ACTIVITY_ADD_GROUP);
}
2012-11-06 10:35:01 +01:00
public class PhoneDataListAdapter extends SimpleCursorAdapter {
2009-10-22 14:26:02 +02:00
public PhoneDataListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, HashSet<String> selected) {
super(context, layout, c, from, to);
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
2012-11-06 10:35:01 +01:00
LinearLayout background = (LinearLayout) v
.findViewById(R.id.row_background);
String contactNumber = ((TextView) v.findViewById(R.id.phone))
.getText().toString();
CheckBox checkbox = (CheckBox) v.findViewById(R.id.CheckBox);
checkbox.setOnClickListener(new addNumberToSelectedClickListener(
contactNumber));
checkbox.setChecked(mSelectedSet.contains(contactNumber));
background.setOnClickListener(new OnClickListener() {
2012-11-06 10:35:01 +01:00
@Override
public void onClick(View v) {
2012-11-06 10:35:01 +01:00
CheckBox checkbox = (CheckBox) v
.findViewById(R.id.CheckBox);
checkbox.performClick();
2012-11-06 10:35:01 +01:00
}
});
return v;
2009-10-22 14:26:02 +02:00
}
}
2012-11-06 10:35:01 +01:00
private class addNumberToSelectedClickListener implements OnClickListener {
2009-10-22 14:26:02 +02:00
String contactNumber;
2009-10-22 14:26:02 +02:00
public addNumberToSelectedClickListener(String contact) {
this.contactNumber = contact;
2009-10-22 14:26:02 +02:00
}
public void onClick(View v) {
2012-11-06 10:35:01 +01:00
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked()) {
mSelectedSet.add(contactNumber);
2012-11-06 10:35:01 +01:00
} else {
mSelectedSet.remove(contactNumber);
2009-10-22 14:26:02 +02:00
}
}
}
2012-11-06 10:35:01 +01:00
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
2009-10-22 14:26:02 +02:00
super.onActivityResult(requestCode, resultCode, intent);
2012-11-06 10:35:01 +01:00
if (intent != null) {
String[] numbers = intent.getExtras().getStringArray(
MultiSmsSender.PARAM_NUMBERS_LIST);
2009-10-22 14:26:02 +02:00
for (int i = 0; i < numbers.length; i++) {
2012-11-06 10:35:01 +01:00
mSelectedSet.add(numbers[i]);
2009-10-22 14:26:02 +02:00
}
2010-01-22 15:21:33 +01:00
2009-10-22 14:26:02 +02:00
fillData();
}
}
}