AbstractSource.java revision 848fa7a19abedc372452073abaf52780c7b6d78d
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 boolean isVersionCodeCompatible(int version) {
58        return getVersionCode() == version;
59    }
60
61    public Drawable getIcon(String drawableId) {
62        return getIconLoader().getIcon(drawableId);
63    }
64
65    public Uri getIconUri(String drawableId) {
66        return getIconLoader().getIconUri(drawableId);
67    }
68
69    public Intent createSearchIntent(String query, Bundle appData) {
70        return createSourceSearchIntent(getIntentComponent(), query, appData);
71    }
72
73    public static Intent createSourceSearchIntent(ComponentName activity, String query,
74            Bundle appData) {
75        if (activity == null) {
76            Log.w(TAG, "Tried to create search intent with no target activity");
77            return null;
78        }
79        Intent intent = new Intent(Intent.ACTION_SEARCH);
80        intent.setComponent(activity);
81        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
82        // We need CLEAR_TOP to avoid reusing an old task that has other activities
83        // on top of the one we want.
84        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
85        intent.putExtra(SearchManager.USER_QUERY, query);
86        intent.putExtra(SearchManager.QUERY, query);
87        if (appData != null) {
88            intent.putExtra(SearchManager.APP_DATA, appData);
89        }
90        return intent;
91    }
92
93    protected Intent createVoiceWebSearchIntent(Bundle appData) {
94        return QsbApplication.get(mContext).getVoiceSearch()
95                .createVoiceWebSearchIntent(appData);
96    }
97
98    public Source getRoot() {
99        return this;
100    }
101
102    @Override
103    public boolean equals(Object o) {
104        if (o != null && o instanceof Source) {
105            Source s = ((Source) o).getRoot();
106            if (s.getClass().equals(this.getClass())) {
107                return s.getName().equals(getName());
108            }
109        }
110        return false;
111    }
112
113    @Override
114    public int hashCode() {
115        return getName().hashCode();
116    }
117
118    @Override
119    public String toString() {
120        return "Source{name=" + getName() + "}";
121    }
122
123}
124