Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 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.contacts.common.util;
     18 
     19 import static android.provider.ContactsContract.CommonDataKinds.Phone;
     20 
     21 import android.content.Context;
     22 import android.util.Log;
     23 
     24 import com.android.contacts.common.R;
     25 
     26 import com.google.common.base.Preconditions;
     27 
     28 /**
     29  * Methods for handling various contact data labels.
     30  */
     31 public class ContactDisplayUtils {
     32 
     33     private static final String TAG = ContactDisplayUtils.class.getSimpleName();
     34 
     35     public static final int INTERACTION_CALL = 1;
     36     public static final int INTERACTION_SMS = 2;
     37 
     38     /**
     39      * Checks if the given data type is a custom type.
     40      *
     41      * @param type Phone data type.
     42      * @return {@literal true} if the type is custom.  {@literal false} if not.
     43      */
     44     public static boolean isCustomPhoneType(Integer type) {
     45         return type == Phone.TYPE_CUSTOM || type == Phone.TYPE_ASSISTANT;
     46     }
     47 
     48     /**
     49      * Gets a display label for a given phone type.
     50      *
     51      * @param type The type of number.
     52      * @param customLabel A custom label to use if the phone is determined to be of custom type
     53      * determined by {@link #isCustomPhoneType(Integer))}
     54      * @param interactionType whether this is a call or sms.  Either {@link #INTERACTION_CALL} or
     55      * {@link #INTERACTION_SMS}.
     56      * @param context The application context.
     57      * @return An appropriate string label
     58      */
     59     public static CharSequence getLabelForCallOrSms(Integer type, CharSequence customLabel,
     60             int interactionType, Context context) {
     61         Preconditions.checkNotNull(context);
     62 
     63         if (isCustomPhoneType(type)) {
     64             return (customLabel == null) ? "" : customLabel;
     65         } else {
     66             int resId;
     67             if (interactionType == INTERACTION_SMS) {
     68                 resId = getSmsLabelResourceId(type);
     69             } else {
     70                 resId = getPhoneLabelResourceId(type);
     71                 if (interactionType != INTERACTION_CALL) {
     72                     Log.e(TAG, "Un-recognized interaction type: " + interactionType +
     73                             ". Defaulting to ContactDisplayUtils.INTERACTION_CALL.");
     74                 }
     75             }
     76 
     77             return context.getResources().getText(resId);
     78         }
     79     }
     80 
     81     /**
     82      * Find a label for calling.
     83      *
     84      * @param type The type of number.
     85      * @return An appropriate string label.
     86      */
     87     public static int getPhoneLabelResourceId(Integer type) {
     88         if (type == null) return R.string.call_other;
     89         switch (type) {
     90             case Phone.TYPE_HOME:
     91                 return R.string.call_home;
     92             case Phone.TYPE_MOBILE:
     93                 return R.string.call_mobile;
     94             case Phone.TYPE_WORK:
     95                 return R.string.call_work;
     96             case Phone.TYPE_FAX_WORK:
     97                 return R.string.call_fax_work;
     98             case Phone.TYPE_FAX_HOME:
     99                 return R.string.call_fax_home;
    100             case Phone.TYPE_PAGER:
    101                 return R.string.call_pager;
    102             case Phone.TYPE_OTHER:
    103                 return R.string.call_other;
    104             case Phone.TYPE_CALLBACK:
    105                 return R.string.call_callback;
    106             case Phone.TYPE_CAR:
    107                 return R.string.call_car;
    108             case Phone.TYPE_COMPANY_MAIN:
    109                 return R.string.call_company_main;
    110             case Phone.TYPE_ISDN:
    111                 return R.string.call_isdn;
    112             case Phone.TYPE_MAIN:
    113                 return R.string.call_main;
    114             case Phone.TYPE_OTHER_FAX:
    115                 return R.string.call_other_fax;
    116             case Phone.TYPE_RADIO:
    117                 return R.string.call_radio;
    118             case Phone.TYPE_TELEX:
    119                 return R.string.call_telex;
    120             case Phone.TYPE_TTY_TDD:
    121                 return R.string.call_tty_tdd;
    122             case Phone.TYPE_WORK_MOBILE:
    123                 return R.string.call_work_mobile;
    124             case Phone.TYPE_WORK_PAGER:
    125                 return R.string.call_work_pager;
    126             case Phone.TYPE_ASSISTANT:
    127                 return R.string.call_assistant;
    128             case Phone.TYPE_MMS:
    129                 return R.string.call_mms;
    130             default:
    131                 return R.string.call_custom;
    132         }
    133 
    134     }
    135 
    136     /**
    137      * Find a label for sending an sms.
    138      *
    139      * @param type The type of number.
    140      * @return An appropriate string label.
    141      */
    142     public static int getSmsLabelResourceId(Integer type) {
    143         if (type == null) return R.string.sms_other;
    144         switch (type) {
    145             case Phone.TYPE_HOME:
    146                 return R.string.sms_home;
    147             case Phone.TYPE_MOBILE:
    148                 return R.string.sms_mobile;
    149             case Phone.TYPE_WORK:
    150                 return R.string.sms_work;
    151             case Phone.TYPE_FAX_WORK:
    152                 return R.string.sms_fax_work;
    153             case Phone.TYPE_FAX_HOME:
    154                 return R.string.sms_fax_home;
    155             case Phone.TYPE_PAGER:
    156                 return R.string.sms_pager;
    157             case Phone.TYPE_OTHER:
    158                 return R.string.sms_other;
    159             case Phone.TYPE_CALLBACK:
    160                 return R.string.sms_callback;
    161             case Phone.TYPE_CAR:
    162                 return R.string.sms_car;
    163             case Phone.TYPE_COMPANY_MAIN:
    164                 return R.string.sms_company_main;
    165             case Phone.TYPE_ISDN:
    166                 return R.string.sms_isdn;
    167             case Phone.TYPE_MAIN:
    168                 return R.string.sms_main;
    169             case Phone.TYPE_OTHER_FAX:
    170                 return R.string.sms_other_fax;
    171             case Phone.TYPE_RADIO:
    172                 return R.string.sms_radio;
    173             case Phone.TYPE_TELEX:
    174                 return R.string.sms_telex;
    175             case Phone.TYPE_TTY_TDD:
    176                 return R.string.sms_tty_tdd;
    177             case Phone.TYPE_WORK_MOBILE:
    178                 return R.string.sms_work_mobile;
    179             case Phone.TYPE_WORK_PAGER:
    180                 return R.string.sms_work_pager;
    181             case Phone.TYPE_ASSISTANT:
    182                 return R.string.sms_assistant;
    183             case Phone.TYPE_MMS:
    184                 return R.string.sms_mms;
    185             default:
    186                 return R.string.sms_custom;
    187         }
    188     }
    189 
    190 }
    191