1 /* 2 * Copyright (C) 2023 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.app; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.annotation.SuppressLint; 22 import android.database.AbstractCursor; 23 import android.database.MatrixCursor; 24 25 /** 26 * Custom cursor implementation to hold a cross-process cursor to pass data to caller. 27 * 28 * @hide 29 */ 30 @SystemApi 31 public class StatsCursor extends AbstractCursor { 32 private final MatrixCursor mMatrixCursor; 33 private final int[] mColumnTypes; 34 private final String[] mColumnNames; 35 private final int mRowCount; 36 37 /** 38 * @hide 39 **/ StatsCursor(String[] queryData, String[] columnNames, int[] columnTypes, int rowCount)40 public StatsCursor(String[] queryData, String[] columnNames, int[] columnTypes, int rowCount) { 41 mColumnTypes = columnTypes; 42 mColumnNames = columnNames; 43 mRowCount = rowCount; 44 mMatrixCursor = new MatrixCursor(columnNames); 45 for (int i = 0; i < rowCount; i++) { 46 MatrixCursor.RowBuilder builder = mMatrixCursor.newRow(); 47 for (int j = 0; j < columnNames.length; j++) { 48 int dataIndex = i * columnNames.length + j; 49 builder.add(columnNames[j], queryData[dataIndex]); 50 } 51 } 52 } 53 54 /** 55 * Returns the numbers of rows in the cursor. 56 * 57 * @return the number of rows in the cursor. 58 */ 59 @Override getCount()60 public int getCount() { 61 return mRowCount; 62 } 63 64 /** 65 * Returns a string array holding the names of all of the columns in the 66 * result set in the order in which they were listed in the result. 67 * 68 * @return the names of the columns returned in this query. 69 */ 70 @Override 71 @NonNull getColumnNames()72 public String[] getColumnNames() { 73 return mColumnNames; 74 } 75 76 /** 77 * Returns the value of the requested column as a String. 78 * 79 * @param column the zero-based index of the target column. 80 * @return the value of that column as a String. 81 */ 82 @Override 83 @NonNull getString(int column)84 public String getString(int column) { 85 return mMatrixCursor.getString(column); 86 } 87 88 /** 89 * Returns the value of the requested column as a short. 90 * 91 * @param column the zero-based index of the target column. 92 * @return the value of that column as a short. 93 */ 94 @Override 95 @SuppressLint("NoByteOrShort") getShort(int column)96 public short getShort(int column) { 97 return mMatrixCursor.getShort(column); 98 } 99 100 /** 101 * Returns the value of the requested column as an int. 102 * 103 * @param column the zero-based index of the target column. 104 * @return the value of that column as an int. 105 */ 106 @Override getInt(int column)107 public int getInt(int column) { 108 return mMatrixCursor.getInt(column); 109 } 110 111 /** 112 * Returns the value of the requested column as a long. 113 * 114 * @param column the zero-based index of the target column. 115 * @return the value of that column as a long. 116 */ 117 @Override getLong(int column)118 public long getLong(int column) { 119 return mMatrixCursor.getLong(column); 120 } 121 122 /** 123 * Returns the value of the requested column as a float. 124 * 125 * @param column the zero-based index of the target column. 126 * @return the value of that column as a float. 127 */ 128 @Override getFloat(int column)129 public float getFloat(int column) { 130 return mMatrixCursor.getFloat(column); 131 } 132 133 /** 134 * Returns the value of the requested column as a double. 135 * 136 * @param column the zero-based index of the target column. 137 * @return the value of that column as a double. 138 */ 139 @Override getDouble(int column)140 public double getDouble(int column) { 141 return mMatrixCursor.getDouble(column); 142 } 143 144 /** 145 * Returns <code>true</code> if the value in the indicated column is null. 146 * 147 * @param column the zero-based index of the target column. 148 * @return whether the column value is null. 149 */ 150 @Override isNull(int column)151 public boolean isNull(int column) { 152 return mMatrixCursor.isNull(column); 153 } 154 155 /** 156 * Returns the data type of the given column's value. 157 * 158 * @param column the zero-based index of the target column. 159 * @return column value type 160 */ 161 @Override getType(int column)162 public int getType(int column) { 163 return mColumnTypes[column]; 164 } 165 166 /** 167 * {@inheritDoc} 168 */ 169 @Override onMove(int oldPosition, int newPosition)170 public boolean onMove(int oldPosition, int newPosition) { 171 return mMatrixCursor.moveToPosition(newPosition); 172 } 173 } 174