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 17package com.android.providers.contacts; 18 19import android.database.CrossProcessCursor; 20import android.database.Cursor; 21import android.database.CursorWindow; 22import android.database.CursorWrapper; 23 24/** 25 * Cursor wrapper that implements {@link CrossProcessCursor}, but will only behave as such if the 26 * cursor it is wrapping is itself a {@link CrossProcessCursor} or another wrapper around the same. 27 */ 28public class CrossProcessCursorWrapper extends CursorWrapper implements CrossProcessCursor { 29 30 // The cross process cursor. Only non-null if the wrapped cursor was a cross-process cursor. 31 private final CrossProcessCursor mCrossProcessCursor; 32 33 public CrossProcessCursorWrapper(Cursor cursor) { 34 super(cursor); 35 mCrossProcessCursor = getCrossProcessCursor(cursor); 36 } 37 38 private CrossProcessCursor getCrossProcessCursor(Cursor cursor) { 39 if (cursor instanceof CrossProcessCursor) { 40 return (CrossProcessCursor) cursor; 41 } else if (cursor instanceof CursorWrapper) { 42 return getCrossProcessCursor(((CursorWrapper) cursor).getWrappedCursor()); 43 } else { 44 return null; 45 } 46 } 47 48 @Override 49 public void fillWindow(int pos, CursorWindow window) { 50 if (mCrossProcessCursor != null) { 51 mCrossProcessCursor.fillWindow(pos, window); 52 } else { 53 throw new UnsupportedOperationException("Wrapped cursor is not a cross-process cursor"); 54 } 55 } 56 57 @Override 58 public CursorWindow getWindow() { 59 if (mCrossProcessCursor != null) { 60 return mCrossProcessCursor.getWindow(); 61 } else { 62 throw new UnsupportedOperationException("Wrapped cursor is not a cross-process cursor"); 63 } 64 } 65 66 @Override 67 public boolean onMove(int oldPosition, int newPosition) { 68 if (mCrossProcessCursor != null) { 69 return mCrossProcessCursor.onMove(oldPosition, newPosition); 70 } else { 71 throw new UnsupportedOperationException("Wrapped cursor is not a cross-process cursor"); 72 } 73 } 74 75} 76