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