2017-11-17 00:21:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2017 Open Whisper Systems
|
2014-03-18 07:25:09 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2017-11-25 07:00:30 +01:00
|
|
|
import android.Manifest;
|
2014-03-18 07:25:09 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2015-10-19 20:23:12 +02:00
|
|
|
import android.database.MatrixCursor;
|
2015-07-14 23:31:03 +02:00
|
|
|
import android.database.MergeCursor;
|
2015-11-09 21:30:36 +01:00
|
|
|
import android.provider.ContactsContract;
|
2015-10-19 20:23:12 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2014-03-18 07:25:09 +01:00
|
|
|
import android.support.v4.content.CursorLoader;
|
2015-07-14 23:31:03 +02:00
|
|
|
import android.text.TextUtils;
|
2015-04-23 13:05:15 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-11-09 21:30:36 +01:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2017-07-26 18:59:15 +02:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-07-14 23:31:03 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-08-22 19:44:04 +02:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
2017-11-17 00:21:46 +01:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
2018-01-25 04:17:44 +01:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2017-08-01 17:56:00 +02:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-07-14 23:31:03 +02:00
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
2015-04-23 13:05:15 +02:00
|
|
|
|
2015-07-14 23:31:03 +02:00
|
|
|
import java.util.ArrayList;
|
2014-03-18 07:25:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CursorLoader that initializes a ContactsDatabase instance
|
|
|
|
*
|
|
|
|
* @author Jake McGinty
|
|
|
|
*/
|
|
|
|
public class ContactsCursorLoader extends CursorLoader {
|
|
|
|
|
2015-07-14 23:31:03 +02:00
|
|
|
private static final String TAG = ContactsCursorLoader.class.getSimpleName();
|
2014-03-18 07:25:09 +01:00
|
|
|
|
2017-11-17 00:21:46 +01:00
|
|
|
public static final int MODE_ALL = 0;
|
|
|
|
public static final int MODE_PUSH_ONLY = 1;
|
|
|
|
public static final int MODE_SMS_ONLY = 2;
|
|
|
|
|
|
|
|
private static final String[] CONTACT_PROJECTION = new String[]{ContactsDatabase.NAME_COLUMN,
|
|
|
|
ContactsDatabase.NUMBER_COLUMN,
|
|
|
|
ContactsDatabase.NUMBER_TYPE_COLUMN,
|
|
|
|
ContactsDatabase.LABEL_COLUMN,
|
|
|
|
ContactsDatabase.CONTACT_TYPE_COLUMN};
|
|
|
|
|
2015-07-14 23:31:03 +02:00
|
|
|
|
2017-11-17 00:21:46 +01:00
|
|
|
private final String filter;
|
|
|
|
private final int mode;
|
|
|
|
private final boolean recents;
|
2015-10-19 20:23:12 +02:00
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
public ContactsCursorLoader(@NonNull Context context, int mode, String filter, boolean recents)
|
2017-11-17 00:21:46 +01:00
|
|
|
{
|
2014-03-18 07:25:09 +01:00
|
|
|
super(context);
|
2015-07-14 23:31:03 +02:00
|
|
|
|
2017-11-17 00:21:46 +01:00
|
|
|
this.filter = filter;
|
|
|
|
this.mode = mode;
|
|
|
|
this.recents = recents;
|
2014-03-18 07:25:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor loadInBackground() {
|
2015-07-14 23:31:03 +02:00
|
|
|
ContactsDatabase contactsDatabase = DatabaseFactory.getContactsDatabase(getContext());
|
2017-11-17 00:21:46 +01:00
|
|
|
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(getContext());
|
|
|
|
ArrayList<Cursor> cursorList = new ArrayList<>(4);
|
|
|
|
|
|
|
|
if (recents && TextUtils.isEmpty(filter)) {
|
|
|
|
try (Cursor recentConversations = DatabaseFactory.getThreadDatabase(getContext()).getRecentConversationList(5)) {
|
|
|
|
MatrixCursor synthesizedContacts = new MatrixCursor(CONTACT_PROJECTION);
|
|
|
|
synthesizedContacts.addRow(new Object[] {getContext().getString(R.string.ContactsCursorLoader_recent_chats), "", ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, "", ContactsDatabase.DIVIDER_TYPE});
|
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
ThreadDatabase.Reader reader = threadDatabase.readerFor(recentConversations);
|
2017-11-17 00:21:46 +01:00
|
|
|
|
|
|
|
ThreadRecord threadRecord;
|
|
|
|
|
|
|
|
while ((threadRecord = reader.getNext()) != null) {
|
|
|
|
synthesizedContacts.addRow(new Object[] {threadRecord.getRecipient().toShortString(),
|
|
|
|
threadRecord.getRecipient().getAddress().serialize(),
|
|
|
|
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
|
|
|
|
"", ContactsDatabase.RECENT_TYPE});
|
|
|
|
}
|
|
|
|
|
|
|
|
synthesizedContacts.addRow(new Object[] {getContext().getString(R.string.ContactsCursorLoader_contacts), "", ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, "", ContactsDatabase.DIVIDER_TYPE});
|
|
|
|
if (synthesizedContacts.getCount() > 2) cursorList.add(synthesizedContacts);
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 23:31:03 +02:00
|
|
|
|
2017-11-25 07:00:30 +01:00
|
|
|
if (Permissions.hasAny(getContext(), Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
if (mode != MODE_SMS_ONLY) {
|
|
|
|
cursorList.add(contactsDatabase.queryTextSecureContacts(filter));
|
|
|
|
}
|
2015-07-14 23:31:03 +02:00
|
|
|
|
2017-11-25 07:00:30 +01:00
|
|
|
if (mode == MODE_ALL) {
|
|
|
|
cursorList.add(contactsDatabase.querySystemContacts(filter));
|
|
|
|
} else if (mode == MODE_SMS_ONLY) {
|
|
|
|
cursorList.add(filterNonPushContacts(contactsDatabase.querySystemContacts(filter)));
|
|
|
|
}
|
2015-04-23 13:05:15 +02:00
|
|
|
}
|
2014-03-18 07:25:09 +01:00
|
|
|
|
2015-07-14 23:31:03 +02:00
|
|
|
if (!TextUtils.isEmpty(filter) && NumberUtil.isValidSmsOrEmail(filter)) {
|
2017-11-17 00:21:46 +01:00
|
|
|
MatrixCursor newNumberCursor = new MatrixCursor(CONTACT_PROJECTION, 1);
|
|
|
|
|
|
|
|
newNumberCursor.addRow(new Object[] {getContext().getString(R.string.contact_selection_list__unknown_contact),
|
2015-11-09 21:30:36 +01:00
|
|
|
filter, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM,
|
|
|
|
"\u21e2", ContactsDatabase.NEW_TYPE});
|
|
|
|
|
|
|
|
cursorList.add(newNumberCursor);
|
2015-04-23 13:05:15 +02:00
|
|
|
}
|
2015-07-14 23:31:03 +02:00
|
|
|
|
2017-11-25 07:00:30 +01:00
|
|
|
if (cursorList.size() > 0) return new MergeCursor(cursorList.toArray(new Cursor[0]));
|
|
|
|
else return null;
|
2014-03-18 07:25:09 +01:00
|
|
|
}
|
2015-10-19 20:23:12 +02:00
|
|
|
|
|
|
|
private @NonNull Cursor filterNonPushContacts(@NonNull Cursor cursor) {
|
|
|
|
try {
|
|
|
|
final long startMillis = System.currentTimeMillis();
|
2017-11-17 00:21:46 +01:00
|
|
|
final MatrixCursor matrix = new MatrixCursor(CONTACT_PROJECTION);
|
2015-10-19 20:23:12 +02:00
|
|
|
while (cursor.moveToNext()) {
|
2017-08-01 17:56:00 +02:00
|
|
|
final String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.NUMBER_COLUMN));
|
2017-08-22 19:44:04 +02:00
|
|
|
final Recipient recipient = Recipient.from(getContext(), Address.fromExternal(getContext(), number), false);
|
2015-10-19 20:23:12 +02:00
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
if (recipient.resolve().getRegistered() != RecipientDatabase.RegisteredState.REGISTERED) {
|
2017-11-17 00:21:46 +01:00
|
|
|
matrix.addRow(new Object[]{cursor.getString(cursor.getColumnIndexOrThrow(ContactsDatabase.NAME_COLUMN)),
|
2015-10-19 20:23:12 +02:00
|
|
|
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-18 07:25:09 +01:00
|
|
|
}
|