1/*
2 * Copyright (C) 2011 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 */
16package com.android.contacts.list;
17
18import com.android.contacts.ContactPhotoManager;
19import com.android.contacts.R;
20import com.android.contacts.list.ContactTileAdapter.ContactEntry;
21
22import android.content.Context;
23import android.net.Uri;
24import android.util.AttributeSet;
25import android.util.Log;
26import android.view.View;
27import android.widget.FrameLayout;
28import android.widget.ImageView;
29import android.widget.QuickContactBadge;
30import android.widget.TextView;
31
32/**
33 * A ContactTile displays the contact's picture overlayed with their name
34 */
35public class ContactTileView extends FrameLayout {
36    private final static String TAG = ContactTileView.class.getSimpleName();
37
38    private Uri mLookupUri;
39    private ImageView mPhoto;
40    private QuickContactBadge mQuickContact;
41    private TextView mName;
42    private TextView mStatus;
43    private TextView mPhoneLabel;
44    private TextView mPhoneNumber;
45    private ContactPhotoManager mPhotoManager = null;
46    private View mPushState;
47    private View mHorizontalDivider;
48    private Listener mListener;
49
50    public ContactTileView(Context context, AttributeSet attrs) {
51        super(context, attrs);
52    }
53
54    @Override
55    protected void onFinishInflate() {
56        super.onFinishInflate();
57        mName = (TextView) findViewById(R.id.contact_tile_name);
58
59        mQuickContact = (QuickContactBadge) findViewById(R.id.contact_tile_quick);
60        mPhoto = (ImageView) findViewById(R.id.contact_tile_image);
61        mStatus = (TextView) findViewById(R.id.contact_tile_status);
62        mPhoneLabel = (TextView) findViewById(R.id.contact_tile_phone_type);
63        mPhoneNumber = (TextView) findViewById(R.id.contact_tile_phone_number);
64        mPushState = findViewById(R.id.contact_tile_push_state);
65        mHorizontalDivider = findViewById(R.id.contact_tile_horizontal_divider);
66
67        OnClickListener listener = new OnClickListener() {
68            @Override
69            public void onClick(View v) {
70                if (mListener != null) {
71                    mListener.onClick(ContactTileView.this);
72                }
73            }
74        };
75
76        if(mPushState != null) {
77            mPushState.setOnClickListener(listener);
78        } else {
79            setOnClickListener(listener);
80        }
81    }
82
83    public void setPhotoManager(ContactPhotoManager photoManager) {
84        mPhotoManager = photoManager;
85    }
86
87    /**
88     * Populates the data members to be displayed from the
89     * fields in {@link ContactEntry}
90     */
91    public void loadFromContact(ContactEntry entry) {
92
93        if (entry != null) {
94            mName.setText(entry.name);
95            mLookupUri = entry.lookupKey;
96
97            if (mStatus != null) {
98                if (entry.status == null) {
99                    mStatus.setVisibility(View.GONE);
100                } else {
101                    mStatus.setText(entry.status);
102                    mStatus.setCompoundDrawablesWithIntrinsicBounds(entry.presenceIcon,
103                            null, null, null);
104                    mStatus.setVisibility(View.VISIBLE);
105                }
106            }
107
108            if (mPhoneLabel != null) {
109                mPhoneLabel.setText(entry.phoneLabel);
110            }
111
112            if (mPhoneNumber != null) {
113                // TODO: Format number correctly
114                mPhoneNumber.setText(entry.phoneNumber);
115            }
116
117            setVisibility(View.VISIBLE);
118
119            if (mPhotoManager != null) {
120                if (mPhoto != null) {
121                    mPhotoManager.loadPhoto(mPhoto, entry.photoUri, isDefaultIconHires(),
122                            isDarkTheme());
123
124                    if (mQuickContact != null) {
125                        mQuickContact.assignContactUri(mLookupUri);
126                    }
127                } else if (mQuickContact != null) {
128                    mQuickContact.assignContactUri(mLookupUri);
129                    mPhotoManager.loadPhoto(mQuickContact, entry.photoUri, isDefaultIconHires(),
130                            isDarkTheme());
131                }
132
133            } else {
134                Log.w(TAG, "contactPhotoManager not set");
135            }
136
137            if (mPushState != null) {
138                mPushState.setContentDescription(entry.name);
139            } else if (mQuickContact != null) {
140                mQuickContact.setContentDescription(entry.name);
141            }
142        } else {
143            setVisibility(View.INVISIBLE);
144        }
145    }
146
147    public void setListener(Listener listener) {
148        mListener = listener;
149    }
150
151    public void setHorizontalDividerVisibility(int visibility) {
152        if (mHorizontalDivider != null) mHorizontalDivider.setVisibility(visibility);
153    }
154
155    public Uri getLookupUri() {
156        return mLookupUri;
157    }
158
159    protected boolean isDefaultIconHires() {
160        return false;
161    }
162
163    protected boolean isDarkTheme() {
164        return false;
165    }
166
167    public interface Listener {
168        void onClick(ContactTileView contactTileView);
169    }
170}
171