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.app.list;
17
18import android.content.Context;
19import android.database.Cursor;
20import android.net.Uri;
21import android.text.TextUtils;
22import com.android.contacts.common.ContactsUtils;
23import com.android.contacts.common.compat.DirectoryCompat;
24import com.android.contacts.common.list.DirectoryPartition;
25import com.android.dialer.phonenumbercache.CachedNumberLookupService;
26import com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo;
27import com.android.dialer.phonenumbercache.ContactInfo;
28import com.android.dialer.phonenumberutil.PhoneNumberHelper;
29import com.android.dialer.util.CallUtil;
30
31/** List adapter to display regular search results. */
32public class RegularSearchListAdapter extends DialerPhoneNumberListAdapter {
33
34  protected boolean mIsQuerySipAddress;
35
36  public RegularSearchListAdapter(Context context) {
37    super(context);
38    setShortcutEnabled(SHORTCUT_CREATE_NEW_CONTACT, false);
39    setShortcutEnabled(SHORTCUT_ADD_TO_EXISTING_CONTACT, false);
40  }
41
42  public CachedContactInfo getContactInfo(CachedNumberLookupService lookupService, int position) {
43    ContactInfo info = new ContactInfo();
44    CachedContactInfo cacheInfo = lookupService.buildCachedContactInfo(info);
45    final Cursor item = (Cursor) getItem(position);
46    if (item != null) {
47      final DirectoryPartition partition =
48          (DirectoryPartition) getPartition(getPartitionForPosition(position));
49      final long directoryId = partition.getDirectoryId();
50      final boolean isExtendedDirectory = isExtendedDirectory(directoryId);
51
52      info.name = item.getString(PhoneQuery.DISPLAY_NAME);
53      info.type = item.getInt(PhoneQuery.PHONE_TYPE);
54      info.label = item.getString(PhoneQuery.PHONE_LABEL);
55      info.number = item.getString(PhoneQuery.PHONE_NUMBER);
56      final String photoUriStr = item.getString(PhoneQuery.PHOTO_URI);
57      info.photoUri = photoUriStr == null ? null : Uri.parse(photoUriStr);
58      /*
59       * An extended directory is custom directory in the app, but not a directory provided by
60       * framework. So it can't be USER_TYPE_WORK.
61       *
62       * When a search result is selected, RegularSearchFragment calls getContactInfo and
63       * cache the resulting @{link ContactInfo} into local db. Set usertype to USER_TYPE_WORK
64       * only if it's NOT extended directory id and is enterprise directory.
65       */
66      info.userType =
67          !isExtendedDirectory && DirectoryCompat.isEnterpriseDirectoryId(directoryId)
68              ? ContactsUtils.USER_TYPE_WORK
69              : ContactsUtils.USER_TYPE_CURRENT;
70
71      cacheInfo.setLookupKey(item.getString(PhoneQuery.LOOKUP_KEY));
72
73      final String sourceName = partition.getLabel();
74      if (isExtendedDirectory) {
75        cacheInfo.setExtendedSource(sourceName, directoryId);
76      } else {
77        cacheInfo.setDirectorySource(sourceName, directoryId);
78      }
79    }
80    return cacheInfo;
81  }
82
83  @Override
84  public String getFormattedQueryString() {
85    if (mIsQuerySipAddress) {
86      // Return unnormalized SIP address
87      return getQueryString();
88    }
89    return super.getFormattedQueryString();
90  }
91
92  @Override
93  public void setQueryString(String queryString) {
94    // Don't show actions if the query string contains a letter.
95    final boolean showNumberShortcuts =
96        !TextUtils.isEmpty(getFormattedQueryString()) && hasDigitsInQueryString();
97    mIsQuerySipAddress = PhoneNumberHelper.isUriNumber(queryString);
98
99    if (isChanged(showNumberShortcuts)) {
100      notifyDataSetChanged();
101    }
102    super.setQueryString(queryString);
103  }
104
105  protected boolean isChanged(boolean showNumberShortcuts) {
106    boolean changed = false;
107    changed |= setShortcutEnabled(SHORTCUT_DIRECT_CALL, showNumberShortcuts || mIsQuerySipAddress);
108    changed |= setShortcutEnabled(SHORTCUT_SEND_SMS_MESSAGE, showNumberShortcuts);
109    changed |=
110        setShortcutEnabled(
111            SHORTCUT_MAKE_VIDEO_CALL, showNumberShortcuts && CallUtil.isVideoEnabled(getContext()));
112    return changed;
113  }
114
115  /** Whether there is at least one digit in the query string. */
116  private boolean hasDigitsInQueryString() {
117    String queryString = getQueryString();
118    int length = queryString.length();
119    for (int i = 0; i < length; i++) {
120      if (Character.isDigit(queryString.charAt(i))) {
121        return true;
122      }
123    }
124    return false;
125  }
126}
127