The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 21 | import java.util.ArrayDeque; |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
| 23 | import java.util.Comparator; |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 24 | import java.util.HashMap; |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 25 | import java.util.SortedSet; |
| 26 | import java.util.TreeSet; |
| 27 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 29 | import android.content.res.Resources; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 30 | import android.content.res.TypedArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 31 | import android.graphics.Rect; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 32 | import android.os.Build; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 33 | import android.util.AttributeSet; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 34 | import android.util.Pools.SimplePool; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 35 | import android.util.SparseArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 36 | import android.view.Gravity; |
| 37 | import android.view.View; |
| 38 | import android.view.ViewDebug; |
| 39 | import android.view.ViewGroup; |
| 40 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 41 | import android.view.accessibility.AccessibilityNodeInfo; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.widget.RemoteViews.RemoteView; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 43 | |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 44 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 45 | import static android.util.Log.d; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * A Layout where the positions of the children can be described in relation to each other or to the |
Romain Guy | 520c420 | 2010-01-04 10:56:38 -0800 | [diff] [blame] | 49 | * parent. |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 50 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | * <p> |
| 52 | * Note that you cannot have a circular dependency between the size of the RelativeLayout and the |
| 53 | * position of its children. For example, you cannot have a RelativeLayout whose height is set to |
| 54 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT WRAP_CONTENT} and a child set to |
| 55 | * {@link #ALIGN_PARENT_BOTTOM}. |
| 56 | * </p> |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 57 | * |
Adam Powell | 2c8cc97 | 2012-12-07 18:04:51 -0800 | [diff] [blame] | 58 | * <p><strong>Note:</strong> In platform version 17 and lower, RelativeLayout was affected by |
| 59 | * a measurement bug that could cause child views to be measured with incorrect |
| 60 | * {@link android.view.View.MeasureSpec MeasureSpec} values. (See |
| 61 | * {@link android.view.View.MeasureSpec#makeMeasureSpec(int, int) MeasureSpec.makeMeasureSpec} |
| 62 | * for more details.) This was triggered when a RelativeLayout container was placed in |
| 63 | * a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view |
| 64 | * not equipped to properly measure with the MeasureSpec mode |
| 65 | * {@link android.view.View.MeasureSpec#UNSPECIFIED UNSPECIFIED} was placed in a RelativeLayout, |
| 66 | * this would silently work anyway as RelativeLayout would pass a very large |
| 67 | * {@link android.view.View.MeasureSpec#AT_MOST AT_MOST} MeasureSpec instead.</p> |
| 68 | * |
| 69 | * <p>This behavior has been preserved for apps that set <code>android:targetSdkVersion="17"</code> |
| 70 | * or older in their manifest's <code>uses-sdk</code> tag for compatibility. Apps targeting SDK |
| 71 | * version 18 or newer will receive the correct behavior</p> |
| 72 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 73 | * <p>See the <a href="{@docRoot}guide/topics/ui/layout/relative.html">Relative |
| 74 | * Layout</a> guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 75 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | * <p> |
| 77 | * Also see {@link android.widget.RelativeLayout.LayoutParams RelativeLayout.LayoutParams} for |
| 78 | * layout attributes |
| 79 | * </p> |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 80 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | * @attr ref android.R.styleable#RelativeLayout_gravity |
| 82 | * @attr ref android.R.styleable#RelativeLayout_ignoreGravity |
| 83 | */ |
| 84 | @RemoteView |
| 85 | public class RelativeLayout extends ViewGroup { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 86 | private static final String LOG_TAG = "RelativeLayout"; |
| 87 | |
| 88 | private static final boolean DEBUG_GRAPH = false; |
| 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | public static final int TRUE = -1; |
| 91 | |
| 92 | /** |
| 93 | * Rule that aligns a child's right edge with another child's left edge. |
| 94 | */ |
| 95 | public static final int LEFT_OF = 0; |
| 96 | /** |
| 97 | * Rule that aligns a child's left edge with another child's right edge. |
| 98 | */ |
| 99 | public static final int RIGHT_OF = 1; |
| 100 | /** |
| 101 | * Rule that aligns a child's bottom edge with another child's top edge. |
| 102 | */ |
| 103 | public static final int ABOVE = 2; |
| 104 | /** |
| 105 | * Rule that aligns a child's top edge with another child's bottom edge. |
| 106 | */ |
| 107 | public static final int BELOW = 3; |
| 108 | |
| 109 | /** |
| 110 | * Rule that aligns a child's baseline with another child's baseline. |
| 111 | */ |
| 112 | public static final int ALIGN_BASELINE = 4; |
| 113 | /** |
| 114 | * Rule that aligns a child's left edge with another child's left edge. |
| 115 | */ |
| 116 | public static final int ALIGN_LEFT = 5; |
| 117 | /** |
| 118 | * Rule that aligns a child's top edge with another child's top edge. |
| 119 | */ |
| 120 | public static final int ALIGN_TOP = 6; |
| 121 | /** |
| 122 | * Rule that aligns a child's right edge with another child's right edge. |
| 123 | */ |
| 124 | public static final int ALIGN_RIGHT = 7; |
| 125 | /** |
| 126 | * Rule that aligns a child's bottom edge with another child's bottom edge. |
| 127 | */ |
| 128 | public static final int ALIGN_BOTTOM = 8; |
| 129 | |
| 130 | /** |
| 131 | * Rule that aligns the child's left edge with its RelativeLayout |
| 132 | * parent's left edge. |
| 133 | */ |
| 134 | public static final int ALIGN_PARENT_LEFT = 9; |
| 135 | /** |
| 136 | * Rule that aligns the child's top edge with its RelativeLayout |
| 137 | * parent's top edge. |
| 138 | */ |
| 139 | public static final int ALIGN_PARENT_TOP = 10; |
| 140 | /** |
| 141 | * Rule that aligns the child's right edge with its RelativeLayout |
| 142 | * parent's right edge. |
| 143 | */ |
| 144 | public static final int ALIGN_PARENT_RIGHT = 11; |
| 145 | /** |
| 146 | * Rule that aligns the child's bottom edge with its RelativeLayout |
| 147 | * parent's bottom edge. |
| 148 | */ |
| 149 | public static final int ALIGN_PARENT_BOTTOM = 12; |
| 150 | |
| 151 | /** |
| 152 | * Rule that centers the child with respect to the bounds of its |
| 153 | * RelativeLayout parent. |
| 154 | */ |
| 155 | public static final int CENTER_IN_PARENT = 13; |
| 156 | /** |
| 157 | * Rule that centers the child horizontally with respect to the |
| 158 | * bounds of its RelativeLayout parent. |
| 159 | */ |
| 160 | public static final int CENTER_HORIZONTAL = 14; |
| 161 | /** |
| 162 | * Rule that centers the child vertically with respect to the |
| 163 | * bounds of its RelativeLayout parent. |
| 164 | */ |
| 165 | public static final int CENTER_VERTICAL = 15; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Rule that aligns a child's end edge with another child's start edge. |
| 168 | */ |
| 169 | public static final int START_OF = 16; |
| 170 | /** |
| 171 | * Rule that aligns a child's start edge with another child's end edge. |
| 172 | */ |
| 173 | public static final int END_OF = 17; |
| 174 | /** |
| 175 | * Rule that aligns a child's start edge with another child's start edge. |
| 176 | */ |
| 177 | public static final int ALIGN_START = 18; |
| 178 | /** |
| 179 | * Rule that aligns a child's end edge with another child's end edge. |
| 180 | */ |
| 181 | public static final int ALIGN_END = 19; |
| 182 | /** |
| 183 | * Rule that aligns the child's start edge with its RelativeLayout |
| 184 | * parent's start edge. |
| 185 | */ |
| 186 | public static final int ALIGN_PARENT_START = 20; |
| 187 | /** |
| 188 | * Rule that aligns the child's end edge with its RelativeLayout |
| 189 | * parent's end edge. |
| 190 | */ |
| 191 | public static final int ALIGN_PARENT_END = 21; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 193 | private static final int VERB_COUNT = 22; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 195 | |
| 196 | private static final int[] RULES_VERTICAL = { |
| 197 | ABOVE, BELOW, ALIGN_BASELINE, ALIGN_TOP, ALIGN_BOTTOM |
| 198 | }; |
| 199 | |
| 200 | private static final int[] RULES_HORIZONTAL = { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 201 | LEFT_OF, RIGHT_OF, ALIGN_LEFT, ALIGN_RIGHT, START_OF, END_OF, ALIGN_START, ALIGN_END |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 202 | }; |
| 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | private View mBaselineView = null; |
| 205 | private boolean mHasBaselineAlignedChild; |
| 206 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 207 | private int mGravity = Gravity.START | Gravity.TOP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | private final Rect mContentBounds = new Rect(); |
| 209 | private final Rect mSelfBounds = new Rect(); |
| 210 | private int mIgnoreGravity; |
| 211 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 212 | private SortedSet<View> mTopToBottomLeftToRightSet = null; |
| 213 | |
| 214 | private boolean mDirtyHierarchy; |
| 215 | private View[] mSortedHorizontalChildren = new View[0]; |
| 216 | private View[] mSortedVerticalChildren = new View[0]; |
| 217 | private final DependencyGraph mGraph = new DependencyGraph(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 218 | |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 219 | // Compatibility hack. Old versions of the platform had problems |
| 220 | // with MeasureSpec value overflow and RelativeLayout was one source of them. |
| 221 | // Some apps came to rely on them. :( |
| 222 | private boolean mAllowBrokenMeasureSpecs = false; |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 223 | // Compatibility hack. Old versions of the platform would not take |
| 224 | // margins and padding into account when generating the height measure spec |
| 225 | // for children during the horizontal measure pass. |
| 226 | private boolean mMeasureVerticalWithPaddingMargin = false; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 227 | |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 228 | // A default width used for RTL measure pass |
Philip Milne | ca2e9e1 | 2013-04-22 12:44:29 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Value reduced so as not to interfere with View's measurement spec. flags. See: |
| 231 | * {@link View#MEASURED_SIZE_MASK}. |
| 232 | * {@link View#MEASURED_STATE_TOO_SMALL}. |
| 233 | **/ |
Philip Milne | 3c647d2 | 2013-04-23 14:31:23 -0700 | [diff] [blame] | 234 | private static final int DEFAULT_WIDTH = 0x00010000; |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | public RelativeLayout(Context context) { |
| 237 | super(context); |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 238 | queryCompatibilityModes(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | public RelativeLayout(Context context, AttributeSet attrs) { |
| 242 | super(context, attrs); |
| 243 | initFromAttributes(context, attrs); |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 244 | queryCompatibilityModes(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | public RelativeLayout(Context context, AttributeSet attrs, int defStyle) { |
| 248 | super(context, attrs, defStyle); |
| 249 | initFromAttributes(context, attrs); |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 250 | queryCompatibilityModes(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private void initFromAttributes(Context context, AttributeSet attrs) { |
| 254 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RelativeLayout); |
| 255 | mIgnoreGravity = a.getResourceId(R.styleable.RelativeLayout_ignoreGravity, View.NO_ID); |
| 256 | mGravity = a.getInt(R.styleable.RelativeLayout_gravity, mGravity); |
| 257 | a.recycle(); |
| 258 | } |
| 259 | |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 260 | private void queryCompatibilityModes(Context context) { |
| 261 | int version = context.getApplicationInfo().targetSdkVersion; |
| 262 | mAllowBrokenMeasureSpecs = version <= Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 263 | mMeasureVerticalWithPaddingMargin = version >= Build.VERSION_CODES.JELLY_BEAN_MR2; |
| 264 | } |
| 265 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public boolean shouldDelayChildPressedState() { |
| 268 | return false; |
| 269 | } |
| 270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | /** |
| 272 | * Defines which View is ignored when the gravity is applied. This setting has no |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 273 | * effect if the gravity is <code>Gravity.START | Gravity.TOP</code>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | * |
| 275 | * @param viewId The id of the View to be ignored by gravity, or 0 if no View |
| 276 | * should be ignored. |
| 277 | * |
| 278 | * @see #setGravity(int) |
| 279 | * |
| 280 | * @attr ref android.R.styleable#RelativeLayout_ignoreGravity |
| 281 | */ |
| 282 | @android.view.RemotableViewMethod |
| 283 | public void setIgnoreGravity(int viewId) { |
| 284 | mIgnoreGravity = viewId; |
| 285 | } |
| 286 | |
| 287 | /** |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 288 | * Describes how the child views are positioned. |
| 289 | * |
| 290 | * @return the gravity. |
| 291 | * |
| 292 | * @see #setGravity(int) |
| 293 | * @see android.view.Gravity |
| 294 | * |
| 295 | * @attr ref android.R.styleable#RelativeLayout_gravity |
| 296 | */ |
| 297 | public int getGravity() { |
| 298 | return mGravity; |
| 299 | } |
| 300 | |
| 301 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | * Describes how the child views are positioned. Defaults to |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 303 | * <code>Gravity.START | Gravity.TOP</code>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | * |
Adam Powell | 1fec24e | 2011-09-15 14:19:30 -0700 | [diff] [blame] | 305 | * <p>Note that since RelativeLayout considers the positioning of each child |
| 306 | * relative to one another to be significant, setting gravity will affect |
| 307 | * the positioning of all children as a single unit within the parent. |
| 308 | * This happens after children have been relatively positioned.</p> |
| 309 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | * @param gravity See {@link android.view.Gravity} |
| 311 | * |
| 312 | * @see #setHorizontalGravity(int) |
| 313 | * @see #setVerticalGravity(int) |
| 314 | * |
| 315 | * @attr ref android.R.styleable#RelativeLayout_gravity |
| 316 | */ |
| 317 | @android.view.RemotableViewMethod |
| 318 | public void setGravity(int gravity) { |
| 319 | if (mGravity != gravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 320 | if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 321 | gravity |= Gravity.START; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { |
| 325 | gravity |= Gravity.TOP; |
| 326 | } |
| 327 | |
| 328 | mGravity = gravity; |
| 329 | requestLayout(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | @android.view.RemotableViewMethod |
| 334 | public void setHorizontalGravity(int horizontalGravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 335 | final int gravity = horizontalGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 336 | if ((mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) { |
| 337 | mGravity = (mGravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | requestLayout(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @android.view.RemotableViewMethod |
| 343 | public void setVerticalGravity(int verticalGravity) { |
| 344 | final int gravity = verticalGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 345 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != gravity) { |
| 346 | mGravity = (mGravity & ~Gravity.VERTICAL_GRAVITY_MASK) | gravity; |
| 347 | requestLayout(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public int getBaseline() { |
| 353 | return mBaselineView != null ? mBaselineView.getBaseline() : super.getBaseline(); |
| 354 | } |
| 355 | |
| 356 | @Override |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 357 | public void requestLayout() { |
| 358 | super.requestLayout(); |
| 359 | mDirtyHierarchy = true; |
| 360 | } |
| 361 | |
| 362 | private void sortChildren() { |
| 363 | int count = getChildCount(); |
| 364 | if (mSortedVerticalChildren.length != count) mSortedVerticalChildren = new View[count]; |
| 365 | if (mSortedHorizontalChildren.length != count) mSortedHorizontalChildren = new View[count]; |
| 366 | |
| 367 | final DependencyGraph graph = mGraph; |
| 368 | graph.clear(); |
| 369 | |
| 370 | for (int i = 0; i < count; i++) { |
| 371 | final View child = getChildAt(i); |
| 372 | graph.add(child); |
| 373 | } |
| 374 | |
| 375 | if (DEBUG_GRAPH) { |
| 376 | d(LOG_TAG, "=== Sorted vertical children"); |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 377 | graph.log(getResources(), RULES_VERTICAL); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 378 | d(LOG_TAG, "=== Sorted horizontal children"); |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 379 | graph.log(getResources(), RULES_HORIZONTAL); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 382 | graph.getSortedViews(mSortedVerticalChildren, RULES_VERTICAL); |
| 383 | graph.getSortedViews(mSortedHorizontalChildren, RULES_HORIZONTAL); |
Romain Guy | 9fffa1e | 2009-06-24 12:42:43 -0700 | [diff] [blame] | 384 | |
| 385 | if (DEBUG_GRAPH) { |
| 386 | d(LOG_TAG, "=== Ordered list of vertical children"); |
| 387 | for (View view : mSortedVerticalChildren) { |
| 388 | DependencyGraph.printViewId(getResources(), view); |
| 389 | } |
| 390 | d(LOG_TAG, "=== Ordered list of horizontal children"); |
| 391 | for (View view : mSortedHorizontalChildren) { |
| 392 | DependencyGraph.printViewId(getResources(), view); |
| 393 | } |
| 394 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 397 | // TODO: we need to find another way to implement RelativeLayout |
| 398 | // This implementation cannot handle every case |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 399 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 401 | if (mDirtyHierarchy) { |
| 402 | mDirtyHierarchy = false; |
| 403 | sortChildren(); |
| 404 | } |
| 405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | int myWidth = -1; |
| 407 | int myHeight = -1; |
| 408 | |
| 409 | int width = 0; |
| 410 | int height = 0; |
| 411 | |
Adam Powell | 132a742 | 2012-09-30 12:39:56 -0700 | [diff] [blame] | 412 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 413 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 414 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 415 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | |
| 417 | // Record our dimensions if they are known; |
| 418 | if (widthMode != MeasureSpec.UNSPECIFIED) { |
| 419 | myWidth = widthSize; |
| 420 | } |
| 421 | |
| 422 | if (heightMode != MeasureSpec.UNSPECIFIED) { |
| 423 | myHeight = heightSize; |
| 424 | } |
| 425 | |
| 426 | if (widthMode == MeasureSpec.EXACTLY) { |
| 427 | width = myWidth; |
| 428 | } |
| 429 | |
| 430 | if (heightMode == MeasureSpec.EXACTLY) { |
| 431 | height = myHeight; |
| 432 | } |
| 433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | mHasBaselineAlignedChild = false; |
| 435 | |
| 436 | View ignore = null; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 437 | int gravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 438 | final boolean horizontalGravity = gravity != Gravity.START && gravity != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 440 | final boolean verticalGravity = gravity != Gravity.TOP && gravity != 0; |
| 441 | |
| 442 | int left = Integer.MAX_VALUE; |
| 443 | int top = Integer.MAX_VALUE; |
| 444 | int right = Integer.MIN_VALUE; |
| 445 | int bottom = Integer.MIN_VALUE; |
| 446 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 447 | boolean offsetHorizontalAxis = false; |
| 448 | boolean offsetVerticalAxis = false; |
| 449 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | if ((horizontalGravity || verticalGravity) && mIgnoreGravity != View.NO_ID) { |
| 451 | ignore = findViewById(mIgnoreGravity); |
| 452 | } |
| 453 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 454 | final boolean isWrapContentWidth = widthMode != MeasureSpec.EXACTLY; |
| 455 | final boolean isWrapContentHeight = heightMode != MeasureSpec.EXACTLY; |
| 456 | |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 457 | // We need to know our size for doing the correct computation of children positioning in RTL |
| 458 | // mode but there is no practical way to get it instead of running the code below. |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 459 | // So, instead of running the code twice, we just set the width to a "default display width" |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 460 | // before the computation and then, as a last pass, we will update their real position with |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 461 | // an offset equals to "DEFAULT_WIDTH - width". |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 462 | final int layoutDirection = getLayoutDirection(); |
| 463 | if (isLayoutRtl() && myWidth == -1) { |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 464 | myWidth = DEFAULT_WIDTH; |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Romain Guy | e24ef60 | 2009-06-25 13:01:55 -0700 | [diff] [blame] | 467 | View[] views = mSortedHorizontalChildren; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 468 | int count = views.length; |
Fabrice Di Meglio | 13705ed | 2012-11-26 20:05:31 -0800 | [diff] [blame] | 469 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 470 | for (int i = 0; i < count; i++) { |
| 471 | View child = views[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | if (child.getVisibility() != GONE) { |
| 473 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 474 | int[] rules = params.getRules(layoutDirection); |
Romain Guy | 9560703 | 2009-06-24 14:37:03 -0700 | [diff] [blame] | 475 | |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 476 | applyHorizontalSizeRules(params, myWidth, rules); |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 477 | measureChildHorizontal(child, params, myWidth, myHeight); |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 478 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 479 | if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) { |
| 480 | offsetHorizontalAxis = true; |
| 481 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Romain Guy | e24ef60 | 2009-06-25 13:01:55 -0700 | [diff] [blame] | 485 | views = mSortedVerticalChildren; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 486 | count = views.length; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 487 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 488 | for (int i = 0; i < count; i++) { |
| 489 | View child = views[i]; |
| 490 | if (child.getVisibility() != GONE) { |
| 491 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Romain Guy | 9560703 | 2009-06-24 14:37:03 -0700 | [diff] [blame] | 492 | |
| 493 | applyVerticalSizeRules(params, myHeight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | measureChild(child, params, myWidth, myHeight); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 495 | if (positionChildVertical(child, params, myHeight, isWrapContentHeight)) { |
| 496 | offsetVerticalAxis = true; |
| 497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 499 | if (isWrapContentWidth) { |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 500 | if (isLayoutRtl()) { |
| 501 | width = Math.max(width, myWidth - params.mLeft); |
| 502 | } else { |
| 503 | width = Math.max(width, params.mRight); |
| 504 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 506 | |
| 507 | if (isWrapContentHeight) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | height = Math.max(height, params.mBottom); |
| 509 | } |
| 510 | |
| 511 | if (child != ignore || verticalGravity) { |
| 512 | left = Math.min(left, params.mLeft - params.leftMargin); |
| 513 | top = Math.min(top, params.mTop - params.topMargin); |
| 514 | } |
| 515 | |
| 516 | if (child != ignore || horizontalGravity) { |
| 517 | right = Math.max(right, params.mRight + params.rightMargin); |
| 518 | bottom = Math.max(bottom, params.mBottom + params.bottomMargin); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (mHasBaselineAlignedChild) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 524 | for (int i = 0; i < count; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | View child = getChildAt(i); |
| 526 | if (child.getVisibility() != GONE) { |
| 527 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
| 528 | alignBaseline(child, params); |
| 529 | |
| 530 | if (child != ignore || verticalGravity) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 531 | left = Math.min(left, params.mLeft - params.leftMargin); |
| 532 | top = Math.min(top, params.mTop - params.topMargin); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if (child != ignore || horizontalGravity) { |
| 536 | right = Math.max(right, params.mRight + params.rightMargin); |
| 537 | bottom = Math.max(bottom, params.mBottom + params.bottomMargin); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 543 | if (isWrapContentWidth) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 544 | // Width already has left padding in it since it was calculated by looking at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | // the right of each child view |
| 546 | width += mPaddingRight; |
| 547 | |
| 548 | if (mLayoutParams.width >= 0) { |
| 549 | width = Math.max(width, mLayoutParams.width); |
| 550 | } |
| 551 | |
| 552 | width = Math.max(width, getSuggestedMinimumWidth()); |
| 553 | width = resolveSize(width, widthMeasureSpec); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 554 | |
| 555 | if (offsetHorizontalAxis) { |
Romain Guy | d10a576 | 2009-07-28 11:18:14 -0700 | [diff] [blame] | 556 | for (int i = 0; i < count; i++) { |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 557 | View child = getChildAt(i); |
| 558 | if (child.getVisibility() != GONE) { |
| 559 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 560 | final int[] rules = params.getRules(layoutDirection); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 561 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) { |
| 562 | centerHorizontal(child, params, width); |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 563 | } else if (rules[ALIGN_PARENT_RIGHT] != 0) { |
| 564 | final int childWidth = child.getMeasuredWidth(); |
| 565 | params.mLeft = width - mPaddingRight - childWidth; |
| 566 | params.mRight = params.mLeft + childWidth; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | } |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 572 | |
| 573 | if (isWrapContentHeight) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 574 | // Height already has top padding in it since it was calculated by looking at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | // the bottom of each child view |
| 576 | height += mPaddingBottom; |
| 577 | |
| 578 | if (mLayoutParams.height >= 0) { |
| 579 | height = Math.max(height, mLayoutParams.height); |
| 580 | } |
| 581 | |
| 582 | height = Math.max(height, getSuggestedMinimumHeight()); |
| 583 | height = resolveSize(height, heightMeasureSpec); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 584 | |
| 585 | if (offsetVerticalAxis) { |
| 586 | for (int i = 0; i < count; i++) { |
| 587 | View child = getChildAt(i); |
| 588 | if (child.getVisibility() != GONE) { |
| 589 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 590 | final int[] rules = params.getRules(layoutDirection); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 591 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) { |
| 592 | centerVertical(child, params, height); |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 593 | } else if (rules[ALIGN_PARENT_BOTTOM] != 0) { |
| 594 | final int childHeight = child.getMeasuredHeight(); |
| 595 | params.mTop = height - mPaddingBottom - childHeight; |
| 596 | params.mBottom = params.mTop + childHeight; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | if (horizontalGravity || verticalGravity) { |
| 604 | final Rect selfBounds = mSelfBounds; |
| 605 | selfBounds.set(mPaddingLeft, mPaddingTop, width - mPaddingRight, |
| 606 | height - mPaddingBottom); |
| 607 | |
| 608 | final Rect contentBounds = mContentBounds; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 609 | Gravity.apply(mGravity, right - left, bottom - top, selfBounds, contentBounds, |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 610 | layoutDirection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | |
| 612 | final int horizontalOffset = contentBounds.left - left; |
| 613 | final int verticalOffset = contentBounds.top - top; |
| 614 | if (horizontalOffset != 0 || verticalOffset != 0) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 615 | for (int i = 0; i < count; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | View child = getChildAt(i); |
| 617 | if (child.getVisibility() != GONE && child != ignore) { |
| 618 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d10a576 | 2009-07-28 11:18:14 -0700 | [diff] [blame] | 619 | if (horizontalGravity) { |
| 620 | params.mLeft += horizontalOffset; |
| 621 | params.mRight += horizontalOffset; |
| 622 | } |
| 623 | if (verticalGravity) { |
| 624 | params.mTop += verticalOffset; |
| 625 | params.mBottom += verticalOffset; |
| 626 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 632 | if (isLayoutRtl()) { |
| 633 | final int offsetWidth = myWidth - width; |
| 634 | for (int i = 0; i < count; i++) { |
| 635 | View child = getChildAt(i); |
| 636 | if (child.getVisibility() != GONE) { |
| 637 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
| 638 | params.mLeft -= offsetWidth; |
| 639 | params.mRight -= offsetWidth; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | } |
| 644 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | setMeasuredDimension(width, height); |
| 646 | } |
| 647 | |
| 648 | private void alignBaseline(View child, LayoutParams params) { |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 649 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 650 | int[] rules = params.getRules(layoutDirection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | int anchorBaseline = getRelatedViewBaseline(rules, ALIGN_BASELINE); |
| 652 | |
| 653 | if (anchorBaseline != -1) { |
| 654 | LayoutParams anchorParams = getRelatedViewParams(rules, ALIGN_BASELINE); |
| 655 | if (anchorParams != null) { |
| 656 | int offset = anchorParams.mTop + anchorBaseline; |
| 657 | int baseline = child.getBaseline(); |
| 658 | if (baseline != -1) { |
| 659 | offset -= baseline; |
| 660 | } |
| 661 | int height = params.mBottom - params.mTop; |
| 662 | params.mTop = offset; |
| 663 | params.mBottom = params.mTop + height; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (mBaselineView == null) { |
| 668 | mBaselineView = child; |
| 669 | } else { |
| 670 | LayoutParams lp = (LayoutParams) mBaselineView.getLayoutParams(); |
| 671 | if (params.mTop < lp.mTop || (params.mTop == lp.mTop && params.mLeft < lp.mLeft)) { |
| 672 | mBaselineView = child; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Measure a child. The child should have left, top, right and bottom information |
| 679 | * stored in its LayoutParams. If any of these values is -1 it means that the view |
| 680 | * can extend up to the corresponding edge. |
| 681 | * |
| 682 | * @param child Child to measure |
| 683 | * @param params LayoutParams associated with child |
| 684 | * @param myWidth Width of the the RelativeLayout |
| 685 | * @param myHeight Height of the RelativeLayout |
| 686 | */ |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 687 | private void measureChild(View child, LayoutParams params, int myWidth, int myHeight) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | int childWidthMeasureSpec = getChildMeasureSpec(params.mLeft, |
| 689 | params.mRight, params.width, |
| 690 | params.leftMargin, params.rightMargin, |
| 691 | mPaddingLeft, mPaddingRight, |
| 692 | myWidth); |
| 693 | int childHeightMeasureSpec = getChildMeasureSpec(params.mTop, |
| 694 | params.mBottom, params.height, |
| 695 | params.topMargin, params.bottomMargin, |
| 696 | mPaddingTop, mPaddingBottom, |
| 697 | myHeight); |
| 698 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 699 | } |
| 700 | |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 701 | private void measureChildHorizontal(View child, LayoutParams params, int myWidth, int myHeight) { |
Romain Guy | 9560703 | 2009-06-24 14:37:03 -0700 | [diff] [blame] | 702 | int childWidthMeasureSpec = getChildMeasureSpec(params.mLeft, |
| 703 | params.mRight, params.width, |
| 704 | params.leftMargin, params.rightMargin, |
| 705 | mPaddingLeft, mPaddingRight, |
| 706 | myWidth); |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 707 | int maxHeight = myHeight; |
| 708 | if (mMeasureVerticalWithPaddingMargin) { |
| 709 | maxHeight = Math.max(0, myHeight - mPaddingTop - mPaddingBottom - |
| 710 | params.topMargin - params.bottomMargin); |
| 711 | } |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 712 | int childHeightMeasureSpec; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 713 | if (myHeight < 0 && !mAllowBrokenMeasureSpecs) { |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 714 | if (params.height >= 0) { |
| 715 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 716 | params.height, MeasureSpec.EXACTLY); |
| 717 | } else { |
| 718 | // Negative values in a mySize/myWidth/myWidth value in RelativeLayout measurement |
| 719 | // is code for, "we got an unspecified mode in the RelativeLayout's measurespec." |
| 720 | // Carry it forward. |
| 721 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 722 | } |
Adam Powell | 132a742 | 2012-09-30 12:39:56 -0700 | [diff] [blame] | 723 | } else if (params.width == LayoutParams.MATCH_PARENT) { |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 724 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY); |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 725 | } else { |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 726 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 727 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 728 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 729 | } |
| 730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | /** |
| 732 | * Get a measure spec that accounts for all of the constraints on this view. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 733 | * This includes size constraints imposed by the RelativeLayout as well as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | * the View's desired dimension. |
| 735 | * |
| 736 | * @param childStart The left or top field of the child's layout params |
| 737 | * @param childEnd The right or bottom field of the child's layout params |
| 738 | * @param childSize The child's desired size (the width or height field of |
| 739 | * the child's layout params) |
| 740 | * @param startMargin The left or top margin |
| 741 | * @param endMargin The right or bottom margin |
| 742 | * @param startPadding mPaddingLeft or mPaddingTop |
| 743 | * @param endPadding mPaddingRight or mPaddingBottom |
| 744 | * @param mySize The width or height of this view (the RelativeLayout) |
| 745 | * @return MeasureSpec for the child |
| 746 | */ |
| 747 | private int getChildMeasureSpec(int childStart, int childEnd, |
| 748 | int childSize, int startMargin, int endMargin, int startPadding, |
| 749 | int endPadding, int mySize) { |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 750 | if (mySize < 0 && !mAllowBrokenMeasureSpecs) { |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 751 | if (childSize >= 0) { |
| 752 | return MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY); |
| 753 | } |
Adam Powell | 132a742 | 2012-09-30 12:39:56 -0700 | [diff] [blame] | 754 | // Negative values in a mySize/myWidth/myWidth value in RelativeLayout measurement |
| 755 | // is code for, "we got an unspecified mode in the RelativeLayout's measurespec." |
| 756 | // Carry it forward. |
| 757 | return MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 758 | } |
| 759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | int childSpecMode = 0; |
| 761 | int childSpecSize = 0; |
| 762 | |
| 763 | // Figure out start and end bounds. |
| 764 | int tempStart = childStart; |
| 765 | int tempEnd = childEnd; |
| 766 | |
| 767 | // If the view did not express a layout constraint for an edge, use |
| 768 | // view's margins and our padding |
| 769 | if (tempStart < 0) { |
| 770 | tempStart = startPadding + startMargin; |
| 771 | } |
| 772 | if (tempEnd < 0) { |
| 773 | tempEnd = mySize - endPadding - endMargin; |
| 774 | } |
| 775 | |
| 776 | // Figure out maximum size available to this view |
| 777 | int maxAvailable = tempEnd - tempStart; |
| 778 | |
| 779 | if (childStart >= 0 && childEnd >= 0) { |
| 780 | // Constraints fixed both edges, so child must be an exact size |
| 781 | childSpecMode = MeasureSpec.EXACTLY; |
| 782 | childSpecSize = maxAvailable; |
| 783 | } else { |
| 784 | if (childSize >= 0) { |
| 785 | // Child wanted an exact size. Give as much as possible |
| 786 | childSpecMode = MeasureSpec.EXACTLY; |
| 787 | |
| 788 | if (maxAvailable >= 0) { |
| 789 | // We have a maxmum size in this dimension. |
| 790 | childSpecSize = Math.min(maxAvailable, childSize); |
| 791 | } else { |
| 792 | // We can grow in this dimension. |
| 793 | childSpecSize = childSize; |
| 794 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 795 | } else if (childSize == LayoutParams.MATCH_PARENT) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 796 | // Child wanted to be as big as possible. Give all available |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | // space |
| 798 | childSpecMode = MeasureSpec.EXACTLY; |
| 799 | childSpecSize = maxAvailable; |
| 800 | } else if (childSize == LayoutParams.WRAP_CONTENT) { |
| 801 | // Child wants to wrap content. Use AT_MOST |
| 802 | // to communicate available space if we know |
| 803 | // our max size |
| 804 | if (maxAvailable >= 0) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 805 | // We have a maximum size in this dimension. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | childSpecMode = MeasureSpec.AT_MOST; |
| 807 | childSpecSize = maxAvailable; |
| 808 | } else { |
| 809 | // We can grow in this dimension. Child can be as big as it |
| 810 | // wants |
| 811 | childSpecMode = MeasureSpec.UNSPECIFIED; |
| 812 | childSpecSize = 0; |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | return MeasureSpec.makeMeasureSpec(childSpecSize, childSpecMode); |
| 818 | } |
| 819 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 820 | private boolean positionChildHorizontal(View child, LayoutParams params, int myWidth, |
| 821 | boolean wrapContent) { |
| 822 | |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 823 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 824 | int[] rules = params.getRules(layoutDirection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | |
| 826 | if (params.mLeft < 0 && params.mRight >= 0) { |
| 827 | // Right is fixed, but left varies |
| 828 | params.mLeft = params.mRight - child.getMeasuredWidth(); |
| 829 | } else if (params.mLeft >= 0 && params.mRight < 0) { |
| 830 | // Left is fixed, but right varies |
| 831 | params.mRight = params.mLeft + child.getMeasuredWidth(); |
| 832 | } else if (params.mLeft < 0 && params.mRight < 0) { |
| 833 | // Both left and right vary |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 834 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) { |
| 835 | if (!wrapContent) { |
| 836 | centerHorizontal(child, params, myWidth); |
| 837 | } else { |
| 838 | params.mLeft = mPaddingLeft + params.leftMargin; |
| 839 | params.mRight = params.mLeft + child.getMeasuredWidth(); |
| 840 | } |
| 841 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | } else { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 843 | // This is the default case. For RTL we start from the right and for LTR we start |
| 844 | // from the left. This will give LEFT/TOP for LTR and RIGHT/TOP for RTL. |
| 845 | if (isLayoutRtl()) { |
| 846 | params.mRight = myWidth - mPaddingRight- params.rightMargin; |
| 847 | params.mLeft = params.mRight - child.getMeasuredWidth(); |
| 848 | } else { |
| 849 | params.mLeft = mPaddingLeft + params.leftMargin; |
| 850 | params.mRight = params.mLeft + child.getMeasuredWidth(); |
| 851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 854 | return rules[ALIGN_PARENT_END] != 0; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 857 | private boolean positionChildVertical(View child, LayoutParams params, int myHeight, |
| 858 | boolean wrapContent) { |
| 859 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 860 | int[] rules = params.getRules(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | |
| 862 | if (params.mTop < 0 && params.mBottom >= 0) { |
| 863 | // Bottom is fixed, but top varies |
| 864 | params.mTop = params.mBottom - child.getMeasuredHeight(); |
| 865 | } else if (params.mTop >= 0 && params.mBottom < 0) { |
| 866 | // Top is fixed, but bottom varies |
| 867 | params.mBottom = params.mTop + child.getMeasuredHeight(); |
| 868 | } else if (params.mTop < 0 && params.mBottom < 0) { |
| 869 | // Both top and bottom vary |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 870 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) { |
| 871 | if (!wrapContent) { |
| 872 | centerVertical(child, params, myHeight); |
| 873 | } else { |
| 874 | params.mTop = mPaddingTop + params.topMargin; |
| 875 | params.mBottom = params.mTop + child.getMeasuredHeight(); |
| 876 | } |
| 877 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | } else { |
| 879 | params.mTop = mPaddingTop + params.topMargin; |
| 880 | params.mBottom = params.mTop + child.getMeasuredHeight(); |
| 881 | } |
| 882 | } |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 883 | return rules[ALIGN_PARENT_BOTTOM] != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 886 | private void applyHorizontalSizeRules(LayoutParams childParams, int myWidth, int[] rules) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | RelativeLayout.LayoutParams anchorParams; |
| 888 | |
| 889 | // -1 indicated a "soft requirement" in that direction. For example: |
| 890 | // left=10, right=-1 means the view must start at 10, but can go as far as it wants to the right |
| 891 | // left =-1, right=10 means the view must end at 10, but can go as far as it wants to the left |
| 892 | // left=10, right=20 means the left and right ends are both fixed |
| 893 | childParams.mLeft = -1; |
| 894 | childParams.mRight = -1; |
| 895 | |
| 896 | anchorParams = getRelatedViewParams(rules, LEFT_OF); |
| 897 | if (anchorParams != null) { |
| 898 | childParams.mRight = anchorParams.mLeft - (anchorParams.leftMargin + |
| 899 | childParams.rightMargin); |
| 900 | } else if (childParams.alignWithParent && rules[LEFT_OF] != 0) { |
| 901 | if (myWidth >= 0) { |
| 902 | childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin; |
| 903 | } else { |
| 904 | // FIXME uh oh... |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | anchorParams = getRelatedViewParams(rules, RIGHT_OF); |
| 909 | if (anchorParams != null) { |
| 910 | childParams.mLeft = anchorParams.mRight + (anchorParams.rightMargin + |
| 911 | childParams.leftMargin); |
| 912 | } else if (childParams.alignWithParent && rules[RIGHT_OF] != 0) { |
| 913 | childParams.mLeft = mPaddingLeft + childParams.leftMargin; |
| 914 | } |
| 915 | |
| 916 | anchorParams = getRelatedViewParams(rules, ALIGN_LEFT); |
| 917 | if (anchorParams != null) { |
| 918 | childParams.mLeft = anchorParams.mLeft + childParams.leftMargin; |
| 919 | } else if (childParams.alignWithParent && rules[ALIGN_LEFT] != 0) { |
| 920 | childParams.mLeft = mPaddingLeft + childParams.leftMargin; |
| 921 | } |
| 922 | |
| 923 | anchorParams = getRelatedViewParams(rules, ALIGN_RIGHT); |
| 924 | if (anchorParams != null) { |
| 925 | childParams.mRight = anchorParams.mRight - childParams.rightMargin; |
| 926 | } else if (childParams.alignWithParent && rules[ALIGN_RIGHT] != 0) { |
| 927 | if (myWidth >= 0) { |
| 928 | childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin; |
| 929 | } else { |
| 930 | // FIXME uh oh... |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if (0 != rules[ALIGN_PARENT_LEFT]) { |
| 935 | childParams.mLeft = mPaddingLeft + childParams.leftMargin; |
| 936 | } |
| 937 | |
| 938 | if (0 != rules[ALIGN_PARENT_RIGHT]) { |
| 939 | if (myWidth >= 0) { |
| 940 | childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin; |
| 941 | } else { |
| 942 | // FIXME uh oh... |
| 943 | } |
| 944 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | private void applyVerticalSizeRules(LayoutParams childParams, int myHeight) { |
| 948 | int[] rules = childParams.getRules(); |
| 949 | RelativeLayout.LayoutParams anchorParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | |
| 951 | childParams.mTop = -1; |
| 952 | childParams.mBottom = -1; |
| 953 | |
| 954 | anchorParams = getRelatedViewParams(rules, ABOVE); |
| 955 | if (anchorParams != null) { |
| 956 | childParams.mBottom = anchorParams.mTop - (anchorParams.topMargin + |
| 957 | childParams.bottomMargin); |
| 958 | } else if (childParams.alignWithParent && rules[ABOVE] != 0) { |
| 959 | if (myHeight >= 0) { |
| 960 | childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin; |
| 961 | } else { |
| 962 | // FIXME uh oh... |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | anchorParams = getRelatedViewParams(rules, BELOW); |
| 967 | if (anchorParams != null) { |
| 968 | childParams.mTop = anchorParams.mBottom + (anchorParams.bottomMargin + |
| 969 | childParams.topMargin); |
| 970 | } else if (childParams.alignWithParent && rules[BELOW] != 0) { |
| 971 | childParams.mTop = mPaddingTop + childParams.topMargin; |
| 972 | } |
| 973 | |
| 974 | anchorParams = getRelatedViewParams(rules, ALIGN_TOP); |
| 975 | if (anchorParams != null) { |
| 976 | childParams.mTop = anchorParams.mTop + childParams.topMargin; |
| 977 | } else if (childParams.alignWithParent && rules[ALIGN_TOP] != 0) { |
| 978 | childParams.mTop = mPaddingTop + childParams.topMargin; |
| 979 | } |
| 980 | |
| 981 | anchorParams = getRelatedViewParams(rules, ALIGN_BOTTOM); |
| 982 | if (anchorParams != null) { |
| 983 | childParams.mBottom = anchorParams.mBottom - childParams.bottomMargin; |
| 984 | } else if (childParams.alignWithParent && rules[ALIGN_BOTTOM] != 0) { |
| 985 | if (myHeight >= 0) { |
| 986 | childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin; |
| 987 | } else { |
| 988 | // FIXME uh oh... |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (0 != rules[ALIGN_PARENT_TOP]) { |
| 993 | childParams.mTop = mPaddingTop + childParams.topMargin; |
| 994 | } |
| 995 | |
| 996 | if (0 != rules[ALIGN_PARENT_BOTTOM]) { |
| 997 | if (myHeight >= 0) { |
| 998 | childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin; |
| 999 | } else { |
| 1000 | // FIXME uh oh... |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | if (rules[ALIGN_BASELINE] != 0) { |
| 1005 | mHasBaselineAlignedChild = true; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | private View getRelatedView(int[] rules, int relation) { |
| 1010 | int id = rules[relation]; |
| 1011 | if (id != 0) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1012 | DependencyGraph.Node node = mGraph.mKeyNodes.get(id); |
Romain Guy | a0fd1d7 | 2009-06-24 14:25:43 -0700 | [diff] [blame] | 1013 | if (node == null) return null; |
| 1014 | View v = node.view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | |
| 1016 | // Find the first non-GONE view up the chain |
| 1017 | while (v.getVisibility() == View.GONE) { |
Fabrice Di Meglio | 13705ed | 2012-11-26 20:05:31 -0800 | [diff] [blame] | 1018 | rules = ((LayoutParams) v.getLayoutParams()).getRules(v.getLayoutDirection()); |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1019 | node = mGraph.mKeyNodes.get((rules[relation])); |
Romain Guy | a0fd1d7 | 2009-06-24 14:25:43 -0700 | [diff] [blame] | 1020 | if (node == null) return null; |
| 1021 | v = node.view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | return v; |
| 1025 | } |
| 1026 | |
| 1027 | return null; |
| 1028 | } |
| 1029 | |
| 1030 | private LayoutParams getRelatedViewParams(int[] rules, int relation) { |
| 1031 | View v = getRelatedView(rules, relation); |
| 1032 | if (v != null) { |
| 1033 | ViewGroup.LayoutParams params = v.getLayoutParams(); |
| 1034 | if (params instanceof LayoutParams) { |
| 1035 | return (LayoutParams) v.getLayoutParams(); |
| 1036 | } |
| 1037 | } |
| 1038 | return null; |
| 1039 | } |
| 1040 | |
| 1041 | private int getRelatedViewBaseline(int[] rules, int relation) { |
| 1042 | View v = getRelatedView(rules, relation); |
| 1043 | if (v != null) { |
| 1044 | return v.getBaseline(); |
| 1045 | } |
| 1046 | return -1; |
| 1047 | } |
| 1048 | |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1049 | private static void centerHorizontal(View child, LayoutParams params, int myWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | int childWidth = child.getMeasuredWidth(); |
| 1051 | int left = (myWidth - childWidth) / 2; |
| 1052 | |
| 1053 | params.mLeft = left; |
| 1054 | params.mRight = left + childWidth; |
| 1055 | } |
| 1056 | |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1057 | private static void centerVertical(View child, LayoutParams params, int myHeight) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | int childHeight = child.getMeasuredHeight(); |
| 1059 | int top = (myHeight - childHeight) / 2; |
| 1060 | |
| 1061 | params.mTop = top; |
| 1062 | params.mBottom = top + childHeight; |
| 1063 | } |
| 1064 | |
| 1065 | @Override |
| 1066 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 1067 | // The layout has actually already been performed and the positions |
| 1068 | // cached. Apply the cached values to the children. |
Fabrice Di Meglio | 13705ed | 2012-11-26 20:05:31 -0800 | [diff] [blame] | 1069 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | |
| 1071 | for (int i = 0; i < count; i++) { |
| 1072 | View child = getChildAt(i); |
| 1073 | if (child.getVisibility() != GONE) { |
| 1074 | RelativeLayout.LayoutParams st = |
| 1075 | (RelativeLayout.LayoutParams) child.getLayoutParams(); |
| 1076 | child.layout(st.mLeft, st.mTop, st.mRight, st.mBottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | @Override |
| 1082 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1083 | return new RelativeLayout.LayoutParams(getContext(), attrs); |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * Returns a set of layout parameters with a width of |
| 1088 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, |
| 1089 | * a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and no spanning. |
| 1090 | */ |
| 1091 | @Override |
| 1092 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
| 1093 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 1094 | } |
| 1095 | |
| 1096 | // Override to allow type-checking of LayoutParams. |
| 1097 | @Override |
| 1098 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1099 | return p instanceof RelativeLayout.LayoutParams; |
| 1100 | } |
| 1101 | |
| 1102 | @Override |
| 1103 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1104 | return new LayoutParams(p); |
| 1105 | } |
| 1106 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1107 | @Override |
| 1108 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 1109 | if (mTopToBottomLeftToRightSet == null) { |
| 1110 | mTopToBottomLeftToRightSet = new TreeSet<View>(new TopToBottomLeftToRightComparator()); |
| 1111 | } |
| 1112 | |
| 1113 | // sort children top-to-bottom and left-to-right |
| 1114 | for (int i = 0, count = getChildCount(); i < count; i++) { |
| 1115 | mTopToBottomLeftToRightSet.add(getChildAt(i)); |
| 1116 | } |
| 1117 | |
| 1118 | for (View view : mTopToBottomLeftToRightSet) { |
Svetoslav Ganov | 0b0a41d | 2011-09-07 18:06:03 -0700 | [diff] [blame] | 1119 | if (view.getVisibility() == View.VISIBLE |
| 1120 | && view.dispatchPopulateAccessibilityEvent(event)) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1121 | mTopToBottomLeftToRightSet.clear(); |
| 1122 | return true; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | mTopToBottomLeftToRightSet.clear(); |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1130 | @Override |
| 1131 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1132 | super.onInitializeAccessibilityEvent(event); |
| 1133 | event.setClassName(RelativeLayout.class.getName()); |
| 1134 | } |
| 1135 | |
| 1136 | @Override |
| 1137 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1138 | super.onInitializeAccessibilityNodeInfo(info); |
| 1139 | info.setClassName(RelativeLayout.class.getName()); |
| 1140 | } |
| 1141 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1142 | /** |
| 1143 | * Compares two views in left-to-right and top-to-bottom fashion. |
| 1144 | */ |
| 1145 | private class TopToBottomLeftToRightComparator implements Comparator<View> { |
| 1146 | public int compare(View first, View second) { |
| 1147 | // top - bottom |
| 1148 | int topDifference = first.getTop() - second.getTop(); |
| 1149 | if (topDifference != 0) { |
| 1150 | return topDifference; |
| 1151 | } |
| 1152 | // left - right |
| 1153 | int leftDifference = first.getLeft() - second.getLeft(); |
| 1154 | if (leftDifference != 0) { |
| 1155 | return leftDifference; |
| 1156 | } |
| 1157 | // break tie by height |
| 1158 | int heightDiference = first.getHeight() - second.getHeight(); |
| 1159 | if (heightDiference != 0) { |
| 1160 | return heightDiference; |
| 1161 | } |
| 1162 | // break tie by width |
| 1163 | int widthDiference = first.getWidth() - second.getWidth(); |
| 1164 | if (widthDiference != 0) { |
| 1165 | return widthDiference; |
| 1166 | } |
| 1167 | return 0; |
| 1168 | } |
| 1169 | } |
| 1170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | /** |
| 1172 | * Per-child layout information associated with RelativeLayout. |
| 1173 | * |
| 1174 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignWithParentIfMissing |
| 1175 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toLeftOf |
| 1176 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toRightOf |
| 1177 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_above |
| 1178 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_below |
| 1179 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBaseline |
| 1180 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignLeft |
| 1181 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignTop |
| 1182 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignRight |
| 1183 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBottom |
| 1184 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentLeft |
| 1185 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentTop |
| 1186 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentRight |
| 1187 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentBottom |
| 1188 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerInParent |
| 1189 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerHorizontal |
| 1190 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerVertical |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1191 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toStartOf |
| 1192 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toEndOf |
| 1193 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignStart |
| 1194 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignEnd |
| 1195 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentStart |
| 1196 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentEnd |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | */ |
| 1198 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1199 | @ViewDebug.ExportedProperty(category = "layout", resolveId = true, indexMapping = { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1200 | @ViewDebug.IntToString(from = ABOVE, to = "above"), |
| 1201 | @ViewDebug.IntToString(from = ALIGN_BASELINE, to = "alignBaseline"), |
| 1202 | @ViewDebug.IntToString(from = ALIGN_BOTTOM, to = "alignBottom"), |
| 1203 | @ViewDebug.IntToString(from = ALIGN_LEFT, to = "alignLeft"), |
| 1204 | @ViewDebug.IntToString(from = ALIGN_PARENT_BOTTOM, to = "alignParentBottom"), |
| 1205 | @ViewDebug.IntToString(from = ALIGN_PARENT_LEFT, to = "alignParentLeft"), |
| 1206 | @ViewDebug.IntToString(from = ALIGN_PARENT_RIGHT, to = "alignParentRight"), |
| 1207 | @ViewDebug.IntToString(from = ALIGN_PARENT_TOP, to = "alignParentTop"), |
| 1208 | @ViewDebug.IntToString(from = ALIGN_RIGHT, to = "alignRight"), |
| 1209 | @ViewDebug.IntToString(from = ALIGN_TOP, to = "alignTop"), |
| 1210 | @ViewDebug.IntToString(from = BELOW, to = "below"), |
| 1211 | @ViewDebug.IntToString(from = CENTER_HORIZONTAL, to = "centerHorizontal"), |
| 1212 | @ViewDebug.IntToString(from = CENTER_IN_PARENT, to = "center"), |
| 1213 | @ViewDebug.IntToString(from = CENTER_VERTICAL, to = "centerVertical"), |
| 1214 | @ViewDebug.IntToString(from = LEFT_OF, to = "leftOf"), |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1215 | @ViewDebug.IntToString(from = RIGHT_OF, to = "rightOf"), |
| 1216 | @ViewDebug.IntToString(from = ALIGN_START, to = "alignStart"), |
| 1217 | @ViewDebug.IntToString(from = ALIGN_END, to = "alignEnd"), |
| 1218 | @ViewDebug.IntToString(from = ALIGN_PARENT_START, to = "alignParentStart"), |
| 1219 | @ViewDebug.IntToString(from = ALIGN_PARENT_END, to = "alignParentEnd"), |
| 1220 | @ViewDebug.IntToString(from = START_OF, to = "startOf"), |
| 1221 | @ViewDebug.IntToString(from = END_OF, to = "endOf") |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1222 | }, mapping = { |
| 1223 | @ViewDebug.IntToString(from = TRUE, to = "true"), |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1224 | @ViewDebug.IntToString(from = 0, to = "false/NO_ID") |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1225 | }) |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | private int[] mRules = new int[VERB_COUNT]; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1228 | private int[] mInitialRules = new int[VERB_COUNT]; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | private int mLeft, mTop, mRight, mBottom; |
| 1231 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 1232 | private int mStart = DEFAULT_MARGIN_RELATIVE; |
| 1233 | private int mEnd = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1234 | |
| 1235 | private boolean mRulesChanged = false; |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1236 | private boolean mIsRtlCompatibilityMode = false; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | /** |
| 1239 | * When true, uses the parent as the anchor if the anchor doesn't exist or if |
| 1240 | * the anchor's visibility is GONE. |
| 1241 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1242 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | public boolean alignWithParent; |
| 1244 | |
| 1245 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1246 | super(c, attrs); |
| 1247 | |
| 1248 | TypedArray a = c.obtainStyledAttributes(attrs, |
| 1249 | com.android.internal.R.styleable.RelativeLayout_Layout); |
| 1250 | |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1251 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
| 1252 | mIsRtlCompatibilityMode = (targetSdkVersion < JELLY_BEAN_MR1 || |
| 1253 | !c.getApplicationInfo().hasRtlSupport()); |
| 1254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | final int[] rules = mRules; |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1256 | //noinspection MismatchedReadAndWriteOfArray |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1257 | final int[] initialRules = mInitialRules; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | |
| 1259 | final int N = a.getIndexCount(); |
| 1260 | for (int i = 0; i < N; i++) { |
| 1261 | int attr = a.getIndex(i); |
| 1262 | switch (attr) { |
| 1263 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignWithParentIfMissing: |
| 1264 | alignWithParent = a.getBoolean(attr, false); |
| 1265 | break; |
| 1266 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toLeftOf: |
| 1267 | rules[LEFT_OF] = a.getResourceId(attr, 0); |
| 1268 | break; |
| 1269 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toRightOf: |
| 1270 | rules[RIGHT_OF] = a.getResourceId(attr, 0); |
| 1271 | break; |
| 1272 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_above: |
| 1273 | rules[ABOVE] = a.getResourceId(attr, 0); |
| 1274 | break; |
| 1275 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_below: |
| 1276 | rules[BELOW] = a.getResourceId(attr, 0); |
| 1277 | break; |
| 1278 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignBaseline: |
| 1279 | rules[ALIGN_BASELINE] = a.getResourceId(attr, 0); |
| 1280 | break; |
| 1281 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignLeft: |
| 1282 | rules[ALIGN_LEFT] = a.getResourceId(attr, 0); |
| 1283 | break; |
| 1284 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignTop: |
| 1285 | rules[ALIGN_TOP] = a.getResourceId(attr, 0); |
| 1286 | break; |
| 1287 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignRight: |
| 1288 | rules[ALIGN_RIGHT] = a.getResourceId(attr, 0); |
| 1289 | break; |
| 1290 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignBottom: |
| 1291 | rules[ALIGN_BOTTOM] = a.getResourceId(attr, 0); |
| 1292 | break; |
| 1293 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentLeft: |
| 1294 | rules[ALIGN_PARENT_LEFT] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1295 | break; |
| 1296 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentTop: |
| 1297 | rules[ALIGN_PARENT_TOP] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1298 | break; |
| 1299 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentRight: |
| 1300 | rules[ALIGN_PARENT_RIGHT] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1301 | break; |
| 1302 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentBottom: |
| 1303 | rules[ALIGN_PARENT_BOTTOM] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1304 | break; |
| 1305 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerInParent: |
| 1306 | rules[CENTER_IN_PARENT] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1307 | break; |
| 1308 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerHorizontal: |
| 1309 | rules[CENTER_HORIZONTAL] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1310 | break; |
| 1311 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerVertical: |
| 1312 | rules[CENTER_VERTICAL] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1313 | break; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1314 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toStartOf: |
| 1315 | rules[START_OF] = a.getResourceId(attr, 0); |
| 1316 | break; |
| 1317 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toEndOf: |
| 1318 | rules[END_OF] = a.getResourceId(attr, 0); |
| 1319 | break; |
| 1320 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignStart: |
| 1321 | rules[ALIGN_START] = a.getResourceId(attr, 0); |
| 1322 | break; |
| 1323 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignEnd: |
| 1324 | rules[ALIGN_END] = a.getResourceId(attr, 0); |
| 1325 | break; |
| 1326 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentStart: |
| 1327 | rules[ALIGN_PARENT_START] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1328 | break; |
| 1329 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentEnd: |
| 1330 | rules[ALIGN_PARENT_END] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1331 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | } |
| 1333 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 1334 | mRulesChanged = true; |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1335 | System.arraycopy(rules, LEFT_OF, initialRules, LEFT_OF, VERB_COUNT); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | a.recycle(); |
| 1338 | } |
| 1339 | |
| 1340 | public LayoutParams(int w, int h) { |
| 1341 | super(w, h); |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * {@inheritDoc} |
| 1346 | */ |
| 1347 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1348 | super(source); |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * {@inheritDoc} |
| 1353 | */ |
| 1354 | public LayoutParams(ViewGroup.MarginLayoutParams source) { |
| 1355 | super(source); |
| 1356 | } |
| 1357 | |
| 1358 | @Override |
| 1359 | public String debug(String output) { |
| 1360 | return output + "ViewGroup.LayoutParams={ width=" + sizeToString(width) + |
| 1361 | ", height=" + sizeToString(height) + " }"; |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Adds a layout rule to be interpreted by the RelativeLayout. This |
| 1366 | * method should only be used for constraints that don't refer to another sibling |
| 1367 | * (e.g., CENTER_IN_PARENT) or take a boolean value ({@link RelativeLayout#TRUE} |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1368 | * for true or 0 for false). To specify a verb that takes a subject, use |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | * {@link #addRule(int, int)} instead. |
| 1370 | * |
| 1371 | * @param verb One of the verbs defined by |
| 1372 | * {@link android.widget.RelativeLayout RelativeLayout}, such as |
| 1373 | * ALIGN_WITH_PARENT_LEFT. |
| 1374 | * @see #addRule(int, int) |
| 1375 | */ |
| 1376 | public void addRule(int verb) { |
| 1377 | mRules[verb] = TRUE; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1378 | mInitialRules[verb] = TRUE; |
| 1379 | mRulesChanged = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * Adds a layout rule to be interpreted by the RelativeLayout. Use this for |
| 1384 | * verbs that take a target, such as a sibling (ALIGN_RIGHT) or a boolean |
| 1385 | * value (VISIBLE). |
| 1386 | * |
| 1387 | * @param verb One of the verbs defined by |
| 1388 | * {@link android.widget.RelativeLayout RelativeLayout}, such as |
| 1389 | * ALIGN_WITH_PARENT_LEFT. |
| 1390 | * @param anchor The id of another view to use as an anchor, |
| 1391 | * or a boolean value(represented as {@link RelativeLayout#TRUE}) |
| 1392 | * for true or 0 for false). For verbs that don't refer to another sibling |
| 1393 | * (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1. |
| 1394 | * @see #addRule(int) |
| 1395 | */ |
| 1396 | public void addRule(int verb, int anchor) { |
| 1397 | mRules[verb] = anchor; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1398 | mInitialRules[verb] = anchor; |
| 1399 | mRulesChanged = true; |
| 1400 | } |
| 1401 | |
Fabrice Di Meglio | a4c1030 | 2012-07-26 12:27:56 -0700 | [diff] [blame] | 1402 | /** |
| 1403 | * Removes a layout rule to be interpreted by the RelativeLayout. |
| 1404 | * |
| 1405 | * @param verb One of the verbs defined by |
| 1406 | * {@link android.widget.RelativeLayout RelativeLayout}, such as |
| 1407 | * ALIGN_WITH_PARENT_LEFT. |
| 1408 | * @see #addRule(int) |
| 1409 | * @see #addRule(int, int) |
| 1410 | */ |
| 1411 | public void removeRule(int verb) { |
| 1412 | mRules[verb] = 0; |
| 1413 | mInitialRules[verb] = 0; |
| 1414 | mRulesChanged = true; |
| 1415 | } |
| 1416 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1417 | private boolean hasRelativeRules() { |
| 1418 | return (mInitialRules[START_OF] != 0 || mInitialRules[END_OF] != 0 || |
| 1419 | mInitialRules[ALIGN_START] != 0 || mInitialRules[ALIGN_END] != 0 || |
| 1420 | mInitialRules[ALIGN_PARENT_START] != 0 || mInitialRules[ALIGN_PARENT_END] != 0); |
| 1421 | } |
| 1422 | |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1423 | // The way we are resolving rules depends on the layout direction and if we are pre JB MR1 |
| 1424 | // or not. |
| 1425 | // |
| 1426 | // If we are pre JB MR1 (said as "RTL compatibility mode"), "left"/"right" rules are having |
| 1427 | // predominance over any "start/end" rules that could have been defined. A special case: |
| 1428 | // if no "left"/"right" rule has been defined and "start"/"end" rules are defined then we |
| 1429 | // resolve those "start"/"end" rules to "left"/"right" respectively. |
| 1430 | // |
| 1431 | // If we are JB MR1+, then "start"/"end" rules are having predominance over "left"/"right" |
| 1432 | // rules. If no "start"/"end" rule is defined then we use "left"/"right" rules. |
| 1433 | // |
| 1434 | // In all cases, the result of the resolution should clear the "start"/"end" rules to leave |
| 1435 | // only the "left"/"right" rules at the end. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1436 | private void resolveRules(int layoutDirection) { |
| 1437 | final boolean isLayoutRtl = (layoutDirection == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1438 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1439 | // Reset to initial state |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1440 | System.arraycopy(mInitialRules, LEFT_OF, mRules, LEFT_OF, VERB_COUNT); |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1441 | |
| 1442 | // Apply rules depending on direction and if we are in RTL compatibility mode |
| 1443 | if (mIsRtlCompatibilityMode) { |
| 1444 | if (mRules[ALIGN_START] != 0) { |
| 1445 | if (mRules[ALIGN_LEFT] == 0) { |
| 1446 | // "left" rule is not defined but "start" rule is: use the "start" rule as |
| 1447 | // the "left" rule |
| 1448 | mRules[ALIGN_LEFT] = mRules[ALIGN_START]; |
| 1449 | } |
| 1450 | mRules[ALIGN_START] = 0; |
| 1451 | } |
| 1452 | |
| 1453 | if (mRules[ALIGN_END] != 0) { |
| 1454 | if (mRules[ALIGN_RIGHT] == 0) { |
| 1455 | // "right" rule is not defined but "end" rule is: use the "end" rule as the |
| 1456 | // "right" rule |
| 1457 | mRules[ALIGN_RIGHT] = mRules[ALIGN_END]; |
| 1458 | } |
| 1459 | mRules[ALIGN_END] = 0; |
| 1460 | } |
| 1461 | |
| 1462 | if (mRules[START_OF] != 0) { |
| 1463 | if (mRules[LEFT_OF] == 0) { |
| 1464 | // "left" rule is not defined but "start" rule is: use the "start" rule as |
| 1465 | // the "left" rule |
| 1466 | mRules[LEFT_OF] = mRules[START_OF]; |
| 1467 | } |
| 1468 | mRules[START_OF] = 0; |
| 1469 | } |
| 1470 | |
| 1471 | if (mRules[END_OF] != 0) { |
| 1472 | if (mRules[RIGHT_OF] == 0) { |
| 1473 | // "right" rule is not defined but "end" rule is: use the "end" rule as the |
| 1474 | // "right" rule |
| 1475 | mRules[RIGHT_OF] = mRules[END_OF]; |
| 1476 | } |
| 1477 | mRules[END_OF] = 0; |
| 1478 | } |
| 1479 | |
| 1480 | if (mRules[ALIGN_PARENT_START] != 0) { |
| 1481 | if (mRules[ALIGN_PARENT_LEFT] == 0) { |
| 1482 | // "left" rule is not defined but "start" rule is: use the "start" rule as |
| 1483 | // the "left" rule |
| 1484 | mRules[ALIGN_PARENT_LEFT] = mRules[ALIGN_PARENT_START]; |
| 1485 | } |
| 1486 | mRules[ALIGN_PARENT_START] = 0; |
| 1487 | } |
| 1488 | |
| 1489 | if (mRules[ALIGN_PARENT_RIGHT] == 0) { |
| 1490 | if (mRules[ALIGN_PARENT_RIGHT] == 0) { |
| 1491 | // "right" rule is not defined but "end" rule is: use the "end" rule as the |
| 1492 | // "right" rule |
| 1493 | mRules[ALIGN_PARENT_RIGHT] = mRules[ALIGN_PARENT_END]; |
| 1494 | } |
| 1495 | mRules[ALIGN_PARENT_END] = 0; |
| 1496 | } |
| 1497 | } else { |
| 1498 | // JB MR1+ case |
| 1499 | if ((mRules[ALIGN_START] != 0 || mRules[ALIGN_END] != 0) && |
| 1500 | (mRules[ALIGN_LEFT] != 0 || mRules[ALIGN_RIGHT] != 0)) { |
| 1501 | // "start"/"end" rules take precedence over "left"/"right" rules |
| 1502 | mRules[ALIGN_LEFT] = 0; |
| 1503 | mRules[ALIGN_RIGHT] = 0; |
| 1504 | } |
| 1505 | if (mRules[ALIGN_START] != 0) { |
| 1506 | // "start" rule resolved to "left" or "right" depending on the direction |
| 1507 | mRules[isLayoutRtl ? ALIGN_RIGHT : ALIGN_LEFT] = mRules[ALIGN_START]; |
| 1508 | mRules[ALIGN_START] = 0; |
| 1509 | } |
| 1510 | if (mRules[ALIGN_END] != 0) { |
| 1511 | // "end" rule resolved to "left" or "right" depending on the direction |
| 1512 | mRules[isLayoutRtl ? ALIGN_LEFT : ALIGN_RIGHT] = mRules[ALIGN_END]; |
| 1513 | mRules[ALIGN_END] = 0; |
| 1514 | } |
| 1515 | |
| 1516 | if ((mRules[START_OF] != 0 || mRules[END_OF] != 0) && |
| 1517 | (mRules[LEFT_OF] != 0 || mRules[RIGHT_OF] != 0)) { |
| 1518 | // "start"/"end" rules take precedence over "left"/"right" rules |
| 1519 | mRules[LEFT_OF] = 0; |
| 1520 | mRules[RIGHT_OF] = 0; |
| 1521 | } |
| 1522 | if (mRules[START_OF] != 0) { |
| 1523 | // "start" rule resolved to "left" or "right" depending on the direction |
| 1524 | mRules[isLayoutRtl ? RIGHT_OF : LEFT_OF] = mRules[START_OF]; |
| 1525 | mRules[START_OF] = 0; |
| 1526 | } |
| 1527 | if (mRules[END_OF] != 0) { |
| 1528 | // "end" rule resolved to "left" or "right" depending on the direction |
| 1529 | mRules[isLayoutRtl ? LEFT_OF : RIGHT_OF] = mRules[END_OF]; |
| 1530 | mRules[END_OF] = 0; |
| 1531 | } |
| 1532 | |
| 1533 | if ((mRules[ALIGN_PARENT_START] != 0 || mRules[ALIGN_PARENT_END] != 0) && |
| 1534 | (mRules[ALIGN_PARENT_LEFT] != 0 || mRules[ALIGN_PARENT_RIGHT] != 0)) { |
| 1535 | // "start"/"end" rules take precedence over "left"/"right" rules |
| 1536 | mRules[ALIGN_PARENT_LEFT] = 0; |
| 1537 | mRules[ALIGN_PARENT_RIGHT] = 0; |
| 1538 | } |
| 1539 | if (mRules[ALIGN_PARENT_START] != 0) { |
| 1540 | // "start" rule resolved to "left" or "right" depending on the direction |
| 1541 | mRules[isLayoutRtl ? ALIGN_PARENT_RIGHT : ALIGN_PARENT_LEFT] = mRules[ALIGN_PARENT_START]; |
| 1542 | mRules[ALIGN_PARENT_START] = 0; |
| 1543 | } |
| 1544 | if (mRules[ALIGN_PARENT_END] != 0) { |
| 1545 | // "end" rule resolved to "left" or "right" depending on the direction |
| 1546 | mRules[isLayoutRtl ? ALIGN_PARENT_LEFT : ALIGN_PARENT_RIGHT] = mRules[ALIGN_PARENT_END]; |
| 1547 | mRules[ALIGN_PARENT_END] = 0; |
| 1548 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1549 | } |
| 1550 | mRulesChanged = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * Retrieves a complete list of all supported rules, where the index is the rule |
| 1555 | * verb, and the element value is the value specified, or "false" if it was never |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1556 | * set. If there are relative rules defined (*_START / *_END), they will be resolved |
| 1557 | * depending on the layout direction. |
| 1558 | * |
| 1559 | * @param layoutDirection the direction of the layout. |
| 1560 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 1561 | * or {@link View#LAYOUT_DIRECTION_RTL} |
| 1562 | * @return the supported rules |
| 1563 | * @see #addRule(int, int) |
| 1564 | * |
| 1565 | * @hide |
| 1566 | */ |
| 1567 | public int[] getRules(int layoutDirection) { |
| 1568 | if (hasRelativeRules() && |
| 1569 | (mRulesChanged || layoutDirection != getLayoutDirection())) { |
| 1570 | resolveRules(layoutDirection); |
| 1571 | if (layoutDirection != getLayoutDirection()) { |
| 1572 | setLayoutDirection(layoutDirection); |
| 1573 | } |
| 1574 | } |
| 1575 | return mRules; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * Retrieves a complete list of all supported rules, where the index is the rule |
| 1580 | * verb, and the element value is the value specified, or "false" if it was never |
| 1581 | * set. There will be no resolution of relative rules done. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | * |
| 1583 | * @return the supported rules |
| 1584 | * @see #addRule(int, int) |
| 1585 | */ |
| 1586 | public int[] getRules() { |
| 1587 | return mRules; |
| 1588 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1589 | |
| 1590 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 1591 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1592 | final boolean isLayoutRtl = isLayoutRtl(); |
| 1593 | if (isLayoutRtl) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 1594 | if (mStart != DEFAULT_MARGIN_RELATIVE) mRight = mStart; |
| 1595 | if (mEnd != DEFAULT_MARGIN_RELATIVE) mLeft = mEnd; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1596 | } else { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 1597 | if (mStart != DEFAULT_MARGIN_RELATIVE) mLeft = mStart; |
| 1598 | if (mEnd != DEFAULT_MARGIN_RELATIVE) mRight = mEnd; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | if (hasRelativeRules() && layoutDirection != getLayoutDirection()) { |
| 1602 | resolveRules(layoutDirection); |
| 1603 | } |
| 1604 | // This will set the layout direction |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 1605 | super.resolveLayoutDirection(layoutDirection); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1608 | |
| 1609 | private static class DependencyGraph { |
| 1610 | /** |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1611 | * List of all views in the graph. |
| 1612 | */ |
| 1613 | private ArrayList<Node> mNodes = new ArrayList<Node>(); |
| 1614 | |
| 1615 | /** |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1616 | * List of nodes in the graph. Each node is identified by its |
| 1617 | * view id (see View#getId()). |
| 1618 | */ |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1619 | private SparseArray<Node> mKeyNodes = new SparseArray<Node>(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1620 | |
| 1621 | /** |
| 1622 | * Temporary data structure used to build the list of roots |
| 1623 | * for this graph. |
| 1624 | */ |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1625 | private ArrayDeque<Node> mRoots = new ArrayDeque<Node>(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1626 | |
| 1627 | /** |
| 1628 | * Clears the graph. |
| 1629 | */ |
| 1630 | void clear() { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1631 | final ArrayList<Node> nodes = mNodes; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1632 | final int count = nodes.size(); |
| 1633 | |
| 1634 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1635 | nodes.get(i).release(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1636 | } |
| 1637 | nodes.clear(); |
| 1638 | |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1639 | mKeyNodes.clear(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1640 | mRoots.clear(); |
| 1641 | } |
| 1642 | |
| 1643 | /** |
| 1644 | * Adds a view to the graph. |
| 1645 | * |
| 1646 | * @param view The view to be added as a node to the graph. |
| 1647 | */ |
| 1648 | void add(View view) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1649 | final int id = view.getId(); |
| 1650 | final Node node = Node.acquire(view); |
| 1651 | |
| 1652 | if (id != View.NO_ID) { |
| 1653 | mKeyNodes.put(id, node); |
| 1654 | } |
| 1655 | |
| 1656 | mNodes.add(node); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * Builds a sorted list of views. The sorting order depends on the dependencies |
| 1661 | * between the view. For instance, if view C needs view A to be processed first |
| 1662 | * and view A needs view B to be processed first, the dependency graph |
| 1663 | * is: B -> A -> C. The sorted array will contain views B, A and C in this order. |
| 1664 | * |
| 1665 | * @param sorted The sorted list of views. The length of this array must |
| 1666 | * be equal to getChildCount(). |
| 1667 | * @param rules The list of rules to take into account. |
| 1668 | */ |
| 1669 | void getSortedViews(View[] sorted, int... rules) { |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1670 | final ArrayDeque<Node> roots = findRoots(rules); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1671 | int index = 0; |
| 1672 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1673 | Node node; |
| 1674 | while ((node = roots.pollLast()) != null) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1675 | final View view = node.view; |
| 1676 | final int key = view.getId(); |
| 1677 | |
| 1678 | sorted[index++] = view; |
| 1679 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1680 | final HashMap<Node, DependencyGraph> dependents = node.dependents; |
| 1681 | for (Node dependent : dependents.keySet()) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1682 | final SparseArray<Node> dependencies = dependent.dependencies; |
| 1683 | |
| 1684 | dependencies.remove(key); |
| 1685 | if (dependencies.size() == 0) { |
| 1686 | roots.add(dependent); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | if (index < sorted.length) { |
| 1692 | throw new IllegalStateException("Circular dependencies cannot exist" |
| 1693 | + " in RelativeLayout"); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * Finds the roots of the graph. A root is a node with no dependency and |
| 1699 | * with [0..n] dependents. |
| 1700 | * |
| 1701 | * @param rulesFilter The list of rules to consider when building the |
| 1702 | * dependencies |
| 1703 | * |
| 1704 | * @return A list of node, each being a root of the graph |
| 1705 | */ |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1706 | private ArrayDeque<Node> findRoots(int[] rulesFilter) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1707 | final SparseArray<Node> keyNodes = mKeyNodes; |
| 1708 | final ArrayList<Node> nodes = mNodes; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1709 | final int count = nodes.size(); |
| 1710 | |
| 1711 | // Find roots can be invoked several times, so make sure to clear |
| 1712 | // all dependents and dependencies before running the algorithm |
| 1713 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1714 | final Node node = nodes.get(i); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1715 | node.dependents.clear(); |
| 1716 | node.dependencies.clear(); |
| 1717 | } |
| 1718 | |
| 1719 | // Builds up the dependents and dependencies for each node of the graph |
| 1720 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1721 | final Node node = nodes.get(i); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1722 | |
| 1723 | final LayoutParams layoutParams = (LayoutParams) node.view.getLayoutParams(); |
| 1724 | final int[] rules = layoutParams.mRules; |
| 1725 | final int rulesCount = rulesFilter.length; |
| 1726 | |
| 1727 | // Look only the the rules passed in parameter, this way we build only the |
| 1728 | // dependencies for a specific set of rules |
| 1729 | for (int j = 0; j < rulesCount; j++) { |
| 1730 | final int rule = rules[rulesFilter[j]]; |
| 1731 | if (rule > 0) { |
| 1732 | // The node this node depends on |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1733 | final Node dependency = keyNodes.get(rule); |
Romain Guy | da3003e | 2009-07-19 19:47:42 -0700 | [diff] [blame] | 1734 | // Skip unknowns and self dependencies |
| 1735 | if (dependency == null || dependency == node) { |
Romain Guy | b8f8de8 | 2009-06-25 12:03:56 -0700 | [diff] [blame] | 1736 | continue; |
| 1737 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1738 | // Add the current node as a dependent |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1739 | dependency.dependents.put(node, this); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1740 | // Add a dependency to the current node |
| 1741 | node.dependencies.put(rule, dependency); |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1746 | final ArrayDeque<Node> roots = mRoots; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1747 | roots.clear(); |
| 1748 | |
| 1749 | // Finds all the roots in the graph: all nodes with no dependencies |
| 1750 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1751 | final Node node = nodes.get(i); |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1752 | if (node.dependencies.size() == 0) roots.addLast(node); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | return roots; |
| 1756 | } |
| 1757 | |
| 1758 | /** |
| 1759 | * Prints the dependency graph for the specified rules. |
| 1760 | * |
| 1761 | * @param resources The context's resources to print the ids. |
| 1762 | * @param rules The list of rules to take into account. |
| 1763 | */ |
| 1764 | void log(Resources resources, int... rules) { |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1765 | final ArrayDeque<Node> roots = findRoots(rules); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1766 | for (Node node : roots) { |
| 1767 | printNode(resources, node); |
| 1768 | } |
| 1769 | } |
| 1770 | |
Romain Guy | 9fffa1e | 2009-06-24 12:42:43 -0700 | [diff] [blame] | 1771 | static void printViewId(Resources resources, View view) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1772 | if (view.getId() != View.NO_ID) { |
| 1773 | d(LOG_TAG, resources.getResourceEntryName(view.getId())); |
| 1774 | } else { |
| 1775 | d(LOG_TAG, "NO_ID"); |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | private static void appendViewId(Resources resources, Node node, StringBuilder buffer) { |
| 1780 | if (node.view.getId() != View.NO_ID) { |
| 1781 | buffer.append(resources.getResourceEntryName(node.view.getId())); |
| 1782 | } else { |
| 1783 | buffer.append("NO_ID"); |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | private static void printNode(Resources resources, Node node) { |
| 1788 | if (node.dependents.size() == 0) { |
| 1789 | printViewId(resources, node.view); |
| 1790 | } else { |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1791 | for (Node dependent : node.dependents.keySet()) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1792 | StringBuilder buffer = new StringBuilder(); |
| 1793 | appendViewId(resources, node, buffer); |
| 1794 | printdependents(resources, dependent, buffer); |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | private static void printdependents(Resources resources, Node node, StringBuilder buffer) { |
| 1800 | buffer.append(" -> "); |
| 1801 | appendViewId(resources, node, buffer); |
| 1802 | |
| 1803 | if (node.dependents.size() == 0) { |
| 1804 | d(LOG_TAG, buffer.toString()); |
| 1805 | } else { |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1806 | for (Node dependent : node.dependents.keySet()) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1807 | StringBuilder subBuffer = new StringBuilder(buffer); |
| 1808 | printdependents(resources, dependent, subBuffer); |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * A node in the dependency graph. A node is a view, its list of dependencies |
| 1815 | * and its list of dependents. |
| 1816 | * |
| 1817 | * A node with no dependent is considered a root of the graph. |
| 1818 | */ |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1819 | static class Node { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1820 | /** |
| 1821 | * The view representing this node in the layout. |
| 1822 | */ |
| 1823 | View view; |
| 1824 | |
| 1825 | /** |
| 1826 | * The list of dependents for this node; a dependent is a node |
| 1827 | * that needs this node to be processed first. |
| 1828 | */ |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1829 | final HashMap<Node, DependencyGraph> dependents = new HashMap<Node, DependencyGraph>(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1830 | |
| 1831 | /** |
| 1832 | * The list of dependencies for this node. |
| 1833 | */ |
| 1834 | final SparseArray<Node> dependencies = new SparseArray<Node>(); |
| 1835 | |
| 1836 | /* |
| 1837 | * START POOL IMPLEMENTATION |
| 1838 | */ |
Romain Guy | baac463 | 2009-06-29 14:28:29 -0700 | [diff] [blame] | 1839 | // The pool is static, so all nodes instances are shared across |
| 1840 | // activities, that's why we give it a rather high limit |
| 1841 | private static final int POOL_LIMIT = 100; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1842 | private static final SimplePool<Node> sPool = new SimplePool<Node>(POOL_LIMIT); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1843 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1844 | static Node acquire(View view) { |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1845 | Node node = sPool.acquire(); |
| 1846 | if (node == null) { |
| 1847 | node = new Node(); |
| 1848 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1849 | node.view = view; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1850 | return node; |
| 1851 | } |
| 1852 | |
| 1853 | void release() { |
| 1854 | view = null; |
| 1855 | dependents.clear(); |
| 1856 | dependencies.clear(); |
| 1857 | |
| 1858 | sPool.release(this); |
| 1859 | } |
| 1860 | /* |
| 1861 | * END POOL IMPLEMENTATION |
| 1862 | */ |
| 1863 | } |
| 1864 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | } |