Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.contacts.common.list; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.CursorLoader; |
| 20 | import android.database.Cursor; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 21 | import android.database.MergeCursor; |
Sean Midford | c2deb41 | 2016-11-22 13:56:24 -0800 | [diff] [blame] | 22 | import android.database.sqlite.SQLiteException; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 24 | import android.provider.ContactsContract.Contacts; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 25 | |
| 26 | import com.google.common.collect.Lists; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
Wenyi Wang | be88bed | 2016-05-13 12:04:14 -0700 | [diff] [blame] | 31 | * A loader for use in the default contact list, which will also query for favorite contacts |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 32 | * if configured to do so. |
| 33 | */ |
Walter Jang | abb790c | 2016-11-16 13:50:12 -0800 | [diff] [blame] | 34 | public class FavoritesAndContactsLoader extends CursorLoader { |
Walter Jang | fddbb83 | 2016-08-16 13:12:27 -0700 | [diff] [blame] | 35 | |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 36 | private boolean mLoadFavorites; |
| 37 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 38 | private String[] mProjection; |
Walter Jang | 1f82861 | 2016-02-09 16:15:37 -0800 | [diff] [blame] | 39 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 40 | |
Wenyi Wang | be88bed | 2016-05-13 12:04:14 -0700 | [diff] [blame] | 41 | public FavoritesAndContactsLoader(Context context) { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 42 | super(context); |
| 43 | } |
| 44 | |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 45 | /** Whether to load favorites and merge results in before any other results. */ |
| 46 | public void setLoadFavorites(boolean flag) { |
| 47 | mLoadFavorites = flag; |
| 48 | } |
| 49 | |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 50 | public void setProjection(String[] projection) { |
| 51 | super.setProjection(projection); |
| 52 | mProjection = projection; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Cursor loadInBackground() { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 57 | List<Cursor> cursors = Lists.newArrayList(); |
Wenyi Wang | f412a79 | 2016-04-13 11:52:29 -0700 | [diff] [blame] | 58 | if (mLoadFavorites) { |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 59 | cursors.add(loadFavoritesContacts()); |
| 60 | } |
Walter Jang | abb790c | 2016-11-16 13:50:12 -0800 | [diff] [blame] | 61 | final Cursor contactsCursor = loadContacts(); |
| 62 | cursors.add(contactsCursor); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 63 | 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 Shrauner | 49dbee1 | 2014-01-30 14:02:18 -0800 | [diff] [blame] | 67 | return contactsCursor == null ? new Bundle() : contactsCursor.getExtras(); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | } |
| 71 | |
Walter Jang | fddbb83 | 2016-08-16 13:12:27 -0700 | [diff] [blame] | 72 | private Cursor loadContacts() { |
| 73 | // ContactsCursor.loadInBackground() can return null; MergeCursor |
| 74 | // correctly handles null cursors. |
| 75 | try { |
| 76 | return super.loadInBackground(); |
Sean Midford | c2deb41 | 2016-11-22 13:56:24 -0800 | [diff] [blame] | 77 | |
| 78 | } catch (NullPointerException | SQLiteException | SecurityException e) { |
| 79 | // Ignore NPEs, SQLiteExceptions and SecurityExceptions thrown by providers |
Walter Jang | fddbb83 | 2016-08-16 13:12:27 -0700 | [diff] [blame] | 80 | } |
| 81 | return null; |
Walter Jang | 5456440 | 2016-01-18 11:56:19 -0800 | [diff] [blame] | 82 | } |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 83 | |
| 84 | private Cursor loadFavoritesContacts() { |
yaolu | e81000b | 2016-10-03 17:07:34 -0700 | [diff] [blame] | 85 | 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 Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 92 | return getContext().getContentResolver().query( |
yaolu | e81000b | 2016-10-03 17:07:34 -0700 | [diff] [blame] | 93 | Contacts.CONTENT_URI, mProjection, selection.toString(), new String[]{"1"}, |
Wenyi Wang | 21fd2aa | 2016-06-05 15:52:07 -0700 | [diff] [blame] | 94 | getSortOrder()); |
Wenyi Wang | 25774d2 | 2016-04-08 11:15:11 -0700 | [diff] [blame] | 95 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 96 | } |