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 */ 16 package com.android.quicksearchbox; 17 18 import android.app.SearchManager; 19 import android.database.AbstractCursor; 20 import android.database.CursorIndexOutOfBoundsException; 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.Collection; 25 26 public class SuggestionCursorBackedCursor extends AbstractCursor { 27 28 // This array also used in CursorBackedSuggestionExtras to avoid duplication. 29 public static final String[] COLUMNS = { 30 "_id", // 0, This will contain the row number. CursorAdapter, used by SuggestionsAdapter, 31 // used by SearchDialog, expects an _id column. 32 SearchManager.SUGGEST_COLUMN_TEXT_1, // 1 33 SearchManager.SUGGEST_COLUMN_TEXT_2, // 2 34 SearchManager.SUGGEST_COLUMN_TEXT_2_URL, // 3 35 SearchManager.SUGGEST_COLUMN_ICON_1, // 4 36 SearchManager.SUGGEST_COLUMN_ICON_2, // 5 37 SearchManager.SUGGEST_COLUMN_INTENT_ACTION, // 6 38 SearchManager.SUGGEST_COLUMN_INTENT_DATA, // 7 39 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA, // 8 40 SearchManager.SUGGEST_COLUMN_QUERY, // 9 41 SearchManager.SUGGEST_COLUMN_FORMAT, // 10 42 SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, // 11 43 SearchManager.SUGGEST_COLUMN_SPINNER_WHILE_REFRESHING, // 12 44 }; 45 46 private static final int COLUMN_INDEX_ID = 0; 47 private static final int COLUMN_INDEX_TEXT1 = 1; 48 private static final int COLUMN_INDEX_TEXT2 = 2; 49 private static final int COLUMN_INDEX_TEXT2_URL = 3; 50 private static final int COLUMN_INDEX_ICON1 = 4; 51 private static final int COLUMN_INDEX_ICON2 = 5; 52 private static final int COLUMN_INDEX_INTENT_ACTION = 6; 53 private static final int COLUMN_INDEX_INTENT_DATA = 7; 54 private static final int COLUMN_INDEX_INTENT_EXTRA_DATA = 8; 55 private static final int COLUMN_INDEX_QUERY = 9; 56 private static final int COLUMN_INDEX_FORMAT = 10; 57 private static final int COLUMN_INDEX_SHORTCUT_ID = 11; 58 private static final int COLUMN_INDEX_SPINNER_WHILE_REFRESHING = 12; 59 60 private final SuggestionCursor mCursor; 61 private ArrayList<String> mExtraColumns; 62 SuggestionCursorBackedCursor(SuggestionCursor cursor)63 public SuggestionCursorBackedCursor(SuggestionCursor cursor) { 64 mCursor = cursor; 65 } 66 67 @Override close()68 public void close() { 69 super.close(); 70 mCursor.close(); 71 } 72 73 @Override getColumnNames()74 public String[] getColumnNames() { 75 Collection<String> extraColumns = mCursor.getExtraColumns(); 76 if (extraColumns != null) { 77 ArrayList<String> allColumns = new ArrayList<String>(COLUMNS.length + 78 extraColumns.size()); 79 mExtraColumns = new ArrayList<String>(extraColumns); 80 allColumns.addAll(Arrays.asList(COLUMNS)); 81 allColumns.addAll(mExtraColumns); 82 return allColumns.toArray(new String[allColumns.size()]); 83 } else { 84 return COLUMNS; 85 } 86 } 87 88 @Override getCount()89 public int getCount() { 90 return mCursor.getCount(); 91 } 92 get()93 private Suggestion get() { 94 mCursor.moveTo(getPosition()); 95 return mCursor; 96 } 97 getExtra(int columnIdx)98 private String getExtra(int columnIdx) { 99 int extraColumn = columnIdx - COLUMNS.length; 100 SuggestionExtras extras = get().getExtras(); 101 if (extras != null) { 102 return extras.getExtra(mExtraColumns.get(extraColumn)); 103 } else { 104 return null; 105 } 106 } 107 108 @Override getInt(int column)109 public int getInt(int column) { 110 if (column == COLUMN_INDEX_ID) { 111 return getPosition(); 112 } else { 113 try { 114 return Integer.valueOf(getString(column)); 115 } catch (NumberFormatException e) { 116 return 0; 117 } 118 } 119 } 120 121 @Override getString(int column)122 public String getString(int column) { 123 if (column < COLUMNS.length) { 124 switch (column) { 125 case COLUMN_INDEX_ID: 126 return String.valueOf(getPosition()); 127 case COLUMN_INDEX_TEXT1: 128 return get().getSuggestionText1(); 129 case COLUMN_INDEX_TEXT2: 130 return get().getSuggestionText2(); 131 case COLUMN_INDEX_TEXT2_URL: 132 return get().getSuggestionText2Url(); 133 case COLUMN_INDEX_ICON1: 134 return get().getSuggestionIcon1(); 135 case COLUMN_INDEX_ICON2: 136 return get().getSuggestionIcon2(); 137 case COLUMN_INDEX_INTENT_ACTION: 138 return get().getSuggestionIntentAction(); 139 case COLUMN_INDEX_INTENT_DATA: 140 return get().getSuggestionIntentDataString(); 141 case COLUMN_INDEX_INTENT_EXTRA_DATA: 142 return get().getSuggestionIntentExtraData(); 143 case COLUMN_INDEX_QUERY: 144 return get().getSuggestionQuery(); 145 case COLUMN_INDEX_FORMAT: 146 return get().getSuggestionFormat(); 147 case COLUMN_INDEX_SHORTCUT_ID: 148 return get().getShortcutId(); 149 case COLUMN_INDEX_SPINNER_WHILE_REFRESHING: 150 return String.valueOf(get().isSpinnerWhileRefreshing()); 151 default: 152 throw new CursorIndexOutOfBoundsException("Requested column " + column 153 + " of " + COLUMNS.length); 154 } 155 } else { 156 return getExtra(column); 157 } 158 } 159 160 @Override getLong(int column)161 public long getLong(int column) { 162 try { 163 return Long.valueOf(getString(column)); 164 } catch (NumberFormatException e) { 165 return 0; 166 } 167 } 168 169 @Override isNull(int column)170 public boolean isNull(int column) { 171 return getString(column) == null; 172 } 173 174 @Override getShort(int column)175 public short getShort(int column) { 176 try { 177 return Short.valueOf(getString(column)); 178 } catch (NumberFormatException e) { 179 return 0; 180 } 181 } 182 183 @Override getDouble(int column)184 public double getDouble(int column) { 185 try { 186 return Double.valueOf(getString(column)); 187 } catch (NumberFormatException e) { 188 return 0; 189 } 190 } 191 192 @Override getFloat(int column)193 public float getFloat(int column) { 194 try { 195 return Float.valueOf(getString(column)); 196 } catch (NumberFormatException e) { 197 return 0; 198 } 199 } 200 } 201