Move label fetch logic into common utility class.

- Move logic from PhoneActionInflater and PhoneActionAltInflater into common
  utility method so it can be shared by PhoneNumberInteraction without going
  through account manager.
- Added unit tests.

Bug: 6993891
Change-Id: I53e74df02bea031886ee512360b9f1abc083d66c
diff --git a/res/values/strings.xml b/res/values/strings.xml
index eb7a757..7e69de2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -19,4 +19,99 @@
     <!-- Toast shown when text is copied to the clipboard [CHAR LIMIT=64] -->
     <string name="toast_text_copied">Text copied</string>
 
+
+    <!-- Action string for calling a custom phone number -->
+    <string name="call_custom">Call
+        <xliff:g id="custom">%s</xliff:g>
+    </string>
+    <!-- Action string for calling a home phone number -->
+    <string name="call_home">Call home</string>
+    <!-- Action string for calling a mobile phone number -->
+    <string name="call_mobile">Call mobile</string>
+    <!-- Action string for calling a work phone number -->
+    <string name="call_work">Call work</string>
+    <!-- Action string for calling a work fax phone number -->
+    <string name="call_fax_work">Call work fax</string>
+    <!-- Action string for calling a home fax phone number -->
+    <string name="call_fax_home">Call home fax</string>
+    <!-- Action string for calling a pager phone number -->
+    <string name="call_pager">Call pager</string>
+    <!-- Action string for calling an other phone number -->
+    <string name="call_other">Call</string>
+    <!-- Action string for calling a callback number -->
+    <string name="call_callback">Call callback</string>
+    <!-- Action string for calling a car phone number -->
+    <string name="call_car">Call car</string>
+    <!-- Action string for calling a company main phone number -->
+    <string name="call_company_main">Call company main</string>
+    <!-- Action string for calling a ISDN phone number -->
+    <string name="call_isdn">Call ISDN</string>
+    <!-- Action string for calling a main phone number -->
+    <string name="call_main">Call main</string>
+    <!-- Action string for calling an other fax phone number -->
+    <string name="call_other_fax">Call fax</string>
+    <!-- Action string for calling a radio phone number -->
+    <string name="call_radio">Call radio</string>
+    <!-- Action string for calling a Telex phone number -->
+    <string name="call_telex">Call telex</string>
+    <!-- Action string for calling a TTY/TDD phone number -->
+    <string name="call_tty_tdd">Call TTY/TDD</string>
+    <!-- Action string for calling a work mobile phone number -->
+    <string name="call_work_mobile">Call work mobile</string>
+    <!-- Action string for calling a work pager phone number -->
+    <string name="call_work_pager">Call work pager</string>
+    <!-- Action string for calling an assistant phone number -->
+    <string name="call_assistant">Call
+        <xliff:g id="assistant">%s</xliff:g>
+    </string>
+    <!-- Action string for calling a MMS phone number -->
+    <string name="call_mms">Call MMS</string>
+
+    <!-- Action string for sending an SMS to a custom phone number -->
+    <string name="sms_custom">Text
+        <xliff:g id="custom">%s</xliff:g>
+    </string>
+    <!-- Action string for sending an SMS to a home phone number -->
+    <string name="sms_home">Text home</string>
+    <!-- Action string for sending an SMS to a mobile phone number -->
+    <string name="sms_mobile">Text mobile</string>
+    <!-- Action string for sending an SMS to a work phone number -->
+    <string name="sms_work">Text work</string>
+    <!-- Action string for sending an SMS to a work fax phone number -->
+    <string name="sms_fax_work">Text work fax</string>
+    <!-- Action string for sending an SMS to a home fax phone number -->
+    <string name="sms_fax_home">Text home fax</string>
+    <!-- Action string for sending an SMS to a pager phone number -->
+    <string name="sms_pager">Text pager</string>
+    <!-- Action string for sending an SMS to an other phone number -->
+    <string name="sms_other">Text</string>
+    <!-- Action string for sending an SMS to a callback number -->
+    <string name="sms_callback">Text callback</string>
+    <!-- Action string for sending an SMS to a car phone number -->
+    <string name="sms_car">Text car</string>
+    <!-- Action string for sending an SMS to a company main phone number -->
+    <string name="sms_company_main">Text company main</string>
+    <!-- Action string for sending an SMS to a ISDN phone number -->
+    <string name="sms_isdn">Text ISDN</string>
+    <!-- Action string for sending an SMS to a main phone number -->
+    <string name="sms_main">Text main</string>
+    <!-- Action string for sending an SMS to an other fax phone number -->
+    <string name="sms_other_fax">Text fax</string>
+    <!-- Action string for sending an SMS to a radio phone number -->
+    <string name="sms_radio">Text radio</string>
+    <!-- Action string for sending an SMS to a Telex phone number -->
+    <string name="sms_telex">Text telex</string>
+    <!-- Action string for sending an SMS to a TTY/TDD phone number -->
+    <string name="sms_tty_tdd">Text TTY/TDD</string>
+    <!-- Action string for sending an SMS to a work mobile phone number -->
+    <string name="sms_work_mobile">Text work mobile</string>
+    <!-- Action string for sending an SMS to a work pager phone number -->
+    <string name="sms_work_pager">Text work pager</string>
+    <!-- Action string for sending an SMS to an assistant phone number -->
+    <string name="sms_assistant">Text
+        <xliff:g id="assistant">%s</xliff:g>
+    </string>
+    <!-- Action string for sending an SMS to a MMS phone number -->
+    <string name="sms_mms">Text MMS</string>
+
 </resources>
