Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d23b633b authored by Wenyi Wang's avatar Wenyi Wang Committed by Android (Google) Code Review
Browse files

Merge "Backport AggregationSuggestion URI builder" into ub-contactsdialer-b-dev

parents 42a08d90 18fd9387
Loading
Loading
Loading
Loading
+22 −8
Original line number Diff line number Diff line
@@ -21,11 +21,11 @@ import android.content.Context;
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.provider.ContactsContract.RawContacts;
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 @@ public class AggregationSuggestionEngine extends HandlerThread {
            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) {
            builder.addNameParameter(nameSb.toString());
                uriBuilder.addNameParameter(nameSb.toString());
            }

            if (phoneticNameSb.length() != 0) {
            builder.addNameParameter(phoneticNameSb.toString());
                uriBuilder.addNameParameter(phoneticNameSb.toString());
            }
            return uriBuilder.build();
        }

        return builder.build();
        // For previous SDKs, use the backup plan.
        final AggregationSuggestionsCompat.Builder uriBuilder =
                new AggregationSuggestionsCompat.Builder()
                .setLimit(mSuggestionsLimit)
                .setContactId(mContactId);
        if (nameSb.length() != 0) {
            uriBuilder.addNameParameter(nameSb.toString());
        }
        if (phoneticNameSb.length() != 0) {
            uriBuilder.addNameParameter(phoneticNameSb.toString());
        }
        return uriBuilder.build();
    }

    private void appendValue(StringBuilder sb, ValuesDelta values, String column) {
+98 −0
Original line number Diff line number Diff line
/*
 * 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();
        }
    }
}