1// Copyright (c) 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 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
77            // access time of 0 for all bookmarks. In the Android provider, this will
78            // yield a negative last access time. A negative last access time will
79            // cause global search to discard the result, so fix it up before
80            // we return it.
81            long lastAccess = mCursor.getLong(
82                    mCursor.getColumnIndex(BookmarkColumns.DATE));
83            return lastAccess < 0 ? "0" : "" + lastAccess;
84        default:
85            throw new UnsupportedOperationException();
86        }
87    }
88
89    @Override
90    public boolean isNull(int c) {
91        return mCursor.isNull(c);
92    }
93
94    @Override
95    public long getLong(int c) {
96        switch (c) {
97            case 7:
98                // See comments above in getString() re. negative last access times.
99                long lastAccess = mCursor.getLong(
100                        mCursor.getColumnIndex(BookmarkColumns.DATE));
101                return lastAccess < 0 ? 0 : lastAccess;
102            default:
103                throw new UnsupportedOperationException();
104        }
105    }
106
107    @Override
108    public short getShort(int c) {
109        throw new UnsupportedOperationException();
110    }
111
112    @Override
113    public double getDouble(int c) {
114        throw new UnsupportedOperationException();
115    }
116
117    @Override
118    public int getInt(int c) {
119        throw new UnsupportedOperationException();
120    }
121
122    @Override
123    public float getFloat(int c) {
124        throw new UnsupportedOperationException();
125    }
126
127    @Override
128    public boolean onMove(int oldPosition, int newPosition) {
129        return mCursor.moveToPosition(newPosition);
130    }
131}
132