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.net.Uri; 21 import android.provider.ContactsContract; 22 import android.provider.ContactsContract.Contacts; 23 import android.support.v7.widget.RecyclerView; 24 import android.view.View; 25 import android.widget.QuickContactBadge; 26 import android.widget.TextView; 27 import com.android.contacts.common.util.Constants; 28 import com.android.dialer.contactphoto.ContactPhotoManager; 29 import com.android.dialer.lettertile.LetterTileDrawable; 30 import com.android.dialer.searchfragment.common.Projections; 31 import com.android.dialer.searchfragment.common.QueryBoldingUtil; 32 import com.android.dialer.searchfragment.common.R; 33 import com.android.dialer.searchfragment.common.RowClickListener; 34 import com.android.dialer.searchfragment.common.SearchCursor; 35 36 /** ViewHolder for a nearby place row. */ 37 public final class NearbyPlaceViewHolder extends RecyclerView.ViewHolder 38 implements View.OnClickListener { 39 40 private final Context context; 41 private final TextView placeName; 42 private final TextView placeAddress; 43 private final QuickContactBadge photo; 44 private final RowClickListener listener; 45 46 private String number; 47 private int position; 48 NearbyPlaceViewHolder(View view, RowClickListener listener)49 public NearbyPlaceViewHolder(View view, RowClickListener listener) { 50 super(view); 51 view.setOnClickListener(this); 52 photo = view.findViewById(R.id.photo); 53 placeName = view.findViewById(R.id.primary); 54 placeAddress = view.findViewById(R.id.secondary); 55 context = view.getContext(); 56 this.listener = listener; 57 } 58 59 /** 60 * Binds the ViewHolder with a cursor from {@link NearbyPlacesCursorLoader} with the data found at 61 * the cursors set position. 62 */ bind(SearchCursor cursor, String query)63 public void bind(SearchCursor cursor, String query) { 64 number = cursor.getString(Projections.PHONE_NUMBER); 65 position = cursor.getPosition(); 66 String name = cursor.getString(Projections.DISPLAY_NAME); 67 String address = cursor.getString(Projections.PHONE_LABEL); 68 69 placeName.setText(QueryBoldingUtil.getNameWithQueryBolded(query, name, context)); 70 placeAddress.setText(QueryBoldingUtil.getNameWithQueryBolded(query, address, context)); 71 String photoUri = cursor.getString(Projections.PHOTO_URI); 72 ContactPhotoManager.getInstance(context) 73 .loadDialerThumbnailOrPhoto( 74 photo, 75 getContactUri(cursor), 76 cursor.getLong(Projections.PHOTO_ID), 77 photoUri == null ? null : Uri.parse(photoUri), 78 name, 79 LetterTileDrawable.TYPE_BUSINESS); 80 } 81 getContactUri(SearchCursor cursor)82 private static Uri getContactUri(SearchCursor cursor) { 83 // Since the lookup key for Nearby Places is actually a JSON representation of the information, 84 // we need to pass it in as an encoded fragment in our contact uri. 85 // It includes information like display name, photo uri, phone number, ect. 86 String businessInfoJson = cursor.getString(Projections.LOOKUP_KEY); 87 return Contacts.CONTENT_LOOKUP_URI 88 .buildUpon() 89 .appendPath(Constants.LOOKUP_URI_ENCODED) 90 .appendQueryParameter( 91 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(cursor.getDirectoryId())) 92 .encodedFragment(businessInfoJson) 93 .build(); 94 } 95 96 @Override onClick(View v)97 public void onClick(View v) { 98 listener.placeVoiceCall(number, position); 99 } 100 } 101