SearchActivityViewTwoPane.java revision fd4a4cbc1143a734d357897531daa7105db6459b
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.Promoter;
21import com.android.quicksearchbox.R;
22import com.android.quicksearchbox.Suggestions;
23
24import android.animation.ObjectAnimator;
25import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
27import android.content.Context;
28import android.graphics.drawable.Drawable;
29import android.util.AttributeSet;
30import android.view.View;
31import android.widget.ImageView;
32import android.widget.ListAdapter;
33
34/**
35 * Two-pane variant for the search activity view.
36 */
37public class SearchActivityViewTwoPane extends SearchActivityView {
38
39    private static final int TINT_ANIMATION_DURATION = 300; // in millis
40    private static final int TINT_ANIMATION_START_DELAY = 400; // in millis
41
42    private ImageView mSettingsButton;
43
44    // View that shows the results other than the query completions
45    private SuggestionsView mResultsView;
46    private SuggestionsAdapter<ListAdapter> mResultsAdapter;
47    private View mResultsHeader;
48
49    public SearchActivityViewTwoPane(Context context) {
50        super(context);
51    }
52
53    public SearchActivityViewTwoPane(Context context, AttributeSet attrs) {
54        super(context, attrs);
55    }
56
57    public SearchActivityViewTwoPane(Context context, AttributeSet attrs, int defStyle) {
58        super(context, attrs, defStyle);
59    }
60
61    @Override
62    protected void onFinishInflate() {
63        super.onFinishInflate();
64
65        mSettingsButton = (ImageView) findViewById(R.id.settings_icon);
66
67        mResultsView = (SuggestionsView) findViewById(R.id.shortcuts);
68        mResultsAdapter = createSuggestionsAdapter();
69        mResultsAdapter.setSuggestionAdapterChangeListener(this);
70        mResultsView.setOnKeyListener(new SuggestionsViewKeyListener());
71        mResultsHeader = findViewById(R.id.shortcut_title);
72
73        mSuggestionsAdapter.setIcon1Enabled(false);
74    }
75
76    @Override
77    public void onSuggestionAdapterChanged() {
78        mResultsView.setSuggestionsAdapter(mResultsAdapter);
79        super.onSuggestionAdapterChanged();
80    }
81
82    @Override
83    public void onResume() {
84        setupWallpaperTint();
85    }
86
87    private void setupWallpaperTint() {
88        // Alpha fade-in the background tint when the activity resumes.
89        final Drawable drawable = getBackground();
90        drawable.setAlpha(0);
91        ValueAnimator animator = ObjectAnimator.ofInt(drawable, "alpha", 0, 255);
92        animator.setDuration(TINT_ANIMATION_DURATION);
93        animator.addUpdateListener(new AnimatorUpdateListener() {
94
95            public void onAnimationUpdate(ValueAnimator animator) {
96                drawable.invalidateSelf();
97            }
98        });
99        animator.setStartDelay(TINT_ANIMATION_START_DELAY);
100        animator.setInterpolator(new android.view.animation.LinearInterpolator());
101        animator.start();
102    }
103
104    @Override
105    public void onStop() {
106    }
107
108    @Override
109    public void start() {
110        super.start();
111        mResultsView.setSuggestionsAdapter(mResultsAdapter);
112    }
113
114    @Override
115    public void destroy() {
116        mResultsView.setSuggestionsAdapter(null);
117
118        super.destroy();
119    }
120
121    @Override
122    public void considerHidingInputMethod() {
123        // Don't hide keyboard when interacting with suggestions list
124    }
125
126    @Override
127    public void hideSuggestions() {
128        // Never hiding suggestions view in two-pane UI
129    }
130
131    @Override
132    public void showSuggestions() {
133        // Never hiding suggestions view in two-pane UI
134    }
135
136    @Override
137    public void showCorpusSelectionDialog() {
138        // not used
139    }
140
141    @Override
142    public void clearSuggestions() {
143        super.clearSuggestions();
144        mResultsAdapter.setSuggestions(null);
145    }
146
147    @Override
148    public void setMaxPromotedResults(int maxPromoted) {
149        mResultsView.setLimitSuggestionsToViewHeight(false);
150        mResultsAdapter.setMaxPromoted(maxPromoted);
151    }
152
153    @Override
154    public void limitResultsToViewHeight() {
155        mResultsView.setLimitSuggestionsToViewHeight(true);
156    }
157
158
159    @Override
160    public void setSettingsButtonClickListener(OnClickListener listener) {
161        mSettingsButton.setOnClickListener(listener);
162    }
163
164    @Override
165    public void setSuggestionClickListener(SuggestionClickListener listener) {
166        super.setSuggestionClickListener(listener);
167        mResultsAdapter.setSuggestionClickListener(listener);
168    }
169
170    @Override
171    public void setEmptySpaceClickListener(final View.OnClickListener listener) {
172        findViewById(R.id.panes).setOnClickListener(listener);
173    }
174
175    @Override
176    public void setSuggestions(Suggestions suggestions) {
177        super.setSuggestions(suggestions);
178        suggestions.acquire();
179        mResultsAdapter.setSuggestions(suggestions);
180    }
181
182    @Override
183    protected void setCorpus(Corpus corpus) {
184        super.setCorpus(corpus);
185        mResultsAdapter.setPromoter(createResultsPromoter());
186    }
187
188    @Override
189    protected Promoter createSuggestionsPromoter() {
190        return getQsbApplication().createWebPromoter();
191    }
192
193    protected Promoter createResultsPromoter() {
194        Corpus corpus = getCorpus();
195        if (corpus == null) {
196            return getQsbApplication().createResultsPromoter();
197        } else {
198            return getQsbApplication().createSingleCorpusResultsPromoter(corpus);
199        }
200    }
201
202    @Override
203    protected void onSuggestionsChanged() {
204        super.onSuggestionsChanged();
205        checkHideResultsHeader();
206    }
207
208    private void checkHideResultsHeader() {
209        if (mResultsHeader != null) {
210            if (mResultsAdapter.getListAdapter().getCount() > 0) {
211                mResultsHeader.setVisibility(VISIBLE);
212            } else {
213                mResultsHeader.setVisibility(INVISIBLE);
214            }
215        }
216    }
217
218    @Override
219    public Corpus getSearchCorpus() {
220        return getWebCorpus();
221    }
222
223}
224