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.blimp.toolbar; | |
6 | |
7 import android.content.Context; | |
8 import android.util.AttributeSet; | |
9 import android.view.KeyEvent; | |
10 import android.view.View; | |
11 import android.view.View.OnKeyListener; | |
12 import android.widget.EditText; | |
13 | |
14 import org.chromium.base.ObserverList; | |
15 import org.chromium.ui.UiUtils; | |
16 | |
17 /** | |
18 * A {@link View} that allows users to enter text and URLs. | |
19 */ | |
20 public class UrlBar extends EditText implements OnKeyListener { | |
21 /** | |
22 * An observer that gets notified of relevant URL bar text changes. | |
23 */ | |
24 public interface UrlBarObserver { | |
25 /** | |
26 * Will be called when the user presses enter after typing something int
o this | |
27 * {@link EditText}. | |
28 * @param text The new text the user entered. | |
29 */ | |
30 void onNewTextEntered(String text); | |
31 } | |
32 | |
33 private final ObserverList<UrlBarObserver> mObservers = new ObserverList<Url
BarObserver>(); | |
34 | |
35 /** | |
36 * Builds a new {@link UrlBar}. | |
37 * @param context A {@link Context} instance. | |
38 * @param attrs An {@link AttributeSet} instance. | |
39 */ | |
40 public UrlBar(Context context, AttributeSet attrs) { | |
41 super(context, attrs); | |
42 setOnKeyListener(this); | |
43 } | |
44 | |
45 /** | |
46 * @return The current text of this {@link EditText}. Guaranteed to be not
{@code null}. | |
47 */ | |
48 public String getQueryText() { | |
49 return getEditableText() == null ? "" : getEditableText().toString(); | |
50 } | |
51 | |
52 /** | |
53 * @param observer An {@link UrlBarObserver} that will be notified of future
text entries. | |
54 */ | |
55 public void addUrlBarObserver(UrlBarObserver observer) { | |
56 mObservers.addObserver(observer); | |
57 } | |
58 | |
59 /** | |
60 * @param observer An {@link UrlBarObserver} that will no longer be notified
of future text | |
61 * entries. | |
62 */ | |
63 public void removeUrlBarObserver(UrlBarObserver observer) { | |
64 mObservers.removeObserver(observer); | |
65 } | |
66 | |
67 // OnKeyListener interface. | |
68 @Override | |
69 public boolean onKey(View view, int keyCode, KeyEvent event) { | |
70 if (event.getAction() == KeyEvent.ACTION_UP | |
71 && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER | |
72 || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) { | |
73 UiUtils.hideKeyboard(view); | |
74 clearFocus(); | |
75 for (UrlBarObserver observer : mObservers) { | |
76 observer.onNewTextEntered(getQueryText()); | |
77 } | |
78 } | |
79 return false; | |
80 } | |
81 } | |
OLD | NEW |