CorpusSelectionDialog.java revision 59920d0365c0ab334935a4f6a6804eae1d278662
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.app.SearchManager; 24import android.content.ActivityNotFoundException; 25import android.content.Context; 26import android.content.Intent; 27import android.content.res.Resources; 28import android.os.Bundle; 29import android.util.Log; 30import android.view.Gravity; 31import android.view.Menu; 32import android.view.View; 33import android.view.Window; 34import android.view.WindowManager; 35import android.widget.AdapterView; 36import android.widget.GridView; 37 38 39/** 40 * Corpus selection dialog. 41 */ 42public class CorpusSelectionDialog extends Dialog { 43 44 private static final boolean DBG = true; 45 private static final String TAG = "QSB.SelectSearchSourceDialog"; 46 47 private GridView mCorpusGrid; 48 49 private Corpus mCorpus; 50 51 private String mQuery; 52 53 private Bundle mAppData; 54 55 private CorporaAdapter mAdapter; 56 57 public CorpusSelectionDialog(Context context) { 58 super(context, R.style.Theme_SelectSearchSource); 59 setContentView(R.layout.corpus_selection_dialog); 60 mCorpusGrid = (GridView) findViewById(R.id.corpus_grid); 61 mCorpusGrid.setOnItemClickListener(new CorpusClickListener()); 62 // TODO: for some reason, putting this in the XML layout instead makes 63 // the list items unclickable. 64 mCorpusGrid.setFocusable(true); 65 setCanceledOnTouchOutside(true); 66 positionWindow(); 67 } 68 69 public void setCorpus(Corpus corpus) { 70 mCorpus = corpus; 71 } 72 73 public void setQuery(String query) { 74 mQuery = query; 75 } 76 77 public void setAppData(Bundle appData) { 78 mAppData = appData; 79 } 80 81 private void positionWindow() { 82 Resources resources = getContext().getResources(); 83 int x = resources.getDimensionPixelSize(R.dimen.corpus_selection_dialog_x); 84 int y = resources.getDimensionPixelSize(R.dimen.corpus_selection_dialog_y); 85 positionWindowAt(x, y); 86 } 87 88 private void positionWindowAt(int x, int y) { 89 Window window = getWindow(); 90 WindowManager.LayoutParams lp = window.getAttributes(); 91 lp.x = x; 92 lp.y = y; 93 lp.gravity = Gravity.TOP | Gravity.LEFT; 94 lp.width = WindowManager.LayoutParams.MATCH_PARENT; 95 lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 96 // Put window on top of input method 97 lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 98 window.setAttributes(lp); 99 if (DBG) Log.d(TAG, "Window params: " + lp); 100 } 101 102 @Override 103 protected void onStart() { 104 super.onStart(); 105 setAdapter(CorporaAdapter.createGridAdapter(getViewFactory(), getCorpora(), 106 getCorpusRanker())); 107 } 108 109 @Override 110 protected void onStop() { 111 setAdapter(null); 112 super.onStop(); 113 } 114 115 @Override 116 public boolean onCreateOptionsMenu(Menu menu) { 117 super.onCreateOptionsMenu(menu); 118 SearchSettings.addSearchSettingsMenuItem(getContext(), menu); 119 return true; 120 } 121 122 private void setAdapter(CorporaAdapter adapter) { 123 if (adapter == mAdapter) return; 124 if (mAdapter != null) mAdapter.close(); 125 mAdapter = adapter; 126 mCorpusGrid.setAdapter(mAdapter); 127 } 128 129 private QsbApplication getQsbApplication() { 130 return (QsbApplication) getContext().getApplicationContext(); 131 } 132 133 private Corpora getCorpora() { 134 return getQsbApplication().getCorpora(); 135 } 136 137 private CorpusRanker getCorpusRanker() { 138 return getQsbApplication().getCorpusRanker(); 139 } 140 141 private CorpusViewFactory getViewFactory() { 142 return getQsbApplication().getCorpusViewFactory(); 143 } 144 145 protected void selectCorpus(Corpus corpus) { 146 dismiss(); 147 // If a new source was selected, start QSB with that source. 148 // If the old source was selected, just finish. 149 if (!isCurrentCorpus(corpus)) { 150 switchCorpus(corpus); 151 } 152 } 153 154 private boolean isCurrentCorpus(Corpus corpus) { 155 if (corpus == null) return mCorpus == null; 156 return corpus.equals(mCorpus); 157 } 158 159 private void switchCorpus(Corpus corpus) { 160 if (DBG) Log.d(TAG, "switchSource(" + corpus + ")"); 161 162 Intent searchIntent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH); 163 searchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 164 searchIntent.setData(SearchActivity.getCorpusUri(corpus)); 165 searchIntent.putExtra(SearchManager.QUERY, mQuery); 166 searchIntent.putExtra(SearchManager.APP_DATA, mAppData); 167 168 try { 169 if (DBG) Log.d(TAG, "startActivity(" + searchIntent.toUri(0) + ")"); 170 getOwnerActivity().startActivity(searchIntent); 171 } catch (ActivityNotFoundException ex) { 172 Log.e(TAG, "Couldn't start QSB: " + ex); 173 } 174 } 175 176 private class CorpusClickListener implements AdapterView.OnItemClickListener { 177 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 178 Corpus corpus = (Corpus) parent.getItemAtPosition(position); 179 selectCorpus(corpus); 180 } 181 } 182} 183