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 android.app.SearchManager;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.graphics.drawable.Drawable;
     24 import android.net.Uri;
     25 import android.os.Bundle;
     26 import android.util.Log;
     27 
     28 /**
     29  * Abstract suggestion source implementation.
     30  */
     31 public 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     @Override
     99     public boolean equals(Object o) {
    100         if (o != null && o.getClass().equals(this.getClass())) {
    101             AbstractSource s = (AbstractSource) o;
    102             return s.getName().equals(getName());
    103         }
    104         return false;
    105     }
    106 
    107     @Override
    108     public int hashCode() {
    109         return getName().hashCode();
    110     }
    111 
    112     @Override
    113     public String toString() {
    114         return "Source{name=" + getName() + "}";
    115     }
    116 
    117 }
    118