CursorBackedSuggestionExtras.java revision 5229b06f00d20aac76cd8519b37f2a579d61c54f
1/* 2 * Copyright (C) 2010 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 */ 16package com.android.quicksearchbox; 17 18import android.database.Cursor; 19import android.util.Log; 20 21import java.util.ArrayList; 22import java.util.Arrays; 23import java.util.HashSet; 24import java.util.List; 25 26/** 27 * SuggestionExtras taking values from the extra columns in a suggestion cursor. 28 */ 29public class CursorBackedSuggestionExtras extends AbstractSuggestionExtras { 30 private static final String TAG = "QSB.CursorBackedSuggestionExtras"; 31 32 private static final HashSet<String> DEFAULT_COLUMNS = new HashSet<String>(); 33 static { 34 DEFAULT_COLUMNS.addAll(Arrays.asList(SuggestionCursorBackedCursor.COLUMNS)); 35 } 36 37 private final Cursor mCursor; 38 private final int mCursorPosition; 39 private final List<String> mExtraColumns; 40 41 static CursorBackedSuggestionExtras createExtrasIfNecessary(Cursor cursor, int position) { 42 List<String> extraColumns = getExtraColumns(cursor); 43 if (extraColumns != null) { 44 return new CursorBackedSuggestionExtras(cursor, position, extraColumns); 45 } else { 46 return null; 47 } 48 } 49 50 static String[] getCursorColumns(Cursor cursor) { 51 try { 52 return cursor.getColumnNames(); 53 } catch (RuntimeException ex) { 54 // all operations on cross-process cursors can throw random exceptions 55 Log.e(TAG, "getColumnNames() failed, ", ex); 56 return null; 57 } 58 } 59 60 static boolean cursorContainsExtras(Cursor cursor) { 61 String[] columns = getCursorColumns(cursor); 62 for (String cursorColumn : columns) { 63 if (!DEFAULT_COLUMNS.contains(cursorColumn)) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 static List<String> getExtraColumns(Cursor cursor) { 71 String[] columns = getCursorColumns(cursor); 72 if (columns == null) return null; 73 List<String> extraColumns = null; 74 for (String cursorColumn : columns) { 75 if (!DEFAULT_COLUMNS.contains(cursorColumn)) { 76 if (extraColumns == null) { 77 extraColumns = new ArrayList<String>(); 78 } 79 extraColumns.add(cursorColumn); 80 } 81 } 82 return extraColumns; 83 } 84 85 private CursorBackedSuggestionExtras(Cursor cursor, int position, List<String> extraColumns) { 86 mCursor = cursor; 87 mCursorPosition = position; 88 mExtraColumns = extraColumns; 89 } 90 91 public String getExtra(String columnName) { 92 try { 93 mCursor.moveToPosition(mCursorPosition); 94 int columnIdx = mCursor.getColumnIndex(columnName); 95 if (columnIdx < 0) return null; 96 return mCursor.getString(columnIdx); 97 } catch (RuntimeException ex) { 98 // all operations on cross-process cursors can throw random exceptions 99 Log.e(TAG, "getExtra(" + columnName + ") failed, ", ex); 100 return null; 101 } 102 } 103 104 public List<String> getExtraColumnNames() { 105 return mExtraColumns; 106 } 107 108} 109