• 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.car.dialer;
18 
19 import android.database.Cursor;
20 import android.net.Uri;
21 import android.provider.ContactsContract.Contacts;
22 import android.support.v7.widget.RecyclerView;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 import androidx.car.widget.PagedListView;
31 
32 /**
33  *  An adapter that will parse a list of contacts given by a {@link Cursor} that display the
34  *  results as a list.
35  */
36 public class ContactResultsAdapter extends RecyclerView.Adapter<ContactResultViewHolder>
37         implements PagedListView.ItemCap {
38     private final List<ContactResultViewHolder.ContactDetails> mContacts = new ArrayList<>();
39 
40     /**
41      * Clears all contact results from this adapter.
42      */
clear()43     public void clear() {
44         mContacts.clear();
45         notifyDataSetChanged();
46     }
47 
48     /**
49      * Sets the list of contacts that should be displayed. The given {@link Cursor} can be safely
50      * closed after this call.
51      */
setData(Cursor data)52     public void setData(Cursor data) {
53         mContacts.clear();
54 
55         while (data.moveToNext()) {
56             int idColIdx = data.getColumnIndex(Contacts._ID);
57             int lookupColIdx = data.getColumnIndex(Contacts.LOOKUP_KEY);
58             int nameColIdx = data.getColumnIndex(Contacts.DISPLAY_NAME);
59             int photoUriColIdx = data.getColumnIndex(Contacts.PHOTO_URI);
60 
61             Uri lookupUri = Contacts.getLookupUri(
62                     data.getLong(idColIdx), data.getString(lookupColIdx));
63 
64             mContacts.add(new ContactResultViewHolder.ContactDetails(
65                     data.getString(nameColIdx),
66                     data.getString(photoUriColIdx),
67                     lookupUri));
68         }
69 
70         notifyDataSetChanged();
71     }
72 
73     @Override
onCreateViewHolder(ViewGroup parent, int viewType)74     public ContactResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
75         View view = LayoutInflater.from(parent.getContext())
76                 .inflate(R.layout.contact_result, parent, false);
77         return new ContactResultViewHolder(view);
78     }
79 
80     @Override
onBindViewHolder(ContactResultViewHolder holder, int position)81     public void onBindViewHolder(ContactResultViewHolder holder, int position) {
82         holder.bind(mContacts.get(position), getItemCount());
83     }
84 
85     @Override
getItemViewType(int position)86     public int getItemViewType(int position) {
87         // Only one type of view is created, so no need for an individualized view type.
88         return 0;
89     }
90 
91     @Override
getItemCount()92     public int getItemCount() {
93         return mContacts.size();
94     }
95 
96     @Override
setMaxItems(int max)97     public void setMaxItems(int max) {
98         // No-op. A PagedListView needs the ItemCap interface to be implemented. However, the
99         // list of contacts not be limited.
100     }
101 }
102