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