MockSource.java revision 5229b06f00d20aac76cd8519b37f2a579d61c54f
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 com.android.quicksearchbox;
18
19import com.android.quicksearchbox.ui.SuggestionViewFactory;
20
21import android.content.ComponentName;
22import android.content.Intent;
23import android.graphics.drawable.Drawable;
24import android.net.Uri;
25import android.os.Bundle;
26
27/**
28 * Mock implementation of {@link Source}.
29 *
30 */
31public class MockSource implements Source {
32
33    public static final MockSource SOURCE_1 = new MockSource("SOURCE_1");
34
35    public static final MockSource SOURCE_2 = new MockSource("SOURCE_2");
36
37    public static final MockSource SOURCE_3 = new MockSource("SOURCE_3");
38
39    public static final MockSource WEB_SOURCE = new MockSource("WEB") {
40        @Override
41        public boolean isWebSuggestionSource() {
42            return true;
43        }
44    };
45
46    private final String mName;
47
48    private final int mVersionCode;
49
50    public MockSource(String name) {
51        this(name, 0);
52    }
53
54    public MockSource(String name, int versionCode) {
55        mName = name;
56        mVersionCode = versionCode;
57    }
58
59    public ComponentName getIntentComponent() {
60        // Not an activity, but no code should treat it as one.
61        return new ComponentName("com.android.quicksearchbox",
62                getClass().getName() + "." + mName);
63    }
64
65    public String getSuggestUri() {
66        return null;
67    }
68
69    public int getVersionCode() {
70        return mVersionCode;
71    }
72
73    public boolean isVersionCodeCompatible(int version) {
74        return version == mVersionCode;
75    }
76
77    public String getName() {
78        return getIntentComponent().flattenToShortString();
79    }
80
81    public String getDefaultIntentAction() {
82        return Intent.ACTION_SEARCH;
83    }
84
85    public String getDefaultIntentData() {
86        return null;
87    }
88
89    public Drawable getIcon(String drawableId) {
90        return null;
91    }
92
93    public Uri getIconUri(String drawableId) {
94        return null;
95    }
96
97    public String getLabel() {
98        return "MockSource " + mName;
99    }
100
101    public int getQueryThreshold() {
102        return 0;
103    }
104
105    public CharSequence getHint() {
106        return null;
107    }
108
109    public String getSettingsDescription() {
110        return "Suggestions from MockSource " + mName;
111    }
112
113    public Drawable getSourceIcon() {
114        return null;
115    }
116
117    public Uri getSourceIconUri() {
118        return null;
119    }
120
121    public boolean canRead() {
122        return true;
123    }
124
125    public SourceResult getSuggestions(String query, int queryLimit, boolean onlySource) {
126        if (query.length() == 0) {
127            return null;
128        }
129        ListSuggestionCursor cursor = new ListSuggestionCursor(query);
130        if (isWebSuggestionSource()) {
131            cursor.add(createWebSuggestion(query + "_1"));
132            cursor.add(createWebSuggestion(query + "_2"));
133        } else {
134            cursor.add(createSuggestion(query + "_1"));
135            cursor.add(createSuggestion(query + "_2"));
136        }
137        return new Result(query, cursor);
138    }
139
140    public SuggestionData createSuggestion(String query) {
141        Uri data = new Uri.Builder().scheme("content").authority(mName).path(query).build();
142        return new SuggestionData(this)
143                .setText1(query)
144                .setIntentAction(Intent.ACTION_VIEW)
145                .setIntentData(data.toString());
146    }
147
148    public SuggestionData createWebSuggestion(String query) {
149        return new SuggestionData(this)
150                .setText1(query)
151                .setIntentAction(Intent.ACTION_WEB_SEARCH)
152                .setSuggestionQuery(query);
153    }
154
155    @Override
156    public boolean equals(Object o) {
157        if (o != null && o.getClass().equals(this.getClass())) {
158            MockSource s = (MockSource) o;
159            return s.mName.equals(mName);
160        }
161        return false;
162    }
163
164    @Override
165    public int hashCode() {
166        return mName.hashCode();
167    }
168
169    @Override
170    public String toString() {
171        return getName() + ":" + getVersionCode();
172    }
173
174    private class Result extends SuggestionCursorWrapper implements SourceResult {
175
176        public Result(String userQuery, SuggestionCursor cursor) {
177            super(userQuery, cursor);
178        }
179
180        public Source getSource() {
181            return MockSource.this;
182        }
183
184    }
185
186    public SuggestionCursor refreshShortcut(String shortcutId, String extraData) {
187        return null;
188    }
189
190    public boolean isExternal() {
191        return false;
192    }
193
194    public boolean isWebSuggestionSource() {
195        return false;
196    }
197
198    public boolean queryAfterZeroResults() {
199        return false;
200    }
201
202    public Intent createSearchIntent(String query, Bundle appData) {
203        return null;
204    }
205
206    public SuggestionData createSearchShortcut(String query) {
207        return null;
208    }
209
210    public Intent createVoiceSearchIntent(Bundle appData) {
211        return null;
212    }
213
214    public boolean voiceSearchEnabled() {
215        return false;
216    }
217
218    public boolean includeInAll() {
219        return true;
220    }
221
222    public Source getRoot() {
223        return this;
224    }
225
226    @Override
227    public SuggestionViewFactory getSuggestionViewFactory() {
228        return null;
229    }
230
231}
232