blob: 25dd711c096dda502a4d989a3e4aa07044f4e3da [file] [log] [blame]
Chiao Cheng89437e82012-11-01 13:41:51 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.contacts.common.list;
17
18import android.content.Context;
19import android.content.CursorLoader;
20import android.database.Cursor;
Chiao Cheng89437e82012-11-01 13:41:51 -070021import android.database.MergeCursor;
Sean Midfordc2deb412016-11-22 13:56:24 -080022import android.database.sqlite.SQLiteException;
Chiao Cheng89437e82012-11-01 13:41:51 -070023import android.os.Bundle;
Wenyi Wang25774d22016-04-08 11:15:11 -070024import android.provider.ContactsContract.Contacts;
Chiao Cheng89437e82012-11-01 13:41:51 -070025
26import com.google.common.collect.Lists;
27
28import java.util.List;
29
30/**
Wenyi Wangbe88bed2016-05-13 12:04:14 -070031 * A loader for use in the default contact list, which will also query for favorite contacts
Chiao Cheng89437e82012-11-01 13:41:51 -070032 * if configured to do so.
33 */
Walter Jangabb790c2016-11-16 13:50:12 -080034public class FavoritesAndContactsLoader extends CursorLoader {
Walter Jangfddbb832016-08-16 13:12:27 -070035
Wenyi Wang25774d22016-04-08 11:15:11 -070036 private boolean mLoadFavorites;
37
Chiao Cheng89437e82012-11-01 13:41:51 -070038 private String[] mProjection;
Walter Jang1f828612016-02-09 16:15:37 -080039
Chiao Cheng89437e82012-11-01 13:41:51 -070040
Wenyi Wangbe88bed2016-05-13 12:04:14 -070041 public FavoritesAndContactsLoader(Context context) {
Chiao Cheng89437e82012-11-01 13:41:51 -070042 super(context);
43 }
44
Wenyi Wang25774d22016-04-08 11:15:11 -070045 /** Whether to load favorites and merge results in before any other results. */
46 public void setLoadFavorites(boolean flag) {
47 mLoadFavorites = flag;
48 }
49
Chiao Cheng89437e82012-11-01 13:41:51 -070050 public void setProjection(String[] projection) {
51 super.setProjection(projection);
52 mProjection = projection;
53 }
54
55 @Override
56 public Cursor loadInBackground() {
Chiao Cheng89437e82012-11-01 13:41:51 -070057 List<Cursor> cursors = Lists.newArrayList();
Wenyi Wangf412a792016-04-13 11:52:29 -070058 if (mLoadFavorites) {
Wenyi Wang25774d22016-04-08 11:15:11 -070059 cursors.add(loadFavoritesContacts());
60 }
Walter Jangabb790c2016-11-16 13:50:12 -080061 final Cursor contactsCursor = loadContacts();
62 cursors.add(contactsCursor);
Chiao Cheng89437e82012-11-01 13:41:51 -070063 return new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) {
64 @Override
65 public Bundle getExtras() {
66 // Need to get the extras from the contacts cursor.
Jay Shrauner49dbee12014-01-30 14:02:18 -080067 return contactsCursor == null ? new Bundle() : contactsCursor.getExtras();
Chiao Cheng89437e82012-11-01 13:41:51 -070068 }
69 };
70 }
71
Walter Jangfddbb832016-08-16 13:12:27 -070072 private Cursor loadContacts() {
73 // ContactsCursor.loadInBackground() can return null; MergeCursor
74 // correctly handles null cursors.
75 try {
76 return super.loadInBackground();
Sean Midfordc2deb412016-11-22 13:56:24 -080077
78 } catch (NullPointerException | SQLiteException | SecurityException e) {
79 // Ignore NPEs, SQLiteExceptions and SecurityExceptions thrown by providers
Walter Jangfddbb832016-08-16 13:12:27 -070080 }
81 return null;
Walter Jang54564402016-01-18 11:56:19 -080082 }
Wenyi Wang25774d22016-04-08 11:15:11 -070083
84 private Cursor loadFavoritesContacts() {
yaolue81000b2016-10-03 17:07:34 -070085 final StringBuilder selection = new StringBuilder();
86 selection.append(Contacts.STARRED + "=?");
87 final ContactListFilter filter =
88 ContactListFilterController.getInstance(getContext()).getFilter();
89 if (filter != null && filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
90 selection.append(" AND ").append(Contacts.IN_VISIBLE_GROUP + "=1");
91 }
Wenyi Wang25774d22016-04-08 11:15:11 -070092 return getContext().getContentResolver().query(
yaolue81000b2016-10-03 17:07:34 -070093 Contacts.CONTENT_URI, mProjection, selection.toString(), new String[]{"1"},
Wenyi Wang21fd2aa2016-06-05 15:52:07 -070094 getSortOrder());
Wenyi Wang25774d22016-04-08 11:15:11 -070095 }
Chiao Cheng89437e82012-11-01 13:41:51 -070096}