MultiSmsSender/src/com/hectorone/multismssender/GroupEditActivity.java
Mathieu Maret f51212fa08 missing files for package renam...
Its too late to code....!
2010-12-12 03:06:07 +01:00

174 lines
4.4 KiB
Java

package com.hectorone.multismssender;
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.Contacts.Phones;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
public class GroupEditActivity extends ListActivity {
GroupDataListAdapter mAdpater;
GroupsDbAdapter mDb;
EditText mGroupNameText;
Long mGid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_group_list);
mGroupNameText = (EditText) findViewById(R.id.groupName);
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME);
startManagingCursor(c);
String[] mSelected = {};
mDb = new GroupsDbAdapter(this);
mDb.open();
Long groupId;
Bundle extras = getIntent().getExtras();
groupId = extras != null ? extras.getLong(SelectGroupActivity.PARAM_GROUP_ID): null;
if (groupId != null) {
Cursor groupNameCursor = mDb.fetchGroup(groupId);
String groupName = groupNameCursor.getString(groupNameCursor.getColumnIndex(GroupsDbAdapter.KEY_GROUP_NAME));
mGroupNameText.setText(groupName);
Cursor numbers = mDb.fetchPhonesFromGroup(groupId);
startManagingCursor(numbers);
numbers.moveToFirst();
int phoneNumIdx = numbers.getColumnIndex(Phones.NUMBER);
mSelected = new String[numbers.getCount()];
for(int i = 0; i < numbers.getCount(); i++) {
mSelected[i] = numbers.getString(phoneNumIdx);
numbers.moveToNext();
}
mGid = groupId;
}
mAdpater = new GroupDataListAdapter(this, R.layout.number_row, c, new String[] {
Phones.NAME, Phones.NUMBER
}, new int[] {R.id.name, R.id.phone}, mSelected);
setListAdapter(mAdpater);
Button ok = (Button) findViewById(R.id.okGroups);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
createGroup();
reNameGroup();
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
}
});
}
protected void onDestroy() {
mDb.close();
super.onDestroy();
}
private void createGroup() {
if (mGid == null) {
String name = mGroupNameText.getText().toString();
if(name.equals("")) {
name = getResources().getString(R.string.noName);
}
mGid = mDb.createGroup(name);
}
}
private void reNameGroup() {
if (mGid != null) {
String name = mGroupNameText.getText().toString();
if(name.equals("")) {
name = getResources().getString(R.string.noName);
}
mDb.updateGroup(mGid, name);
}
}
public class GroupDataListAdapter extends SimpleCursorAdapter{
public HashSet<String> selected;
int nameidx;
int numberidx;
int idIdx;
Context mContext;
public GroupDataListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, String[] rawSelected) {
super(context, layout, c, from, to);
nameidx = c.getColumnIndex(Phones.NAME);
numberidx = c.getColumnIndex(Phones.NUMBER);
idIdx = c.getColumnIndex(Phones._ID);
mContext = context;
selected = new HashSet<String>();
for (int i = 0; i < rawSelected.length; i++) {
selected.add(rawSelected[i].trim());
}
}
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);
long id = c.getLong(idIdx);
ContactRow contact;
if (convertView == null) {
contact = new ContactRow(mContext);
}else {
contact = (ContactRow) convertView;
}
contact.display(name, number, selected.contains(number), id);
contact.mSelectedCheckBox.setOnClickListener(new addNumberToGroupClickListener(contact));
return contact;
}
}
private class addNumberToGroupClickListener implements OnClickListener{
ContactRow mContact;
public addNumberToGroupClickListener(ContactRow mContact) {
super();
this.mContact = mContact;
}
public void onClick(View v) {
CheckBox cBox = (CheckBox)v;
createGroup();
long phoneId = mContact.mId;
if(cBox.isChecked()) {
mDb.addPhoneToGroup(mGid, phoneId);
}else {
mDb.removePhoneToGroup(mGid, phoneId);
}
}
}
}