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.dialer.calllog;
18
19import android.content.Context;
20import android.provider.CallLog.Calls;
21import android.text.format.DateUtils;
22import android.text.format.Formatter;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.BaseAdapter;
27import android.widget.TextView;
28
29import com.android.contacts.common.CallUtil;
30import com.android.dialer.PhoneCallDetails;
31import com.android.dialer.R;
32import com.android.dialer.util.DialerUtils;
33import com.google.common.collect.Lists;
34
35import java.util.ArrayList;
36
37/**
38 * Adapter for a ListView containing history items from the details of a call.
39 */
40public class CallDetailHistoryAdapter extends BaseAdapter {
41    /** Each history item shows the detail of a call. */
42    private static final int VIEW_TYPE_HISTORY_ITEM = 1;
43
44    private final Context mContext;
45    private final LayoutInflater mLayoutInflater;
46    private final CallTypeHelper mCallTypeHelper;
47    private final PhoneCallDetails[] mPhoneCallDetails;
48
49    /**
50     * List of items to be concatenated together for duration strings.
51     */
52    private ArrayList<CharSequence> mDurationItems = Lists.newArrayList();
53
54    public CallDetailHistoryAdapter(Context context, LayoutInflater layoutInflater,
55            CallTypeHelper callTypeHelper, PhoneCallDetails[] phoneCallDetails) {
56        mContext = context;
57        mLayoutInflater = layoutInflater;
58        mCallTypeHelper = callTypeHelper;
59        mPhoneCallDetails = phoneCallDetails;
60    }
61
62    @Override
63    public boolean isEnabled(int position) {
64        // None of history will be clickable.
65        return false;
66    }
67
68    @Override
69    public int getCount() {
70        return mPhoneCallDetails.length;
71    }
72
73    @Override
74    public Object getItem(int position) {
75        return mPhoneCallDetails[position];
76    }
77
78    @Override
79    public long getItemId(int position) {
80        return position;
81    }
82
83    @Override
84    public int getViewTypeCount() {
85        return 1;
86    }
87
88    @Override
89    public int getItemViewType(int position) {
90        return VIEW_TYPE_HISTORY_ITEM;
91    }
92
93    @Override
94    public View getView(int position, View convertView, ViewGroup parent) {
95        // Make sure we have a valid convertView to start with
96        final View result  = convertView == null
97                ? mLayoutInflater.inflate(R.layout.call_detail_history_item, parent, false)
98                : convertView;
99
100        PhoneCallDetails details = mPhoneCallDetails[position];
101        CallTypeIconsView callTypeIconView =
102                (CallTypeIconsView) result.findViewById(R.id.call_type_icon);
103        TextView callTypeTextView = (TextView) result.findViewById(R.id.call_type_text);
104        TextView dateView = (TextView) result.findViewById(R.id.date);
105        TextView durationView = (TextView) result.findViewById(R.id.duration);
106
107        int callType = details.callTypes[0];
108        boolean isVideoCall = (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO
109                && CallUtil.isVideoEnabled(mContext);
110
111        callTypeIconView.clear();
112        callTypeIconView.add(callType);
113        callTypeIconView.setShowVideo(isVideoCall);
114        callTypeTextView.setText(mCallTypeHelper.getCallTypeText(callType, isVideoCall));
115        // Set the date.
116        CharSequence dateValue = DateUtils.formatDateRange(mContext, details.date, details.date,
117                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
118                DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
119        dateView.setText(dateValue);
120        // Set the duration
121        if (Calls.VOICEMAIL_TYPE == callType || CallTypeHelper.isMissedCallType(callType)) {
122            durationView.setVisibility(View.GONE);
123        } else {
124            durationView.setVisibility(View.VISIBLE);
125            durationView.setText(formatDurationAndDataUsage(details.duration, details.dataUsage));
126        }
127
128        return result;
129    }
130
131    private CharSequence formatDuration(long elapsedSeconds) {
132        long minutes = 0;
133        long seconds = 0;
134
135        if (elapsedSeconds >= 60) {
136            minutes = elapsedSeconds / 60;
137            elapsedSeconds -= minutes * 60;
138            seconds = elapsedSeconds;
139            return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds);
140        } else {
141            seconds = elapsedSeconds;
142            return mContext.getString(R.string.callDetailsShortDurationFormat, seconds);
143        }
144    }
145
146    /**
147     * Formats a string containing the call duration and the data usage (if specified).
148     *
149     * @param elapsedSeconds Total elapsed seconds.
150     * @param dataUsage Data usage in bytes, or null if not specified.
151     * @return String containing call duration and data usage.
152     */
153    private CharSequence formatDurationAndDataUsage(long elapsedSeconds, Long dataUsage) {
154        CharSequence duration = formatDuration(elapsedSeconds);
155
156        if (dataUsage != null) {
157            mDurationItems.clear();
158            mDurationItems.add(duration);
159            mDurationItems.add(Formatter.formatShortFileSize(mContext, dataUsage));
160
161            return DialerUtils.join(mContext.getResources(), mDurationItems);
162        } else {
163            return duration;
164        }
165    }
166}
167