AppsCorpus.java revision 3f00b91235290a95a8ff9394a3612935d602ef20
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
19
20import com.android.quicksearchbox.util.Util;
21
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.graphics.drawable.Drawable;
27import android.net.Uri;
28import android.os.Bundle;
29import android.text.TextUtils;
30import android.util.Log;
31
32/**
33 * The apps search source.
34 */
35public class AppsCorpus extends SingleSourceCorpus {
36
37    private static final String TAG = "QSB.AppsCorpus";
38
39    private static final String APPS_CORPUS_NAME = "apps";
40
41    public AppsCorpus(Context context, Source appsSource) {
42        super(context, appsSource);
43    }
44
45    @Override
46    public CharSequence getLabel() {
47        return getContext().getText(R.string.corpus_label_apps);
48    }
49
50    @Override
51    public CharSequence getHint() {
52        return getContext().getText(R.string.corpus_hint_apps);
53    }
54
55    @Override
56    public Drawable getCorpusIcon() {
57        // TODO: Should we have a different icon for the apps corpus?
58        return getContext().getResources().getDrawable(android.R.drawable.sym_def_app_icon);
59    }
60
61    @Override
62    public Uri getCorpusIconUri() {
63        return Util.getResourceUri(getContext(), android.R.drawable.sym_def_app_icon);
64    }
65
66    @Override
67    public String getName() {
68        return APPS_CORPUS_NAME;
69    }
70
71    @Override
72    public CharSequence getSettingsDescription() {
73        return getContext().getText(R.string.corpus_description_apps);
74    }
75
76    @Override
77    public Intent createSearchIntent(String query, Bundle appData) {
78        Intent appSearchIntent = createAppSearchIntent(query, appData);
79        if (appSearchIntent != null) {
80            return appSearchIntent;
81        } else {
82            // Fall back to sending the intent to ApplicationsProvider
83            return super.createSearchIntent(query, appData);
84        }
85    }
86
87    /**
88     * Creates an intent that starts the search activity specified in
89     * R.string.apps_search_activity.
90     *
91     * @return An intent, or {@code null} if the search activity is not set or can't be found.
92     */
93    private Intent createAppSearchIntent(String query, Bundle appData) {
94        ComponentName name = getComponentName(getContext(), R.string.apps_search_activity);
95        if (name == null) return null;
96        Intent intent = SearchableSource.createSourceSearchIntent(name, query, appData);
97        if (intent == null) return null;
98        ActivityInfo ai = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
99        if (ai != null) {
100            return intent;
101        } else {
102            Log.w(TAG, "Can't find app search acitivity " + name);
103            return null;
104        }
105    }
106
107    private static ComponentName getComponentName(Context context, int res) {
108        String nameStr = context.getString(res);
109        if (TextUtils.isEmpty(nameStr)) {
110            return null;
111        } else {
112            return ComponentName.unflattenFromString(nameStr);
113        }
114    }
115}
116