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 
17 package com.android.quicksearchbox;
18 
19 import android.content.ComponentName;
20 import android.content.Intent;
21 import android.graphics.drawable.Drawable;
22 import android.net.Uri;
23 import android.os.Bundle;
24 
25 import com.android.quicksearchbox.util.Now;
26 import com.android.quicksearchbox.util.NowOrLater;
27 
28 /**
29  * Mock implementation of {@link Source}.
30  *
31  */
32 public 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 
MockSource(String name)53     public MockSource(String name) {
54         this(name, 0);
55     }
56 
MockSource(String name, int versionCode)57     public MockSource(String name, int versionCode) {
58         mName = name;
59     }
60 
getIntentComponent()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 
getSuggestUri()67     public String getSuggestUri() {
68         return null;
69     }
70 
getName()71     public String getName() {
72         return getIntentComponent().flattenToShortString();
73     }
74 
getDefaultIntentAction()75     public String getDefaultIntentAction() {
76         return Intent.ACTION_SEARCH;
77     }
78 
getDefaultIntentData()79     public String getDefaultIntentData() {
80         return null;
81     }
82 
83     @Override
getIcon(String drawableId)84     public NowOrLater<Drawable> getIcon(String drawableId) {
85         return new Now<Drawable>(null);
86     }
87 
getIconUri(String drawableId)88     public Uri getIconUri(String drawableId) {
89         return null;
90     }
91 
getLabel()92     public String getLabel() {
93         return "MockSource " + mName;
94     }
95 
getQueryThreshold()96     public int getQueryThreshold() {
97         return 0;
98     }
99 
getHint()100     public CharSequence getHint() {
101         return null;
102     }
103 
getSettingsDescription()104     public String getSettingsDescription() {
105         return "Suggestions from MockSource " + mName;
106     }
107 
getSourceIcon()108     public Drawable getSourceIcon() {
109         return null;
110     }
111 
getSourceIconUri()112     public Uri getSourceIconUri() {
113         return null;
114     }
115 
canRead()116     public boolean canRead() {
117         return true;
118     }
119 
getSuggestions(String query, int queryLimit)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 
createSuggestion(String query)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
equals(Object o)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
hashCode()148     public int hashCode() {
149         return mName.hashCode();
150     }
151 
152     @Override
toString()153     public String toString() {
154         return getName();
155     }
156 
157     private class Result extends SuggestionCursorWrapper implements SourceResult {
158 
Result(String userQuery, SuggestionCursor cursor)159         public Result(String userQuery, SuggestionCursor cursor) {
160             super(userQuery, cursor);
161         }
162 
getSource()163         public Source getSource() {
164             return MockSource.this;
165         }
166 
167     }
168 
queryAfterZeroResults()169     public boolean queryAfterZeroResults() {
170         return false;
171     }
172 
createSearchIntent(String query, Bundle appData)173     public Intent createSearchIntent(String query, Bundle appData) {
174         return null;
175     }
176 
createVoiceSearchIntent(Bundle appData)177     public Intent createVoiceSearchIntent(Bundle appData) {
178         return null;
179     }
180 
voiceSearchEnabled()181     public boolean voiceSearchEnabled() {
182         return false;
183     }
184 
includeInAll()185     public boolean includeInAll() {
186         return true;
187     }
188 
getRoot()189     public Source getRoot() {
190         return this;
191     }
192 
193 }
194