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.database.Cursor;
19 import android.util.Log;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 
26 /**
27  * SuggestionExtras taking values from the extra columns in a suggestion cursor.
28  */
29 public 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 {
Arrays.asList(SuggestionCursorBackedCursor.COLUMNS)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 
createExtrasIfNecessary(Cursor cursor, int position)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 
getCursorColumns(Cursor cursor)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 
cursorContainsExtras(Cursor cursor)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 
getExtraColumns(Cursor cursor)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 
CursorBackedSuggestionExtras(Cursor cursor, int position, List<String> extraColumns)85     private CursorBackedSuggestionExtras(Cursor cursor, int position, List<String> extraColumns) {
86         super(null);
87         mCursor = cursor;
88         mCursorPosition = position;
89         mExtraColumns = extraColumns;
90     }
91 
92     @Override
doGetExtra(String columnName)93     public String doGetExtra(String columnName) {
94         try {
95             mCursor.moveToPosition(mCursorPosition);
96             int columnIdx = mCursor.getColumnIndex(columnName);
97             if (columnIdx < 0) return null;
98             return mCursor.getString(columnIdx);
99         } catch (RuntimeException ex) {
100             // all operations on cross-process cursors can throw random exceptions
101             Log.e(TAG, "getExtra(" + columnName + ") failed, ", ex);
102             return null;
103         }
104     }
105 
106     @Override
doGetExtraColumnNames()107     public List<String> doGetExtraColumnNames() {
108         return mExtraColumns;
109     }
110 
111 }
112