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 int getVersionCode() { 64 return mVersionCode; 65 } 66 67 public boolean isVersionCodeCompatible(int version) { 68 return version == mVersionCode; 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 public Drawable getIcon(String drawableId) { 84 return null; 85 } 86 87 public Uri getIconUri(String drawableId) { 88 return null; 89 } 90 91 public String getLabel() { 92 return "MockSource " + mName; 93 } 94 95 public int getQueryThreshold() { 96 return 0; 97 } 98 99 public CharSequence getHint() { 100 return null; 101 } 102 103 public String getSettingsDescription() { 104 return "Suggestions from MockSource " + mName; 105 } 106 107 public Drawable getSourceIcon() { 108 return null; 109 } 110 111 public Uri getSourceIconUri() { 112 return null; 113 } 114 115 public boolean canRead() { 116 return true; 117 } 118 119 public boolean isLocationAware() { 120 return false; 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 cursor.add(createSuggestion(query + "_1")); 129 cursor.add(createSuggestion(query + "_2")); 130 return new Result(query, cursor); 131 } 132 133 public Suggestion createSuggestion(String query) { 134 Uri data = new Uri.Builder().scheme("content").authority(mName).path(query).build(); 135 return new SuggestionData(this) 136 .setText1(query) 137 .setIntentAction(Intent.ACTION_VIEW) 138 .setIntentData(data.toString()); 139 } 140 141 @Override 142 public boolean equals(Object o) { 143 if (o != null && o.getClass().equals(this.getClass())) { 144 MockSource s = (MockSource) o; 145 return s.mName.equals(mName); 146 } 147 return false; 148 } 149 150 @Override 151 public int hashCode() { 152 return mName.hashCode(); 153 } 154 155 @Override 156 public String toString() { 157 return getName() + ":" + getVersionCode(); 158 } 159 160 private class Result extends SuggestionCursorWrapper implements SourceResult { 161 162 public Result(String userQuery, SuggestionCursor cursor) { 163 super(userQuery, cursor); 164 } 165 166 public Source getSource() { 167 return MockSource.this; 168 } 169 170 } 171 172 public SuggestionCursor refreshShortcut(String shortcutId, String extraData) { 173 return null; 174 } 175 176 public boolean isExternal() { 177 return false; 178 } 179 180 public boolean isWebSuggestionSource() { 181 return false; 182 } 183 184 public boolean queryAfterZeroResults() { 185 return false; 186 } 187 188 public Intent createSearchIntent(String query, Bundle appData) { 189 return null; 190 } 191 192 public SuggestionData createSearchShortcut(String query) { 193 return null; 194 } 195 196 public Intent createVoiceSearchIntent(Bundle appData) { 197 return null; 198 } 199 200 public boolean voiceSearchEnabled() { 201 return false; 202 } 203 204} 205