Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2009 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.quicksearchbox;
     18 
     19 import com.google.common.annotations.VisibleForTesting;
     20 
     21 import android.database.DataSetObservable;
     22 import android.database.DataSetObserver;
     23 
     24 import java.util.ArrayList;
     25 
     26 /**
     27  * A SuggestionCursor that is backed by a list of Suggestions.
     28  */
     29 public class ListSuggestionCursor extends AbstractSuggestionCursorWrapper {
     30 
     31     private static final int DEFAULT_CAPACITY = 16;
     32 
     33     private final DataSetObservable mDataSetObservable = new DataSetObservable();
     34 
     35     private final ArrayList<Suggestion> mSuggestions;
     36 
     37     private int mPos = 0;
     38 
     39     public ListSuggestionCursor(String userQuery) {
     40         this(userQuery, DEFAULT_CAPACITY);
     41     }
     42 
     43     @VisibleForTesting
     44     public ListSuggestionCursor(String userQuery, Suggestion...suggestions) {
     45         this(userQuery, suggestions.length);
     46         for (Suggestion suggestion : suggestions) {
     47             mSuggestions.add(suggestion);
     48         }
     49     }
     50 
     51     public ListSuggestionCursor(String userQuery, int capacity) {
     52         super(userQuery);
     53         mSuggestions = new ArrayList<Suggestion>(capacity);
     54     }
     55 
     56     /**
     57      * Adds a suggestion from another suggestion cursor.
     58      *
     59      * @return {@code true} if the suggestion was added.
     60      */
     61     public boolean add(Suggestion suggestion) {
     62         mSuggestions.add(suggestion);
     63         return true;
     64     }
     65 
     66     public void close() {
     67         mSuggestions.clear();
     68     }
     69 
     70     public int getPosition() {
     71         return mPos;
     72     }
     73 
     74     public void moveTo(int pos) {
     75         mPos = pos;
     76     }
     77 
     78     public boolean moveToNext() {
     79         int size = mSuggestions.size();
     80         if (mPos >= size) {
     81             // Already past the end
     82             return false;
     83         }
     84         mPos++;
     85         return mPos < size;
     86     }
     87 
     88     public void removeRow() {
     89         mSuggestions.remove(mPos);
     90     }
     91 
     92     public void replaceRow(Suggestion suggestion) {
     93         mSuggestions.set(mPos, suggestion);
     94     }
     95 
     96     public int getCount() {
     97         return mSuggestions.size();
     98     }
     99 
    100     @Override
    101     protected Suggestion current() {
    102         return mSuggestions.get(mPos);
    103     }
    104 
    105     @Override
    106     public String toString() {
    107         return getClass().getSimpleName() + "{[" + getUserQuery() + "] " + mSuggestions + "}";
    108     }
    109 
    110     /**
    111      * Register an observer that is called when changes happen to this data set.
    112      *
    113      * @param observer gets notified when the data set changes.
    114      */
    115     public void registerDataSetObserver(DataSetObserver observer) {
    116         mDataSetObservable.registerObserver(observer);
    117     }
    118 
    119     /**
    120      * Unregister an observer that has previously been registered with
    121      * {@link #registerDataSetObserver(DataSetObserver)}
    122      *
    123      * @param observer the observer to unregister.
    124      */
    125     public void unregisterDataSetObserver(DataSetObserver observer) {
    126         mDataSetObservable.unregisterObserver(observer);
    127     }
    128 
    129     protected void notifyDataSetChanged() {
    130         mDataSetObservable.notifyChanged();
    131     }
    132 }
    133