1/*
2 * Copyright (C) 2013 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 */
16package com.android.dialer.list;
17
18import android.content.Context;
19import android.database.Cursor;
20import android.net.Uri;
21import android.provider.ContactsContract;
22import android.text.TextUtils;
23
24import com.android.contacts.common.CallUtil;
25import com.android.contacts.common.list.DirectoryPartition;
26import com.android.contacts.common.list.PhoneNumberListAdapter;
27import com.android.dialer.calllog.ContactInfo;
28import com.android.dialer.service.CachedNumberLookupService;
29import com.android.dialer.service.CachedNumberLookupService.CachedContactInfo;
30
31/**
32 * List adapter to display regular search results.
33 */
34public class RegularSearchListAdapter extends DialerPhoneNumberListAdapter {
35
36    public RegularSearchListAdapter(Context context) {
37        super(context);
38    }
39
40    public CachedContactInfo getContactInfo(
41            CachedNumberLookupService lookupService, int position) {
42        ContactInfo info = new ContactInfo();
43        CachedContactInfo cacheInfo = lookupService.buildCachedContactInfo(info);
44        final Cursor item = (Cursor) getItem(position);
45        if (item != null) {
46            info.name = item.getString(PhoneQuery.DISPLAY_NAME);
47            info.type = item.getInt(PhoneQuery.PHONE_TYPE);
48            info.label = item.getString(PhoneQuery.PHONE_LABEL);
49            info.number = item.getString(PhoneQuery.PHONE_NUMBER);
50            final String photoUriStr = item.getString(PhoneQuery.PHOTO_URI);
51            info.photoUri = photoUriStr == null ? null : Uri.parse(photoUriStr);
52
53            cacheInfo.setLookupKey(item.getString(PhoneQuery.LOOKUP_KEY));
54
55            final int partitionIndex = getPartitionForPosition(position);
56            final DirectoryPartition partition =
57                (DirectoryPartition) getPartition(partitionIndex);
58            final long directoryId = partition.getDirectoryId();
59            final String sourceName = partition.getLabel();
60            if (isExtendedDirectory(directoryId)) {
61                cacheInfo.setExtendedSource(sourceName, directoryId);
62            } else {
63                cacheInfo.setDirectorySource(sourceName, directoryId);
64            }
65        }
66        return cacheInfo;
67    }
68
69    @Override
70    public void setQueryString(String queryString) {
71        final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString());
72        boolean changed = false;
73        changed |= setShortcutEnabled(SHORTCUT_DIRECT_CALL, showNumberShortcuts);
74        // Either one of the add contacts options should be enabled. If the user entered
75        // a dialable number, then clicking add to contact should add it as a number.
76        // Otherwise, it should add it to a new contact as a name.
77        changed |= setShortcutEnabled(SHORTCUT_ADD_NUMBER_TO_CONTACTS, showNumberShortcuts);
78        changed |= setShortcutEnabled(SHORTCUT_MAKE_VIDEO_CALL,
79                showNumberShortcuts && CallUtil.isVideoEnabled(getContext()));
80        if (changed) {
81            notifyDataSetChanged();
82        }
83        super.setQueryString(queryString);
84    }
85}
86