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