CallDetailHistoryAdapter.java revision c37573179d14786670358275b898b8eb8fa4f150
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.FrameLayout; 30import android.widget.TextView; 31 32/** 33 * Adapter for a ListView containing history items from the details of a call. 34 */ 35public class CallDetailHistoryAdapter extends BaseAdapter { 36 private final Context mContext; 37 private final LayoutInflater mLayoutInflater; 38 private final CallTypeHelper mCallTypeHelper; 39 private final PhoneCallDetails[] mPhoneCallDetails; 40 41 public CallDetailHistoryAdapter(Context context, LayoutInflater layoutInflater, 42 CallTypeHelper callTypeHelper, PhoneCallDetails[] phoneCallDetails) { 43 mContext = context; 44 mLayoutInflater = layoutInflater; 45 mCallTypeHelper = callTypeHelper; 46 mPhoneCallDetails = phoneCallDetails; 47 } 48 49 @Override 50 public int getCount() { 51 return mPhoneCallDetails.length; 52 } 53 54 @Override 55 public Object getItem(int position) { 56 return mPhoneCallDetails[position]; 57 } 58 59 @Override 60 public long getItemId(int position) { 61 return position; 62 } 63 64 @Override 65 public View getView(int position, View convertView, ViewGroup parent) { 66 // Make sure we have a valid convertView to start with 67 if (convertView == null) { 68 convertView = mLayoutInflater.inflate(R.layout.call_detail_history_item, parent, false); 69 } 70 71 PhoneCallDetails details = mPhoneCallDetails[position]; 72 FrameLayout callTypeIconView = (FrameLayout) convertView.findViewById(R.id.call_type_icon); 73 TextView callTypeTextView = (TextView) convertView.findViewById(R.id.call_type_text); 74 TextView dateView = (TextView) convertView.findViewById(R.id.date); 75 TextView durationView = (TextView) convertView.findViewById(R.id.duration); 76 77 int callType = details.callTypes[0]; 78 callTypeIconView.removeAllViews(); 79 mCallTypeHelper.inflateCallTypeIcon(callType, callTypeIconView); 80 callTypeTextView.setText(mCallTypeHelper.getCallTypeText(callType)); 81 // Set the date. 82 CharSequence dateValue = DateUtils.formatDateRange(mContext, details.date, details.date, 83 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | 84 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR); 85 dateView.setText(dateValue); 86 // Set the duration 87 if (callType == Calls.MISSED_TYPE || callType == Calls.VOICEMAIL_TYPE) { 88 durationView.setVisibility(View.GONE); 89 } else { 90 durationView.setVisibility(View.VISIBLE); 91 durationView.setText(formatDuration(details.duration)); 92 } 93 94 return convertView; 95 } 96 97 private String formatDuration(long elapsedSeconds) { 98 long minutes = 0; 99 long seconds = 0; 100 101 if (elapsedSeconds >= 60) { 102 minutes = elapsedSeconds / 60; 103 elapsedSeconds -= minutes * 60; 104 } 105 seconds = elapsedSeconds; 106 107 return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds); 108 } 109} 110