1 /* 2 * Copyright (C) 2006 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.common; 18 19 import android.database.AbstractCursor; 20 import android.database.CursorWindow; 21 22 import java.lang.System; 23 import java.util.ArrayList; 24 25 /** 26 * A convenience class that presents a two-dimensional ArrayList 27 * as a Cursor. 28 * @deprecated This is has been replaced by MatrixCursor. 29 */ 30 public class ArrayListCursor extends AbstractCursor { 31 private String[] mColumnNames; 32 private ArrayList<Object>[] mRows; 33 34 @SuppressWarnings({"unchecked"}) 35 public ArrayListCursor(String[] columnNames, ArrayList<ArrayList> rows) { 36 int colCount = columnNames.length; 37 boolean foundID = false; 38 // Add an _id column if not in columnNames 39 for (int i = 0; i < colCount; ++i) { 40 if (columnNames[i].compareToIgnoreCase("_id") == 0) { 41 mColumnNames = columnNames; 42 foundID = true; 43 break; 44 } 45 } 46 47 if (!foundID) { 48 mColumnNames = new String[colCount + 1]; 49 System.arraycopy(columnNames, 0, mColumnNames, 0, columnNames.length); 50 mColumnNames[colCount] = "_id"; 51 } 52 53 int rowCount = rows.size(); 54 mRows = new ArrayList[rowCount]; 55 56 for (int i = 0; i < rowCount; ++i) { 57 mRows[i] = rows.get(i); 58 if (!foundID) { 59 mRows[i].add(i); 60 } 61 } 62 } 63 64 @Override 65 public void fillWindow(int position, CursorWindow window) { 66 if (position < 0 || position > getCount()) { 67 return; 68 } 69 70 window.acquireReference(); 71 try { 72 int oldpos = mPos; 73 mPos = position - 1; 74 window.clear(); 75 window.setStartPosition(position); 76 int columnNum = getColumnCount(); 77 window.setNumColumns(columnNum); 78 while (moveToNext() && window.allocRow()) { 79 for (int i = 0; i < columnNum; i++) { 80 final Object data = mRows[mPos].get(i); 81 if (data != null) { 82 if (data instanceof byte[]) { 83 byte[] field = (byte[]) data; 84 if (!window.putBlob(field, mPos, i)) { 85 window.freeLastRow(); 86 break; 87 } 88 } else { 89 String field = data.toString(); 90 if (!window.putString(field, mPos, i)) { 91 window.freeLastRow(); 92 break; 93 } 94 } 95 } else { 96 if (!window.putNull(mPos, i)) { 97 window.freeLastRow(); 98 break; 99 } 100 } 101 } 102 } 103 104 mPos = oldpos; 105 } catch (IllegalStateException e){ 106 // simply ignore it 107 } finally { 108 window.releaseReference(); 109 } 110 } 111 112 @Override 113 public int getCount() { 114 return mRows.length; 115 } 116 117 @Override 118 public String[] getColumnNames() { 119 return mColumnNames; 120 } 121 122 @Override 123 public byte[] getBlob(int columnIndex) { 124 return (byte[]) mRows[mPos].get(columnIndex); 125 } 126 127 @Override 128 public String getString(int columnIndex) { 129 Object cell = mRows[mPos].get(columnIndex); 130 return (cell == null) ? null : cell.toString(); 131 } 132 133 @Override 134 public short getShort(int columnIndex) { 135 Number num = (Number) mRows[mPos].get(columnIndex); 136 return num.shortValue(); 137 } 138 139 @Override 140 public int getInt(int columnIndex) { 141 Number num = (Number) mRows[mPos].get(columnIndex); 142 return num.intValue(); 143 } 144 145 @Override 146 public long getLong(int columnIndex) { 147 Number num = (Number) mRows[mPos].get(columnIndex); 148 return num.longValue(); 149 } 150 151 @Override 152 public float getFloat(int columnIndex) { 153 Number num = (Number) mRows[mPos].get(columnIndex); 154 return num.floatValue(); 155 } 156 157 @Override 158 public double getDouble(int columnIndex) { 159 Number num = (Number) mRows[mPos].get(columnIndex); 160 return num.doubleValue(); 161 } 162 163 @Override 164 public boolean isNull(int columnIndex) { 165 return mRows[mPos].get(columnIndex) == null; 166 } 167 } 168