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