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