diff options
author | calderwoodra <calderwoodra@google.com> | 2017-08-08 19:04:48 -0700 |
---|---|---|
committer | Eric Erfanian <erfanian@google.com> | 2017-08-09 17:33:42 -0700 |
commit | d021f7a61ae8debee8e2b3ca69a6aee99be41753 (patch) | |
tree | d978da56fd913c918b9f0040dd966a860e4e69a4 /java/com/android/dialer/searchfragment/cp2 | |
parent | a9189cc5fddba6de17e0a65616506fa0486521e8 (diff) |
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
Diffstat (limited to 'java/com/android/dialer/searchfragment/cp2')
-rw-r--r-- | java/com/android/dialer/searchfragment/cp2/AndroidManifest.xml | 16 | ||||
-rw-r--r-- | java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java (renamed from java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java) | 6 | ||||
-rw-r--r-- | java/com/android/dialer/searchfragment/cp2/SearchContactViewHolder.java | 16 | ||||
-rw-r--r-- | java/com/android/dialer/searchfragment/cp2/SearchContactsCursor.java | 64 | ||||
-rw-r--r-- | java/com/android/dialer/searchfragment/cp2/SearchContactsCursorLoader.java | 7 | ||||
-rw-r--r-- | java/com/android/dialer/searchfragment/cp2/res/values/strings.xml | 20 |
6 files changed, 118 insertions, 11 deletions
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 @@ +<!-- + ~ 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 + --> +<manifest package="com.android.dialer.searchfragment.cp2"/>
\ No newline at end of file diff --git a/java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java b/java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java index 05e98cc84..d5fcfba4f 100644 --- a/java/com/android/dialer/searchfragment/cp2/SearchContactCursor.java +++ b/java/com/android/dialer/searchfragment/cp2/ContactFilterCursor.java @@ -34,12 +34,12 @@ import java.util.ArrayList; import java.util.List; /** - * Wrapper for a cursor returned by {@link SearchContactsCursorLoader}. + * Wrapper for a cursor containing all on device contacts. * * <p>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 { +final class ContactFilterCursor implements Cursor { private final Cursor cursor; // List of cursor ids that are valid for displaying after filtering. @@ -66,7 +66,7 @@ public final class SearchContactCursor implements Cursor { * @param cursor with projection {@link Projections#PHONE_PROJECTION}. * @param query to filter cursor results. */ - public SearchContactCursor(Cursor cursor, @Nullable String query) { + ContactFilterCursor(Cursor cursor, @Nullable String query) { // TODO(calderwoodra) investigate copying this into a MatrixCursor and holding in memory this.cursor = cursor; filter(query); 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. + * + * <p>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 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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 + --> +<resources> + <!-- Label for a list of all contacts on device. [CHAR LIMIT=30]--> + <string name="all_contacts">All contacts</string> +</resources>
\ No newline at end of file |