Home | History | Annotate | Download | only in list
      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.list;
     18 
     19 import android.content.Context;
     20 import android.provider.ContactsContract.QuickContact;
     21 import android.text.TextUtils;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.widget.ImageButton;
     25 
     26 import com.android.contacts.common.R;
     27 import com.android.contacts.common.list.ContactEntry;
     28 
     29 import java.util.regex.Pattern;
     30 
     31 /**
     32  * Displays the contact's picture overlayed with their name
     33  * in a perfect square. It also has an additional touch target for a secondary action.
     34  */
     35 public class PhoneFavoriteSquareTileView extends PhoneFavoriteTileView {
     36     private static final String TAG = PhoneFavoriteSquareTileView.class.getSimpleName();
     37     private ImageButton mSecondaryButton;
     38 
     39     // TODO: Use a more expansive name token separator if needed. For now it should be fine to
     40     // not split by dashes, underscore etc.
     41     private static final Pattern NAME_TOKEN_SEPARATOR_PATTERN = Pattern.compile("\\s+");
     42 
     43     public PhoneFavoriteSquareTileView(Context context, AttributeSet attrs) {
     44         super(context, attrs);
     45     }
     46 
     47     @Override
     48     protected void onFinishInflate() {
     49         super.onFinishInflate();
     50 
     51         mSecondaryButton = (ImageButton) findViewById(R.id.contact_tile_secondary_button);
     52     }
     53 
     54     @Override
     55     protected int getApproximateImageSize() {
     56         // The picture is the full size of the tile (minus some padding, but we can be generous)
     57         return mListener.getApproximateTileWidth();
     58     }
     59 
     60     private void launchQuickContact() {
     61         QuickContact.showQuickContact(getContext(), PhoneFavoriteSquareTileView.this,
     62                 getLookupUri(), QuickContact.MODE_LARGE, null);
     63     }
     64 
     65     @Override
     66     protected String getNameForView(String name) {
     67         if (TextUtils.isEmpty(name)) return name;
     68         final String[] tokens = NAME_TOKEN_SEPARATOR_PATTERN.split(name, 2);
     69         if (tokens.length < 1) return name;
     70         return tokens[0];
     71     }
     72 
     73     @Override
     74     public void loadFromContact(ContactEntry entry) {
     75         super.loadFromContact(entry);
     76         if (entry != null) {
     77             final boolean contactIsFavorite = entry.isFavorite;
     78             mSecondaryButton.setVisibility(contactIsFavorite ? GONE : VISIBLE);
     79 
     80             if (contactIsFavorite) {
     81                 mStarView.setOnClickListener(new OnClickListener() {
     82                     @Override
     83                     public void onClick(View v) {
     84                         launchQuickContact();
     85                     }
     86                 });
     87             } else {
     88                 mSecondaryButton.setOnClickListener(new OnClickListener() {
     89                     @Override
     90                     public void onClick(View v) {
     91                         launchQuickContact();
     92                     }
     93                 });
     94             }
     95         }
     96     }
     97 }
     98