Chromium Code Reviews| 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; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 50 mContext = context; | 52 mContext = context; |
| 51 mUrlField = urlField; | 53 mUrlField = urlField; |
| 52 mToolbar = toolbar; | 54 mToolbar = toolbar; |
| 53 mAutocomplete = new AutocompleteController(this); | 55 mAutocomplete = new AutocompleteController(this); |
| 54 OnLayoutChangeListener listener = new OnLayoutChangeListener() { | 56 OnLayoutChangeListener listener = new OnLayoutChangeListener() { |
| 55 @Override | 57 @Override |
| 56 public void onLayoutChange(View v, int left, int top, int right, int bottom, | 58 public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 57 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 59 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 58 if (mSuggestionsPopup == null || !mSuggestionsPopup.isShowing()) return; | 60 if (mSuggestionsPopup == null || !mSuggestionsPopup.isShowing()) return; |
| 59 mSuggestionsPopup.setWidth(mUrlField.getWidth()); | 61 mSuggestionsPopup.setWidth(mUrlField.getWidth()); |
| 62 mSuggestionsPopup.setHeight(getSuggestionPopupHeight()); | |
| 60 mSuggestionsPopup.show(); | 63 mSuggestionsPopup.show(); |
| 61 } | 64 } |
| 62 }; | 65 }; |
| 63 mUrlField.addOnLayoutChangeListener(listener); | 66 mUrlField.addOnLayoutChangeListener(listener); |
| 64 } | 67 } |
| 65 | 68 |
| 66 private void navigateToSuggestion(int position) { | 69 private void navigateToSuggestion(int position) { |
| 67 mToolbar.getCurrentTab().loadUrlWithSanitization( | 70 mToolbar.getCurrentTab().loadUrlWithSanitization( |
| 68 mSuggestionArrayAdapter.getItem(position).getUrl()); | 71 mSuggestionArrayAdapter.getItem(position).getUrl()); |
| 69 mUrlField.clearFocus(); | 72 mUrlField.clearFocus(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 91 * Signals the autocomplete controller to stop generating suggestions and | 94 * Signals the autocomplete controller to stop generating suggestions and |
| 92 * cancels the queued task to start the autocomplete controller, if any. | 95 * cancels the queued task to start the autocomplete controller, if any. |
| 93 * | 96 * |
| 94 * @param clear Whether to clear the most recent autocomplete results. | 97 * @param clear Whether to clear the most recent autocomplete results. |
| 95 */ | 98 */ |
| 96 private void stopAutocomplete(boolean clear) { | 99 private void stopAutocomplete(boolean clear) { |
| 97 if (mAutocomplete != null) mAutocomplete.stop(clear); | 100 if (mAutocomplete != null) mAutocomplete.stop(clear); |
| 98 if (mRequestSuggestions != null) mRequestSuggestions = null; | 101 if (mRequestSuggestions != null) mRequestSuggestions = null; |
| 99 } | 102 } |
| 100 | 103 |
| 104 private int getSuggestionPopupHeight() { | |
| 105 Rect appRect = new Rect(); | |
| 106 ((ChromeShellActivity)mContext).getWindow().getDecorView(). | |
| 107 getWindowVisibleDisplayFrame(appRect); | |
| 108 int dropDownItemHeight = mContext.getResources(). | |
| 109 getDimensionPixelSize(R.dimen.dropdown_item_height); | |
| 110 int height = dropDownItemHeight; | |
|
Bernhard Bauer
2014/09/01 13:45:10
This value isn't used (and the variable only insid
ankit
2014/09/02 04:20:11
Done.
| |
| 111 if (mSuggestionsPopup != null && mSuggestionsPopup.getListView() != null ) { | |
| 112 height = mSuggestionsPopup.getListView().getCount() * dropDownItemHe ight; | |
|
Bernhard Bauer
2014/09/01 13:45:10
Instead of asking the view for its number of items
ankit
2014/09/02 04:20:11
Stored value in a local variable and using it inst
| |
| 113 if (appRect.height() > height) | |
|
Bernhard Bauer
2014/09/01 13:45:10
I don't quite understand this. If the height of th
ankit
2014/09/02 04:20:11
Done.
| |
| 114 return height; | |
| 115 } | |
| 116 //maintaining margin height of dropdDownItemHeight | |
|
Bernhard Bauer
2014/09/01 13:45:10
Add a space after //, capitalize the first charact
ankit
2014/09/02 04:20:11
Done.
| |
| 117 return appRect.height() - dropDownItemHeight; | |
| 118 } | |
| 119 | |
| 101 // OnSuggestionsReceivedListener implementation | 120 // OnSuggestionsReceivedListener implementation |
| 102 | 121 |
| 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; |
| 108 if (mSuggestionsPopup == null) { | 127 if (mSuggestionsPopup == null) { |
| 109 mSuggestionsPopup = new ListPopupWindow( | 128 mSuggestionsPopup = new ListPopupWindow( |
| 110 mContext, null, android.R.attr.autoCompleteTextViewStyle); | 129 mContext, null, android.R.attr.autoCompleteTextViewStyle); |
| 111 mSuggestionsPopup.setOnDismissListener(new OnDismissListener() { | 130 mSuggestionsPopup.setOnDismissListener(new OnDismissListener() { |
| 112 @Override | 131 @Override |
| 113 public void onDismiss() { | 132 public void onDismiss() { |
| 114 mHasStartedNewOmniboxEditSession = false; | 133 mHasStartedNewOmniboxEditSession = false; |
| 115 mSuggestionArrayAdapter = null; | 134 mSuggestionArrayAdapter = null; |
| 116 } | 135 } |
| 117 }); | 136 }); |
| 118 } | 137 } |
| 138 mSuggestionsPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED ); | |
| 119 mSuggestionsPopup.setWidth(mUrlField.getWidth()); | 139 mSuggestionsPopup.setWidth(mUrlField.getWidth()); |
| 120 mSuggestionArrayAdapter = | 140 mSuggestionArrayAdapter = |
| 121 new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, sug gestions); | 141 new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, sug gestions); |
| 142 mSuggestionsPopup.setHeight(getSuggestionPopupHeight()); | |
| 122 mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter); | 143 mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter); |
| 123 mSuggestionsPopup.setAnchorView(mUrlField); | 144 mSuggestionsPopup.setAnchorView(mUrlField); |
| 124 mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() { | 145 mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() { |
| 125 @Override | 146 @Override |
| 126 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { | 147 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { |
| 127 navigateToSuggestion(position); | 148 navigateToSuggestion(position); |
| 128 } | 149 } |
| 129 }); | 150 }); |
| 130 mSuggestionsPopup.show(); | 151 mSuggestionsPopup.show(); |
| 131 } | 152 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 161 | 182 |
| 162 @Override | 183 @Override |
| 163 public void beforeTextChanged(CharSequence s, int start, int count, int afte r) { | 184 public void beforeTextChanged(CharSequence s, int start, int count, int afte r) { |
| 164 mRequestSuggestions = null; | 185 mRequestSuggestions = null; |
| 165 } | 186 } |
| 166 | 187 |
| 167 @Override | 188 @Override |
| 168 public void onTextChanged(CharSequence s, int start, int before, int count) { | 189 public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 169 } | 190 } |
| 170 } | 191 } |
| OLD | NEW |