ListSuggestionCursor.java revision bf61e445cbe423cc2554b722b6dd38675015c36d
13e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/* 23e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Copyright (C) 2009 The Android Open Source Project 33e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * 43e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 53e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * you may not use this file except in compliance with the License. 63e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * You may obtain a copy of the License at 73e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * 83e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 93e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * 103e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 113e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 123e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * See the License for the specific language governing permissions and 143e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * limitations under the License. 153e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */ 163e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 173e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpackage com.android.quicksearchbox; 183e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 19fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringertimport android.database.DataSetObservable; 20fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringertimport android.database.DataSetObserver; 213e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 223e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertimport java.util.ArrayList; 233e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 243e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/** 253e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * A SuggestionCursor that is backed by a list of SuggestionPosition objects. 263e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * This cursor does not own the SuggestionCursors that the SuggestionPosition 273e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * objects refer to. 283e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * 293e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */ 3004a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringertpublic class ListSuggestionCursor extends AbstractSuggestionCursorWrapper { 313e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 32fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert private final DataSetObservable mDataSetObservable = new DataSetObservable(); 33fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert 343e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert private final ArrayList<SuggestionPosition> mSuggestions; 353e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 363e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert private int mPos; 373e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 383e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert public ListSuggestionCursor(String userQuery) { 393e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert super(userQuery); 403e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert mSuggestions = new ArrayList<SuggestionPosition>(); 413e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert mPos = 0; 423e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 433e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 443e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert /** 453e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Adds a suggestion from another suggestion cursor. 463e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * 473e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * @param suggestionPos 483e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * @return {@code true} if the suggestion was added. 493e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */ 503e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert public boolean add(SuggestionPosition suggestionPos) { 513e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert mSuggestions.add(suggestionPos); 523e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert return true; 533e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 543e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 553e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert public void close() { 563e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert mSuggestions.clear(); 573e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 583e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 593e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert public int getPosition() { 603e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert return mPos; 613e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 623e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 633e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert public void moveTo(int pos) { 643e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert mPos = pos; 653e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 663e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 6787e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert public boolean moveToNext() { 6887e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert int size = mSuggestions.size(); 6987e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert if (mPos >= size) { 7087e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert // Already past the end 7187e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert return false; 7287e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert } 7387e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert mPos++; 7487e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert return mPos < size; 7587e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert } 7687e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert 7794e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney public void removeRow() { 7894e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney mSuggestions.remove(mPos); 7994e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney } 8094e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney 8194e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney public void replaceRow(SuggestionPosition suggestionPos) { 8294e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney mSuggestions.set(mPos, suggestionPos); 8394e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney } 8494e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney 853e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert public int getCount() { 863e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert return mSuggestions.size(); 873e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 883e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 8904a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert @Override 90fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert protected SuggestionCursor current() { 91fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert return mSuggestions.get(mPos).current(); 923e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 933e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 94bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert @Override 95bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert public String toString() { 96bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert return "[" + getUserQuery() + "] " + mSuggestions; 97bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert } 98bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert 99fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert /** 100fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * Register an observer that is called when changes happen to this data set. 101fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * 102fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * @param observer gets notified when the data set changes. 103fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert */ 104fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert public void registerDataSetObserver(DataSetObserver observer) { 105fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert mDataSetObservable.registerObserver(observer); 106ca78085bb2127559e6f55276a307bfa857018ecaBjorn Bringert } 107ca78085bb2127559e6f55276a307bfa857018ecaBjorn Bringert 108fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert /** 109fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * Unregister an observer that has previously been registered with 110fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * {@link #registerDataSetObserver(DataSetObserver)} 111fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * 112fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * @param observer the observer to unregister. 113fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert */ 114fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert public void unregisterDataSetObserver(DataSetObserver observer) { 115fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert mDataSetObservable.unregisterObserver(observer); 1163e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 1173e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert 118fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert protected void notifyDataSetChanged() { 119fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert mDataSetObservable.notifyChanged(); 1203e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert } 1213e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert} 122