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