2009-10-22 14:26:02 +02:00
|
|
|
package com.openwide.android;
|
|
|
|
|
|
|
|
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);
|
2010-01-27 17:59:55 +01:00
|
|
|
startManagingCursor(numbers);
|
2009-10-22 14:26:02 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-01-22 15:21:33 +01:00
|
|
|
protected void onDestroy() {
|
|
|
|
mDb.close();
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2009-10-22 14:26:02 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|