SearchActivityViewSinglePane.java revision 0a73d81f02118d0343d3f1c9219a8354466f72b3
1/* 2 * Copyright (C) 2010 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.ui; 18 19import com.android.quicksearchbox.Corpus; 20import com.android.quicksearchbox.CorpusSelectionDialog; 21import com.android.quicksearchbox.R; 22 23import android.content.Context; 24import android.graphics.drawable.Drawable; 25import android.util.AttributeSet; 26import android.view.View; 27import android.widget.ImageButton; 28 29/** 30 * Finishes the containing activity on BACK, even if input method is showing. 31 */ 32public class SearchActivityViewSinglePane extends SearchActivityView { 33 34 private Corpus mCorpus; 35 36 private CorpusSelectionDialog mCorpusSelectionDialog; 37 38 private ImageButton mCorpusIndicator; 39 40 public SearchActivityViewSinglePane(Context context) { 41 super(context); 42 } 43 44 public SearchActivityViewSinglePane(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 } 47 48 public SearchActivityViewSinglePane(Context context, AttributeSet attrs, int defStyle) { 49 super(context, attrs, defStyle); 50 } 51 52 @Override 53 protected void onFinishInflate() { 54 super.onFinishInflate(); 55 mCorpusIndicator = (ImageButton) findViewById(R.id.corpus_indicator); 56 mCorpusIndicator.setOnKeyListener(mButtonsKeyListener); 57 mCorpusIndicator.setOnClickListener(new View.OnClickListener(){ 58 public void onClick(View v) { 59 showCorpusSelectionDialog(); 60 }}); 61 } 62 63 @Override 64 public void onResume() { 65 if (!isCorpusSelectionDialogShowing()) { 66 focusQueryTextView(); 67 } 68 } 69 70 @Override 71 public void onStop() { 72 dismissCorpusSelectionDialog(); 73 } 74 75 @Override 76 public Corpus getCorpus() { 77 return mCorpus; 78 } 79 80 @Override 81 public void setCorpus(Corpus corpus) { 82 mCorpus = corpus; 83 mSuggestionsAdapter.setCorpus(corpus); 84 if (corpus == null) { 85 mSuggestionsAdapter.setPromoter(getQsbApplication().createBlendingPromoter()); 86 } else { 87 mSuggestionsAdapter.setPromoter(getQsbApplication().createSingleCorpusPromoter()); 88 } 89 90 if (mCorpusIndicator != null) { 91 Drawable sourceIcon; 92 if (corpus == null) { 93 sourceIcon = getCorpusViewFactory().getGlobalSearchIcon(); 94 } else { 95 sourceIcon = corpus.getCorpusIcon(); 96 } 97 mCorpusIndicator.setImageDrawable(sourceIcon); 98 } 99 100 updateUi(getQuery().length() == 0); 101 } 102 103 /** 104 * Gets the corpus to use for any searches. This is the web corpus in "All" mode, 105 * and the selected corpus otherwise. 106 */ 107 @Override 108 public Corpus getSearchCorpus() { 109 if (mCorpus == null) { 110 return getWebCorpus(); 111 } else { 112 return mCorpus; 113 } 114 } 115 116 @Override 117 public void setSettingsButtonClickListener(View.OnClickListener listener) { 118 } 119 120 @Override 121 public void showCorpusSelectionDialog() { 122 if (mCorpusSelectionDialog == null) { 123 mCorpusSelectionDialog = getActivity().getCorpusSelectionDialog(); 124 mCorpusSelectionDialog.setOnCorpusSelectedListener(new CorpusSelectionListener()); 125 } 126 mCorpusSelectionDialog.show(mCorpus); 127 } 128 129 protected boolean isCorpusSelectionDialogShowing() { 130 return mCorpusSelectionDialog != null && mCorpusSelectionDialog.isShowing(); 131 } 132 133 protected void dismissCorpusSelectionDialog() { 134 if (mCorpusSelectionDialog != null) { 135 mCorpusSelectionDialog.dismiss(); 136 } 137 } 138 139 @Override 140 protected void considerHidingInputMethod() { 141 mQueryTextView.hideInputMethod(); 142 } 143 144 private class CorpusSelectionListener 145 implements CorpusSelectionDialog.OnCorpusSelectedListener { 146 public void onCorpusSelected(String corpusName) { 147 setCorpus(corpusName); 148 getActivity().updateSuggestions(getQuery()); 149 focusQueryTextView(); 150 showInputMethodForQuery(); 151 } 152 } 153 154} 155