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.chrome.browser.omnibox; |
| 6 |
| 7 import android.animation.ObjectAnimator; |
| 8 import android.annotation.SuppressLint; |
| 9 import android.content.Context; |
| 10 import android.content.res.ColorStateList; |
| 11 import android.graphics.Canvas; |
| 12 import android.graphics.Rect; |
| 13 import android.text.Selection; |
| 14 import android.text.TextUtils; |
| 15 import android.util.AttributeSet; |
| 16 import android.view.KeyEvent; |
| 17 import android.view.MotionEvent; |
| 18 import android.view.TouchDelegate; |
| 19 import android.view.View; |
| 20 import android.view.ViewGroup; |
| 21 import android.view.WindowManager; |
| 22 |
| 23 import com.google.android.apps.chrome.R; |
| 24 |
| 25 import org.chromium.base.ApiCompatibilityUtils; |
| 26 import org.chromium.chrome.browser.Tab; |
| 27 import org.chromium.chrome.browser.WindowDelegate; |
| 28 import org.chromium.chrome.browser.appmenu.AppMenuButtonHelper; |
| 29 import org.chromium.chrome.browser.document.BrandColorUtils; |
| 30 import org.chromium.chrome.browser.ntp.NewTabPage; |
| 31 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 32 import org.chromium.chrome.browser.widget.TintedImageButton; |
| 33 import org.chromium.ui.UiUtils; |
| 34 |
| 35 /** |
| 36 * A location bar implementation specific for smaller/phone screens. |
| 37 */ |
| 38 public class LocationBarPhone extends LocationBarLayout { |
| 39 private static final int KEYBOARD_MODE_CHANGE_DELAY_MS = 300; |
| 40 private static final int KEYBOARD_HIDE_DELAY_MS = 150; |
| 41 |
| 42 private static final int ACTION_BUTTON_TOUCH_OVERFLOW_LEFT = 15; |
| 43 |
| 44 private View mFirstVisibleFocusedView; |
| 45 private View mIncognitoBadge; |
| 46 private View mUrlActionsContainer; |
| 47 private TintedImageButton mMenuButton; |
| 48 private int mIncognitoBadgePadding; |
| 49 private boolean mVoiceSearchEnabled; |
| 50 private boolean mUrlFocusChangeInProgress; |
| 51 private float mUrlFocusChangePercent; |
| 52 private Runnable mKeyboardResizeModeTask; |
| 53 private ObjectAnimator mOmniboxBackgroundAnimator; |
| 54 |
| 55 /** |
| 56 * Constructor used to inflate from XML. |
| 57 */ |
| 58 public LocationBarPhone(Context context, AttributeSet attrs) { |
| 59 super(context, attrs); |
| 60 } |
| 61 |
| 62 @Override |
| 63 protected void onFinishInflate() { |
| 64 super.onFinishInflate(); |
| 65 |
| 66 mFirstVisibleFocusedView = findViewById(R.id.url_container); |
| 67 mIncognitoBadge = findViewById(R.id.incognito_badge); |
| 68 mIncognitoBadgePadding = |
| 69 getResources().getDimensionPixelSize(R.dimen.location_bar_incogn
ito_badge_padding); |
| 70 |
| 71 mUrlActionsContainer = findViewById(R.id.url_action_container); |
| 72 Rect delegateArea = new Rect(); |
| 73 mUrlActionsContainer.getHitRect(delegateArea); |
| 74 delegateArea.left -= ACTION_BUTTON_TOUCH_OVERFLOW_LEFT; |
| 75 TouchDelegate touchDelegate = new TouchDelegate(delegateArea, mUrlAction
sContainer); |
| 76 assert mUrlActionsContainer.getParent() == this; |
| 77 setTouchDelegate(touchDelegate); |
| 78 |
| 79 mMenuButton = (TintedImageButton) findViewById(R.id.document_menu_button
); |
| 80 |
| 81 if (hasVisibleViewsAfterUrlBarWhenUnfocused()) mUrlActionsContainer.setV
isibility(VISIBLE); |
| 82 if (!showMenuButtonInOmnibox()) { |
| 83 ((ViewGroup) mMenuButton.getParent()).removeView(mMenuButton); |
| 84 } |
| 85 } |
| 86 |
| 87 @Override |
| 88 public void setMenuButtonHelper(final AppMenuButtonHelper helper) { |
| 89 super.setMenuButtonHelper(helper); |
| 90 mMenuButton.setOnTouchListener(new OnTouchListener() { |
| 91 @SuppressLint("ClickableViewAccessibility") |
| 92 @Override |
| 93 public boolean onTouch(View v, MotionEvent event) { |
| 94 return helper.onTouch(v, event); |
| 95 } |
| 96 }); |
| 97 mMenuButton.setOnKeyListener(new OnKeyListener() { |
| 98 @Override |
| 99 public boolean onKey(View view, int keyCode, KeyEvent event) { |
| 100 if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == Ke
yEvent.ACTION_UP) { |
| 101 return helper.onEnterKeyPress(view); |
| 102 } |
| 103 return false; |
| 104 } |
| 105 }); |
| 106 } |
| 107 |
| 108 @Override |
| 109 public View getMenuAnchor() { |
| 110 return mMenuButton; |
| 111 } |
| 112 |
| 113 @Override |
| 114 public void getContentRect(Rect outRect) { |
| 115 super.getContentRect(outRect); |
| 116 if (mIncognitoBadge.getVisibility() == View.GONE) return; |
| 117 |
| 118 if (!ApiCompatibilityUtils.isLayoutRtl(this)) { |
| 119 outRect.left += mIncognitoBadge.getWidth(); |
| 120 } else { |
| 121 outRect.right -= mIncognitoBadge.getWidth(); |
| 122 } |
| 123 } |
| 124 |
| 125 /** |
| 126 * @return The first view visible when the location bar is focused. |
| 127 */ |
| 128 public View getFirstViewVisibleWhenFocused() { |
| 129 return mFirstVisibleFocusedView; |
| 130 } |
| 131 |
| 132 /** |
| 133 * @return Whether there are visible views that are aligned following the Ur
l Bar when it |
| 134 * does not have foucs. |
| 135 */ |
| 136 public boolean hasVisibleViewsAfterUrlBarWhenUnfocused() { |
| 137 return showMenuButtonInOmnibox(); |
| 138 } |
| 139 |
| 140 /** |
| 141 * @return Whether the menu should be shown in the omnibox instead of outsid
e of it. |
| 142 */ |
| 143 public boolean showMenuButtonInOmnibox() { |
| 144 return FeatureUtilities.isDocumentMode(getContext()); |
| 145 } |
| 146 |
| 147 /** |
| 148 * Updates percentage of current the URL focus change animation. |
| 149 * @param percent 1.0 is 100% focused, 0 is completely unfocused. |
| 150 */ |
| 151 public void setUrlFocusChangePercent(float percent) { |
| 152 mUrlFocusChangePercent = percent; |
| 153 |
| 154 if (percent > 0f && !hasVisibleViewsAfterUrlBarWhenUnfocused()) { |
| 155 mUrlActionsContainer.setVisibility(VISIBLE); |
| 156 } |
| 157 |
| 158 mDeleteButton.setAlpha(percent); |
| 159 mMicButton.setAlpha(percent); |
| 160 if (showMenuButtonInOmnibox()) mMenuButton.setAlpha(1f - percent); |
| 161 |
| 162 updateDeleteButtonVisibility(); |
| 163 } |
| 164 |
| 165 @Override |
| 166 public void onUrlFocusChange(boolean hasFocus) { |
| 167 if (mOmniboxBackgroundAnimator != null && mOmniboxBackgroundAnimator.isR
unning()) { |
| 168 mOmniboxBackgroundAnimator.cancel(); |
| 169 mOmniboxBackgroundAnimator = null; |
| 170 } |
| 171 if (hasFocus) { |
| 172 // Remove the focus of this view once the URL field has taken focus
as this view no |
| 173 // longer needs it. |
| 174 setFocusable(false); |
| 175 setFocusableInTouchMode(false); |
| 176 } |
| 177 mUrlFocusChangeInProgress = true; |
| 178 super.onUrlFocusChange(hasFocus); |
| 179 } |
| 180 |
| 181 @Override |
| 182 protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 183 boolean needsCanvasRestore = false; |
| 184 if (child == mUrlContainer && mUrlActionsContainer.getVisibility() == VI
SIBLE) { |
| 185 canvas.save(); |
| 186 |
| 187 // Clip the URL bar contents to ensure they do not draw under the UR
L actions during |
| 188 // focus animations. Based on the RTL state of the location bar, th
e url actions |
| 189 // container can be on the left or right side, so clip accordingly. |
| 190 if (mUrlContainer.getLeft() < mUrlActionsContainer.getLeft()) { |
| 191 canvas.clipRect(0, 0, (int) mUrlActionsContainer.getX(), getBott
om()); |
| 192 } else { |
| 193 canvas.clipRect(mUrlActionsContainer.getX() + mUrlActionsContain
er.getWidth(), |
| 194 0, getWidth(), getBottom()); |
| 195 } |
| 196 needsCanvasRestore = true; |
| 197 } |
| 198 boolean retVal = super.drawChild(canvas, child, drawingTime); |
| 199 if (needsCanvasRestore) { |
| 200 canvas.restore(); |
| 201 } |
| 202 return retVal; |
| 203 } |
| 204 |
| 205 /** |
| 206 * Handles any actions to be performed after all other actions triggered by
the URL focus |
| 207 * change. This will be called after any animations are performed to transi
tion from one |
| 208 * focus state to the other. |
| 209 * @param hasFocus Whether the URL field has gained focus. |
| 210 */ |
| 211 public void finishUrlFocusChange(boolean hasFocus) { |
| 212 final WindowDelegate windowDelegate = getWindowDelegate(); |
| 213 if (!hasFocus) { |
| 214 // Remove the selection from the url text. The ending selection pos
ition |
| 215 // will determine the scroll position when the url field is restored
. If |
| 216 // we do not clear this, it will scroll to the end of the text when
you |
| 217 // enter/exit the tab stack. |
| 218 // We set the selection to 0 instead of removing the selection to av
oid a crash that |
| 219 // happens if you clear the selection instead. |
| 220 // |
| 221 // Triggering the bug happens by: |
| 222 // 1.) Selecting some portion of the URL (where the two selection ha
ndles |
| 223 // appear) |
| 224 // 2.) Trigger a text change in the URL bar (i.e. by triggering a ne
w URL load |
| 225 // by a command line intent) |
| 226 // 3.) Simultaneously moving one of the selection handles left and r
ight. This will |
| 227 // occasionally throw an AssertionError on the bounds of the sel
ection. |
| 228 Selection.setSelection(mUrlBar.getText(), 0); |
| 229 |
| 230 // The animation rendering may not yet be 100% complete and hiding t
he keyboard makes |
| 231 // the animation quite choppy. |
| 232 postDelayed(new Runnable() { |
| 233 @Override |
| 234 public void run() { |
| 235 UiUtils.hideKeyboard(mUrlBar); |
| 236 } |
| 237 }, KEYBOARD_HIDE_DELAY_MS); |
| 238 // Convert the keyboard back to resize mode (delay the change for an
arbitrary amount |
| 239 // of time in hopes the keyboard will be completely hidden before ma
king this change). |
| 240 if (mKeyboardResizeModeTask == null |
| 241 && windowDelegate.getWindowSoftInputMode() |
| 242 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESI
ZE) { |
| 243 mKeyboardResizeModeTask = new Runnable() { |
| 244 @Override |
| 245 public void run() { |
| 246 windowDelegate.setWindowSoftInputMode( |
| 247 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RES
IZE); |
| 248 mKeyboardResizeModeTask = null; |
| 249 } |
| 250 }; |
| 251 postDelayed(mKeyboardResizeModeTask, KEYBOARD_MODE_CHANGE_DELAY_
MS); |
| 252 } |
| 253 if (!hasVisibleViewsAfterUrlBarWhenUnfocused()) { |
| 254 mUrlActionsContainer.setVisibility(GONE); |
| 255 } |
| 256 } else { |
| 257 if (mKeyboardResizeModeTask != null) { |
| 258 removeCallbacks(mKeyboardResizeModeTask); |
| 259 mKeyboardResizeModeTask = null; |
| 260 } |
| 261 if (windowDelegate.getWindowSoftInputMode() |
| 262 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) { |
| 263 windowDelegate.setWindowSoftInputMode( |
| 264 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); |
| 265 } |
| 266 UiUtils.showKeyboard(mUrlBar); |
| 267 // As the position of the navigation icon has changed, ensure the su
ggestions are |
| 268 // updated to reflect the new position. |
| 269 if (getSuggestionList() != null && getSuggestionList().isShown()) { |
| 270 getSuggestionList().invalidateSuggestionViews(); |
| 271 } |
| 272 } |
| 273 mUrlFocusChangeInProgress = false; |
| 274 updateDeleteButtonVisibility(); |
| 275 |
| 276 NewTabPage ntp = getToolbarDataProvider().getNewTabPageForCurrentTab(); |
| 277 if (hasFocus && ntp != null && ntp.isLocationBarShownInNTP()) { |
| 278 fadeInOmniboxResultsContainerBackground(); |
| 279 } |
| 280 } |
| 281 |
| 282 @Override |
| 283 protected boolean shouldShowDeleteButton() { |
| 284 boolean hasText = !TextUtils.isEmpty(mUrlBar.getText()); |
| 285 return hasText && (mUrlBar.hasFocus() || mUrlFocusChangeInProgress); |
| 286 } |
| 287 |
| 288 @Override |
| 289 protected void updateDeleteButtonVisibility() { |
| 290 boolean enabled = shouldShowDeleteButton(); |
| 291 mDeleteButton.setEnabled(enabled); |
| 292 mDeleteButton.setVisibility(enabled ? VISIBLE : INVISIBLE); |
| 293 |
| 294 boolean showMicButton = mVoiceSearchEnabled && !enabled |
| 295 && (mUrlBar.hasFocus() || mUrlFocusChangeInProgress |
| 296 || mUrlFocusChangePercent > 0f); |
| 297 mMicButton.setVisibility(showMicButton ? VISIBLE : INVISIBLE); |
| 298 } |
| 299 |
| 300 @Override |
| 301 public void updateMicButtonState() { |
| 302 mVoiceSearchEnabled = isVoiceSearchEnabled(); |
| 303 mMicButton.setVisibility(mVoiceSearchEnabled && mUrlBar.hasFocus() |
| 304 && mDeleteButton.getVisibility() != VISIBLE ? VISIBLE : INVISIBL
E); |
| 305 } |
| 306 |
| 307 @Override |
| 308 protected void updateLocationBarIconContainerVisibility() { |
| 309 super.updateLocationBarIconContainerVisibility(); |
| 310 updateIncognitoBadgePadding(); |
| 311 } |
| 312 |
| 313 private void updateIncognitoBadgePadding() { |
| 314 // This can be triggered in the super.onFinishInflate, so we need to nul
l check in this |
| 315 // place only. |
| 316 if (mIncognitoBadge == null) return; |
| 317 |
| 318 if (findViewById(R.id.location_bar_icon).getVisibility() == GONE) { |
| 319 ApiCompatibilityUtils.setPaddingRelative( |
| 320 mIncognitoBadge, 0, 0, mIncognitoBadgePadding, 0); |
| 321 } else { |
| 322 ApiCompatibilityUtils.setPaddingRelative(mIncognitoBadge, 0, 0, 0, 0
); |
| 323 } |
| 324 } |
| 325 |
| 326 @Override |
| 327 public void updateVisualsForState() { |
| 328 super.updateVisualsForState(); |
| 329 |
| 330 Tab tab = getCurrentTab(); |
| 331 boolean isIncognito = tab != null && tab.isIncognito(); |
| 332 mIncognitoBadge.setVisibility(isIncognito ? VISIBLE : GONE); |
| 333 updateIncognitoBadgePadding(); |
| 334 |
| 335 if (showMenuButtonInOmnibox()) { |
| 336 boolean useLightDrawables = isIncognito; |
| 337 if (getToolbarDataProvider().isUsingBrandColor()) { |
| 338 int currentPrimaryColor = getToolbarDataProvider().getPrimaryCol
or(); |
| 339 useLightDrawables |= |
| 340 BrandColorUtils.shouldUseLightDrawablesForToolbar(curren
tPrimaryColor); |
| 341 } |
| 342 ColorStateList dark = getResources().getColorStateList(R.color.dark_
mode_tint); |
| 343 ColorStateList white = getResources().getColorStateList(R.color.ligh
t_mode_tint); |
| 344 mMenuButton.setTint(useLightDrawables ? white : dark); |
| 345 } |
| 346 } |
| 347 |
| 348 @Override |
| 349 protected boolean shouldAnimateIconChanges() { |
| 350 return super.shouldAnimateIconChanges() || mUrlFocusChangeInProgress; |
| 351 } |
| 352 |
| 353 @Override |
| 354 public void setLayoutDirection(int layoutDirection) { |
| 355 super.setLayoutDirection(layoutDirection); |
| 356 updateIncognitoBadgePadding(); |
| 357 } |
| 358 } |
OLD | NEW |