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 17 package com.android.contacts.calllog; 18 19 import com.android.contacts.PhoneCallDetails; 20 import com.android.contacts.R; 21 22 import android.content.Context; 23 import android.provider.CallLog.Calls; 24 import android.text.format.DateUtils; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.widget.BaseAdapter; 29 import android.widget.TextView; 30 31 /** 32 * Adapter for a ListView containing history items from the details of a call. 33 */ 34 public 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 /** The controls that are shown on top of the history list. */ 49 private final View mControls; 50 /** The listener to changes of focus of the header. */ 51 private View.OnFocusChangeListener mHeaderFocusChangeListener = 52 new View.OnFocusChangeListener() { 53 @Override 54 public void onFocusChange(View v, boolean hasFocus) { 55 // When the header is focused, focus the controls above it instead. 56 if (hasFocus) { 57 mControls.requestFocus(); 58 } 59 } 60 }; 61 62 public CallDetailHistoryAdapter(Context context, LayoutInflater layoutInflater, 63 CallTypeHelper callTypeHelper, PhoneCallDetails[] phoneCallDetails, 64 boolean showVoicemail, boolean showCallAndSms, View controls) { 65 mContext = context; 66 mLayoutInflater = layoutInflater; 67 mCallTypeHelper = callTypeHelper; 68 mPhoneCallDetails = phoneCallDetails; 69 mShowVoicemail = showVoicemail; 70 mShowCallAndSms = showCallAndSms; 71 mControls = controls; 72 } 73 74 @Override 75 public int getCount() { 76 return mPhoneCallDetails.length + 1; 77 } 78 79 @Override 80 public Object getItem(int position) { 81 if (position == 0) { 82 return null; 83 } 84 return mPhoneCallDetails[position - 1]; 85 } 86 87 @Override 88 public long getItemId(int position) { 89 if (position == 0) { 90 return -1; 91 } 92 return position - 1; 93 } 94 95 @Override 96 public int getViewTypeCount() { 97 return 2; 98 } 99 100 @Override 101 public int getItemViewType(int position) { 102 if (position == 0) { 103 return VIEW_TYPE_HEADER; 104 } 105 return VIEW_TYPE_HISTORY_ITEM; 106 } 107 108 @Override 109 public View getView(int position, View convertView, ViewGroup parent) { 110 if (position == 0) { 111 final View header = convertView == null 112 ? mLayoutInflater.inflate(R.layout.call_detail_history_header, parent, false) 113 : convertView; 114 // Voicemail controls are only shown in the main UI if there is a voicemail. 115 View voicemailContainer = header.findViewById(R.id.header_voicemail_container); 116 voicemailContainer.setVisibility(mShowVoicemail ? View.VISIBLE : View.GONE); 117 // Call and SMS controls are only shown in the main UI if there is a known number. 118 View callAndSmsContainer = header.findViewById(R.id.header_call_and_sms_container); 119 callAndSmsContainer.setVisibility(mShowCallAndSms ? View.VISIBLE : View.GONE); 120 header.setFocusable(true); 121 header.setOnFocusChangeListener(mHeaderFocusChangeListener); 122 return header; 123 } 124 125 // Make sure we have a valid convertView to start with 126 final View result = convertView == null 127 ? mLayoutInflater.inflate(R.layout.call_detail_history_item, parent, false) 128 : convertView; 129 130 PhoneCallDetails details = mPhoneCallDetails[position - 1]; 131 CallTypeIconsView callTypeIconView = 132 (CallTypeIconsView) result.findViewById(R.id.call_type_icon); 133 TextView callTypeTextView = (TextView) result.findViewById(R.id.call_type_text); 134 TextView dateView = (TextView) result.findViewById(R.id.date); 135 TextView durationView = (TextView) result.findViewById(R.id.duration); 136 137 int callType = details.callTypes[0]; 138 callTypeIconView.clear(); 139 callTypeIconView.add(callType); 140 callTypeTextView.setText(mCallTypeHelper.getCallTypeText(callType)); 141 // Set the date. 142 CharSequence dateValue = DateUtils.formatDateRange(mContext, details.date, details.date, 143 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | 144 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR); 145 dateView.setText(dateValue); 146 // Set the duration 147 if (callType == Calls.MISSED_TYPE || callType == Calls.VOICEMAIL_TYPE) { 148 durationView.setVisibility(View.GONE); 149 } else { 150 durationView.setVisibility(View.VISIBLE); 151 durationView.setText(formatDuration(details.duration)); 152 } 153 154 return result; 155 } 156 157 private String formatDuration(long elapsedSeconds) { 158 long minutes = 0; 159 long seconds = 0; 160 161 if (elapsedSeconds >= 60) { 162 minutes = elapsedSeconds / 60; 163 elapsedSeconds -= minutes * 60; 164 } 165 seconds = elapsedSeconds; 166 167 return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds); 168 } 169 } 170