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-07-14 14:31:03 -07:00
|
|
|
import android.database.MergeCursor;
|
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-07-14 14:31:03 -07:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
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
|
|
|
|
2015-07-14 14:31:03 -07:00
|
|
|
private final String filter;
|
|
|
|
private boolean includeSmsContacts;
|
|
|
|
|
|
|
|
public ContactsCursorLoader(Context context, boolean includeSmsContacts, String filter) {
|
2014-03-17 23:25:09 -07:00
|
|
|
super(context);
|
2015-07-14 14:31:03 -07:00
|
|
|
|
2014-04-01 16:40:16 -07:00
|
|
|
this.filter = filter;
|
2015-07-14 14:31:03 -07:00
|
|
|
this.includeSmsContacts = includeSmsContacts;
|
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);
|
|
|
|
|
|
|
|
cursorList.add(contactsDatabase.queryTextSecureContacts(filter));
|
|
|
|
|
|
|
|
if (includeSmsContacts) {
|
|
|
|
cursorList.add(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)) {
|
|
|
|
cursorList.add(contactsDatabase.getNewNumberCursor(filter));
|
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
|
|
|
}
|
|
|
|
}
|