• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.database.Cursor;
21 import android.database.MatrixCursor;
22 import android.database.MergeCursor;
23 import android.support.annotation.Nullable;
24 import com.android.dialer.searchfragment.common.SearchCursor;
25 
26 /** {@link SearchCursor} implementation for displaying on nearby places. */
27 final class NearbyPlacesCursor extends MergeCursor implements SearchCursor {
28 
29   private final Cursor nearbyPlacesCursor;
30   private final long directoryId;
31 
32   /**
33    * @param directoryId unique directory id that doesn't collide with other remote/local
34    *     directories. directoryIds are needed to load the correct quick contact card.
35    */
newInstance( Context context, Cursor nearbyPlacesCursor, long directoryId)36   static NearbyPlacesCursor newInstance(
37       Context context, Cursor nearbyPlacesCursor, long directoryId) {
38     MatrixCursor headerCursor = new MatrixCursor(HEADER_PROJECTION);
39     headerCursor.addRow(new String[] {context.getString(R.string.nearby_places)});
40     return new NearbyPlacesCursor(new Cursor[] {headerCursor, nearbyPlacesCursor}, directoryId);
41   }
42 
NearbyPlacesCursor(Cursor[] cursors, long directoryId)43   private NearbyPlacesCursor(Cursor[] cursors, long directoryId) {
44     super(cursors);
45     nearbyPlacesCursor = cursors[1];
46     this.directoryId = directoryId;
47   }
48 
49   @Override
isHeader()50   public boolean isHeader() {
51     return isFirst();
52   }
53 
54   @Override
updateQuery(@ullable String query)55   public boolean updateQuery(@Nullable String query) {
56     // When the query changes, a new network request is made for nearby places. Meaning this cursor
57     // will be closed and another created, so return false.
58     return false;
59   }
60 
61   @Override
getCount()62   public int getCount() {
63     // If we don't have any contents, we don't want to show the header
64     if (nearbyPlacesCursor == null || nearbyPlacesCursor.isClosed()) {
65       return 0;
66     }
67 
68     int count = nearbyPlacesCursor.getCount();
69     return count == 0 ? 0 : count + 1;
70   }
71 
72   @Override
getDirectoryId()73   public long getDirectoryId() {
74     return directoryId;
75   }
76 }
77