/**
* Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
* This file is part of CSipSimple.
*
* CSipSimple is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* If you own a pjsip commercial license you can also redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as an android library.
*
* CSipSimple is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CSipSimple. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This file contains relicensed code from som Apache copyright of
* Copyright (C) 2011, The Android Open Source Project
*/
package com.csipsimple.ui.calllog;
import android.content.Context;
import android.provider.CallLog.Calls;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.csipsimple.R;
/**
* Adapter for a ListView containing history items from the details of a call.
*/
public class CallDetailHistoryAdapter extends BaseAdapter {
/** The top element is a blank header, which is hidden under the rest of the UI. */
private static final int VIEW_TYPE_HEADER = 0;
/** Each history item shows the detail of a call. */
private static final int VIEW_TYPE_HISTORY_ITEM = 1;
private final Context mContext;
private final LayoutInflater mLayoutInflater;
private final PhoneCallDetails[] mPhoneCallDetails;
public CallDetailHistoryAdapter(Context context, LayoutInflater layoutInflater,
PhoneCallDetails[] phoneCallDetails) {
mContext = context;
mLayoutInflater = layoutInflater;
mPhoneCallDetails = phoneCallDetails;
}
@Override
public int getCount() {
return mPhoneCallDetails.length;
}
@Override
public Object getItem(int position) {
return mPhoneCallDetails[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return VIEW_TYPE_HEADER;
}
return VIEW_TYPE_HISTORY_ITEM;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a valid convertView to start with
final View result = convertView == null
? mLayoutInflater.inflate(R.layout.call_detail_history_item, parent, false)
: convertView;
PhoneCallDetails details = (PhoneCallDetails) getItem(position);
CallTypeIconsView callTypeIconView =
(CallTypeIconsView) result.findViewById(R.id.call_type_icon);
TextView callTypeTextView = (TextView) result.findViewById(R.id.call_type_text);
TextView dateView = (TextView) result.findViewById(R.id.date);
TextView durationView = (TextView) result.findViewById(R.id.duration);
int callType = details.callTypes[0];
callTypeIconView.clear();
callTypeIconView.add(callType);
StringBuilder typeSb = new StringBuilder();
typeSb.append(mContext.getResources().getString(getCallTypeText(callType)));
// If not 200, we add text for user feedback about what went wrong
if(details.statusCode != 200) {
typeSb.append(" - ");
typeSb.append(details.statusCode);
if(!TextUtils.isEmpty(details.statusText)) {
typeSb.append(" / ");
typeSb.append(details.statusText);
}
}
callTypeTextView.setText(typeSb.toString());
// Set the date.
CharSequence dateValue = DateUtils.formatDateRange(mContext, details.date, details.date,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
dateView.setText(dateValue);
// Set the duration
if (callType == Calls.MISSED_TYPE) {
durationView.setVisibility(View.GONE);
} else {
durationView.setVisibility(View.VISIBLE);
durationView.setText(formatDuration(details.duration));
}
return result;
}
private int getCallTypeText(int callType) {
switch (callType) {
case Calls.INCOMING_TYPE:
return R.string.type_incoming;
case Calls.OUTGOING_TYPE:
return R.string.type_outgoing;
case Calls.MISSED_TYPE:
return R.string.type_missed;
default:
throw new IllegalArgumentException("invalid call type: " + callType);
}
}
private String formatDuration(long elapsedSeconds) {
long minutes = 0;
long seconds = 0;
if (elapsedSeconds >= 60) {
minutes = elapsedSeconds / 60;
elapsedSeconds -= minutes * 60;
}
seconds = elapsedSeconds;
return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds);
}
}