Merge "Backport ContactsContract methods (2/2)" into ub-contactsdialer-b-dev
diff --git a/src/com/android/contacts/common/compat/ContactsCompat.java b/src/com/android/contacts/common/compat/ContactsCompat.java
index 7243574..5b50385 100644
--- a/src/com/android/contacts/common/compat/ContactsCompat.java
+++ b/src/com/android/contacts/common/compat/ContactsCompat.java
@@ -17,16 +17,28 @@
 package com.android.contacts.common.compat;
 
 import android.net.Uri;
+import android.provider.ContactsContract;
 import android.provider.ContactsContract.Contacts;
 
 import com.android.contacts.common.ContactsUtils;
 
+/**
+ * Compatibility class for {@link ContactsContract.Contacts}
+ */
 public class ContactsCompat {
+    /**
+     * Not instantiable.
+     */
+    private ContactsCompat() {
+    }
 
     // TODO: Use N APIs
     private static final Uri ENTERPRISE_CONTENT_FILTER_URI =
             Uri.withAppendedPath(Contacts.CONTENT_URI, "filter_enterprise");
 
+    // Copied from ContactsContract.Contacts#ENTERPRISE_CONTACT_ID_BASE, which is hidden.
+    private static final long ENTERPRISE_CONTACT_ID_BASE = 1000000000;
+
     public static Uri getContentUri() {
         // TODO: Use N APIs
         if (ContactsUtils.FLAG_N_FEATURE && android.os.Build.VERSION.CODENAME.startsWith("N")) {
@@ -34,4 +46,17 @@
         }
         return Contacts.CONTENT_FILTER_URI;
     }
+
+    /**
+     * Return {@code true} if a contact ID is from the contacts provider on the enterprise profile.
+     */
+    public static boolean isEnterpriseContactId(long contactId) {
+        if (CompatUtils.isLollipopCompatible()) {
+            return Contacts.isEnterpriseContactId(contactId);
+        } else {
+            // copied from ContactsContract.Contacts.isEnterpriseContactId
+            return (contactId >= ENTERPRISE_CONTACT_ID_BASE) &&
+                    (contactId < ContactsContract.Profile.MIN_ID);
+        }
+    }
 }
diff --git a/src/com/android/contacts/common/compat/EventCompat.java b/src/com/android/contacts/common/compat/EventCompat.java
new file mode 100644
index 0000000..f37aeff
--- /dev/null
+++ b/src/com/android/contacts/common/compat/EventCompat.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2015 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.compat;
+
+import android.content.res.Resources;
+import android.provider.ContactsContract.CommonDataKinds.BaseTypes;
+import android.provider.ContactsContract.CommonDataKinds.Event;
+import android.text.TextUtils;
+
+/**
+ * Compatibility class for {@link Event}
+ */
+public class EventCompat {
+    /**
+     * Not instantiable.
+     */
+    private EventCompat() {
+    }
+
+    /**
+     * Return a {@link CharSequence} that best describes the given type, possibly substituting
+     * the given label value for TYPE_CUSTOM.
+     */
+    public static CharSequence getTypeLabel(Resources res, int type, CharSequence label) {
+        if (CompatUtils.isLollipopCompatible()) {
+            return Event.getTypeLabel(res, type, label);
+        } else {
+            return getTypeLabelInternal(res, type, label);
+        }
+    }
+
+    /**
+     * The method was added in API level 21, and below is the implementation copied from
+     * {@link Event#getTypeLabel(Resources, int, CharSequence)}
+     */
+    private static CharSequence getTypeLabelInternal(Resources res, int type, CharSequence label) {
+        if (type == BaseTypes.TYPE_CUSTOM && !TextUtils.isEmpty(label)) {
+            return label;
+        } else {
+            return res.getText(Event.getTypeResource(type));
+        }
+    }
+
+}
diff --git a/src/com/android/contacts/common/compat/PhoneNumberFormattingTextWatcherCompat.java b/src/com/android/contacts/common/compat/PhoneNumberFormattingTextWatcherCompat.java
new file mode 100644
index 0000000..42b604e
--- /dev/null
+++ b/src/com/android/contacts/common/compat/PhoneNumberFormattingTextWatcherCompat.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2015 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.compat;
+
+import android.telephony.PhoneNumberFormattingTextWatcher;
+
+public class PhoneNumberFormattingTextWatcherCompat {
+    public static PhoneNumberFormattingTextWatcher newInstance(String countryCode) {
+        if (CompatUtils.isLollipopCompatible()) {
+            return new PhoneNumberFormattingTextWatcher(countryCode);
+        }
+        return new PhoneNumberFormattingTextWatcher();
+    }
+}
diff --git a/src/com/android/contacts/common/list/ContactListAdapter.java b/src/com/android/contacts/common/list/ContactListAdapter.java
index 5b2957b..d68788c 100644
--- a/src/com/android/contacts/common/list/ContactListAdapter.java
+++ b/src/com/android/contacts/common/list/ContactListAdapter.java
@@ -30,6 +30,7 @@
 import com.android.contacts.common.ContactPhotoManager;
 import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
 import com.android.contacts.common.R;
+import com.android.contacts.common.compat.ContactsCompat;
 import com.android.contacts.common.preference.ContactsPreferences;
 
 /**
@@ -176,7 +177,7 @@
         final Cursor cursor = (Cursor) getItem(position);
         if (cursor != null) {
             final long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
-            return Contacts.isEnterpriseContactId(contactId);
+            return ContactsCompat.isEnterpriseContactId(contactId);
         }
         return false;
     }
diff --git a/src/com/android/contacts/common/util/BitmapUtil.java b/src/com/android/contacts/common/util/BitmapUtil.java
index 0056952..5cd856e 100644
--- a/src/com/android/contacts/common/util/BitmapUtil.java
+++ b/src/com/android/contacts/common/util/BitmapUtil.java
@@ -134,7 +134,8 @@
         final Paint paint = new Paint();
         canvas.drawARGB(0, 0, 0, 0);
         paint.setAntiAlias(true);
-        canvas.drawOval(0, 0, targetWidth, targetHeight, paint);
+        final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
+        canvas.drawOval(dst, paint);
 
         // Specifies that only pixels present in the destination (i.e. the drawn oval) should
         // be overwritten with pixels from the input bitmap.
@@ -157,7 +158,6 @@
                 inputWidth / 2 + xCropAmountHalved,
                 inputHeight / 2 + yCropAmountHalved);
 
-        final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
         canvas.drawBitmap(input, src, dst, paint);
         return result;
     }
diff --git a/src/com/android/contacts/common/util/PhoneNumberFormatter.java b/src/com/android/contacts/common/util/PhoneNumberFormatter.java
index e780115..dbf6dfc 100644
--- a/src/com/android/contacts/common/util/PhoneNumberFormatter.java
+++ b/src/com/android/contacts/common/util/PhoneNumberFormatter.java
@@ -22,6 +22,7 @@
 import android.widget.TextView;
 
 import com.android.contacts.common.GeoUtil;
+import com.android.contacts.common.compat.PhoneNumberFormattingTextWatcherCompat;
 
 public final class PhoneNumberFormatter {
     private PhoneNumberFormatter() {}
@@ -41,7 +42,7 @@
 
         @Override
         protected PhoneNumberFormattingTextWatcher doInBackground(Void... params) {
-            return new PhoneNumberFormattingTextWatcher(mCountryCode);
+            return PhoneNumberFormattingTextWatcherCompat.newInstance(mCountryCode);
         }
 
         @Override