PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 1ms 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
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  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. package com.csipsimple.ui.calllog;
  26. import android.content.res.Resources;
  27. import android.graphics.Typeface;
  28. import android.text.SpannableString;
  29. import android.text.Spanned;
  30. import android.text.TextUtils;
  31. import android.text.format.DateUtils;
  32. import android.text.style.StyleSpan;
  33. import android.view.View;
  34. import android.widget.TextView;
  35. import com.csipsimple.R;
  36. import com.csipsimple.api.SipUri;
  37. /**
  38. * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
  39. */
  40. public class PhoneCallDetailsHelper {
  41. /**
  42. * The maximum number of icons will be shown to represent the call types in
  43. * a group.
  44. */
  45. private static final int MAX_CALL_TYPE_ICONS = 3;
  46. private final Resources mResources;
  47. /**
  48. * The injected current time in milliseconds since the epoch. Used only by
  49. * tests.
  50. */
  51. private Long mCurrentTimeMillisForTest;
  52. /**
  53. * Creates a new instance of the helper.
  54. * <p>
  55. * Generally you should have a single instance of this helper in any
  56. * context.
  57. *
  58. * @param resources used to look up strings
  59. */
  60. public PhoneCallDetailsHelper(Resources resources) {
  61. mResources = resources;
  62. }
  63. /** Fills the call details views with content. */
  64. public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
  65. // Display up to a given number of icons.
  66. views.callTypeIcons.clear();
  67. int count = details.callTypes.length;
  68. for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
  69. views.callTypeIcons.add(details.callTypes[index]);
  70. }
  71. views.callTypeIcons.setVisibility(View.VISIBLE);
  72. // Show the total call count only if there are more than the maximum
  73. // number of icons.
  74. Integer callCount;
  75. if (count > MAX_CALL_TYPE_ICONS) {
  76. callCount = count;
  77. } else {
  78. callCount = null;
  79. }
  80. // The date of this call, relative to the current time.
  81. CharSequence dateText =
  82. DateUtils.getRelativeTimeSpanString(details.date,
  83. getCurrentTimeMillis(),
  84. DateUtils.MINUTE_IN_MILLIS,
  85. DateUtils.FORMAT_ABBREV_RELATIVE);
  86. // Set the call count and date.
  87. setCallCountAndDate(views, callCount, dateText);
  88. // Display number and display name
  89. CharSequence displayName;
  90. if (!TextUtils.isEmpty(details.name)) {
  91. displayName = details.name;
  92. } else {
  93. // Try to fallback on number treat
  94. if (!TextUtils.isEmpty(details.number)) {
  95. displayName = SipUri.getDisplayedSimpleContact(details.number.toString());
  96. // SipUri.getCanonicalSipContact(details.number.toString(),
  97. // false);
  98. } else {
  99. displayName = mResources.getString(R.string.unknown);
  100. }
  101. if (!TextUtils.isEmpty(details.numberLabel)) {
  102. SpannableString text = new SpannableString(details.numberLabel + " " + displayName);
  103. text.setSpan(new StyleSpan(Typeface.BOLD), 0, details.numberLabel.length(),
  104. Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  105. displayName = text;
  106. }
  107. }
  108. views.nameView.setText(displayName);
  109. if (!TextUtils.isEmpty(details.formattedNumber)) {
  110. views.numberView.setText(details.formattedNumber);
  111. } else if (!TextUtils.isEmpty(details.number)) {
  112. views.numberView.setText(details.number);
  113. } else {
  114. // In this case we can assume that display name was set to unknown
  115. views.numberView.setText(displayName);
  116. }
  117. }
  118. /** Sets the text of the header view for the details page of a phone call. */
  119. public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
  120. CharSequence nameText;
  121. final CharSequence displayNumber = details.number;
  122. if (TextUtils.isEmpty(details.name)) {
  123. nameText = displayNumber;
  124. } else {
  125. nameText = details.name;
  126. }
  127. nameView.setText(nameText);
  128. }
  129. public void setCurrentTimeForTest(long currentTimeMillis) {
  130. mCurrentTimeMillisForTest = currentTimeMillis;
  131. }
  132. /**
  133. * Returns the current time in milliseconds since the epoch.
  134. * <p>
  135. * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
  136. */
  137. private long getCurrentTimeMillis() {
  138. if (mCurrentTimeMillisForTest == null) {
  139. return System.currentTimeMillis();
  140. } else {
  141. return mCurrentTimeMillisForTest;
  142. }
  143. }
  144. /** Sets the call count and date. */
  145. private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
  146. CharSequence dateText) {
  147. // Combine the count (if present) and the date.
  148. CharSequence text;
  149. if (callCount != null) {
  150. text = mResources.getString(
  151. R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
  152. } else {
  153. text = dateText;
  154. }
  155. views.callTypeAndDate.setText(text);
  156. }
  157. }