Merge "Set default account when there's only one writable account." into ub-contactsdialer-b-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 090190d..2d685d7 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -16,8 +16,8 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.android.contacts"
-    android:versionCode="10306"
-    android:versionName="1.3.06">
+    android:versionCode="10400"
+    android:versionName="1.4.0">
 
     <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
     <original-package android:name="com.android.contacts" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a211c20..c963506 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -904,4 +904,7 @@
          For example: Photo from Google abc@gmail.com checked. [CHAR LIMIT=60]-->
     <string name="photo_view_description_checked">Photo from <xliff:g id="account_type">%s </xliff:g><xliff:g id="user_name">%s </xliff:g>checked</string>
 
+    <!-- Text shown in the contacts app while the background process updates contacts after a locale change [CHAR LIMIT=150]-->
+    <string name="locale_change_in_progress">Contact list is being updated to reflect the change of language.\n\nPlease wait...</string>
+
 </resources>
diff --git a/src/com/android/contacts/editor/AggregationSuggestionEngine.java b/src/com/android/contacts/editor/AggregationSuggestionEngine.java
index aa5f988..816bbc7 100644
--- a/src/com/android/contacts/editor/AggregationSuggestionEngine.java
+++ b/src/com/android/contacts/editor/AggregationSuggestionEngine.java
@@ -21,11 +21,11 @@
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.Message;
 import android.os.Process;
-import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.Email;
 import android.provider.ContactsContract.CommonDataKinds.Nickname;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
@@ -39,6 +39,7 @@
 import android.text.TextUtils;
 
 import com.android.contacts.common.model.ValuesDelta;
+import com.android.contacts.util.AggregationSuggestionsCompat;
 import com.google.common.collect.Lists;
 
 import java.util.ArrayList;
@@ -228,19 +229,32 @@
             return null;
         }
 
-        Builder builder = new AggregationSuggestions.Builder()
+        // AggregationSuggestions.Builder() became visible in API level 23, so use it if applicable.
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            final Builder uriBuilder = new AggregationSuggestions.Builder()
+                    .setLimit(mSuggestionsLimit)
+                    .setContactId(mContactId);
+            if (nameSb.length() != 0) {
+                uriBuilder.addNameParameter(nameSb.toString());
+            }
+            if (phoneticNameSb.length() != 0) {
+                uriBuilder.addNameParameter(phoneticNameSb.toString());
+            }
+            return uriBuilder.build();
+        }
+
+        // For previous SDKs, use the backup plan.
+        final AggregationSuggestionsCompat.Builder uriBuilder =
+                new AggregationSuggestionsCompat.Builder()
                 .setLimit(mSuggestionsLimit)
                 .setContactId(mContactId);
-
         if (nameSb.length() != 0) {
-            builder.addNameParameter(nameSb.toString());
+            uriBuilder.addNameParameter(nameSb.toString());
         }
-
         if (phoneticNameSb.length() != 0) {
-            builder.addNameParameter(phoneticNameSb.toString());
+            uriBuilder.addNameParameter(phoneticNameSb.toString());
         }
-
-        return builder.build();
+        return uriBuilder.build();
     }
 
     private void appendValue(StringBuilder sb, ValuesDelta values, String column) {
diff --git a/src/com/android/contacts/editor/TextFieldsEditorView.java b/src/com/android/contacts/editor/TextFieldsEditorView.java
index 98b46ef..0ec71fb 100644
--- a/src/com/android/contacts/editor/TextFieldsEditorView.java
+++ b/src/com/android/contacts/editor/TextFieldsEditorView.java
@@ -21,7 +21,6 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.provider.ContactsContract;
-import android.telephony.PhoneNumberUtils;
 import android.text.Editable;
 import android.text.InputType;
 import android.text.TextUtils;
@@ -39,6 +38,7 @@
 
 import com.android.contacts.R;
 import com.android.contacts.common.model.RawContactDelta;
+import com.android.contacts.common.compat.PhoneNumberUtilsCompat;
 import com.android.contacts.common.ContactsUtils;
 import com.android.contacts.common.model.ValuesDelta;
 import com.android.contacts.common.model.account.AccountType.EditField;
@@ -255,7 +255,7 @@
             final String column = field.column;
             final String value = entry.getAsString(column);
             if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(kind.mimeType)) {
-                fieldView.setText(PhoneNumberUtils.createTtsSpannable(value));
+                fieldView.setText(PhoneNumberUtilsCompat.createTtsSpannable(value));
             } else {
                 fieldView.setText(value);
             }
