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