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 com.android.contacts.calllog; 18 19 import com.android.common.io.MoreCloseables; 20 21 import android.database.AbstractCursor; 22 import android.database.ContentObserver; 23 import android.database.Cursor; 24 import android.database.DataSetObserver; 25 26 /** 27 * Wraps a cursor to add an additional column with the same value for all rows. 28 * <p> 29 * The number of rows in the cursor and the set of columns is determined by the cursor being 30 * wrapped. 31 */ 32 public class ExtendedCursor extends AbstractCursor { 33 /** The cursor to wrap. */ 34 private final Cursor mCursor; 35 /** The name of the additional column. */ 36 private final String mColumnName; 37 /** The value to be assigned to the additional column. */ 38 private final Object mValue; 39 40 /** 41 * Creates a new cursor which extends the given cursor by adding a column with a constant value. 42 * 43 * @param cursor the cursor to extend 44 * @param columnName the name of the additional column 45 * @param value the value to be assigned to the additional column 46 */ 47 public ExtendedCursor(Cursor cursor, String columnName, Object value) { 48 mCursor = cursor; 49 mColumnName = columnName; 50 mValue = value; 51 } 52 53 @Override 54 public int getCount() { 55 return mCursor.getCount(); 56 } 57 58 @Override 59 public String[] getColumnNames() { 60 String[] columnNames = mCursor.getColumnNames(); 61 int length = columnNames.length; 62 String[] extendedColumnNames = new String[length + 1]; 63 System.arraycopy(columnNames, 0, extendedColumnNames, 0, length); 64 extendedColumnNames[length] = mColumnName; 65 return extendedColumnNames; 66 } 67 68 @Override 69 public String getString(int column) { 70 if (column == mCursor.getColumnCount()) { 71 return (String) mValue; 72 } 73 return mCursor.getString(column); 74 } 75 76 @Override 77 public short getShort(int column) { 78 if (column == mCursor.getColumnCount()) { 79 return (Short) mValue; 80 } 81 return mCursor.getShort(column); 82 } 83 84 @Override 85 public int getInt(int column) { 86 if (column == mCursor.getColumnCount()) { 87 return (Integer) mValue; 88 } 89 return mCursor.getInt(column); 90 } 91 92 @Override 93 public long getLong(int column) { 94 if (column == mCursor.getColumnCount()) { 95 return (Long) mValue; 96 } 97 return mCursor.getLong(column); 98 } 99 100 @Override 101 public float getFloat(int column) { 102 if (column == mCursor.getColumnCount()) { 103 return (Float) mValue; 104 } 105 return mCursor.getFloat(column); 106 } 107 108 @Override 109 public double getDouble(int column) { 110 if (column == mCursor.getColumnCount()) { 111 return (Double) mValue; 112 } 113 return mCursor.getDouble(column); 114 } 115 116 @Override 117 public boolean isNull(int column) { 118 if (column == mCursor.getColumnCount()) { 119 return mValue == null; 120 } 121 return mCursor.isNull(column); 122 } 123 124 @Override 125 public boolean onMove(int oldPosition, int newPosition) { 126 return mCursor.moveToPosition(newPosition); 127 } 128 129 @Override 130 public void close() { 131 MoreCloseables.closeQuietly(mCursor); 132 super.close(); 133 } 134 135 @Override 136 public void registerContentObserver(ContentObserver observer) { 137 mCursor.registerContentObserver(observer); 138 } 139 140 @Override 141 public void unregisterContentObserver(ContentObserver observer) { 142 mCursor.unregisterContentObserver(observer); 143 } 144 145 @Override 146 public void registerDataSetObserver(DataSetObserver observer) { 147 mCursor.registerDataSetObserver(observer); 148 } 149 150 @Override 151 public void unregisterDataSetObserver(DataSetObserver observer) { 152 mCursor.unregisterDataSetObserver(observer); 153 } 154 } 155