1 /* 2 * Copyright (C) 2017 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 17 package com.android.dialer.searchfragment.nearbyplaces; 18 19 import android.content.Context; 20 import android.content.CursorLoader; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.provider.ContactsContract; 24 import android.support.annotation.NonNull; 25 import com.android.contacts.common.extensions.PhoneDirectoryExtenderAccessor; 26 import com.android.dialer.common.LogUtil; 27 import com.android.dialer.searchfragment.common.Projections; 28 import java.util.List; 29 30 /** Cursor loader for nearby places search results. */ 31 public final class NearbyPlacesCursorLoader extends CursorLoader { 32 33 private static final String MAX_RESULTS = "3"; 34 private static final long INVALID_DIRECTORY_ID = Long.MAX_VALUE; 35 private final long directoryId; 36 37 /** 38 * @param directoryIds List of directoryIds associated with all directories on device. Required in 39 * order to find a directory ID for the nearby places cursor that doesn't collide with 40 * existing directories. 41 */ NearbyPlacesCursorLoader(Context context, String query, @NonNull List<Long> directoryIds)42 public NearbyPlacesCursorLoader(Context context, String query, @NonNull List<Long> directoryIds) { 43 super(context, getContentUri(context, query), Projections.DATA_PROJECTION, null, null, null); 44 this.directoryId = getDirectoryId(directoryIds); 45 } 46 47 @Override loadInBackground()48 public Cursor loadInBackground() { 49 if (directoryId == INVALID_DIRECTORY_ID) { 50 LogUtil.i("NearbyPlacesCursorLoader.loadInBackground", "directory id not set."); 51 return null; 52 } 53 return NearbyPlacesCursor.newInstance(getContext(), super.loadInBackground(), directoryId); 54 } 55 getContentUri(Context context, String query)56 private static Uri getContentUri(Context context, String query) { 57 return PhoneDirectoryExtenderAccessor.get(context) 58 .getContentUri() 59 .buildUpon() 60 .appendPath(query) 61 .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, MAX_RESULTS) 62 .build(); 63 } 64 getDirectoryId(List<Long> directoryIds)65 private static long getDirectoryId(List<Long> directoryIds) { 66 if (directoryIds.isEmpty()) { 67 return INVALID_DIRECTORY_ID; 68 } 69 70 // The Directory.LOCAL_INVISIBLE might not be a directory we use, but we can't reuse it's 71 // "special" ID. 72 long maxId = ContactsContract.Directory.LOCAL_INVISIBLE; 73 for (int i = 0, n = directoryIds.size(); i < n; i++) { 74 long id = directoryIds.get(i); 75 if (id > maxId) { 76 maxId = id; 77 } 78 } 79 // Add one so that the nearby places ID doesn't collide with extended directory IDs. 80 return maxId + 1; 81 } 82 } 83