single contact selection
parent
9438973eac
commit
9b41675f8f
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<solid android:color="#00ffffff" />
|
||||
<stroke android:width="1px" android:color="#22000000" />
|
||||
<corners android:radius="4dp"/>
|
||||
<padding android:left="4dp" android:top="4dp" android:right="4dp" android:bottom="4dp" />
|
||||
</shape>
|
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout android:layout_gravity="center"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_width="fill_parent"
|
||||
android:orientation="vertical"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<fragment
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/contact_selection_list_fragment"
|
||||
android:name="org.thoughtcrime.securesms.SingleContactSelectionListFragment">
|
||||
</fragment>
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,29 @@
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<EditText android:id="@+id/filter"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Type a name to filter..."
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:layout_marginTop="7dp"
|
||||
android:layout_marginBottom="7dp" />
|
||||
|
||||
<ListView android:id="@android:id/list"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:fastScrollEnabled="true" />
|
||||
|
||||
<TextView android:id="@android:id/empty"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center|center_vertical"
|
||||
android:layout_marginTop="15dp"
|
||||
android:text="@string/contact_selection_group_activity__no_contacts"
|
||||
android:textSize="20sp" />
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="?android:attr/listPreferredItemHeight"
|
||||
android:paddingRight="25dip">
|
||||
|
||||
<TextView android:id="@+id/label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_marginBottom="8dip"
|
||||
android:layout_marginTop="-8dip"
|
||||
android:layout_marginLeft="14dip"
|
||||
android:singleLine="true"
|
||||
android:ellipsize="marquee"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||
android:textStyle="bold"
|
||||
android:visibility = "gone"
|
||||
/>
|
||||
|
||||
<TextView android:id="@+id/number"
|
||||
android:visibility = "visible"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="8dip"
|
||||
android:layout_marginTop="-8dip"
|
||||
android:layout_marginLeft="14dip"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:singleLine="true"
|
||||
android:ellipsize="marquee"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||
android:fontFamily="sans-serif-light"
|
||||
/>
|
||||
|
||||
<TextView android:id="@+id/name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_above="@id/number"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_marginBottom="1dip"
|
||||
android:layout_marginLeft="14dip"
|
||||
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
|
||||
android:singleLine="true"
|
||||
android:ellipsize="marquee"
|
||||
android:gravity="center_vertical|left"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
import android.widget.CursorAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Filter;
|
||||
import android.widget.FilterQueryProvider;
|
||||
|
||||
import com.actionbarsherlock.app.ActionBar;
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
||||
import org.thoughtcrime.securesms.util.ActionBarUtil;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
|
||||
|
||||
/**
|
||||
* Activity container for selecting a list of contacts. Provides a tab frame for
|
||||
* contact, group, and "recent contact" activity tabs. Used by ComposeMessageActivity
|
||||
* when selecting a list of contacts to address a message to.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*
|
||||
*/
|
||||
public class SingleContactSelectionActivity extends PassphraseRequiredSherlockFragmentActivity {
|
||||
private final String TAG = "SingleContactSelectionActivity";
|
||||
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle icicle) {
|
||||
dynamicTheme.onCreate(this);
|
||||
super.onCreate(icicle);
|
||||
|
||||
final ActionBar actionBar = this.getSupportActionBar();
|
||||
ActionBarUtil.initializeDefaultActionBar(this, actionBar);
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
setContentView(R.layout.single_contact_selection_activity);
|
||||
final SingleContactSelectionListFragment listFragment = (SingleContactSelectionListFragment)getSupportFragmentManager().findFragmentById(R.id.contact_selection_list_fragment);
|
||||
listFragment.setOnContactSelectedListener(new SingleContactSelectionListFragment.OnContactSelectedListener() {
|
||||
@Override
|
||||
public void onContactSelected(ContactData contactData) {
|
||||
Intent resultIntent = getIntent();
|
||||
ArrayList<ContactData> contactList = new ArrayList<ContactData>();
|
||||
contactList.add(contactData);
|
||||
resultIntent.putParcelableArrayListExtra("contacts", contactList);
|
||||
setResult(RESULT_OK, resultIntent);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
EditText filter = (EditText)findViewById(R.id.filter);
|
||||
filter.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable editable) {
|
||||
final CursorAdapter adapter = (CursorAdapter)listFragment.getListView().getAdapter();
|
||||
Log.i(TAG, "new text change: " + editable);
|
||||
Filter filter = adapter.getFilter();
|
||||
|
||||
if (filter != null) { filter.filter(editable.toString()); adapter.notifyDataSetChanged(); }
|
||||
else { Log.w(TAG, "filter was null, bad time."); }
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
dynamicTheme.onResume(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
setResult(RESULT_CANCELED);
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,298 @@
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.database.Cursor;
|
||||
import android.database.MergeCursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.ContactsContract;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CursorAdapter;
|
||||
import android.widget.FilterQueryProvider;
|
||||
import android.widget.ListView;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockListFragment;
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor.NumberData;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Activity for selecting a list of contacts. Displayed inside
|
||||
* a PushContactSelectionActivity tab frame, and ultimately called by
|
||||
* ComposeMessageActivity for selecting a list of destination contacts.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*
|
||||
*/
|
||||
|
||||
public class SingleContactSelectionListFragment extends SherlockListFragment
|
||||
implements LoaderManager.LoaderCallbacks<Cursor>
|
||||
{
|
||||
private final String TAG = "SingleContactSelectionListFragment";
|
||||
private final HashMap<Long, ContactData> selectedContacts = new HashMap<Long, ContactData>();
|
||||
private static LayoutInflater li;
|
||||
private OnContactSelectedListener onContactSelectedListener;
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
li = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
initializeResources();
|
||||
initializeCursor();
|
||||
}
|
||||
|
||||
public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) {
|
||||
this.onContactSelectedListener = onContactSelectedListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.single_contact_selection_list_activity, container, false);
|
||||
}
|
||||
|
||||
private void addSingleNumberContact(ContactData contactData) {
|
||||
if (onContactSelectedListener != null) {
|
||||
onContactSelectedListener.onContactSelected(contactData);
|
||||
}
|
||||
}
|
||||
|
||||
private void addMultipleNumberContact(ContactData contactData, TextView textView) {
|
||||
String[] options = new String[contactData.numbers.size()];
|
||||
int i = 0;
|
||||
|
||||
for (NumberData option : contactData.numbers) {
|
||||
options[i++] = option.type + " " + option.number;
|
||||
}
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
builder.setTitle(R.string.ContactSelectionlistFragment_select_for + " " + contactData.name);
|
||||
builder.setMultiChoiceItems(options, null, new DiscriminatorClickedListener(contactData));
|
||||
builder.setPositiveButton(android.R.string.ok, new DiscriminatorFinishedListener(contactData, textView));
|
||||
builder.setOnCancelListener(new DiscriminatorFinishedListener(contactData, textView));
|
||||
builder.show();
|
||||
}
|
||||
|
||||
private void initializeCursor() {
|
||||
final ContactSelectionListAdapter listAdapter = new ContactSelectionListAdapter(getActivity(), null);
|
||||
listAdapter.setFilterQueryProvider(new FilterQueryProvider() {
|
||||
@Override
|
||||
public Cursor runQuery(CharSequence charSequence) {
|
||||
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
||||
final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1" +
|
||||
" AND " + ContactsContract.Contacts.DISPLAY_NAME + " like ?";
|
||||
|
||||
final Cursor filteredCursor = getActivity().getContentResolver().query(uri, null, selection, new String[]{"%"+charSequence.toString()+"%"},
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
return filteredCursor;
|
||||
}
|
||||
});
|
||||
setListAdapter(listAdapter);
|
||||
this.getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
private void initializeResources() {
|
||||
this.getListView().setFocusable(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
((ContactItemView)v).selected();
|
||||
}
|
||||
|
||||
private class ContactSelectionListAdapter extends CursorAdapter {
|
||||
|
||||
public ContactSelectionListAdapter(Context context, Cursor c) {
|
||||
super(context, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
||||
ContactItemView view = new ContactItemView(context);
|
||||
bindView(view, context, cursor);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindView(View view, Context context, Cursor cursor) {
|
||||
boolean isPushUser;
|
||||
try {
|
||||
isPushUser = (cursor.getInt(cursor.getColumnIndexOrThrow(ContactAccessor.PUSH_COLUMN)) > 0);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
isPushUser = false;
|
||||
}
|
||||
ContactData contactData = ContactAccessor.getInstance().getContactData(context, cursor);
|
||||
PushContactData pushContactData = new PushContactData(contactData, isPushUser);
|
||||
((ContactItemView)view).set(pushContactData);
|
||||
}
|
||||
}
|
||||
|
||||
private class PushContactData {
|
||||
private final ContactData contactData;
|
||||
private final boolean pushSupport;
|
||||
public PushContactData(ContactData contactData, boolean pushSupport) {
|
||||
this.contactData = contactData;
|
||||
this.pushSupport = pushSupport;
|
||||
}
|
||||
}
|
||||
|
||||
private class ContactItemView extends RelativeLayout {
|
||||
private ContactData contactData;
|
||||
private boolean pushSupport;
|
||||
private TextView name;
|
||||
private TextView number;
|
||||
private TextView label;
|
||||
private View pushLabel;
|
||||
|
||||
public ContactItemView(Context context) {
|
||||
super(context);
|
||||
|
||||
li.inflate(R.layout.single_contact_selection_list_item, this, true);
|
||||
|
||||
this.name = (TextView) findViewById(R.id.name);
|
||||
this.number = (TextView) findViewById(R.id.number);
|
||||
this.label = (TextView) findViewById(R.id.label);
|
||||
this.pushLabel = findViewById(R.id.push_support_label);
|
||||
}
|
||||
|
||||
public void selected() {
|
||||
if (contactData.numbers.size() == 1) addSingleNumberContact(contactData);
|
||||
else addMultipleNumberContact(contactData, name);
|
||||
}
|
||||
|
||||
public void set(PushContactData pushContactData) {
|
||||
this.contactData = pushContactData.contactData;
|
||||
this.pushSupport = pushContactData.pushSupport;
|
||||
|
||||
/*if (!pushSupport) {
|
||||
this.name.setTextColor(0xa0000000);
|
||||
this.number.setTextColor(0xa0000000);
|
||||
this.pushLabel.setBackgroundColor(0x99000000);
|
||||
} else {
|
||||
this.name.setTextColor(0xff000000);
|
||||
this.number.setTextColor(0xff000000);
|
||||
this.pushLabel.setBackgroundColor(0xff64a926);
|
||||
}*/
|
||||
|
||||
this.name.setText(contactData.name);
|
||||
|
||||
if (contactData.numbers.isEmpty()) {
|
||||
this.name.setEnabled(false);
|
||||
this.number.setText("");
|
||||
this.label.setText("");
|
||||
} else {
|
||||
this.number.setText(contactData.numbers.get(0).number);
|
||||
this.label.setText(contactData.numbers.get(0).type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DiscriminatorFinishedListener implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
|
||||
private final ContactData contactData;
|
||||
private final TextView textView;
|
||||
|
||||
public DiscriminatorFinishedListener(ContactData contactData, TextView textView) {
|
||||
this.contactData = contactData;
|
||||
this.textView = textView;
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
ContactData selected = selectedContacts.get(contactData.id);
|
||||
|
||||
if (selected.numbers.size() == 0) {
|
||||
selectedContacts.remove(selected.id);
|
||||
}
|
||||
|
||||
if (textView == null)
|
||||
((CursorAdapter) getListView().getAdapter()).notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
onClick(dialog, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private class DiscriminatorClickedListener implements DialogInterface.OnMultiChoiceClickListener {
|
||||
private final ContactData contactData;
|
||||
|
||||
public DiscriminatorClickedListener(ContactData contactData) {
|
||||
this.contactData = contactData;
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
|
||||
Log.w(TAG, "Got checked: " + isChecked);
|
||||
|
||||
ContactData existing = selectedContacts.get(contactData.id);
|
||||
|
||||
if (existing == null) {
|
||||
Log.w(TAG, "No existing contact data, creating...");
|
||||
|
||||
if (!isChecked)
|
||||
throw new AssertionError("We shouldn't be unchecking data that doesn't exist.");
|
||||
|
||||
existing = new ContactData(contactData.id, contactData.name);
|
||||
selectedContacts.put(existing.id, existing);
|
||||
}
|
||||
|
||||
NumberData selectedData = contactData.numbers.get(which);
|
||||
|
||||
if (!isChecked) existing.numbers.remove(selectedData);
|
||||
else existing.numbers.add(selectedData);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
|
||||
return ContactAccessor.getInstance().getCursorLoaderForContactsWithNumbers(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
|
||||
((CursorAdapter) getListAdapter()).changeCursor(cursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> arg0) {
|
||||
((CursorAdapter) getListAdapter()).changeCursor(null);
|
||||
}
|
||||
|
||||
public interface OnContactSelectedListener {
|
||||
public void onContactSelected(ContactData contactData);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue