Home | History | Annotate | Download | only in database
      1 /*
      2  * Copyright (C) 2011 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 android.database;
     18 
     19 import android.database.CrossProcessCursor;
     20 import android.database.Cursor;
     21 import android.database.CursorWindow;
     22 import android.database.CursorWrapper;
     23 
     24 /**
     25  * Cursor wrapper that implements {@link CrossProcessCursor}.
     26  * <p>
     27  * If the wrapped cursor implements {@link CrossProcessCursor}, then the wrapper
     28  * delegates {@link #fillWindow}, {@link #getWindow()} and {@link #onMove} to it.
     29  * Otherwise, the wrapper provides default implementations of these methods that
     30  * traverse the contents of the cursor similar to {@link AbstractCursor#fillWindow}.
     31  * </p><p>
     32  * This wrapper can be used to adapt an ordinary {@link Cursor} into a
     33  * {@link CrossProcessCursor}.
     34  * </p>
     35  */
     36 public class CrossProcessCursorWrapper extends CursorWrapper implements CrossProcessCursor {
     37     /**
     38      * Creates a cross process cursor wrapper.
     39      * @param cursor The underlying cursor to wrap.
     40      */
     41     public CrossProcessCursorWrapper(Cursor cursor) {
     42         super(cursor);
     43     }
     44 
     45     @Override
     46     public void fillWindow(int position, CursorWindow window) {
     47         if (mCursor instanceof CrossProcessCursor) {
     48             final CrossProcessCursor crossProcessCursor = (CrossProcessCursor)mCursor;
     49             crossProcessCursor.fillWindow(position, window);
     50             return;
     51         }
     52 
     53         DatabaseUtils.cursorFillWindow(mCursor, position, window);
     54     }
     55 
     56     @Override
     57     public CursorWindow getWindow() {
     58         if (mCursor instanceof CrossProcessCursor) {
     59             final CrossProcessCursor crossProcessCursor = (CrossProcessCursor)mCursor;
     60             return crossProcessCursor.getWindow();
     61         }
     62 
     63         return null;
     64     }
     65 
     66     @Override
     67     public boolean onMove(int oldPosition, int newPosition) {
     68         if (mCursor instanceof CrossProcessCursor) {
     69             final CrossProcessCursor crossProcessCursor = (CrossProcessCursor)mCursor;
     70             return crossProcessCursor.onMove(oldPosition, newPosition);
     71         }
     72 
     73         return true;
     74     }
     75 }
     76