mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-25 13:58:40 +00:00
refactor and improve contact selection
* unify single and multi contact selection activities * follow android listview design recommendations more closely * add contact photos to selection * change indicator for push to be more obvious * cache circle-cropped bitmaps * dedupe numbers when contact has multiple of same phone number // FREEBIE
This commit is contained in:
@@ -20,9 +20,7 @@ import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.database.MergeCursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
@@ -33,6 +31,7 @@ import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.Data;
|
||||
import android.provider.ContactsContract.PhoneLookup;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -44,6 +43,7 @@ import org.whispersystems.textsecure.util.Base64;
|
||||
import java.io.IOException;
|
||||
import java.lang.Long;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -82,6 +82,10 @@ public class ContactAccessor {
|
||||
null, null, null, ContactsContract.Groups.TITLE + " ASC");
|
||||
}
|
||||
|
||||
public Loader<Cursor> getCursorLoaderForContacts(Context context) {
|
||||
return new ContactsCursorLoader(context);
|
||||
}
|
||||
|
||||
public Cursor getCursorForContactsWithNumbers(Context context) {
|
||||
Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
||||
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1";
|
||||
@@ -90,25 +94,28 @@ public class ContactAccessor {
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
}
|
||||
|
||||
public Cursor getCursorForContactsWithPush(Context context) {
|
||||
public Collection<ContactData> getContactsWithPush(Context context) {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};
|
||||
final String[] outProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME, PUSH_COLUMN};
|
||||
MatrixCursor cursor = new MatrixCursor(outProjection);
|
||||
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};
|
||||
|
||||
List<String> pushNumbers = Directory.getInstance(context).getActiveNumbers();
|
||||
final Collection<ContactData> lookupData = new ArrayList<ContactData>(pushNumbers.size());
|
||||
|
||||
for (String pushNumber : pushNumbers) {
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(pushNumber));
|
||||
Cursor lookupCursor = resolver.query(uri, inProjection, null, null, null);
|
||||
try {
|
||||
if (lookupCursor != null && lookupCursor.moveToFirst()) {
|
||||
cursor.addRow(new Object[]{lookupCursor.getLong(0), lookupCursor.getString(1), 1});
|
||||
final ContactData contactData = new ContactData(lookupCursor.getLong(0), lookupCursor.getString(1));
|
||||
contactData.numbers.add(new NumberData("TextSecure", pushNumber));
|
||||
lookupData.add(contactData);
|
||||
}
|
||||
} finally {
|
||||
if (lookupCursor != null)
|
||||
lookupCursor.close();
|
||||
}
|
||||
}
|
||||
return cursor;
|
||||
return lookupData;
|
||||
}
|
||||
|
||||
public String getNameFromContact(Context context, Uri uri) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.provider.ContactsContract.Contacts;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
|
||||
import java.io.InputStream;
|
||||
@@ -82,10 +83,13 @@ public class ContactPhotoFactory {
|
||||
localUserContactPhotoCache.remove(recipient.getContactUri());
|
||||
}
|
||||
|
||||
private static Bitmap getContactPhoto(Context context, Uri uri) {
|
||||
public static Bitmap getContactPhoto(Context context, Uri uri) {
|
||||
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
|
||||
|
||||
if (inputStream == null) return ContactPhotoFactory.getDefaultContactPhoto(context);
|
||||
else return BitmapFactory.decodeStream(inputStream);
|
||||
final Bitmap contactPhoto;
|
||||
if (inputStream == null) contactPhoto = ContactPhotoFactory.getDefaultContactPhoto(context);
|
||||
else contactPhoto = BitmapFactory.decodeStream(inputStream);
|
||||
|
||||
return contactPhoto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
/**
|
||||
* Copyright (C) 2014 Open 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 android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.provider.ContactsContract;
|
||||
import android.support.v4.widget.CursorAdapter;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.BitmapWorkerRunnable;
|
||||
import org.thoughtcrime.securesms.util.BitmapWorkerRunnable.AsyncDrawable;
|
||||
import org.thoughtcrime.securesms.util.TaggedFutureTask;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
|
||||
|
||||
/**
|
||||
* List adapter to display all contacts and their related information
|
||||
*
|
||||
* @author Jake McGinty
|
||||
*/
|
||||
public class ContactSelectionListAdapter extends CursorAdapter
|
||||
implements StickyListHeadersAdapter
|
||||
{
|
||||
private final static String TAG = "ContactListAdapter";
|
||||
|
||||
private final static ExecutorService photoResolver = Util.newSingleThreadedLifoExecutor();
|
||||
|
||||
private final static int STYLE_ATTRIBUTES[] = new int[]{R.attr.contact_selection_push_user,
|
||||
R.attr.contact_selection_lay_user,
|
||||
R.attr.contact_selection_label_text};
|
||||
|
||||
private int TYPE_COLUMN = -1;
|
||||
private int NAME_COLUMN = -1;
|
||||
private int NUMBER_COLUMN = -1;
|
||||
private int NUMBER_TYPE_COLUMN = -1;
|
||||
private int ID_COLUMN = -1;
|
||||
|
||||
private final Context context;
|
||||
private final boolean multiSelect;
|
||||
private final LayoutInflater li;
|
||||
private final TypedArray drawables;
|
||||
private final Bitmap defaultPhoto;
|
||||
private final Bitmap defaultCroppedPhoto;
|
||||
private final int scaledPhotoSize;
|
||||
|
||||
private final HashMap<Long, ContactAccessor.ContactData> selectedContacts = new HashMap<Long, ContactAccessor.ContactData>();
|
||||
|
||||
public ContactSelectionListAdapter(Context context, Cursor cursor, boolean multiSelect) {
|
||||
super(context, cursor, 0);
|
||||
this.context = context;
|
||||
this.li = LayoutInflater.from(context);
|
||||
this.drawables = context.obtainStyledAttributes(STYLE_ATTRIBUTES);
|
||||
this.multiSelect = multiSelect;
|
||||
this.defaultPhoto = ContactPhotoFactory.getDefaultContactPhoto(context);
|
||||
this.scaledPhotoSize = context.getResources().getDimensionPixelSize(R.dimen.contact_selection_photo_size);
|
||||
this.defaultCroppedPhoto = BitmapUtil.getScaledCircleCroppedBitmap(defaultPhoto, scaledPhotoSize);
|
||||
}
|
||||
|
||||
public static class ViewHolder {
|
||||
public CheckBox checkBox;
|
||||
public TextView name;
|
||||
public TextView number;
|
||||
public ImageView contactPhoto;
|
||||
public int position;
|
||||
}
|
||||
|
||||
public static class DataHolder {
|
||||
public int type;
|
||||
public String name;
|
||||
public String number;
|
||||
public int numberType;
|
||||
public long id;
|
||||
}
|
||||
|
||||
public static class HeaderViewHolder {
|
||||
TextView text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
||||
final View v = li.inflate(R.layout.push_contact_selection_list_item, parent, false);
|
||||
final ViewHolder holder = new ViewHolder();
|
||||
|
||||
if (v != null) {
|
||||
holder.name = (TextView) v.findViewById(R.id.name);
|
||||
holder.number = (TextView) v.findViewById(R.id.number);
|
||||
holder.checkBox = (CheckBox) v.findViewById(R.id.check_box);
|
||||
holder.contactPhoto = (ImageView) v.findViewById(R.id.contact_photo_image);
|
||||
|
||||
if (!multiSelect) holder.checkBox.setVisibility(View.GONE);
|
||||
|
||||
v.setTag(R.id.holder_tag, holder);
|
||||
v.setTag(R.id.contact_info_tag, new DataHolder());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindView(View view, Context context, Cursor cursor) {
|
||||
final DataHolder contactData = (DataHolder) view.getTag(R.id.contact_info_tag);
|
||||
final ViewHolder holder = (ViewHolder) view.getTag(R.id.holder_tag);
|
||||
if (holder == null) {
|
||||
Log.w(TAG, "ViewHolder was null. This should not happen.");
|
||||
return;
|
||||
}
|
||||
if (contactData == null) {
|
||||
Log.w(TAG, "DataHolder was null. This should not happen.");
|
||||
return;
|
||||
}
|
||||
if (ID_COLUMN < 0) {
|
||||
populateColumnIndices(cursor);
|
||||
}
|
||||
|
||||
contactData.type = cursor.getInt(TYPE_COLUMN);
|
||||
contactData.name = cursor.getString(NAME_COLUMN);
|
||||
contactData.number = cursor.getString(NUMBER_COLUMN);
|
||||
contactData.numberType = cursor.getInt(NUMBER_TYPE_COLUMN);
|
||||
contactData.id = cursor.getLong(ID_COLUMN);
|
||||
|
||||
if (contactData.type != ContactsDatabase.PUSH_TYPE) {
|
||||
holder.name.setTextColor(drawables.getColor(1, 0xff000000));
|
||||
holder.number.setTextColor(drawables.getColor(1, 0xff000000));
|
||||
} else {
|
||||
holder.name.setTextColor(drawables.getColor(0, 0xa0000000));
|
||||
holder.number.setTextColor(drawables.getColor(0, 0xa0000000));
|
||||
}
|
||||
|
||||
if (selectedContacts.containsKey(contactData.id)) {
|
||||
holder.checkBox.setChecked(true);
|
||||
} else {
|
||||
holder.checkBox.setChecked(false);
|
||||
}
|
||||
|
||||
holder.name.setText(contactData.name);
|
||||
|
||||
if (contactData.number == null || contactData.number.isEmpty()) {
|
||||
holder.name.setEnabled(false);
|
||||
holder.number.setText("");
|
||||
} else if (contactData.type == ContactsDatabase.PUSH_TYPE) {
|
||||
holder.number.setText(contactData.number);
|
||||
} else {
|
||||
final CharSequence label = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(),
|
||||
contactData.numberType, "");
|
||||
final CharSequence numberWithLabel = contactData.number + " " + label;
|
||||
final Spannable numberLabelSpan = new SpannableString(numberWithLabel);
|
||||
numberLabelSpan.setSpan(new ForegroundColorSpan(drawables.getColor(2, 0xff444444)), contactData.number.length(), numberWithLabel.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||
holder.number.setText(numberLabelSpan);
|
||||
}
|
||||
holder.contactPhoto.setImageBitmap(defaultCroppedPhoto);
|
||||
loadBitmap(contactData.number, holder.contactPhoto);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getHeaderView(int i, View convertView, ViewGroup viewGroup) {
|
||||
final Cursor c = getCursor();
|
||||
final HeaderViewHolder holder;
|
||||
if (convertView == null) {
|
||||
holder = new HeaderViewHolder();
|
||||
convertView = li.inflate(R.layout.push_contact_selection_list_header, viewGroup, false);
|
||||
holder.text = (TextView) convertView.findViewById(R.id.text);
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
holder = (HeaderViewHolder) convertView.getTag();
|
||||
}
|
||||
c.moveToPosition(i);
|
||||
|
||||
final int type = c.getInt(c.getColumnIndexOrThrow(ContactsDatabase.TYPE_COLUMN));
|
||||
final int headerTextRes;
|
||||
switch (type) {
|
||||
case 1: headerTextRes = R.string.contact_selection_list__header_textsecure_users; break;
|
||||
default: headerTextRes = R.string.contact_selection_list__header_other; break;
|
||||
}
|
||||
holder.text.setText(headerTextRes);
|
||||
return convertView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getHeaderId(int i) {
|
||||
final Cursor c = getCursor();
|
||||
c.moveToPosition(i);
|
||||
return c.getInt(c.getColumnIndexOrThrow(ContactsDatabase.TYPE_COLUMN));
|
||||
}
|
||||
|
||||
public boolean cancelPotentialWork(String number, ImageView imageView) {
|
||||
final TaggedFutureTask<?> bitmapWorkerTask = AsyncDrawable.getBitmapWorkerTask(imageView);
|
||||
|
||||
if (bitmapWorkerTask != null) {
|
||||
final Object tag = bitmapWorkerTask.getTag();
|
||||
if (tag != null && !tag.equals(number)) {
|
||||
bitmapWorkerTask.cancel(true);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void loadBitmap(String number, ImageView imageView) {
|
||||
if (cancelPotentialWork(number, imageView)) {
|
||||
final BitmapWorkerRunnable runnable = new BitmapWorkerRunnable(context, imageView, defaultPhoto, number, scaledPhotoSize);
|
||||
final TaggedFutureTask<?> task = new TaggedFutureTask<Void>(runnable, null, number);
|
||||
final AsyncDrawable asyncDrawable = new AsyncDrawable(context.getResources(), defaultCroppedPhoto, task);
|
||||
|
||||
imageView.setImageDrawable(asyncDrawable);
|
||||
if (!task.isCancelled()) photoResolver.execute(new FutureTask<Void>(task, null));
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long,ContactAccessor.ContactData> getSelectedContacts() {
|
||||
return selectedContacts;
|
||||
}
|
||||
|
||||
private void populateColumnIndices(final Cursor cursor) {
|
||||
this.TYPE_COLUMN = cursor.getColumnIndexOrThrow(ContactsDatabase.TYPE_COLUMN);
|
||||
this.NAME_COLUMN = cursor.getColumnIndexOrThrow(ContactsDatabase.NAME_COLUMN);
|
||||
this.NUMBER_COLUMN = cursor.getColumnIndexOrThrow(ContactsDatabase.NUMBER_COLUMN);
|
||||
this.NUMBER_TYPE_COLUMN = cursor.getColumnIndexOrThrow(ContactsDatabase.NUMBER_TYPE_COLUMN);
|
||||
this.ID_COLUMN = cursor.getColumnIndexOrThrow(ContactsDatabase.ID_COLUMN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (C) 2013 Open 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 android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
|
||||
/**
|
||||
* CursorLoader that initializes a ContactsDatabase instance
|
||||
*
|
||||
* @author Jake McGinty
|
||||
*/
|
||||
public class ContactsCursorLoader extends CursorLoader {
|
||||
|
||||
private final Context context;
|
||||
private ContactsDatabase db;
|
||||
|
||||
public ContactsCursorLoader(Context context) {
|
||||
super(context);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor loadInBackground() {
|
||||
db = new ContactsDatabase(context);
|
||||
return db.getAllContacts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReset() {
|
||||
super.onReset();
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
211
src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java
Normal file
211
src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java
Normal file
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* Copyright (C) 2013 Open 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 android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.CursorWrapper;
|
||||
import android.database.MergeCursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Database to supply all types of contacts that TextSecure needs to know about
|
||||
*
|
||||
* @author Jake McGinty
|
||||
*/
|
||||
public class ContactsDatabase {
|
||||
private static final String TAG = ContactsDatabase.class.getSimpleName();
|
||||
private final DatabaseOpenHelper dbHelper;
|
||||
private final Context context;
|
||||
|
||||
public static final String TABLE_NAME = "CONTACTS";
|
||||
public static final String ID_COLUMN = ContactsContract.CommonDataKinds.Phone._ID;
|
||||
public static final String NAME_COLUMN = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
|
||||
public static final String NUMBER_TYPE_COLUMN = ContactsContract.CommonDataKinds.Phone.TYPE;
|
||||
public static final String NUMBER_COLUMN = ContactsContract.CommonDataKinds.Phone.NUMBER;
|
||||
public static final String TYPE_COLUMN = "type";
|
||||
|
||||
private static final String CONTACT_LIST_SORT = NAME_COLUMN + " ASC";
|
||||
private static final String[] ANDROID_PROJECTION = new String[]{ID_COLUMN,
|
||||
NAME_COLUMN,
|
||||
NUMBER_TYPE_COLUMN,
|
||||
NUMBER_COLUMN};
|
||||
|
||||
public static final int NORMAL_TYPE = 0;
|
||||
public static final int PUSH_TYPE = 1;
|
||||
public static final int GROUP_TYPE = 2;
|
||||
|
||||
public ContactsDatabase(Context context) {
|
||||
this.dbHelper = new DatabaseOpenHelper(context);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
dbHelper.close();
|
||||
}
|
||||
|
||||
public Cursor getAllContacts() {
|
||||
return query(null, null, null);
|
||||
}
|
||||
|
||||
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
|
||||
final Cursor localCursor = queryLocalDb(selection, selectionArgs, columns);
|
||||
final Cursor androidCursor;
|
||||
|
||||
if (TextSecurePreferences.isSmsNonDataOutEnabled(context)) {
|
||||
androidCursor = queryAndroidDb();
|
||||
} else{
|
||||
return localCursor;
|
||||
}
|
||||
|
||||
if (localCursor != null && androidCursor != null) return new MergeCursor(new Cursor[]{localCursor,androidCursor});
|
||||
else if (localCursor != null) return localCursor;
|
||||
else if (androidCursor != null) return androidCursor;
|
||||
else return null;
|
||||
}
|
||||
|
||||
private Cursor queryAndroidDb() {
|
||||
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ANDROID_PROJECTION, null, null, CONTACT_LIST_SORT);
|
||||
return new TypedCursorWrapper(cursor);
|
||||
}
|
||||
|
||||
private Cursor queryLocalDb(String selection, String[] selectionArgs, String[] columns) {
|
||||
SQLiteDatabase localDb = dbHelper.getReadableDatabase();
|
||||
final Cursor localCursor;
|
||||
if (localDb != null) localCursor = localDb.query(TABLE_NAME, columns, selection, selectionArgs, null, null, CONTACT_LIST_SORT);
|
||||
else localCursor = null;
|
||||
if (localCursor != null && !localCursor.moveToFirst()) {
|
||||
localCursor.close();
|
||||
return null;
|
||||
}
|
||||
return localCursor;
|
||||
}
|
||||
|
||||
private static class DatabaseOpenHelper extends SQLiteOpenHelper {
|
||||
|
||||
private final Context context;
|
||||
private SQLiteDatabase mDatabase;
|
||||
|
||||
private static final String TABLE_CREATE =
|
||||
"CREATE TABLE " + TABLE_NAME + " (" +
|
||||
ID_COLUMN + " INTEGER PRIMARY KEY, " +
|
||||
NAME_COLUMN + " TEXT, " +
|
||||
NUMBER_TYPE_COLUMN + " INTEGER, " +
|
||||
NUMBER_COLUMN + " TEXT, " +
|
||||
TYPE_COLUMN + " INTEGER);";
|
||||
|
||||
DatabaseOpenHelper(Context context) {
|
||||
super(context, null, null, 1);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
Log.d(TAG, "onCreate called for contacts database.");
|
||||
mDatabase = db;
|
||||
mDatabase.execSQL(TABLE_CREATE);
|
||||
if (TextSecurePreferences.isPushRegistered(context)) {
|
||||
try {
|
||||
loadPushUsers();
|
||||
} catch (IOException ioe) {
|
||||
Log.e(TAG, "Issue when trying to load push users into memory db.", ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
|
||||
+ newVersion + ", which will destroy all old data");
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
|
||||
onCreate(db);
|
||||
}
|
||||
|
||||
private void loadPushUsers() throws IOException {
|
||||
Log.d(TAG, "populating push users into virtual db.");
|
||||
Collection<ContactAccessor.ContactData> pushUsers = ContactAccessor.getInstance().getContactsWithPush(context);
|
||||
for (ContactAccessor.ContactData user : pushUsers) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(ID_COLUMN, user.id);
|
||||
values.put(NAME_COLUMN, user.name);
|
||||
values.put(NUMBER_TYPE_COLUMN, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
|
||||
values.put(NUMBER_COLUMN, user.numbers.get(0).number);
|
||||
values.put(TYPE_COLUMN, PUSH_TYPE);
|
||||
mDatabase.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
Log.d(TAG, "finished populating push users.");
|
||||
}
|
||||
}
|
||||
|
||||
private static class TypedCursorWrapper extends CursorWrapper {
|
||||
|
||||
private final int pushColumnIndex;
|
||||
|
||||
public TypedCursorWrapper(Cursor cursor) {
|
||||
super(cursor);
|
||||
pushColumnIndex = cursor.getColumnCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumnCount() {
|
||||
return super.getColumnCount() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumnIndex(String columnName) {
|
||||
if (TYPE_COLUMN.equals(columnName)) return super.getColumnCount();
|
||||
else return super.getColumnIndex(columnName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
|
||||
if (TYPE_COLUMN.equals(columnName)) return super.getColumnCount();
|
||||
else return super.getColumnIndexOrThrow(columnName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnName(int columnIndex) {
|
||||
if (columnIndex == pushColumnIndex) return TYPE_COLUMN;
|
||||
else return super.getColumnName(columnIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getColumnNames() {
|
||||
final String[] columns = new String[super.getColumnCount() + 1];
|
||||
System.arraycopy(super.getColumnNames(), 0, columns, 0, super.getColumnCount());
|
||||
columns[pushColumnIndex] = TYPE_COLUMN;
|
||||
return columns;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(int columnIndex) {
|
||||
if (columnIndex == pushColumnIndex) return NORMAL_TYPE;
|
||||
else return super.getInt(columnIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user