1// Copyright 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.chrome.browser;
6
7import android.app.SearchManager;
8import android.content.Intent;
9import android.database.AbstractCursor;
10import android.database.Cursor;
11import android.provider.BaseColumns;
12import android.provider.Browser.BookmarkColumns;
13
14import org.chromium.chrome.R;
15
16/**
17 * For bookmarks/history suggestions, wrap the cursor returned in one that can feed
18 * the data back to global search in the format it wants.
19 */
20class ChromeBrowserProviderSuggestionsCursor extends AbstractCursor {
21
22    private static final String[] COLS = new String [] {
23        BaseColumns._ID,
24        SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
25        SearchManager.SUGGEST_COLUMN_INTENT_DATA,
26        SearchManager.SUGGEST_COLUMN_TEXT_1,
27        SearchManager.SUGGEST_COLUMN_TEXT_2,
28        SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
29        SearchManager.SUGGEST_COLUMN_ICON_1,
30        SearchManager.SUGGEST_COLUMN_LAST_ACCESS_HINT
31    };
32
33    private static final int COLUMN_ID = 0;
34    private static final int COLUMN_SUGGEST_INTENT_ACTION = 1;
35    private static final int COLUMN_SUGGEST_INTENT_DATA = 2;
36    private static final int COLUMN_SUGGEST_TEXT_1 = 3;
37    private static final int COLUMN_SUGGEST_TEXT_2 = 4;
38    private static final int COLUMN_SUGGEST_TEXT_2_URL = 5;
39    private static final int COLUMN_SUGGEST_ICON_1 = 6;
40    private static final int COLUMN_SUGGEST_LAST_ACCESS_HINT = 7;
41
42    private final Cursor mCursor;
43
44    public ChromeBrowserProviderSuggestionsCursor(Cursor c) {
45        mCursor = c;
46    }
47
48    @Override
49    public String[] getColumnNames() {
50        return COLS;
51    }
52
53    @Override
54    public int getCount() {
55        return mCursor.getCount();
56    }
57
58    @Override
59    public String getString(int column) {
60        switch (column) {
61            case COLUMN_ID:
62                return mCursor.getString(mCursor.getColumnIndex(BookmarkColumns._ID));
63            case COLUMN_SUGGEST_INTENT_ACTION:
64                return Intent.ACTION_VIEW;
65            case COLUMN_SUGGEST_INTENT_DATA:
66                return mCursor.getString(mCursor.getColumnIndex(BookmarkColumns.URL));
67            case COLUMN_SUGGEST_TEXT_1:
68                return mCursor.getString(mCursor.getColumnIndex(BookmarkColumns.TITLE));
69            case COLUMN_SUGGEST_TEXT_2:
70            case COLUMN_SUGGEST_TEXT_2_URL:
71                return mCursor.getString(mCursor.getColumnIndex(BookmarkColumns.URL));
72            case COLUMN_SUGGEST_ICON_1:
73                // This is the icon displayed to the left of the result in QSB.
74                return Integer.toString(R.mipmap.app_icon);
75            case COLUMN_SUGGEST_LAST_ACCESS_HINT:
76                // After clearing history, the Chrome bookmarks database will have a last access
77                // time of 0 for all bookmarks. In the Android provider, this will yield a negative
78                // last access time. A negative last access time will cause global search to discard
79                // the result, so fix it up before we return it.
80                long lastAccess = mCursor.getLong(
81                        mCursor.getColumnIndex(BookmarkColumns.DATE));
82                return lastAccess < 0 ? "0" : "" + lastAccess;
83            default:
84                throw new UnsupportedOperationException();
85        }
86    }
87
88    @Override
89    public boolean isNull(int c) {
90        return mCursor.isNull(c);
91    }
92
93    @Override
94    public long getLong(int c) {
95        switch (c) {
96            case 7:
97                // See comments above in getString() re. negative last access times.
98                long lastAccess = mCursor.getLong(
99                        mCursor.getColumnIndex(BookmarkColumns.DATE));
100                return lastAccess < 0 ? 0 : lastAccess;
101            default:
102                throw new UnsupportedOperationException();
103        }
104    }
105
106    @Override
107    public short getShort(int c) {
108        throw new UnsupportedOperationException();
109    }
110
111    @Override
112    public double getDouble(int c) {
113        throw new UnsupportedOperationException();
114    }
115
116    @Override
117    public int getInt(int c) {
118        throw new UnsupportedOperationException();
119    }
120
121    @Override
122    public float getFloat(int c) {
123        throw new UnsupportedOperationException();
124    }
125
126    @Override
127    public boolean onMove(int oldPosition, int newPosition) {
128        return mCursor.moveToPosition(newPosition);
129    }
130}
131