OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.shell.omnibox; | 5 package org.chromium.chrome.shell.omnibox; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.graphics.Rect; |
8 import android.os.Handler; | 9 import android.os.Handler; |
9 import android.text.Editable; | 10 import android.text.Editable; |
10 import android.text.TextUtils; | 11 import android.text.TextUtils; |
11 import android.text.TextWatcher; | 12 import android.text.TextWatcher; |
12 import android.view.View; | 13 import android.view.View; |
13 import android.view.View.OnLayoutChangeListener; | 14 import android.view.View.OnLayoutChangeListener; |
14 import android.widget.AdapterView; | 15 import android.widget.AdapterView; |
15 import android.widget.AdapterView.OnItemClickListener; | 16 import android.widget.AdapterView.OnItemClickListener; |
16 import android.widget.ListPopupWindow; | 17 import android.widget.ListPopupWindow; |
17 import android.widget.PopupWindow.OnDismissListener; | 18 import android.widget.PopupWindow.OnDismissListener; |
18 import android.widget.TextView; | 19 import android.widget.TextView; |
19 | 20 |
20 import org.chromium.chrome.browser.omnibox.AutocompleteController; | 21 import org.chromium.chrome.browser.omnibox.AutocompleteController; |
21 import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsR
eceivedListener; | 22 import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsR
eceivedListener; |
22 import org.chromium.chrome.browser.omnibox.OmniboxSuggestion; | 23 import org.chromium.chrome.browser.omnibox.OmniboxSuggestion; |
| 24 import org.chromium.chrome.shell.ChromeShellActivity; |
23 import org.chromium.chrome.shell.ChromeShellToolbar; | 25 import org.chromium.chrome.shell.ChromeShellToolbar; |
24 import org.chromium.chrome.shell.R; | 26 import org.chromium.chrome.shell.R; |
25 | 27 |
26 import java.util.List; | 28 import java.util.List; |
27 | 29 |
28 /** | 30 /** |
29 * Displays suggestions for the text that is entered to the ChromeShell URL fiel
d. | 31 * Displays suggestions for the text that is entered to the ChromeShell URL fiel
d. |
30 */ | 32 */ |
31 public class SuggestionPopup implements OnSuggestionsReceivedListener, TextWatch
er { | 33 public class SuggestionPopup implements OnSuggestionsReceivedListener, TextWatch
er { |
32 private static final long SUGGESTION_START_DELAY_MS = 30; | 34 private static final long SUGGESTION_START_DELAY_MS = 30; |
33 | 35 |
34 private final Context mContext; | 36 private final Context mContext; |
35 private final TextView mUrlField; | 37 private final TextView mUrlField; |
36 private final ChromeShellToolbar mToolbar; | 38 private final ChromeShellToolbar mToolbar; |
37 private final AutocompleteController mAutocomplete; | 39 private final AutocompleteController mAutocomplete; |
38 | 40 |
39 private boolean mHasStartedNewOmniboxEditSession; | 41 private boolean mHasStartedNewOmniboxEditSession; |
40 private Runnable mRequestSuggestions; | 42 private Runnable mRequestSuggestions; |
41 private ListPopupWindow mSuggestionsPopup; | 43 private ListPopupWindow mSuggestionsPopup; |
42 private SuggestionArrayAdapter mSuggestionArrayAdapter; | 44 private SuggestionArrayAdapter mSuggestionArrayAdapter; |
| 45 private int mSuggestionsPopupItemsCount; |
43 | 46 |
44 /** | 47 /** |
45 * Initializes a suggestion popup that will track urlField value and display
suggestions based | 48 * Initializes a suggestion popup that will track urlField value and display
suggestions based |
46 * on that value. | 49 * on that value. |
47 */ | 50 */ |
48 public SuggestionPopup(Context context, TextView urlField, | 51 public SuggestionPopup(Context context, TextView urlField, |
49 ChromeShellToolbar toolbar) { | 52 ChromeShellToolbar toolbar) { |
50 mContext = context; | 53 mContext = context; |
51 mUrlField = urlField; | 54 mUrlField = urlField; |
52 mToolbar = toolbar; | 55 mToolbar = toolbar; |
53 mAutocomplete = new AutocompleteController(this); | 56 mAutocomplete = new AutocompleteController(this); |
54 OnLayoutChangeListener listener = new OnLayoutChangeListener() { | 57 OnLayoutChangeListener listener = new OnLayoutChangeListener() { |
55 @Override | 58 @Override |
56 public void onLayoutChange(View v, int left, int top, int right, int
bottom, | 59 public void onLayoutChange(View v, int left, int top, int right, int
bottom, |
57 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 60 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
58 if (mSuggestionsPopup == null || !mSuggestionsPopup.isShowing())
return; | 61 if (mSuggestionsPopup == null || !mSuggestionsPopup.isShowing())
return; |
59 mSuggestionsPopup.setWidth(mUrlField.getWidth()); | 62 mSuggestionsPopup.setWidth(mUrlField.getWidth()); |
| 63 mSuggestionsPopup.setHeight(getSuggestionPopupHeight()); |
60 mSuggestionsPopup.show(); | 64 mSuggestionsPopup.show(); |
61 } | 65 } |
62 }; | 66 }; |
63 mUrlField.addOnLayoutChangeListener(listener); | 67 mUrlField.addOnLayoutChangeListener(listener); |
64 } | 68 } |
65 | 69 |
66 private void navigateToSuggestion(int position) { | 70 private void navigateToSuggestion(int position) { |
67 mToolbar.getCurrentTab().loadUrlWithSanitization( | 71 mToolbar.getCurrentTab().loadUrlWithSanitization( |
68 mSuggestionArrayAdapter.getItem(position).getUrl()); | 72 mSuggestionArrayAdapter.getItem(position).getUrl()); |
69 mUrlField.clearFocus(); | 73 mUrlField.clearFocus(); |
(...skipping 21 matching lines...) Expand all Loading... |
91 * Signals the autocomplete controller to stop generating suggestions and | 95 * Signals the autocomplete controller to stop generating suggestions and |
92 * cancels the queued task to start the autocomplete controller, if any. | 96 * cancels the queued task to start the autocomplete controller, if any. |
93 * | 97 * |
94 * @param clear Whether to clear the most recent autocomplete results. | 98 * @param clear Whether to clear the most recent autocomplete results. |
95 */ | 99 */ |
96 private void stopAutocomplete(boolean clear) { | 100 private void stopAutocomplete(boolean clear) { |
97 if (mAutocomplete != null) mAutocomplete.stop(clear); | 101 if (mAutocomplete != null) mAutocomplete.stop(clear); |
98 if (mRequestSuggestions != null) mRequestSuggestions = null; | 102 if (mRequestSuggestions != null) mRequestSuggestions = null; |
99 } | 103 } |
100 | 104 |
| 105 private int getSuggestionPopupHeight() { |
| 106 Rect appRect = new Rect(); |
| 107 ((ChromeShellActivity)mContext).getWindow().getDecorView(). |
| 108 getWindowVisibleDisplayFrame(appRect); |
| 109 int dropDownItemHeight = mContext.getResources(). |
| 110 getDimensionPixelSize(R.dimen.dropdown_item_height); |
| 111 // Applying margin height equal to |dropDownItemHeight| if constrained b
y app rect. |
| 112 int popupHeight = appRect.height() - dropDownItemHeight; |
| 113 if (mSuggestionsPopup != null) { |
| 114 int height = mSuggestionsPopupItemsCount * dropDownItemHeight; |
| 115 if (height < popupHeight) |
| 116 popupHeight = height; |
| 117 } |
| 118 return popupHeight; |
| 119 } |
| 120 |
101 // OnSuggestionsReceivedListener implementation | 121 // OnSuggestionsReceivedListener implementation |
102 | |
103 @Override | 122 @Override |
104 public void onSuggestionsReceived(List<OmniboxSuggestion> suggestions, | 123 public void onSuggestionsReceived(List<OmniboxSuggestion> suggestions, |
105 String inlineAutocompleteText) { | 124 String inlineAutocompleteText) { |
106 if (!mUrlField.isFocused() || suggestions.isEmpty()) | 125 if (!mUrlField.isFocused() || suggestions.isEmpty()) |
107 return; | 126 return; |
| 127 mSuggestionsPopupItemsCount = suggestions.size(); |
108 if (mSuggestionsPopup == null) { | 128 if (mSuggestionsPopup == null) { |
109 mSuggestionsPopup = new ListPopupWindow( | 129 mSuggestionsPopup = new ListPopupWindow( |
110 mContext, null, android.R.attr.autoCompleteTextViewStyle); | 130 mContext, null, android.R.attr.autoCompleteTextViewStyle); |
111 mSuggestionsPopup.setOnDismissListener(new OnDismissListener() { | 131 mSuggestionsPopup.setOnDismissListener(new OnDismissListener() { |
112 @Override | 132 @Override |
113 public void onDismiss() { | 133 public void onDismiss() { |
114 mHasStartedNewOmniboxEditSession = false; | 134 mHasStartedNewOmniboxEditSession = false; |
115 mSuggestionArrayAdapter = null; | 135 mSuggestionArrayAdapter = null; |
116 } | 136 } |
117 }); | 137 }); |
118 } | 138 } |
| 139 mSuggestionsPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED
); |
119 mSuggestionsPopup.setWidth(mUrlField.getWidth()); | 140 mSuggestionsPopup.setWidth(mUrlField.getWidth()); |
120 mSuggestionArrayAdapter = | 141 mSuggestionArrayAdapter = |
121 new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, sug
gestions); | 142 new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, sug
gestions); |
| 143 mSuggestionsPopup.setHeight(getSuggestionPopupHeight()); |
122 mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter); | 144 mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter); |
123 mSuggestionsPopup.setAnchorView(mUrlField); | 145 mSuggestionsPopup.setAnchorView(mUrlField); |
124 mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() { | 146 mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() { |
125 @Override | 147 @Override |
126 public void onItemClick(AdapterView<?> parent, View view, int positi
on, long id) { | 148 public void onItemClick(AdapterView<?> parent, View view, int positi
on, long id) { |
127 navigateToSuggestion(position); | 149 navigateToSuggestion(position); |
128 } | 150 } |
129 }); | 151 }); |
130 mSuggestionsPopup.show(); | 152 mSuggestionsPopup.show(); |
131 } | 153 } |
(...skipping 29 matching lines...) Expand all Loading... |
161 | 183 |
162 @Override | 184 @Override |
163 public void beforeTextChanged(CharSequence s, int start, int count, int afte
r) { | 185 public void beforeTextChanged(CharSequence s, int start, int count, int afte
r) { |
164 mRequestSuggestions = null; | 186 mRequestSuggestions = null; |
165 } | 187 } |
166 | 188 |
167 @Override | 189 @Override |
168 public void onTextChanged(CharSequence s, int start, int before, int count)
{ | 190 public void onTextChanged(CharSequence s, int start, int before, int count)
{ |
169 } | 191 } |
170 } | 192 } |
OLD | NEW |