Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(551)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabScrollView.java

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

Powered by Google App Engine
This is Rietveld 408576698