1 /*
2 
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package com.android.dialer.app.list;
18 
19 import android.content.Context;
20 import android.provider.ContactsContract.CommonDataKinds.Phone;
21 import android.provider.ContactsContract.QuickContact;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.ImageButton;
25 import android.widget.TextView;
26 import com.android.contacts.common.list.ContactEntry;
27 import com.android.dialer.app.R;
28 import com.android.dialer.logging.InteractionEvent;
29 import com.android.dialer.logging.Logger;
30 import com.android.dialer.widget.BidiTextView;
31 
32 /** Displays the contact's picture overlaid with their name and number type in a tile. */
33 public class PhoneFavoriteSquareTileView extends PhoneFavoriteTileView {
34 
35   private final float heightToWidthRatio;
36 
37   private ImageButton secondaryButton;
38 
39   private ContactEntry contactEntry;
40 
PhoneFavoriteSquareTileView(Context context, AttributeSet attrs)41   public PhoneFavoriteSquareTileView(Context context, AttributeSet attrs) {
42     super(context, attrs);
43 
44     heightToWidthRatio =
45         getResources().getFraction(R.dimen.contact_tile_height_to_width_ratio, 1, 1);
46   }
47 
48   @Override
onFinishInflate()49   protected void onFinishInflate() {
50     super.onFinishInflate();
51     BidiTextView nameView = findViewById(R.id.contact_tile_name);
52     nameView.setElegantTextHeight(false);
53 
54     TextView phoneTypeView = findViewById(R.id.contact_tile_phone_type);
55     phoneTypeView.setElegantTextHeight(false);
56     secondaryButton = findViewById(R.id.contact_tile_secondary_button);
57   }
58 
59   @Override
getApproximateImageSize()60   protected int getApproximateImageSize() {
61     // The picture is the full size of the tile (minus some padding, but we can be generous)
62     return getWidth();
63   }
64 
launchQuickContact()65   private void launchQuickContact() {
66     QuickContact.showQuickContact(
67         getContext(),
68         PhoneFavoriteSquareTileView.this,
69         getLookupUri(),
70         null,
71         Phone.CONTENT_ITEM_TYPE);
72   }
73 
74   @Override
loadFromContact(ContactEntry entry)75   public void loadFromContact(ContactEntry entry) {
76     super.loadFromContact(entry);
77     if (entry != null) {
78       secondaryButton.setOnClickListener(
79           new OnClickListener() {
80             @Override
81             public void onClick(View v) {
82               Logger.get(getContext())
83                   .logInteraction(InteractionEvent.Type.SPEED_DIAL_OPEN_CONTACT_CARD);
84               launchQuickContact();
85             }
86           });
87     }
88     contactEntry = entry;
89   }
90 
91   @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)92   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
93     final int width = MeasureSpec.getSize(widthMeasureSpec);
94     final int height = (int) (heightToWidthRatio * width);
95     final int count = getChildCount();
96     for (int i = 0; i < count; i++) {
97       getChildAt(i)
98           .measure(
99               MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
100               MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
101     }
102     setMeasuredDimension(width, height);
103   }
104 
105   @Override
getNameForView(ContactEntry contactEntry)106   protected String getNameForView(ContactEntry contactEntry) {
107     return contactEntry.getPreferredDisplayName(getContext());
108   }
109 
getContactEntry()110   public ContactEntry getContactEntry() {
111     return contactEntry;
112   }
113 }
114