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