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.model.dataitem;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.provider.ContactsContract.CommonDataKinds.Phone;
22 import com.android.dialer.phonenumberutil.PhoneNumberHelper;
23 
24 /**
25  * Represents a phone data item, wrapping the columns in {@link
26  * android.provider.ContactsContract.CommonDataKinds.Phone}.
27  */
28 public class PhoneDataItem extends DataItem {
29 
30   private static final String KEY_FORMATTED_PHONE_NUMBER = "formattedPhoneNumber";
31 
PhoneDataItem(ContentValues values)32   /* package */ PhoneDataItem(ContentValues values) {
33     super(values);
34   }
35 
getNumber()36   public String getNumber() {
37     return getContentValues().getAsString(Phone.NUMBER);
38   }
39 
40   /** Returns the normalized phone number in E164 format. */
getNormalizedNumber()41   public String getNormalizedNumber() {
42     return getContentValues().getAsString(Phone.NORMALIZED_NUMBER);
43   }
44 
getFormattedPhoneNumber()45   public String getFormattedPhoneNumber() {
46     return getContentValues().getAsString(KEY_FORMATTED_PHONE_NUMBER);
47   }
48 
getLabel()49   public String getLabel() {
50     return getContentValues().getAsString(Phone.LABEL);
51   }
52 
computeFormattedPhoneNumber(Context context, String defaultCountryIso)53   public void computeFormattedPhoneNumber(Context context, String defaultCountryIso) {
54     final String phoneNumber = getNumber();
55     if (phoneNumber != null) {
56       final String formattedPhoneNumber =
57           PhoneNumberHelper.formatNumber(
58               context, phoneNumber, getNormalizedNumber(), defaultCountryIso);
59       getContentValues().put(KEY_FORMATTED_PHONE_NUMBER, formattedPhoneNumber);
60     }
61   }
62 
63   /**
64    * Returns the formatted phone number (if already computed using {@link
65    * #computeFormattedPhoneNumber}). Otherwise this method returns the unformatted phone number.
66    */
67   @Override
buildDataStringForDisplay(Context context, DataKind kind)68   public String buildDataStringForDisplay(Context context, DataKind kind) {
69     final String formatted = getFormattedPhoneNumber();
70     if (formatted != null) {
71       return formatted;
72     } else {
73       return getNumber();
74     }
75   }
76 }
77