2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.database;
20 * A convience class that lets you present an array of Cursors as a single linear Cursor.
21 * The schema of the cursors presented is entirely up to the creator of the MergeCursor, and
22 * may be different if that is desired. Calls to getColumns, getColumnIndex, etc will return the
23 * value for the row that the MergeCursor is currently pointing at.
25 public class MergeCursor extends AbstractCursor
27 private DataSetObserver mObserver = new DataSetObserver() {
30 public void onChanged() {
31 // Reset our position so the optimizations in move-related code
32 // don't screw us over
37 public void onInvalidated() {
42 public MergeCursor(Cursor[] cursors)
47 for (int i = 0; i < mCursors.length; i++) {
48 if (mCursors[i] == null) continue;
50 mCursors[i].registerDataSetObserver(mObserver);
58 int length = mCursors.length;
59 for (int i = 0 ; i < length ; i++) {
60 if (mCursors[i] != null) {
61 count += mCursors[i].getCount();
68 public boolean onMove(int oldPosition, int newPosition)
70 /* Find the right cursor */
72 int cursorStartPos = 0;
73 int length = mCursors.length;
74 for (int i = 0 ; i < length; i++) {
75 if (mCursors[i] == null) {
79 if (newPosition < (cursorStartPos + mCursors[i].getCount())) {
80 mCursor = mCursors[i];
84 cursorStartPos += mCursors[i].getCount();
87 /* Move it to the right position */
88 if (mCursor != null) {
89 boolean ret = mCursor.moveToPosition(newPosition - cursorStartPos);
100 public boolean deleteRow()
102 return mCursor.deleteRow();
110 public boolean commitUpdates() {
111 int length = mCursors.length;
112 for (int i = 0 ; i < length ; i++) {
113 if (mCursors[i] != null) {
114 mCursors[i].commitUpdates();
122 public String getString(int column)
124 return mCursor.getString(column);
128 public short getShort(int column)
130 return mCursor.getShort(column);
134 public int getInt(int column)
136 return mCursor.getInt(column);
140 public long getLong(int column)
142 return mCursor.getLong(column);
146 public float getFloat(int column)
148 return mCursor.getFloat(column);
152 public double getDouble(int column)
154 return mCursor.getDouble(column);
158 public boolean isNull(int column)
160 return mCursor.isNull(column);
164 public byte[] getBlob(int column)
166 return mCursor.getBlob(column);
170 public String[] getColumnNames()
172 if (mCursor != null) {
173 return mCursor.getColumnNames();
175 return new String[0];
180 public void deactivate()
182 int length = mCursors.length;
183 for (int i = 0 ; i < length ; i++) {
184 if (mCursors[i] != null) {
185 mCursors[i].deactivate();
192 public void close() {
193 int length = mCursors.length;
194 for (int i = 0 ; i < length ; i++) {
195 if (mCursors[i] == null) continue;
202 public void registerContentObserver(ContentObserver observer) {
203 int length = mCursors.length;
204 for (int i = 0 ; i < length ; i++) {
205 if (mCursors[i] != null) {
206 mCursors[i].registerContentObserver(observer);
211 public void unregisterContentObserver(ContentObserver observer) {
212 int length = mCursors.length;
213 for (int i = 0 ; i < length ; i++) {
214 if (mCursors[i] != null) {
215 mCursors[i].unregisterContentObserver(observer);
221 public void registerDataSetObserver(DataSetObserver observer) {
222 int length = mCursors.length;
223 for (int i = 0 ; i < length ; i++) {
224 if (mCursors[i] != null) {
225 mCursors[i].registerDataSetObserver(observer);
231 public void unregisterDataSetObserver(DataSetObserver observer) {
232 int length = mCursors.length;
233 for (int i = 0 ; i < length ; i++) {
234 if (mCursors[i] != null) {
235 mCursors[i].unregisterDataSetObserver(observer);
241 public boolean requery()
243 int length = mCursors.length;
244 for (int i = 0 ; i < length ; i++) {
245 if (mCursors[i] == null) {
249 if (mCursors[i].requery() == false) {
257 private Cursor mCursor; // updated in onMove
258 private Cursor[] mCursors;