Commit 3da39e7b authored by aurimas@chromium.org's avatar aurimas@chromium.org

[ChromeShell] Add suggestions for ChromeShell toolbar box.

BUG=

Review URL: https://codereview.chromium.org/287293004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273052 0039d316-1c4b-4281-b951-d872f2087c98
parent 444ed75d
......@@ -28,8 +28,7 @@ public class TemplateUrlServiceTest extends ChromeShellTestBase {
public void setUp() throws Exception {
super.setUp();
clearAppData();
launchChromeShellWithBlankPage();
assertTrue(waitForActiveShellToBeDoneLoading());
startChromeBrowserProcessSync(getInstrumentation().getTargetContext());
}
@SmallTest
......
......@@ -23,6 +23,7 @@ import org.chromium.chrome.browser.Tab;
import org.chromium.chrome.browser.TabObserver;
import org.chromium.chrome.browser.appmenu.AppMenuButtonHelper;
import org.chromium.chrome.browser.appmenu.AppMenuHandler;
import org.chromium.chrome.shell.omnibox.SuggestionPopup;
/**
* A Toolbar {@link View} that shows the URL and navigation buttons.
......@@ -117,6 +118,7 @@ public class ChromeShellToolbar extends LinearLayout {
}
}
});
mUrlTextView.addTextChangedListener(new SuggestionPopup(getContext(), mUrlTextView, this));
}
private void initializeMenuButton() {
......@@ -135,7 +137,18 @@ public class ChromeShellToolbar extends LinearLayout {
});
}
private void setKeyboardVisibilityForUrl(boolean visible) {
/**
* @return Current tab that is shown by ChromeShell.
*/
public ChromeShellTab getCurrentTab() {
return mTab;
}
/**
* Change the visibility of the software keyboard.
* @param visible Whether the keyboard should be shown or hidden.
*/
public void setKeyboardVisibilityForUrl(boolean visible) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (visible) {
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.shell.omnibox;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.chromium.chrome.browser.omnibox.OmniboxSuggestion;
import org.chromium.chrome.shell.R;
import java.util.List;
/**
* Adapter that provides suggestion views for the suggestion popup.
*/
class SuggestionArrayAdapter extends ArrayAdapter<OmniboxSuggestion> {
private final List<OmniboxSuggestion> mSuggestions;
public SuggestionArrayAdapter(Context context, int res, List<OmniboxSuggestion> suggestions) {
super(context, res, suggestions);
mSuggestions = suggestions;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.dropdown_item, null);
int height = getContext().getResources().getDimensionPixelSize(
R.dimen.dropdown_item_height);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height));
}
TextView t1 = (TextView) v.findViewById(R.id.dropdown_label);
t1.setText(mSuggestions.get(position).getDisplayText());
TextView t2 = (TextView) v.findViewById(R.id.dropdown_sublabel);
t2.setText(mSuggestions.get(position).getUrl());
return v;
}
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.shell.omnibox;
import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import org.chromium.chrome.browser.omnibox.AutocompleteController;
import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsReceivedListener;
import org.chromium.chrome.browser.omnibox.OmniboxSuggestion;
import org.chromium.chrome.shell.ChromeShellToolbar;
import org.chromium.chrome.shell.R;
import java.util.List;
/**
* Displays suggestions for the text that is entered to the ChromeShell URL field.
*/
public class SuggestionPopup implements OnSuggestionsReceivedListener, TextWatcher {
private static final long SUGGESTION_START_DELAY_MS = 30;
private final Context mContext;
private final TextView mUrlField;
private final ChromeShellToolbar mToolbar;
private final AutocompleteController mAutocomplete;
private boolean mHasStartedNewOmniboxEditSession;
private Runnable mRequestSuggestions;
private ListPopupWindow mSuggestionsPopup;
private SuggestionArrayAdapter mSuggestionArrayAdapter;
/**
* Initializes a suggestion popup that will track urlField value and display suggestions based
* on that value.
*/
public SuggestionPopup(Context context, TextView urlField,
ChromeShellToolbar toolbar) {
mContext = context;
mUrlField = urlField;
mToolbar = toolbar;
mAutocomplete = new AutocompleteController(this);
}
private void navigateToSuggestion(int position) {
mToolbar.getCurrentTab().loadUrlWithSanitization(
mSuggestionArrayAdapter.getItem(position).getUrl());
mUrlField.clearFocus();
mToolbar.setKeyboardVisibilityForUrl(false);
mToolbar.getCurrentTab().getView().requestFocus();
dismissPopup();
}
private void dismissPopup() {
if (mSuggestionsPopup != null) {
mSuggestionsPopup.dismiss();
mSuggestionsPopup = null;
}
}
/**
* Signals the autocomplete controller to stop generating suggestions and
* cancels the queued task to start the autocomplete controller, if any.
*
* @param clear Whether to clear the most recent autocomplete results.
*/
private void stopAutocomplete(boolean clear) {
if (mAutocomplete != null) mAutocomplete.stop(clear);
if (mRequestSuggestions != null) mRequestSuggestions = null;
}
// OnSuggestionsReceivedListener implementation
@Override
public void onSuggestionsReceived(List<OmniboxSuggestion> suggestions,
String inlineAutocompleteText) {
if (!mUrlField.isFocused() || suggestions.isEmpty())
return;
if (mSuggestionsPopup == null) {
mSuggestionsPopup = new ListPopupWindow(
mContext, null, android.R.attr.autoCompleteTextViewStyle);
mSuggestionsPopup.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
mHasStartedNewOmniboxEditSession = false;
mSuggestionArrayAdapter = null;
}
});
}
mSuggestionsPopup.setWidth(mUrlField.getWidth());
mSuggestionArrayAdapter =
new SuggestionArrayAdapter(mContext, R.layout.dropdown_item, suggestions);
mSuggestionsPopup.setAdapter(mSuggestionArrayAdapter);
mSuggestionsPopup.setAnchorView(mUrlField);
mSuggestionsPopup.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
navigateToSuggestion(position);
}
});
mSuggestionsPopup.show();
}
// TextWatcher implementation
@Override
public void afterTextChanged(final Editable editableText) {
if (!mHasStartedNewOmniboxEditSession) {
mAutocomplete.resetSession();
mHasStartedNewOmniboxEditSession = true;
}
stopAutocomplete(false);
if (TextUtils.isEmpty(editableText)) {
dismissPopup();
} else {
assert mRequestSuggestions == null : "Multiple omnibox requests in flight.";
mRequestSuggestions = new Runnable() {
@Override
public void run() {
mRequestSuggestions = null;
mAutocomplete.start(
mToolbar.getCurrentTab().getProfile(),
mToolbar.getCurrentTab().getUrl(),
editableText.toString(), false);
}
};
new Handler().postDelayed(mRequestSuggestions, SUGGESTION_START_DELAY_MS);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mRequestSuggestions = null;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment