1/*
2 * Copyright (C) 2009 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
17package android.app;
18
19import android.app.SearchManager;
20import android.content.ContentProvider;
21import android.content.ContentValues;
22import android.content.Intent;
23import android.content.UriMatcher;
24import android.database.Cursor;
25import android.database.MatrixCursor;
26import android.net.Uri;
27
28/** Simple test provider that runs in the local process.
29 *
30 * Used by {@link SearchManagerTest}.
31 */
32public class SuggestionProvider extends ContentProvider {
33    private static final String TAG = "SuggestionProvider";
34
35    private static final int SEARCH_SUGGESTIONS = 1;
36
37    private static final UriMatcher sURLMatcher = new UriMatcher(
38            UriMatcher.NO_MATCH);
39
40    static {
41        sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY,
42                SEARCH_SUGGESTIONS);
43        sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
44                SEARCH_SUGGESTIONS);
45    }
46
47    private static final String[] COLUMNS = new String[] {
48            "_id",
49            SearchManager.SUGGEST_COLUMN_TEXT_1,
50            SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
51            SearchManager.SUGGEST_COLUMN_QUERY
52    };
53
54    public SuggestionProvider() {
55    }
56
57    @Override
58    public boolean onCreate() {
59        return true;
60    }
61
62    @Override
63    public Cursor query(Uri url, String[] projectionIn, String selection,
64            String[] selectionArgs, String sort) {
65        int match = sURLMatcher.match(url);
66        switch (match) {
67            case SEARCH_SUGGESTIONS:
68                String query = url.getLastPathSegment();
69                MatrixCursor cursor = new MatrixCursor(COLUMNS);
70                String[] suffixes = { "", "a", " foo", "XXXXXXXXXXXXXXXXX" };
71                for (String suffix : suffixes) {
72                    addRow(cursor, query + suffix);
73                }
74                return cursor;
75            default:
76                throw new IllegalArgumentException("Unknown URL: " + url);
77        }
78    }
79
80    private void addRow(MatrixCursor cursor, String string) {
81        long id = cursor.getCount();
82        cursor.newRow().add(id).add(string).add(Intent.ACTION_SEARCH).add(string);
83    }
84
85    @Override
86    public String getType(Uri url) {
87        int match = sURLMatcher.match(url);
88        switch (match) {
89            case SEARCH_SUGGESTIONS:
90                return SearchManager.SUGGEST_MIME_TYPE;
91            default:
92                throw new IllegalArgumentException("Unknown URL: " + url);
93        }
94    }
95
96    @Override
97    public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
98        throw new UnsupportedOperationException("update not supported");
99    }
100
101    @Override
102    public Uri insert(Uri url, ContentValues initialValues) {
103        throw new UnsupportedOperationException("insert not supported");
104    }
105
106    @Override
107    public int delete(Uri url, String where, String[] whereArgs) {
108        throw new UnsupportedOperationException("delete not supported");
109    }
110}
111