From d021f7a61ae8debee8e2b3ca69a6aee99be41753 Mon Sep 17 00:00:00 2001 From: calderwoodra Date: Tue, 8 Aug 2017 19:04:48 -0700 Subject: Abstracted search cursors in SearchCursor interface This change should help with separating concerns between each module in searchfragment/. Now cursors returned from each CursorLoader are expected to insert their own headers if they want to. This also simplifies the logic in SearchCursorManager and allows for easier implementation of new cursors. Future CLs will include abstracting ViewHolders and CursorLoaders. Bug: 37209462 Test: existing PiperOrigin-RevId: 164676135 Change-Id: Ib50090c3990c903cfd78f3a168032edd88f0fe56 --- .../dialer/searchfragment/common/SearchCursor.java | 38 ++ .../dialer/searchfragment/cp2/AndroidManifest.xml | 16 + .../searchfragment/cp2/ContactFilterCursor.java | 392 +++++++++++++++++++++ .../searchfragment/cp2/SearchContactCursor.java | 392 --------------------- .../cp2/SearchContactViewHolder.java | 16 +- .../searchfragment/cp2/SearchContactsCursor.java | 64 ++++ .../cp2/SearchContactsCursorLoader.java | 7 +- .../searchfragment/cp2/res/values/strings.xml | 20 ++ .../searchfragment/list/NewSearchFragment.java | 12 +- .../dialer/searchfragment/list/SearchAdapter.java | 25 +- .../searchfragment/list/SearchCursorManager.java | 119 +++---- .../nearbyplaces/AndroidManifest.xml | 2 +- .../nearbyplaces/NearbyPlacesCursor.java | 64 ++++ .../nearbyplaces/NearbyPlacesCursorLoader.java | 6 + .../searchfragment/testing/TestSearchCursor.java | 47 +++ 15 files changed, 727 insertions(+), 493 deletions(-) create mode 100644 java/com/android/dialer/searchfragment/common/SearchCursor.java create mode 100644 java/com/android/dialer/searchfragment/cp2/AndroidManifest.xml create mode 100644 java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java delete mode 100644 java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java create mode 100644 java/com/android/dialer/searchfragment/cp2/SearchContactsCursor.java create mode 100644 java/com/android/dialer/searchfragment/cp2/res/values/strings.xml create mode 100644 java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursor.java create mode 100644 java/com/android/dialer/searchfragment/testing/TestSearchCursor.java (limited to 'java') diff --git a/java/com/android/dialer/searchfragment/common/SearchCursor.java b/java/com/android/dialer/searchfragment/common/SearchCursor.java new file mode 100644 index 000000000..368ee09d6 --- /dev/null +++ b/java/com/android/dialer/searchfragment/common/SearchCursor.java @@ -0,0 +1,38 @@ +/* + * 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.common; + +import android.database.Cursor; +import android.support.annotation.NonNull; + +/** Base cursor interface needed for all cursors used in search. */ +public interface SearchCursor extends Cursor { + + String[] HEADER_PROJECTION = {"header_text"}; + + int HEADER_TEXT_POSITION = 0; + + /** Returns true if the current cursor position is a header */ + boolean isHeader(); + + /** + * Notifies the cursor that the query has updated. + * + * @return true if the data set has changed. + */ + boolean updateQuery(@NonNull String query); +} diff --git a/java/com/android/dialer/searchfragment/cp2/AndroidManifest.xml b/java/com/android/dialer/searchfragment/cp2/AndroidManifest.xml new file mode 100644 index 000000000..8d2efca40 --- /dev/null +++ b/java/com/android/dialer/searchfragment/cp2/AndroidManifest.xml @@ -0,0 +1,16 @@ + + \ No newline at end of file diff --git a/java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java b/java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java new file mode 100644 index 000000000..d5fcfba4f --- /dev/null +++ b/java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java @@ -0,0 +1,392 @@ +/* + * 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.ContentResolver; +import android.database.CharArrayBuffer; +import android.database.ContentObserver; +import android.database.Cursor; +import android.database.DataSetObserver; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.IntDef; +import android.support.annotation.Nullable; +import android.text.TextUtils; +import com.android.dialer.searchfragment.common.Projections; +import com.android.dialer.searchfragment.common.QueryFilteringUtil; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; +import java.util.List; + +/** + * Wrapper for a cursor containing all on device contacts. + * + *

This cursor removes duplicate phone numbers associated with the same contact and can filter + * contacts based on a query by calling {@link #filter(String)}. + */ +final class ContactFilterCursor implements Cursor { + + private final Cursor cursor; + // List of cursor ids that are valid for displaying after filtering. + private final List queryFilteredPositions = new ArrayList<>(); + + private int currentPosition = 0; + + @Retention(RetentionPolicy.SOURCE) + @IntDef({ + Qualification.NUMBERS_ARE_NOT_DUPLICATES, + Qualification.NEW_NUMBER_IS_MORE_QUALIFIED, + Qualification.CURRENT_MORE_QUALIFIED + }) + private @interface Qualification { + /** Numbers are not duplicates (i.e. neither is more qualified than the other). */ + int NUMBERS_ARE_NOT_DUPLICATES = 0; + /** Number are duplicates and new number is more qualified than the existing number. */ + int NEW_NUMBER_IS_MORE_QUALIFIED = 1; + /** Numbers are duplicates but current/existing number is more qualified than new number. */ + int CURRENT_MORE_QUALIFIED = 2; + } + + /** + * @param cursor with projection {@link Projections#PHONE_PROJECTION}. + * @param query to filter cursor results. + */ + ContactFilterCursor(Cursor cursor, @Nullable String query) { + // TODO(calderwoodra) investigate copying this into a MatrixCursor and holding in memory + this.cursor = cursor; + filter(query); + } + + /** + * Filters out contacts that do not match the query. + * + *

The query can have at least 1 of 3 forms: + * + *

+ * + *

A contact is considered a match if: + * + *

+ */ + public void filter(@Nullable String query) { + if (query == null) { + query = ""; + } + queryFilteredPositions.clear(); + + // On some devices, contacts have multiple rows with identical phone numbers. These numbers are + // considered duplicates. Since the order might not be guaranteed, we compare all of the numbers + // and hold onto the most qualified one as the one we want to display to the user. + // See #getQualification for details on how qualification is determined. + int previousMostQualifiedPosition = 0; + String previousName = ""; + String previousMostQualifiedNumber = ""; + + query = query.toLowerCase(); + cursor.moveToPosition(-1); + + while (cursor.moveToNext()) { + int position = cursor.getPosition(); + String currentNumber = cursor.getString(Projections.PHONE_NUMBER); + String currentName = cursor.getString(Projections.PHONE_DISPLAY_NAME); + + if (!previousName.equals(currentName)) { + previousName = currentName; + previousMostQualifiedNumber = currentNumber; + previousMostQualifiedPosition = position; + } else { + // Since the contact name is the same, check if this number is a duplicate + switch (getQualification(currentNumber, previousMostQualifiedNumber)) { + case Qualification.CURRENT_MORE_QUALIFIED: + // Number is a less qualified duplicate, ignore it. + continue; + case Qualification.NEW_NUMBER_IS_MORE_QUALIFIED: + // If number wasn't filtered out before, remove it and add it's more qualified version. + if (queryFilteredPositions.contains(previousMostQualifiedPosition)) { + queryFilteredPositions.remove(previousMostQualifiedPosition); + queryFilteredPositions.add(position); + } + previousMostQualifiedNumber = currentNumber; + previousMostQualifiedPosition = position; + continue; + case Qualification.NUMBERS_ARE_NOT_DUPLICATES: + default: + previousMostQualifiedNumber = currentNumber; + previousMostQualifiedPosition = position; + } + } + + if (TextUtils.isEmpty(query) + || QueryFilteringUtil.nameMatchesT9Query(query, previousName) + || QueryFilteringUtil.numberMatchesNumberQuery(query, previousMostQualifiedNumber) + || QueryFilteringUtil.nameContainsQuery(query, previousName)) { + queryFilteredPositions.add(previousMostQualifiedPosition); + } + } + currentPosition = 0; + cursor.moveToFirst(); + } + + /** + * @param number that may or may not be more qualified than the existing most qualified number + * @param mostQualifiedNumber currently most qualified number associated with same contact + * @return {@link Qualification} where the more qualified number is the number with the most + * digits. If the digits are the same, the number with the most formatting is more qualified. + */ + private @Qualification int getQualification(String number, String mostQualifiedNumber) { + // Ignore formatting + String numberDigits = QueryFilteringUtil.digitsOnly(number); + String qualifiedNumberDigits = QueryFilteringUtil.digitsOnly(mostQualifiedNumber); + + // If the numbers are identical, return version with more formatting + if (qualifiedNumberDigits.equals(numberDigits)) { + if (mostQualifiedNumber.length() >= number.length()) { + return Qualification.CURRENT_MORE_QUALIFIED; + } else { + return Qualification.NEW_NUMBER_IS_MORE_QUALIFIED; + } + } + + // If one number is a suffix of another, then return the longer one. + // If they are equal, then return the current most qualified number. + if (qualifiedNumberDigits.endsWith(numberDigits)) { + return Qualification.CURRENT_MORE_QUALIFIED; + } + if (numberDigits.endsWith(qualifiedNumberDigits)) { + return Qualification.NEW_NUMBER_IS_MORE_QUALIFIED; + } + return Qualification.NUMBERS_ARE_NOT_DUPLICATES; + } + + @Override + public boolean moveToPosition(int position) { + currentPosition = position; + return currentPosition < getCount() + && cursor.moveToPosition(queryFilteredPositions.get(currentPosition)); + } + + @Override + public boolean move(int offset) { + currentPosition += offset; + return moveToPosition(currentPosition); + } + + @Override + public int getCount() { + return queryFilteredPositions.size(); + } + + @Override + public boolean isFirst() { + return currentPosition == 0; + } + + @Override + public boolean isLast() { + return currentPosition == getCount() - 1; + } + + @Override + public int getPosition() { + return currentPosition; + } + + @Override + public boolean moveToFirst() { + return moveToPosition(0); + } + + @Override + public boolean moveToLast() { + return moveToPosition(getCount() - 1); + } + + @Override + public boolean moveToNext() { + return moveToPosition(++currentPosition); + } + + @Override + public boolean moveToPrevious() { + return moveToPosition(--currentPosition); + } + + // Methods below simply call the corresponding method in cursor. + @Override + public boolean isBeforeFirst() { + return cursor.isBeforeFirst(); + } + + @Override + public boolean isAfterLast() { + return cursor.isAfterLast(); + } + + @Override + public int getColumnIndex(String columnName) { + return cursor.getColumnIndex(columnName); + } + + @Override + public int getColumnIndexOrThrow(String columnName) { + return cursor.getColumnIndexOrThrow(columnName); + } + + @Override + public String getColumnName(int columnIndex) { + return cursor.getColumnName(columnIndex); + } + + @Override + public String[] getColumnNames() { + return cursor.getColumnNames(); + } + + @Override + public int getColumnCount() { + return cursor.getColumnCount(); + } + + @Override + public byte[] getBlob(int columnIndex) { + return cursor.getBlob(columnIndex); + } + + @Override + public String getString(int columnIndex) { + return cursor.getString(columnIndex); + } + + @Override + public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) { + cursor.copyStringToBuffer(columnIndex, buffer); + } + + @Override + public short getShort(int columnIndex) { + return cursor.getShort(columnIndex); + } + + @Override + public int getInt(int columnIndex) { + return cursor.getInt(columnIndex); + } + + @Override + public long getLong(int columnIndex) { + return cursor.getLong(columnIndex); + } + + @Override + public float getFloat(int columnIndex) { + return cursor.getFloat(columnIndex); + } + + @Override + public double getDouble(int columnIndex) { + return cursor.getDouble(columnIndex); + } + + @Override + public int getType(int columnIndex) { + return cursor.getType(columnIndex); + } + + @Override + public boolean isNull(int columnIndex) { + return cursor.isNull(columnIndex); + } + + @Override + public void deactivate() { + cursor.deactivate(); + } + + @Override + public boolean requery() { + return cursor.requery(); + } + + @Override + public void close() { + cursor.close(); + } + + @Override + public boolean isClosed() { + return cursor.isClosed(); + } + + @Override + public void registerContentObserver(ContentObserver observer) { + cursor.registerContentObserver(observer); + } + + @Override + public void unregisterContentObserver(ContentObserver observer) { + cursor.unregisterContentObserver(observer); + } + + @Override + public void registerDataSetObserver(DataSetObserver observer) { + cursor.registerDataSetObserver(observer); + } + + @Override + public void unregisterDataSetObserver(DataSetObserver observer) { + cursor.unregisterDataSetObserver(observer); + } + + @Override + public void setNotificationUri(ContentResolver cr, Uri uri) { + cursor.setNotificationUri(cr, uri); + } + + @Override + public Uri getNotificationUri() { + return cursor.getNotificationUri(); + } + + @Override + public boolean getWantsAllOnMoveCalls() { + return cursor.getWantsAllOnMoveCalls(); + } + + @Override + public void setExtras(Bundle extras) { + cursor.setExtras(extras); + } + + @Override + public Bundle getExtras() { + return cursor.getExtras(); + } + + @Override + public Bundle respond(Bundle extras) { + return cursor.respond(extras); + } +} diff --git a/java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java b/java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java deleted file mode 100644 index 05e98cc84..000000000 --- a/java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java +++ /dev/null @@ -1,392 +0,0 @@ -/* - * 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.ContentResolver; -import android.database.CharArrayBuffer; -import android.database.ContentObserver; -import android.database.Cursor; -import android.database.DataSetObserver; -import android.net.Uri; -import android.os.Bundle; -import android.support.annotation.IntDef; -import android.support.annotation.Nullable; -import android.text.TextUtils; -import com.android.dialer.searchfragment.common.Projections; -import com.android.dialer.searchfragment.common.QueryFilteringUtil; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.util.ArrayList; -import java.util.List; - -/** - * Wrapper for a cursor returned by {@link SearchContactsCursorLoader}. - * - *

This cursor removes duplicate phone numbers associated with the same contact and can filter - * contacts based on a query by calling {@link #filter(String)}. - */ -public final class SearchContactCursor implements Cursor { - - private final Cursor cursor; - // List of cursor ids that are valid for displaying after filtering. - private final List queryFilteredPositions = new ArrayList<>(); - - private int currentPosition = 0; - - @Retention(RetentionPolicy.SOURCE) - @IntDef({ - Qualification.NUMBERS_ARE_NOT_DUPLICATES, - Qualification.NEW_NUMBER_IS_MORE_QUALIFIED, - Qualification.CURRENT_MORE_QUALIFIED - }) - private @interface Qualification { - /** Numbers are not duplicates (i.e. neither is more qualified than the other). */ - int NUMBERS_ARE_NOT_DUPLICATES = 0; - /** Number are duplicates and new number is more qualified than the existing number. */ - int NEW_NUMBER_IS_MORE_QUALIFIED = 1; - /** Numbers are duplicates but current/existing number is more qualified than new number. */ - int CURRENT_MORE_QUALIFIED = 2; - } - - /** - * @param cursor with projection {@link Projections#PHONE_PROJECTION}. - * @param query to filter cursor results. - */ - public SearchContactCursor(Cursor cursor, @Nullable String query) { - // TODO(calderwoodra) investigate copying this into a MatrixCursor and holding in memory - this.cursor = cursor; - filter(query); - } - - /** - * Filters out contacts that do not match the query. - * - *

The query can have at least 1 of 3 forms: - * - *

- * - *

A contact is considered a match if: - * - *

- */ - public void filter(@Nullable String query) { - if (query == null) { - query = ""; - } - queryFilteredPositions.clear(); - - // On some devices, contacts have multiple rows with identical phone numbers. These numbers are - // considered duplicates. Since the order might not be guaranteed, we compare all of the numbers - // and hold onto the most qualified one as the one we want to display to the user. - // See #getQualification for details on how qualification is determined. - int previousMostQualifiedPosition = 0; - String previousName = ""; - String previousMostQualifiedNumber = ""; - - query = query.toLowerCase(); - cursor.moveToPosition(-1); - - while (cursor.moveToNext()) { - int position = cursor.getPosition(); - String currentNumber = cursor.getString(Projections.PHONE_NUMBER); - String currentName = cursor.getString(Projections.PHONE_DISPLAY_NAME); - - if (!previousName.equals(currentName)) { - previousName = currentName; - previousMostQualifiedNumber = currentNumber; - previousMostQualifiedPosition = position; - } else { - // Since the contact name is the same, check if this number is a duplicate - switch (getQualification(currentNumber, previousMostQualifiedNumber)) { - case Qualification.CURRENT_MORE_QUALIFIED: - // Number is a less qualified duplicate, ignore it. - continue; - case Qualification.NEW_NUMBER_IS_MORE_QUALIFIED: - // If number wasn't filtered out before, remove it and add it's more qualified version. - if (queryFilteredPositions.contains(previousMostQualifiedPosition)) { - queryFilteredPositions.remove(previousMostQualifiedPosition); - queryFilteredPositions.add(position); - } - previousMostQualifiedNumber = currentNumber; - previousMostQualifiedPosition = position; - continue; - case Qualification.NUMBERS_ARE_NOT_DUPLICATES: - default: - previousMostQualifiedNumber = currentNumber; - previousMostQualifiedPosition = position; - } - } - - if (TextUtils.isEmpty(query) - || QueryFilteringUtil.nameMatchesT9Query(query, previousName) - || QueryFilteringUtil.numberMatchesNumberQuery(query, previousMostQualifiedNumber) - || QueryFilteringUtil.nameContainsQuery(query, previousName)) { - queryFilteredPositions.add(previousMostQualifiedPosition); - } - } - currentPosition = 0; - cursor.moveToFirst(); - } - - /** - * @param number that may or may not be more qualified than the existing most qualified number - * @param mostQualifiedNumber currently most qualified number associated with same contact - * @return {@link Qualification} where the more qualified number is the number with the most - * digits. If the digits are the same, the number with the most formatting is more qualified. - */ - private @Qualification int getQualification(String number, String mostQualifiedNumber) { - // Ignore formatting - String numberDigits = QueryFilteringUtil.digitsOnly(number); - String qualifiedNumberDigits = QueryFilteringUtil.digitsOnly(mostQualifiedNumber); - - // If the numbers are identical, return version with more formatting - if (qualifiedNumberDigits.equals(numberDigits)) { - if (mostQualifiedNumber.length() >= number.length()) { - return Qualification.CURRENT_MORE_QUALIFIED; - } else { - return Qualification.NEW_NUMBER_IS_MORE_QUALIFIED; - } - } - - // If one number is a suffix of another, then return the longer one. - // If they are equal, then return the current most qualified number. - if (qualifiedNumberDigits.endsWith(numberDigits)) { - return Qualification.CURRENT_MORE_QUALIFIED; - } - if (numberDigits.endsWith(qualifiedNumberDigits)) { - return Qualification.NEW_NUMBER_IS_MORE_QUALIFIED; - } - return Qualification.NUMBERS_ARE_NOT_DUPLICATES; - } - - @Override - public boolean moveToPosition(int position) { - currentPosition = position; - return currentPosition < getCount() - && cursor.moveToPosition(queryFilteredPositions.get(currentPosition)); - } - - @Override - public boolean move(int offset) { - currentPosition += offset; - return moveToPosition(currentPosition); - } - - @Override - public int getCount() { - return queryFilteredPositions.size(); - } - - @Override - public boolean isFirst() { - return currentPosition == 0; - } - - @Override - public boolean isLast() { - return currentPosition == getCount() - 1; - } - - @Override - public int getPosition() { - return currentPosition; - } - - @Override - public boolean moveToFirst() { - return moveToPosition(0); - } - - @Override - public boolean moveToLast() { - return moveToPosition(getCount() - 1); - } - - @Override - public boolean moveToNext() { - return moveToPosition(++currentPosition); - } - - @Override - public boolean moveToPrevious() { - return moveToPosition(--currentPosition); - } - - // Methods below simply call the corresponding method in cursor. - @Override - public boolean isBeforeFirst() { - return cursor.isBeforeFirst(); - } - - @Override - public boolean isAfterLast() { - return cursor.isAfterLast(); - } - - @Override - public int getColumnIndex(String columnName) { - return cursor.getColumnIndex(columnName); - } - - @Override - public int getColumnIndexOrThrow(String columnName) { - return cursor.getColumnIndexOrThrow(columnName); - } - - @Override - public String getColumnName(int columnIndex) { - return cursor.getColumnName(columnIndex); - } - - @Override - public String[] getColumnNames() { - return cursor.getColumnNames(); - } - - @Override - public int getColumnCount() { - return cursor.getColumnCount(); - } - - @Override - public byte[] getBlob(int columnIndex) { - return cursor.getBlob(columnIndex); - } - - @Override - public String getString(int columnIndex) { - return cursor.getString(columnIndex); - } - - @Override - public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) { - cursor.copyStringToBuffer(columnIndex, buffer); - } - - @Override - public short getShort(int columnIndex) { - return cursor.getShort(columnIndex); - } - - @Override - public int getInt(int columnIndex) { - return cursor.getInt(columnIndex); - } - - @Override - public long getLong(int columnIndex) { - return cursor.getLong(columnIndex); - } - - @Override - public float getFloat(int columnIndex) { - return cursor.getFloat(columnIndex); - } - - @Override - public double getDouble(int columnIndex) { - return cursor.getDouble(columnIndex); - } - - @Override - public int getType(int columnIndex) { - return cursor.getType(columnIndex); - } - - @Override - public boolean isNull(int columnIndex) { - return cursor.isNull(columnIndex); - } - - @Override - public void deactivate() { - cursor.deactivate(); - } - - @Override - public boolean requery() { - return cursor.requery(); - } - - @Override - public void close() { - cursor.close(); - } - - @Override - public boolean isClosed() { - return cursor.isClosed(); - } - - @Override - public void registerContentObserver(ContentObserver observer) { - cursor.registerContentObserver(observer); - } - - @Override - public void unregisterContentObserver(ContentObserver observer) { - cursor.unregisterContentObserver(observer); - } - - @Override - public void registerDataSetObserver(DataSetObserver observer) { - cursor.registerDataSetObserver(observer); - } - - @Override - public void unregisterDataSetObserver(DataSetObserver observer) { - cursor.unregisterDataSetObserver(observer); - } - - @Override - public void setNotificationUri(ContentResolver cr, Uri uri) { - cursor.setNotificationUri(cr, uri); - } - - @Override - public Uri getNotificationUri() { - return cursor.getNotificationUri(); - } - - @Override - public boolean getWantsAllOnMoveCalls() { - return cursor.getWantsAllOnMoveCalls(); - } - - @Override - public void setExtras(Bundle extras) { - cursor.setExtras(extras); - } - - @Override - public Bundle getExtras() { - return cursor.getExtras(); - } - - @Override - public Bundle respond(Bundle extras) { - return cursor.respond(extras); - } -} diff --git a/java/com/android/dialer/searchfragment/cp2/SearchContactViewHolder.java b/java/com/android/dialer/searchfragment/cp2/SearchContactViewHolder.java index 4b5cab901..2bd9cdd8a 100644 --- a/java/com/android/dialer/searchfragment/cp2/SearchContactViewHolder.java +++ b/java/com/android/dialer/searchfragment/cp2/SearchContactViewHolder.java @@ -38,6 +38,7 @@ import com.android.dialer.lettertile.LetterTileDrawable; import com.android.dialer.searchfragment.common.Projections; import com.android.dialer.searchfragment.common.QueryBoldingUtil; import com.android.dialer.searchfragment.common.R; +import com.android.dialer.searchfragment.common.SearchCursor; import com.android.dialer.telecom.TelecomUtil; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -77,7 +78,7 @@ public final class SearchContactViewHolder extends ViewHolder implements OnClick * Binds the ViewHolder with a cursor from {@link SearchContactsCursorLoader} with the data found * at the cursors set position. */ - public void bind(Cursor cursor, String query) { + public void bind(SearchCursor cursor, String query) { number = cursor.getString(Projections.PHONE_NUMBER); String name = cursor.getString(Projections.PHONE_DISPLAY_NAME); String label = getLabel(context.getResources(), cursor); @@ -109,17 +110,18 @@ public final class SearchContactViewHolder extends ViewHolder implements OnClick } } - private boolean shouldShowPhoto(Cursor cursor) { + private boolean shouldShowPhoto(SearchCursor cursor) { int currentPosition = cursor.getPosition(); - if (currentPosition == 0) { - return true; - } else { - String currentLookupKey = cursor.getString(Projections.PHONE_LOOKUP_KEY); - cursor.moveToPosition(currentPosition - 1); + String currentLookupKey = cursor.getString(Projections.PHONE_LOOKUP_KEY); + cursor.moveToPosition(currentPosition - 1); + + if (!cursor.isHeader() && !cursor.isBeforeFirst()) { String previousLookupKey = cursor.getString(Projections.PHONE_LOOKUP_KEY); cursor.moveToPosition(currentPosition); return !currentLookupKey.equals(previousLookupKey); } + cursor.moveToPosition(currentPosition); + return true; } private static Uri getContactUri(Cursor cursor) { diff --git a/java/com/android/dialer/searchfragment/cp2/SearchContactsCursor.java b/java/com/android/dialer/searchfragment/cp2/SearchContactsCursor.java new file mode 100644 index 000000000..18c9ecd7f --- /dev/null +++ b/java/com/android/dialer/searchfragment/cp2/SearchContactsCursor.java @@ -0,0 +1,64 @@ +/* + * 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.database.Cursor; +import android.database.MatrixCursor; +import android.database.MergeCursor; +import android.support.annotation.Nullable; +import com.android.dialer.searchfragment.common.SearchCursor; + +/** + * {@link SearchCursor} implementation for displaying on device contacts. + * + *

Inserts header "All Contacts" at position 0. + */ +final class SearchContactsCursor extends MergeCursor implements SearchCursor { + + private final ContactFilterCursor contactFilterCursor; + + public static SearchContactsCursor newInstnace( + Context context, ContactFilterCursor contactFilterCursor) { + MatrixCursor headerCursor = new MatrixCursor(HEADER_PROJECTION); + headerCursor.addRow(new String[] {context.getString(R.string.all_contacts)}); + return new SearchContactsCursor(new Cursor[] {headerCursor, contactFilterCursor}); + } + + private SearchContactsCursor(Cursor[] cursors) { + super(cursors); + contactFilterCursor = (ContactFilterCursor) cursors[1]; + } + + @Override + public boolean isHeader() { + return isFirst(); + } + + @Override + public boolean updateQuery(@Nullable String query) { + contactFilterCursor.filter(query); + return true; + } + + @Override + public int getCount() { + // If we don't have any contents, we don't want to show the header + int count = contactFilterCursor.getCount(); + return count == 0 ? 0 : count + 1; + } +} diff --git a/java/com/android/dialer/searchfragment/cp2/SearchContactsCursorLoader.java b/java/com/android/dialer/searchfragment/cp2/SearchContactsCursorLoader.java index c72f28b25..d75a66122 100644 --- a/java/com/android/dialer/searchfragment/cp2/SearchContactsCursorLoader.java +++ b/java/com/android/dialer/searchfragment/cp2/SearchContactsCursorLoader.java @@ -37,6 +37,11 @@ public final class SearchContactsCursorLoader extends CursorLoader { @Override public Cursor loadInBackground() { - return new SearchContactCursor(super.loadInBackground(), null); + // All contacts + Cursor cursor = super.loadInBackground(); + // Filtering logic + ContactFilterCursor contactFilterCursor = new ContactFilterCursor(cursor, null); + // Header logic + return SearchContactsCursor.newInstnace(getContext(), contactFilterCursor); } } diff --git a/java/com/android/dialer/searchfragment/cp2/res/values/strings.xml b/java/com/android/dialer/searchfragment/cp2/res/values/strings.xml new file mode 100644 index 000000000..5462dc926 --- /dev/null +++ b/java/com/android/dialer/searchfragment/cp2/res/values/strings.xml @@ -0,0 +1,20 @@ + + + + + All contacts + \ No newline at end of file diff --git a/java/com/android/dialer/searchfragment/list/NewSearchFragment.java b/java/com/android/dialer/searchfragment/list/NewSearchFragment.java index 70358bb16..1a489513c 100644 --- a/java/com/android/dialer/searchfragment/list/NewSearchFragment.java +++ b/java/com/android/dialer/searchfragment/list/NewSearchFragment.java @@ -34,8 +34,10 @@ import android.view.ViewGroup; import android.view.animation.Interpolator; import com.android.contacts.common.extensions.PhoneDirectoryExtenderAccessor; import com.android.dialer.animation.AnimUtils; +import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.dialer.common.concurrent.ThreadUtil; +import com.android.dialer.searchfragment.common.SearchCursor; import com.android.dialer.searchfragment.cp2.SearchContactsCursorLoader; import com.android.dialer.searchfragment.nearbyplaces.NearbyPlacesCursorLoader; import com.android.dialer.util.PermissionsUtil; @@ -72,7 +74,7 @@ public final class NewSearchFragment extends Fragment public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle bundle) { View view = inflater.inflate(R.layout.fragment_search, parent, false); - adapter = new SearchAdapter(getContext()); + adapter = new SearchAdapter(getContext(), new SearchCursorManager()); emptyContentView = view.findViewById(R.id.empty_view); recyclerView = view.findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); @@ -113,10 +115,14 @@ public final class NewSearchFragment extends Fragment @Override public void onLoadFinished(Loader loader, Cursor cursor) { + if (!(cursor instanceof SearchCursor)) { + throw Assert.createIllegalStateFailException("Cursors must implement SearchCursor"); + } + if (loader instanceof SearchContactsCursorLoader) { - adapter.setContactsCursor(cursor); + adapter.setContactsCursor((SearchCursor) cursor); } else if (loader instanceof NearbyPlacesCursorLoader) { - adapter.setNearbyPlacesCursor(cursor); + adapter.setNearbyPlacesCursor((SearchCursor) cursor); } else { throw new IllegalStateException("Invalid loader: " + loader); } diff --git a/java/com/android/dialer/searchfragment/list/SearchAdapter.java b/java/com/android/dialer/searchfragment/list/SearchAdapter.java index faa80fe85..c8588fc7d 100644 --- a/java/com/android/dialer/searchfragment/list/SearchAdapter.java +++ b/java/com/android/dialer/searchfragment/list/SearchAdapter.java @@ -17,12 +17,12 @@ package com.android.dialer.searchfragment.list; import android.content.Context; -import android.database.Cursor; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.LayoutInflater; import android.view.ViewGroup; import com.android.dialer.common.Assert; +import com.android.dialer.searchfragment.common.SearchCursor; import com.android.dialer.searchfragment.cp2.SearchContactViewHolder; import com.android.dialer.searchfragment.list.SearchCursorManager.RowType; import com.android.dialer.searchfragment.nearbyplaces.NearbyPlaceViewHolder; @@ -35,9 +35,9 @@ class SearchAdapter extends RecyclerView.Adapter { private String query; - SearchAdapter(Context context) { - searchCursorManager = new SearchCursorManager(); + SearchAdapter(Context context, SearchCursorManager searchCursorManager) { this.context = context; + this.searchCursorManager = searchCursorManager; } @Override @@ -49,6 +49,7 @@ class SearchAdapter extends RecyclerView.Adapter { case RowType.NEARBY_PLACES_ROW: return new NearbyPlaceViewHolder( LayoutInflater.from(context).inflate(R.layout.search_contact_row, root, false)); + case RowType.CONTACT_HEADER: case RowType.DIRECTORY_HEADER: case RowType.NEARBY_PLACES_HEADER: return new HeaderViewHolder( @@ -68,20 +69,17 @@ class SearchAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(ViewHolder holder, int position) { if (holder instanceof SearchContactViewHolder) { - Cursor cursor = searchCursorManager.getCursor(position); - ((SearchContactViewHolder) holder).bind(cursor, query); + ((SearchContactViewHolder) holder).bind(searchCursorManager.getCursor(position), query); } else if (holder instanceof NearbyPlaceViewHolder) { - Cursor cursor = searchCursorManager.getCursor(position); - ((NearbyPlaceViewHolder) holder).bind(cursor, query); + ((NearbyPlaceViewHolder) holder).bind(searchCursorManager.getCursor(position), query); } else if (holder instanceof HeaderViewHolder) { - String header = context.getString(searchCursorManager.getHeaderText(position)); - ((HeaderViewHolder) holder).setHeader(header); + ((HeaderViewHolder) holder).setHeader(searchCursorManager.getHeaderText(position)); } else { throw Assert.createIllegalStateFailException("Invalid ViewHolder: " + holder); } } - void setContactsCursor(Cursor cursor) { + void setContactsCursor(SearchCursor cursor) { searchCursorManager.setContactsCursor(cursor); notifyDataSetChanged(); } @@ -97,11 +95,12 @@ class SearchAdapter extends RecyclerView.Adapter { public void setQuery(String query) { this.query = query; - searchCursorManager.setQuery(query); - notifyDataSetChanged(); + if (searchCursorManager.setQuery(query)) { + notifyDataSetChanged(); + } } - public void setNearbyPlacesCursor(Cursor nearbyPlacesCursor) { + public void setNearbyPlacesCursor(SearchCursor nearbyPlacesCursor) { searchCursorManager.setNearbyPlacesCursor(nearbyPlacesCursor); notifyDataSetChanged(); } diff --git a/java/com/android/dialer/searchfragment/list/SearchCursorManager.java b/java/com/android/dialer/searchfragment/list/SearchCursorManager.java index 45d66aab8..68f770af9 100644 --- a/java/com/android/dialer/searchfragment/list/SearchCursorManager.java +++ b/java/com/android/dialer/searchfragment/list/SearchCursorManager.java @@ -16,11 +16,9 @@ package com.android.dialer.searchfragment.list; -import android.database.Cursor; import android.support.annotation.IntDef; -import android.support.annotation.StringRes; import com.android.dialer.common.Assert; -import com.android.dialer.searchfragment.cp2.SearchContactCursor; +import com.android.dialer.searchfragment.common.SearchCursor; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -30,9 +28,9 @@ import java.lang.annotation.RetentionPolicy; *

This class accepts three cursors: * *

* *

The key purpose of this class is to compose three aforementioned cursors together to function @@ -50,6 +48,7 @@ final class SearchCursorManager { @Retention(RetentionPolicy.SOURCE) @IntDef({ SearchCursorManager.RowType.INVALID, + SearchCursorManager.RowType.CONTACT_HEADER, SearchCursorManager.RowType.CONTACT_ROW, SearchCursorManager.RowType.NEARBY_PLACES_HEADER, SearchCursorManager.RowType.NEARBY_PLACES_ROW, @@ -58,23 +57,25 @@ final class SearchCursorManager { }) @interface RowType { int INVALID = 0; + /** Header to mark the start of contact rows. */ + int CONTACT_HEADER = 1; /** A row containing contact information for contacts stored locally on device. */ - int CONTACT_ROW = 1; + int CONTACT_ROW = 2; /** Header to mark the end of contact rows and start of nearby places rows. */ - int NEARBY_PLACES_HEADER = 2; + int NEARBY_PLACES_HEADER = 3; /** A row containing nearby places information/search results. */ - int NEARBY_PLACES_ROW = 3; + int NEARBY_PLACES_ROW = 4; /** Header to mark the end of the previous row set and start of directory rows. */ - int DIRECTORY_HEADER = 4; + int DIRECTORY_HEADER = 5; /** A row containing contact information for contacts stored externally in corp directories. */ - int DIRECTORY_ROW = 5; + int DIRECTORY_ROW = 6; } - private Cursor contactsCursor = null; - private Cursor nearbyPlacesCursor = null; - private Cursor corpDirectoryCursor = null; + private SearchCursor contactsCursor = null; + private SearchCursor nearbyPlacesCursor = null; + private SearchCursor corpDirectoryCursor = null; - void setContactsCursor(Cursor cursor) { + void setContactsCursor(SearchCursor cursor) { if (cursor == contactsCursor) { return; } @@ -90,7 +91,7 @@ final class SearchCursorManager { } } - void setNearbyPlacesCursor(Cursor cursor) { + void setNearbyPlacesCursor(SearchCursor cursor) { if (cursor == nearbyPlacesCursor) { return; } @@ -106,7 +107,7 @@ final class SearchCursorManager { } } - void setCorpDirectoryCursor(Cursor cursor) { + void setCorpDirectoryCursor(SearchCursor cursor) { if (cursor == corpDirectoryCursor) { return; } @@ -122,14 +123,23 @@ final class SearchCursorManager { } } - void setQuery(String query) { + boolean setQuery(String query) { + boolean updated = false; if (contactsCursor != null) { - // TODO(calderwoodra): abstract this - ((SearchContactCursor) contactsCursor).filter(query); + updated = contactsCursor.updateQuery(query); } + + if (nearbyPlacesCursor != null) { + updated |= nearbyPlacesCursor.updateQuery(query); + } + + if (corpDirectoryCursor != null) { + updated |= corpDirectoryCursor.updateQuery(query); + } + return updated; } - /** @return the sum of counts of all cursors, including headers. */ + /** Returns the sum of counts of all cursors, including headers. */ int getCount() { int count = 0; if (contactsCursor != null) { @@ -137,12 +147,10 @@ final class SearchCursorManager { } if (nearbyPlacesCursor != null) { - count++; // header count += nearbyPlacesCursor.getCount(); } if (corpDirectoryCursor != null) { - count++; // header count += corpDirectoryCursor.getCount(); } @@ -151,54 +159,30 @@ final class SearchCursorManager { @RowType int getRowType(int position) { - if (contactsCursor != null) { - position -= contactsCursor.getCount(); - - if (position < 0) { - return SearchCursorManager.RowType.CONTACT_ROW; - } + SearchCursor cursor = getCursor(position); + if (cursor == contactsCursor) { + return cursor.isHeader() ? RowType.CONTACT_HEADER : RowType.CONTACT_ROW; } - if (nearbyPlacesCursor != null) { - if (position == 0) { - return SearchCursorManager.RowType.NEARBY_PLACES_HEADER; - } else { - position--; // header - } - - position -= nearbyPlacesCursor.getCount(); - - if (position < 0) { - return SearchCursorManager.RowType.NEARBY_PLACES_ROW; - } + if (cursor == nearbyPlacesCursor) { + return cursor.isHeader() ? RowType.NEARBY_PLACES_HEADER : RowType.NEARBY_PLACES_ROW; } - if (corpDirectoryCursor != null) { - if (position == 0) { - return SearchCursorManager.RowType.DIRECTORY_HEADER; - } else { - position--; // header - } - - position -= corpDirectoryCursor.getCount(); - - if (position < 0) { - return SearchCursorManager.RowType.DIRECTORY_ROW; - } + if (cursor == corpDirectoryCursor) { + return cursor.isHeader() ? RowType.DIRECTORY_HEADER : RowType.DIRECTORY_ROW; } - throw Assert.createIllegalStateFailException("No valid row type."); } /** - * Gets cursor corresponding to position in coelesced list of search cursors. Callers should + * Gets cursor corresponding to position in coalesced list of search cursors. Callers should * ensure that {@link #getRowType(int)} doesn't correspond to header position, otherwise an * exception will be thrown. * - * @param position in coalecsed list of search cursors + * @param position in coalesced list of search cursors * @return Cursor moved to position specific to passed in position. */ - Cursor getCursor(int position) { + SearchCursor getCursor(int position) { if (contactsCursor != null) { int count = contactsCursor.getCount(); @@ -210,8 +194,6 @@ final class SearchCursorManager { } if (nearbyPlacesCursor != null) { - Assert.checkArgument(position != 0, "No valid cursor, position is nearby places header."); - position--; // header int count = nearbyPlacesCursor.getCount(); if (position - count < 0) { @@ -222,8 +204,6 @@ final class SearchCursorManager { } if (corpDirectoryCursor != null) { - Assert.checkArgument(position != 0, "No valid cursor, position is directory search header."); - position--; // header int count = corpDirectoryCursor.getCount(); if (position - count < 0) { @@ -236,21 +216,8 @@ final class SearchCursorManager { throw Assert.createIllegalStateFailException("No valid cursor."); } - @StringRes - int getHeaderText(int position) { - @RowType int rowType = getRowType(position); - switch (rowType) { - case RowType.NEARBY_PLACES_HEADER: - return R.string.nearby_places; - case RowType.DIRECTORY_HEADER: // TODO(calderwoodra) - case RowType.DIRECTORY_ROW: - case RowType.CONTACT_ROW: - case RowType.NEARBY_PLACES_ROW: - case RowType.INVALID: - default: - throw Assert.createIllegalStateFailException( - "Invalid row type, position " + position + " is rowtype " + rowType); - } + String getHeaderText(int position) { + return getCursor(position).getString(SearchCursor.HEADER_TEXT_POSITION); } /** removes all cursors. */ diff --git a/java/com/android/dialer/searchfragment/nearbyplaces/AndroidManifest.xml b/java/com/android/dialer/searchfragment/nearbyplaces/AndroidManifest.xml index 178cd83c3..52fb08671 100644 --- a/java/com/android/dialer/searchfragment/nearbyplaces/AndroidManifest.xml +++ b/java/com/android/dialer/searchfragment/nearbyplaces/AndroidManifest.xml @@ -13,4 +13,4 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License --> - \ No newline at end of file + \ No newline at end of file diff --git a/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursor.java b/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursor.java new file mode 100644 index 000000000..a4142a41d --- /dev/null +++ b/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursor.java @@ -0,0 +1,64 @@ +/* + * 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.nearbyplaces; + +import android.content.Context; +import android.database.Cursor; +import android.database.MatrixCursor; +import android.database.MergeCursor; +import android.support.annotation.Nullable; +import com.android.dialer.searchfragment.common.SearchCursor; + +/** {@link SearchCursor} implementation for displaying on nearby places. */ +final class NearbyPlacesCursor extends MergeCursor implements SearchCursor { + + private final Cursor nearbyPlacesCursor; + + public static NearbyPlacesCursor newInstnace(Context context, Cursor nearbyPlacesCursor) { + MatrixCursor headerCursor = new MatrixCursor(HEADER_PROJECTION); + headerCursor.addRow(new String[] {context.getString(R.string.nearby_places)}); + return new NearbyPlacesCursor(new Cursor[] {headerCursor, nearbyPlacesCursor}); + } + + private NearbyPlacesCursor(Cursor[] cursors) { + super(cursors); + nearbyPlacesCursor = cursors[1]; + } + + @Override + public boolean isHeader() { + return isFirst(); + } + + @Override + public boolean updateQuery(@Nullable String query) { + // When the query changes, a new network request is made for nearby places. Meaning this cursor + // will be closed and another created, so return false. + return false; + } + + @Override + public int getCount() { + // If we don't have any contents, we don't want to show the header + if (nearbyPlacesCursor == null || nearbyPlacesCursor.isClosed()) { + return 0; + } + + int count = nearbyPlacesCursor.getCount(); + return count == 0 ? 0 : count + 1; + } +} diff --git a/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursorLoader.java b/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursorLoader.java index 9f3193e92..6807a6e6b 100644 --- a/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursorLoader.java +++ b/java/com/android/dialer/searchfragment/nearbyplaces/NearbyPlacesCursorLoader.java @@ -18,6 +18,7 @@ package com.android.dialer.searchfragment.nearbyplaces; import android.content.Context; import android.content.CursorLoader; +import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import com.android.contacts.common.extensions.PhoneDirectoryExtenderAccessor; @@ -32,6 +33,11 @@ public final class NearbyPlacesCursorLoader extends CursorLoader { super(context, getContentUri(context, query), Projections.PHONE_PROJECTION, null, null, null); } + @Override + public Cursor loadInBackground() { + return NearbyPlacesCursor.newInstnace(getContext(), super.loadInBackground()); + } + private static Uri getContentUri(Context context, String query) { return PhoneDirectoryExtenderAccessor.get(context) .getContentUri() diff --git a/java/com/android/dialer/searchfragment/testing/TestSearchCursor.java b/java/com/android/dialer/searchfragment/testing/TestSearchCursor.java new file mode 100644 index 000000000..9a0b95789 --- /dev/null +++ b/java/com/android/dialer/searchfragment/testing/TestSearchCursor.java @@ -0,0 +1,47 @@ +/* + * 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.testing; + +import android.database.Cursor; +import android.database.MatrixCursor; +import android.database.MergeCursor; +import android.support.annotation.Nullable; +import com.android.dialer.searchfragment.common.SearchCursor; + +/** {@link SearchCursor} implementation useful for testing with a header inserted at position 0. */ +public final class TestSearchCursor extends MergeCursor implements SearchCursor { + + public static TestSearchCursor newInstance(Cursor cursor, String header) { + MatrixCursor headerRow = new MatrixCursor(HEADER_PROJECTION); + headerRow.addRow(new String[] {header}); + return new TestSearchCursor(new Cursor[] {headerRow, cursor}); + } + + private TestSearchCursor(Cursor[] cursors) { + super(cursors); + } + + @Override + public boolean isHeader() { + return isFirst(); + } + + @Override + public boolean updateQuery(@Nullable String query) { + return false; + } +} -- cgit v1.2.3