OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.omnibox; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.view.View; |
| 9 import android.view.ViewGroup; |
| 10 import android.widget.BaseAdapter; |
| 11 |
| 12 import org.chromium.base.VisibleForTesting; |
| 13 |
| 14 import java.util.List; |
| 15 |
| 16 /** |
| 17 * Adapter for providing data and views to the omnibox results list. |
| 18 */ |
| 19 @VisibleForTesting |
| 20 public class OmniboxResultsAdapter extends BaseAdapter { |
| 21 |
| 22 private final List<OmniboxResultItem> mSuggestionItems; |
| 23 private final Context mContext; |
| 24 private final LocationBar mLocationBar; |
| 25 private OmniboxSuggestionDelegate mSuggestionDelegate; |
| 26 private boolean mUseDarkColors = true; |
| 27 |
| 28 public OmniboxResultsAdapter( |
| 29 Context context, |
| 30 LocationBar locationBar, |
| 31 List<OmniboxResultItem> suggestionItems) { |
| 32 mContext = context; |
| 33 mLocationBar = locationBar; |
| 34 mSuggestionItems = suggestionItems; |
| 35 } |
| 36 |
| 37 public void notifySuggestionsChanged() { |
| 38 notifyDataSetChanged(); |
| 39 } |
| 40 |
| 41 @Override |
| 42 public int getCount() { |
| 43 return mSuggestionItems.size(); |
| 44 } |
| 45 |
| 46 @Override |
| 47 public Object getItem(int position) { |
| 48 return mSuggestionItems.get(position); |
| 49 } |
| 50 |
| 51 @Override |
| 52 public long getItemId(int position) { |
| 53 return position; |
| 54 } |
| 55 |
| 56 @Override |
| 57 public View getView(int position, View convertView, ViewGroup parent) { |
| 58 SuggestionView suggestionView; |
| 59 if (convertView != null && convertView instanceof SuggestionView) { |
| 60 suggestionView = (SuggestionView) convertView; |
| 61 } else { |
| 62 suggestionView = new SuggestionView(mContext, mLocationBar); |
| 63 } |
| 64 suggestionView.init( |
| 65 mSuggestionItems.get(position), mSuggestionDelegate, position, m
UseDarkColors); |
| 66 return suggestionView; |
| 67 } |
| 68 |
| 69 /** |
| 70 * Set the selection delegate for suggestion entries in the adapter. |
| 71 * |
| 72 * @param delegate The delegate for suggestion selections. |
| 73 */ |
| 74 public void setSuggestionDelegate(OmniboxSuggestionDelegate delegate) { |
| 75 mSuggestionDelegate = delegate; |
| 76 } |
| 77 |
| 78 /** |
| 79 * @return The selection delegate for suggestion entries in the adapter. |
| 80 */ |
| 81 @VisibleForTesting |
| 82 public OmniboxSuggestionDelegate getSuggestionDelegate() { |
| 83 return mSuggestionDelegate; |
| 84 } |
| 85 |
| 86 /** |
| 87 * Specifies the visual state to be used by the suggestions. |
| 88 * @param useDarkColors Whether dark colors should be used for fonts and ico
ns. |
| 89 */ |
| 90 public void setUseDarkColors(boolean useDarkColors) { |
| 91 mUseDarkColors = useDarkColors; |
| 92 } |
| 93 |
| 94 /** |
| 95 * Handler for actions that happen on suggestion view. |
| 96 */ |
| 97 @VisibleForTesting |
| 98 public static interface OmniboxSuggestionDelegate { |
| 99 /** |
| 100 * Triggered when the user selects one of the omnibox suggestions to nav
igate to. |
| 101 * @param suggestion The OmniboxSuggestion which was selected. |
| 102 * @param position Position of the suggestion in the drop down view. |
| 103 */ |
| 104 public void onSelection(OmniboxSuggestion suggestion, int position); |
| 105 |
| 106 /** |
| 107 * Triggered when the user selects to refine one of the omnibox suggesti
ons. |
| 108 * @param suggestion |
| 109 */ |
| 110 public void onRefineSuggestion(OmniboxSuggestion suggestion); |
| 111 |
| 112 /** |
| 113 * Triggered when the user navigates to one of the suggestions without c
licking on it. |
| 114 * @param suggestion |
| 115 */ |
| 116 public void onSetUrlToSuggestion(OmniboxSuggestion suggestion); |
| 117 |
| 118 /** |
| 119 * Triggered before we show a modal dialog triggered through suggestions
UI (e.g. the |
| 120 * delete suggestions confirmation dialog). |
| 121 */ |
| 122 public void onShowModal(); |
| 123 |
| 124 /** |
| 125 * Triggered during the modal dialog dismissal. |
| 126 */ |
| 127 public void onHideModal(); |
| 128 |
| 129 /** |
| 130 * Triggered when the user indicates they want to delete a suggestion. |
| 131 * @param position The position of the suggestion in the drop down view. |
| 132 */ |
| 133 public void onDeleteSuggestion(int position); |
| 134 |
| 135 /** |
| 136 * Triggered when the user touches the suggestion view. |
| 137 */ |
| 138 public void onGestureDown(); |
| 139 |
| 140 /** |
| 141 * Triggered when text width information is updated. |
| 142 * These values should be used to calculate max text widths. |
| 143 * @param requiredWidth a new required width. |
| 144 * @param matchContentsWidth a new match contents width. |
| 145 */ |
| 146 public void onTextWidthsUpdated(float requiredWidth, float matchContents
Width); |
| 147 |
| 148 /** |
| 149 * @return max required width for the suggestion. |
| 150 */ |
| 151 public float getMaxRequiredWidth(); |
| 152 |
| 153 /** |
| 154 * @return max match contents width for the suggestion. |
| 155 */ |
| 156 public float getMaxMatchContentsWidth(); |
| 157 } |
| 158 |
| 159 /** |
| 160 * Simple wrapper around the omnibox suggestions provided in the backend and
the query that |
| 161 * matched it. |
| 162 */ |
| 163 @VisibleForTesting |
| 164 public static class OmniboxResultItem { |
| 165 private final OmniboxSuggestion mSuggestion; |
| 166 private final String mMatchedQuery; |
| 167 |
| 168 public OmniboxResultItem(OmniboxSuggestion suggestion, String matchedQue
ry) { |
| 169 mSuggestion = suggestion; |
| 170 mMatchedQuery = matchedQuery; |
| 171 } |
| 172 |
| 173 /** |
| 174 * @return The omnibox suggestion for this item. |
| 175 */ |
| 176 public OmniboxSuggestion getSuggestion() { |
| 177 return mSuggestion; |
| 178 } |
| 179 |
| 180 /** |
| 181 * @return The user query that triggered this suggestion to be shown. |
| 182 */ |
| 183 public String getMatchedQuery() { |
| 184 return mMatchedQuery; |
| 185 } |
| 186 |
| 187 @Override |
| 188 public boolean equals(Object o) { |
| 189 if (!(o instanceof OmniboxResultItem)) { |
| 190 return false; |
| 191 } |
| 192 |
| 193 OmniboxResultItem item = (OmniboxResultItem) o; |
| 194 return mMatchedQuery.equals(item.mMatchedQuery) && mSuggestion.equal
s(item.mSuggestion); |
| 195 } |
| 196 |
| 197 @Override |
| 198 public int hashCode() { |
| 199 return 53 * mMatchedQuery.hashCode() ^ mSuggestion.hashCode(); |
| 200 } |
| 201 } |
| 202 } |
OLD | NEW |