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