/* Copyright © 2013-2014, Silent Circle, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Any redistribution, use, or modification is done solely for personal benefit and not for any commercial purpose or for monetary gain * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name Silent Circle nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SILENT CIRCLE, LLC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * This implementation is edited version of original Android sources. */ /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.silentcircle.contacts; import java.text.SimpleDateFormat; import java.util.Date; import android.content.res.Resources; import android.graphics.Typeface; import android.text.SpannableString; import android.text.Spanned; import android.text.TextUtils; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.view.View; import android.widget.TextView; import com.silentcircle.contacts.calllog.PhoneNumberHelper; import com.silentcircle.contacts.R; import com.silentcircle.contacts.calllog.CallTypeHelper; // import com.android.contacts.test.NeededForTesting; import com.silentcircle.contacts.model.account.LabelHelper.PhoneLabel; /** * Helper class to fill in the views in {@link PhoneCallDetailsViews}. */ public class PhoneCallDetailsHelper { /** The maximum number of icons will be shown to represent the call types in a group. */ private static final int MAX_CALL_TYPE_ICONS = 3; private final Resources mResources; /** The injected current time in milliseconds since the epoch. Used only by tests. */ private Long mCurrentTimeMillisForTest; // Helper classes. private final CallTypeHelper mCallTypeHelper; private final PhoneNumberHelper mPhoneNumberHelper; /** * Creates a new instance of the helper. * <p> * Generally you should have a single instance of this helper in any context. * * @param resources used to look up strings */ public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, PhoneNumberHelper phoneNumberHelper) { mResources = resources; mCallTypeHelper = callTypeHelper; mPhoneNumberHelper = phoneNumberHelper; } /** Fills the call details views with content. */ public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, boolean isHighlighted) { // Display up to a given number of icons. views.callTypeIcons.clear(); int count = details.callTypes.length; for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { views.callTypeIcons.add(details.callTypes[index]); } views.callTypeIcons.setVisibility(View.VISIBLE); // views.contentIcon.setImageResource(R.drawable.spa_list_view); // Show the total call count only if there are more than the maximum number of icons. final Integer callCount; if (count > MAX_CALL_TYPE_ICONS) { callCount = count; } else { callCount = null; } // The color to highlight the count and date in, if any. This is based on the first call. Integer highlightColor = isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null; // The date of this call, relative to the current time. CharSequence dateText = SimpleDateFormat.getTimeInstance().format(new Date(details.date)); // Set the call count and date. setCallCountAndDate(views, callCount, dateText, highlightColor); CharSequence numberFormattedLabel = null; // Only show a label if the number is shown and it is not a SIP address. if (!TextUtils.isEmpty(details.number) && !com.silentcircle.contacts.utils.PhoneNumberHelper.isUriNumber(details.number.toString())) { numberFormattedLabel = PhoneLabel.getTypeLabel(mResources, details.numberType, details.numberLabel); } final CharSequence nameText; final CharSequence numberText; final CharSequence labelText; final CharSequence displayNumber = mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber); if (TextUtils.isEmpty(details.name)) { nameText = displayNumber; if (TextUtils.isEmpty(details.geocode)) { numberText = mResources.getString(R.string.call_log_empty_gecode); } else { numberText = details.geocode; } labelText = null; } else { nameText = details.name; numberText = displayNumber; labelText = numberFormattedLabel; } views.nameView.setText(nameText); views.numberView.setText(numberText); views.labelView.setText(labelText); views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE); } /** Sets the text of the header view for the details page of a phone call. */ public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) { final CharSequence nameText; final CharSequence displayNumber = mPhoneNumberHelper.getDisplayNumber(details.number, mResources.getString(R.string.recentCalls_addToContact)); if (TextUtils.isEmpty(details.name)) { nameText = displayNumber; } else { nameText = details.name; } nameView.setText(nameText); } // @NeededForTesting public void setCurrentTimeForTest(long currentTimeMillis) { mCurrentTimeMillisForTest = currentTimeMillis; } /** * Returns the current time in milliseconds since the epoch. * <p> * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. */ private long getCurrentTimeMillis() { if (mCurrentTimeMillisForTest == null) { return System.currentTimeMillis(); } else { return mCurrentTimeMillisForTest; } } /** Sets the call count and date. */ private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount, CharSequence dateText, Integer highlightColor) { // Combine the count (if present) and the date. final CharSequence text; if (callCount != null) { text = mResources.getString(R.string.call_log_item_count_and_date, callCount.intValue(), dateText); } else { text = dateText; } // Apply the highlight color if present. final CharSequence formattedText; if (highlightColor != null) { formattedText = addBoldAndColor(text, highlightColor); } else { formattedText = text; } views.callTypeAndDate.setText(formattedText); } /** Creates a SpannableString for the given text which is bold and in the given color. */ private CharSequence addBoldAndColor(CharSequence text, int color) { int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE; SpannableString result = new SpannableString(text); result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags); result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags); return result; } }