1/*
2 * Copyright (C) 2011 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
17package com.android.ex.chips;
18
19import android.content.ContentResolver;
20import android.content.ContentValues;
21import android.content.Context;
22import android.os.Build;
23import android.provider.ContactsContract;
24import android.provider.ContactsContract.Data;
25import android.text.TextUtils;
26import android.widget.MultiAutoCompleteTextView;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.Collection;
31
32public class ChipsUtil {
33
34    /**
35     * @return true when the caller can use Chips UI in its environment.
36     */
37    public static boolean supportsChipsUi() {
38        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
39    }
40
41    public static boolean tryUpdateRecencyInfo(MultiAutoCompleteTextView... views) {
42        for (MultiAutoCompleteTextView view : views) {
43            if (view instanceof RecipientEditTextView) {
44                updateRecencyInfo((RecipientEditTextView)view);
45            }
46        }
47        return true;
48    }
49
50    // TODO: check this works
51    public static void updateRecencyInfo(RecipientEditTextView view) {
52        final Context context = view.getContext();
53        final ContentResolver resolver = context.getContentResolver();
54        final long currentTimeMillis = System.currentTimeMillis();
55
56        final Collection<Long> contactIds = view.getContactIds();
57        if (contactIds != null) {
58            StringBuilder whereBuilder = new StringBuilder();
59            ArrayList<String> whereArgs = new ArrayList<String>();
60            String[] questionMarks = new String[contactIds.size()];
61            for (Long contactId : contactIds) {
62                whereArgs.add(String.valueOf(contactId));
63            }
64            Arrays.fill(questionMarks, "?");
65            whereBuilder.append(ContactsContract.Contacts._ID + " IN (").
66                    append(TextUtils.join(",", questionMarks)).
67                    append(")");
68
69            ContentValues values = new ContentValues();
70            values.put(ContactsContract.Contacts.LAST_TIME_CONTACTED,
71                    System.currentTimeMillis());
72            resolver.update(ContactsContract.Contacts.CONTENT_URI, values,
73                    whereBuilder.toString(), whereArgs.toArray(new String[0]));
74        }
75
76        final Collection<Long> dataIds = view.getDataIds();
77        if (dataIds != null) {
78            StringBuilder whereBuilder = new StringBuilder();
79            ArrayList<String> whereArgs = new ArrayList<String>();
80            String[] questionMarks = new String[dataIds.size()];
81            for (Long dataId : dataIds) {
82                whereArgs.add(String.valueOf(dataId));
83            }
84            Arrays.fill(questionMarks, "?");
85            whereBuilder.append(ContactsContract.Data._ID + " IN (").
86            append(TextUtils.join(",", questionMarks)).
87            append(")");
88
89            final ContentValues values = new ContentValues();
90            values.put("last_time_contacted", currentTimeMillis);
91            resolver.update(Data.CONTENT_URI, values,
92                    whereBuilder.toString(), whereArgs.toArray(new String[0]));
93        }
94    }
95}