2014-03-17 23:25:09 -07:00
|
|
|
/**
|
|
|
|
* 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;
|
2015-10-19 11:23:12 -07:00
|
|
|
import android.database.MatrixCursor;
|
2015-07-14 14:31:03 -07:00
|
|
|
import android.database.MergeCursor;
|
2015-11-09 12:30:36 -08:00
|
|
|
import android.provider.ContactsContract;
|
2015-10-19 11:23:12 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2014-03-17 23:25:09 -07:00
|
|
|
import android.support.v4.content.CursorLoader;
|
2015-07-14 14:31:03 -07:00
|
|
|
import android.text.TextUtils;
|
2015-04-23 06:05:15 -05:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-11-09 12:30:36 -08:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-07-14 14:31:03 -07:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-10-19 11:23:12 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper;
|
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper.UserCapabilities.Capability;
|
2015-07-14 14:31:03 -07:00
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
2015-04-23 06:05:15 -05:00
|
|
|
|
2015-07-14 14:31:03 -07:00
|
|
|
import java.util.ArrayList;
|
2014-03-17 23:25:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CursorLoader that initializes a ContactsDatabase instance
|
|
|
|
*
|
|
|
|
* @author Jake McGinty
|
|
|
|
*/
|
|
|
|
public class ContactsCursorLoader extends CursorLoader {
|
|
|
|
|
2015-07-14 14:31:03 -07:00
|
|
|
private static final String TAG = ContactsCursorLoader.class.getSimpleName();
|
2014-03-17 23:25:09 -07:00
|
|
|
|
2017-05-07 18:59:18 -07:00
|
|
|
public final static int MODE_ALL = 0;
|
|
|
|
public final static int MODE_PUSH_ONLY = 1;
|
|
|
|
public final static int MODE_SMS_ONLY = 2;
|
2015-07-14 14:31:03 -07:00
|
|
|
|
2015-10-19 11:23:12 -07:00
|
|
|
private final String filter;
|
|
|
|
private final int mode;
|
|
|
|
|
|
|
|
public ContactsCursorLoader(Context context, int mode, String filter) {
|
2014-03-17 23:25:09 -07:00
|
|
|
super(context);
|
2015-07-14 14:31:03 -07:00
|
|
|
|
2015-10-19 11:23:12 -07:00
|
|
|
this.filter = filter;
|
|
|
|
this.mode = mode;
|
2014-03-17 23:25:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor loadInBackground() {
|
2015-07-14 14:31:03 -07:00
|
|
|
ContactsDatabase contactsDatabase = DatabaseFactory.getContactsDatabase(getContext());
|
|
|
|
ArrayList<Cursor> cursorList = new ArrayList<>(3);
|
|
|
|
|
2017-05-07 18:59:18 -07:00
|
|
|
if (mode != MODE_SMS_ONLY) {
|
2015-10-19 11:23:12 -07:00
|
|
|
cursorList.add(contactsDatabase.queryTextSecureContacts(filter));
|
|
|
|
}
|
2015-07-14 14:31:03 -07:00
|
|
|
|
2015-10-19 11:23:12 -07:00
|
|
|
if (mode == MODE_ALL) {
|
2015-07-14 14:31:03 -07:00
|
|
|
cursorList.add(contactsDatabase.querySystemContacts(filter));
|
2017-05-07 18:59:18 -07:00
|
|
|
} else if (mode == MODE_SMS_ONLY) {
|
2015-10-19 11:23:12 -07:00
|
|
|
cursorList.add(filterNonPushContacts(contactsDatabase.querySystemContacts(filter)));
|
2015-04-23 06:05:15 -05:00
|
|
|
}
|
2014-03-17 23:25:09 -07:00
|
|
|
|
2015-07-14 14:31:03 -07:00
|
|
|
if (!TextUtils.isEmpty(filter) && NumberUtil.isValidSmsOrEmail(filter)) {
|
2015-11-09 12:30:36 -08:00
|
|
|
MatrixCursor newNumberCursor = new MatrixCursor(new String[] {ContactsDatabase.ID_COLUMN,
|
|
|
|
ContactsDatabase.NAME_COLUMN,
|
|
|
|
ContactsDatabase.NUMBER_COLUMN,
|
|
|
|
ContactsDatabase.NUMBER_TYPE_COLUMN,
|
|
|
|
ContactsDatabase.LABEL_COLUMN,
|
|
|
|
ContactsDatabase.CONTACT_TYPE_COLUMN}, 1);
|
|
|
|
|
|
|
|
newNumberCursor.addRow(new Object[] {-1L, getContext().getString(R.string.contact_selection_list__unknown_contact),
|
|
|
|
filter, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM,
|
|
|
|
"\u21e2", ContactsDatabase.NEW_TYPE});
|
|
|
|
|
|
|
|
cursorList.add(newNumberCursor);
|
2015-04-23 06:05:15 -05:00
|
|
|
}
|
2015-07-14 14:31:03 -07:00
|
|
|
|
|
|
|
return new MergeCursor(cursorList.toArray(new Cursor[0]));
|
2014-03-17 23:25:09 -07:00
|
|
|
}
|
2015-10-19 11:23:12 -07:00
|
|
|
|
|
|
|
private @NonNull Cursor filterNonPushContacts(@NonNull Cursor cursor) {
|
|
|
|
try {
|
|
|
|
final long startMillis = System.currentTimeMillis();
|
|
|
|
final MatrixCursor matrix = new MatrixCursor(new String[]{ContactsDatabase.ID_COLUMN,
|
|
|
|
ContactsDatabase.NAME_COLUMN,
|
|
|
|
ContactsDatabase.NUMBER_COLUMN,
|
|
|
|
ContactsDatabase.NUMBER_TYPE_COLUMN,
|
|
|
|
ContactsDatabase.LABEL_COLUMN,
|
|
|
|
ContactsDatabase.CONTACT_TYPE_COLUMN});
|
|
|
|
while (cursor.moveToNext()) {
|
2017-08-01 08:56:00 -07:00
|
|
|
final String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.NUMBER_COLUMN));
|
|
|
|
final Recipient recipient = RecipientFactory.getRecipientFor(getContext(), Address.fromExternal(getContext(), number), true);
|
2015-10-19 11:23:12 -07:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
if (DirectoryHelper.getUserCapabilities(getContext(), recipient).getTextCapability() != Capability.SUPPORTED) {
|
2015-10-19 11:23:12 -07:00
|
|
|
matrix.addRow(new Object[]{cursor.getLong(cursor.getColumnIndexOrThrow(ContactsDatabase.ID_COLUMN)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.NAME_COLUMN)),
|
|
|
|
number,
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.NUMBER_TYPE_COLUMN)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.LABEL_COLUMN)),
|
|
|
|
ContactsDatabase.NORMAL_TYPE});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.w(TAG, "filterNonPushContacts() -> " + (System.currentTimeMillis() - startMillis) + "ms");
|
|
|
|
return matrix;
|
|
|
|
} finally {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2014-03-17 23:25:09 -07:00
|
|
|
}
|