OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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.browser.omnibox; | 5 package org.chromium.chrome.browser.omnibox; |
6 | 6 |
7 import android.content.ClipData; | 7 import android.content.ClipData; |
8 import android.content.ClipboardManager; | 8 import android.content.ClipboardManager; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.res.ColorStateList; | 10 import android.content.res.ColorStateList; |
11 import android.content.res.Resources; | 11 import android.content.res.Resources; |
12 import android.graphics.Canvas; | 12 import android.graphics.Canvas; |
13 import android.graphics.Paint; | 13 import android.graphics.Paint; |
14 import android.graphics.Rect; | 14 import android.graphics.Rect; |
15 import android.os.SystemClock; | 15 import android.os.SystemClock; |
16 import android.text.Editable; | 16 import android.text.Editable; |
17 import android.text.Layout; | |
18 import android.text.Selection; | 17 import android.text.Selection; |
19 import android.text.Spanned; | 18 import android.text.Spanned; |
20 import android.text.TextUtils; | 19 import android.text.TextUtils; |
21 import android.text.style.ReplacementSpan; | 20 import android.text.style.ReplacementSpan; |
22 import android.util.AttributeSet; | 21 import android.util.AttributeSet; |
23 import android.view.GestureDetector; | 22 import android.view.GestureDetector; |
24 import android.view.KeyEvent; | 23 import android.view.KeyEvent; |
25 import android.view.MotionEvent; | 24 import android.view.MotionEvent; |
26 import android.view.View; | 25 import android.view.View; |
27 import android.view.accessibility.AccessibilityEvent; | 26 import android.view.accessibility.AccessibilityEvent; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 /** The contents of the URL that precede the path/query before formatting. * / | 65 /** The contents of the URL that precede the path/query before formatting. * / |
67 private String mOriginalUrlLocation; | 66 private String mOriginalUrlLocation; |
68 | 67 |
69 /** Overrides the text announced during accessibility events. */ | 68 /** Overrides the text announced during accessibility events. */ |
70 private String mAccessibilityTextOverride; | 69 private String mAccessibilityTextOverride; |
71 | 70 |
72 private boolean mShowKeyboardOnWindowFocus; | 71 private boolean mShowKeyboardOnWindowFocus; |
73 | 72 |
74 private boolean mFirstDrawComplete; | 73 private boolean mFirstDrawComplete; |
75 | 74 |
76 /** | |
77 * The text direction of the URL or query: LAYOUT_DIRECTION_LOCALE, LAYOUT_D IRECTION_LTR, or | |
78 * LAYOUT_DIRECTION_RTL. | |
79 * */ | |
80 private int mUrlDirection; | |
81 | |
82 private UrlBarDelegate mUrlBarDelegate; | 75 private UrlBarDelegate mUrlBarDelegate; |
83 | 76 |
84 private UrlDirectionListener mUrlDirectionListener; | |
85 | |
86 private final AutocompleteSpan mAutocompleteSpan; | 77 private final AutocompleteSpan mAutocompleteSpan; |
87 | 78 |
88 /** | 79 /** |
89 * The gesture detector is used to detect long presses. Long presses require special treatment | 80 * The gesture detector is used to detect long presses. Long presses require special treatment |
90 * because the URL bar has custom touch event handling. See: {@link #onTouch Event}. | 81 * because the URL bar has custom touch event handling. See: {@link #onTouch Event}. |
91 */ | 82 */ |
92 private final GestureDetector mGestureDetector; | 83 private final GestureDetector mGestureDetector; |
93 private boolean mFocused; | 84 private boolean mFocused; |
94 private boolean mAllowFocus = true; | 85 private boolean mAllowFocus = true; |
95 | 86 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 // guarantee that the text does contain the span currently as newly set text may have cleared | 119 // guarantee that the text does contain the span currently as newly set text may have cleared |
129 // this (and it the value will only be recalculated after the text has been changed). | 120 // this (and it the value will only be recalculated after the text has been changed). |
130 private boolean mDidEllipsizeTextHint; | 121 private boolean mDidEllipsizeTextHint; |
131 | 122 |
132 // Set to true when the URL bar text is modified programmatically. Initially set | 123 // Set to true when the URL bar text is modified programmatically. Initially set |
133 // to true until the old state has been loaded. | 124 // to true until the old state has been loaded. |
134 private boolean mIgnoreAutocomplete = true; | 125 private boolean mIgnoreAutocomplete = true; |
135 private boolean mLastUrlEditWasDelete; | 126 private boolean mLastUrlEditWasDelete; |
136 | 127 |
137 /** | 128 /** |
138 * Implement this to get updates when the direction of the text in the URL b ar changes. | |
139 * E.g. If the user is typing a URL, then erases it and starts typing a quer y in Arabic, | |
140 * the direction will change from left-to-right to right-to-left. | |
141 */ | |
142 interface UrlDirectionListener { | |
143 /** | |
144 * Called whenever the layout direction of the UrlBar changes. | |
145 * @param layoutDirection the new direction: android.view.View.LAYOUT_DI RECTION_LTR or | |
146 * android.view.View.LAYOUT_DIRECTION_RTL | |
147 */ | |
148 public void onUrlDirectionChanged(int layoutDirection); | |
149 } | |
150 | |
151 /** | |
152 * Delegate used to communicate with the content side and the parent layout. | 129 * Delegate used to communicate with the content side and the parent layout. |
153 */ | 130 */ |
154 public interface UrlBarDelegate { | 131 public interface UrlBarDelegate { |
155 /** | 132 /** |
156 * @return The current active {@link Tab}. | 133 * @return The current active {@link Tab}. |
157 */ | 134 */ |
158 Tab getCurrentTab(); | 135 Tab getCurrentTab(); |
159 | 136 |
160 /** | 137 /** |
161 * Called when the text state has changed and the autocomplete suggestio ns should be | 138 * Called when the text state has changed and the autocomplete suggestio ns should be |
(...skipping 26 matching lines...) Expand all Loading... | |
188 | 165 |
189 mLightDefaultTextColor = | 166 mLightDefaultTextColor = |
190 ApiCompatibilityUtils.getColor(resources, R.color.url_emphasis_l ight_default_text); | 167 ApiCompatibilityUtils.getColor(resources, R.color.url_emphasis_l ight_default_text); |
191 mLightHintColor = | 168 mLightHintColor = |
192 ApiCompatibilityUtils.getColor(resources, R.color.locationbar_li ght_hint_text); | 169 ApiCompatibilityUtils.getColor(resources, R.color.locationbar_li ght_hint_text); |
193 mLightHighlightColor = ApiCompatibilityUtils.getColor(resources, | 170 mLightHighlightColor = ApiCompatibilityUtils.getColor(resources, |
194 R.color.locationbar_light_selection_color); | 171 R.color.locationbar_light_selection_color); |
195 | 172 |
196 setUseDarkTextColors(true); | 173 setUseDarkTextColors(true); |
197 | 174 |
198 mUrlDirection = LAYOUT_DIRECTION_LOCALE; | 175 // Force left-to-right rendering at the paragraph level. Right-to-left r uns are still |
176 // rendered RTL, but will not flip the whole URL around. For example (if "ABC" is Hebrew), | |
177 // this will render "ABC.com" as "CBA.com", rather than "com.CBA". This is consistent with | |
178 // OmniboxViewViews on desktop. | |
179 setTextDirection(TEXT_DIRECTION_LTR); | |
Ted C
2016/05/18 17:01:01
Only available in API 17+
https://developer.andro
Matt Giuca
2016/06/09 08:39:12
Done.
| |
180 setTextAlignment(TEXT_ALIGNMENT_VIEW_START); | |
Ted C
2016/05/18 17:01:01
Same:
https://code.google.com/p/chromium/codesear
Matt Giuca
2016/06/09 08:39:12
Done.
| |
181 | |
199 mAutocompleteSpan = new AutocompleteSpan(); | 182 mAutocompleteSpan = new AutocompleteSpan(); |
200 | 183 |
201 // The URL Bar is derived from an text edit class, and as such is focusa ble by | 184 // The URL Bar is derived from an text edit class, and as such is focusa ble by |
202 // default. This means that if it is created before the first draw of th e UI it | 185 // default. This means that if it is created before the first draw of th e UI it |
203 // will (as the only focusable element of the UI) get focus on the first draw. | 186 // will (as the only focusable element of the UI) get focus on the first draw. |
204 // We react to this by greying out the tab area and bringing up the keyb oard, | 187 // We react to this by greying out the tab area and bringing up the keyb oard, |
205 // which we don't want to do at startup. Prevent this by disabling focus until | 188 // which we don't want to do at startup. Prevent this by disabling focus until |
206 // the first draw. | 189 // the first draw. |
207 setFocusable(false); | 190 setFocusable(false); |
208 setFocusableInTouchMode(false); | 191 setFocusableInTouchMode(false); |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 // the constructor) so we want to make the URL bar focusable so that | 543 // the constructor) so we want to make the URL bar focusable so that |
561 // touches etc. activate it. | 544 // touches etc. activate it. |
562 setFocusable(mAllowFocus); | 545 setFocusable(mAllowFocus); |
563 setFocusableInTouchMode(mAllowFocus); | 546 setFocusableInTouchMode(mAllowFocus); |
564 | 547 |
565 // The URL bar will now react correctly to a focus change event | 548 // The URL bar will now react correctly to a focus change event |
566 if (mOmniboxLivenessListener != null) { | 549 if (mOmniboxLivenessListener != null) { |
567 mOmniboxLivenessListener.onOmniboxInteractive(); | 550 mOmniboxLivenessListener.onOmniboxInteractive(); |
568 } | 551 } |
569 } | 552 } |
570 | |
571 // Notify listeners if the URL's direction has changed. | |
572 updateUrlDirection(); | |
573 } | |
574 | |
575 /** | |
576 * If the direction of the URL has changed, update mUrlDirection and notify the | |
577 * UrlDirectionListeners. | |
578 */ | |
579 private void updateUrlDirection() { | |
580 Layout layout = getLayout(); | |
581 if (layout == null) return; | |
582 | |
583 int urlDirection; | |
584 if (length() == 0) { | |
585 urlDirection = LAYOUT_DIRECTION_LOCALE; | |
586 } else if (layout.getParagraphDirection(0) == Layout.DIR_LEFT_TO_RIGHT) { | |
587 urlDirection = LAYOUT_DIRECTION_LTR; | |
588 } else { | |
589 urlDirection = LAYOUT_DIRECTION_RTL; | |
590 } | |
591 | |
592 if (urlDirection != mUrlDirection) { | |
593 mUrlDirection = urlDirection; | |
594 if (mUrlDirectionListener != null) { | |
595 mUrlDirectionListener.onUrlDirectionChanged(urlDirection); | |
596 } | |
597 } | |
598 } | 553 } |
599 | 554 |
600 /** | 555 /** |
601 * @return The text direction of the URL, e.g. LAYOUT_DIRECTION_LTR. | |
602 */ | |
603 public int getUrlDirection() { | |
604 return mUrlDirection; | |
605 } | |
606 | |
607 /** | |
608 * Sets the listener for changes in the url bar's layout direction. Also cal ls | |
609 * onUrlDirectionChanged() immediately on the listener. | |
610 * | |
611 * @param listener The UrlDirectionListener to receive callbacks when the ur l direction changes, | |
612 * or null to unregister any previously registered listener. | |
613 */ | |
614 public void setUrlDirectionListener(UrlDirectionListener listener) { | |
615 mUrlDirectionListener = listener; | |
616 if (mUrlDirectionListener != null) { | |
617 mUrlDirectionListener.onUrlDirectionChanged(mUrlDirection); | |
618 } | |
619 } | |
620 | |
621 /** | |
622 * Set the url delegate to handle communication from the {@link UrlBar} to t he rest of the UI. | 556 * Set the url delegate to handle communication from the {@link UrlBar} to t he rest of the UI. |
623 * @param delegate The {@link UrlBarDelegate} to be used. | 557 * @param delegate The {@link UrlBarDelegate} to be used. |
624 */ | 558 */ |
625 public void setDelegate(UrlBarDelegate delegate) { | 559 public void setDelegate(UrlBarDelegate delegate) { |
626 mUrlBarDelegate = delegate; | 560 mUrlBarDelegate = delegate; |
627 } | 561 } |
628 | 562 |
629 /** | 563 /** |
630 * Set {@link OmniboxLivenessListener} to be used for receiving interaction related messages | 564 * Set {@link OmniboxLivenessListener} to be used for receiving interaction related messages |
631 * during startup. | 565 * during startup. |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1168 return (int) paint.measureText(ELLIPSIS); | 1102 return (int) paint.measureText(ELLIPSIS); |
1169 } | 1103 } |
1170 | 1104 |
1171 @Override | 1105 @Override |
1172 public void draw(Canvas canvas, CharSequence text, int start, int end, | 1106 public void draw(Canvas canvas, CharSequence text, int start, int end, |
1173 float x, int top, int y, int bottom, Paint paint) { | 1107 float x, int top, int y, int bottom, Paint paint) { |
1174 canvas.drawText(ELLIPSIS, x, y, paint); | 1108 canvas.drawText(ELLIPSIS, x, y, paint); |
1175 } | 1109 } |
1176 } | 1110 } |
1177 } | 1111 } |
OLD | NEW |