CorpusSelectionDialog.java revision 1d870d9687e1831241e528a558fcd008f8309604
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 static final int NUM_COLUMNS = 4; 46 47 private GridView mCorpusGrid; 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.setNumColumns(NUM_COLUMNS); 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() / NUM_COLUMNS; 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 mListener.onCorpusSelected(corpus); 187 } 188 } 189 190 private class CorpusClickListener implements AdapterView.OnItemClickListener { 191 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 192 Corpus corpus = (Corpus) parent.getItemAtPosition(position); 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(Corpus corpus); 206 } 207} 208