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 import com.android.quicksearchbox.ui.CorpusViewFactory; 21 22 import android.app.Dialog; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.util.Log; 27 import android.view.KeyEvent; 28 import android.view.Menu; 29 import android.view.MotionEvent; 30 import android.view.View; 31 import android.view.Window; 32 import android.view.WindowManager; 33 import android.widget.AdapterView; 34 import android.widget.GridView; 35 import android.widget.ImageView; 36 import android.widget.TextView; 37 38 /** 39 * Corpus selection dialog. 40 */ 41 public class CorpusSelectionDialog extends Dialog { 42 43 private static final boolean DBG = false; 44 private static final String TAG = "QSB.SelectSearchSourceDialog"; 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) { 57 super(context, R.style.Theme_SelectSearchSource); 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 mEditItems = (ImageView) findViewById(R.id.corpus_edit_items); 84 mEditItems.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 if (mEditItems.isFocused()) { 137 cancel(); 138 return true; 139 } 140 } 141 // Dismiss dialog when typing on hard keyboard (soft keyboard is behind the dialog, 142 // so that can't be typed on) 143 if (event.isPrintingKey()) { 144 cancel(); 145 return true; 146 } 147 return false; 148 } 149 150 @Override 151 public void onBackPressed() { 152 SearchActivity searchActivity = getSearchActivity(); 153 if (searchActivity.startedIntoCorpusSelectionDialog()) { 154 searchActivity.onBackPressed(); 155 } 156 cancel(); 157 } 158 159 private SearchActivity getSearchActivity() { 160 return (SearchActivity) getOwnerActivity(); 161 } 162 163 private void setAdapter(CorporaAdapter adapter) { 164 if (adapter == mAdapter) return; 165 if (mAdapter != null) mAdapter.close(); 166 mAdapter = adapter; 167 mCorpusGrid.setAdapter(mAdapter); 168 } 169 170 private QsbApplication getQsbApplication() { 171 return QsbApplication.get(getContext()); 172 } 173 174 private CorpusRanker getCorpusRanker() { 175 return getQsbApplication().getCorpusRanker(); 176 } 177 178 private CorpusViewFactory getViewFactory() { 179 return getQsbApplication().getCorpusViewFactory(); 180 } 181 182 protected void selectCorpus(Corpus corpus) { 183 dismiss(); 184 if (mListener != null) { 185 String corpusName = corpus == null ? null : corpus.getName(); 186 mListener.onCorpusSelected(corpusName); 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 if (DBG) Log.d(TAG, "Corpus selected: " + corpus); 194 selectCorpus(corpus); 195 } 196 } 197 198 private class CorpusEditListener implements View.OnClickListener { 199 public void onClick(View v) { 200 Intent intent = SearchSettings.getSearchableItemsIntent(getContext()); 201 getContext().startActivity(intent); 202 } 203 } 204 205 public interface OnCorpusSelectedListener { 206 void onCorpusSelected(String corpusName); 207 } 208 } 209