diff --git a/src/com/android/contacts/list/ContactsUnavailableFragment.java b/src/com/android/contacts/list/ContactsUnavailableFragment.java
index 293ed04..2b9e184 100644
--- a/src/com/android/contacts/list/ContactsUnavailableFragment.java
+++ b/src/com/android/contacts/list/ContactsUnavailableFragment.java
@@ -33,6 +33,7 @@
 
 import com.android.contacts.activities.ActionBarAdapter.TabState;
 import com.android.contacts.R;
+import com.android.contacts.util.ProviderStatusCompat;
 
 /**
  * Fragment shown when contacts are unavailable. It contains provider status
@@ -94,27 +95,41 @@
             // The view hasn't been inflated yet.
             return;
         }
-        switch (providerStatus) {
-            case ProviderStatus.STATUS_EMPTY:
-                setTabInfo(mNoContactsMsgResId, mLastTab);
-                if (mLastTab == TabState.ALL) {
-                    mAddAccountButton.setVisibility(View.VISIBLE);
-                    mImportContactsButton.setVisibility(View.VISIBLE);
-                }
-                mProgress.setVisibility(View.GONE);
-                break;
-
-            case ProviderStatus.STATUS_BUSY:
-                mMessageView.setText(R.string.upgrade_in_progress);
-                mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
-                mMessageView.setVisibility(View.VISIBLE);
-                mAddAccountButton.setVisibility(View.GONE);
-                mImportContactsButton.setVisibility(View.GONE);
-                mProgress.setVisibility(View.VISIBLE);
-                break;
+        if (providerStatus == ProviderStatusCompat.STATUS_EMPTY) {
+            updateViewsForEmptyStatus();
+        } else if (providerStatus == ProviderStatusCompat.STATUS_BUSY) {
+            updateViewsForBusyStatus(R.string.upgrade_in_progress);
+        } else if (providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE){
+            updateViewsForBusyStatus(R.string.locale_change_in_progress);
         }
     }
 
+    /**
+     * Update views in the fragment when provider status is empty.
+     */
+    private void updateViewsForEmptyStatus() {
+        setTabInfo(mNoContactsMsgResId, mLastTab);
+        if (mLastTab == TabState.ALL) {
+            mAddAccountButton.setVisibility(View.VISIBLE);
+            mImportContactsButton.setVisibility(View.VISIBLE);
+        }
+        mProgress.setVisibility(View.GONE);
+    }
+
+    /**
+     * Update views in the fragment when provider status is busy.
+     *
+     * @param resId resource ID of the string to show in mMessageView.
+     */
+    private void updateViewsForBusyStatus(int resId) {
+        mMessageView.setText(resId);
+        mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
+        mMessageView.setVisibility(View.VISIBLE);
+        mAddAccountButton.setVisibility(View.GONE);
+        mImportContactsButton.setVisibility(View.GONE);
+        mProgress.setVisibility(View.VISIBLE);
+    }
+
     @Override
     public void onClick(View v) {
         if (mListener == null) {
@@ -138,7 +153,7 @@
         mNoContactsMsgResId = resId;
         mLastTab = callerTab;
         if ((mMessageView != null) && (mProviderStatus != null) &&
-                (mProviderStatus.equals(ProviderStatus.STATUS_EMPTY))) {
+                mProviderStatus.equals(ProviderStatusCompat.STATUS_EMPTY)) {
             if (resId != -1) {
                 mMessageView.setText(mNoContactsMsgResId);
                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
diff --git a/src/com/android/contacts/list/ProviderStatusWatcher.java b/src/com/android/contacts/list/ProviderStatusWatcher.java
index f42bc3e..50ed653 100644
--- a/src/com/android/contacts/list/ProviderStatusWatcher.java
+++ b/src/com/android/contacts/list/ProviderStatusWatcher.java
@@ -25,6 +25,8 @@
 import android.provider.ContactsContract.ProviderStatus;
 import android.util.Log;
 
+import com.android.contacts.util.ProviderStatusCompat;
+
 import com.google.common.collect.Lists;
 
 import java.util.ArrayList;
@@ -167,16 +169,16 @@
      * provider?) this may still cause ANRs.
      *
      * In order to avoid that, if we can't load the status within {@link #LOAD_WAIT_TIMEOUT_MS},
-     * we'll give up and just returns {@link ProviderStatus#STATUS_BUSY} in order to unblock
+     * we'll give up and just returns {@link ProviderStatusCompat#STATUS_BUSY} in order to unblock
      * the UI thread.  The actual result will be delivered later via {@link ProviderStatusListener}.
-     * (If {@link ProviderStatus#STATUS_BUSY} is returned, the app (should) shows an according
+     * (If {@link ProviderStatusCompat#STATUS_BUSY} is returned, the app (should) shows an according
      * message, like "contacts are being updated".)
      */
     public int getProviderStatus() {
         waitForLoaded();
 
         if (mProviderStatus == null) {
-            return ProviderStatus.STATUS_BUSY;
+            return ProviderStatusCompat.STATUS_BUSY;
         }
 
         return mProviderStatus;
diff --git a/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java b/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
index 10887cb..762ff6c 100644
--- a/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
+++ b/src/com/android/contacts/quickcontact/ExpandingEntryCardView.java
@@ -108,7 +108,7 @@
         private final Intent mIntent;
         private final Drawable mAlternateIcon;
         private final Intent mAlternateIntent;
-        private final String mAlternateContentDescription;
+        private Spannable mAlternateContentDescription;
         private final boolean mShouldApplyColor;
         private final boolean mIsEditable;
         private final EntryContextMenuInfo mEntryContextMenuInfo;
@@ -122,8 +122,8 @@
         public Entry(int id, Drawable mainIcon, String header, String subHeader,
                 Drawable subHeaderIcon, String text, Drawable textIcon,
                 Spannable primaryContentDescription, Intent intent,
-                Drawable alternateIcon, Intent alternateIntent, String alternateContentDescription,
-                boolean shouldApplyColor, boolean isEditable,
+                Drawable alternateIcon, Intent alternateIntent,
+                Spannable alternateContentDescription, boolean shouldApplyColor, boolean isEditable,
                 EntryContextMenuInfo entryContextMenuInfo, Drawable thirdIcon, Intent thirdIntent,
                 String thirdContentDescription, int thirdAction, Bundle thirdExtras,
                 int iconResourceId) {
@@ -190,7 +190,7 @@
             return mAlternateIntent;
         }
 
-        String getAlternateContentDescription() {
+        Spannable getAlternateContentDescription() {
             return mAlternateContentDescription;
         }
 
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 0f5b3b0..a3d92d4 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -1700,6 +1700,7 @@
         Drawable textIcon = null;
         StringBuilder primaryContentDescription = new StringBuilder();
         Spannable phoneContentDescription = null;
+        Spannable smsContentDescription = null;
         Intent intent = null;
         boolean shouldApplyColor = true;
         Drawable alternateIcon = null;
@@ -1857,6 +1858,8 @@
 
                 alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
                 alternateContentDescription.append(res.getString(R.string.sms_custom, header));
+                smsContentDescription = com.android.contacts.common.util.ContactDisplayUtils
+                        .getTelephoneTtsSpannable(alternateContentDescription.toString(), header);
 
                 if (CallUtil.isCallWithSubjectSupported(context)) {
                     thirdIcon = res.getDrawable(R.drawable.ic_call_note_white_24dp);
@@ -2082,7 +2085,10 @@
                         ? new SpannableString(primaryContentDescription.toString())
                         : phoneContentDescription,
                 intent, alternateIcon, alternateIntent,
-                alternateContentDescription.toString(), shouldApplyColor, isEditable,
+                smsContentDescription == null
+                        ? new SpannableString(alternateContentDescription.toString())
+                        : smsContentDescription,
+                shouldApplyColor, isEditable,
                 entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription, thirdAction,
                 thirdExtras, iconResourceId);
     }
diff --git a/src/com/android/contacts/util/AggregationSuggestionsCompat.java b/src/com/android/contacts/util/AggregationSuggestionsCompat.java
new file mode 100644
index 0000000..75d1333
--- /dev/null
+++ b/src/com/android/contacts/util/AggregationSuggestionsCompat.java
@@ -0,0 +1,98 @@
+/*
+ * 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.util;
+
+import android.net.Uri;
+import android.provider.ContactsContract;
+
+import java.util.ArrayList;
+
+/**
+ * This class contains Builder class extracted from ContactsContract, and it became visible in API
+ * level 23. We need maintain this class and keep it synced with ContactsContract.
+ */
+public class AggregationSuggestionsCompat {
+
+    /**
+     * Used to specify what kind of data is supplied for the suggestion query.
+     */
+    public static final String PARAMETER_MATCH_NAME = "name";
+
+    /**
+     * A convenience builder for aggregation suggestion content URIs.
+     */
+    public static final class Builder {
+        private long mContactId;
+        private final ArrayList<String> mValues = new ArrayList<String>();
+        private int mLimit;
+
+        /**
+         * Optional existing contact ID.  If it is not provided, the search
+         * will be based exclusively on the values supplied with {@link #addNameParameter}.
+         *
+         * @param contactId contact to find aggregation suggestions for
+         * @return This Builder object to allow for chaining of calls to builder methods
+         */
+        public Builder setContactId(long contactId) {
+            this.mContactId = contactId;
+            return this;
+        }
+
+        /**
+         * Add a name to be used when searching for aggregation suggestions.
+         *
+         * @param name name to find aggregation suggestions for
+         * @return This Builder object to allow for chaining of calls to builder methods
+         */
+        public Builder addNameParameter(String name) {
+            mValues.add(name);
+            return this;
+        }
+
+        /**
+         * Sets the Maximum number of suggested aggregations that should be returned.
+         * @param limit The maximum number of suggested aggregations
+         *
+         * @return This Builder object to allow for chaining of calls to builder methods
+         */
+        public Builder setLimit(int limit) {
+            mLimit = limit;
+            return this;
+        }
+
+        /**
+         * Combine all of the options that have been set and return a new {@link Uri}
+         * object for fetching aggregation suggestions.
+         */
+        public Uri build() {
+            android.net.Uri.Builder builder = ContactsContract.Contacts.CONTENT_URI.buildUpon();
+            builder.appendEncodedPath(String.valueOf(mContactId));
+            builder.appendPath(ContactsContract.Contacts.AggregationSuggestions.CONTENT_DIRECTORY);
+            if (mLimit != 0) {
+                builder.appendQueryParameter("limit", String.valueOf(mLimit));
+            }
+
+            int count = mValues.size();
+            for (int i = 0; i < count; i++) {
+                builder.appendQueryParameter("query", PARAMETER_MATCH_NAME
+                        + ":" + mValues.get(i));
+            }
+
+            return builder.build();
+        }
+    }
+}
diff --git a/src/com/android/contacts/util/ProviderStatusCompat.java b/src/com/android/contacts/util/ProviderStatusCompat.java
new file mode 100644
index 0000000..72476f9
--- /dev/null
+++ b/src/com/android/contacts/util/ProviderStatusCompat.java
@@ -0,0 +1,68 @@
+/*
+ * 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.util;
+
+import android.os.Build;
+import android.provider.ContactsContract.ProviderStatus;
+import com.android.contacts.common.compat.SdkVersionOverride;
+
+/**
+ * This class contains constants from the pre-M version of ContactsContract.ProviderStatus class
+ * and also the mappings between pre-M constants and M constants for compatibility purpose,
+ * because ProviderStatus class constant names and values changed and the class became visible in
+ * API level 23.
+ */
+public class ProviderStatusCompat {
+    /**
+     * Not instantiable.
+     */
+    private ProviderStatusCompat() {
+    }
+
+    public static final boolean USE_CURRENT_VERSION =
+            SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M;
+
+    public static final int STATUS_EMPTY = USE_CURRENT_VERSION ?
+            ProviderStatus.STATUS_EMPTY : ProviderStatusCompat.STATUS_NO_ACCOUNTS_NO_CONTACTS;
+
+    public static final int STATUS_BUSY = USE_CURRENT_VERSION ?
+            ProviderStatus.STATUS_BUSY : ProviderStatusCompat.STATUS_UPGRADING;
+
+    /**
+     * Default status of the provider, using the actual constant to guard against errors
+     */
+    public static final int STATUS_NORMAL = ProviderStatus.STATUS_NORMAL;
+
+    /**
+     * The following three constants are from pre-M.
+     *
+     * The status used when the provider is in the process of upgrading.  Contacts
+     * are temporarily unaccessible.
+     */
+    private static final int STATUS_UPGRADING = 1;
+
+    /**
+     * The status used during a locale change.
+     */
+    public static final int STATUS_CHANGING_LOCALE = 3;
+
+    /**
+     * The status that indicates that there are no accounts and no contacts
+     * on the device.
+     */
+    private static final int STATUS_NO_ACCOUNTS_NO_CONTACTS = 4;
+}