PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/CSipSimple/src/com/csipsimple/ui/calllog/PhoneCallDetailsHelper.java

http://csipsimple.googlecode.com/
Java | 177 lines | 95 code | 20 blank | 62 comment | 21 complexity | 54c28eee44a907adfa177d9335c32582 MD5 | raw file
  1/**
  2 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
  3 * This file is part of CSipSimple.
  4 *
  5 *  CSipSimple is free software: you can redistribute it and/or modify
  6 *  it under the terms of the GNU General Public License as published by
  7 *  the Free Software Foundation, either version 3 of the License, or
  8 *  (at your option) any later version.
  9 *  If you own a pjsip commercial license you can also redistribute it
 10 *  and/or modify it under the terms of the GNU Lesser General Public License
 11 *  as an android library.
 12 *
 13 *  CSipSimple is distributed in the hope that it will be useful,
 14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 *  GNU General Public License for more details.
 17 *
 18 *  You should have received a copy of the GNU General Public License
 19 *  along with CSipSimple.  If not, see <http://www.gnu.org/licenses/>.
 20 */
 21/**
 22 * This file contains relicensed code from som Apache copyright of 
 23 * Copyright (C) 2011, The Android Open Source Project
 24 */
 25
 26package com.csipsimple.ui.calllog;
 27
 28import android.content.res.Resources;
 29import android.graphics.Typeface;
 30import android.text.SpannableString;
 31import android.text.Spanned;
 32import android.text.TextUtils;
 33import android.text.format.DateUtils;
 34import android.text.style.StyleSpan;
 35import android.view.View;
 36import android.widget.TextView;
 37
 38import com.csipsimple.R;
 39import com.csipsimple.api.SipUri;
 40
 41/**
 42 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
 43 */
 44public class PhoneCallDetailsHelper {
 45    /**
 46     * The maximum number of icons will be shown to represent the call types in
 47     * a group.
 48     */
 49    private static final int MAX_CALL_TYPE_ICONS = 3;
 50
 51    private final Resources mResources;
 52    /**
 53     * The injected current time in milliseconds since the epoch. Used only by
 54     * tests.
 55     */
 56    private Long mCurrentTimeMillisForTest;
 57
 58    /**
 59     * Creates a new instance of the helper.
 60     * <p>
 61     * Generally you should have a single instance of this helper in any
 62     * context.
 63     * 
 64     * @param resources used to look up strings
 65     */
 66    public PhoneCallDetailsHelper(Resources resources) {
 67        mResources = resources;
 68    }
 69
 70    /** Fills the call details views with content. */
 71    public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
 72        // Display up to a given number of icons.
 73        views.callTypeIcons.clear();
 74        int count = details.callTypes.length;
 75        for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
 76            views.callTypeIcons.add(details.callTypes[index]);
 77        }
 78        views.callTypeIcons.setVisibility(View.VISIBLE);
 79
 80        // Show the total call count only if there are more than the maximum
 81        // number of icons.
 82        Integer callCount;
 83        if (count > MAX_CALL_TYPE_ICONS) {
 84            callCount = count;
 85        } else {
 86            callCount = null;
 87        }
 88
 89        // The date of this call, relative to the current time.
 90        CharSequence dateText =
 91                DateUtils.getRelativeTimeSpanString(details.date,
 92                        getCurrentTimeMillis(),
 93                        DateUtils.MINUTE_IN_MILLIS,
 94                        DateUtils.FORMAT_ABBREV_RELATIVE);
 95
 96        // Set the call count and date.
 97        setCallCountAndDate(views, callCount, dateText);
 98
 99        // Display number and display name
100        CharSequence displayName;
101        if (!TextUtils.isEmpty(details.name)) {
102            displayName = details.name;
103        } else {
104            // Try to fallback on number treat
105            if (!TextUtils.isEmpty(details.number)) {
106                displayName = SipUri.getDisplayedSimpleContact(details.number.toString());
107                // SipUri.getCanonicalSipContact(details.number.toString(),
108                // false);
109            } else {
110                displayName = mResources.getString(R.string.unknown);
111            }
112
113            if (!TextUtils.isEmpty(details.numberLabel)) {
114                SpannableString text = new SpannableString(details.numberLabel + " " + displayName);
115                text.setSpan(new StyleSpan(Typeface.BOLD), 0, details.numberLabel.length(),
116                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
117                displayName = text;
118            }
119        }
120
121        views.nameView.setText(displayName);
122        if (!TextUtils.isEmpty(details.formattedNumber)) {
123            views.numberView.setText(details.formattedNumber);
124        } else if (!TextUtils.isEmpty(details.number)) {
125            views.numberView.setText(details.number);
126        } else {
127            // In this case we can assume that display name was set to unknown
128            views.numberView.setText(displayName);
129        }
130    }
131
132    /** Sets the text of the header view for the details page of a phone call. */
133    public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
134        CharSequence nameText;
135        final CharSequence displayNumber = details.number;
136        if (TextUtils.isEmpty(details.name)) {
137            nameText = displayNumber;
138        } else {
139            nameText = details.name;
140        }
141
142        nameView.setText(nameText);
143    }
144
145    public void setCurrentTimeForTest(long currentTimeMillis) {
146        mCurrentTimeMillisForTest = currentTimeMillis;
147    }
148
149    /**
150     * Returns the current time in milliseconds since the epoch.
151     * <p>
152     * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
153     */
154    private long getCurrentTimeMillis() {
155        if (mCurrentTimeMillisForTest == null) {
156            return System.currentTimeMillis();
157        } else {
158            return mCurrentTimeMillisForTest;
159        }
160    }
161
162    /** Sets the call count and date. */
163    private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
164            CharSequence dateText) {
165        // Combine the count (if present) and the date.
166        CharSequence text;
167        if (callCount != null) {
168            text = mResources.getString(
169                    R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
170        } else {
171            text = dateText;
172        }
173
174        views.callTypeAndDate.setText(text);
175    }
176
177}