session-android/src/org/thoughtcrime/securesms/contacts/ContactsCursorLoader.java

68 lines
2.1 KiB
Java
Raw Normal View History

/**
* 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-07-14 23:31:03 +02:00
import android.database.MergeCursor;
import android.support.v4.content.CursorLoader;
2015-07-14 23:31:03 +02:00
import android.text.TextUtils;
import android.util.Log;
2015-07-14 23:31:03 +02:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.util.NumberUtil;
2015-07-14 23:31:03 +02:00
import java.util.ArrayList;
/**
* 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();
2015-07-14 23:31:03 +02:00
private final String filter;
private boolean includeSmsContacts;
public ContactsCursorLoader(Context context, boolean includeSmsContacts, String filter) {
super(context);
2015-07-14 23:31:03 +02:00
this.filter = filter;
2015-07-14 23:31:03 +02:00
this.includeSmsContacts = includeSmsContacts;
}
@Override
public Cursor loadInBackground() {
2015-07-14 23:31:03 +02:00
ContactsDatabase contactsDatabase = DatabaseFactory.getContactsDatabase(getContext());
ArrayList<Cursor> cursorList = new ArrayList<>(3);
cursorList.add(contactsDatabase.queryTextSecureContacts(filter));
if (includeSmsContacts) {
cursorList.add(contactsDatabase.querySystemContacts(filter));
}
2015-07-14 23:31:03 +02:00
if (!TextUtils.isEmpty(filter) && NumberUtil.isValidSmsOrEmail(filter)) {
cursorList.add(contactsDatabase.getNewNumberCursor(filter));
}
2015-07-14 23:31:03 +02:00
return new MergeCursor(cursorList.toArray(new Cursor[0]));
}
}