1/* 2 * Copyright (C) 2010 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.util.NamedTaskExecutor; 20import com.android.quicksearchbox.util.NowOrLater; 21 22import android.app.SearchManager; 23import android.content.ComponentName; 24import android.content.Context; 25import android.content.Intent; 26import android.graphics.drawable.Drawable; 27import android.net.Uri; 28import android.os.Bundle; 29import android.os.Handler; 30import android.util.Log; 31 32/** 33 * Abstract suggestion source implementation. 34 */ 35public abstract class AbstractSource implements Source { 36 37 private static final String TAG = "QSB.AbstractSource"; 38 39 private final Context mContext; 40 private final Handler mUiThread; 41 42 private IconLoader mIconLoader; 43 44 private final NamedTaskExecutor mIconLoaderExecutor; 45 46 public AbstractSource(Context context, Handler uiThread, NamedTaskExecutor iconLoader) { 47 mContext = context; 48 mUiThread = uiThread; 49 mIconLoaderExecutor = iconLoader; 50 } 51 52 protected Context getContext() { 53 return mContext; 54 } 55 56 protected IconLoader getIconLoader() { 57 if (mIconLoader == null) { 58 String iconPackage = getIconPackage(); 59 mIconLoader = new CachingIconLoader( 60 new PackageIconLoader(mContext, iconPackage, mUiThread, mIconLoaderExecutor)); 61 } 62 return mIconLoader; 63 } 64 65 protected abstract String getIconPackage(); 66 67 @Override 68 public NowOrLater<Drawable> getIcon(String drawableId) { 69 return getIconLoader().getIcon(drawableId); 70 } 71 72 @Override 73 public Uri getIconUri(String drawableId) { 74 return getIconLoader().getIconUri(drawableId); 75 } 76 77 @Override 78 public Intent createSearchIntent(String query, Bundle appData) { 79 return createSourceSearchIntent(getIntentComponent(), query, appData); 80 } 81 82 public static Intent createSourceSearchIntent(ComponentName activity, String query, 83 Bundle appData) { 84 if (activity == null) { 85 Log.w(TAG, "Tried to create search intent with no target activity"); 86 return null; 87 } 88 Intent intent = new Intent(Intent.ACTION_SEARCH); 89 intent.setComponent(activity); 90 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 91 // We need CLEAR_TOP to avoid reusing an old task that has other activities 92 // on top of the one we want. 93 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 94 intent.putExtra(SearchManager.USER_QUERY, query); 95 intent.putExtra(SearchManager.QUERY, query); 96 if (appData != null) { 97 intent.putExtra(SearchManager.APP_DATA, appData); 98 } 99 return intent; 100 } 101 102 protected Intent createVoiceWebSearchIntent(Bundle appData) { 103 return QsbApplication.get(mContext).getVoiceSearch() 104 .createVoiceWebSearchIntent(appData); 105 } 106 107 @Override 108 public Source getRoot() { 109 return this; 110 } 111 112 @Override 113 public boolean equals(Object o) { 114 if (o != null && o instanceof Source) { 115 Source s = ((Source) o).getRoot(); 116 if (s.getClass().equals(this.getClass())) { 117 return s.getName().equals(getName()); 118 } 119 } 120 return false; 121 } 122 123 @Override 124 public int hashCode() { 125 return getName().hashCode(); 126 } 127 128 @Override 129 public String toString() { 130 return "Source{name=" + getName() + "}"; 131 } 132 133} 134