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

206 lines
5.3 KiB
Java
Raw Permalink 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;
2011-01-10 11:39:18 +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.View;
import android.view.View.OnClickListener;
2011-01-10 11:39:18 +01:00
import android.view.ViewGroup;
2009-10-22 14:26:02 +02:00
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
2009-10-22 14:26:02 +02:00
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);
2011-01-10 11:39:18 +01:00
2012-11-06 10:35:01 +01:00
// Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null,
// null, Phones.NAME);
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);
String[] mSelected = {};
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
mDb = new GroupsDbAdapter(this);
mDb.open();
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
Long groupId;
Bundle extras = getIntent().getExtras();
2012-11-06 10:35:01 +01:00
groupId = extras != null ? extras
.getLong(SelectGroupActivity.PARAM_GROUP_ID) : null;
2009-10-22 14:26:02 +02:00
if (groupId != null) {
Cursor groupNameCursor = mDb.fetchGroup(groupId);
2011-01-10 11:39:18 +01:00
startManagingCursor(groupNameCursor);
2012-11-06 10:35:01 +01:00
String groupName = groupNameCursor.getString(groupNameCursor
.getColumnIndex(GroupsDbAdapter.KEY_GROUP_NAME));
2009-10-22 14:26:02 +02:00
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();
2011-01-10 11:39:18 +01:00
int phoneNumIdx = numbers.getColumnIndex(Phone.NUMBER);
2009-10-22 14:26:02 +02:00
mSelected = new String[numbers.getCount()];
2012-11-06 10:35:01 +01:00
for (int i = 0; i < numbers.getCount(); i++) {
2009-10-22 14:26:02 +02:00
mSelected[i] = numbers.getString(phoneNumIdx);
numbers.moveToNext();
}
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
mGid = groupId;
}
2012-11-06 10:35:01 +01:00
mAdpater = new GroupDataListAdapter(this, R.layout.number_row, c,
new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER },
new int[] { R.id.name, R.id.phone }, mSelected);
2009-10-22 14:26:02 +02:00
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();
}
});
}
2012-11-06 10:35:01 +01:00
2011-01-10 11:39:18 +01:00
protected void onStart() {
2012-11-06 10:35:01 +01:00
//Log.d("GroupEdit", "---onStart");
2011-01-10 11:39:18 +01:00
mDb.open();
super.onStart();
}
2012-11-06 10:35:01 +01:00
2011-01-10 11:39:18 +01:00
protected void onResume() {
2012-11-06 10:35:01 +01:00
//Log.d("GroupEdit", "---onResume");
2011-01-10 11:39:18 +01:00
super.onResume();
}
2009-10-22 14:26:02 +02:00
2011-01-10 11:39:18 +01:00
protected void onStop() {
2012-11-06 10:35:01 +01:00
//Log.d("GroupEdit", "---OnStop");
2010-01-22 15:21:33 +01:00
mDb.close();
2011-01-10 11:39:18 +01:00
super.onStop();
2010-01-22 15:21:33 +01:00
}
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
private void createGroup() {
2012-11-06 10:35:01 +01:00
if (mGid == null) {
2009-10-22 14:26:02 +02:00
String name = mGroupNameText.getText().toString();
2012-11-06 10:35:01 +01:00
if (name.equals("")) {
2009-10-22 14:26:02 +02:00
name = getResources().getString(R.string.noName);
}
mGid = mDb.createGroup(name);
}
}
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
private void reNameGroup() {
2012-11-06 10:35:01 +01:00
if (mGid != null) {
2009-10-22 14:26:02 +02:00
String name = mGroupNameText.getText().toString();
2012-11-06 10:35:01 +01:00
if (name.equals("")) {
2009-10-22 14:26:02 +02:00
name = getResources().getString(R.string.noName);
}
mDb.updateGroup(mGid, name);
}
}
2012-11-06 10:35:01 +01:00
public class GroupDataListAdapter extends SimpleCursorAdapter {
2009-10-22 14:26:02 +02:00
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);
2012-11-06 10:35:01 +01:00
nameidx = c.getColumnIndex(Contacts.DISPLAY_NAME);
2011-01-10 11:39:18 +01:00
numberidx = c.getColumnIndex(Phone.NUMBER);
2012-11-06 10:35:01 +01:00
idIdx = c.getColumnIndex(Data._ID);
mContext = context;
selected = new HashSet<String>();
2009-10-22 14:26:02 +02:00
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();
2011-01-10 11:39:18 +01:00
startManagingCursor(c);
2009-10-22 14:26:02 +02:00
c.moveToPosition(position);
2012-11-06 10:35:01 +01:00
String contactNumber = c.getString(numberidx);
long id = c.getLong(idIdx);
View v = super.getView(position, convertView, parent);
LinearLayout background = (LinearLayout) v
.findViewById(R.id.row_background);
CheckBox checkbox = (CheckBox) v.findViewById(R.id.CheckBox);
checkbox.setOnClickListener(new addNumberToGroupClickListener(id));
checkbox.setChecked(selected.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 addNumberToGroupClickListener implements OnClickListener {
2009-10-22 14:26:02 +02:00
Long mId;
2012-11-06 10:35:01 +01:00
public addNumberToGroupClickListener(Long id) {
2009-10-22 14:26:02 +02:00
super();
this.mId = id;
2009-10-22 14:26:02 +02:00
}
public void onClick(View v) {
2012-11-06 10:35:01 +01:00
CheckBox cBox = (CheckBox) v;
2009-10-22 14:26:02 +02:00
createGroup();
2012-11-06 10:35:01 +01:00
if (cBox.isChecked()) {
mDb.addPhoneToGroup(mGid, mId);
2012-11-06 10:35:01 +01:00
} else {
mDb.removePhoneToGroup(mGid, mId);
2009-10-22 14:26:02 +02:00
}
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
}
2012-11-06 10:35:01 +01:00
2009-10-22 14:26:02 +02:00
}
}