• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.quicksearchbox;
18 
19 import com.android.quicksearchbox.ui.CorporaAdapter;
20 
21 import android.app.Dialog;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.KeyEvent;
27 import android.view.Menu;
28 import android.view.MotionEvent;
29 import android.view.View;
30 import android.view.Window;
31 import android.view.WindowManager;
32 import android.widget.AdapterView;
33 import android.widget.GridView;
34 import android.widget.ImageView;
35 
36 /**
37  * Corpus selection dialog.
38  */
39 public 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 
CorpusSelectionDialog(Context context, SearchSettings settings)56     public CorpusSelectionDialog(Context context, SearchSettings settings) {
57         super(context, R.style.Theme_SelectSearchSource);
58         mSettings = settings;
59     }
60 
getSettings()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      */
show(Corpus corpus)70     public void show(Corpus corpus) {
71         mCorpus = corpus;
72         show();
73     }
74 
setOnCorpusSelectedListener(OnCorpusSelectedListener listener)75     public void setOnCorpusSelectedListener(OnCorpusSelectedListener listener) {
76         mListener = listener;
77     }
78 
79     @Override
onCreate(Bundle savedInstanceState)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
onStart()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
onStop()113     protected void onStop() {
114         setAdapter(null);
115         super.onStop();
116     }
117 
118     @Override
onPrepareOptionsMenu(Menu menu)119     public boolean onPrepareOptionsMenu(Menu menu) {
120         menu.clear();
121         getSettings().addMenuItems(menu, true);
122         return true;
123     }
124 
125     @Override
onTouchEvent(MotionEvent event)126     public boolean onTouchEvent(MotionEvent event) {
127         if (event.getAction() == MotionEvent.ACTION_DOWN) {
128             // Cancel dialog on any touch down event which is not handled by the corpus grid
129             cancel();
130             return true;
131         }
132         return false;
133     }
134 
135     @Override
onKeyDown(int keyCode, KeyEvent event)136     public boolean onKeyDown(int keyCode, KeyEvent event) {
137         boolean handled = super.onKeyDown(keyCode, event);
138         if (handled) {
139             return handled;
140         }
141         // Dismiss dialog on up move when nothing, or an item on the top row, is selected.
142         if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
143             if (mEditItems.isFocused()) {
144                 cancel();
145                 return true;
146             }
147         }
148         // Dismiss dialog when typing on hard keyboard (soft keyboard is behind the dialog,
149         // so that can't be typed on)
150         if (event.isPrintingKey()) {
151             cancel();
152             return true;
153         }
154         return false;
155     }
156 
157     @Override
onBackPressed()158     public void onBackPressed() {
159         SearchActivity searchActivity = getSearchActivity();
160         if (searchActivity.startedIntoCorpusSelectionDialog()) {
161             searchActivity.onBackPressed();
162         }
163         cancel();
164     }
165 
getSearchActivity()166     private SearchActivity getSearchActivity() {
167         return (SearchActivity) getOwnerActivity();
168     }
169 
setAdapter(CorporaAdapter adapter)170     private void setAdapter(CorporaAdapter adapter) {
171         if (adapter == mAdapter) return;
172         if (mAdapter != null) mAdapter.close();
173         mAdapter = adapter;
174         mCorpusGrid.setAdapter(mAdapter);
175     }
176 
getQsbApplication()177     private QsbApplication getQsbApplication() {
178         return QsbApplication.get(getContext());
179     }
180 
selectCorpus(Corpus corpus)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 {
onItemClick(AdapterView<?> parent, View view, int position, long id)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 {
onClick(View v)198         public void onClick(View v) {
199             Intent intent = getSettings().getSearchableItemsIntent();
200             getContext().startActivity(intent);
201         }
202     }
203 
204     public interface OnCorpusSelectedListener {
onCorpusSelected(String corpusName)205         void onCorpusSelected(String corpusName);
206     }
207 }
208