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