mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-25 22:59:25 +00:00
Initial Project Import
This commit is contained in:
169
src/org/thoughtcrime/securesms/contacts/ArrayListCursor.java
Normal file
169
src/org/thoughtcrime/securesms/contacts/ArrayListCursor.java
Normal file
@@ -0,0 +1,169 @@
|
||||
package org.thoughtcrime.securesms.contacts;
|
||||
/*
|
||||
* Copyright (C) 2006 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import android.database.AbstractCursor;
|
||||
import android.database.CursorWindow;
|
||||
|
||||
import java.lang.System;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A convenience class that presents a two-dimensional ArrayList
|
||||
* as a Cursor.
|
||||
*/
|
||||
public class ArrayListCursor extends AbstractCursor {
|
||||
private String[] mColumnNames;
|
||||
private ArrayList<Object>[] mRows;
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public ArrayListCursor(String[] columnNames, ArrayList<ArrayList> rows) {
|
||||
int colCount = columnNames.length;
|
||||
boolean foundID = false;
|
||||
// Add an _id column if not in columnNames
|
||||
for (int i = 0; i < colCount; ++i) {
|
||||
if (columnNames[i].compareToIgnoreCase("_id") == 0) {
|
||||
mColumnNames = columnNames;
|
||||
foundID = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundID) {
|
||||
mColumnNames = new String[colCount + 1];
|
||||
System.arraycopy(columnNames, 0, mColumnNames, 0, columnNames.length);
|
||||
mColumnNames[colCount] = "_id";
|
||||
}
|
||||
|
||||
int rowCount = rows.size();
|
||||
mRows = new ArrayList[rowCount];
|
||||
|
||||
for (int i = 0; i < rowCount; ++i) {
|
||||
mRows[i] = rows.get(i);
|
||||
if (!foundID) {
|
||||
mRows[i].add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillWindow(int position, CursorWindow window) {
|
||||
if (position < 0 || position > getCount()) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.acquireReference();
|
||||
try {
|
||||
int oldpos = mPos;
|
||||
mPos = position - 1;
|
||||
window.clear();
|
||||
window.setStartPosition(position);
|
||||
int columnNum = getColumnCount();
|
||||
window.setNumColumns(columnNum);
|
||||
while (moveToNext() && window.allocRow()) {
|
||||
for (int i = 0; i < columnNum; i++) {
|
||||
final Object data = mRows[mPos].get(i);
|
||||
if (data != null) {
|
||||
if (data instanceof byte[]) {
|
||||
byte[] field = (byte[]) data;
|
||||
if (!window.putBlob(field, mPos, i)) {
|
||||
window.freeLastRow();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
String field = data.toString();
|
||||
if (!window.putString(field, mPos, i)) {
|
||||
window.freeLastRow();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!window.putNull(mPos, i)) {
|
||||
window.freeLastRow();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mPos = oldpos;
|
||||
} catch (IllegalStateException e){
|
||||
// simply ignore it
|
||||
} finally {
|
||||
window.releaseReference();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mRows.length;
|
||||
}
|
||||
|
||||
public boolean deleteRow() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getColumnNames() {
|
||||
return mColumnNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBlob(int columnIndex) {
|
||||
return (byte[]) mRows[mPos].get(columnIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(int columnIndex) {
|
||||
Object cell = mRows[mPos].get(columnIndex);
|
||||
return (cell == null) ? null : cell.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getShort(int columnIndex) {
|
||||
Number num = (Number) mRows[mPos].get(columnIndex);
|
||||
return num.shortValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(int columnIndex) {
|
||||
Number num = (Number) mRows[mPos].get(columnIndex);
|
||||
return num.intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(int columnIndex) {
|
||||
Number num = (Number) mRows[mPos].get(columnIndex);
|
||||
return num.longValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(int columnIndex) {
|
||||
Number num = (Number) mRows[mPos].get(columnIndex);
|
||||
return num.floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(int columnIndex) {
|
||||
Number num = (Number) mRows[mPos].get(columnIndex);
|
||||
return num.doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull(int columnIndex) {
|
||||
return mRows[mPos].get(columnIndex) == null;
|
||||
}
|
||||
}
|
||||
165
src/org/thoughtcrime/securesms/contacts/ContactAccessor.java
Normal file
165
src/org/thoughtcrime/securesms/contacts/ContactAccessor.java
Normal file
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* 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.contacts;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
/**
|
||||
* Android changed their contacts API pretty heavily between
|
||||
* 1.x and 2.x. This class provides a common interface to both
|
||||
* API operations, using a singleton pattern that will Class.forName
|
||||
* the correct one so we don't trigger NoClassDefFound exceptions on
|
||||
* old platforms.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*/
|
||||
|
||||
public abstract class ContactAccessor {
|
||||
public static final int UNIQUE_ID = 0;
|
||||
public static final int DISPLAY_NAME = 1;
|
||||
|
||||
private static ContactAccessor sInstance;
|
||||
|
||||
public static synchronized ContactAccessor getInstance() {
|
||||
if (sInstance == null) {
|
||||
String className;
|
||||
|
||||
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.ECLAIR)
|
||||
className = "ContactAccessorOldApi";
|
||||
else
|
||||
className = "ContactAccessorNewApi";
|
||||
|
||||
try {
|
||||
Class<? extends ContactAccessor> clazz =
|
||||
Class.forName("org.thoughtcrime.securesms.contacts." + className )
|
||||
.asSubclass(ContactAccessor.class);
|
||||
|
||||
sInstance = clazz.newInstance();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public abstract NameAndNumber getNameAndNumberFromContact(Context context, Uri uri);
|
||||
public abstract String getNameFromContact(Context context, Uri uri);
|
||||
public abstract IdentityKey importIdentityKey(Context context, Uri uri);
|
||||
public abstract void insertIdentityKey(Context context, Uri uri, IdentityKey identityKey);
|
||||
public abstract Intent getIntentForContactSelection();
|
||||
public abstract List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver);
|
||||
public abstract List<ContactData> getGroupMembership(Context context, long groupId);
|
||||
public abstract Cursor getCursorForContactGroups(Context context);
|
||||
public abstract Cursor getCursorForContactsWithNumbers(Context context);
|
||||
public abstract GroupData getGroupData(Context context, Cursor cursor);
|
||||
public abstract ContactData getContactData(Context context, Cursor cursor);
|
||||
public abstract Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver);
|
||||
public abstract CharSequence phoneTypeToString(Context mContext, int type, CharSequence label);
|
||||
public abstract String getNameForNumber(Context context, String number);
|
||||
public abstract Uri getContactsUri();
|
||||
|
||||
public static class NumberData implements Parcelable {
|
||||
|
||||
public static final Parcelable.Creator<NumberData> CREATOR = new Parcelable.Creator<NumberData>() {
|
||||
public NumberData createFromParcel(Parcel in) {
|
||||
return new NumberData(in);
|
||||
}
|
||||
|
||||
public NumberData[] newArray(int size) {
|
||||
return new NumberData[size];
|
||||
}
|
||||
};
|
||||
|
||||
public String number;
|
||||
public String type;
|
||||
|
||||
public NumberData(String type, String number) {
|
||||
this.type = type;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public NumberData(Parcel in) {
|
||||
number = in.readString();
|
||||
type = in.readString();
|
||||
}
|
||||
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(number);
|
||||
dest.writeString(type);
|
||||
}
|
||||
}
|
||||
|
||||
public static class GroupData {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
public static class ContactData implements Parcelable {
|
||||
|
||||
public static final Parcelable.Creator<ContactData> CREATOR = new Parcelable.Creator<ContactData>() {
|
||||
public ContactData createFromParcel(Parcel in) {
|
||||
return new ContactData(in);
|
||||
}
|
||||
|
||||
public ContactData[] newArray(int size) {
|
||||
return new ContactData[size];
|
||||
}
|
||||
};
|
||||
|
||||
public long id;
|
||||
public String name;
|
||||
public List<NumberData> numbers;
|
||||
|
||||
public ContactData() {}
|
||||
|
||||
public ContactData(Parcel in) {
|
||||
id = in.readLong();
|
||||
name = in.readString();
|
||||
numbers = new LinkedList<NumberData>();
|
||||
in.readTypedList(numbers, NumberData.CREATOR);
|
||||
}
|
||||
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeLong(id);
|
||||
dest.writeString(name);
|
||||
dest.writeTypedList(numbers);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
/**
|
||||
* 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.contacts;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
import org.thoughtcrime.securesms.crypto.InvalidKeyException;
|
||||
import org.thoughtcrime.securesms.util.Base64;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.MergeCursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Im;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.Data;
|
||||
import android.provider.ContactsContract.PhoneLookup;
|
||||
import android.provider.ContactsContract.RawContacts;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Interface into the Android 2.x+ contacts operations.
|
||||
*
|
||||
* @author Stuart Anderson
|
||||
*/
|
||||
|
||||
public class ContactAccessorNewApi extends ContactAccessor {
|
||||
|
||||
private static final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Phone.TYPE;
|
||||
|
||||
private static final String[] PROJECTION_PHONE = {
|
||||
Phone._ID, // 0
|
||||
Phone.CONTACT_ID, // 1
|
||||
Phone.TYPE, // 2
|
||||
Phone.NUMBER, // 3
|
||||
Phone.LABEL, // 4
|
||||
Phone.DISPLAY_NAME, // 5
|
||||
};
|
||||
|
||||
@Override
|
||||
public List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver) {
|
||||
LinkedList<String> numberList = new LinkedList<String>();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = contentResolver.query(Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint)),
|
||||
null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext())
|
||||
numberList.add(cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER)));
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return numberList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
|
||||
String phone = "";
|
||||
String cons = null;
|
||||
if (constraint != null) {
|
||||
cons = constraint.toString();
|
||||
|
||||
if (RecipientsAdapter.usefulAsDigits(cons)) {
|
||||
phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
|
||||
if (phone.equals(cons)) {
|
||||
phone = "";
|
||||
} else {
|
||||
phone = phone.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
|
||||
String selection = String.format("%s=%s OR %s=%s OR %s=%s",
|
||||
Phone.TYPE,
|
||||
Phone.TYPE_MOBILE,
|
||||
Phone.TYPE,
|
||||
Phone.TYPE_WORK_MOBILE,
|
||||
Phone.TYPE,
|
||||
Phone.TYPE_MMS);
|
||||
|
||||
Cursor phoneCursor = mContentResolver.query(uri,
|
||||
PROJECTION_PHONE,
|
||||
null,
|
||||
null,
|
||||
SORT_ORDER);
|
||||
|
||||
|
||||
|
||||
if (phone.length() > 0) {
|
||||
ArrayList result = new ArrayList();
|
||||
result.add(Integer.valueOf(-1)); // ID
|
||||
result.add(Long.valueOf(-1)); // CONTACT_ID
|
||||
result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
|
||||
result.add(phone); // NUMBER
|
||||
|
||||
/*
|
||||
* The "\u00A0" keeps Phone.getDisplayLabel() from deciding
|
||||
* to display the default label ("Home") next to the transformation
|
||||
* of the letters into numbers.
|
||||
*/
|
||||
result.add("\u00A0"); // LABEL
|
||||
result.add(cons); // NAME
|
||||
|
||||
ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
|
||||
wrap.add(result);
|
||||
|
||||
ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);
|
||||
|
||||
return new MergeCursor(new Cursor[] { translated, phoneCursor });
|
||||
} else {
|
||||
return phoneCursor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence phoneTypeToString( Context mContext, int type, CharSequence label ) {
|
||||
return Phone.getTypeLabel(mContext.getResources(), type, label);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Intent getIntentForContactSelection() {
|
||||
Intent intent = new Intent(Intent.ACTION_PICK);
|
||||
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
|
||||
return intent;
|
||||
}
|
||||
|
||||
private long getContactIdFromLookupUri(Context context, Uri uri) {
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri, new String[] {ContactsContract.Contacts._ID}, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getLong(0);
|
||||
else
|
||||
return -1;
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<Long> getRawContactIds(Context context, long contactId) {
|
||||
Cursor cursor = null;
|
||||
ArrayList<Long> rawContactIds = new ArrayList<Long>();
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
|
||||
RawContacts.CONTACT_ID + " = ?", new String[] {contactId+""},
|
||||
null);
|
||||
|
||||
if (cursor == null)
|
||||
return rawContactIds;
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
rawContactIds.add(new Long(cursor.getLong(0)));
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return rawContactIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertIdentityKey(Context context, Uri uri, IdentityKey identityKey) {
|
||||
long contactId = getContactIdFromLookupUri(context, uri);
|
||||
Log.w("ContactAccessorNewApi", "Got contact ID: " + contactId + " from uri: " + uri.toString());
|
||||
ArrayList<Long> rawContactIds = getRawContactIds(context, contactId);
|
||||
|
||||
for (long rawContactId : rawContactIds) {
|
||||
Log.w("ContactAccessorNewApi", "Inserting data for raw contact id: " + rawContactId);
|
||||
ContentValues contentValues = new ContentValues();
|
||||
contentValues.put(Data.RAW_CONTACT_ID, rawContactId);
|
||||
contentValues.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
|
||||
contentValues.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM);
|
||||
contentValues.put(Im.CUSTOM_PROTOCOL, "TextSecure-IdentityKey");
|
||||
contentValues.put(Im.DATA, Base64.encodeBytes(identityKey.serialize()));
|
||||
|
||||
context.getContentResolver().insert(Data.CONTENT_URI, contentValues);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKey importIdentityKey(Context context, Uri uri) {
|
||||
long contactId = getContactIdFromLookupUri(context, uri);
|
||||
String selection = Im.CONTACT_ID + " = ? AND " + Im.PROTOCOL + " = ? AND " + Im.CUSTOM_PROTOCOL + " = ?";
|
||||
String[] selectionArgs = new String[] {contactId+"", Im.PROTOCOL_CUSTOM+"", "TextSecure-IdentityKey"};
|
||||
|
||||
Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, null, selection, selectionArgs, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
String data = cursor.getString(cursor.getColumnIndexOrThrow(Im.DATA));
|
||||
|
||||
if (data != null)
|
||||
return new IdentityKey(Base64.decode(data), 0);
|
||||
|
||||
}
|
||||
} catch (InvalidKeyException e) {
|
||||
Log.w("ContactAccessorNewApi", e);
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
Log.w("ContactAccessorNewApi", e);
|
||||
return null;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameFromContact(Context context, Uri uri) {
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri, new String[] {Contacts.DISPLAY_NAME}, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(0);
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getMobileNumberForId(Context context, long id) {
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ? AND " + Phone.TYPE + " = ?",
|
||||
new String[] {id+"", Phone.TYPE_MOBILE+""}, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER));
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NameAndNumber getNameAndNumberFromContact(Context context, Uri uri) {
|
||||
Log.w("ContactAccessorNewApi", "Get name and number from: " + uri.toString());
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
NameAndNumber results = new NameAndNumber();
|
||||
cursor = context.getContentResolver().query(uri, new String[] {Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
results.name = cursor.getString(1);
|
||||
results.number = getMobileNumberForId(context, cursor.getLong(0));
|
||||
return results;
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactsWithNumbers(Context context) {
|
||||
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1";
|
||||
return context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
}
|
||||
|
||||
private ContactData getContactData(Context context, String displayName, long id) {
|
||||
ContactData contactData = new ContactData();
|
||||
contactData.id = id;
|
||||
contactData.name = displayName;
|
||||
contactData.numbers = new LinkedList<NumberData>();
|
||||
|
||||
Cursor numberCursor = null;
|
||||
|
||||
try {
|
||||
numberCursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?",
|
||||
new String[] {contactData.id + ""}, null);
|
||||
|
||||
while (numberCursor != null && numberCursor.moveToNext())
|
||||
contactData.numbers.add(new NumberData(Phone.getTypeLabel(context.getResources(),
|
||||
numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phone.TYPE)),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.LABEL))).toString(),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.NUMBER))));
|
||||
} finally {
|
||||
if (numberCursor != null)
|
||||
numberCursor.close();
|
||||
}
|
||||
|
||||
return contactData;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContactData getContactData(Context context, Cursor cursor) {
|
||||
return getContactData(context,
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME)),
|
||||
cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactGroups(Context context) {
|
||||
return context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, ContactsContract.Groups.TITLE + " ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ContactData> getGroupMembership(Context context, long groupId) {
|
||||
LinkedList<ContactData> contacts = new LinkedList<ContactData>();
|
||||
Cursor groupMembership = null;
|
||||
|
||||
try {
|
||||
String selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ? AND " +
|
||||
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + " = ?";
|
||||
String[] args = new String[] {groupId+"",
|
||||
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE};
|
||||
|
||||
groupMembership = context.getContentResolver().query(Data.CONTENT_URI, null, selection, args, null);
|
||||
|
||||
while (groupMembership != null && groupMembership.moveToNext()) {
|
||||
String displayName = groupMembership.getString(groupMembership.getColumnIndexOrThrow(Data.DISPLAY_NAME));
|
||||
long contactId = groupMembership.getLong(groupMembership.getColumnIndexOrThrow(Data.CONTACT_ID));
|
||||
|
||||
contacts.add(getContactData(context, displayName, contactId));
|
||||
}
|
||||
} finally {
|
||||
if (groupMembership != null)
|
||||
groupMembership.close();
|
||||
}
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupData getGroupData(Context context, Cursor cursor) {
|
||||
GroupData groupData = new GroupData();
|
||||
groupData.id = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Groups._ID));
|
||||
groupData.name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Groups.TITLE));
|
||||
|
||||
return groupData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameForNumber(Context context, String number) {
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
||||
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri getContactsUri() {
|
||||
return ContactsContract.Contacts.CONTENT_URI;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
/**
|
||||
* 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.contacts;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.DatabaseUtils;
|
||||
import android.database.MergeCursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.Contacts;
|
||||
import android.provider.Contacts.GroupMembership;
|
||||
import android.provider.Contacts.People;
|
||||
import android.provider.Contacts.Phones;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
/**
|
||||
* A contact interface into the 1.x API for older clients.
|
||||
*
|
||||
* @author Stuart Anderson
|
||||
*/
|
||||
|
||||
public class ContactAccessorOldApi extends ContactAccessor {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static final String SORT_ORDER = Phones.NAME + "," + Phones.TYPE;
|
||||
@SuppressWarnings("deprecation")
|
||||
private static final String[] PROJECTION_PHONE = {
|
||||
Phones._ID, // 0
|
||||
Phones.PERSON_ID, // 1
|
||||
Phones.TYPE, // 2
|
||||
Phones.NUMBER, // 3
|
||||
Phones.LABEL, // 4
|
||||
Phones.DISPLAY_NAME, // 5
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public Cursor getCursorForRecipientFilter(CharSequence constraint,
|
||||
ContentResolver mContentResolver) {
|
||||
String phone = "";
|
||||
String wherePhone = null;
|
||||
|
||||
String cons = null;
|
||||
if (constraint != null) {
|
||||
cons = constraint.toString();
|
||||
|
||||
if (RecipientsAdapter.usefulAsDigits(cons)) {
|
||||
phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
|
||||
if (phone.equals(cons)) {
|
||||
phone = "";
|
||||
} else {
|
||||
phone = phone.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String filter = DatabaseUtils.sqlEscapeString(cons + '%');
|
||||
String filterLastName = DatabaseUtils.sqlEscapeString("% " + cons + '%');
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append("((name LIKE ");
|
||||
s.append(filter);
|
||||
s.append(") OR (name LIKE ");
|
||||
s.append(filterLastName);
|
||||
s.append(") OR (REPLACE(REPLACE(REPLACE(REPLACE(number, ' ', ''), '(', ''), ')', ''), '-', '') LIKE ");
|
||||
s.append(filter);
|
||||
s.append("))");
|
||||
|
||||
wherePhone = s.toString();
|
||||
|
||||
Cursor phoneCursor = mContentResolver.query(Phones.CONTENT_URI,
|
||||
PROJECTION_PHONE,
|
||||
wherePhone,
|
||||
null,
|
||||
SORT_ORDER);
|
||||
|
||||
//dumpCursor(phoneCursor);
|
||||
|
||||
|
||||
if (phone.length() > 0) {
|
||||
ArrayList result = new ArrayList();
|
||||
result.add(Integer.valueOf(-1)); // ID
|
||||
result.add(Long.valueOf(-1)); // CONTACT_ID
|
||||
result.add(Integer.valueOf(Phones.TYPE_CUSTOM)); // TYPE
|
||||
result.add(phone); // NUMBER
|
||||
|
||||
/*
|
||||
* The "\u00A0" keeps Phone.getDisplayLabel() from deciding
|
||||
* to display the default label ("Home") next to the transformation
|
||||
* of the letters into numbers.
|
||||
*/
|
||||
result.add("\u00A0"); // LABEL
|
||||
result.add(cons); // NAME
|
||||
|
||||
ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
|
||||
wrap.add(result);
|
||||
|
||||
ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);
|
||||
|
||||
return new MergeCursor(new Cursor[] { translated, phoneCursor });
|
||||
} else {
|
||||
return phoneCursor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public CharSequence phoneTypeToString(Context mContext, int type,
|
||||
CharSequence label) {
|
||||
return Phones.getDisplayLabel(mContext, type, label);
|
||||
}
|
||||
|
||||
public static void dumpCursor( Cursor c ) {
|
||||
c.moveToFirst();
|
||||
Log.d( "DC", "Begin:" );
|
||||
for( int i=0; i < c.getCount(); i++ ) {
|
||||
String rowStr = "";
|
||||
for( int j=0; j < c.getColumnCount(); j++ ) {
|
||||
rowStr = rowStr + c.getColumnName(j) + "=" + c.getString(j) +" ";
|
||||
}
|
||||
Log.d( "DC", rowStr + "\n" );
|
||||
c.moveToNext();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Intent getIntentForContactSelection() {
|
||||
return new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertIdentityKey(Context context, Uri uri, IdentityKey identityKey) {
|
||||
Toast.makeText(context, "Sorry, reading and writing identity keys to the contacts database is not supported on Android 1.X", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKey importIdentityKey(Context context, Uri uri) {
|
||||
Toast.makeText(context, "Sorry, reading and writing identity keys to the contacts database is not supported on Android 1.X", Toast.LENGTH_LONG).show();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameFromContact(Context context, Uri uri) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver) {
|
||||
LinkedList<String> numberList = new LinkedList<String>();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = contentResolver.query(Uri.withAppendedPath(Contacts.People.CONTENT_FILTER_URI, Uri.encode(constraint)),
|
||||
null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String number = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.NUMBER));
|
||||
if (number != null)
|
||||
numberList.add(number);
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return numberList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NameAndNumber getNameAndNumberFromContact(Context context, Uri uri) {
|
||||
Cursor cursor = null;
|
||||
NameAndNumber result = new NameAndNumber();
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri, null, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
result.name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
|
||||
result.number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
|
||||
|
||||
return result;
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactsWithNumbers(Context context) {
|
||||
return context.getContentResolver().query(People.CONTENT_URI,new String[]{People._ID,People.DISPLAY_NAME},
|
||||
People.NUMBER + " NOT NULL", null, "UPPER( " + People.DISPLAY_NAME + " ) ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContactData getContactData(Context context, Cursor cursor) {
|
||||
ContactData contactData = new ContactData();
|
||||
contactData.id = cursor.getLong(cursor.getColumnIndexOrThrow(People._ID));
|
||||
contactData.name = cursor.getString(cursor.getColumnIndexOrThrow(People.DISPLAY_NAME));
|
||||
contactData.numbers = getNumberDataForPersonId(context, contactData.id);
|
||||
|
||||
return contactData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactGroups(Context context) {
|
||||
return context.getContentResolver().query(Contacts.Groups.CONTENT_URI, null, null, null, Contacts.Groups.NAME + " ASC");
|
||||
}
|
||||
|
||||
private LinkedList<NumberData> getNumberDataForPersonId(Context context, long personId) {
|
||||
LinkedList<NumberData> numbers = new LinkedList<NumberData>();
|
||||
Cursor numberCursor = context.getContentResolver().query(Phones.CONTENT_URI, null,
|
||||
Phones.PERSON_ID + " = ?",
|
||||
new String[] {personId+""}, null);
|
||||
try {
|
||||
while (numberCursor != null && numberCursor.moveToNext()) {
|
||||
numbers.add(new NumberData(Phones.getDisplayLabel(context, numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phones.TYPE)), "").toString(),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phones.NUMBER))));
|
||||
}
|
||||
} finally {
|
||||
if (numberCursor != null)
|
||||
numberCursor.close();
|
||||
}
|
||||
|
||||
return numbers;
|
||||
}
|
||||
|
||||
private ContactData getContactDataFromGroupMembership(Context context, Cursor cursor) {
|
||||
ContactData contactData = new ContactData();
|
||||
contactData.id = cursor.getLong(cursor.getColumnIndexOrThrow(GroupMembership.PERSON_ID));
|
||||
|
||||
Cursor personCursor = context.getContentResolver().query(Uri.withAppendedPath(People.CONTENT_URI, contactData.id+""), null, null, null, null);
|
||||
|
||||
try {
|
||||
if (personCursor == null || !personCursor.moveToFirst())
|
||||
throw new AssertionError("Non-existent user in group?");
|
||||
|
||||
contactData.name = personCursor.getString(personCursor.getColumnIndexOrThrow(People.DISPLAY_NAME));
|
||||
contactData.numbers = getNumberDataForPersonId(context, contactData.id);
|
||||
|
||||
return contactData;
|
||||
|
||||
} finally {
|
||||
if (personCursor != null)
|
||||
personCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ContactData> getGroupMembership(Context context, long groupId) {
|
||||
LinkedList<ContactData> contacts = new LinkedList<ContactData>();
|
||||
Cursor groupMembershipCursor = context.getContentResolver().query(Contacts.GroupMembership.CONTENT_URI, null,
|
||||
GroupMembership.GROUP_ID + " = ?",
|
||||
new String[] {groupId+""}, null);
|
||||
|
||||
try {
|
||||
while (groupMembershipCursor != null && groupMembershipCursor.moveToNext()) {
|
||||
contacts.add(getContactDataFromGroupMembership(context, groupMembershipCursor));
|
||||
}
|
||||
} finally {
|
||||
if (groupMembershipCursor != null)
|
||||
groupMembershipCursor.close();
|
||||
}
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupData getGroupData(Context context, Cursor cursor) {
|
||||
GroupData groupData = new GroupData();
|
||||
groupData.id = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.Groups._ID));
|
||||
groupData.name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Groups.NAME));
|
||||
|
||||
return groupData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameForNumber(Context context, String number) {
|
||||
Cursor cursor = context.getContentResolver().query(Contacts.Phones.CONTENT_URI, null,
|
||||
Phones.NUMBER + " = ?",
|
||||
new String[] {number}, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.DISPLAY_NAME));
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri getContactsUri() {
|
||||
return Contacts.People.CONTENT_URI;
|
||||
}
|
||||
|
||||
}
|
||||
35
src/org/thoughtcrime/securesms/contacts/NameAndNumber.java
Normal file
35
src/org/thoughtcrime/securesms/contacts/NameAndNumber.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 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.contacts;
|
||||
|
||||
/**
|
||||
* Name and number tuple.
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*
|
||||
*/
|
||||
public class NameAndNumber {
|
||||
public String name;
|
||||
public String number;
|
||||
|
||||
public NameAndNumber(String name, String number) {
|
||||
this.name = name;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public NameAndNumber() {}
|
||||
}
|
||||
153
src/org/thoughtcrime/securesms/contacts/RecipientsAdapter.java
Normal file
153
src/org/thoughtcrime/securesms/contacts/RecipientsAdapter.java
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Esmertec AG.
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.contacts;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientsFormatter;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.text.Annotation;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.ResourceCursorAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* This adapter is used to filter contacts on both name and number.
|
||||
*/
|
||||
public class RecipientsAdapter extends ResourceCursorAdapter {
|
||||
|
||||
public static final int CONTACT_ID_INDEX = 1;
|
||||
public static final int TYPE_INDEX = 2;
|
||||
public static final int NUMBER_INDEX = 3;
|
||||
public static final int LABEL_INDEX = 4;
|
||||
public static final int NAME_INDEX = 5;
|
||||
|
||||
private final Context mContext;
|
||||
private final ContentResolver mContentResolver;
|
||||
private ContactAccessor mContactAccessor;
|
||||
|
||||
public RecipientsAdapter(Context context) {
|
||||
super(context, R.layout.recipient_filter_item, null);
|
||||
mContext = context;
|
||||
mContentResolver = context.getContentResolver();
|
||||
mContactAccessor = ContactAccessor.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final CharSequence convertToString(Cursor cursor) {
|
||||
String name = cursor.getString(RecipientsAdapter.NAME_INDEX);
|
||||
int type = cursor.getInt(RecipientsAdapter.TYPE_INDEX);
|
||||
String number = cursor.getString(RecipientsAdapter.NUMBER_INDEX).trim();
|
||||
|
||||
String label = cursor.getString(RecipientsAdapter.LABEL_INDEX);
|
||||
CharSequence displayLabel = mContactAccessor.phoneTypeToString(mContext, type, label);
|
||||
|
||||
if (number.length() == 0) {
|
||||
return number;
|
||||
}
|
||||
|
||||
if (name == null) {
|
||||
name = "";
|
||||
} else {
|
||||
// Names with commas are the bane of the recipient editor's existence.
|
||||
// We've worked around them by using spans, but there are edge cases
|
||||
// where the spans get deleted. Furthermore, having commas in names
|
||||
// can be confusing to the user since commas are used as separators
|
||||
// between recipients. The best solution is to simply remove commas
|
||||
// from names.
|
||||
name = name.replace(", ", " ")
|
||||
.replace(",", " "); // Make sure we leave a space between parts of names.
|
||||
}
|
||||
|
||||
String nameAndNumber = RecipientsFormatter.formatNameAndNumber(name, number);
|
||||
|
||||
SpannableString out = new SpannableString(nameAndNumber);
|
||||
int len = out.length();
|
||||
|
||||
if (!TextUtils.isEmpty(name)) {
|
||||
out.setSpan(new Annotation("name", name), 0, len,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
} else {
|
||||
out.setSpan(new Annotation("name", number), 0, len,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
}
|
||||
|
||||
String person_id = cursor.getString(RecipientsAdapter.CONTACT_ID_INDEX);
|
||||
out.setSpan(new Annotation("person_id", person_id), 0, len,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
out.setSpan(new Annotation("label", displayLabel.toString()), 0, len,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
out.setSpan(new Annotation("number", number), 0, len,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void bindView(View view, Context context, Cursor cursor) {
|
||||
TextView name = (TextView) view.findViewById(R.id.name);
|
||||
name.setText(cursor.getString(NAME_INDEX));
|
||||
|
||||
TextView label = (TextView) view.findViewById(R.id.label);
|
||||
int type = cursor.getInt(TYPE_INDEX);
|
||||
label.setText(mContactAccessor.phoneTypeToString(mContext, type, cursor.getString(LABEL_INDEX)));
|
||||
|
||||
TextView number = (TextView) view.findViewById(R.id.number);
|
||||
number.setText("(" + cursor.getString(NUMBER_INDEX) + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
|
||||
return mContactAccessor.getCursorForRecipientFilter( constraint, mContentResolver );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all the characters are meaningful as digits
|
||||
* in a phone number -- letters, digits, and a few punctuation marks.
|
||||
*/
|
||||
public static boolean usefulAsDigits(CharSequence cons) {
|
||||
int len = cons.length();
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = cons.charAt(i);
|
||||
|
||||
if ((c >= '0') && (c <= '9')) {
|
||||
continue;
|
||||
}
|
||||
if ((c == ' ') || (c == '-') || (c == '(') || (c == ')') || (c == '.') || (c == '+')
|
||||
|| (c == '#') || (c == '*')) {
|
||||
continue;
|
||||
}
|
||||
if ((c >= 'A') && (c <= 'Z')) {
|
||||
continue;
|
||||
}
|
||||
if ((c >= 'a') && (c <= 'z')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
427
src/org/thoughtcrime/securesms/contacts/RecipientsEditor.java
Normal file
427
src/org/thoughtcrime/securesms/contacts/RecipientsEditor.java
Normal file
@@ -0,0 +1,427 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Esmertec AG.
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.contacts;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
||||
import org.thoughtcrime.securesms.recipients.Recipients;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientsFormatter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.text.Annotation;
|
||||
import android.text.Editable;
|
||||
import android.text.Layout;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.ContextMenu.ContextMenuInfo;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.widget.MultiAutoCompleteTextView;
|
||||
|
||||
/**
|
||||
* Provide UI for editing the recipients of multi-media messages.
|
||||
*/
|
||||
public class RecipientsEditor extends MultiAutoCompleteTextView {
|
||||
private int mLongPressedPosition = -1;
|
||||
private final RecipientsEditorTokenizer mTokenizer;
|
||||
private char mLastSeparator = ',';
|
||||
private Context mContext;
|
||||
|
||||
public RecipientsEditor(Context context, AttributeSet attrs) {
|
||||
super(context, attrs, android.R.attr.autoCompleteTextViewStyle);
|
||||
mContext = context;
|
||||
mTokenizer = new RecipientsEditorTokenizer(context, this);
|
||||
setTokenizer(mTokenizer);
|
||||
// For the focus to move to the message body when soft Next is pressed
|
||||
setImeOptions(EditorInfo.IME_ACTION_NEXT);
|
||||
|
||||
/*
|
||||
* The point of this TextWatcher is that when the user chooses
|
||||
* an address completion from the AutoCompleteTextView menu, it
|
||||
* is marked up with Annotation objects to tie it back to the
|
||||
* address book entry that it came from. If the user then goes
|
||||
* back and edits that part of the text, it no longer corresponds
|
||||
* to that address book entry and needs to have the Annotations
|
||||
* claiming that it does removed.
|
||||
*/
|
||||
addTextChangedListener(new TextWatcher() {
|
||||
private Annotation[] mAffected;
|
||||
|
||||
public void beforeTextChanged(CharSequence s, int start,
|
||||
int count, int after) {
|
||||
mAffected = ((Spanned) s).getSpans(start, start + count,
|
||||
Annotation.class);
|
||||
}
|
||||
|
||||
public void onTextChanged(CharSequence s, int start,
|
||||
int before, int after) {
|
||||
if (before == 0 && after == 1) { // inserting a character
|
||||
char c = s.charAt(start);
|
||||
if (c == ',' || c == ';') {
|
||||
// Remember the delimiter the user typed to end this recipient. We'll
|
||||
// need it shortly in terminateToken().
|
||||
mLastSeparator = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (mAffected != null) {
|
||||
for (Annotation a : mAffected) {
|
||||
s.removeSpan(a);
|
||||
}
|
||||
}
|
||||
|
||||
mAffected = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enoughToFilter() {
|
||||
if (!super.enoughToFilter()) {
|
||||
return false;
|
||||
}
|
||||
// If the user is in the middle of editing an existing recipient, don't offer the
|
||||
// auto-complete menu. Without this, when the user selects an auto-complete menu item,
|
||||
// it will get added to the list of recipients so we end up with the old before-editing
|
||||
// recipient and the new post-editing recipient. As a precedent, gmail does not show
|
||||
// the auto-complete menu when editing an existing recipient.
|
||||
int end = getSelectionEnd();
|
||||
int len = getText().length();
|
||||
|
||||
return end == len;
|
||||
|
||||
}
|
||||
|
||||
public int getRecipientCount() {
|
||||
return mTokenizer.getNumbers().size();
|
||||
}
|
||||
|
||||
public List<String> getNumbers() {
|
||||
return mTokenizer.getNumbers();
|
||||
}
|
||||
|
||||
public Recipients constructContactsFromInput() {
|
||||
Recipients r = null;
|
||||
try {
|
||||
r = RecipientFactory.getRecipientsFromString(mContext, mTokenizer.getRawString() );
|
||||
} catch (RecipientFormattingException e) {
|
||||
Log.w( "RecipientsEditor", e);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private boolean isValidAddress(String number, boolean isMms) {
|
||||
/*if (isMms) {
|
||||
return MessageUtils.isValidMmsAddress(number);
|
||||
} else {*/
|
||||
// TODO: PhoneNumberUtils.isWellFormedSmsAddress() only check if the number is a valid
|
||||
// GSM SMS address. If the address contains a dialable char, it considers it a well
|
||||
// formed SMS addr. CDMA doesn't work that way and has a different parser for SMS
|
||||
// address (see CdmaSmsAddress.parse(String address)). We should definitely fix this!!!
|
||||
return PhoneNumberUtils.isWellFormedSmsAddress(number);
|
||||
}
|
||||
|
||||
public boolean hasValidRecipient(boolean isMms) {
|
||||
for (String number : mTokenizer.getNumbers()) {
|
||||
if (isValidAddress(number, isMms))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*public boolean hasInvalidRecipient(boolean isMms) {
|
||||
for (String number : mTokenizer.getNumbers()) {
|
||||
if (!isValidAddress(number, isMms)) {
|
||||
/* TODO if (MmsConfig.getEmailGateway() == null) {
|
||||
return true;
|
||||
} else if (!MessageUtils.isAlias(number)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}*/
|
||||
|
||||
public String formatInvalidNumbers(boolean isMms) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String number : mTokenizer.getNumbers()) {
|
||||
if (!isValidAddress(number, isMms)) {
|
||||
if (sb.length() != 0) {
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(number);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/*public boolean containsEmail() {
|
||||
if (TextUtils.indexOf(getText(), '@') == -1)
|
||||
return false;
|
||||
|
||||
List<String> numbers = mTokenizer.getNumbers();
|
||||
for (String number : numbers) {
|
||||
if (Mms.isEmailAddress(number))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}*/
|
||||
|
||||
public static CharSequence contactToToken(Recipient c) {
|
||||
String name = c.getName();
|
||||
String number = c.getNumber();
|
||||
SpannableString s = new SpannableString(RecipientsFormatter.formatNameAndNumber(name, number));
|
||||
int len = s.length();
|
||||
|
||||
if (len == 0) {
|
||||
return s;
|
||||
}
|
||||
|
||||
s.setSpan(new Annotation("number", c.getNumber()), 0, len,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public void populate(Recipients list) {
|
||||
SpannableStringBuilder sb = new SpannableStringBuilder();
|
||||
|
||||
for (Recipient c : list.getRecipientsList()) {
|
||||
if (sb.length() != 0) {
|
||||
sb.append(", ");
|
||||
}
|
||||
|
||||
sb.append(contactToToken(c));
|
||||
}
|
||||
|
||||
setText(sb);
|
||||
}
|
||||
|
||||
private int pointToPosition(int x, int y) {
|
||||
x -= getCompoundPaddingLeft();
|
||||
y -= getExtendedPaddingTop();
|
||||
|
||||
|
||||
x += getScrollX();
|
||||
y += getScrollY();
|
||||
|
||||
Layout layout = getLayout();
|
||||
if (layout == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int line = layout.getLineForVertical(y);
|
||||
int off = layout.getOffsetForHorizontal(line, x);
|
||||
|
||||
return off;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
final int action = ev.getAction();
|
||||
final int x = (int) ev.getX();
|
||||
final int y = (int) ev.getY();
|
||||
|
||||
if (action == MotionEvent.ACTION_DOWN) {
|
||||
mLongPressedPosition = pointToPosition(x, y);
|
||||
}
|
||||
|
||||
return super.onTouchEvent(ev);
|
||||
}
|
||||
|
||||
private static String getNumberAt(Spanned sp, int start, int end, Context context) {
|
||||
return getFieldAt("number", sp, start, end, context);
|
||||
}
|
||||
|
||||
private static int getSpanLength(Spanned sp, int start, int end, Context context) {
|
||||
// TODO: there's a situation where the span can lose its annotations:
|
||||
// - add an auto-complete contact
|
||||
// - add another auto-complete contact
|
||||
// - delete that second contact and keep deleting into the first
|
||||
// - we lose the annotation and can no longer get the span.
|
||||
// Need to fix this case because it breaks auto-complete contacts with commas in the name.
|
||||
Annotation[] a = sp.getSpans(start, end, Annotation.class);
|
||||
if (a.length > 0) {
|
||||
return sp.getSpanEnd(a[0]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static String getFieldAt(String field, Spanned sp, int start, int end,
|
||||
Context context) {
|
||||
Annotation[] a = sp.getSpans(start, end, Annotation.class);
|
||||
String fieldValue = getAnnotation(a, field);
|
||||
if (TextUtils.isEmpty(fieldValue)) {
|
||||
fieldValue = TextUtils.substring(sp, start, end);
|
||||
}
|
||||
return fieldValue;
|
||||
|
||||
}
|
||||
|
||||
private static String getAnnotation(Annotation[] a, String key) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i].getKey().equals(key)) {
|
||||
return a[i].getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private class RecipientsEditorTokenizer
|
||||
implements MultiAutoCompleteTextView.Tokenizer {
|
||||
private final MultiAutoCompleteTextView mList;
|
||||
private final Context mContext;
|
||||
|
||||
RecipientsEditorTokenizer(Context context, MultiAutoCompleteTextView list) {
|
||||
mList = list;
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the start of the token that ends at offset
|
||||
* <code>cursor</code> within <code>text</code>.
|
||||
* It is a method from the MultiAutoCompleteTextView.Tokenizer interface.
|
||||
*/
|
||||
public int findTokenStart(CharSequence text, int cursor) {
|
||||
int i = cursor;
|
||||
char c;
|
||||
|
||||
while (i > 0 && (c = text.charAt(i - 1)) != ',' && c != ';') {
|
||||
i--;
|
||||
}
|
||||
while (i < cursor && text.charAt(i) == ' ') {
|
||||
i++;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the end of the token (minus trailing punctuation)
|
||||
* that begins at offset <code>cursor</code> within <code>text</code>.
|
||||
* It is a method from the MultiAutoCompleteTextView.Tokenizer interface.
|
||||
*/
|
||||
public int findTokenEnd(CharSequence text, int cursor) {
|
||||
int i = cursor;
|
||||
int len = text.length();
|
||||
char c;
|
||||
|
||||
while (i < len) {
|
||||
if ((c = text.charAt(i)) == ',' || c == ';') {
|
||||
return i;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>text</code>, modified, if necessary, to ensure that
|
||||
* it ends with a token terminator (for example a space or comma).
|
||||
* It is a method from the MultiAutoCompleteTextView.Tokenizer interface.
|
||||
*/
|
||||
public CharSequence terminateToken(CharSequence text) {
|
||||
int i = text.length();
|
||||
|
||||
while (i > 0 && text.charAt(i - 1) == ' ') {
|
||||
i--;
|
||||
}
|
||||
|
||||
char c;
|
||||
if (i > 0 && ((c = text.charAt(i - 1)) == ',' || c == ';')) {
|
||||
return text;
|
||||
} else {
|
||||
// Use the same delimiter the user just typed.
|
||||
// This lets them have a mixture of commas and semicolons in their list.
|
||||
String separator = mLastSeparator + " ";
|
||||
if (text instanceof Spanned) {
|
||||
SpannableString sp = new SpannableString(text + separator);
|
||||
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
|
||||
Object.class, sp, 0);
|
||||
return sp;
|
||||
} else {
|
||||
return text + separator;
|
||||
}
|
||||
}
|
||||
}
|
||||
public String getRawString() {
|
||||
return mList.getText().toString();
|
||||
}
|
||||
public List<String> getNumbers() {
|
||||
Spanned sp = mList.getText();
|
||||
int len = sp.length();
|
||||
List<String> list = new ArrayList<String>();
|
||||
|
||||
int start = 0;
|
||||
int i = 0;
|
||||
while (i < len + 1) {
|
||||
char c;
|
||||
if ((i == len) || ((c = sp.charAt(i)) == ',') || (c == ';')) {
|
||||
if (i > start) {
|
||||
list.add(getNumberAt(sp, start, i, mContext));
|
||||
|
||||
// calculate the recipients total length. This is so if the name contains
|
||||
// commas or semis, we'll skip over the whole name to the next
|
||||
// recipient, rather than parsing this single name into multiple
|
||||
// recipients.
|
||||
int spanLen = getSpanLength(sp, start, i, mContext);
|
||||
if (spanLen > i) {
|
||||
i = spanLen;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
while ((i < len) && (sp.charAt(i) == ' ')) {
|
||||
i++;
|
||||
}
|
||||
|
||||
start = i;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
static class RecipientContextMenuInfo implements ContextMenuInfo {
|
||||
final Recipient recipient;
|
||||
|
||||
RecipientContextMenuInfo(Recipient r) {
|
||||
recipient = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user