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