Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.compat;
     18 
     19 import android.net.Uri;
     20 import android.provider.ContactsContract;
     21 
     22 import java.util.ArrayList;
     23 
     24 /**
     25  * This class contains Builder class extracted from ContactsContract, and it became visible in API
     26  * level 23. We need maintain this class and keep it synced with ContactsContract.
     27  */
     28 public class AggregationSuggestionsCompat {
     29 
     30     /**
     31      * Used to specify what kind of data is supplied for the suggestion query.
     32      */
     33     public static final String PARAMETER_MATCH_NAME = "name";
     34 
     35     /**
     36      * A convenience builder for aggregation suggestion content URIs.
     37      */
     38     public static final class Builder {
     39         private long mContactId;
     40         private final ArrayList<String> mValues = new ArrayList<String>();
     41         private int mLimit;
     42 
     43         /**
     44          * Optional existing contact ID.  If it is not provided, the search
     45          * will be based exclusively on the values supplied with {@link #addNameParameter}.
     46          *
     47          * @param contactId contact to find aggregation suggestions for
     48          * @return This Builder object to allow for chaining of calls to builder methods
     49          */
     50         public Builder setContactId(long contactId) {
     51             this.mContactId = contactId;
     52             return this;
     53         }
     54 
     55         /**
     56          * Add a name to be used when searching for aggregation suggestions.
     57          *
     58          * @param name name to find aggregation suggestions for
     59          * @return This Builder object to allow for chaining of calls to builder methods
     60          */
     61         public Builder addNameParameter(String name) {
     62             mValues.add(name);
     63             return this;
     64         }
     65 
     66         /**
     67          * Sets the Maximum number of suggested aggregations that should be returned.
     68          * @param limit The maximum number of suggested aggregations
     69          *
     70          * @return This Builder object to allow for chaining of calls to builder methods
     71          */
     72         public Builder setLimit(int limit) {
     73             mLimit = limit;
     74             return this;
     75         }
     76 
     77         /**
     78          * Combine all of the options that have been set and return a new {@link Uri}
     79          * object for fetching aggregation suggestions.
     80          */
     81         public Uri build() {
     82             android.net.Uri.Builder builder = ContactsContract.Contacts.CONTENT_URI.buildUpon();
     83             builder.appendEncodedPath(String.valueOf(mContactId));
     84             builder.appendPath(ContactsContract.Contacts.AggregationSuggestions.CONTENT_DIRECTORY);
     85             if (mLimit != 0) {
     86                 builder.appendQueryParameter("limit", String.valueOf(mLimit));
     87             }
     88 
     89             int count = mValues.size();
     90             for (int i = 0; i < count; i++) {
     91                 builder.appendQueryParameter("query", PARAMETER_MATCH_NAME
     92                         + ":" + mValues.get(i));
     93             }
     94 
     95             return builder.build();
     96         }
     97     }
     98 }
     99