CorpusSelectionDialog.java revision b83882b9efa37ec0f20a0f1c85cf5ccc93194aee
1/*
2 * Copyright (C) 2009 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.app.Dialog;
23import android.content.Context;
24import android.content.Intent;
25import android.os.Bundle;
26import android.util.Log;
27import android.view.KeyEvent;
28import android.view.Menu;
29import android.view.MotionEvent;
30import android.view.View;
31import android.view.Window;
32import android.view.WindowManager;
33import android.widget.AdapterView;
34import android.widget.GridView;
35import android.widget.ImageView;
36
37/**
38 * Corpus selection dialog.
39 */
40public class CorpusSelectionDialog extends Dialog {
41
42    private static final boolean DBG = false;
43    private static final String TAG = "QSB.SelectSearchSourceDialog";
44
45    private GridView mCorpusGrid;
46
47    private ImageView mEditItems;
48
49    private OnCorpusSelectedListener mListener;
50
51    private Corpus mCorpus;
52
53    private CorporaAdapter mAdapter;
54
55    public CorpusSelectionDialog(Context context) {
56        super(context, R.style.Theme_SelectSearchSource);
57    }
58
59    /**
60     * Shows the corpus selection dialog.
61     *
62     * @param corpus The currently selected corpus.
63     */
64    public void show(Corpus corpus) {
65        mCorpus = corpus;
66        show();
67    }
68
69    public void setOnCorpusSelectedListener(OnCorpusSelectedListener listener) {
70        mListener = listener;
71    }
72
73    @Override
74    protected void onCreate(Bundle savedInstanceState) {
75        setContentView(R.layout.corpus_selection_dialog);
76        mCorpusGrid = (GridView) findViewById(R.id.corpus_grid);
77        mCorpusGrid.setOnItemClickListener(new CorpusClickListener());
78        // TODO: for some reason, putting this in the XML layout instead makes
79        // the list items unclickable.
80        mCorpusGrid.setFocusable(true);
81
82        mEditItems = (ImageView) findViewById(R.id.corpus_edit_items);
83        mEditItems.setOnClickListener(new CorpusEditListener());
84
85        Window window = getWindow();
86        WindowManager.LayoutParams lp = window.getAttributes();
87        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
88        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
89        // Put window on top of input method
90        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
91        window.setAttributes(lp);
92        if (DBG) Log.d(TAG, "Window params: " + lp);
93    }
94
95    @Override
96    protected void onStart() {
97        super.onStart();
98        CorporaAdapter adapter =
99                CorporaAdapter.createGridAdapter(getViewFactory(), getCorpusRanker());
100        setAdapter(adapter);
101        mCorpusGrid.setSelection(adapter.getCorpusPosition(mCorpus));
102    }
103
104    @Override
105    protected void onStop() {
106        setAdapter(null);
107        super.onStop();
108    }
109
110    @Override
111    public boolean onCreateOptionsMenu(Menu menu) {
112        super.onCreateOptionsMenu(menu);
113        SearchSettings.addSearchSettingsMenuItem(getContext(), menu);
114        return true;
115    }
116
117    @Override
118    public boolean onTouchEvent(MotionEvent event) {
119        if (event.getAction() == MotionEvent.ACTION_DOWN) {
120            // Cancel dialog on any touch down event which is not handled by the corpus grid
121            cancel();
122            return true;
123        }
124        return false;
125    }
126
127    @Override
128    public boolean onKeyDown(int keyCode, KeyEvent event) {
129        boolean handled = super.onKeyDown(keyCode, event);
130        if (handled) {
131            return handled;
132        }
133        // Dismiss dialog on up move when nothing, or an item on the top row, is selected.
134        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
135            if (mEditItems.isFocused()) {
136                cancel();
137                return true;
138            }
139        }
140        // Dismiss dialog when typing on hard keyboard (soft keyboard is behind the dialog,
141        // so that can't be typed on)
142        if (event.isPrintingKey()) {
143            cancel();
144            return true;
145        }
146        return false;
147    }
148
149    @Override
150    public void onBackPressed() {
151        SearchActivity searchActivity = getSearchActivity();
152        if (searchActivity.startedIntoCorpusSelectionDialog()) {
153            searchActivity.onBackPressed();
154        }
155        cancel();
156    }
157
158    private SearchActivity getSearchActivity() {
159        return (SearchActivity) getOwnerActivity();
160    }
161
162    private void setAdapter(CorporaAdapter adapter) {
163        if (adapter == mAdapter) return;
164        if (mAdapter != null) mAdapter.close();
165        mAdapter = adapter;
166        mCorpusGrid.setAdapter(mAdapter);
167    }
168
169    private QsbApplication getQsbApplication() {
170        return QsbApplication.get(getContext());
171    }
172
173    private CorpusRanker getCorpusRanker() {
174        return getQsbApplication().getCorpusRanker();
175    }
176
177    private CorpusViewFactory getViewFactory() {
178        return getQsbApplication().getCorpusViewFactory();
179    }
180
181    protected void selectCorpus(Corpus corpus) {
182        dismiss();
183        if (mListener != null) {
184            String corpusName = corpus == null ? null : corpus.getName();
185            mListener.onCorpusSelected(corpusName);
186        }
187    }
188
189    private class CorpusClickListener implements AdapterView.OnItemClickListener {
190        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
191            Corpus corpus = (Corpus) parent.getItemAtPosition(position);
192            if (DBG) Log.d(TAG, "Corpus selected: " + corpus);
193            selectCorpus(corpus);
194        }
195    }
196
197    private class CorpusEditListener implements View.OnClickListener {
198        public void onClick(View v) {
199            Intent intent = SearchSettings.getSearchableItemsIntent(getContext());
200            getContext().startActivity(intent);
201        }
202    }
203
204    public interface OnCorpusSelectedListener {
205        void onCorpusSelected(String corpusName);
206    }
207}
208