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;
|
2010-12-10 22:31:06 +01:00
|
|
|
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;
|
2010-12-10 22:31:06 +01:00
|
|
|
import android.view.ViewGroup;
|
2009-10-22 14:26:02 +02:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.CheckBox;
|
2012-09-27 22:31:15 +02:00
|
|
|
import android.widget.LinearLayout;
|
2009-10-22 14:26:02 +02:00
|
|
|
import android.widget.SimpleCursorAdapter;
|
2012-09-27 22:31:15 +02:00
|
|
|
import android.widget.TextView;
|
2009-10-22 14:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-12-10 22:31:06 +01:00
|
|
|
|
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() {
|
2010-12-10 22:31:06 +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);
|
|
|
|
mAdpater = new PhoneDataListAdapter(this, R.layout.number_row, c, new String[] {
|
2010-12-10 22:31:06 +01:00
|
|
|
Contacts.DISPLAY_NAME, Phone.NUMBER
|
2009-10-22 14:26:02 +02:00
|
|
|
}, new int[] {R.id.name, R.id.phone}, mSelectedSet);
|
2010-12-10 22:31:06 +01:00
|
|
|
|
|
|
|
|
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) {
|
|
|
|
switch(item.getItemId()) {
|
|
|
|
case INSERT_ID:
|
|
|
|
display_group_list();
|
|
|
|
return true;
|
|
|
|
case SELECT_ALL_ID:
|
2010-12-10 22:31:06 +01:00
|
|
|
|
|
|
|
/*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);
|
2010-12-10 22:31:06 +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, 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{
|
|
|
|
|
|
|
|
public PhoneDataListAdapter(Context context, int layout, Cursor c,
|
|
|
|
String[] from, int[] to, HashSet<String> selected) {
|
|
|
|
super(context, layout, c, from, to);
|
|
|
|
}
|
|
|
|
|
2012-09-27 22:31:15 +02:00
|
|
|
|
2009-10-22 14:26:02 +02:00
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
|
2012-09-27 22:31:15 +02:00
|
|
|
View v = super.getView(position, convertView, parent);
|
|
|
|
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() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
CheckBox checkbox = (CheckBox)v.findViewById(R.id.CheckBox);
|
|
|
|
checkbox.performClick();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return v;
|
2009-10-22 14:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class addNumberToSelectedClickListener implements OnClickListener{
|
|
|
|
|
2012-09-27 22:31:15 +02:00
|
|
|
String contactNumber;
|
2009-10-22 14:26:02 +02:00
|
|
|
|
2012-09-27 22:31:15 +02:00
|
|
|
public addNumberToSelectedClickListener(String contact) {
|
|
|
|
this.contactNumber = contact;
|
2009-10-22 14:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
CheckBox checkBox = (CheckBox)v;
|
|
|
|
if(checkBox.isChecked()) {
|
2012-09-27 22:31:15 +02:00
|
|
|
mSelectedSet.add(contactNumber);
|
2009-10-22 14:26:02 +02:00
|
|
|
}else {
|
2012-09-27 22:31:15 +02:00
|
|
|
mSelectedSet.remove(contactNumber);
|
2009-10-22 14:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|