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 17 package com.android.quicksearchbox.ui; 18 19 import com.android.quicksearchbox.QsbApplication; 20 import com.android.quicksearchbox.R; 21 import com.android.quicksearchbox.Suggestion; 22 import com.android.quicksearchbox.SuggestionFormatter; 23 24 import android.content.Context; 25 import android.util.AttributeSet; 26 import android.view.KeyEvent; 27 import android.view.View; 28 29 /** 30 * View for web search suggestions. 31 */ 32 public class WebSearchSuggestionView extends BaseSuggestionView { 33 34 private static final String VIEW_ID = "web_search"; 35 36 private final SuggestionFormatter mSuggestionFormatter; 37 WebSearchSuggestionView(Context context, AttributeSet attrs)38 public WebSearchSuggestionView(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 mSuggestionFormatter = QsbApplication.get(context).getSuggestionFormatter(); 41 } 42 43 @Override onFinishInflate()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 bindAsSuggestion(Suggestion suggestion, String userQuery)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 setIsHistorySuggestion(suggestion.isHistorySuggestion()); 65 } 66 setIsHistorySuggestion(boolean isHistory)67 private void setIsHistorySuggestion(boolean isHistory) { 68 if (isHistory) { 69 mIcon1.setImageResource(R.drawable.ic_history_suggestion); 70 mIcon1.setVisibility(VISIBLE); 71 } else { 72 mIcon1.setVisibility(INVISIBLE); 73 } 74 } 75 76 private class KeyListener implements View.OnKeyListener { onKey(View v, int keyCode, KeyEvent event)77 public boolean onKey(View v, int keyCode, KeyEvent event) { 78 boolean consumed = false; 79 if (event.getAction() == KeyEvent.ACTION_DOWN) { 80 if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && v != mIcon2) { 81 consumed = mIcon2.requestFocus(); 82 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v == mIcon2) { 83 consumed = requestFocus(); 84 } 85 } 86 return consumed; 87 } 88 } 89 90 public static class Factory extends SuggestionViewInflater { 91 Factory(Context context)92 public Factory(Context context) { 93 super(VIEW_ID, WebSearchSuggestionView.class, R.layout.web_search_suggestion, context); 94 } 95 96 @Override canCreateView(Suggestion suggestion)97 public boolean canCreateView(Suggestion suggestion) { 98 return suggestion.isWebSearchSuggestion(); 99 } 100 } 101 102 } 103