CorpusSelectionDialog.java revision 6859aead3af0680b2c9dc326244aa89835c2c852
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 = getQsbApplication().createCorporaGridAdapter(); 99 setAdapter(adapter); 100 mCorpusGrid.setSelection(adapter.getCorpusPosition(mCorpus)); 101 } 102 103 @Override 104 protected void onStop() { 105 setAdapter(null); 106 super.onStop(); 107 } 108 109 @Override 110 public boolean onCreateOptionsMenu(Menu menu) { 111 super.onCreateOptionsMenu(menu); 112 SearchSettings.addSearchSettingsMenuItem(getContext(), menu); 113 return true; 114 } 115 116 @Override 117 public boolean onTouchEvent(MotionEvent event) { 118 if (event.getAction() == MotionEvent.ACTION_DOWN) { 119 // Cancel dialog on any touch down event which is not handled by the corpus grid 120 cancel(); 121 return true; 122 } 123 return false; 124 } 125 126 @Override 127 public boolean onKeyDown(int keyCode, KeyEvent event) { 128 boolean handled = super.onKeyDown(keyCode, event); 129 if (handled) { 130 return handled; 131 } 132 // Dismiss dialog on up move when nothing, or an item on the top row, is selected. 133 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { 134 if (mEditItems.isFocused()) { 135 cancel(); 136 return true; 137 } 138 } 139 // Dismiss dialog when typing on hard keyboard (soft keyboard is behind the dialog, 140 // so that can't be typed on) 141 if (event.isPrintingKey()) { 142 cancel(); 143 return true; 144 } 145 return false; 146 } 147 148 @Override 149 public void onBackPressed() { 150 SearchActivity searchActivity = getSearchActivity(); 151 if (searchActivity.startedIntoCorpusSelectionDialog()) { 152 searchActivity.onBackPressed(); 153 } 154 cancel(); 155 } 156 157 private SearchActivity getSearchActivity() { 158 return (SearchActivity) getOwnerActivity(); 159 } 160 161 private void setAdapter(CorporaAdapter adapter) { 162 if (adapter == mAdapter) return; 163 if (mAdapter != null) mAdapter.close(); 164 mAdapter = adapter; 165 mCorpusGrid.setAdapter(mAdapter); 166 } 167 168 private QsbApplication getQsbApplication() { 169 return QsbApplication.get(getContext()); 170 } 171 172 protected void selectCorpus(Corpus corpus) { 173 dismiss(); 174 if (mListener != null) { 175 String corpusName = corpus == null ? null : corpus.getName(); 176 mListener.onCorpusSelected(corpusName); 177 } 178 } 179 180 private class CorpusClickListener implements AdapterView.OnItemClickListener { 181 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 182 Corpus corpus = (Corpus) parent.getItemAtPosition(position); 183 if (DBG) Log.d(TAG, "Corpus selected: " + corpus); 184 selectCorpus(corpus); 185 } 186 } 187 188 private class CorpusEditListener implements View.OnClickListener { 189 public void onClick(View v) { 190 Intent intent = SearchSettings.getSearchableItemsIntent(getContext()); 191 getContext().startActivity(intent); 192 } 193 } 194 195 public interface OnCorpusSelectedListener { 196 void onCorpusSelected(String corpusName); 197 } 198} 199