Home | History | Annotate | Download | only in quicksearchbox
      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 
     17 package com.android.quicksearchbox;
     18 
     19 import com.android.quicksearchbox.util.NamedTaskExecutor;
     20 import com.android.quicksearchbox.util.NowOrLater;
     21 
     22 import android.app.SearchManager;
     23 import android.content.ComponentName;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.graphics.drawable.Drawable;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.os.Handler;
     30 import android.util.Log;
     31 
     32 /**
     33  * Abstract suggestion source implementation.
     34  */
     35 public 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     public boolean isVersionCodeCompatible(int version) {
     68         return getVersionCode() == version;
     69     }
     70 
     71     public NowOrLater<Drawable> getIcon(String drawableId) {
     72         return getIconLoader().getIcon(drawableId);
     73     }
     74 
     75     public Uri getIconUri(String drawableId) {
     76         return getIconLoader().getIconUri(drawableId);
     77     }
     78 
     79     public Intent createSearchIntent(String query, Bundle appData) {
     80         return createSourceSearchIntent(getIntentComponent(), query, appData);
     81     }
     82 
     83     public static Intent createSourceSearchIntent(ComponentName activity, String query,
     84             Bundle appData) {
     85         if (activity == null) {
     86             Log.w(TAG, "Tried to create search intent with no target activity");
     87             return null;
     88         }
     89         Intent intent = new Intent(Intent.ACTION_SEARCH);
     90         intent.setComponent(activity);
     91         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     92         // We need CLEAR_TOP to avoid reusing an old task that has other activities
     93         // on top of the one we want.
     94         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     95         intent.putExtra(SearchManager.USER_QUERY, query);
     96         intent.putExtra(SearchManager.QUERY, query);
     97         if (appData != null) {
     98             intent.putExtra(SearchManager.APP_DATA, appData);
     99         }
    100         return intent;
    101     }
    102 
    103     protected Intent createVoiceWebSearchIntent(Bundle appData) {
    104         return QsbApplication.get(mContext).getVoiceSearch()
    105                 .createVoiceWebSearchIntent(appData);
    106     }
    107 
    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