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.ui.CorporaAdapter;
     20 import com.android.quicksearchbox.ui.CorpusViewFactory;
     21 
     22 import android.appwidget.AppWidgetManager;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.SharedPreferences;
     26 import android.os.Bundle;
     27 import android.view.View;
     28 import android.widget.AdapterView;
     29 import android.widget.ListAdapter;
     30 
     31 /**
     32  * The configuration screen for search widgets.
     33  */
     34 public class SearchWidgetConfigActivity extends ChoiceActivity {
     35     static final String TAG = "QSB.SearchWidgetConfigActivity";
     36 
     37     private static final String PREFS_NAME = "SearchWidgetConfig";
     38     private static final String WIDGET_CORPUS_PREF_PREFIX = "widget_corpus_";
     39 
     40     private CorporaAdapter mAdapter;
     41 
     42     private int mAppWidgetId;
     43 
     44     @Override
     45     public void onCreate(Bundle icicle) {
     46         super.onCreate(icicle);
     47 
     48         setHeading(R.string.search_widget);
     49         setOnItemClickListener(new SourceClickListener());
     50 
     51         Intent intent = getIntent();
     52         mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
     53                 AppWidgetManager.INVALID_APPWIDGET_ID);
     54         if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
     55             finish();
     56         }
     57     }
     58 
     59     @Override
     60     protected void onStart() {
     61         setAdapter(CorporaAdapter.createListAdapter(getViewFactory(), getCorpusRanker()));
     62         super.onStart();
     63     }
     64 
     65     @Override
     66     protected void onStop() {
     67         setAdapter(null);
     68         super.onStop();
     69     }
     70 
     71     @Override
     72     public void setAdapter(ListAdapter adapter) {
     73         if (adapter == mAdapter) return;
     74         if (mAdapter != null) mAdapter.close();
     75         mAdapter = (CorporaAdapter) adapter;
     76         super.setAdapter(adapter);
     77     }
     78 
     79     protected void selectCorpus(Corpus corpus) {
     80         writeWidgetCorpusPref(mAppWidgetId, corpus);
     81         updateWidget(corpus);
     82 
     83         Intent result = new Intent();
     84         result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
     85         setResult(RESULT_OK, result);
     86         finish();
     87     }
     88 
     89     private void updateWidget(Corpus corpus) {
     90         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
     91         SearchWidgetProvider.setupSearchWidget(this, appWidgetManager,
     92                 mAppWidgetId, corpus);
     93     }
     94 
     95     private static SharedPreferences getWidgetPreferences(Context context) {
     96         return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
     97     }
     98 
     99     private static String getCorpusPrefKey(int appWidgetId) {
    100         return WIDGET_CORPUS_PREF_PREFIX + appWidgetId;
    101     }
    102 
    103     private void writeWidgetCorpusPref(int appWidgetId, Corpus corpus) {
    104         String corpusName = corpus == null ? null : corpus.getName();
    105         SharedPreferences.Editor prefs = getWidgetPreferences(this).edit();
    106         prefs.putString(getCorpusPrefKey(appWidgetId), corpusName);
    107         prefs.commit();
    108     }
    109 
    110     public static String readWidgetCorpusPref(Context context, int appWidgetId) {
    111         SharedPreferences prefs = getWidgetPreferences(context);
    112         return prefs.getString(getCorpusPrefKey(appWidgetId), null);
    113     }
    114 
    115     private QsbApplication getQsbApplication() {
    116         return (QsbApplication) getApplication();
    117     }
    118 
    119     private CorpusRanker getCorpusRanker() {
    120         return getQsbApplication().getCorpusRanker();
    121     }
    122 
    123     private CorpusViewFactory getViewFactory() {
    124         return getQsbApplication().getCorpusViewFactory();
    125     }
    126 
    127     private class SourceClickListener implements AdapterView.OnItemClickListener {
    128         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    129             Corpus corpus = (Corpus) parent.getItemAtPosition(position);
    130             selectCorpus(corpus);
    131         }
    132     }
    133 }
    134