Home | History | Annotate | Download | only in dialer
      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.dialer;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.drawable.Drawable;
     22 import android.provider.CallLog;
     23 import android.provider.CallLog.Calls;
     24 import android.provider.ContactsContract.CommonDataKinds.Phone;
     25 import android.telecom.PhoneAccount;
     26 import android.text.TextUtils;
     27 import android.text.format.DateUtils;
     28 import android.view.View;
     29 import android.widget.TextView;
     30 
     31 import com.android.contacts.common.CallUtil;
     32 import com.android.contacts.common.testing.NeededForTesting;
     33 import com.android.contacts.common.util.PhoneNumberHelper;
     34 import com.android.dialer.calllog.ContactInfo;
     35 import com.android.dialer.calllog.PhoneAccountUtils;
     36 import com.android.dialer.calllog.PhoneNumberDisplayHelper;
     37 import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
     38 import com.android.dialer.util.DialerUtils;
     39 
     40 import com.google.common.collect.Lists;
     41 
     42 import java.util.ArrayList;
     43 
     44 /**
     45  * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
     46  */
     47 public class PhoneCallDetailsHelper {
     48     /** The maximum number of icons will be shown to represent the call types in a group. */
     49     private static final int MAX_CALL_TYPE_ICONS = 3;
     50 
     51     private final Context mContext;
     52     private final Resources mResources;
     53     /** The injected current time in milliseconds since the epoch. Used only by tests. */
     54     private Long mCurrentTimeMillisForTest;
     55     // Helper classes.
     56     private final PhoneNumberDisplayHelper mPhoneNumberHelper;
     57     private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
     58 
     59     /**
     60      * List of items to be concatenated together for accessibility descriptions
     61      */
     62     private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
     63 
     64     /**
     65      * Creates a new instance of the helper.
     66      * <p>
     67      * Generally you should have a single instance of this helper in any context.
     68      *
     69      * @param resources used to look up strings
     70      */
     71     public PhoneCallDetailsHelper(Context context, Resources resources,
     72             PhoneNumberUtilsWrapper phoneUtils) {
     73         mContext = context;
     74         mResources = resources;
     75         mPhoneNumberUtilsWrapper = phoneUtils;
     76         mPhoneNumberHelper = new PhoneNumberDisplayHelper(context, resources, phoneUtils);
     77     }
     78 
     79     /** Fills the call details views with content. */
     80     public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
     81         // Display up to a given number of icons.
     82         views.callTypeIcons.clear();
     83         int count = details.callTypes.length;
     84         boolean isVoicemail = false;
     85         for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
     86             views.callTypeIcons.add(details.callTypes[index]);
     87             if (index == 0) {
     88                 isVoicemail = details.callTypes[index] == Calls.VOICEMAIL_TYPE;
     89             }
     90         }
     91 
     92         // Show the video icon if the call had video enabled.
     93         views.callTypeIcons.setShowVideo(
     94                 (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO);
     95         views.callTypeIcons.requestLayout();
     96         views.callTypeIcons.setVisibility(View.VISIBLE);
     97 
     98         // Show the total call count only if there are more than the maximum number of icons.
     99         final Integer callCount;
    100         if (count > MAX_CALL_TYPE_ICONS) {
    101             callCount = count;
    102         } else {
    103             callCount = null;
    104         }
    105 
    106         CharSequence callLocationAndDate = getCallLocationAndDate(details);
    107 
    108         // Set the call count, location and date.
    109         setCallCountAndDate(views, callCount, callLocationAndDate);
    110 
    111         // Set the account label if it exists.
    112         String accountLabel = PhoneAccountUtils.getAccountLabel(mContext, details.accountHandle);
    113 
    114         if (accountLabel != null) {
    115             views.callAccountLabel.setVisibility(View.VISIBLE);
    116             views.callAccountLabel.setText(accountLabel);
    117             int color = PhoneAccountUtils.getAccountColor(mContext, details.accountHandle);
    118             if (color == PhoneAccount.NO_HIGHLIGHT_COLOR) {
    119                 int defaultColor = R.color.dialtacts_secondary_text_color;
    120                 views.callAccountLabel.setTextColor(mContext.getResources().getColor(defaultColor));
    121             } else {
    122                 views.callAccountLabel.setTextColor(color);
    123             }
    124         } else {
    125             views.callAccountLabel.setVisibility(View.GONE);
    126         }
    127 
    128         final CharSequence nameText;
    129         final CharSequence displayNumber =
    130             mPhoneNumberHelper.getDisplayNumber(details.accountHandle, details.number,
    131                     details.numberPresentation, details.formattedNumber);
    132         if (TextUtils.isEmpty(details.name)) {
    133             nameText = displayNumber;
    134             // We have a real phone number as "nameView" so make it always LTR
    135             views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
    136         } else {
    137             nameText = details.name;
    138         }
    139 
    140         views.nameView.setText(nameText);
    141 
    142         if (isVoicemail && !TextUtils.isEmpty(details.transcription)) {
    143             views.voicemailTranscriptionView.setText(details.transcription);
    144             views.voicemailTranscriptionView.setVisibility(View.VISIBLE);
    145         } else {
    146             views.voicemailTranscriptionView.setText(null);
    147             views.voicemailTranscriptionView.setVisibility(View.GONE);
    148         }
    149     }
    150 
    151     /**
    152      * Builds a string containing the call location and date.
    153      *
    154      * @param details The call details.
    155      * @return The call location and date string.
    156      */
    157     private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
    158         mDescriptionItems.clear();
    159 
    160         // Get type of call (ie mobile, home, etc) if known, or the caller's location.
    161         CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
    162 
    163         // Only add the call type or location if its not empty.  It will be empty for unknown
    164         // callers.
    165         if (!TextUtils.isEmpty(callTypeOrLocation)) {
    166             mDescriptionItems.add(callTypeOrLocation);
    167         }
    168         // The date of this call, relative to the current time.
    169         mDescriptionItems.add(getCallDate(details));
    170 
    171         // Create a comma separated list from the call type or location, and call date.
    172         return DialerUtils.join(mResources, mDescriptionItems);
    173     }
    174 
    175     /**
    176      * For a call, if there is an associated contact for the caller, return the known call type
    177      * (e.g. mobile, home, work).  If there is no associated contact, attempt to use the caller's
    178      * location if known.
    179      * @param details Call details to use.
    180      * @return Type of call (mobile/home) if known, or the location of the caller (if known).
    181      */
    182     public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
    183         CharSequence numberFormattedLabel = null;
    184         // Only show a label if the number is shown and it is not a SIP address.
    185         if (!TextUtils.isEmpty(details.number)
    186                 && !PhoneNumberHelper.isUriNumber(details.number.toString())
    187                 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.accountHandle,
    188                         details.number)) {
    189 
    190             if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
    191                 numberFormattedLabel = details.geocode;
    192             } else {
    193                 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
    194                         details.numberLabel);
    195             }
    196         }
    197 
    198         if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
    199             numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.accountHandle,
    200                     details.number, details.numberPresentation, details.formattedNumber);
    201         }
    202         return numberFormattedLabel;
    203     }
    204 
    205     /**
    206      * Get the call date/time of the call, relative to the current time.
    207      * e.g. 3 minutes ago
    208      * @param details Call details to use.
    209      * @return String representing when the call occurred.
    210      */
    211     public CharSequence getCallDate(PhoneCallDetails details) {
    212         return DateUtils.getRelativeTimeSpanString(details.date,
    213                 getCurrentTimeMillis(),
    214                 DateUtils.MINUTE_IN_MILLIS,
    215                 DateUtils.FORMAT_ABBREV_RELATIVE);
    216     }
    217 
    218     /** Sets the text of the header view for the details page of a phone call. */
    219     @NeededForTesting
    220     public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
    221         final CharSequence nameText;
    222         final CharSequence displayNumber =
    223             mPhoneNumberHelper.getDisplayNumber(details.accountHandle, details.number,
    224                     details.numberPresentation,
    225                     mResources.getString(R.string.recentCalls_addToContact));
    226         if (TextUtils.isEmpty(details.name)) {
    227             nameText = displayNumber;
    228         } else {
    229             nameText = details.name;
    230         }
    231 
    232         nameView.setText(nameText);
    233     }
    234 
    235     @NeededForTesting
    236     public void setCurrentTimeForTest(long currentTimeMillis) {
    237         mCurrentTimeMillisForTest = currentTimeMillis;
    238     }
    239 
    240     /**
    241      * Returns the current time in milliseconds since the epoch.
    242      * <p>
    243      * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
    244      */
    245     private long getCurrentTimeMillis() {
    246         if (mCurrentTimeMillisForTest == null) {
    247             return System.currentTimeMillis();
    248         } else {
    249             return mCurrentTimeMillisForTest;
    250         }
    251     }
    252 
    253     /** Sets the call count and date. */
    254     private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
    255             CharSequence dateText) {
    256         // Combine the count (if present) and the date.
    257         final CharSequence text;
    258         if (callCount != null) {
    259             text = mResources.getString(
    260                     R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
    261         } else {
    262             text = dateText;
    263         }
    264 
    265         views.callLocationAndDate.setText(text);
    266     }
    267 }
    268