ListSuggestionCursor.java revision 9038d65a5a8ebcfada1ec3067f81a26f05622088
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;
21import android.util.Log;
22
23import java.util.ArrayList;
24
25/**
26 * A SuggestionCursor that is backed by a list of Suggestions.
27 */
28public class ListSuggestionCursor extends AbstractSuggestionCursorWrapper {
29
30    private static final int DEFAULT_CAPACITY = 16;
31
32    private final DataSetObservable mDataSetObservable = new DataSetObservable();
33
34    private final ArrayList<Suggestion> mSuggestions;
35
36    private int mPos = 0;
37
38    public ListSuggestionCursor(String userQuery) {
39        this(userQuery, DEFAULT_CAPACITY);
40    }
41
42    public ListSuggestionCursor(String userQuery, Suggestion...suggestions) {
43        this(userQuery, suggestions.length);
44        for (Suggestion suggestion : suggestions) {
45            mSuggestions.add(suggestion);
46        }
47    }
48
49    public ListSuggestionCursor(String userQuery, int capacity) {
50        super(userQuery);
51        mSuggestions = new ArrayList<Suggestion>(capacity);
52    }
53
54    /**
55     * Adds a suggestion from another suggestion cursor.
56     *
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 getClass().getSimpleName() + "{[" + 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