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.ntp; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.graphics.Canvas; |
| 9 import android.os.Build; |
| 10 import android.util.AttributeSet; |
| 11 import android.util.Log; |
| 12 import android.view.GestureDetector; |
| 13 import android.view.KeyEvent; |
| 14 import android.view.MotionEvent; |
| 15 import android.view.View; |
| 16 import android.view.inputmethod.EditorInfo; |
| 17 import android.view.inputmethod.InputConnection; |
| 18 import android.widget.ScrollView; |
| 19 |
| 20 import com.google.android.apps.chrome.R; |
| 21 |
| 22 import org.chromium.chrome.browser.widget.FadingShadow; |
| 23 |
| 24 /** |
| 25 * Simple wrapper on top of a ScrollView that will acquire focus when tapped. E
nsures the |
| 26 * New Tab page receives focus when clicked. |
| 27 */ |
| 28 public class NewTabScrollView extends ScrollView { |
| 29 |
| 30 private static final String TAG = "NewTabScrollView"; |
| 31 |
| 32 /** |
| 33 * Listener for scroll changes. |
| 34 */ |
| 35 public interface OnScrollListener { |
| 36 /** |
| 37 * Triggered when the scroll changes. See ScrollView#onScrollChanged fo
r more |
| 38 * details. |
| 39 */ |
| 40 void onScrollChanged(int l, int t, int oldl, int oldt); |
| 41 } |
| 42 |
| 43 private GestureDetector mGestureDetector; |
| 44 private OnScrollListener mOnScrollListener; |
| 45 |
| 46 private NewTabPageLayout mNewTabPageLayout; |
| 47 |
| 48 private FadingShadow mFadingShadow; |
| 49 |
| 50 /** |
| 51 * Constructor needed to inflate from XML. |
| 52 */ |
| 53 public NewTabScrollView(Context context, AttributeSet attrs) { |
| 54 super(context, attrs); |
| 55 |
| 56 mGestureDetector = new GestureDetector( |
| 57 getContext(), new GestureDetector.SimpleOnGestureListener() { |
| 58 @Override |
| 59 public boolean onSingleTapUp(MotionEvent e) { |
| 60 boolean retVal = super.onSingleTapUp(e); |
| 61 requestFocus(); |
| 62 return retVal; |
| 63 } |
| 64 }); |
| 65 } |
| 66 |
| 67 /** |
| 68 * Enables drawing a shadow at the bottom of the view when the view's conten
t extends beyond |
| 69 * the bottom of the view. This is exactly the same as a fading edge, except
that the shadow |
| 70 * color can have an alpha component, whereas a fading edge color must be op
aque. |
| 71 * |
| 72 * @param shadowColor The color of the shadow, e.g. 0x11000000. |
| 73 */ |
| 74 public void enableBottomShadow(int shadowColor) { |
| 75 mFadingShadow = new FadingShadow(shadowColor); |
| 76 setFadingEdgeLength(getResources().getDimensionPixelSize(R.dimen.ntp_sha
dow_height)); |
| 77 } |
| 78 |
| 79 @Override |
| 80 protected void onFinishInflate() { |
| 81 super.onFinishInflate(); |
| 82 |
| 83 View child = getChildAt(0); |
| 84 if (child instanceof NewTabPageLayout) mNewTabPageLayout = (NewTabPageLa
yout) child; |
| 85 } |
| 86 |
| 87 @Override |
| 88 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 89 if (mNewTabPageLayout != null) { |
| 90 mNewTabPageLayout.setParentScrollViewportHeight( |
| 91 MeasureSpec.getSize(heightMeasureSpec)); |
| 92 } |
| 93 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 94 } |
| 95 |
| 96 @Override |
| 97 public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 98 mGestureDetector.onTouchEvent(ev); |
| 99 return super.onInterceptTouchEvent(ev); |
| 100 } |
| 101 |
| 102 @Override |
| 103 public boolean onTouchEvent(MotionEvent ev) { |
| 104 // Action down would already have been handled in onInterceptTouchEvent |
| 105 if (ev.getActionMasked() != MotionEvent.ACTION_DOWN) { |
| 106 mGestureDetector.onTouchEvent(ev); |
| 107 } |
| 108 try { |
| 109 return super.onTouchEvent(ev); |
| 110 } catch (IllegalArgumentException ex) { |
| 111 // In JB MR0 and earlier, an ACTION_MOVE that is not preceded by an
ACTION_DOWN event |
| 112 // causes a crash. This can happen under normal circumstances (e.g.
going back to the |
| 113 // NTP while a finger is down on the screen) and should not crash. T
he most reliable way |
| 114 // to prevent this crash is to catch the exception. http://crbug.com
/293822 |
| 115 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 |
| 116 && ev.getActionMasked() == MotionEvent.ACTION_MOVE |
| 117 && "pointerIndex out of range".equals(ex.getMessage())) { |
| 118 Log.d(TAG, "Ignoring pointerIndex out of range exception."); |
| 119 return true; |
| 120 } |
| 121 throw ex; |
| 122 } |
| 123 } |
| 124 |
| 125 /** |
| 126 * Sets the listener to be notified of scroll changes. |
| 127 * @param listener The listener to be updated on scroll changes. |
| 128 */ |
| 129 public void setOnScrollListener(OnScrollListener listener) { |
| 130 mOnScrollListener = listener; |
| 131 } |
| 132 |
| 133 @Override |
| 134 protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 135 super.onScrollChanged(l, t, oldl, oldt); |
| 136 if (mOnScrollListener != null) mOnScrollListener.onScrollChanged(l, t, o
ldl, oldt); |
| 137 } |
| 138 |
| 139 @Override |
| 140 public void focusableViewAvailable(View v) { |
| 141 // To avoid odd jumps during NTP animation transitions, we do not attemp
t to give focus |
| 142 // to child views if this scroll view already has focus. |
| 143 if (hasFocus()) return; |
| 144 super.focusableViewAvailable(v); |
| 145 } |
| 146 |
| 147 @Override |
| 148 public boolean executeKeyEvent(KeyEvent event) { |
| 149 // Ignore all key events except arrow keys and spacebar. Otherwise, the
ScrollView consumes |
| 150 // unwanted events (including the hardware menu button and app-level key
board shortcuts). |
| 151 // http://crbug.com/308322 |
| 152 switch (event.getKeyCode()) { |
| 153 case KeyEvent.KEYCODE_DPAD_UP: |
| 154 case KeyEvent.KEYCODE_DPAD_DOWN: |
| 155 case KeyEvent.KEYCODE_SPACE: |
| 156 return super.executeKeyEvent(event); |
| 157 default: |
| 158 return false; |
| 159 } |
| 160 } |
| 161 |
| 162 @Override |
| 163 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| 164 // Fixes lanscape transitions when unfocusing the URL bar: crbug.com/288
546 |
| 165 outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN; |
| 166 return super.onCreateInputConnection(outAttrs); |
| 167 } |
| 168 |
| 169 @Override |
| 170 protected void dispatchDraw(Canvas canvas) { |
| 171 super.dispatchDraw(canvas); |
| 172 if (mFadingShadow != null) { |
| 173 setVerticalFadingEdgeEnabled(true); |
| 174 float shadowStrength = getBottomFadingEdgeStrength(); |
| 175 float shadowHeight = getVerticalFadingEdgeLength(); |
| 176 setVerticalFadingEdgeEnabled(false); |
| 177 mFadingShadow.drawShadow(this, canvas, FadingShadow.POSITION_BOTTOM, |
| 178 shadowHeight, shadowStrength); |
| 179 } |
| 180 } |
| 181 } |
OLD | NEW |