10a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood/*
20a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * Copyright (C) 2010 The Android Open Source Project
30a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood *
40a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * Licensed under the Apache License, Version 2.0 (the "License");
50a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * you may not use this file except in compliance with the License.
60a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * You may obtain a copy of the License at
70a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood *
80a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood *      http://www.apache.org/licenses/LICENSE-2.0
90a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood *
100a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * Unless required by applicable law or agreed to in writing, software
110a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * distributed under the License is distributed on an "AS IS" BASIS,
120a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * See the License for the specific language governing permissions and
140a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * limitations under the License.
150a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood */
160a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
170a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodpackage com.android.quicksearchbox.ui;
180a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
190a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport com.android.quicksearchbox.Corpus;
200a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport com.android.quicksearchbox.CorpusSelectionDialog;
218b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport com.android.quicksearchbox.Promoter;
220a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport com.android.quicksearchbox.R;
230a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
240a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport android.content.Context;
250a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport android.graphics.drawable.Drawable;
260a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport android.util.AttributeSet;
270a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport android.view.View;
280a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodimport android.widget.ImageButton;
290a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
300a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood/**
310a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood * Finishes the containing activity on BACK, even if input method is showing.
320a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood */
330a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwoodpublic class SearchActivityViewSinglePane extends SearchActivityView {
340a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
350a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    private CorpusSelectionDialog mCorpusSelectionDialog;
360a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
370a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    private ImageButton mCorpusIndicator;
380a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
390a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public SearchActivityViewSinglePane(Context context) {
400a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        super(context);
410a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
420a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
430a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public SearchActivityViewSinglePane(Context context, AttributeSet attrs) {
440a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        super(context, attrs);
450a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
460a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
470a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public SearchActivityViewSinglePane(Context context, AttributeSet attrs, int defStyle) {
480a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        super(context, attrs, defStyle);
490a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
500a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
510a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
520a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    protected void onFinishInflate() {
530a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        super.onFinishInflate();
540a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        mCorpusIndicator = (ImageButton) findViewById(R.id.corpus_indicator);
550a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        mCorpusIndicator.setOnKeyListener(mButtonsKeyListener);
560a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        mCorpusIndicator.setOnClickListener(new View.OnClickListener(){
570a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            public void onClick(View v) {
580a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood                showCorpusSelectionDialog();
590a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            }});
600a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
610a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
620a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
630a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public void onResume() {
640a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        if (!isCorpusSelectionDialogShowing()) {
650a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            focusQueryTextView();
660a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        }
670a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
680a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
690a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
700a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public void onStop() {
710a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        dismissCorpusSelectionDialog();
720a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
730a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
740a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
758b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    protected void setCorpus(Corpus corpus) {
768b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        super.setCorpus(corpus);
770a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
780a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        if (mCorpusIndicator != null) {
790a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            Drawable sourceIcon;
800a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            if (corpus == null) {
81138c0a8d821f02d13e73bfc579ad9bded7d4d683Ying Wang                sourceIcon = getContext().getResources().getDrawable(R.mipmap.search_app_icon);
820a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            } else {
830a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood                sourceIcon = corpus.getCorpusIcon();
840a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            }
850a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            mCorpusIndicator.setImageDrawable(sourceIcon);
860a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        }
878b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    }
880a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
898b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    @Override
908b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    protected Promoter createSuggestionsPromoter() {
918b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        Corpus corpus = getCorpus();
928b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        if (corpus == null) {
938b2936607176720172aee068abc5631bdf77e843Bjorn Bringert            return getQsbApplication().createBlendingPromoter();
948b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        } else {
958b2936607176720172aee068abc5631bdf77e843Bjorn Bringert            return getQsbApplication().createSingleCorpusPromoter(corpus);
968b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        }
970a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
980a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
990a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    /**
1000a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood     * Gets the corpus to use for any searches. This is the web corpus in "All" mode,
1010a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood     * and the selected corpus otherwise.
1020a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood     */
1030a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
1040a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public Corpus getSearchCorpus() {
1058b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        Corpus corpus = getCorpus();
1068b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        return corpus == null ? getWebCorpus() : corpus;
1070a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
1080a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
1090a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
1100a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    public void showCorpusSelectionDialog() {
1110a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        if (mCorpusSelectionDialog == null) {
1120a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            mCorpusSelectionDialog = getActivity().getCorpusSelectionDialog();
1130a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            mCorpusSelectionDialog.setOnCorpusSelectedListener(new CorpusSelectionListener());
1140a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        }
1158b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        mCorpusSelectionDialog.show(getCorpus());
1160a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
1170a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
1180a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    protected boolean isCorpusSelectionDialogShowing() {
1190a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        return mCorpusSelectionDialog != null && mCorpusSelectionDialog.isShowing();
1200a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
1210a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
1220a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    protected void dismissCorpusSelectionDialog() {
1230a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        if (mCorpusSelectionDialog != null) {
1240a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            mCorpusSelectionDialog.dismiss();
1250a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        }
1260a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
1270a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
1280a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    @Override
1298749e424d7645e708f238530621248abb567e3beBjorn Bringert    public void considerHidingInputMethod() {
1300a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        mQueryTextView.hideInputMethod();
1310a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
1320a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
1330a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    private class CorpusSelectionListener
1340a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood            implements CorpusSelectionDialog.OnCorpusSelectedListener {
1350a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        public void onCorpusSelected(String corpusName) {
1368b2936607176720172aee068abc5631bdf77e843Bjorn Bringert            SearchActivityViewSinglePane.this.onCorpusSelected(corpusName);
1370a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood        }
1380a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood    }
1390a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood
1400a73d81f02118d0343d3f1c9219a8354466f72b3Mathew Inwood}
141