| 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.ntp; | 5 package org.chromium.chrome.browser.ntp; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
| 9 import android.util.AttributeSet; | 9 import android.util.AttributeSet; |
| 10 import android.view.View; | 10 import android.view.View; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 */ | 54 */ |
| 55 public NewTabPageLayout(Context context, AttributeSet attrs) { | 55 public NewTabPageLayout(Context context, AttributeSet attrs) { |
| 56 super(context, attrs); | 56 super(context, attrs); |
| 57 Resources res = getResources(); | 57 Resources res = getResources(); |
| 58 float density = res.getDisplayMetrics().density; | 58 float density = res.getDisplayMetrics().density; |
| 59 mTopSpacerIdealHeight = Math.round(density * TOP_SPACER_HEIGHT_DP); | 59 mTopSpacerIdealHeight = Math.round(density * TOP_SPACER_HEIGHT_DP); |
| 60 mMiddleSpacerIdealHeight = Math.round(density * MIDDLE_SPACER_HEIGHT_DP)
; | 60 mMiddleSpacerIdealHeight = Math.round(density * MIDDLE_SPACER_HEIGHT_DP)
; |
| 61 mBottomSpacerIdealHeight = Math.round(density * BOTTOM_SPACER_HEIGHT_DP)
; | 61 mBottomSpacerIdealHeight = Math.round(density * BOTTOM_SPACER_HEIGHT_DP)
; |
| 62 mTotalSpacerIdealHeight = Math.round(density * TOTAL_SPACER_HEIGHT_DP); | 62 mTotalSpacerIdealHeight = Math.round(density * TOTAL_SPACER_HEIGHT_DP); |
| 63 mMostVisitedLayoutBleed = res.getDimensionPixelSize(R.dimen.most_visited
_layout_bleed); | 63 mMostVisitedLayoutBleed = res.getDimensionPixelSize(R.dimen.most_visited
_layout_bleed); |
| 64 | 64 mPeekingCardHeight = |
| 65 mPeekingCardHeight = getResources() | 65 res.getDimensionPixelSize(R.dimen.snippets_padding_and_peeking_c
ard_height); |
| 66 .getDimensionPixelSize(R.dimen.snippets_padding_and_peeking_card
_height); | 66 mTabStripHeight = res.getDimensionPixelSize(R.dimen.tab_strip_height); |
| 67 mTabStripHeight = getResources().getDimensionPixelSize(R.dimen.tab_strip
_height); | |
| 68 } | 67 } |
| 69 | 68 |
| 70 @Override | 69 @Override |
| 71 protected void onFinishInflate() { | 70 protected void onFinishInflate() { |
| 72 super.onFinishInflate(); | 71 super.onFinishInflate(); |
| 73 mTopSpacer = findViewById(R.id.ntp_top_spacer); | 72 mTopSpacer = findViewById(R.id.ntp_top_spacer); |
| 74 mMiddleSpacer = findViewById(R.id.ntp_middle_spacer); | 73 mMiddleSpacer = findViewById(R.id.ntp_middle_spacer); |
| 75 mBottomSpacer = findViewById(R.id.ntp_bottom_spacer); | 74 mBottomSpacer = findViewById(R.id.ntp_bottom_spacer); |
| 76 mScrollCompensationSpacer = findViewById(R.id.ntp_scroll_spacer); | 75 mScrollCompensationSpacer = findViewById(R.id.ntp_scroll_spacer); |
| 77 mSearchProviderLogoView = (LogoView) findViewById(R.id.search_provider_l
ogo); | 76 mSearchProviderLogoView = (LogoView) findViewById(R.id.search_provider_l
ogo); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 95 /** | 94 /** |
| 96 * Sets whether the cards UI is enabled. | 95 * Sets whether the cards UI is enabled. |
| 97 * This view assumes that a peeking card will always be present when the car
ds UI is used. | 96 * This view assumes that a peeking card will always be present when the car
ds UI is used. |
| 98 */ | 97 */ |
| 99 public void setUseCardsUiEnabled(boolean useCardsUi) { | 98 public void setUseCardsUiEnabled(boolean useCardsUi) { |
| 100 mCardsUiEnabled = useCardsUi; | 99 mCardsUiEnabled = useCardsUi; |
| 101 } | 100 } |
| 102 | 101 |
| 103 @Override | 102 @Override |
| 104 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | 103 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 104 // Remove the scroll spacer from the layout so the weighted children can
be measured |
| 105 // correctly. |
| 105 mScrollCompensationSpacer.setVisibility(View.GONE); | 106 mScrollCompensationSpacer.setVisibility(View.GONE); |
| 106 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | 107 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 107 | 108 |
| 108 if (getMeasuredHeight() > mParentViewportHeight) { | 109 if (getMeasuredHeight() > mParentViewportHeight) { |
| 109 // This layout is bigger than its parent's viewport, so the user wil
l need to scroll | 110 // This layout is bigger than its parent's viewport, so the user wil
l need to scroll |
| 110 // to see all of it. Extra spacing should be added at the bottom so
the user can scroll | 111 // to see all of it. Extra spacing should be added at the bottom so
the user can scroll |
| 111 // until Most Visited is at the top. | 112 // until Most Visited is at the top. |
| 112 | 113 |
| 113 // All the spacers have height 0 since they use weights to set heigh
t. | 114 // The top, middle, and bottom spacers should have a measured height
of 0 at this point |
| 115 // since they use weights to set height, and there was no extra spac
e. |
| 114 assert mTopSpacer.getMeasuredHeight() == 0; | 116 assert mTopSpacer.getMeasuredHeight() == 0; |
| 117 assert mMiddleSpacer.getMeasuredHeight() == 0; |
| 118 assert mBottomSpacer.getMeasuredHeight() == 0; |
| 115 | 119 |
| 116 final int topOfMostVisited = calculateTopOfMostVisited(); | 120 final int topOfMostVisited = calculateTopOfMostVisited(); |
| 117 final int belowTheFoldHeight = getMeasuredHeight() - mParentViewport
Height; | 121 final int belowTheFoldHeight = getMeasuredHeight() - mParentViewport
Height; |
| 118 if (belowTheFoldHeight < topOfMostVisited) { | 122 if (belowTheFoldHeight < topOfMostVisited) { |
| 119 mScrollCompensationSpacer.getLayoutParams().height = | 123 mScrollCompensationSpacer.getLayoutParams().height = |
| 120 topOfMostVisited - belowTheFoldHeight; | 124 topOfMostVisited - belowTheFoldHeight; |
| 121 | 125 |
| 122 if (mCardsUiEnabled) { | 126 if (mCardsUiEnabled) { |
| 123 // If we have a peeking card, allow that to show at the bott
om of the screen. | 127 // If we have a peeking card, allow that to show at the bott
om of the screen. |
| 124 mScrollCompensationSpacer.getLayoutParams().height -= mPeeki
ngCardHeight; | 128 mScrollCompensationSpacer.getLayoutParams().height -= mPeeki
ngCardHeight; |
| 125 } | 129 } |
| 126 | 130 |
| 131 // Include the scroll spacer in the layout and call super.onMeas
ure again so it is |
| 132 // measured. |
| 127 mScrollCompensationSpacer.setVisibility(View.INVISIBLE); | 133 mScrollCompensationSpacer.setVisibility(View.INVISIBLE); |
| 128 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | 134 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 129 } | 135 } |
| 130 } else { | 136 } else { |
| 131 // This layout is smaller than it's parent viewport, redistribute th
e extra space. | 137 // This layout is smaller than or equal to its parent viewport. Redi
stribute any |
| 138 // weighted space. |
| 132 if (mCardsUiEnabled) { | 139 if (mCardsUiEnabled) { |
| 133 getLayoutParams().height = Math.max(getMeasuredHeight(), | 140 // Call super.onMeasure with mode EXACTLY and the target height
to allow the top |
| 141 // spacer (which has a weight of 1) to grow and take up the rema
ining space. |
| 142 int targetHeight = Math.max(getMeasuredHeight(), |
| 134 mParentViewportHeight - mPeekingCardHeight - mTabStripHe
ight); | 143 mParentViewportHeight - mPeekingCardHeight - mTabStripHe
ight); |
| 135 // Call onMeasure to update mTopScaper's height. | 144 heightMeasureSpec = MeasureSpec.makeMeasureSpec(targetHeight, Me
asureSpec.EXACTLY); |
| 136 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | 145 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 137 } | 146 } |
| 138 distributeExtraSpace(mTopSpacer.getMeasuredHeight()); | 147 distributeExtraSpace(mTopSpacer.getMeasuredHeight()); |
| 139 } | 148 } |
| 140 | 149 |
| 141 // Make the search box and logo as wide as the most visited items. | 150 // Make the search box and logo as wide as the most visited items. |
| 142 if (mMostVisitedLayout.getVisibility() != GONE) { | 151 if (mMostVisitedLayout.getVisibility() != GONE) { |
| 143 final int width = mMostVisitedLayout.getMeasuredWidth() - mMostVisit
edLayoutBleed; | 152 final int width = mMostVisitedLayout.getMeasuredWidth() - mMostVisit
edLayoutBleed; |
| 144 setMeasure(mSearchBoxView, width + mSearchboxViewShadowWidth, | 153 measureExactly(mSearchBoxView, width + mSearchboxViewShadowWidth, |
| 145 mSearchBoxView.getMeasuredHeight()); | 154 mSearchBoxView.getMeasuredHeight()); |
| 146 setMeasure(mSearchProviderLogoView, width, mSearchProviderLogoView.g
etMeasuredHeight()); | 155 measureExactly( |
| 156 mSearchProviderLogoView, width, mSearchProviderLogoView.getM
easuredHeight()); |
| 147 } | 157 } |
| 148 } | 158 } |
| 149 | 159 |
| 150 /** | 160 /** |
| 151 * Calculate the vertical position of Most Visited. | 161 * Calculate the vertical position of Most Visited. |
| 152 * This method does not use mMostVisitedLayout.getTop(), so can be called in
onMeasure. | 162 * This method does not use mMostVisitedLayout.getTop(), so can be called in
onMeasure. |
| 153 */ | 163 */ |
| 154 private int calculateTopOfMostVisited() { | 164 private int calculateTopOfMostVisited() { |
| 155 // Manually add the heights (and margins) of all children above Most Vis
ited. | 165 // Manually add the heights (and margins) of all children above Most Vis
ited. |
| 156 int top = 0; | 166 int top = 0; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 | 200 |
| 191 mSearchBoxView.setBackgroundResource(R.drawable.textbox); | 201 mSearchBoxView.setBackgroundResource(R.drawable.textbox); |
| 192 mSearchBoxView.setPadding( | 202 mSearchBoxView.setPadding( |
| 193 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_left), | 203 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_left), |
| 194 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_top), | 204 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_top), |
| 195 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_right), | 205 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_right), |
| 196 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_bottom)); | 206 resources.getDimensionPixelSize(R.dimen.ntp_search_box_material_
padding_bottom)); |
| 197 } | 207 } |
| 198 | 208 |
| 199 /** | 209 /** |
| 200 * Distribute extra vertical space between the three spacer views. | 210 * Distribute extra vertical space between the three spacer views. Doing thi
s here allows for |
| 211 * more sophisticated constraints than in xml. |
| 201 * @param extraHeight The amount of extra space, in pixels. | 212 * @param extraHeight The amount of extra space, in pixels. |
| 202 */ | 213 */ |
| 203 private void distributeExtraSpace(int extraHeight) { | 214 private void distributeExtraSpace(int extraHeight) { |
| 204 int topSpacerHeight; | 215 int topSpacerHeight; |
| 205 int middleSpacerHeight; | 216 int middleSpacerHeight; |
| 206 int bottomSpacerHeight; | 217 int bottomSpacerHeight; |
| 207 | 218 |
| 208 if (extraHeight < mTotalSpacerIdealHeight) { | 219 if (extraHeight < mTotalSpacerIdealHeight) { |
| 209 // The spacers will be less than their ideal height, shrink them pro
portionally. | 220 // The spacers will be less than their ideal height, shrink them pro
portionally. |
| 210 topSpacerHeight = | 221 topSpacerHeight = |
| 211 Math.round(extraHeight * (TOP_SPACER_HEIGHT_DP / TOTAL_SPACE
R_HEIGHT_DP)); | 222 Math.round(extraHeight * (TOP_SPACER_HEIGHT_DP / TOTAL_SPACE
R_HEIGHT_DP)); |
| 212 middleSpacerHeight = | 223 middleSpacerHeight = |
| 213 Math.round(extraHeight * (MIDDLE_SPACER_HEIGHT_DP / TOTAL_SP
ACER_HEIGHT_DP)); | 224 Math.round(extraHeight * (MIDDLE_SPACER_HEIGHT_DP / TOTAL_SP
ACER_HEIGHT_DP)); |
| 214 bottomSpacerHeight = extraHeight - topSpacerHeight - middleSpacerHei
ght; | 225 bottomSpacerHeight = extraHeight - topSpacerHeight - middleSpacerHei
ght; |
| 215 } else { | 226 } else { |
| 216 // Distribute remaining space evenly between the top and bottom spac
ers. | 227 // Distribute remaining space evenly between the top and bottom spac
ers. |
| 217 extraHeight -= mTotalSpacerIdealHeight; | 228 extraHeight -= mTotalSpacerIdealHeight; |
| 218 topSpacerHeight = mTopSpacerIdealHeight + extraHeight / 2; | 229 topSpacerHeight = mTopSpacerIdealHeight + extraHeight / 2; |
| 219 middleSpacerHeight = mMiddleSpacerIdealHeight; | 230 middleSpacerHeight = mMiddleSpacerIdealHeight; |
| 220 bottomSpacerHeight = mBottomSpacerIdealHeight + extraHeight / 2; | 231 bottomSpacerHeight = mBottomSpacerIdealHeight + extraHeight / 2; |
| 221 } | 232 } |
| 222 | 233 |
| 223 setMeasure(mTopSpacer, 0, topSpacerHeight); | 234 measureExactly(mTopSpacer, 0, topSpacerHeight); |
| 224 setMeasure(mMiddleSpacer, 0, middleSpacerHeight); | 235 measureExactly(mMiddleSpacer, 0, middleSpacerHeight); |
| 225 setMeasure(mBottomSpacer, 0, bottomSpacerHeight); | 236 measureExactly(mBottomSpacer, 0, bottomSpacerHeight); |
| 226 } | 237 } |
| 227 | 238 |
| 228 | |
| 229 /** | 239 /** |
| 230 * Convenience method to call |measure| on the given View with MeasureSpecs
converted from the | 240 * Convenience method to call measure() on the given View with MeasureSpecs
converted from the |
| 231 * given dimensions (in pixels) with MeasureSpec.EXACTLY. | 241 * given dimensions (in pixels) with MeasureSpec.EXACTLY. |
| 232 */ | 242 */ |
| 233 private void setMeasure(View view, int widthPx, int heightPx) { | 243 private static void measureExactly(View view, int widthPx, int heightPx) { |
| 234 view.measure(MeasureSpec.makeMeasureSpec(widthPx, MeasureSpec.EXACTLY), | 244 view.measure(MeasureSpec.makeMeasureSpec(widthPx, MeasureSpec.EXACTLY), |
| 235 MeasureSpec.makeMeasureSpec(heightPx, MeasureSpec.EXACTLY)); | 245 MeasureSpec.makeMeasureSpec(heightPx, MeasureSpec.EXACTLY)); |
| 236 } | 246 } |
| 237 } | 247 } |
| OLD | NEW |