CallDetailHistoryAdapter.java revision d1333a29ffe2b85b7e2bcb0a5d9801e1ffa62cd2
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 com.android.contacts.PhoneCallDetails;
20import com.android.contacts.R;
21
22import android.content.Context;
23import android.provider.CallLog.Calls;
24import android.text.format.DateUtils;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.BaseAdapter;
29import android.widget.TextView;
30
31/**
32 * Adapter for a ListView containing history items from the details of a call.
33 */
34public class CallDetailHistoryAdapter extends BaseAdapter {
35    /** The top element is a blank header, which is hidden under the rest of the UI. */
36    private static final int VIEW_TYPE_HEADER = 0;
37    /** Each history item shows the detail of a call. */
38    private static final int VIEW_TYPE_HISTORY_ITEM = 1;
39
40    private final Context mContext;
41    private final LayoutInflater mLayoutInflater;
42    private final CallTypeHelper mCallTypeHelper;
43    private final PhoneCallDetails[] mPhoneCallDetails;
44    /** Whether the voicemail controls are shown. */
45    private final boolean mShowVoicemail;
46    /** Whether the call and SMS controls are shown. */
47    private final boolean mShowCallAndSms;
48
49    public CallDetailHistoryAdapter(Context context, LayoutInflater layoutInflater,
50            CallTypeHelper callTypeHelper, PhoneCallDetails[] phoneCallDetails,
51            boolean showVoicemail, boolean showCallAndSms) {
52        mContext = context;
53        mLayoutInflater = layoutInflater;
54        mCallTypeHelper = callTypeHelper;
55        mPhoneCallDetails = phoneCallDetails;
56        mShowVoicemail = showVoicemail;
57        mShowCallAndSms = showCallAndSms;
58    }
59
60    @Override
61    public int getCount() {
62        return mPhoneCallDetails.length + 1;
63    }
64
65    @Override
66    public Object getItem(int position) {
67        if (position == 0) {
68            return null;
69        }
70        return mPhoneCallDetails[position - 1];
71    }
72
73    @Override
74    public long getItemId(int position) {
75        if (position == 0) {
76            return -1;
77        }
78        return position - 1;
79    }
80
81    @Override
82    public int getViewTypeCount() {
83        return 2;
84    }
85
86    @Override
87    public int getItemViewType(int position) {
88        if (position == 0) {
89            return VIEW_TYPE_HEADER;
90        }
91        return VIEW_TYPE_HISTORY_ITEM;
92    }
93
94    @Override
95    public View getView(int position, View convertView, ViewGroup parent) {
96        if (position == 0) {
97            final View header = convertView == null
98                    ? mLayoutInflater.inflate(R.layout.call_detail_history_header, parent, false)
99                    : convertView;
100            // Voicemail controls are only shown in the main UI if there is a voicemail.
101            View voicemailContainer = header.findViewById(R.id.header_voicemail_container);
102            voicemailContainer.setVisibility(mShowVoicemail ? View.VISIBLE : View.GONE);
103            // Call and SMS controls are only shown in the main UI if there is a known number.
104            View callAndSmsContainer = header.findViewById(R.id.header_call_and_sms_container);
105            callAndSmsContainer.setVisibility(mShowCallAndSms ? View.VISIBLE : View.GONE);
106            return header;
107        }
108
109        // Make sure we have a valid convertView to start with
110        final View result  = convertView == null
111                ? mLayoutInflater.inflate(R.layout.call_detail_history_item, parent, false)
112                : convertView;
113
114        PhoneCallDetails details = mPhoneCallDetails[position - 1];
115        CallTypeIconsView callTypeIconView =
116                (CallTypeIconsView) result.findViewById(R.id.call_type_icon);
117        TextView callTypeTextView = (TextView) result.findViewById(R.id.call_type_text);
118        TextView dateView = (TextView) result.findViewById(R.id.date);
119        TextView durationView = (TextView) result.findViewById(R.id.duration);
120
121        int callType = details.callTypes[0];
122        callTypeIconView.clear();
123        callTypeIconView.add(callType);
124        callTypeTextView.setText(mCallTypeHelper.getCallTypeText(callType));
125        // Set the date.
126        CharSequence dateValue = DateUtils.formatDateRange(mContext, details.date, details.date,
127                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
128                DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
129        dateView.setText(dateValue);
130        // Set the duration
131        if (callType == Calls.MISSED_TYPE || callType == Calls.VOICEMAIL_TYPE) {
132            durationView.setVisibility(View.GONE);
133        } else {
134            durationView.setVisibility(View.VISIBLE);
135            durationView.setText(formatDuration(details.duration));
136        }
137
138        return result;
139    }
140
141    private String formatDuration(long elapsedSeconds) {
142        long minutes = 0;
143        long seconds = 0;
144
145        if (elapsedSeconds >= 60) {
146            minutes = elapsedSeconds / 60;
147            elapsedSeconds -= minutes * 60;
148        }
149        seconds = elapsedSeconds;
150
151        return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds);
152    }
153}
154