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