Backport AggregationSuggestion URI builder

Bug: 25629359
Change-Id: Ie498d0a08c78ce98c46b82f9c7e6d448b119d130
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/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();
+        }
+    }
+}