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

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

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 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.content.res.Resources;
9 import android.graphics.Canvas;
10 import android.graphics.drawable.Drawable;
11 import android.util.AttributeSet;
12 import android.view.View;
13 import android.widget.FrameLayout;
14
15 import com.google.android.apps.chrome.R;
16
17 import org.chromium.base.ApiCompatibilityUtils;
18
19 /**
20 * A layout that arranges most visited items in a grid. All items must be the sa me size.
21 */
22 public class MostVisitedLayout extends FrameLayout {
23
24 private int mHorizontalSpacing;
25 private int mVerticalSpacing;
26 private int mTwoColumnMinWidth;
27 private int mThreeColumnMinWidth;
28
29 /**
30 * The ideal width of a child. The children may need be stretched or shrunk a bit to fill the
31 * available width.
32 */
33 private int mDefaultChildWidth;
34
35 private Drawable mEmptyTileDrawable;
36 private int mNumEmptyTiles;
37 private int mEmptyTileTop;
38 private int mFirstEmptyTileLeft;
39
40 /**
41 * @param context The view context in which this item will be shown.
42 * @param attrs The attributes of the XML tag that is inflating the view.
43 */
44 public MostVisitedLayout(Context context, AttributeSet attrs) {
45 super(context, attrs);
46 setWillNotDraw(false);
47
48 Resources res = getResources();
49 mHorizontalSpacing = res.getDimensionPixelOffset(R.dimen.most_visited_sp acing);
50 mVerticalSpacing = mHorizontalSpacing;
51 mDefaultChildWidth = res.getDimensionPixelSize(R.dimen.most_visited_tile _width);
52 mTwoColumnMinWidth = res.getDimensionPixelOffset(R.dimen.most_visited_tw o_column_min_width);
53 mThreeColumnMinWidth = res.getDimensionPixelOffset(
54 R.dimen.most_visited_three_column_min_width);
55 }
56
57 @Override
58 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
59 // Determine the number of columns in the grid.
60 final int totalWidth = MeasureSpec.getSize(widthMeasureSpec);
61 int childWidth = mDefaultChildWidth;
62 int childWidthWithSpacing = childWidth + mHorizontalSpacing;
63
64 int numColumns;
65 if (totalWidth + mHorizontalSpacing >= 3 * childWidthWithSpacing) {
66 numColumns = 3;
67 } else if (totalWidth < mTwoColumnMinWidth) {
68 numColumns = 1;
69 } else {
70 numColumns = totalWidth < mThreeColumnMinWidth ? 2 : 3;
71
72 // Resize the tiles to make them fill the entire available width.
73 childWidthWithSpacing = Math.max(mHorizontalSpacing,
74 (totalWidth + mHorizontalSpacing) / numColumns);
75 childWidth = childWidthWithSpacing - mHorizontalSpacing;
76 }
77
78 int childCount = getChildCount();
79 int childHeight = 0;
80 if (childCount > 0) {
81 // Measure the children.
82 int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, Measure Spec.EXACTLY);
83 int childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNS PECIFIED);
84 for (int i = 0; i < childCount; i++) {
85 getChildAt(i).measure(childWidthSpec, childHeightSpec);
86 }
87 childHeight = getChildAt(0).getMeasuredHeight();
88 }
89
90 // Arrange the children in a grid.
91 int childStart = 0;
92 int childTop = 0;
93 int column = 0;
94 boolean isRtl = ApiCompatibilityUtils.isLayoutRtl(this);
95 for (int i = 0; i < childCount; i++) {
96 View child = getChildAt(i);
97 MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayo utParams();
98 layoutParams.setMargins(isRtl ? 0 : childStart, childTop,
99 isRtl ? childStart : 0, 0);
100 child.setLayoutParams(layoutParams);
101 column++;
102 if (column == numColumns) {
103 column = 0;
104 childStart = 0;
105 childTop += childHeight + mVerticalSpacing;
106 } else {
107 childStart += childWidthWithSpacing;
108 }
109 }
110
111 // Fill the rest of the current row with empty tiles.
112 if (column != 0) {
113 mNumEmptyTiles = numColumns - column;
114 mEmptyTileTop = childTop + getPaddingTop();
115 mFirstEmptyTileLeft = isRtl ? 0 : childStart;
116 } else {
117 mNumEmptyTiles = 0;
118 }
119
120 int numRows = (childCount + mNumEmptyTiles) / numColumns;
121 int totalHeight = getPaddingTop() + getPaddingBottom() + numRows * child Height
122 + (numRows - 1) * mVerticalSpacing;
123
124 int gridWidth = numColumns * childWidthWithSpacing - mHorizontalSpacing;
125 setMeasuredDimension(gridWidth, resolveSize(totalHeight, heightMeasureSp ec));
126 }
127
128 @Override
129 protected void onDraw(Canvas canvas) {
130 super.onDraw(canvas);
131 if (mNumEmptyTiles == 0 || getChildCount() == 0) return;
132
133 // Draw empty tiles.
134 if (mEmptyTileDrawable == null) {
135 mEmptyTileDrawable = ApiCompatibilityUtils.getDrawable(
136 getResources(), R.drawable.most_visited_item_empty);
137 }
138 int tileLeft = mFirstEmptyTileLeft;
139 int tileWidth = getChildAt(0).getMeasuredWidth();
140 int tileHeight = getChildAt(0).getMeasuredHeight();
141 for (int i = 0; i < mNumEmptyTiles; i++) {
142 mEmptyTileDrawable.setBounds(
143 tileLeft,
144 mEmptyTileTop,
145 tileLeft + tileWidth,
146 mEmptyTileTop + tileHeight);
147 mEmptyTileDrawable.draw(canvas);
148 tileLeft += tileWidth + mHorizontalSpacing;
149 }
150 }
151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698