WebSearchSuggestionView.java revision 77909685887bd6db7454b73cf274afc3aca2f58d
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.QsbApplication;
20import com.android.quicksearchbox.R;
21import com.android.quicksearchbox.Suggestion;
22import com.android.quicksearchbox.SuggestionFormatter;
23
24import android.content.Context;
25import android.util.AttributeSet;
26import android.view.KeyEvent;
27import android.view.View;
28
29/**
30 * View for web search suggestions.
31 */
32public class WebSearchSuggestionView extends BaseSuggestionView {
33
34    private static final String VIEW_ID = "web_search";
35
36    private final SuggestionFormatter mSuggestionFormatter;
37
38    public WebSearchSuggestionView(Context context, AttributeSet attrs) {
39        super(context, attrs);
40        mSuggestionFormatter = QsbApplication.get(context).getSuggestionFormatter();
41    }
42
43    @Override
44    protected void onFinishInflate() {
45        super.onFinishInflate();
46        KeyListener keyListener = new KeyListener();
47        setOnKeyListener(keyListener);
48        mIcon2.setOnKeyListener(keyListener);
49        mIcon2.setOnClickListener(new View.OnClickListener() {
50            public void onClick(View v) {
51                onSuggestionQueryRefineClicked();
52            }
53        });
54        mIcon2.setFocusable(true);
55    }
56
57    @Override
58    public void bindAsSuggestion(Suggestion suggestion, String userQuery) {
59        super.bindAsSuggestion(suggestion, userQuery);
60
61        CharSequence text1 = mSuggestionFormatter.formatSuggestion(userQuery,
62                suggestion.getSuggestionText1());
63        setText1(text1);
64    }
65
66    private class KeyListener implements View.OnKeyListener {
67        public boolean onKey(View v, int keyCode, KeyEvent event) {
68            boolean consumed = false;
69            if (event.getAction() == KeyEvent.ACTION_DOWN) {
70                if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && v != mIcon2) {
71                    consumed = mIcon2.requestFocus();
72                } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v == mIcon2) {
73                    consumed = requestFocus();
74                }
75            }
76            return consumed;
77        }
78    }
79
80    public static class Factory extends SuggestionViewInflater {
81
82        public Factory(Context context) {
83            super(VIEW_ID, WebSearchSuggestionView.class, R.layout.web_search_suggestion, context);
84        }
85
86        @Override
87        public boolean canCreateView(Suggestion suggestion) {
88            return suggestion.isWebSearchSuggestion();
89        }
90    }
91
92}
93