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.Animator; |
| 8 import android.animation.AnimatorListenerAdapter; |
| 9 import android.animation.ObjectAnimator; |
| 10 import android.content.Context; |
| 11 import android.graphics.Rect; |
| 12 import android.text.Selection; |
| 13 import android.util.AttributeSet; |
| 14 import android.util.Property; |
| 15 import android.view.MotionEvent; |
| 16 import android.view.View; |
| 17 import android.view.WindowManager; |
| 18 |
| 19 import com.google.android.apps.chrome.R; |
| 20 |
| 21 import org.chromium.base.ApiCompatibilityUtils; |
| 22 import org.chromium.chrome.browser.ntp.NewTabPage; |
| 23 import org.chromium.ui.UiUtils; |
| 24 |
| 25 /** |
| 26 * Location bar for tablet form factors. |
| 27 */ |
| 28 public class LocationBarTablet extends LocationBarLayout { |
| 29 |
| 30 private static final int KEYBOARD_MODE_CHANGE_DELAY_MS = 300; |
| 31 private static final long MAX_NTP_KEYBOARD_FOCUS_DURATION_MS = 200; |
| 32 |
| 33 private final Property<LocationBarTablet, Float> mUrlFocusChangePercentPrope
rty = |
| 34 new Property<LocationBarTablet, Float>(Float.class, "") { |
| 35 @Override |
| 36 public Float get(LocationBarTablet object) { |
| 37 return object.mUrlFocusChangePercent; |
| 38 } |
| 39 |
| 40 @Override |
| 41 public void set(LocationBarTablet object, Float value) { |
| 42 setUrlFocusChangePercent(value); |
| 43 } |
| 44 }; |
| 45 |
| 46 private final Runnable mKeyboardResizeModeTask = new Runnable() { |
| 47 @Override |
| 48 public void run() { |
| 49 getWindowDelegate().setWindowSoftInputMode( |
| 50 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); |
| 51 } |
| 52 }; |
| 53 |
| 54 private View mBookmarkButton; |
| 55 private float mUrlFocusChangePercent; |
| 56 private Animator mUrlFocusChangeAnimator; |
| 57 private View[] mTargets; |
| 58 private final Rect mCachedTargetBounds = new Rect(); |
| 59 |
| 60 /** |
| 61 * Constructor used to inflate from XML. |
| 62 */ |
| 63 public LocationBarTablet(Context context, AttributeSet attrs) { |
| 64 super(context, attrs); |
| 65 } |
| 66 |
| 67 @Override |
| 68 protected void onFinishInflate() { |
| 69 super.onFinishInflate(); |
| 70 |
| 71 mBookmarkButton = findViewById(R.id.bookmark_button); |
| 72 mTargets = new View[] { mUrlBar, mDeleteButton }; |
| 73 } |
| 74 |
| 75 @Override |
| 76 public boolean onTouchEvent(MotionEvent event) { |
| 77 if (mTargets == null) return true; |
| 78 |
| 79 View selectedTarget = null; |
| 80 float selectedDistance = 0; |
| 81 // newX and newY are in the coordinates of the selectedTarget. |
| 82 float newX = 0; |
| 83 float newY = 0; |
| 84 for (View target : mTargets) { |
| 85 if (!target.isShown()) continue; |
| 86 |
| 87 mCachedTargetBounds.set(0, 0, target.getWidth(), target.getHeight())
; |
| 88 offsetDescendantRectToMyCoords(target, mCachedTargetBounds); |
| 89 float x = event.getX(); |
| 90 float y = event.getY(); |
| 91 float dx = distanceToRange( |
| 92 mCachedTargetBounds.left, mCachedTargetBounds.right, x); |
| 93 float dy = distanceToRange( |
| 94 mCachedTargetBounds.top, mCachedTargetBounds.bottom, y); |
| 95 float distance = Math.abs(dx) + Math.abs(dy); |
| 96 if (selectedTarget == null || distance < selectedDistance) { |
| 97 selectedTarget = target; |
| 98 selectedDistance = distance; |
| 99 newX = x + dx; |
| 100 newY = y + dy; |
| 101 } |
| 102 } |
| 103 |
| 104 if (selectedTarget == null) return false; |
| 105 |
| 106 event.setLocation(newX, newY); |
| 107 return selectedTarget.onTouchEvent(event); |
| 108 } |
| 109 |
| 110 // Returns amount by which to adjust to move value inside the given range. |
| 111 private static float distanceToRange(float min, float max, float value) { |
| 112 return value < min ? (min - value) : value > max ? (max - value) : 0; |
| 113 } |
| 114 |
| 115 @Override |
| 116 public void onUrlFocusChange(final boolean hasFocus) { |
| 117 super.onUrlFocusChange(hasFocus); |
| 118 |
| 119 removeCallbacks(mKeyboardResizeModeTask); |
| 120 |
| 121 if (mUrlFocusChangeAnimator != null && mUrlFocusChangeAnimator.isRunning
()) { |
| 122 mUrlFocusChangeAnimator.cancel(); |
| 123 mUrlFocusChangeAnimator = null; |
| 124 } |
| 125 |
| 126 if (getToolbarDataProvider().getNewTabPageForCurrentTab() == null) { |
| 127 finishUrlFocusChange(hasFocus); |
| 128 return; |
| 129 } |
| 130 |
| 131 Rect rootViewBounds = new Rect(); |
| 132 getRootView().getLocalVisibleRect(rootViewBounds); |
| 133 float screenSizeRatio = (rootViewBounds.height() |
| 134 / (float) (Math.max(rootViewBounds.height(), rootViewBounds.widt
h()))); |
| 135 mUrlFocusChangeAnimator = |
| 136 ObjectAnimator.ofFloat(this, mUrlFocusChangePercentProperty, has
Focus ? 1f : 0f); |
| 137 mUrlFocusChangeAnimator.setDuration( |
| 138 (long) (MAX_NTP_KEYBOARD_FOCUS_DURATION_MS * screenSizeRatio)); |
| 139 mUrlFocusChangeAnimator.addListener(new AnimatorListenerAdapter() { |
| 140 private boolean mIsCancelled; |
| 141 |
| 142 @Override |
| 143 public void onAnimationCancel(Animator animation) { |
| 144 mIsCancelled = true; |
| 145 } |
| 146 |
| 147 @Override |
| 148 public void onAnimationEnd(Animator animation) { |
| 149 if (mIsCancelled) return; |
| 150 finishUrlFocusChange(hasFocus); |
| 151 } |
| 152 }); |
| 153 mUrlFocusChangeAnimator.start(); |
| 154 } |
| 155 |
| 156 private void finishUrlFocusChange(boolean hasFocus) { |
| 157 if (hasFocus) { |
| 158 if (mSecurityButton.getVisibility() == VISIBLE) mSecurityButton.setV
isibility(GONE); |
| 159 if (getWindowDelegate().getWindowSoftInputMode() |
| 160 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) { |
| 161 getWindowDelegate().setWindowSoftInputMode( |
| 162 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); |
| 163 } |
| 164 UiUtils.showKeyboard(mUrlBar); |
| 165 } else { |
| 166 if (mSecurityButton.getVisibility() == GONE |
| 167 && mSecurityButton.getDrawable() != null |
| 168 && mSecurityButton.getDrawable().getIntrinsicWidth() > 0 |
| 169 && mSecurityButton.getDrawable().getIntrinsicHeight() > 0) { |
| 170 mSecurityButton.setVisibility(VISIBLE); |
| 171 } |
| 172 UiUtils.hideKeyboard(mUrlBar); |
| 173 Selection.setSelection(mUrlBar.getText(), 0); |
| 174 // Convert the keyboard back to resize mode (delay the change for an
arbitrary |
| 175 // amount of time in hopes the keyboard will be completely hidden be
fore making |
| 176 // this change). |
| 177 if (getWindowDelegate().getWindowSoftInputMode() |
| 178 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) { |
| 179 postDelayed(mKeyboardResizeModeTask, KEYBOARD_MODE_CHANGE_DELAY_
MS); |
| 180 } |
| 181 } |
| 182 } |
| 183 |
| 184 /** |
| 185 * Updates percentage of current the URL focus change animation. |
| 186 * @param percent 1.0 is 100% focused, 0 is completely unfocused. |
| 187 */ |
| 188 private void setUrlFocusChangePercent(float percent) { |
| 189 mUrlFocusChangePercent = percent; |
| 190 |
| 191 NewTabPage ntp = getToolbarDataProvider().getNewTabPageForCurrentTab(); |
| 192 if (ntp != null) ntp.setUrlFocusChangeAnimationPercent(percent); |
| 193 } |
| 194 |
| 195 @Override |
| 196 protected void updateDeleteButtonVisibility() { |
| 197 boolean enabled = shouldShowDeleteButton(); |
| 198 mDeleteButton.setVisibility(enabled ? VISIBLE : GONE); |
| 199 mBookmarkButton.setVisibility(enabled ? View.GONE : View.VISIBLE); |
| 200 } |
| 201 |
| 202 @Override |
| 203 protected void updateLayoutParams() { |
| 204 // Calculate the bookmark/delete button margins. |
| 205 final MarginLayoutParams micLayoutParams = |
| 206 (MarginLayoutParams) mMicButton.getLayoutParams(); |
| 207 int micSpace = ApiCompatibilityUtils.getMarginEnd(micLayoutParams); |
| 208 if (mMicButton.getVisibility() != View.GONE) micSpace += mMicButton.getW
idth(); |
| 209 |
| 210 final MarginLayoutParams deleteLayoutParams = |
| 211 (MarginLayoutParams) mDeleteButton.getLayoutParams(); |
| 212 final MarginLayoutParams bookmarkLayoutParams = |
| 213 (MarginLayoutParams) mBookmarkButton.getLayoutParams(); |
| 214 |
| 215 ApiCompatibilityUtils.setMarginEnd(deleteLayoutParams, micSpace); |
| 216 ApiCompatibilityUtils.setMarginEnd(bookmarkLayoutParams, micSpace); |
| 217 |
| 218 mDeleteButton.setLayoutParams(deleteLayoutParams); |
| 219 mBookmarkButton.setLayoutParams(bookmarkLayoutParams); |
| 220 |
| 221 super.updateLayoutParams(); |
| 222 } |
| 223 } |
OLD | NEW |