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