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