diff --git a/src/com/android/contacts/common/util/ContactDisplayUtils.java b/src/com/android/contacts/common/util/ContactDisplayUtils.java
new file mode 100644
index 0000000..7817d90
--- /dev/null
+++ b/src/com/android/contacts/common/util/ContactDisplayUtils.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2012 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.android.contacts.common.util;
+
+import static android.provider.ContactsContract.CommonDataKinds.Phone;
+
+import android.content.Context;
+import android.util.Log;
+
+import com.android.contacts.common.R;
+import com.android.internal.util.Preconditions;
+
+/**
+ * Methods for handling various contact data labels.
+ */
+public class ContactDisplayUtils {
+
+    private static final String TAG = ContactDisplayUtils.class.getSimpleName();
+
+    public static final int INTERACTION_CALL = 1;
+    public static final int INTERACTION_SMS = 2;
+
+    /**
+     * Checks if the given data type is a custom type.
+     *
+     * @param type Phone data type.
+     * @return {@literal true} if the type is custom.  {@literal false} if not.
+     */
+    public static boolean isCustomPhoneType(Integer type) {
+        return type == Phone.TYPE_CUSTOM || type == Phone.TYPE_ASSISTANT;
+    }
+
+    /**
+     * Gets a display label for a given phone type.
+     *
+     * @param type The type of number.
+     * @param customLabel A custom label to use if the phone is determined to be of custom type
+     * determined by {@link #isCustomPhoneType(Integer))}
+     * @param interactionType whether this is a call or sms.  Either {@link #INTERACTION_CALL} or
+     * {@link #INTERACTION_SMS}.
+     * @param context The application context.
+     * @return An appropriate string label
+     */
+    public static CharSequence getLabelForCallOrSms(Integer type, CharSequence customLabel,
+            int interactionType, Context context) {
+        Preconditions.checkNotNull(context);
+
+        if (isCustomPhoneType(type)) {
+            return (customLabel == null) ? "" : customLabel;
+        } else {
+            int resId;
+            if (interactionType == INTERACTION_SMS) {
+                resId = getSmsLabelResourceId(type);
+            } else {
+                resId = getPhoneLabelResourceId(type);
+                if (interactionType != INTERACTION_CALL) {
+                    Log.e(TAG, "Un-recognized interaction type: " + interactionType +
+                            ". Defaulting to ContactDisplayUtils.INTERACTION_CALL.");
+                }
+            }
+
+            return context.getResources().getText(resId);
+        }
+    }
+
+    /**
+     * Find a label for calling.
+     *
+     * @param type The type of number.
+     * @return An appropriate string label.
+     */
+    public static int getPhoneLabelResourceId(Integer type) {
+        if (type == null) return R.string.call_other;
+        switch (type) {
+            case Phone.TYPE_HOME:
+                return R.string.call_home;
+            case Phone.TYPE_MOBILE:
+                return R.string.call_mobile;
+            case Phone.TYPE_WORK:
+                return R.string.call_work;
+            case Phone.TYPE_FAX_WORK:
+                return R.string.call_fax_work;
+            case Phone.TYPE_FAX_HOME:
+                return R.string.call_fax_home;
+            case Phone.TYPE_PAGER:
+                return R.string.call_pager;
+            case Phone.TYPE_OTHER:
+                return R.string.call_other;
+            case Phone.TYPE_CALLBACK:
+                return R.string.call_callback;
+            case Phone.TYPE_CAR:
+                return R.string.call_car;
+            case Phone.TYPE_COMPANY_MAIN:
+                return R.string.call_company_main;
+            case Phone.TYPE_ISDN:
+                return R.string.call_isdn;
+            case Phone.TYPE_MAIN:
+                return R.string.call_main;
+            case Phone.TYPE_OTHER_FAX:
+                return R.string.call_other_fax;
+            case Phone.TYPE_RADIO:
+                return R.string.call_radio;
+            case Phone.TYPE_TELEX:
+                return R.string.call_telex;
+            case Phone.TYPE_TTY_TDD:
+                return R.string.call_tty_tdd;
+            case Phone.TYPE_WORK_MOBILE:
+                return R.string.call_work_mobile;
+            case Phone.TYPE_WORK_PAGER:
+                return R.string.call_work_pager;
+            case Phone.TYPE_ASSISTANT:
+                return R.string.call_assistant;
+            case Phone.TYPE_MMS:
+                return R.string.call_mms;
+            default:
+                return R.string.call_custom;
+        }
+
+    }
+
+    /**
+     * Find a label for sending an sms.
+     *
+     * @param type The type of number.
+     * @return An appropriate string label.
+     */
+    public static int getSmsLabelResourceId(Integer type) {
+        if (type == null) return R.string.sms_other;
+        switch (type) {
+            case Phone.TYPE_HOME:
+                return R.string.sms_home;
+            case Phone.TYPE_MOBILE:
+                return R.string.sms_mobile;
+            case Phone.TYPE_WORK:
+                return R.string.sms_work;
+            case Phone.TYPE_FAX_WORK:
+                return R.string.sms_fax_work;
+            case Phone.TYPE_FAX_HOME:
+                return R.string.sms_fax_home;
+            case Phone.TYPE_PAGER:
+                return R.string.sms_pager;
+            case Phone.TYPE_OTHER:
+                return R.string.sms_other;
+            case Phone.TYPE_CALLBACK:
+                return R.string.sms_callback;
+            case Phone.TYPE_CAR:
+                return R.string.sms_car;
+            case Phone.TYPE_COMPANY_MAIN:
+                return R.string.sms_company_main;
+            case Phone.TYPE_ISDN:
+                return R.string.sms_isdn;
+            case Phone.TYPE_MAIN:
+                return R.string.sms_main;
+            case Phone.TYPE_OTHER_FAX:
+                return R.string.sms_other_fax;
+            case Phone.TYPE_RADIO:
+                return R.string.sms_radio;
+            case Phone.TYPE_TELEX:
+                return R.string.sms_telex;
+            case Phone.TYPE_TTY_TDD:
+                return R.string.sms_tty_tdd;
+            case Phone.TYPE_WORK_MOBILE:
+                return R.string.sms_work_mobile;
+            case Phone.TYPE_WORK_PAGER:
+                return R.string.sms_work_pager;
+            case Phone.TYPE_ASSISTANT:
+                return R.string.sms_assistant;
+            case Phone.TYPE_MMS:
+                return R.string.sms_mms;
+            default:
+                return R.string.sms_custom;
+        }
+    }
+
+}
diff --git a/tests/src/com/android/contacts/common/util/ContactDisplayUtilTests.java b/tests/src/com/android/contacts/common/util/ContactDisplayUtilTests.java
new file mode 100644
index 0000000..544abbb
--- /dev/null
+++ b/tests/src/com/android/contacts/common/util/ContactDisplayUtilTests.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2012 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.android.contacts.common.util;
+
+import static android.provider.ContactsContract.CommonDataKinds.Phone;
+
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.contacts.common.R;
+
+/**
+ * Unit tests for (@link ContactDisplayUtils}
+ */
+@SmallTest
+public class ContactDisplayUtilTests extends AndroidTestCase {
+
+    public void testIsCustomPhoneTypeReturnsTrue() {
+        assertTrue(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_CUSTOM));
+        assertTrue(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_ASSISTANT));
+    }
+
+    public void testIsCustomPhoneTypeReturnsFalse() {
+        assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_HOME));
+        assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_FAX_WORK));
+        assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_MOBILE));
+        assertFalse(ContactDisplayUtils.isCustomPhoneType(Phone.TYPE_OTHER));
+    }
+
+    public void testGetLabelForCallOrSmsReturnsCustomLabel() {
+        final CharSequence smsResult = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_CUSTOM,
+                "expected sms label", ContactDisplayUtils.INTERACTION_SMS, getContext());
+        assertEquals("expected sms label", smsResult);
+
+        final CharSequence callResult = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_CUSTOM,
+                "expected call label", ContactDisplayUtils.INTERACTION_CALL, getContext());
+        assertEquals("expected call label", callResult);
+    }
+
+    public void testGetLabelForCallOrSmsReturnsCallLabels() {
+        CharSequence result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_HOME, "",
+                ContactDisplayUtils.INTERACTION_CALL, getContext());
+        CharSequence expected = getContext().getResources().getText(R.string.call_home);
+        assertEquals(expected, result);
+
+        result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_MOBILE, "",
+                ContactDisplayUtils.INTERACTION_CALL, getContext());
+        expected = getContext().getResources().getText(R.string.call_mobile);
+        assertEquals(expected, result);
+    }
+
+    public void testGetLabelForCallOrSmsReturnsSmsLabels() {
+        CharSequence result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_HOME, "",
+                ContactDisplayUtils.INTERACTION_SMS, getContext());
+        CharSequence expected = getContext().getResources().getText(R.string.sms_home);
+        assertEquals(expected, result);
+
+        result = ContactDisplayUtils.getLabelForCallOrSms(Phone.TYPE_MOBILE, "",
+                ContactDisplayUtils.INTERACTION_SMS, getContext());
+        expected = getContext().getResources().getText(R.string.sms_mobile);
+        assertEquals(expected, result);
+    }
+
+    public void testGetPhoneLabelResourceIdReturnsOther() {
+        assertEquals(R.string.call_other, ContactDisplayUtils.getPhoneLabelResourceId(null));
+    }
+
+    public void testGetPhoneLabelResourceIdReturnsMatchHome() {
+        assertEquals(R.string.call_home, ContactDisplayUtils.getPhoneLabelResourceId(
+                Phone.TYPE_HOME));
+    }
+
+    public void testGetSmsLabelResourceIdReturnsOther() {
+        assertEquals(R.string.sms_other, ContactDisplayUtils.getSmsLabelResourceId(null));
+    }
+
+    public void testGetSmsLabelResourceIdReturnsMatchHome() {
+        assertEquals(R.string.sms_home, ContactDisplayUtils.getSmsLabelResourceId(Phone.TYPE_HOME));
+    }
+
+}