summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/searchfragment/cp2/SearchContactsCursorLoader.java
blob: 23f368f54e70bbd3cca50bf631e11e56b6eb0987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.searchfragment.cp2;

import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Directory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.android.contacts.common.preference.ContactsPreferences;
import com.android.dialer.searchfragment.common.Projections;
import com.android.dialer.searchfragment.common.SearchCursor;
import com.android.dialer.smartdial.SmartDialCursorLoader;

/** Cursor Loader for CP2 contacts. */
public final class SearchContactsCursorLoader extends CursorLoader {

  private final String query;
  private final boolean isRegularSearch;

  /** @param query Contacts cursor will be filtered based on this query. */
  public SearchContactsCursorLoader(
      Context context, @Nullable String query, boolean isRegularSearch) {
    super(
        context,
        buildUri(query),
        getProjection(context),
        getWhere(context),
        null,
        getSortKey(context) + " ASC");
    this.query = TextUtils.isEmpty(query) ? "" : query;
    this.isRegularSearch = isRegularSearch;
  }

  private static String[] getProjection(Context context) {
    ContactsPreferences contactsPrefs = new ContactsPreferences(context);
    boolean displayOrderPrimary =
        (contactsPrefs.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY);
    return displayOrderPrimary
        ? Projections.CP2_PROJECTION
        : Projections.CP2_PROJECTION_ALTERNATIVE;
  }

  private static String getWhere(Context context) {
    String where = getProjection(context)[Projections.DISPLAY_NAME] + " IS NOT NULL";
    where += " AND " + Phone.NUMBER + " IS NOT NULL";
    return where;
  }

  private static String getSortKey(Context context) {
    ContactsPreferences contactsPrefs = new ContactsPreferences(context);
    boolean sortOrderPrimary =
        (contactsPrefs.getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY);
    return sortOrderPrimary ? Phone.SORT_KEY_PRIMARY : Phone.SORT_KEY_ALTERNATIVE;
  }

  private static Uri buildUri(String query) {
    return Phone.CONTENT_FILTER_URI.buildUpon().appendPath(query).build();
  }

  @Override
  public Cursor loadInBackground() {
    return isRegularSearch ? regularSearchLoadInBackground() : dialpadSearchLoadInBackground();
  }

  private Cursor regularSearchLoadInBackground() {
    return RegularSearchCursor.newInstance(getContext(), super.loadInBackground());
  }

  private Cursor dialpadSearchLoadInBackground() {
    SmartDialCursorLoader loader = new SmartDialCursorLoader(getContext());
    loader.configureQuery(query);
    Cursor cursor = loader.loadInBackground();
    return SmartDialCursor.newInstance(getContext(), cursor);
  }

  static class SmartDialCursor extends MergeCursor implements SearchCursor {

    static SmartDialCursor newInstance(Context context, Cursor smartDialCursor) {
      if (smartDialCursor.getCount() == 0) {
        return new SmartDialCursor(new Cursor[] {new MatrixCursor(Projections.CP2_PROJECTION)});
      }

      MatrixCursor headerCursor = new MatrixCursor(HEADER_PROJECTION);
      headerCursor.addRow(new String[] {context.getString(R.string.all_contacts)});
      return new SmartDialCursor(
          new Cursor[] {headerCursor, convertSmartDialCursorToSearchCursor(smartDialCursor)});
    }

    private SmartDialCursor(Cursor[] cursors) {
      super(cursors);
    }

    @Override
    public boolean isHeader() {
      return isFirst();
    }

    @Override
    public boolean updateQuery(@Nullable String query) {
      return false;
    }

    @Override
    public long getDirectoryId() {
      return Directory.DEFAULT;
    }

    private static MatrixCursor convertSmartDialCursorToSearchCursor(Cursor smartDialCursor) {
      MatrixCursor cursor = new MatrixCursor(Projections.CP2_PROJECTION);
      if (!smartDialCursor.moveToFirst()) {
        return cursor;
      }

      do {
        Object[] newRow = new Object[Projections.CP2_PROJECTION.length];
        for (int i = 0; i < Projections.CP2_PROJECTION.length; i++) {
          String column = Projections.CP2_PROJECTION[i];
          int index = smartDialCursor.getColumnIndex(column);
          if (index != -1) {
            switch (smartDialCursor.getType(index)) {
              case FIELD_TYPE_INTEGER:
                newRow[i] = smartDialCursor.getInt(index);
                break;
              case FIELD_TYPE_STRING:
                newRow[i] = smartDialCursor.getString(index);
                break;
              case FIELD_TYPE_FLOAT:
                newRow[i] = smartDialCursor.getFloat(index);
                break;
              case FIELD_TYPE_BLOB:
                newRow[i] = smartDialCursor.getBlob(index);
                break;
              case FIELD_TYPE_NULL:
              default:
                // No-op
                break;
            }
          }
        }
        cursor.addRow(newRow);
      } while (smartDialCursor.moveToNext());
      return cursor;
    }
  }

  static class RegularSearchCursor extends MergeCursor implements SearchCursor {

    static RegularSearchCursor newInstance(Context context, Cursor regularSearchCursor) {
      if (regularSearchCursor.getCount() == 0) {
        return new RegularSearchCursor(new Cursor[] {new MatrixCursor(Projections.CP2_PROJECTION)});
      }

      MatrixCursor headerCursor = new MatrixCursor(HEADER_PROJECTION);
      headerCursor.addRow(new String[] {context.getString(R.string.all_contacts)});
      return new RegularSearchCursor(new Cursor[] {headerCursor, regularSearchCursor});
    }

    public RegularSearchCursor(Cursor[] cursors) {
      super(cursors);
    }

    @Override
    public boolean isHeader() {
      return isFirst();
    }

    @Override
    public boolean updateQuery(@NonNull String query) {
      return false; // no-op
    }

    @Override
    public long getDirectoryId() {
      return 0; // no-op
    }
  }
}