Home | History | Annotate | Download | only in cp2
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.android.dialer.searchfragment.cp2;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.database.MatrixCursor;
     22 import android.database.MergeCursor;
     23 import android.provider.ContactsContract.Directory;
     24 import android.support.annotation.Nullable;
     25 import com.android.dialer.searchfragment.common.SearchCursor;
     26 
     27 /**
     28  * {@link SearchCursor} implementation for displaying on device contacts.
     29  *
     30  * <p>Inserts header "All Contacts" at position 0.
     31  */
     32 final class SearchContactsCursor extends MergeCursor implements SearchCursor {
     33 
     34   private final ContactFilterCursor contactFilterCursor;
     35   private final Context context;
     36 
     37   static SearchContactsCursor newInstance(
     38       Context context, ContactFilterCursor contactFilterCursor) {
     39     MatrixCursor headerCursor = new MatrixCursor(HEADER_PROJECTION);
     40     headerCursor.addRow(new String[] {context.getString(R.string.all_contacts)});
     41     return new SearchContactsCursor(new Cursor[] {headerCursor, contactFilterCursor}, context);
     42   }
     43 
     44   private SearchContactsCursor(Cursor[] cursors, Context context) {
     45     super(cursors);
     46     this.contactFilterCursor = (ContactFilterCursor) cursors[1];
     47     this.context = context;
     48   }
     49 
     50   @Override
     51   public boolean isHeader() {
     52     return isFirst();
     53   }
     54 
     55   @Override
     56   public boolean updateQuery(@Nullable String query) {
     57     contactFilterCursor.filter(query, context);
     58     return true;
     59   }
     60 
     61   @Override
     62   public long getDirectoryId() {
     63     return Directory.DEFAULT;
     64   }
     65 
     66   @Override
     67   public int getCount() {
     68     // If we don't have any contents, we don't want to show the header
     69     int count = contactFilterCursor.getCount();
     70     return count == 0 ? 0 : count + 1;
     71   }
     72 }
     73