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 */
16
17package com.android.contacts.calllog;
18
19import android.content.res.Resources;
20import android.provider.CallLog.Calls;
21import android.text.TextUtils;
22import android.view.View;
23
24import com.android.contacts.PhoneCallDetails;
25import com.android.contacts.PhoneCallDetailsHelper;
26import com.android.contacts.R;
27
28/**
29 * Helper class to fill in the views of a call log entry.
30 */
31/*package*/ class CallLogListItemHelper {
32    /** Helper for populating the details of a phone call. */
33    private final PhoneCallDetailsHelper mPhoneCallDetailsHelper;
34    /** Helper for handling phone numbers. */
35    private final PhoneNumberHelper mPhoneNumberHelper;
36    /** Resources to look up strings. */
37    private final Resources mResources;
38
39    /**
40     * Creates a new helper instance.
41     *
42     * @param phoneCallDetailsHelper used to set the details of a phone call
43     * @param phoneNumberHelper used to process phone number
44     */
45    public CallLogListItemHelper(PhoneCallDetailsHelper phoneCallDetailsHelper,
46            PhoneNumberHelper phoneNumberHelper, Resources resources) {
47        mPhoneCallDetailsHelper = phoneCallDetailsHelper;
48        mPhoneNumberHelper = phoneNumberHelper;
49        mResources = resources;
50    }
51
52    /**
53     * Sets the name, label, and number for a contact.
54     *
55     * @param views the views to populate
56     * @param details the details of a phone call needed to fill in the data
57     * @param isHighlighted whether to use the highlight text for the call
58     */
59    public void setPhoneCallDetails(CallLogListItemViews views, PhoneCallDetails details,
60            boolean isHighlighted) {
61        mPhoneCallDetailsHelper.setPhoneCallDetails(views.phoneCallDetailsViews, details,
62                isHighlighted);
63        boolean canCall = mPhoneNumberHelper.canPlaceCallsTo(details.number);
64        boolean canPlay = details.callTypes[0] == Calls.VOICEMAIL_TYPE;
65
66        if (canPlay) {
67            // Playback action takes preference.
68            configurePlaySecondaryAction(views, isHighlighted);
69            views.dividerView.setVisibility(View.VISIBLE);
70        } else if (canCall) {
71            // Call is the secondary action.
72            configureCallSecondaryAction(views, details);
73            views.dividerView.setVisibility(View.VISIBLE);
74        } else {
75            // No action available.
76            views.secondaryActionView.setVisibility(View.GONE);
77            views.dividerView.setVisibility(View.GONE);
78        }
79    }
80
81    /** Sets the secondary action to correspond to the call button. */
82    private void configureCallSecondaryAction(CallLogListItemViews views,
83            PhoneCallDetails details) {
84        views.secondaryActionView.setVisibility(View.VISIBLE);
85        views.secondaryActionView.setImageResource(R.drawable.ic_ab_dialer_holo_dark);
86        views.secondaryActionView.setContentDescription(getCallActionDescription(details));
87    }
88
89    /** Returns the description used by the call action for this phone call. */
90    private CharSequence getCallActionDescription(PhoneCallDetails details) {
91        final CharSequence recipient;
92        if (!TextUtils.isEmpty(details.name)) {
93            recipient = details.name;
94        } else {
95            recipient = mPhoneNumberHelper.getDisplayNumber(
96                    details.number, details.formattedNumber);
97        }
98        return mResources.getString(R.string.description_call, recipient);
99    }
100
101    /** Sets the secondary action to correspond to the play button. */
102    private void configurePlaySecondaryAction(CallLogListItemViews views, boolean isHighlighted) {
103        views.secondaryActionView.setVisibility(View.VISIBLE);
104        views.secondaryActionView.setImageResource(
105                isHighlighted ? R.drawable.ic_play_active_holo_dark : R.drawable.ic_play_holo_dark);
106        views.secondaryActionView.setContentDescription(
107                mResources.getString(R.string.description_call_log_play_button));
108    }
109}
110