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