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