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 com.android.quicksearchbox.ui.CorporaAdapter;
20import com.android.quicksearchbox.ui.CorpusViewFactory;
21
22import android.appwidget.AppWidgetManager;
23import android.content.Context;
24import android.content.Intent;
25import android.content.SharedPreferences;
26import android.os.Bundle;
27import android.util.Log;
28import android.view.View;
29import android.widget.AdapterView;
30import android.widget.ListAdapter;
31
32/**
33 * The configuration screen for search widgets.
34 */
35public class SearchWidgetConfigActivity extends ChoiceActivity {
36    private static final boolean DBG = false;
37    private static final String TAG = "QSB.SearchWidgetConfigActivity";
38
39    private static final String PREFS_NAME = "SearchWidgetConfig";
40    private static final String WIDGET_CORPUS_NAME_PREFIX = "widget_corpus_";
41    private static final String WIDGET_CORPUS_SHOWING_HINT_PREFIX = "widget_showing_hint_";
42
43    private CorporaAdapter mAdapter;
44
45    private int mAppWidgetId;
46
47    @Override
48    public void onCreate(Bundle icicle) {
49        super.onCreate(icicle);
50
51        setHeading(R.string.search_widget);
52        setOnItemClickListener(new SourceClickListener());
53
54        Intent intent = getIntent();
55        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
56                AppWidgetManager.INVALID_APPWIDGET_ID);
57        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
58            finish();
59        }
60
61        // If there is only All, or only All and one other corpus, there is no
62        // point in asking the user to select a corpus.
63        if (getCorpusRanker().getRankedCorpora().size() <= 1) {
64            selectCorpus(null);
65        }
66    }
67
68    @Override
69    protected void onStart() {
70        setAdapter(CorporaAdapter.createListAdapter(getViewFactory(), getCorpusRanker()));
71        super.onStart();
72    }
73
74    @Override
75    protected void onStop() {
76        setAdapter(null);
77        super.onStop();
78    }
79
80    @Override
81    public void setAdapter(ListAdapter adapter) {
82        if (adapter == mAdapter) return;
83        if (mAdapter != null) mAdapter.close();
84        mAdapter = (CorporaAdapter) adapter;
85        super.setAdapter(adapter);
86    }
87
88    protected void selectCorpus(Corpus corpus) {
89        setWidgetCorpusName(mAppWidgetId, corpus);
90        SearchWidgetProvider.updateSearchWidgets(this);
91
92        Intent result = new Intent();
93        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
94        setResult(RESULT_OK, result);
95        finish();
96    }
97
98    private static SharedPreferences getWidgetPreferences(Context context) {
99        return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
100    }
101
102    private static String getCorpusNameKey(int appWidgetId) {
103        return WIDGET_CORPUS_NAME_PREFIX + appWidgetId;
104    }
105
106    private static String getShowingHintKey(int appWidgetId) {
107        return WIDGET_CORPUS_SHOWING_HINT_PREFIX + appWidgetId;
108    }
109
110    private void setWidgetCorpusName(int appWidgetId, Corpus corpus) {
111        String corpusName = corpus == null ? null : corpus.getName();
112        SharedPreferences.Editor prefs = getWidgetPreferences(this).edit();
113        prefs.putString(getCorpusNameKey(appWidgetId), corpusName);
114        prefs.commit();
115    }
116
117    public static String getWidgetCorpusName(Context context, int appWidgetId) {
118        SharedPreferences prefs = getWidgetPreferences(context);
119        return prefs.getString(getCorpusNameKey(appWidgetId), null);
120    }
121
122    public static void setWidgetShowingHint(Context context, int appWidgetId, boolean showing) {
123        SharedPreferences.Editor prefs = getWidgetPreferences(context).edit();
124        prefs.putBoolean(getShowingHintKey(appWidgetId), showing);
125        boolean c = prefs.commit();
126        if (DBG) Log.d(TAG, "Widget " + appWidgetId + " set showing hint " + showing + "("+c+")");
127    }
128
129    public static boolean getWidgetShowingHint(Context context, int appWidgetId) {
130        SharedPreferences prefs = getWidgetPreferences(context);
131        boolean r = prefs.getBoolean(getShowingHintKey(appWidgetId), false);
132        if (DBG) Log.d(TAG, "Widget " + appWidgetId + " showing hint: " + r);
133        return r;
134    }
135
136    private CorpusRanker getCorpusRanker() {
137        return QsbApplication.get(this).getCorpusRanker();
138    }
139
140    private CorpusViewFactory getViewFactory() {
141        return QsbApplication.get(this).getCorpusViewFactory();
142    }
143
144    private class SourceClickListener implements AdapterView.OnItemClickListener {
145        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
146            Corpus corpus = (Corpus) parent.getItemAtPosition(position);
147            selectCorpus(corpus);
148        }
149    }
150}
151