blob: b9babdcc2e039855ddf0b84715d532948c9d0dd1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.view;
18
Svetoslav Ganov80943d82013-01-02 10:25:37 -080019import android.app.ActivityThread;
Christopher Tatea53146c2010-09-07 11:57:52 -070020import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080022import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070026import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070028import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070029import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.LinearGradient;
31import android.graphics.Matrix;
32import android.graphics.Paint;
33import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070034import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
37import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070038import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.Region;
40import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.drawable.ColorDrawable;
42import android.graphics.drawable.Drawable;
Jeff Brownbd6e1502012-08-28 03:27:37 -070043import android.hardware.display.DisplayManagerGlobal;
Adam Powell7da4b732012-12-07 15:28:33 -080044import android.os.Build;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Handler;
47import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070052import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070053import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070055import android.util.FloatProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080057import android.util.Pools.SynchronizedPool;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
Jeff Sharkey36901b62012-09-19 19:06:22 -070085import com.google.android.collect.Lists;
86import com.google.android.collect.Maps;
Doug Feltcb3791202011-07-07 11:57:48 -070087
Christopher Tatea0374192010-10-05 13:06:41 -070088import java.lang.ref.WeakReference;
Jeff Sharkey36901b62012-09-19 19:06:22 -070089import java.lang.reflect.Field;
svetoslavganov75986cf2009-05-14 22:28:01 -070090import java.lang.reflect.InvocationTargetException;
91import java.lang.reflect.Method;
Jeff Sharkey36901b62012-09-19 19:06:22 -070092import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.util.ArrayList;
94import java.util.Arrays;
Jeff Sharkey36901b62012-09-19 19:06:22 -070095import java.util.Collections;
96import java.util.HashMap;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070097import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080098import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070099import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101/**
102 * <p>
103 * This class represents the basic building block for user interface components. A View
104 * occupies a rectangular area on the screen and is responsible for drawing and
105 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700106 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
108 * are invisible containers that hold other Views (or other ViewGroups) and define
109 * their layout properties.
110 * </p>
111 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700112 * <div class="special reference">
113 * <h3>Developer Guides</h3>
114 * <p>For information about using this class to develop your application's user interface,
115 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * <a name="Using"></a>
119 * <h3>Using Views</h3>
120 * <p>
121 * All of the views in a window are arranged in a single tree. You can add views
122 * either from code or by specifying a tree of views in one or more XML layout
123 * files. There are many specialized subclasses of views that act as controls or
124 * are capable of displaying text, images, or other content.
125 * </p>
126 * <p>
127 * Once you have created a tree of views, there are typically a few types of
128 * common operations you may wish to perform:
129 * <ul>
130 * <li><strong>Set properties:</strong> for example setting the text of a
131 * {@link android.widget.TextView}. The available properties and the methods
132 * that set them will vary among the different subclasses of views. Note that
133 * properties that are known at build time can be set in the XML layout
134 * files.</li>
135 * <li><strong>Set focus:</strong> The framework will handled moving focus in
136 * response to user input. To force focus to a specific view, call
137 * {@link #requestFocus}.</li>
138 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
139 * that will be notified when something interesting happens to the view. For
140 * example, all views will let you set a listener to be notified when the view
141 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700142 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700143 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700144 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700146 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 * </ul>
148 * </p>
149 * <p><em>
150 * Note: The Android framework is responsible for measuring, laying out and
151 * drawing views. You should not call methods that perform these actions on
152 * views yourself unless you are actually implementing a
153 * {@link android.view.ViewGroup}.
154 * </em></p>
155 *
156 * <a name="Lifecycle"></a>
157 * <h3>Implementing a Custom View</h3>
158 *
159 * <p>
160 * To implement a custom view, you will usually begin by providing overrides for
161 * some of the standard methods that the framework calls on all views. You do
162 * not need to override all of these methods. In fact, you can start by just
163 * overriding {@link #onDraw(android.graphics.Canvas)}.
164 * <table border="2" width="85%" align="center" cellpadding="5">
165 * <thead>
166 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
167 * </thead>
168 *
169 * <tbody>
170 * <tr>
171 * <td rowspan="2">Creation</td>
172 * <td>Constructors</td>
173 * <td>There is a form of the constructor that are called when the view
174 * is created from code and a form that is called when the view is
175 * inflated from a layout file. The second form should parse and apply
176 * any attributes defined in the layout file.
177 * </td>
178 * </tr>
179 * <tr>
180 * <td><code>{@link #onFinishInflate()}</code></td>
181 * <td>Called after a view and all of its children has been inflated
182 * from XML.</td>
183 * </tr>
184 *
185 * <tr>
186 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called to determine the size requirements for this view and all
189 * of its children.
190 * </td>
191 * </tr>
192 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when this view should assign a size and position to all
195 * of its children.
196 * </td>
197 * </tr>
198 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the size of this view has changed.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when the view should render its content.
208 * </td>
209 * </tr>
210 *
211 * <tr>
212 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900214 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900219 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 * </td>
221 * </tr>
222 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when a trackball motion event occurs.
225 * </td>
226 * </tr>
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when a touch screen motion event occurs.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700235 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <td>Called when the view gains or loses focus.
237 * </td>
238 * </tr>
239 *
240 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700241 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * <td>Called when the window containing the view gains or loses focus.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td rowspan="3">Attaching</td>
248 * <td><code>{@link #onAttachedToWindow()}</code></td>
249 * <td>Called when the view is attached to a window.
250 * </td>
251 * </tr>
252 *
253 * <tr>
254 * <td><code>{@link #onDetachedFromWindow}</code></td>
255 * <td>Called when the view is detached from its window.
256 * </td>
257 * </tr>
258 *
259 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700260 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * <td>Called when the visibility of the window containing the view
262 * has changed.
263 * </td>
264 * </tr>
265 * </tbody>
266 *
267 * </table>
268 * </p>
269 *
270 * <a name="IDs"></a>
271 * <h3>IDs</h3>
272 * Views may have an integer id associated with them. These ids are typically
273 * assigned in the layout XML files, and are used to find specific views within
274 * the view tree. A common pattern is to:
275 * <ul>
276 * <li>Define a Button in the layout file and assign it a unique ID.
277 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700278 * &lt;Button
279 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 * android:layout_width="wrap_content"
281 * android:layout_height="wrap_content"
282 * android:text="@string/my_button_text"/&gt;
283 * </pre></li>
284 * <li>From the onCreate method of an Activity, find the Button
285 * <pre class="prettyprint">
286 * Button myButton = (Button) findViewById(R.id.my_button);
287 * </pre></li>
288 * </ul>
289 * <p>
290 * View IDs need not be unique throughout the tree, but it is good practice to
291 * ensure that they are at least unique within the part of the tree you are
292 * searching.
293 * </p>
294 *
295 * <a name="Position"></a>
296 * <h3>Position</h3>
297 * <p>
298 * The geometry of a view is that of a rectangle. A view has a location,
299 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
300 * two dimensions, expressed as a width and a height. The unit for location
301 * and dimensions is the pixel.
302 * </p>
303 *
304 * <p>
305 * It is possible to retrieve the location of a view by invoking the methods
306 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
307 * coordinate of the rectangle representing the view. The latter returns the
308 * top, or Y, coordinate of the rectangle representing the view. These methods
309 * both return the location of the view relative to its parent. For instance,
310 * when getLeft() returns 20, that means the view is located 20 pixels to the
311 * right of the left edge of its direct parent.
312 * </p>
313 *
314 * <p>
315 * In addition, several convenience methods are offered to avoid unnecessary
316 * computations, namely {@link #getRight()} and {@link #getBottom()}.
317 * These methods return the coordinates of the right and bottom edges of the
318 * rectangle representing the view. For instance, calling {@link #getRight()}
319 * is similar to the following computation: <code>getLeft() + getWidth()</code>
320 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
321 * </p>
322 *
323 * <a name="SizePaddingMargins"></a>
324 * <h3>Size, padding and margins</h3>
325 * <p>
326 * The size of a view is expressed with a width and a height. A view actually
327 * possess two pairs of width and height values.
328 * </p>
329 *
330 * <p>
331 * The first pair is known as <em>measured width</em> and
332 * <em>measured height</em>. These dimensions define how big a view wants to be
333 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
334 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
335 * and {@link #getMeasuredHeight()}.
336 * </p>
337 *
338 * <p>
339 * The second pair is simply known as <em>width</em> and <em>height</em>, or
340 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
341 * dimensions define the actual size of the view on screen, at drawing time and
342 * after layout. These values may, but do not have to, be different from the
343 * measured width and height. The width and height can be obtained by calling
344 * {@link #getWidth()} and {@link #getHeight()}.
345 * </p>
346 *
347 * <p>
348 * To measure its dimensions, a view takes into account its padding. The padding
349 * is expressed in pixels for the left, top, right and bottom parts of the view.
350 * Padding can be used to offset the content of the view by a specific amount of
351 * pixels. For instance, a left padding of 2 will push the view's content by
352 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700353 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
354 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
355 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
356 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * </p>
358 *
359 * <p>
360 * Even though a view can define a padding, it does not provide any support for
361 * margins. However, view groups provide such a support. Refer to
362 * {@link android.view.ViewGroup} and
363 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
364 * </p>
365 *
366 * <a name="Layout"></a>
367 * <h3>Layout</h3>
368 * <p>
369 * Layout is a two pass process: a measure pass and a layout pass. The measuring
370 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
371 * of the view tree. Each view pushes dimension specifications down the tree
372 * during the recursion. At the end of the measure pass, every view has stored
373 * its measurements. The second pass happens in
374 * {@link #layout(int,int,int,int)} and is also top-down. During
375 * this pass each parent is responsible for positioning all of its children
376 * using the sizes computed in the measure pass.
377 * </p>
378 *
379 * <p>
380 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
381 * {@link #getMeasuredHeight()} values must be set, along with those for all of
382 * that view's descendants. A view's measured width and measured height values
383 * must respect the constraints imposed by the view's parents. This guarantees
384 * that at the end of the measure pass, all parents accept all of their
385 * children's measurements. A parent view may call measure() more than once on
386 * its children. For example, the parent may measure each child once with
387 * unspecified dimensions to find out how big they want to be, then call
388 * measure() on them again with actual numbers if the sum of all the children's
389 * unconstrained sizes is too big or too small.
390 * </p>
391 *
392 * <p>
393 * The measure pass uses two classes to communicate dimensions. The
394 * {@link MeasureSpec} class is used by views to tell their parents how they
395 * want to be measured and positioned. The base LayoutParams class just
396 * describes how big the view wants to be for both width and height. For each
397 * dimension, it can specify one of:
398 * <ul>
399 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800400 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * (minus padding)
402 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
403 * enclose its content (plus padding).
404 * </ul>
405 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
406 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
407 * an X and Y value.
408 * </p>
409 *
410 * <p>
411 * MeasureSpecs are used to push requirements down the tree from parent to
412 * child. A MeasureSpec can be in one of three modes:
413 * <ul>
414 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
415 * of a child view. For example, a LinearLayout may call measure() on its child
416 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
417 * tall the child view wants to be given a width of 240 pixels.
418 * <li>EXACTLY: This is used by the parent to impose an exact size on the
419 * child. The child must use this size, and guarantee that all of its
420 * descendants will fit within this size.
421 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
422 * child. The child must gurantee that it and all of its descendants will fit
423 * within this size.
424 * </ul>
425 * </p>
426 *
427 * <p>
428 * To intiate a layout, call {@link #requestLayout}. This method is typically
429 * called by a view on itself when it believes that is can no longer fit within
430 * its current bounds.
431 * </p>
432 *
433 * <a name="Drawing"></a>
434 * <h3>Drawing</h3>
435 * <p>
436 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700437 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * this means that parents will draw before (i.e., behind) their children, with
439 * siblings drawn in the order they appear in the tree.
440 * If you set a background drawable for a View, then the View will draw it for you
441 * before calling back to its <code>onDraw()</code> method.
442 * </p>
443 *
444 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700445 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * </p>
447 *
448 * <p>
449 * To force a view to draw, call {@link #invalidate()}.
450 * </p>
451 *
452 * <a name="EventHandlingThreading"></a>
453 * <h3>Event Handling and Threading</h3>
454 * <p>
455 * The basic cycle of a view is as follows:
456 * <ol>
457 * <li>An event comes in and is dispatched to the appropriate view. The view
458 * handles the event and notifies any listeners.</li>
459 * <li>If in the course of processing the event, the view's bounds may need
460 * to be changed, the view will call {@link #requestLayout()}.</li>
461 * <li>Similarly, if in the course of processing the event the view's appearance
462 * may need to be changed, the view will call {@link #invalidate()}.</li>
463 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
464 * the framework will take care of measuring, laying out, and drawing the tree
465 * as appropriate.</li>
466 * </ol>
467 * </p>
468 *
469 * <p><em>Note: The entire view tree is single threaded. You must always be on
470 * the UI thread when calling any method on any view.</em>
471 * If you are doing work on other threads and want to update the state of a view
472 * from that thread, you should use a {@link Handler}.
473 * </p>
474 *
475 * <a name="FocusHandling"></a>
476 * <h3>Focus Handling</h3>
477 * <p>
478 * The framework will handle routine focus movement in response to user input.
479 * This includes changing the focus as views are removed or hidden, or as new
480 * views become available. Views indicate their willingness to take focus
481 * through the {@link #isFocusable} method. To change whether a view can take
482 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
483 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
484 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
485 * </p>
486 * <p>
487 * Focus movement is based on an algorithm which finds the nearest neighbor in a
488 * given direction. In rare cases, the default algorithm may not match the
489 * intended behavior of the developer. In these situations, you can provide
490 * explicit overrides by using these XML attributes in the layout file:
491 * <pre>
492 * nextFocusDown
493 * nextFocusLeft
494 * nextFocusRight
495 * nextFocusUp
496 * </pre>
497 * </p>
498 *
499 *
500 * <p>
501 * To get a particular view to take focus, call {@link #requestFocus()}.
502 * </p>
503 *
504 * <a name="TouchMode"></a>
505 * <h3>Touch Mode</h3>
506 * <p>
507 * When a user is navigating a user interface via directional keys such as a D-pad, it is
508 * necessary to give focus to actionable items such as buttons so the user can see
509 * what will take input. If the device has touch capabilities, however, and the user
510 * begins interacting with the interface by touching it, it is no longer necessary to
511 * always highlight, or give focus to, a particular view. This motivates a mode
512 * for interaction named 'touch mode'.
513 * </p>
514 * <p>
515 * For a touch capable device, once the user touches the screen, the device
516 * will enter touch mode. From this point onward, only views for which
517 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
518 * Other views that are touchable, like buttons, will not take focus when touched; they will
519 * only fire the on click listeners.
520 * </p>
521 * <p>
522 * Any time a user hits a directional key, such as a D-pad direction, the view device will
523 * exit touch mode, and find a view to take focus, so that the user may resume interacting
524 * with the user interface without touching the screen again.
525 * </p>
526 * <p>
527 * The touch mode state is maintained across {@link android.app.Activity}s. Call
528 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
529 * </p>
530 *
531 * <a name="Scrolling"></a>
532 * <h3>Scrolling</h3>
533 * <p>
534 * The framework provides basic support for views that wish to internally
535 * scroll their content. This includes keeping track of the X and Y scroll
536 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800537 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700538 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 * </p>
540 *
541 * <a name="Tags"></a>
542 * <h3>Tags</h3>
543 * <p>
544 * Unlike IDs, tags are not used to identify views. Tags are essentially an
545 * extra piece of information that can be associated with a view. They are most
546 * often used as a convenience to store data related to views in the views
547 * themselves rather than by putting them in a separate structure.
548 * </p>
549 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700550 * <a name="Properties"></a>
551 * <h3>Properties</h3>
552 * <p>
553 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
554 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
555 * available both in the {@link Property} form as well as in similarly-named setter/getter
556 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
557 * be used to set persistent state associated with these rendering-related properties on the view.
558 * The properties and methods can also be used in conjunction with
559 * {@link android.animation.Animator Animator}-based animations, described more in the
560 * <a href="#Animation">Animation</a> section.
561 * </p>
562 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * <a name="Animation"></a>
564 * <h3>Animation</h3>
565 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700566 * Starting with Android 3.0, the preferred way of animating views is to use the
567 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
568 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
569 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
570 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
571 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
572 * makes animating these View properties particularly easy and efficient.
573 * </p>
574 * <p>
575 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 * You can attach an {@link Animation} object to a view using
577 * {@link #setAnimation(Animation)} or
578 * {@link #startAnimation(Animation)}. The animation can alter the scale,
579 * rotation, translation and alpha of a view over time. If the animation is
580 * attached to a view that has children, the animation will affect the entire
581 * subtree rooted by that node. When an animation is started, the framework will
582 * take care of redrawing the appropriate views until the animation completes.
583 * </p>
584 *
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * <a name="Security"></a>
586 * <h3>Security</h3>
587 * <p>
588 * Sometimes it is essential that an application be able to verify that an action
589 * is being performed with the full knowledge and consent of the user, such as
590 * granting a permission request, making a purchase or clicking on an advertisement.
591 * Unfortunately, a malicious application could try to spoof the user into
592 * performing these actions, unaware, by concealing the intended purpose of the view.
593 * As a remedy, the framework offers a touch filtering mechanism that can be used to
594 * improve the security of views that provide access to sensitive functionality.
595 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700596 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800597 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700598 * will discard touches that are received whenever the view's window is obscured by
599 * another visible window. As a result, the view will not receive touches whenever a
600 * toast, dialog or other window appears above the view's window.
601 * </p><p>
602 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700603 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
604 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700605 * </p>
606 *
Romain Guy171c5922011-01-06 10:04:23 -0800607 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_background
609 * @attr ref android.R.styleable#View_clickable
610 * @attr ref android.R.styleable#View_contentDescription
611 * @attr ref android.R.styleable#View_drawingCacheQuality
612 * @attr ref android.R.styleable#View_duplicateParentState
613 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700614 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700615 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700617 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_isScrollContainer
620 * @attr ref android.R.styleable#View_focusable
621 * @attr ref android.R.styleable#View_focusableInTouchMode
622 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
623 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800624 * @attr ref android.R.styleable#View_layerType
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800625 * @attr ref android.R.styleable#View_layoutDirection
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_longClickable
627 * @attr ref android.R.styleable#View_minHeight
628 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 * @attr ref android.R.styleable#View_nextFocusDown
630 * @attr ref android.R.styleable#View_nextFocusLeft
631 * @attr ref android.R.styleable#View_nextFocusRight
632 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_onClick
634 * @attr ref android.R.styleable#View_padding
635 * @attr ref android.R.styleable#View_paddingBottom
636 * @attr ref android.R.styleable#View_paddingLeft
637 * @attr ref android.R.styleable#View_paddingRight
638 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800639 * @attr ref android.R.styleable#View_paddingStart
640 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800642 * @attr ref android.R.styleable#View_rotation
643 * @attr ref android.R.styleable#View_rotationX
644 * @attr ref android.R.styleable#View_rotationY
645 * @attr ref android.R.styleable#View_scaleX
646 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollX
648 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_scrollbarSize
650 * @attr ref android.R.styleable#View_scrollbarStyle
651 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700652 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
653 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
655 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * @attr ref android.R.styleable#View_scrollbarThumbVertical
657 * @attr ref android.R.styleable#View_scrollbarTrackVertical
658 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
659 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_soundEffectsEnabled
661 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700662 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800663 * @attr ref android.R.styleable#View_textDirection
Chet Haase73066682010-11-29 15:55:32 -0800664 * @attr ref android.R.styleable#View_transformPivotX
665 * @attr ref android.R.styleable#View_transformPivotY
666 * @attr ref android.R.styleable#View_translationX
667 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700668 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 *
670 * @see android.view.ViewGroup
671 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700672public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700673 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 private static final boolean DBG = false;
675
676 /**
677 * The logging tag used by this class with android.util.Log.
678 */
679 protected static final String VIEW_LOG_TAG = "View";
680
681 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700682 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700683 *
684 * @hide
685 */
686 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
687
688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 * Used to mark a View that has no ID.
690 */
691 public static final int NO_ID = -1;
692
Adam Powell7da4b732012-12-07 15:28:33 -0800693 private static boolean sUseBrokenMakeMeasureSpec = false;
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 /**
696 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
697 * calling setFlags.
698 */
699 private static final int NOT_FOCUSABLE = 0x00000000;
700
701 /**
702 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
703 * setFlags.
704 */
705 private static final int FOCUSABLE = 0x00000001;
706
707 /**
708 * Mask for use with setFlags indicating bits used for focus.
709 */
710 private static final int FOCUSABLE_MASK = 0x00000001;
711
712 /**
713 * This view will adjust its padding to fit sytem windows (e.g. status bar)
714 */
715 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
716
717 /**
Scott Main812634c22011-07-27 13:22:35 -0700718 * This view is visible.
719 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
720 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 */
722 public static final int VISIBLE = 0x00000000;
723
724 /**
725 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700726 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
727 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 */
729 public static final int INVISIBLE = 0x00000004;
730
731 /**
732 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700733 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
734 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 */
736 public static final int GONE = 0x00000008;
737
738 /**
739 * Mask for use with setFlags indicating bits used for visibility.
740 * {@hide}
741 */
742 static final int VISIBILITY_MASK = 0x0000000C;
743
744 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
745
746 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700747 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 * Use with ENABLED_MASK when calling setFlags.
749 * {@hide}
750 */
751 static final int ENABLED = 0x00000000;
752
753 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700754 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 * Use with ENABLED_MASK when calling setFlags.
756 * {@hide}
757 */
758 static final int DISABLED = 0x00000020;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is enabled
763 * {@hide}
764 */
765 static final int ENABLED_MASK = 0x00000020;
766
767 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700768 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
769 * called and further optimizations will be performed. It is okay to have
770 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 * {@hide}
772 */
773 static final int WILL_NOT_DRAW = 0x00000080;
774
775 /**
776 * Mask for use with setFlags indicating bits used for indicating whether
777 * this view is will draw
778 * {@hide}
779 */
780 static final int DRAW_MASK = 0x00000080;
781
782 /**
783 * <p>This view doesn't show scrollbars.</p>
784 * {@hide}
785 */
786 static final int SCROLLBARS_NONE = 0x00000000;
787
788 /**
789 * <p>This view shows horizontal scrollbars.</p>
790 * {@hide}
791 */
792 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
793
794 /**
795 * <p>This view shows vertical scrollbars.</p>
796 * {@hide}
797 */
798 static final int SCROLLBARS_VERTICAL = 0x00000200;
799
800 /**
801 * <p>Mask for use with setFlags indicating bits used for indicating which
802 * scrollbars are enabled.</p>
803 * {@hide}
804 */
805 static final int SCROLLBARS_MASK = 0x00000300;
806
Jeff Brown85a31762010-09-01 17:01:00 -0700807 /**
808 * Indicates that the view should filter touches when its window is obscured.
809 * Refer to the class comments for more information about this security feature.
810 * {@hide}
811 */
812 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
813
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700814 /**
815 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
816 * that they are optional and should be skipped if the window has
817 * requested system UI flags that ignore those insets for layout.
818 */
819 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820
821 /**
822 * <p>This view doesn't show fading edges.</p>
823 * {@hide}
824 */
825 static final int FADING_EDGE_NONE = 0x00000000;
826
827 /**
828 * <p>This view shows horizontal fading edges.</p>
829 * {@hide}
830 */
831 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
832
833 /**
834 * <p>This view shows vertical fading edges.</p>
835 * {@hide}
836 */
837 static final int FADING_EDGE_VERTICAL = 0x00002000;
838
839 /**
840 * <p>Mask for use with setFlags indicating bits used for indicating which
841 * fading edges are enabled.</p>
842 * {@hide}
843 */
844 static final int FADING_EDGE_MASK = 0x00003000;
845
846 /**
847 * <p>Indicates this view can be clicked. When clickable, a View reacts
848 * to clicks by notifying the OnClickListener.<p>
849 * {@hide}
850 */
851 static final int CLICKABLE = 0x00004000;
852
853 /**
854 * <p>Indicates this view is caching its drawing into a bitmap.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_ENABLED = 0x00008000;
858
859 /**
860 * <p>Indicates that no icicle should be saved for this view.<p>
861 * {@hide}
862 */
863 static final int SAVE_DISABLED = 0x000010000;
864
865 /**
866 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
867 * property.</p>
868 * {@hide}
869 */
870 static final int SAVE_DISABLED_MASK = 0x000010000;
871
872 /**
873 * <p>Indicates that no drawing cache should ever be created for this view.<p>
874 * {@hide}
875 */
876 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
877
878 /**
879 * <p>Indicates this view can take / keep focus when int touch mode.</p>
880 * {@hide}
881 */
882 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
883
884 /**
885 * <p>Enables low quality mode for the drawing cache.</p>
886 */
887 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
888
889 /**
890 * <p>Enables high quality mode for the drawing cache.</p>
891 */
892 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
893
894 /**
895 * <p>Enables automatic quality mode for the drawing cache.</p>
896 */
897 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
898
899 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
900 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
901 };
902
903 /**
904 * <p>Mask for use with setFlags indicating bits used for the cache
905 * quality property.</p>
906 * {@hide}
907 */
908 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
909
910 /**
911 * <p>
912 * Indicates this view can be long clicked. When long clickable, a View
913 * reacts to long clicks by notifying the OnLongClickListener or showing a
914 * context menu.
915 * </p>
916 * {@hide}
917 */
918 static final int LONG_CLICKABLE = 0x00200000;
919
920 /**
921 * <p>Indicates that this view gets its drawable states from its direct parent
922 * and ignores its original internal states.</p>
923 *
924 * @hide
925 */
926 static final int DUPLICATE_PARENT_STATE = 0x00400000;
927
928 /**
929 * The scrollbar style to display the scrollbars inside the content area,
930 * without increasing the padding. The scrollbars will be overlaid with
931 * translucency on the view's content.
932 */
933 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
934
935 /**
936 * The scrollbar style to display the scrollbars inside the padded area,
937 * increasing the padding of the view. The scrollbars will not overlap the
938 * content area of the view.
939 */
940 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
941
942 /**
943 * The scrollbar style to display the scrollbars at the edge of the view,
944 * without increasing the padding. The scrollbars will be overlaid with
945 * translucency.
946 */
947 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
948
949 /**
950 * The scrollbar style to display the scrollbars at the edge of the view,
951 * increasing the padding of the view. The scrollbars will only overlap the
952 * background, if any.
953 */
954 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
955
956 /**
957 * Mask to check if the scrollbar style is overlay or inset.
958 * {@hide}
959 */
960 static final int SCROLLBARS_INSET_MASK = 0x01000000;
961
962 /**
963 * Mask to check if the scrollbar style is inside or outside.
964 * {@hide}
965 */
966 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
967
968 /**
969 * Mask for scrollbar style.
970 * {@hide}
971 */
972 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
973
974 /**
975 * View flag indicating that the screen should remain on while the
976 * window containing this view is visible to the user. This effectively
977 * takes care of automatically setting the WindowManager's
978 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
979 */
980 public static final int KEEP_SCREEN_ON = 0x04000000;
981
982 /**
983 * View flag indicating whether this view should have sound effects enabled
984 * for events such as clicking and touching.
985 */
986 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
987
988 /**
989 * View flag indicating whether this view should have haptic feedback
990 * enabled for events such as long presses.
991 */
992 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
993
994 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700995 * <p>Indicates that the view hierarchy should stop saving state when
996 * it reaches this view. If state saving is initiated immediately at
997 * the view, it will be allowed.
998 * {@hide}
999 */
1000 static final int PARENT_SAVE_DISABLED = 0x20000000;
1001
1002 /**
1003 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
1004 * {@hide}
1005 */
1006 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
1007
1008 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001009 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1010 * should add all focusable Views regardless if they are focusable in touch mode.
1011 */
1012 public static final int FOCUSABLES_ALL = 0x00000000;
1013
1014 /**
1015 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1016 * should add only Views focusable in touch mode.
1017 */
1018 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1019
1020 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001021 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * item.
1023 */
1024 public static final int FOCUS_BACKWARD = 0x00000001;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 * item.
1029 */
1030 public static final int FOCUS_FORWARD = 0x00000002;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_LEFT = 0x00000011;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_UP = 0x00000021;
1041
1042 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001043 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 */
1045 public static final int FOCUS_RIGHT = 0x00000042;
1046
1047 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001048 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 */
1050 public static final int FOCUS_DOWN = 0x00000082;
1051
Svetoslav Ganov42138042012-03-20 11:51:39 -07001052 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001053 * Bits of {@link #getMeasuredWidthAndState()} and
1054 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1055 */
1056 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1057
1058 /**
1059 * Bits of {@link #getMeasuredWidthAndState()} and
1060 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1061 */
1062 public static final int MEASURED_STATE_MASK = 0xff000000;
1063
1064 /**
1065 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1066 * for functions that combine both width and height into a single int,
1067 * such as {@link #getMeasuredState()} and the childState argument of
1068 * {@link #resolveSizeAndState(int, int, int)}.
1069 */
1070 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1071
1072 /**
1073 * Bit of {@link #getMeasuredWidthAndState()} and
1074 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1075 * is smaller that the space the view would like to have.
1076 */
1077 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1078
1079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 * Base View state sets
1081 */
1082 // Singles
1083 /**
1084 * Indicates the view has no states set. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is enabled. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is focused. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is selected. States are used with
1112 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1113 * view depending on its state.
1114 *
1115 * @see android.graphics.drawable.Drawable
1116 * @see #getDrawableState()
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is pressed. States are used with
1121 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1122 * view depending on its state.
1123 *
1124 * @see android.graphics.drawable.Drawable
1125 * @see #getDrawableState()
1126 * @hide
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 /**
1130 * Indicates the view's window has focus. States are used with
1131 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1132 * view depending on its state.
1133 *
1134 * @see android.graphics.drawable.Drawable
1135 * @see #getDrawableState()
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 // Doubles
1139 /**
1140 * Indicates the view is enabled and has the focus.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled and selected.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #SELECTED_STATE_SET
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 /**
1154 * Indicates the view is enabled and that its window has focus.
1155 *
1156 * @see #ENABLED_STATE_SET
1157 * @see #WINDOW_FOCUSED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view is focused and selected.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view has the focus and that its window has the focus.
1169 *
1170 * @see #FOCUSED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is selected and that its window has the focus.
1176 *
1177 * @see #SELECTED_STATE_SET
1178 * @see #WINDOW_FOCUSED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 // Triples
1182 /**
1183 * Indicates the view is enabled, focused and selected.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, focused and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #FOCUSED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is enabled, selected and its window has the focus.
1200 *
1201 * @see #ENABLED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is focused, selected and its window has the focus.
1208 *
1209 * @see #FOCUSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #WINDOW_FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is enabled, focused, selected and its window
1216 * has the focus.
1217 *
1218 * @see #ENABLED_STATE_SET
1219 * @see #FOCUSED_STATE_SET
1220 * @see #SELECTED_STATE_SET
1221 * @see #WINDOW_FOCUSED_STATE_SET
1222 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001223 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 /**
1225 * Indicates the view is pressed and its window has the focus.
1226 *
1227 * @see #PRESSED_STATE_SET
1228 * @see #WINDOW_FOCUSED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is pressed and selected.
1233 *
1234 * @see #PRESSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, selected and its window has the focus.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #SELECTED_STATE_SET
1243 * @see #WINDOW_FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed and focused.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and its window has the focus.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 * @see #WINDOW_FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused and selected.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, focused, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #FOCUSED_STATE_SET
1274 * @see #SELECTED_STATE_SET
1275 * @see #WINDOW_FOCUSED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed and enabled.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and its window has the focus.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled and selected.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled, selected and its window has the
1303 * focus.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #SELECTED_STATE_SET
1308 * @see #WINDOW_FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled and focused.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #FOCUSED_STATE_SET
1317 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
1320 * Indicates the view is pressed, enabled, focused and its window has the
1321 * focus.
1322 *
1323 * @see #PRESSED_STATE_SET
1324 * @see #ENABLED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 * @see #WINDOW_FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, focused and selected.
1331 *
1332 * @see #PRESSED_STATE_SET
1333 * @see #ENABLED_STATE_SET
1334 * @see #SELECTED_STATE_SET
1335 * @see #FOCUSED_STATE_SET
1336 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 /**
1339 * Indicates the view is pressed, enabled, focused, selected and its window
1340 * has the focus.
1341 *
1342 * @see #PRESSED_STATE_SET
1343 * @see #ENABLED_STATE_SET
1344 * @see #SELECTED_STATE_SET
1345 * @see #FOCUSED_STATE_SET
1346 * @see #WINDOW_FOCUSED_STATE_SET
1347 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001348 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 /**
1351 * The order here is very important to {@link #getDrawableState()}
1352 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001353 private static final int[][] VIEW_STATE_SETS;
1354
Romain Guyb051e892010-09-28 19:09:36 -07001355 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1356 static final int VIEW_STATE_SELECTED = 1 << 1;
1357 static final int VIEW_STATE_FOCUSED = 1 << 2;
1358 static final int VIEW_STATE_ENABLED = 1 << 3;
1359 static final int VIEW_STATE_PRESSED = 1 << 4;
1360 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001361 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001362 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001363 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1364 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001365
1366 static final int[] VIEW_STATE_IDS = new int[] {
1367 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1368 R.attr.state_selected, VIEW_STATE_SELECTED,
1369 R.attr.state_focused, VIEW_STATE_FOCUSED,
1370 R.attr.state_enabled, VIEW_STATE_ENABLED,
1371 R.attr.state_pressed, VIEW_STATE_PRESSED,
1372 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001373 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001374 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001375 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001376 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 };
1378
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001380 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1381 throw new IllegalStateException(
1382 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1383 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001385 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001387 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001389 orderedIds[i * 2] = viewState;
1390 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 }
1392 }
1393 }
Romain Guyb051e892010-09-28 19:09:36 -07001394 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1395 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1396 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 int numBits = Integer.bitCount(i);
1398 int[] set = new int[numBits];
1399 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int j = 0; j < orderedIds.length; j += 2) {
1401 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001402 set[pos++] = orderedIds[j];
1403 }
1404 }
1405 VIEW_STATE_SETS[i] = set;
1406 }
1407
1408 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1409 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1410 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1411 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1413 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1414 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1416 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1418 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_FOCUSED];
1421 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1422 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1424 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1426 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED];
1437 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1440
1441 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1442 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1444 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1446 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1473 | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1479 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1482 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1483 | VIEW_STATE_PRESSED];
1484 }
1485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001487 * Accessibility event types that are dispatched for text population.
1488 */
1489 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1490 AccessibilityEvent.TYPE_VIEW_CLICKED
1491 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1492 | AccessibilityEvent.TYPE_VIEW_SELECTED
1493 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1494 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1495 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001496 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001497 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001498 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001499 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1500 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001501
1502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 * Temporary Rect currently for use in setBackground(). This will probably
1504 * be extended in the future to hold our own class with more than just
1505 * a Rect. :)
1506 */
1507 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001508
1509 /**
1510 * Map used to store views' tags.
1511 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001512 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001515 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001516 */
1517 private static int sNextAccessibilityViewId;
1518
1519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 * The animation currently associated with this view.
1521 * @hide
1522 */
1523 protected Animation mCurrentAnimation = null;
1524
1525 /**
1526 * Width as measured during measure pass.
1527 * {@hide}
1528 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001529 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001530 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531
1532 /**
1533 * Height as measured during measure pass.
1534 * {@hide}
1535 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001536 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001537 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538
1539 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001540 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1541 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1542 * its display list. This flag, used only when hw accelerated, allows us to clear the
1543 * flag while retaining this information until it's needed (at getDisplayList() time and
1544 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1545 *
1546 * {@hide}
1547 */
1548 boolean mRecreateDisplayList = false;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The view's identifier.
1552 * {@hide}
1553 *
1554 * @see #setId(int)
1555 * @see #getId()
1556 */
1557 @ViewDebug.ExportedProperty(resolveId = true)
1558 int mID = NO_ID;
1559
1560 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001561 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001562 */
1563 int mAccessibilityViewId = NO_ID;
1564
1565 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001566 * @hide
1567 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001568 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001569
1570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 * The view's tag.
1572 * {@hide}
1573 *
1574 * @see #setTag(Object)
1575 * @see #getTag()
1576 */
1577 protected Object mTag;
1578
1579 // for mPrivateFlags:
1580 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001581 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001583 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001585 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001587 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001589 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001591 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 /**
1593 * When this flag is set, this view is running an animation on behalf of its
1594 * children and should therefore not cancel invalidate requests, even if they
1595 * lie outside of this view's bounds.
1596 *
1597 * {@hide}
1598 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001599 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001601 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001603 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001605 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001607 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001609 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001611 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001612 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001613 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614
Dianne Hackborn4702a852012-08-17 15:18:29 -07001615 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616
1617 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001618 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 /**
1620 * Flag used to indicate that this view should be drawn once more (and only once
1621 * more) after its animation has completed.
1622 * {@hide}
1623 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001624 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625
Dianne Hackborn4702a852012-08-17 15:18:29 -07001626 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
1628 /**
1629 * Indicates that the View returned true when onSetAlpha() was called and that
1630 * the alpha must be restored.
1631 * {@hide}
1632 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001633 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634
1635 /**
1636 * Set by {@link #setScrollContainer(boolean)}.
1637 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001638 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639
1640 /**
1641 * Set by {@link #setScrollContainer(boolean)}.
1642 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001643 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644
1645 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001646 * View flag indicating whether this view was invalidated (fully or partially.)
1647 *
1648 * @hide
1649 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001650 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001651
1652 /**
1653 * View flag indicating whether this view was invalidated by an opaque
1654 * invalidate request.
1655 *
1656 * @hide
1657 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001658 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001659
1660 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001661 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001662 *
1663 * @hide
1664 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001665 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001666
1667 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001668 * Indicates whether the background is opaque.
1669 *
1670 * @hide
1671 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001672 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001673
1674 /**
1675 * Indicates whether the scrollbars are opaque.
1676 *
1677 * @hide
1678 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001679 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001680
1681 /**
1682 * Indicates whether the view is opaque.
1683 *
1684 * @hide
1685 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001686 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001687
Adam Powelle14579b2009-12-16 18:39:52 -08001688 /**
1689 * Indicates a prepressed state;
1690 * the short time between ACTION_DOWN and recognizing
1691 * a 'real' press. Prepressed is used to recognize quick taps
1692 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001693 *
Adam Powelle14579b2009-12-16 18:39:52 -08001694 * @hide
1695 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001696 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001697
Adam Powellc9fbaab2010-02-16 17:16:19 -08001698 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001699 * Indicates whether the view is temporarily detached.
1700 *
1701 * @hide
1702 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001703 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001704
Adam Powell8568c3a2010-04-19 14:26:11 -07001705 /**
1706 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001707 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001708 * @hide
1709 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001710 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001711
1712 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001713 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1714 * @hide
1715 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001716 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001717
1718 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001719 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1720 * for transform operations
1721 *
1722 * @hide
1723 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001724 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001725
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001726 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001727 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001728
Chet Haasefd2b0022010-08-06 13:08:56 -07001729 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001730 * Indicates that this view was specifically invalidated, not just dirtied because some
1731 * child view was invalidated. The flag is used to determine when we need to recreate
1732 * a view's display list (as opposed to just returning a reference to its existing
1733 * display list).
1734 *
1735 * @hide
1736 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001737 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001738
Jeff Sharkey36901b62012-09-19 19:06:22 -07001739 /**
1740 * Masks for mPrivateFlags2, as generated by dumpFlags():
1741 *
1742 * -------|-------|-------|-------|
1743 * PFLAG2_TEXT_ALIGNMENT_FLAGS[0]
1744 * PFLAG2_TEXT_DIRECTION_FLAGS[0]
1745 * 1 PFLAG2_DRAG_CAN_ACCEPT
1746 * 1 PFLAG2_DRAG_HOVERED
1747 * 1 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
1748 * 11 PFLAG2_TEXT_DIRECTION_MASK_SHIFT
1749 * 1 1 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT
1750 * 11 PFLAG2_LAYOUT_DIRECTION_MASK
1751 * 11 1 PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
1752 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
1753 * 1 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT
1754 * 1 1 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT
1755 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED
1756 * 11 PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
1757 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[1]
1758 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[2]
1759 * 11 PFLAG2_TEXT_DIRECTION_FLAGS[3]
1760 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[4]
1761 * 1 1 PFLAG2_TEXT_DIRECTION_FLAGS[5]
1762 * 111 PFLAG2_TEXT_DIRECTION_MASK
1763 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED
1764 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT
1765 * 111 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK
1766 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[1]
1767 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[2]
1768 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[3]
1769 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[4]
1770 * 1 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[5]
1771 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[6]
1772 * 111 PFLAG2_TEXT_ALIGNMENT_MASK
1773 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED
1774 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT
1775 * 111 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK
1776 * 11 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK
1777 * 1 PFLAG2_HAS_TRANSIENT_STATE
1778 * 1 PFLAG2_ACCESSIBILITY_FOCUSED
1779 * 1 PFLAG2_ACCESSIBILITY_STATE_CHANGED
1780 * 1 PFLAG2_VIEW_QUICK_REJECTED
1781 * 1 PFLAG2_PADDING_RESOLVED
1782 * -------|-------|-------|-------|
1783 */
Christopher Tate3d4bf172011-03-28 16:16:46 -07001784
1785 /**
1786 * Indicates that this view has reported that it can accept the current drag's content.
1787 * Cleared when the drag operation concludes.
1788 * @hide
1789 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001790 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001791
1792 /**
1793 * Indicates that this view is currently directly under the drag location in a
1794 * drag-and-drop operation involving content that it can accept. Cleared when
1795 * the drag exits the view, or when the drag operation concludes.
1796 * @hide
1797 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001798 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001799
Cibu Johny86666632010-02-22 13:01:02 -08001800 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001801 * Horizontal layout direction of this view is from Left to Right.
1802 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001803 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001804 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001805
1806 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807 * Horizontal layout direction of this view is from Right to Left.
1808 * Use with {@link #setLayoutDirection}.
1809 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001810 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001811
1812 /**
1813 * Horizontal layout direction of this view is inherited from its parent.
1814 * Use with {@link #setLayoutDirection}.
1815 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001816 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001817
1818 /**
1819 * Horizontal layout direction of this view is from deduced from the default language
1820 * script for the locale. Use with {@link #setLayoutDirection}.
1821 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001822 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001823
1824 /**
1825 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001826 * @hide
1827 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001828 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001829
1830 /**
1831 * Mask for use with private flags indicating bits used for horizontal layout direction.
1832 * @hide
1833 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001834 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001835
1836 /**
1837 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1838 * right-to-left direction.
1839 * @hide
1840 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001841 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001842
1843 /**
1844 * Indicates whether the view horizontal layout direction has been resolved.
1845 * @hide
1846 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001847 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001848
1849 /**
1850 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1851 * @hide
1852 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001853 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1854 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001855
1856 /*
1857 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1858 * flag value.
1859 * @hide
1860 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001861 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1862 LAYOUT_DIRECTION_LTR,
1863 LAYOUT_DIRECTION_RTL,
1864 LAYOUT_DIRECTION_INHERIT,
1865 LAYOUT_DIRECTION_LOCALE
1866 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001867
1868 /**
1869 * Default horizontal layout direction.
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001870 */
1871 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001872
Adam Powell539ee872012-02-03 19:00:49 -08001873 /**
1874 * Indicates that the view is tracking some sort of transient state
1875 * that the app should not need to be aware of, but that the framework
1876 * should take special care to preserve.
1877 *
1878 * @hide
1879 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001880 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001881
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001882 /**
1883 * Text direction is inherited thru {@link ViewGroup}
1884 */
1885 public static final int TEXT_DIRECTION_INHERIT = 0;
1886
1887 /**
1888 * Text direction is using "first strong algorithm". The first strong directional character
1889 * determines the paragraph direction. If there is no strong directional character, the
1890 * paragraph direction is the view's resolved layout direction.
1891 */
1892 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1893
1894 /**
1895 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1896 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1897 * If there are neither, the paragraph direction is the view's resolved layout direction.
1898 */
1899 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1900
1901 /**
1902 * Text direction is forced to LTR.
1903 */
1904 public static final int TEXT_DIRECTION_LTR = 3;
1905
1906 /**
1907 * Text direction is forced to RTL.
1908 */
1909 public static final int TEXT_DIRECTION_RTL = 4;
1910
1911 /**
1912 * Text direction is coming from the system Locale.
1913 */
1914 public static final int TEXT_DIRECTION_LOCALE = 5;
1915
1916 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 * Default text direction is inherited
1918 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07001919 private static final int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001920
1921 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001922 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1923 * @hide
1924 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001925 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001926
1927 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001928 * Mask for use with private flags indicating bits used for text direction.
1929 * @hide
1930 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001931 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1932 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001933
1934 /**
1935 * Array of text direction flags for mapping attribute "textDirection" to correct
1936 * flag value.
1937 * @hide
1938 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001939 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1940 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1944 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1945 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001946 };
1947
1948 /**
1949 * Indicates whether the view text direction has been resolved.
1950 * @hide
1951 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001952 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1953 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001954
1955 /**
1956 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1957 * @hide
1958 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001959 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001960
1961 /**
1962 * Mask for use with private flags indicating bits used for resolved text direction.
1963 * @hide
1964 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001965 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1966 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001967
1968 /**
1969 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1970 * @hide
1971 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001972 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1973 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001974
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 /*
1976 * Default text alignment. The text alignment of this View is inherited from its parent.
1977 * Use with {@link #setTextAlignment(int)}
1978 */
1979 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1980
1981 /**
1982 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1983 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1984 *
1985 * Use with {@link #setTextAlignment(int)}
1986 */
1987 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1988
1989 /**
1990 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1991 *
1992 * Use with {@link #setTextAlignment(int)}
1993 */
1994 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1995
1996 /**
1997 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1998 *
1999 * Use with {@link #setTextAlignment(int)}
2000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
2007 */
2008 public static final int TEXT_ALIGNMENT_CENTER = 4;
2009
2010 /**
2011 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2012 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2013 *
2014 * Use with {@link #setTextAlignment(int)}
2015 */
2016 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2017
2018 /**
2019 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2020 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2021 *
2022 * Use with {@link #setTextAlignment(int)}
2023 */
2024 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2025
2026 /**
2027 * Default text alignment is inherited
2028 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07002029 private static final int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002030
2031 /**
2032 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2033 * @hide
2034 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002035 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002036
2037 /**
2038 * Mask for use with private flags indicating bits used for text alignment.
2039 * @hide
2040 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002041 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002042
2043 /**
2044 * Array of text direction flags for mapping attribute "textAlignment" to correct
2045 * flag value.
2046 * @hide
2047 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002048 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2049 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2052 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2053 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002056 };
2057
2058 /**
2059 * Indicates whether the view text alignment has been resolved.
2060 * @hide
2061 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002062 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002063
2064 /**
2065 * Bit shift to get the resolved text alignment.
2066 * @hide
2067 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002068 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002069
2070 /**
2071 * Mask for use with private flags indicating bits used for text alignment.
2072 * @hide
2073 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002074 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2075 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002076
2077 /**
2078 * Indicates whether if the view text alignment has been resolved to gravity
2079 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002080 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2081 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002082
Svetoslav Ganov42138042012-03-20 11:51:39 -07002083 // Accessiblity constants for mPrivateFlags2
2084
2085 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002086 * Shift for the bits in {@link #mPrivateFlags2} related to the
2087 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002088 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002089 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002090
2091 /**
2092 * Automatically determine whether a view is important for accessibility.
2093 */
2094 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2095
2096 /**
2097 * The view is important for accessibility.
2098 */
2099 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2100
2101 /**
2102 * The view is not important for accessibility.
2103 */
2104 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2105
2106 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002107 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002108 */
2109 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2110
2111 /**
2112 * Mask for obtainig the bits which specify how to determine
2113 * whether a view is important for accessibility.
2114 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002115 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002116 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002117 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002118
2119 /**
2120 * Flag indicating whether a view has accessibility focus.
2121 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002122 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002123
2124 /**
2125 * Flag indicating whether a view state for accessibility has changed.
2126 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002127 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2128 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002129
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002130 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002131 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2132 * is used to check whether later changes to the view's transform should invalidate the
2133 * view to force the quickReject test to run again.
2134 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002135 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002136
Adam Powell0090f202012-08-07 17:15:30 -07002137 /**
2138 * Flag indicating that start/end padding has been resolved into left/right padding
2139 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2140 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2141 * during measurement. In some special cases this is required such as when an adapter-based
2142 * view measures prospective children without attaching them to a window.
2143 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002144 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002145
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002146 /**
2147 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2148 */
2149 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2150
2151 /**
2152 * Group of bits indicating that RTL properties resolution is done.
2153 */
2154 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002155 PFLAG2_TEXT_DIRECTION_RESOLVED |
2156 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2157 PFLAG2_PADDING_RESOLVED |
2158 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002159
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002160 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002161
Christopher Tate3d4bf172011-03-28 16:16:46 -07002162 /* End of masks for mPrivateFlags2 */
2163
Chet Haase21433372012-06-05 07:54:09 -07002164 /* Masks for mPrivateFlags3 */
2165
2166 /**
2167 * Flag indicating that view has a transform animation set on it. This is used to track whether
2168 * an animation is cleared between successive frames, in order to tell the associated
2169 * DisplayList to clear its animation matrix.
2170 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002171 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002172
2173 /**
2174 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2175 * animation is cleared between successive frames, in order to tell the associated
2176 * DisplayList to restore its alpha value.
2177 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002178 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002179
2180
2181 /* End of masks for mPrivateFlags3 */
2182
Dianne Hackborn4702a852012-08-17 15:18:29 -07002183 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002184
Chet Haasedaf98e92011-01-10 14:10:36 -08002185 /**
Adam Powell637d3372010-08-25 14:37:03 -07002186 * Always allow a user to over-scroll this view, provided it is a
2187 * view that can scroll.
2188 *
2189 * @see #getOverScrollMode()
2190 * @see #setOverScrollMode(int)
2191 */
2192 public static final int OVER_SCROLL_ALWAYS = 0;
2193
2194 /**
2195 * Allow a user to over-scroll this view only if the content is large
2196 * enough to meaningfully scroll, provided it is a view that can scroll.
2197 *
2198 * @see #getOverScrollMode()
2199 * @see #setOverScrollMode(int)
2200 */
2201 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2202
2203 /**
2204 * Never allow a user to over-scroll this view.
2205 *
2206 * @see #getOverScrollMode()
2207 * @see #setOverScrollMode(int)
2208 */
2209 public static final int OVER_SCROLL_NEVER = 2;
2210
2211 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002212 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2213 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002214 *
Joe Malin32736f02011-01-19 16:14:20 -08002215 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002216 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002217 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002218
2219 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002220 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2221 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002222 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002223 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002224 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002225 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002226 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002227 *
Joe Malin32736f02011-01-19 16:14:20 -08002228 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002229 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002230 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2231
2232 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002233 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2234 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002235 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002236 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002237 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2238 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2239 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002240 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002241 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2242 * window flags) for displaying content using every last pixel on the display.
2243 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002244 * <p>There is a limitation: because navigation controls are so important, the least user
2245 * interaction will cause them to reappear immediately. When this happens, both
2246 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2247 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002248 *
2249 * @see #setSystemUiVisibility(int)
2250 */
2251 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2252
2253 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002254 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2255 * into the normal fullscreen mode so that its content can take over the screen
2256 * while still allowing the user to interact with the application.
2257 *
2258 * <p>This has the same visual effect as
2259 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2260 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2261 * meaning that non-critical screen decorations (such as the status bar) will be
2262 * hidden while the user is in the View's window, focusing the experience on
2263 * that content. Unlike the window flag, if you are using ActionBar in
2264 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2265 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2266 * hide the action bar.
2267 *
2268 * <p>This approach to going fullscreen is best used over the window flag when
2269 * it is a transient state -- that is, the application does this at certain
2270 * points in its user interaction where it wants to allow the user to focus
2271 * on content, but not as a continuous state. For situations where the application
2272 * would like to simply stay full screen the entire time (such as a game that
2273 * wants to take over the screen), the
2274 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2275 * is usually a better approach. The state set here will be removed by the system
2276 * in various situations (such as the user moving to another application) like
2277 * the other system UI states.
2278 *
2279 * <p>When using this flag, the application should provide some easy facility
2280 * for the user to go out of it. A common example would be in an e-book
2281 * reader, where tapping on the screen brings back whatever screen and UI
2282 * decorations that had been hidden while the user was immersed in reading
2283 * the book.
2284 *
2285 * @see #setSystemUiVisibility(int)
2286 */
2287 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2288
2289 /**
2290 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2291 * flags, we would like a stable view of the content insets given to
2292 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2293 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002294 * as a continuous state. In the stock Android UI this is the space for
2295 * the system bar, nav bar, and status bar, but not more transient elements
2296 * such as an input method.
2297 *
2298 * The stable layout your UI sees is based on the system UI modes you can
2299 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2300 * then you will get a stable layout for changes of the
2301 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2302 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2303 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2304 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2305 * with a stable layout. (Note that you should avoid using
2306 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2307 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002308 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002309 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2310 * then a hidden status bar will be considered a "stable" state for purposes
2311 * here. This allows your UI to continually hide the status bar, while still
2312 * using the system UI flags to hide the action bar while still retaining
2313 * a stable layout. Note that changing the window fullscreen flag will never
2314 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002315 *
2316 * <p>If you are using ActionBar in
2317 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2318 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2319 * insets it adds to those given to the application.
2320 */
2321 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2322
2323 /**
2324 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2325 * to be layed out as if it has requested
2326 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2327 * allows it to avoid artifacts when switching in and out of that mode, at
2328 * the expense that some of its user interface may be covered by screen
2329 * decorations when they are shown. You can perform layout of your inner
2330 * UI elements to account for the navagation system UI through the
2331 * {@link #fitSystemWindows(Rect)} method.
2332 */
2333 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2334
2335 /**
2336 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2337 * to be layed out as if it has requested
2338 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2339 * allows it to avoid artifacts when switching in and out of that mode, at
2340 * the expense that some of its user interface may be covered by screen
2341 * decorations when they are shown. You can perform layout of your inner
2342 * UI elements to account for non-fullscreen system UI through the
2343 * {@link #fitSystemWindows(Rect)} method.
2344 */
2345 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2346
2347 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002348 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2349 */
2350 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2351
2352 /**
2353 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2354 */
2355 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002356
2357 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002358 * @hide
2359 *
2360 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2361 * out of the public fields to keep the undefined bits out of the developer's way.
2362 *
2363 * Flag to make the status bar not expandable. Unless you also
2364 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2365 */
2366 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2367
2368 /**
2369 * @hide
2370 *
2371 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2372 * out of the public fields to keep the undefined bits out of the developer's way.
2373 *
2374 * Flag to hide notification icons and scrolling ticker text.
2375 */
2376 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2377
2378 /**
2379 * @hide
2380 *
2381 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2382 * out of the public fields to keep the undefined bits out of the developer's way.
2383 *
2384 * Flag to disable incoming notification alerts. This will not block
2385 * icons, but it will block sound, vibrating and other visual or aural notifications.
2386 */
2387 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2388
2389 /**
2390 * @hide
2391 *
2392 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2393 * out of the public fields to keep the undefined bits out of the developer's way.
2394 *
2395 * Flag to hide only the scrolling ticker. Note that
2396 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2397 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2398 */
2399 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2400
2401 /**
2402 * @hide
2403 *
2404 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2405 * out of the public fields to keep the undefined bits out of the developer's way.
2406 *
2407 * Flag to hide the center system info area.
2408 */
2409 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2410
2411 /**
2412 * @hide
2413 *
2414 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2415 * out of the public fields to keep the undefined bits out of the developer's way.
2416 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002417 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002418 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2419 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002420 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002421
2422 /**
2423 * @hide
2424 *
2425 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2426 * out of the public fields to keep the undefined bits out of the developer's way.
2427 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002428 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002429 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2430 */
2431 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2432
2433 /**
2434 * @hide
2435 *
2436 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2437 * out of the public fields to keep the undefined bits out of the developer's way.
2438 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002439 * Flag to hide only the clock. You might use this if your activity has
2440 * its own clock making the status bar's clock redundant.
2441 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002442 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2443
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002444 /**
2445 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002446 *
2447 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2448 * out of the public fields to keep the undefined bits out of the developer's way.
2449 *
2450 * Flag to hide only the recent apps button. Don't use this
2451 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2452 */
2453 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2454
2455 /**
2456 * @hide
Daniel Sandlerd5483c32012-10-19 16:44:15 -04002457 *
2458 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2459 * out of the public fields to keep the undefined bits out of the developer's way.
2460 *
2461 * Flag to disable the global search gesture. Don't use this
2462 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2463 */
2464 public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
2465
2466 /**
2467 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002468 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002469 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002470
2471 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002472 * These are the system UI flags that can be cleared by events outside
2473 * of an application. Currently this is just the ability to tap on the
2474 * screen while hiding the navigation bar to have it return.
2475 * @hide
2476 */
2477 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002478 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2479 | SYSTEM_UI_FLAG_FULLSCREEN;
2480
2481 /**
2482 * Flags that can impact the layout in relation to system UI.
2483 */
2484 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2485 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2486 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002487
2488 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002489 * Find views that render the specified text.
2490 *
2491 * @see #findViewsWithText(ArrayList, CharSequence, int)
2492 */
2493 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2494
2495 /**
2496 * Find find views that contain the specified content description.
2497 *
2498 * @see #findViewsWithText(ArrayList, CharSequence, int)
2499 */
2500 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2501
2502 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002503 * Find views that contain {@link AccessibilityNodeProvider}. Such
2504 * a View is a root of virtual view hierarchy and may contain the searched
2505 * text. If this flag is set Views with providers are automatically
2506 * added and it is a responsibility of the client to call the APIs of
2507 * the provider to determine whether the virtual tree rooted at this View
2508 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2509 * represeting the virtual views with this text.
2510 *
2511 * @see #findViewsWithText(ArrayList, CharSequence, int)
2512 *
2513 * @hide
2514 */
2515 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2516
2517 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002518 * The undefined cursor position.
2519 */
2520 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2521
2522 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002523 * Indicates that the screen has changed state and is now off.
2524 *
2525 * @see #onScreenStateChanged(int)
2526 */
2527 public static final int SCREEN_STATE_OFF = 0x0;
2528
2529 /**
2530 * Indicates that the screen has changed state and is now on.
2531 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002532 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002533 */
2534 public static final int SCREEN_STATE_ON = 0x1;
2535
2536 /**
Adam Powell637d3372010-08-25 14:37:03 -07002537 * Controls the over-scroll mode for this view.
2538 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2539 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2540 * and {@link #OVER_SCROLL_NEVER}.
2541 */
2542 private int mOverScrollMode;
2543
2544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * The parent this view is attached to.
2546 * {@hide}
2547 *
2548 * @see #getParent()
2549 */
2550 protected ViewParent mParent;
2551
2552 /**
2553 * {@hide}
2554 */
2555 AttachInfo mAttachInfo;
2556
2557 /**
2558 * {@hide}
2559 */
Romain Guy809a7f62009-05-14 15:44:42 -07002560 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002561 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002562 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002563 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002564 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002565 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002566 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002567 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2568 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2569 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2570 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002571 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002573 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002574 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575
2576 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002577 * This view's request for the visibility of the status bar.
2578 * @hide
2579 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002580 @ViewDebug.ExportedProperty(flagMapping = {
2581 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2582 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2583 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2584 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2585 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2586 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2587 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2588 equals = SYSTEM_UI_FLAG_VISIBLE,
2589 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2590 })
Joe Onorato664644d2011-01-23 17:53:23 -08002591 int mSystemUiVisibility;
2592
2593 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002594 * Reference count for transient state.
2595 * @see #setHasTransientState(boolean)
2596 */
2597 int mTransientStateCount = 0;
2598
2599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 * Count of how many windows this view has been attached to.
2601 */
2602 int mWindowAttachCount;
2603
2604 /**
2605 * The layout parameters associated with this view and used by the parent
2606 * {@link android.view.ViewGroup} to determine how this view should be
2607 * laid out.
2608 * {@hide}
2609 */
2610 protected ViewGroup.LayoutParams mLayoutParams;
2611
2612 /**
2613 * The view flags hold various views states.
2614 * {@hide}
2615 */
2616 @ViewDebug.ExportedProperty
2617 int mViewFlags;
2618
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002619 static class TransformationInfo {
2620 /**
2621 * The transform matrix for the View. This transform is calculated internally
2622 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2623 * is used by default. Do *not* use this variable directly; instead call
2624 * getMatrix(), which will automatically recalculate the matrix if necessary
2625 * to get the correct matrix based on the latest rotation and scale properties.
2626 */
2627 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002628
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002629 /**
2630 * The transform matrix for the View. This transform is calculated internally
2631 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2632 * is used by default. Do *not* use this variable directly; instead call
2633 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2634 * to get the correct matrix based on the latest rotation and scale properties.
2635 */
2636 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002637
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002638 /**
2639 * An internal variable that tracks whether we need to recalculate the
2640 * transform matrix, based on whether the rotation or scaleX/Y properties
2641 * have changed since the matrix was last calculated.
2642 */
2643 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002644
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002645 /**
2646 * An internal variable that tracks whether we need to recalculate the
2647 * transform matrix, based on whether the rotation or scaleX/Y properties
2648 * have changed since the matrix was last calculated.
2649 */
2650 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002651
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002652 /**
2653 * A variable that tracks whether we need to recalculate the
2654 * transform matrix, based on whether the rotation or scaleX/Y properties
2655 * have changed since the matrix was last calculated. This variable
2656 * is only valid after a call to updateMatrix() or to a function that
2657 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2658 */
2659 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002660
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002661 /**
2662 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2663 */
2664 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002665
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002666 /**
2667 * This matrix is used when computing the matrix for 3D rotations.
2668 */
2669 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002670
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002671 /**
2672 * These prev values are used to recalculate a centered pivot point when necessary. The
2673 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2674 * set), so thes values are only used then as well.
2675 */
2676 private int mPrevWidth = -1;
2677 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002678
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002679 /**
2680 * The degrees rotation around the vertical axis through the pivot point.
2681 */
2682 @ViewDebug.ExportedProperty
2683 float mRotationY = 0f;
2684
2685 /**
2686 * The degrees rotation around the horizontal axis through the pivot point.
2687 */
2688 @ViewDebug.ExportedProperty
2689 float mRotationX = 0f;
2690
2691 /**
2692 * The degrees rotation around the pivot point.
2693 */
2694 @ViewDebug.ExportedProperty
2695 float mRotation = 0f;
2696
2697 /**
2698 * The amount of translation of the object away from its left property (post-layout).
2699 */
2700 @ViewDebug.ExportedProperty
2701 float mTranslationX = 0f;
2702
2703 /**
2704 * The amount of translation of the object away from its top property (post-layout).
2705 */
2706 @ViewDebug.ExportedProperty
2707 float mTranslationY = 0f;
2708
2709 /**
2710 * The amount of scale in the x direction around the pivot point. A
2711 * value of 1 means no scaling is applied.
2712 */
2713 @ViewDebug.ExportedProperty
2714 float mScaleX = 1f;
2715
2716 /**
2717 * The amount of scale in the y direction around the pivot point. A
2718 * value of 1 means no scaling is applied.
2719 */
2720 @ViewDebug.ExportedProperty
2721 float mScaleY = 1f;
2722
2723 /**
Chet Haasea33de552012-02-03 16:28:24 -08002724 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002725 */
2726 @ViewDebug.ExportedProperty
2727 float mPivotX = 0f;
2728
2729 /**
Chet Haasea33de552012-02-03 16:28:24 -08002730 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002731 */
2732 @ViewDebug.ExportedProperty
2733 float mPivotY = 0f;
2734
2735 /**
2736 * The opacity of the View. This is a value from 0 to 1, where 0 means
2737 * completely transparent and 1 means completely opaque.
2738 */
2739 @ViewDebug.ExportedProperty
2740 float mAlpha = 1f;
2741 }
2742
2743 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002744
Joe Malin32736f02011-01-19 16:14:20 -08002745 private boolean mLastIsOpaque;
2746
Chet Haasefd2b0022010-08-06 13:08:56 -07002747 /**
2748 * Convenience value to check for float values that are close enough to zero to be considered
2749 * zero.
2750 */
Romain Guy2542d192010-08-18 11:47:12 -07002751 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002752
2753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 * The distance in pixels from the left edge of this view's parent
2755 * to the left edge of this view.
2756 * {@hide}
2757 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002758 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 protected int mLeft;
2760 /**
2761 * The distance in pixels from the left edge of this view's parent
2762 * to the right edge of this view.
2763 * {@hide}
2764 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002765 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 protected int mRight;
2767 /**
2768 * The distance in pixels from the top edge of this view's parent
2769 * to the top edge of this view.
2770 * {@hide}
2771 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002772 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 protected int mTop;
2774 /**
2775 * The distance in pixels from the top edge of this view's parent
2776 * to the bottom edge of this view.
2777 * {@hide}
2778 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002779 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 protected int mBottom;
2781
2782 /**
2783 * The offset, in pixels, by which the content of this view is scrolled
2784 * horizontally.
2785 * {@hide}
2786 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002787 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 protected int mScrollX;
2789 /**
2790 * The offset, in pixels, by which the content of this view is scrolled
2791 * vertically.
2792 * {@hide}
2793 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002794 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 protected int mScrollY;
2796
2797 /**
2798 * The left padding in pixels, that is the distance in pixels between the
2799 * left edge of this view and the left edge of its content.
2800 * {@hide}
2801 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002802 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002803 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 /**
2805 * The right padding in pixels, that is the distance in pixels between the
2806 * right edge of this view and the right edge of its content.
2807 * {@hide}
2808 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002809 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002810 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 /**
2812 * The top padding in pixels, that is the distance in pixels between the
2813 * top edge of this view and the top edge of its content.
2814 * {@hide}
2815 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002816 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 protected int mPaddingTop;
2818 /**
2819 * The bottom padding in pixels, that is the distance in pixels between the
2820 * bottom edge of this view and the bottom edge of its content.
2821 * {@hide}
2822 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002823 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 protected int mPaddingBottom;
2825
2826 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002827 * The layout insets in pixels, that is the distance in pixels between the
2828 * visible edges of this view its bounds.
2829 */
2830 private Insets mLayoutInsets;
2831
2832 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002833 * Briefly describes the view and is primarily used for accessibility support.
2834 */
2835 private CharSequence mContentDescription;
2836
2837 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002838 * Specifies the id of a view for which this view serves as a label for
2839 * accessibility purposes.
2840 */
2841 private int mLabelForId = View.NO_ID;
2842
2843 /**
2844 * Predicate for matching labeled view id with its label for
2845 * accessibility purposes.
2846 */
2847 private MatchLabelForPredicate mMatchLabelForPredicate;
2848
2849 /**
2850 * Predicate for matching a view by its id.
2851 */
2852 private MatchIdPredicate mMatchIdPredicate;
2853
2854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002856 *
2857 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002859 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002860 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861
2862 /**
2863 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002864 *
2865 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002867 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002868 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002870 /**
Adam Powell20232d02010-12-08 21:08:53 -08002871 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002872 *
2873 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002874 */
2875 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002876 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002877
2878 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002879 * Cache the paddingStart set by the user to append to the scrollbar's size.
2880 *
2881 */
2882 @ViewDebug.ExportedProperty(category = "padding")
2883 int mUserPaddingStart;
2884
2885 /**
2886 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2887 *
2888 */
2889 @ViewDebug.ExportedProperty(category = "padding")
2890 int mUserPaddingEnd;
2891
2892 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002893 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002894 *
2895 * @hide
2896 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002897 int mUserPaddingLeftInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002898
2899 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002900 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002901 *
2902 * @hide
2903 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002904 int mUserPaddingRightInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002905
2906 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002907 * Default undefined padding
2908 */
2909 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2910
2911 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002912 * @hide
2913 */
2914 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2915 /**
2916 * @hide
2917 */
2918 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
Romain Guydfab3632012-10-03 14:53:25 -07002920 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002921 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
2923 private int mBackgroundResource;
2924 private boolean mBackgroundSizeChanged;
2925
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002926 static class ListenerInfo {
2927 /**
2928 * Listener used to dispatch focus change events.
2929 * This field should be made private, so it is hidden from the SDK.
2930 * {@hide}
2931 */
2932 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002934 /**
2935 * Listeners for layout change events.
2936 */
2937 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002938
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002939 /**
2940 * Listeners for attach events.
2941 */
2942 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002943
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002944 /**
2945 * Listener used to dispatch click events.
2946 * This field should be made private, so it is hidden from the SDK.
2947 * {@hide}
2948 */
2949 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002951 /**
2952 * Listener used to dispatch long click events.
2953 * This field should be made private, so it is hidden from the SDK.
2954 * {@hide}
2955 */
2956 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002958 /**
2959 * Listener used to build the context menu.
2960 * This field should be made private, so it is hidden from the SDK.
2961 * {@hide}
2962 */
2963 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002965 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002967 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002969 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002970
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002971 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002972
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002973 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002974
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002975 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2976 }
2977
2978 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 /**
2981 * The application environment this view lives in.
2982 * This field should be made private, so it is hidden from the SDK.
2983 * {@hide}
2984 */
2985 protected Context mContext;
2986
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002987 private final Resources mResources;
2988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 private ScrollabilityCache mScrollCache;
2990
2991 private int[] mDrawableState = null;
2992
Romain Guy0211a0a2011-02-14 16:34:59 -08002993 /**
2994 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002995 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002996 * @hide
2997 */
2998 public boolean mCachingFailed;
2999
Romain Guy02890fd2010-08-06 17:58:44 -07003000 private Bitmap mDrawingCache;
3001 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08003002 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003003 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004
3005 /**
3006 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3007 * the user may specify which view to go to next.
3008 */
3009 private int mNextFocusLeftId = View.NO_ID;
3010
3011 /**
3012 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3013 * the user may specify which view to go to next.
3014 */
3015 private int mNextFocusRightId = View.NO_ID;
3016
3017 /**
3018 * When this view has focus and the next focus is {@link #FOCUS_UP},
3019 * the user may specify which view to go to next.
3020 */
3021 private int mNextFocusUpId = View.NO_ID;
3022
3023 /**
3024 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3025 * the user may specify which view to go to next.
3026 */
3027 private int mNextFocusDownId = View.NO_ID;
3028
Jeff Brown4e6319b2010-12-13 10:36:51 -08003029 /**
3030 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3031 * the user may specify which view to go to next.
3032 */
3033 int mNextFocusForwardId = View.NO_ID;
3034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003036 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003037 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003038 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 private UnsetPressedState mUnsetPressedState;
3041
3042 /**
3043 * Whether the long press's action has been invoked. The tap's action is invoked on the
3044 * up event while a long press is invoked as soon as the long press duration is reached, so
3045 * a long press could be performed before the tap is checked, in which case the tap's action
3046 * should not be invoked.
3047 */
3048 private boolean mHasPerformedLongPress;
3049
3050 /**
3051 * The minimum height of the view. We'll try our best to have the height
3052 * of this view to at least this amount.
3053 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003054 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 private int mMinHeight;
3056
3057 /**
3058 * The minimum width of the view. We'll try our best to have the width
3059 * of this view to at least this amount.
3060 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003061 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 private int mMinWidth;
3063
3064 /**
3065 * The delegate to handle touch events that are physically in this view
3066 * but should be handled by another view.
3067 */
3068 private TouchDelegate mTouchDelegate = null;
3069
3070 /**
3071 * Solid color to use as a background when creating the drawing cache. Enables
3072 * the cache to use 16 bit bitmaps instead of 32 bit.
3073 */
3074 private int mDrawingCacheBackgroundColor = 0;
3075
3076 /**
3077 * Special tree observer used when mAttachInfo is null.
3078 */
3079 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003080
Adam Powelle14579b2009-12-16 18:39:52 -08003081 /**
3082 * Cache the touch slop from the context that created the view.
3083 */
3084 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003087 * Object that handles automatic animation of view properties.
3088 */
3089 private ViewPropertyAnimator mAnimator = null;
3090
3091 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003092 * Flag indicating that a drag can cross window boundaries. When
3093 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3094 * with this flag set, all visible applications will be able to participate
3095 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003096 *
3097 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003098 */
3099 public static final int DRAG_FLAG_GLOBAL = 1;
3100
3101 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003102 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3103 */
3104 private float mVerticalScrollFactor;
3105
3106 /**
Adam Powell20232d02010-12-08 21:08:53 -08003107 * Position of the vertical scroll bar.
3108 */
3109 private int mVerticalScrollbarPosition;
3110
3111 /**
3112 * Position the scroll bar at the default position as determined by the system.
3113 */
3114 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3115
3116 /**
3117 * Position the scroll bar along the left edge.
3118 */
3119 public static final int SCROLLBAR_POSITION_LEFT = 1;
3120
3121 /**
3122 * Position the scroll bar along the right edge.
3123 */
3124 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3125
3126 /**
Romain Guy171c5922011-01-06 10:04:23 -08003127 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003128 *
3129 * @see #getLayerType()
3130 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003131 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003132 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003133 */
3134 public static final int LAYER_TYPE_NONE = 0;
3135
3136 /**
3137 * <p>Indicates that the view has a software layer. A software layer is backed
3138 * by a bitmap and causes the view to be rendered using Android's software
3139 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003140 *
Romain Guy171c5922011-01-06 10:04:23 -08003141 * <p>Software layers have various usages:</p>
3142 * <p>When the application is not using hardware acceleration, a software layer
3143 * is useful to apply a specific color filter and/or blending mode and/or
3144 * translucency to a view and all its children.</p>
3145 * <p>When the application is using hardware acceleration, a software layer
3146 * is useful to render drawing primitives not supported by the hardware
3147 * accelerated pipeline. It can also be used to cache a complex view tree
3148 * into a texture and reduce the complexity of drawing operations. For instance,
3149 * when animating a complex view tree with a translation, a software layer can
3150 * be used to render the view tree only once.</p>
3151 * <p>Software layers should be avoided when the affected view tree updates
3152 * often. Every update will require to re-render the software layer, which can
3153 * potentially be slow (particularly when hardware acceleration is turned on
3154 * since the layer will have to be uploaded into a hardware texture after every
3155 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003156 *
3157 * @see #getLayerType()
3158 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003159 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003160 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003161 */
3162 public static final int LAYER_TYPE_SOFTWARE = 1;
3163
3164 /**
3165 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3166 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3167 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3168 * rendering pipeline, but only if hardware acceleration is turned on for the
3169 * view hierarchy. When hardware acceleration is turned off, hardware layers
3170 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003171 *
Romain Guy171c5922011-01-06 10:04:23 -08003172 * <p>A hardware layer is useful to apply a specific color filter and/or
3173 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003174 * <p>A hardware layer can be used to cache a complex view tree into a
3175 * texture and reduce the complexity of drawing operations. For instance,
3176 * when animating a complex view tree with a translation, a hardware layer can
3177 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003178 * <p>A hardware layer can also be used to increase the rendering quality when
3179 * rotation transformations are applied on a view. It can also be used to
3180 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003181 *
3182 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003183 * @see #setLayerType(int, android.graphics.Paint)
3184 * @see #LAYER_TYPE_NONE
3185 * @see #LAYER_TYPE_SOFTWARE
3186 */
3187 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003188
Romain Guy3aaff3a2011-01-12 14:18:47 -08003189 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3190 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3191 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3192 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3193 })
Romain Guy171c5922011-01-06 10:04:23 -08003194 int mLayerType = LAYER_TYPE_NONE;
3195 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003196 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003197
3198 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003199 * Set to true when the view is sending hover accessibility events because it
3200 * is the innermost hovered view.
3201 */
3202 private boolean mSendingHoverAccessibilityEvents;
3203
Dianne Hackborn4702a852012-08-17 15:18:29 -07003204 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003205 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003206 */
3207 AccessibilityDelegate mAccessibilityDelegate;
3208
3209 /**
3210 * Consistency verifier for debugging purposes.
3211 * @hide
3212 */
3213 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3214 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3215 new InputEventConsistencyVerifier(this, 0) : null;
3216
Adam Powella9108a22012-07-18 11:18:09 -07003217 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3218
Jeff Brown87b7f802011-06-21 18:35:45 -07003219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 * Simple constructor to use when creating a view from code.
3221 *
3222 * @param context The Context the view is running in, through which it can
3223 * access the current theme, resources, etc.
3224 */
3225 public View(Context context) {
3226 mContext = context;
3227 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003228 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07003229 // Set some flags defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003230 mPrivateFlags2 =
3231 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003232 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003233 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003234 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003235 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003236 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003237 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003238 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003239 mUserPaddingStart = UNDEFINED_PADDING;
3240 mUserPaddingEnd = UNDEFINED_PADDING;
Adam Powell7da4b732012-12-07 15:28:33 -08003241
3242 if (!sUseBrokenMakeMeasureSpec && context.getApplicationInfo().targetSdkVersion <=
3243 Build.VERSION_CODES.JELLY_BEAN_MR1 ) {
3244 // Older apps may need this compatibility hack for measurement.
3245 sUseBrokenMakeMeasureSpec = true;
3246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
3248
3249 /**
3250 * Constructor that is called when inflating a view from XML. This is called
3251 * when a view is being constructed from an XML file, supplying attributes
3252 * that were specified in the XML file. This version uses a default style of
3253 * 0, so the only attribute values applied are those in the Context's Theme
3254 * and the given AttributeSet.
3255 *
3256 * <p>
3257 * The method onFinishInflate() will be called after all children have been
3258 * added.
3259 *
3260 * @param context The Context the view is running in, through which it can
3261 * access the current theme, resources, etc.
3262 * @param attrs The attributes of the XML tag that is inflating the view.
3263 * @see #View(Context, AttributeSet, int)
3264 */
3265 public View(Context context, AttributeSet attrs) {
3266 this(context, attrs, 0);
3267 }
3268
3269 /**
3270 * Perform inflation from XML and apply a class-specific base style. This
3271 * constructor of View allows subclasses to use their own base style when
3272 * they are inflating. For example, a Button class's constructor would call
3273 * this version of the super class constructor and supply
3274 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3275 * the theme's button style to modify all of the base view attributes (in
3276 * particular its background) as well as the Button class's attributes.
3277 *
3278 * @param context The Context the view is running in, through which it can
3279 * access the current theme, resources, etc.
3280 * @param attrs The attributes of the XML tag that is inflating the view.
3281 * @param defStyle The default style to apply to this view. If 0, no style
3282 * will be applied (beyond what is included in the theme). This may
3283 * either be an attribute resource, whose value will be retrieved
3284 * from the current theme, or an explicit style resource.
3285 * @see #View(Context, AttributeSet)
3286 */
3287 public View(Context context, AttributeSet attrs, int defStyle) {
3288 this(context);
3289
3290 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3291 defStyle, 0);
3292
3293 Drawable background = null;
3294
3295 int leftPadding = -1;
3296 int topPadding = -1;
3297 int rightPadding = -1;
3298 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003299 int startPadding = UNDEFINED_PADDING;
3300 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301
3302 int padding = -1;
3303
3304 int viewFlagValues = 0;
3305 int viewFlagMasks = 0;
3306
3307 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 int x = 0;
3310 int y = 0;
3311
Chet Haase73066682010-11-29 15:55:32 -08003312 float tx = 0;
3313 float ty = 0;
3314 float rotation = 0;
3315 float rotationX = 0;
3316 float rotationY = 0;
3317 float sx = 1f;
3318 float sy = 1f;
3319 boolean transformSet = false;
3320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003322 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003323 boolean initializeScrollbars = false;
3324
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003325 boolean leftPaddingDefined = false;
3326 boolean rightPaddingDefined = false;
3327 boolean startPaddingDefined = false;
3328 boolean endPaddingDefined = false;
3329
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003330 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 final int N = a.getIndexCount();
3333 for (int i = 0; i < N; i++) {
3334 int attr = a.getIndex(i);
3335 switch (attr) {
3336 case com.android.internal.R.styleable.View_background:
3337 background = a.getDrawable(attr);
3338 break;
3339 case com.android.internal.R.styleable.View_padding:
3340 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003341 mUserPaddingLeftInitial = padding;
3342 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003343 leftPaddingDefined = true;
3344 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 break;
3346 case com.android.internal.R.styleable.View_paddingLeft:
3347 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003348 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003349 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 break;
3351 case com.android.internal.R.styleable.View_paddingTop:
3352 topPadding = a.getDimensionPixelSize(attr, -1);
3353 break;
3354 case com.android.internal.R.styleable.View_paddingRight:
3355 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003356 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003357 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 break;
3359 case com.android.internal.R.styleable.View_paddingBottom:
3360 bottomPadding = a.getDimensionPixelSize(attr, -1);
3361 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003362 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003363 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003364 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003365 break;
3366 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003367 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003368 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003369 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 case com.android.internal.R.styleable.View_scrollX:
3371 x = a.getDimensionPixelOffset(attr, 0);
3372 break;
3373 case com.android.internal.R.styleable.View_scrollY:
3374 y = a.getDimensionPixelOffset(attr, 0);
3375 break;
Chet Haase73066682010-11-29 15:55:32 -08003376 case com.android.internal.R.styleable.View_alpha:
3377 setAlpha(a.getFloat(attr, 1f));
3378 break;
3379 case com.android.internal.R.styleable.View_transformPivotX:
3380 setPivotX(a.getDimensionPixelOffset(attr, 0));
3381 break;
3382 case com.android.internal.R.styleable.View_transformPivotY:
3383 setPivotY(a.getDimensionPixelOffset(attr, 0));
3384 break;
3385 case com.android.internal.R.styleable.View_translationX:
3386 tx = a.getDimensionPixelOffset(attr, 0);
3387 transformSet = true;
3388 break;
3389 case com.android.internal.R.styleable.View_translationY:
3390 ty = a.getDimensionPixelOffset(attr, 0);
3391 transformSet = true;
3392 break;
3393 case com.android.internal.R.styleable.View_rotation:
3394 rotation = a.getFloat(attr, 0);
3395 transformSet = true;
3396 break;
3397 case com.android.internal.R.styleable.View_rotationX:
3398 rotationX = a.getFloat(attr, 0);
3399 transformSet = true;
3400 break;
3401 case com.android.internal.R.styleable.View_rotationY:
3402 rotationY = a.getFloat(attr, 0);
3403 transformSet = true;
3404 break;
3405 case com.android.internal.R.styleable.View_scaleX:
3406 sx = a.getFloat(attr, 1f);
3407 transformSet = true;
3408 break;
3409 case com.android.internal.R.styleable.View_scaleY:
3410 sy = a.getFloat(attr, 1f);
3411 transformSet = true;
3412 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 case com.android.internal.R.styleable.View_id:
3414 mID = a.getResourceId(attr, NO_ID);
3415 break;
3416 case com.android.internal.R.styleable.View_tag:
3417 mTag = a.getText(attr);
3418 break;
3419 case com.android.internal.R.styleable.View_fitsSystemWindows:
3420 if (a.getBoolean(attr, false)) {
3421 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3422 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3423 }
3424 break;
3425 case com.android.internal.R.styleable.View_focusable:
3426 if (a.getBoolean(attr, false)) {
3427 viewFlagValues |= FOCUSABLE;
3428 viewFlagMasks |= FOCUSABLE_MASK;
3429 }
3430 break;
3431 case com.android.internal.R.styleable.View_focusableInTouchMode:
3432 if (a.getBoolean(attr, false)) {
3433 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3434 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3435 }
3436 break;
3437 case com.android.internal.R.styleable.View_clickable:
3438 if (a.getBoolean(attr, false)) {
3439 viewFlagValues |= CLICKABLE;
3440 viewFlagMasks |= CLICKABLE;
3441 }
3442 break;
3443 case com.android.internal.R.styleable.View_longClickable:
3444 if (a.getBoolean(attr, false)) {
3445 viewFlagValues |= LONG_CLICKABLE;
3446 viewFlagMasks |= LONG_CLICKABLE;
3447 }
3448 break;
3449 case com.android.internal.R.styleable.View_saveEnabled:
3450 if (!a.getBoolean(attr, true)) {
3451 viewFlagValues |= SAVE_DISABLED;
3452 viewFlagMasks |= SAVE_DISABLED_MASK;
3453 }
3454 break;
3455 case com.android.internal.R.styleable.View_duplicateParentState:
3456 if (a.getBoolean(attr, false)) {
3457 viewFlagValues |= DUPLICATE_PARENT_STATE;
3458 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3459 }
3460 break;
3461 case com.android.internal.R.styleable.View_visibility:
3462 final int visibility = a.getInt(attr, 0);
3463 if (visibility != 0) {
3464 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3465 viewFlagMasks |= VISIBILITY_MASK;
3466 }
3467 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003468 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003469 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003470 mPrivateFlags2 &=
3471 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003472 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003473 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003474 final int value = (layoutDirection != -1) ?
3475 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003476 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003477 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 case com.android.internal.R.styleable.View_drawingCacheQuality:
3479 final int cacheQuality = a.getInt(attr, 0);
3480 if (cacheQuality != 0) {
3481 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3482 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3483 }
3484 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003485 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003486 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003487 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003488 case com.android.internal.R.styleable.View_labelFor:
3489 setLabelFor(a.getResourceId(attr, NO_ID));
3490 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3492 if (!a.getBoolean(attr, true)) {
3493 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3494 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3495 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003496 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3498 if (!a.getBoolean(attr, true)) {
3499 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3500 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3501 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003502 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 case R.styleable.View_scrollbars:
3504 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3505 if (scrollbars != SCROLLBARS_NONE) {
3506 viewFlagValues |= scrollbars;
3507 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003508 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 }
3510 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003511 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003513 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003514 // Ignore the attribute starting with ICS
3515 break;
3516 }
3517 // With builds < ICS, fall through and apply fading edges
3518 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3520 if (fadingEdge != FADING_EDGE_NONE) {
3521 viewFlagValues |= fadingEdge;
3522 viewFlagMasks |= FADING_EDGE_MASK;
3523 initializeFadingEdge(a);
3524 }
3525 break;
3526 case R.styleable.View_scrollbarStyle:
3527 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3528 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3529 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3530 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3531 }
3532 break;
3533 case R.styleable.View_isScrollContainer:
3534 setScrollContainer = true;
3535 if (a.getBoolean(attr, false)) {
3536 setScrollContainer(true);
3537 }
3538 break;
3539 case com.android.internal.R.styleable.View_keepScreenOn:
3540 if (a.getBoolean(attr, false)) {
3541 viewFlagValues |= KEEP_SCREEN_ON;
3542 viewFlagMasks |= KEEP_SCREEN_ON;
3543 }
3544 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003545 case R.styleable.View_filterTouchesWhenObscured:
3546 if (a.getBoolean(attr, false)) {
3547 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3548 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3549 }
3550 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 case R.styleable.View_nextFocusLeft:
3552 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3553 break;
3554 case R.styleable.View_nextFocusRight:
3555 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3556 break;
3557 case R.styleable.View_nextFocusUp:
3558 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3559 break;
3560 case R.styleable.View_nextFocusDown:
3561 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3562 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003563 case R.styleable.View_nextFocusForward:
3564 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3565 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 case R.styleable.View_minWidth:
3567 mMinWidth = a.getDimensionPixelSize(attr, 0);
3568 break;
3569 case R.styleable.View_minHeight:
3570 mMinHeight = a.getDimensionPixelSize(attr, 0);
3571 break;
Romain Guy9a817362009-05-01 10:57:14 -07003572 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003573 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003574 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003575 + "be used within a restricted context");
3576 }
3577
Romain Guy9a817362009-05-01 10:57:14 -07003578 final String handlerName = a.getString(attr);
3579 if (handlerName != null) {
3580 setOnClickListener(new OnClickListener() {
3581 private Method mHandler;
3582
3583 public void onClick(View v) {
3584 if (mHandler == null) {
3585 try {
3586 mHandler = getContext().getClass().getMethod(handlerName,
3587 View.class);
3588 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003589 int id = getId();
3590 String idText = id == NO_ID ? "" : " with id '"
3591 + getContext().getResources().getResourceEntryName(
3592 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003593 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003594 handlerName + "(View) in the activity "
3595 + getContext().getClass() + " for onClick handler"
3596 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003597 }
3598 }
3599
3600 try {
3601 mHandler.invoke(getContext(), View.this);
3602 } catch (IllegalAccessException e) {
3603 throw new IllegalStateException("Could not execute non "
3604 + "public method of the activity", e);
3605 } catch (InvocationTargetException e) {
3606 throw new IllegalStateException("Could not execute "
3607 + "method of the activity", e);
3608 }
3609 }
3610 });
3611 }
3612 break;
Adam Powell637d3372010-08-25 14:37:03 -07003613 case R.styleable.View_overScrollMode:
3614 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3615 break;
Adam Powell20232d02010-12-08 21:08:53 -08003616 case R.styleable.View_verticalScrollbarPosition:
3617 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3618 break;
Romain Guy171c5922011-01-06 10:04:23 -08003619 case R.styleable.View_layerType:
3620 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3621 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003622 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003623 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003624 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003625 // Set the text direction flags depending on the value of the attribute
3626 final int textDirection = a.getInt(attr, -1);
3627 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003628 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003629 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003630 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003631 case R.styleable.View_textAlignment:
3632 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003633 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003634 // Set the text alignment flag depending on the value of the attribute
3635 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003636 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003637 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003638 case R.styleable.View_importantForAccessibility:
3639 setImportantForAccessibility(a.getInt(attr,
3640 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003641 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 }
3643 }
3644
Adam Powell637d3372010-08-25 14:37:03 -07003645 setOverScrollMode(overScrollMode);
3646
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003647 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3648 // the resolved layout direction). Those cached values will be used later during padding
3649 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003650 mUserPaddingStart = startPadding;
3651 mUserPaddingEnd = endPadding;
3652
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003653 if (background != null) {
3654 setBackground(background);
3655 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 if (padding >= 0) {
3658 leftPadding = padding;
3659 topPadding = padding;
3660 rightPadding = padding;
3661 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003662 mUserPaddingLeftInitial = padding;
3663 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 }
3665
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003666 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003667 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3668 // left / right padding are used if defined (meaning here nothing to do). If they are not
3669 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3670 // start / end and resolve them as left / right (layout direction is not taken into account).
3671 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3672 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3673 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003674 if (!leftPaddingDefined && startPaddingDefined) {
3675 leftPadding = startPadding;
3676 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003677 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003678 if (!rightPaddingDefined && endPaddingDefined) {
3679 rightPadding = endPadding;
3680 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003681 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003682 } else {
3683 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3684 // values defined. Otherwise, left /right values are used.
3685 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3686 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3687 // defined.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003688 if (leftPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003689 mUserPaddingLeftInitial = leftPadding;
3690 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003691 if (rightPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003692 mUserPaddingRightInitial = rightPadding;
3693 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003694 }
3695
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003696 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003697 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003699 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3701
3702 if (viewFlagMasks != 0) {
3703 setFlags(viewFlagValues, viewFlagMasks);
3704 }
3705
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003706 if (initializeScrollbars) {
3707 initializeScrollbars(a);
3708 }
3709
3710 a.recycle();
3711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 // Needs to be called after mViewFlags is set
3713 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3714 recomputePadding();
3715 }
3716
3717 if (x != 0 || y != 0) {
3718 scrollTo(x, y);
3719 }
3720
Chet Haase73066682010-11-29 15:55:32 -08003721 if (transformSet) {
3722 setTranslationX(tx);
3723 setTranslationY(ty);
3724 setRotation(rotation);
3725 setRotationX(rotationX);
3726 setRotationY(rotationY);
3727 setScaleX(sx);
3728 setScaleY(sy);
3729 }
3730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3732 setScrollContainer(true);
3733 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003734
3735 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 }
3737
3738 /**
3739 * Non-public constructor for use in testing
3740 */
3741 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003742 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 }
3744
Dianne Hackborn4702a852012-08-17 15:18:29 -07003745 public String toString() {
3746 StringBuilder out = new StringBuilder(128);
3747 out.append(getClass().getName());
3748 out.append('{');
3749 out.append(Integer.toHexString(System.identityHashCode(this)));
3750 out.append(' ');
3751 switch (mViewFlags&VISIBILITY_MASK) {
3752 case VISIBLE: out.append('V'); break;
3753 case INVISIBLE: out.append('I'); break;
3754 case GONE: out.append('G'); break;
3755 default: out.append('.'); break;
3756 }
3757 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3758 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3759 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3760 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3761 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3762 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3763 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3764 out.append(' ');
3765 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3766 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3767 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3768 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3769 out.append('p');
3770 } else {
3771 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3772 }
3773 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3774 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3775 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3776 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3777 out.append(' ');
3778 out.append(mLeft);
3779 out.append(',');
3780 out.append(mTop);
3781 out.append('-');
3782 out.append(mRight);
3783 out.append(',');
3784 out.append(mBottom);
3785 final int id = getId();
3786 if (id != NO_ID) {
3787 out.append(" #");
3788 out.append(Integer.toHexString(id));
3789 final Resources r = mResources;
3790 if (id != 0 && r != null) {
3791 try {
3792 String pkgname;
3793 switch (id&0xff000000) {
3794 case 0x7f000000:
3795 pkgname="app";
3796 break;
3797 case 0x01000000:
3798 pkgname="android";
3799 break;
3800 default:
3801 pkgname = r.getResourcePackageName(id);
3802 break;
3803 }
3804 String typename = r.getResourceTypeName(id);
3805 String entryname = r.getResourceEntryName(id);
3806 out.append(" ");
3807 out.append(pkgname);
3808 out.append(":");
3809 out.append(typename);
3810 out.append("/");
3811 out.append(entryname);
3812 } catch (Resources.NotFoundException e) {
3813 }
3814 }
3815 }
3816 out.append("}");
3817 return out.toString();
3818 }
3819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 /**
3821 * <p>
3822 * Initializes the fading edges from a given set of styled attributes. This
3823 * method should be called by subclasses that need fading edges and when an
3824 * instance of these subclasses is created programmatically rather than
3825 * being inflated from XML. This method is automatically called when the XML
3826 * is inflated.
3827 * </p>
3828 *
3829 * @param a the styled attributes set to initialize the fading edges from
3830 */
3831 protected void initializeFadingEdge(TypedArray a) {
3832 initScrollCache();
3833
3834 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3835 R.styleable.View_fadingEdgeLength,
3836 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3837 }
3838
3839 /**
3840 * Returns the size of the vertical faded edges used to indicate that more
3841 * content in this view is visible.
3842 *
3843 * @return The size in pixels of the vertical faded edge or 0 if vertical
3844 * faded edges are not enabled for this view.
3845 * @attr ref android.R.styleable#View_fadingEdgeLength
3846 */
3847 public int getVerticalFadingEdgeLength() {
3848 if (isVerticalFadingEdgeEnabled()) {
3849 ScrollabilityCache cache = mScrollCache;
3850 if (cache != null) {
3851 return cache.fadingEdgeLength;
3852 }
3853 }
3854 return 0;
3855 }
3856
3857 /**
3858 * Set the size of the faded edge used to indicate that more content in this
3859 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003860 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3861 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3862 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 *
3864 * @param length The size in pixels of the faded edge used to indicate that more
3865 * content in this view is visible.
3866 */
3867 public void setFadingEdgeLength(int length) {
3868 initScrollCache();
3869 mScrollCache.fadingEdgeLength = length;
3870 }
3871
3872 /**
3873 * Returns the size of the horizontal faded edges used to indicate that more
3874 * content in this view is visible.
3875 *
3876 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3877 * faded edges are not enabled for this view.
3878 * @attr ref android.R.styleable#View_fadingEdgeLength
3879 */
3880 public int getHorizontalFadingEdgeLength() {
3881 if (isHorizontalFadingEdgeEnabled()) {
3882 ScrollabilityCache cache = mScrollCache;
3883 if (cache != null) {
3884 return cache.fadingEdgeLength;
3885 }
3886 }
3887 return 0;
3888 }
3889
3890 /**
3891 * Returns the width of the vertical scrollbar.
3892 *
3893 * @return The width in pixels of the vertical scrollbar or 0 if there
3894 * is no vertical scrollbar.
3895 */
3896 public int getVerticalScrollbarWidth() {
3897 ScrollabilityCache cache = mScrollCache;
3898 if (cache != null) {
3899 ScrollBarDrawable scrollBar = cache.scrollBar;
3900 if (scrollBar != null) {
3901 int size = scrollBar.getSize(true);
3902 if (size <= 0) {
3903 size = cache.scrollBarSize;
3904 }
3905 return size;
3906 }
3907 return 0;
3908 }
3909 return 0;
3910 }
3911
3912 /**
3913 * Returns the height of the horizontal scrollbar.
3914 *
3915 * @return The height in pixels of the horizontal scrollbar or 0 if
3916 * there is no horizontal scrollbar.
3917 */
3918 protected int getHorizontalScrollbarHeight() {
3919 ScrollabilityCache cache = mScrollCache;
3920 if (cache != null) {
3921 ScrollBarDrawable scrollBar = cache.scrollBar;
3922 if (scrollBar != null) {
3923 int size = scrollBar.getSize(false);
3924 if (size <= 0) {
3925 size = cache.scrollBarSize;
3926 }
3927 return size;
3928 }
3929 return 0;
3930 }
3931 return 0;
3932 }
3933
3934 /**
3935 * <p>
3936 * Initializes the scrollbars from a given set of styled attributes. This
3937 * method should be called by subclasses that need scrollbars and when an
3938 * instance of these subclasses is created programmatically rather than
3939 * being inflated from XML. This method is automatically called when the XML
3940 * is inflated.
3941 * </p>
3942 *
3943 * @param a the styled attributes set to initialize the scrollbars from
3944 */
3945 protected void initializeScrollbars(TypedArray a) {
3946 initScrollCache();
3947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003949
Mike Cleronf116bf82009-09-27 19:14:12 -07003950 if (scrollabilityCache.scrollBar == null) {
3951 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3952 }
Joe Malin32736f02011-01-19 16:14:20 -08003953
Romain Guy8bda2482010-03-02 11:42:11 -08003954 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955
Mike Cleronf116bf82009-09-27 19:14:12 -07003956 if (!fadeScrollbars) {
3957 scrollabilityCache.state = ScrollabilityCache.ON;
3958 }
3959 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003960
3961
Mike Cleronf116bf82009-09-27 19:14:12 -07003962 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3963 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3964 .getScrollBarFadeDuration());
3965 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3966 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3967 ViewConfiguration.getScrollDefaultDelay());
3968
Joe Malin32736f02011-01-19 16:14:20 -08003969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3971 com.android.internal.R.styleable.View_scrollbarSize,
3972 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3973
3974 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3975 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3976
3977 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3978 if (thumb != null) {
3979 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3980 }
3981
3982 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3983 false);
3984 if (alwaysDraw) {
3985 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3986 }
3987
3988 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3989 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3990
3991 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3992 if (thumb != null) {
3993 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3994 }
3995
3996 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3997 false);
3998 if (alwaysDraw) {
3999 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
4000 }
4001
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07004002 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07004003 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07004004 if (track != null) {
4005 track.setLayoutDirection(layoutDirection);
4006 }
4007 if (thumb != null) {
4008 thumb.setLayoutDirection(layoutDirection);
4009 }
4010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004012 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014
4015 /**
4016 * <p>
4017 * Initalizes the scrollability cache if necessary.
4018 * </p>
4019 */
4020 private void initScrollCache() {
4021 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004022 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024 }
4025
Philip Milne6c8ea062012-04-03 17:38:43 -07004026 private ScrollabilityCache getScrollCache() {
4027 initScrollCache();
4028 return mScrollCache;
4029 }
4030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 /**
Adam Powell20232d02010-12-08 21:08:53 -08004032 * Set the position of the vertical scroll bar. Should be one of
4033 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4034 * {@link #SCROLLBAR_POSITION_RIGHT}.
4035 *
4036 * @param position Where the vertical scroll bar should be positioned.
4037 */
4038 public void setVerticalScrollbarPosition(int position) {
4039 if (mVerticalScrollbarPosition != position) {
4040 mVerticalScrollbarPosition = position;
4041 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004042 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004043 }
4044 }
4045
4046 /**
4047 * @return The position where the vertical scroll bar will show, if applicable.
4048 * @see #setVerticalScrollbarPosition(int)
4049 */
4050 public int getVerticalScrollbarPosition() {
4051 return mVerticalScrollbarPosition;
4052 }
4053
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004054 ListenerInfo getListenerInfo() {
4055 if (mListenerInfo != null) {
4056 return mListenerInfo;
4057 }
4058 mListenerInfo = new ListenerInfo();
4059 return mListenerInfo;
4060 }
4061
Adam Powell20232d02010-12-08 21:08:53 -08004062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 * Register a callback to be invoked when focus of this view changed.
4064 *
4065 * @param l The callback that will run.
4066 */
4067 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004068 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
4070
4071 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004072 * Add a listener that will be called when the bounds of the view change due to
4073 * layout processing.
4074 *
4075 * @param listener The listener that will be called when layout bounds change.
4076 */
4077 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004078 ListenerInfo li = getListenerInfo();
4079 if (li.mOnLayoutChangeListeners == null) {
4080 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004081 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004082 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4083 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004084 }
Chet Haase21cd1382010-09-01 17:42:29 -07004085 }
4086
4087 /**
4088 * Remove a listener for layout changes.
4089 *
4090 * @param listener The listener for layout bounds change.
4091 */
4092 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004093 ListenerInfo li = mListenerInfo;
4094 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004095 return;
4096 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004098 }
4099
4100 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004101 * Add a listener for attach state changes.
4102 *
4103 * This listener will be called whenever this view is attached or detached
4104 * from a window. Remove the listener using
4105 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4106 *
4107 * @param listener Listener to attach
4108 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4109 */
4110 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004111 ListenerInfo li = getListenerInfo();
4112 if (li.mOnAttachStateChangeListeners == null) {
4113 li.mOnAttachStateChangeListeners
4114 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004115 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004116 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004117 }
4118
4119 /**
4120 * Remove a listener for attach state changes. The listener will receive no further
4121 * notification of window attach/detach events.
4122 *
4123 * @param listener Listener to remove
4124 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4125 */
4126 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004127 ListenerInfo li = mListenerInfo;
4128 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004129 return;
4130 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004131 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004132 }
4133
4134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 * Returns the focus-change callback registered for this view.
4136 *
4137 * @return The callback, or null if one is not registered.
4138 */
4139 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004140 ListenerInfo li = mListenerInfo;
4141 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
4143
4144 /**
4145 * Register a callback to be invoked when this view is clicked. If this view is not
4146 * clickable, it becomes clickable.
4147 *
4148 * @param l The callback that will run
4149 *
4150 * @see #setClickable(boolean)
4151 */
4152 public void setOnClickListener(OnClickListener l) {
4153 if (!isClickable()) {
4154 setClickable(true);
4155 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004156 getListenerInfo().mOnClickListener = l;
4157 }
4158
4159 /**
4160 * Return whether this view has an attached OnClickListener. Returns
4161 * true if there is a listener, false if there is none.
4162 */
4163 public boolean hasOnClickListeners() {
4164 ListenerInfo li = mListenerInfo;
4165 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
4167
4168 /**
4169 * Register a callback to be invoked when this view is clicked and held. If this view is not
4170 * long clickable, it becomes long clickable.
4171 *
4172 * @param l The callback that will run
4173 *
4174 * @see #setLongClickable(boolean)
4175 */
4176 public void setOnLongClickListener(OnLongClickListener l) {
4177 if (!isLongClickable()) {
4178 setLongClickable(true);
4179 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004180 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182
4183 /**
4184 * Register a callback to be invoked when the context menu for this view is
4185 * being built. If this view is not long clickable, it becomes long clickable.
4186 *
4187 * @param l The callback that will run
4188 *
4189 */
4190 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4191 if (!isLongClickable()) {
4192 setLongClickable(true);
4193 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004194 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196
4197 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004198 * Call this view's OnClickListener, if it is defined. Performs all normal
4199 * actions associated with clicking: reporting accessibility event, playing
4200 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 *
4202 * @return True there was an assigned OnClickListener that was called, false
4203 * otherwise is returned.
4204 */
4205 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004206 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4207
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004208 ListenerInfo li = mListenerInfo;
4209 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004211 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 return true;
4213 }
4214
4215 return false;
4216 }
4217
4218 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004219 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4220 * this only calls the listener, and does not do any associated clicking
4221 * actions like reporting an accessibility event.
4222 *
4223 * @return True there was an assigned OnClickListener that was called, false
4224 * otherwise is returned.
4225 */
4226 public boolean callOnClick() {
4227 ListenerInfo li = mListenerInfo;
4228 if (li != null && li.mOnClickListener != null) {
4229 li.mOnClickListener.onClick(this);
4230 return true;
4231 }
4232 return false;
4233 }
4234
4235 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004236 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4237 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004239 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 */
4241 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004242 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004245 ListenerInfo li = mListenerInfo;
4246 if (li != null && li.mOnLongClickListener != null) {
4247 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 if (!handled) {
4250 handled = showContextMenu();
4251 }
4252 if (handled) {
4253 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4254 }
4255 return handled;
4256 }
4257
4258 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004259 * Performs button-related actions during a touch down event.
4260 *
4261 * @param event The event.
4262 * @return True if the down was consumed.
4263 *
4264 * @hide
4265 */
4266 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4267 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4268 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4269 return true;
4270 }
4271 }
4272 return false;
4273 }
4274
4275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 * Bring up the context menu for this view.
4277 *
4278 * @return Whether a context menu was displayed.
4279 */
4280 public boolean showContextMenu() {
4281 return getParent().showContextMenuForChild(this);
4282 }
4283
4284 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004285 * Bring up the context menu for this view, referring to the item under the specified point.
4286 *
4287 * @param x The referenced x coordinate.
4288 * @param y The referenced y coordinate.
4289 * @param metaState The keyboard modifiers that were pressed.
4290 * @return Whether a context menu was displayed.
4291 *
4292 * @hide
4293 */
4294 public boolean showContextMenu(float x, float y, int metaState) {
4295 return showContextMenu();
4296 }
4297
4298 /**
Adam Powell6e346362010-07-23 10:18:23 -07004299 * Start an action mode.
4300 *
4301 * @param callback Callback that will control the lifecycle of the action mode
4302 * @return The new action mode if it is started, null otherwise
4303 *
4304 * @see ActionMode
4305 */
4306 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004307 ViewParent parent = getParent();
4308 if (parent == null) return null;
4309 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004310 }
4311
4312 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004313 * Register a callback to be invoked when a hardware key is pressed in this view.
4314 * Key presses in software input methods will generally not trigger the methods of
4315 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 * @param l the key listener to attach to this view
4317 */
4318 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004319 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 }
4321
4322 /**
4323 * Register a callback to be invoked when a touch event is sent to this view.
4324 * @param l the touch listener to attach to this view
4325 */
4326 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004327 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 }
4329
4330 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004331 * Register a callback to be invoked when a generic motion event is sent to this view.
4332 * @param l the generic motion listener to attach to this view
4333 */
4334 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004335 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004336 }
4337
4338 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004339 * Register a callback to be invoked when a hover event is sent to this view.
4340 * @param l the hover listener to attach to this view
4341 */
4342 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004343 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004344 }
4345
4346 /**
Joe Malin32736f02011-01-19 16:14:20 -08004347 * Register a drag event listener callback object for this View. The parameter is
4348 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4349 * View, the system calls the
4350 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4351 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004352 */
4353 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004354 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004355 }
4356
4357 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004358 * Give this view focus. This will cause
4359 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 *
4361 * Note: this does not check whether this {@link View} should get focus, it just
4362 * gives it focus no matter what. It should only be called internally by framework
4363 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4364 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004365 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4366 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 * focus moved when requestFocus() is called. It may not always
4368 * apply, in which case use the default View.FOCUS_DOWN.
4369 * @param previouslyFocusedRect The rectangle of the view that had focus
4370 * prior in this View's coordinate system.
4371 */
4372 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4373 if (DBG) {
4374 System.out.println(this + " requestFocus()");
4375 }
4376
Dianne Hackborn4702a852012-08-17 15:18:29 -07004377 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4378 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004380 View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null;
4381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 if (mParent != null) {
4383 mParent.requestChildFocus(this, this);
4384 }
4385
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004386 if (mAttachInfo != null) {
4387 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, this);
4388 }
4389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 onFocusChanged(true, direction, previouslyFocusedRect);
4391 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004392
4393 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4394 notifyAccessibilityStateChanged();
4395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 }
4397 }
4398
4399 /**
4400 * Request that a rectangle of this view be visible on the screen,
4401 * scrolling if necessary just enough.
4402 *
4403 * <p>A View should call this if it maintains some notion of which part
4404 * of its content is interesting. For example, a text editing view
4405 * should call this when its cursor moves.
4406 *
4407 * @param rectangle The rectangle.
4408 * @return Whether any parent scrolled.
4409 */
4410 public boolean requestRectangleOnScreen(Rect rectangle) {
4411 return requestRectangleOnScreen(rectangle, false);
4412 }
4413
4414 /**
4415 * Request that a rectangle of this view be visible on the screen,
4416 * scrolling if necessary just enough.
4417 *
4418 * <p>A View should call this if it maintains some notion of which part
4419 * of its content is interesting. For example, a text editing view
4420 * should call this when its cursor moves.
4421 *
4422 * <p>When <code>immediate</code> is set to true, scrolling will not be
4423 * animated.
4424 *
4425 * @param rectangle The rectangle.
4426 * @param immediate True to forbid animated scrolling, false otherwise
4427 * @return Whether any parent scrolled.
4428 */
4429 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004430 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004431 return false;
4432 }
4433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004435
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004436 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004437 position.set(rectangle);
4438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 ViewParent parent = mParent;
4440 boolean scrolled = false;
4441 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004442 rectangle.set((int) position.left, (int) position.top,
4443 (int) position.right, (int) position.bottom);
4444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 scrolled |= parent.requestChildRectangleOnScreen(child,
4446 rectangle, immediate);
4447
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004448 if (!child.hasIdentityMatrix()) {
4449 child.getMatrix().mapRect(position);
4450 }
4451
4452 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453
4454 if (!(parent instanceof View)) {
4455 break;
4456 }
Romain Guy8506ab42009-06-11 17:35:47 -07004457
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004458 View parentView = (View) parent;
4459
4460 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4461
4462 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 parent = child.getParent();
4464 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 return scrolled;
4467 }
4468
4469 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004470 * Called when this view wants to give up focus. If focus is cleared
4471 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4472 * <p>
4473 * <strong>Note:</strong> When a View clears focus the framework is trying
4474 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004475 * View is the first from the top that can take focus, then all callbacks
4476 * related to clearing focus will be invoked after wich the framework will
4477 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004478 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 */
4480 public void clearFocus() {
4481 if (DBG) {
4482 System.out.println(this + " clearFocus()");
4483 }
4484
Dianne Hackborn4702a852012-08-17 15:18:29 -07004485 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4486 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487
4488 if (mParent != null) {
4489 mParent.clearChildFocus(this);
4490 }
4491
4492 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004495
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004496 if (!rootViewRequestFocus()) {
4497 notifyGlobalFocusCleared(this);
4498 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004499
4500 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4501 notifyAccessibilityStateChanged();
4502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
4504 }
4505
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004506 void notifyGlobalFocusCleared(View oldFocus) {
4507 if (oldFocus != null && mAttachInfo != null) {
4508 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
4509 }
4510 }
4511
4512 boolean rootViewRequestFocus() {
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004513 View root = getRootView();
4514 if (root != null) {
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004515 return root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004517 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
4519
4520 /**
4521 * Called internally by the view system when a new view is getting focus.
4522 * This is what clears the old focus.
4523 */
4524 void unFocus() {
4525 if (DBG) {
4526 System.out.println(this + " unFocus()");
4527 }
4528
Dianne Hackborn4702a852012-08-17 15:18:29 -07004529 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4530 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531
4532 onFocusChanged(false, 0, null);
4533 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004534
4535 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4536 notifyAccessibilityStateChanged();
4537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 }
4539 }
4540
4541 /**
4542 * Returns true if this view has focus iteself, or is the ancestor of the
4543 * view that has focus.
4544 *
4545 * @return True if this view has or contains focus, false otherwise.
4546 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004547 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004549 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 }
4551
4552 /**
4553 * Returns true if this view is focusable or if it contains a reachable View
4554 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4555 * is a View whose parents do not block descendants focus.
4556 *
4557 * Only {@link #VISIBLE} views are considered focusable.
4558 *
4559 * @return True if the view is focusable or if the view contains a focusable
4560 * View, false otherwise.
4561 *
4562 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4563 */
4564 public boolean hasFocusable() {
4565 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4566 }
4567
4568 /**
4569 * Called by the view system when the focus state of this view changes.
4570 * When the focus change event is caused by directional navigation, direction
4571 * and previouslyFocusedRect provide insight into where the focus is coming from.
4572 * When overriding, be sure to call up through to the super class so that
4573 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004574 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 * @param gainFocus True if the View has focus; false otherwise.
4576 * @param direction The direction focus has moved when requestFocus()
4577 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004578 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4579 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4580 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4582 * system, of the previously focused view. If applicable, this will be
4583 * passed in as finer grained information about where the focus is coming
4584 * from (in addition to direction). Will be <code>null</code> otherwise.
4585 */
4586 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004587 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004588 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4589 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004590 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004591 }
4592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 InputMethodManager imm = InputMethodManager.peekInstance();
4594 if (!gainFocus) {
4595 if (isPressed()) {
4596 setPressed(false);
4597 }
4598 if (imm != null && mAttachInfo != null
4599 && mAttachInfo.mHasWindowFocus) {
4600 imm.focusOut(this);
4601 }
Romain Guya2431d02009-04-30 16:30:00 -07004602 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 } else if (imm != null && mAttachInfo != null
4604 && mAttachInfo.mHasWindowFocus) {
4605 imm.focusIn(this);
4606 }
Romain Guy8506ab42009-06-11 17:35:47 -07004607
Romain Guy0fd89bf2011-01-26 15:41:30 -08004608 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004609 ListenerInfo li = mListenerInfo;
4610 if (li != null && li.mOnFocusChangeListener != null) {
4611 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 }
Joe Malin32736f02011-01-19 16:14:20 -08004613
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004614 if (mAttachInfo != null) {
4615 mAttachInfo.mKeyDispatchState.reset(this);
4616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 }
4618
4619 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004620 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004621 * not enabled this method has no effect. The default implementation calls
4622 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4623 * to populate information about the event source (this View), then calls
4624 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4625 * populate the text content of the event source including its descendants,
4626 * and last calls
4627 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4628 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004629 * <p>
4630 * If an {@link AccessibilityDelegate} has been specified via calling
4631 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4632 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4633 * responsible for handling this call.
4634 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004635 *
Scott Mainb303d832011-10-12 16:45:18 -07004636 * @param eventType The type of the event to send, as defined by several types from
4637 * {@link android.view.accessibility.AccessibilityEvent}, such as
4638 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4639 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004640 *
4641 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4642 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4643 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004644 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004645 */
4646 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004647 if (mAccessibilityDelegate != null) {
4648 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4649 } else {
4650 sendAccessibilityEventInternal(eventType);
4651 }
4652 }
4653
4654 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004655 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4656 * {@link AccessibilityEvent} to make an announcement which is related to some
4657 * sort of a context change for which none of the events representing UI transitions
4658 * is a good fit. For example, announcing a new page in a book. If accessibility
4659 * is not enabled this method does nothing.
4660 *
4661 * @param text The announcement text.
4662 */
4663 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004664 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004665 AccessibilityEvent event = AccessibilityEvent.obtain(
4666 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004667 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004668 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004669 event.setContentDescription(null);
4670 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004671 }
4672 }
4673
4674 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004675 * @see #sendAccessibilityEvent(int)
4676 *
4677 * Note: Called from the default {@link AccessibilityDelegate}.
4678 */
4679 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004680 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4681 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4682 }
4683 }
4684
4685 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004686 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4687 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004688 * perform a check whether accessibility is enabled.
4689 * <p>
4690 * If an {@link AccessibilityDelegate} has been specified via calling
4691 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4692 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4693 * is responsible for handling this call.
4694 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004695 *
4696 * @param event The event to send.
4697 *
4698 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004699 */
4700 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004701 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004702 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004703 } else {
4704 sendAccessibilityEventUncheckedInternal(event);
4705 }
4706 }
4707
4708 /**
4709 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4710 *
4711 * Note: Called from the default {@link AccessibilityDelegate}.
4712 */
4713 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004714 if (!isShown()) {
4715 return;
4716 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004717 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004718 // Only a subset of accessibility events populates text content.
4719 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4720 dispatchPopulateAccessibilityEvent(event);
4721 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004722 // In the beginning we called #isShown(), so we know that getParent() is not null.
4723 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004724 }
4725
4726 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004727 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4728 * to its children for adding their text content to the event. Note that the
4729 * event text is populated in a separate dispatch path since we add to the
4730 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004731 * A typical implementation will call
4732 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4733 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4734 * on each child. Override this method if custom population of the event text
4735 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004736 * <p>
4737 * If an {@link AccessibilityDelegate} has been specified via calling
4738 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4739 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4740 * is responsible for handling this call.
4741 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004742 * <p>
4743 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4744 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4745 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004746 *
4747 * @param event The event.
4748 *
4749 * @return True if the event population was completed.
4750 */
4751 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004752 if (mAccessibilityDelegate != null) {
4753 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4754 } else {
4755 return dispatchPopulateAccessibilityEventInternal(event);
4756 }
4757 }
4758
4759 /**
4760 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4761 *
4762 * Note: Called from the default {@link AccessibilityDelegate}.
4763 */
4764 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004765 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004766 return false;
4767 }
4768
4769 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004770 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004771 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004772 * text content. While this method is free to modify event
4773 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004774 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4775 * <p>
4776 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004777 * to the text added by the super implementation:
4778 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004779 * super.onPopulateAccessibilityEvent(event);
4780 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4781 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4782 * mCurrentDate.getTimeInMillis(), flags);
4783 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004784 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004785 * <p>
4786 * If an {@link AccessibilityDelegate} has been specified via calling
4787 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4788 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4789 * is responsible for handling this call.
4790 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004791 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4792 * information to the event, in case the default implementation has basic information to add.
4793 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004794 *
4795 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004796 *
4797 * @see #sendAccessibilityEvent(int)
4798 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004799 */
4800 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004801 if (mAccessibilityDelegate != null) {
4802 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4803 } else {
4804 onPopulateAccessibilityEventInternal(event);
4805 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004806 }
4807
4808 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004809 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4810 *
4811 * Note: Called from the default {@link AccessibilityDelegate}.
4812 */
4813 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4814
4815 }
4816
4817 /**
4818 * Initializes an {@link AccessibilityEvent} with information about
4819 * this View which is the event source. In other words, the source of
4820 * an accessibility event is the view whose state change triggered firing
4821 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004822 * <p>
4823 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004824 * to properties set by the super implementation:
4825 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4826 * super.onInitializeAccessibilityEvent(event);
4827 * event.setPassword(true);
4828 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004829 * <p>
4830 * If an {@link AccessibilityDelegate} has been specified via calling
4831 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4832 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4833 * is responsible for handling this call.
4834 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004835 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4836 * information to the event, in case the default implementation has basic information to add.
4837 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004838 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004839 *
4840 * @see #sendAccessibilityEvent(int)
4841 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4842 */
4843 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004844 if (mAccessibilityDelegate != null) {
4845 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4846 } else {
4847 onInitializeAccessibilityEventInternal(event);
4848 }
4849 }
4850
4851 /**
4852 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4853 *
4854 * Note: Called from the default {@link AccessibilityDelegate}.
4855 */
4856 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004857 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004858 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004859 event.setPackageName(getContext().getPackageName());
4860 event.setEnabled(isEnabled());
4861 event.setContentDescription(mContentDescription);
4862
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004863 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004864 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004865 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4866 FOCUSABLES_ALL);
4867 event.setItemCount(focusablesTempList.size());
4868 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4869 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004870 }
4871 }
4872
4873 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004874 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4875 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4876 * This method is responsible for obtaining an accessibility node info from a
4877 * pool of reusable instances and calling
4878 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4879 * initialize the former.
4880 * <p>
4881 * Note: The client is responsible for recycling the obtained instance by calling
4882 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4883 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004884 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004885 * @return A populated {@link AccessibilityNodeInfo}.
4886 *
4887 * @see AccessibilityNodeInfo
4888 */
4889 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004890 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4891 if (provider != null) {
4892 return provider.createAccessibilityNodeInfo(View.NO_ID);
4893 } else {
4894 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4895 onInitializeAccessibilityNodeInfo(info);
4896 return info;
4897 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004898 }
4899
4900 /**
4901 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4902 * The base implementation sets:
4903 * <ul>
4904 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004905 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4906 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004907 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4908 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4909 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4910 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4911 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4912 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4913 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4914 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4915 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4916 * </ul>
4917 * <p>
4918 * Subclasses should override this method, call the super implementation,
4919 * and set additional attributes.
4920 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004921 * <p>
4922 * If an {@link AccessibilityDelegate} has been specified via calling
4923 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4924 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4925 * is responsible for handling this call.
4926 * </p>
4927 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004928 * @param info The instance to initialize.
4929 */
4930 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004931 if (mAccessibilityDelegate != null) {
4932 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4933 } else {
4934 onInitializeAccessibilityNodeInfoInternal(info);
4935 }
4936 }
4937
4938 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004939 * Gets the location of this view in screen coordintates.
4940 *
4941 * @param outRect The output location
4942 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004943 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004944 if (mAttachInfo == null) {
4945 return;
4946 }
4947
4948 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004949 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004950
4951 if (!hasIdentityMatrix()) {
4952 getMatrix().mapRect(position);
4953 }
4954
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004955 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004956
4957 ViewParent parent = mParent;
4958 while (parent instanceof View) {
4959 View parentView = (View) parent;
4960
4961 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4962
4963 if (!parentView.hasIdentityMatrix()) {
4964 parentView.getMatrix().mapRect(position);
4965 }
4966
4967 position.offset(parentView.mLeft, parentView.mTop);
4968
4969 parent = parentView.mParent;
4970 }
4971
4972 if (parent instanceof ViewRootImpl) {
4973 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4974 position.offset(0, -viewRootImpl.mCurScrollY);
4975 }
4976
4977 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4978
4979 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4980 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4981 }
4982
4983 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004984 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4985 *
4986 * Note: Called from the default {@link AccessibilityDelegate}.
4987 */
4988 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004989 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004990
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004991 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004992 info.setBoundsInParent(bounds);
4993
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004994 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004995 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004996
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004997 ViewParent parent = getParentForAccessibility();
4998 if (parent instanceof View) {
4999 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005000 }
5001
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005002 if (mID != View.NO_ID) {
5003 View rootView = getRootView();
5004 if (rootView == null) {
5005 rootView = this;
5006 }
5007 View label = rootView.findLabelForView(this, mID);
5008 if (label != null) {
5009 info.setLabeledBy(label);
5010 }
Svetoslav Ganov80943d82013-01-02 10:25:37 -08005011
5012 if ((mAttachInfo.mAccessibilityFetchFlags
5013 & AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS) != 0) {
5014 String viewId = null;
5015 try {
5016 viewId = getResources().getResourceName(mID);
5017 } catch (Resources.NotFoundException nfe) {
5018 /* ignore */
5019 }
5020 if (viewId == null) {
5021 try {
5022 viewId = ((Context) ActivityThread.currentActivityThread()
5023 .getSystemContext()).getResources().getResourceName(mID);
5024 } catch (Resources.NotFoundException nfe) {
5025 /* ignore */
5026 }
5027 }
5028 info.setViewId(viewId);
5029 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005030 }
5031
5032 if (mLabelForId != View.NO_ID) {
5033 View rootView = getRootView();
5034 if (rootView == null) {
5035 rootView = this;
5036 }
5037 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
5038 if (labeled != null) {
5039 info.setLabelFor(labeled);
5040 }
5041 }
5042
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005043 info.setVisibleToUser(isVisibleToUser());
5044
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005045 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005046 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005047 info.setContentDescription(getContentDescription());
5048
5049 info.setEnabled(isEnabled());
5050 info.setClickable(isClickable());
5051 info.setFocusable(isFocusable());
5052 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005053 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005054 info.setSelected(isSelected());
5055 info.setLongClickable(isLongClickable());
5056
5057 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005058 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005059 // we should report as selectable view in an AdapterView.
5060 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5061 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5062
5063 if (isFocusable()) {
5064 if (isFocused()) {
5065 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5066 } else {
5067 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5068 }
5069 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005070
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005071 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005072 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005073 } else {
5074 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5075 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005076
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005077 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005078 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5079 }
5080
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005081 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005082 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5083 }
5084
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005085 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005086 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5087 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5088 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005089 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5090 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005091 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005092 }
5093
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005094 private View findLabelForView(View view, int labeledId) {
5095 if (mMatchLabelForPredicate == null) {
5096 mMatchLabelForPredicate = new MatchLabelForPredicate();
5097 }
5098 mMatchLabelForPredicate.mLabeledId = labeledId;
5099 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5100 }
5101
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005102 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005103 * Computes whether this view is visible to the user. Such a view is
5104 * attached, visible, all its predecessors are visible, it is not clipped
5105 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005106 *
5107 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005108 *
5109 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005110 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005111 protected boolean isVisibleToUser() {
5112 return isVisibleToUser(null);
5113 }
5114
5115 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005116 * Computes whether the given portion of this view is visible to the user.
5117 * Such a view is attached, visible, all its predecessors are visible,
5118 * has an alpha greater than zero, and the specified portion is not
5119 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005120 *
5121 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5122 * <code>null</code>, and the entire view will be tested in this case.
5123 * When <code>true</code> is returned by the function, the actual visible
5124 * region will be stored in this parameter; that is, if boundInView is fully
5125 * contained within the view, no modification will be made, otherwise regions
5126 * outside of the visible area of the view will be clipped.
5127 *
5128 * @return Whether the specified portion of the view is visible on the screen.
5129 *
5130 * @hide
5131 */
5132 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005133 if (mAttachInfo != null) {
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005134 // Attached to invisible window means this view is not visible.
5135 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
5136 return false;
5137 }
5138 // An invisible predecessor or one with alpha zero means
5139 // that this view is not visible to the user.
5140 Object current = this;
5141 while (current instanceof View) {
5142 View view = (View) current;
5143 // We have attach info so this view is attached and there is no
5144 // need to check whether we reach to ViewRootImpl on the way up.
5145 if (view.getAlpha() <= 0 || view.getVisibility() != VISIBLE) {
5146 return false;
5147 }
5148 current = view.mParent;
5149 }
5150 // Check if the view is entirely covered by its predecessors.
Romain Guyf0af1d52012-07-11 18:31:21 -07005151 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5152 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005153 if (!getGlobalVisibleRect(visibleRect, offset)) {
5154 return false;
Guang Zhu0d607fb2012-05-11 19:34:56 -07005155 }
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005156 // Check if the visible portion intersects the rectangle of interest.
5157 if (boundInView != null) {
5158 visibleRect.offset(-offset.x, -offset.y);
5159 return boundInView.intersect(visibleRect);
5160 }
5161 return true;
Romain Guyf0af1d52012-07-11 18:31:21 -07005162 }
Romain Guyf0af1d52012-07-11 18:31:21 -07005163 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005164 }
5165
5166 /**
alanvb72fe7a2012-08-27 16:44:25 -07005167 * Returns the delegate for implementing accessibility support via
5168 * composition. For more details see {@link AccessibilityDelegate}.
5169 *
5170 * @return The delegate, or null if none set.
5171 *
5172 * @hide
5173 */
5174 public AccessibilityDelegate getAccessibilityDelegate() {
5175 return mAccessibilityDelegate;
5176 }
5177
5178 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005179 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005180 * opposed to inheritance. The delegate's primary use is for implementing
5181 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5182 *
5183 * @param delegate The delegate instance.
5184 *
5185 * @see AccessibilityDelegate
5186 */
5187 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5188 mAccessibilityDelegate = delegate;
5189 }
5190
5191 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005192 * Gets the provider for managing a virtual view hierarchy rooted at this View
5193 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5194 * that explore the window content.
5195 * <p>
5196 * If this method returns an instance, this instance is responsible for managing
5197 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5198 * View including the one representing the View itself. Similarly the returned
5199 * instance is responsible for performing accessibility actions on any virtual
5200 * view or the root view itself.
5201 * </p>
5202 * <p>
5203 * If an {@link AccessibilityDelegate} has been specified via calling
5204 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5205 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5206 * is responsible for handling this call.
5207 * </p>
5208 *
5209 * @return The provider.
5210 *
5211 * @see AccessibilityNodeProvider
5212 */
5213 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5214 if (mAccessibilityDelegate != null) {
5215 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5216 } else {
5217 return null;
5218 }
5219 }
5220
5221 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005222 * Gets the unique identifier of this view on the screen for accessibility purposes.
5223 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5224 *
5225 * @return The view accessibility id.
5226 *
5227 * @hide
5228 */
5229 public int getAccessibilityViewId() {
5230 if (mAccessibilityViewId == NO_ID) {
5231 mAccessibilityViewId = sNextAccessibilityViewId++;
5232 }
5233 return mAccessibilityViewId;
5234 }
5235
5236 /**
5237 * Gets the unique identifier of the window in which this View reseides.
5238 *
5239 * @return The window accessibility id.
5240 *
5241 * @hide
5242 */
5243 public int getAccessibilityWindowId() {
5244 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5245 }
5246
5247 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005248 * Gets the {@link View} description. It briefly describes the view and is
5249 * primarily used for accessibility support. Set this property to enable
5250 * better accessibility support for your application. This is especially
5251 * true for views that do not have textual representation (For example,
5252 * ImageButton).
5253 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005254 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005255 *
5256 * @attr ref android.R.styleable#View_contentDescription
5257 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005258 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005259 public CharSequence getContentDescription() {
5260 return mContentDescription;
5261 }
5262
5263 /**
5264 * Sets the {@link View} description. It briefly describes the view and is
5265 * primarily used for accessibility support. Set this property to enable
5266 * better accessibility support for your application. This is especially
5267 * true for views that do not have textual representation (For example,
5268 * ImageButton).
5269 *
5270 * @param contentDescription The content description.
5271 *
5272 * @attr ref android.R.styleable#View_contentDescription
5273 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005274 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005275 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005276 if (mContentDescription == null) {
5277 if (contentDescription == null) {
5278 return;
5279 }
5280 } else if (mContentDescription.equals(contentDescription)) {
5281 return;
5282 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005283 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005284 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5285 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5286 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5287 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005288 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005289 }
5290
5291 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005292 * Gets the id of a view for which this view serves as a label for
5293 * accessibility purposes.
5294 *
5295 * @return The labeled view id.
5296 */
5297 @ViewDebug.ExportedProperty(category = "accessibility")
5298 public int getLabelFor() {
5299 return mLabelForId;
5300 }
5301
5302 /**
5303 * Sets the id of a view for which this view serves as a label for
5304 * accessibility purposes.
5305 *
5306 * @param id The labeled view id.
5307 */
5308 @RemotableViewMethod
5309 public void setLabelFor(int id) {
5310 mLabelForId = id;
5311 if (mLabelForId != View.NO_ID
5312 && mID == View.NO_ID) {
5313 mID = generateViewId();
5314 }
5315 }
5316
5317 /**
Romain Guya2431d02009-04-30 16:30:00 -07005318 * Invoked whenever this view loses focus, either by losing window focus or by losing
5319 * focus within its window. This method can be used to clear any state tied to the
5320 * focus. For instance, if a button is held pressed with the trackball and the window
5321 * loses focus, this method can be used to cancel the press.
5322 *
5323 * Subclasses of View overriding this method should always call super.onFocusLost().
5324 *
5325 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005326 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005327 *
5328 * @hide pending API council approval
5329 */
5330 protected void onFocusLost() {
5331 resetPressedState();
5332 }
5333
5334 private void resetPressedState() {
5335 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5336 return;
5337 }
5338
5339 if (isPressed()) {
5340 setPressed(false);
5341
5342 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005343 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005344 }
5345 }
5346 }
5347
5348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 * Returns true if this view has focus
5350 *
5351 * @return True if this view has focus, false otherwise.
5352 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005353 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005355 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 }
5357
5358 /**
5359 * Find the view in the hierarchy rooted at this view that currently has
5360 * focus.
5361 *
5362 * @return The view that currently has focus, or null if no focused view can
5363 * be found.
5364 */
5365 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005366 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 }
5368
5369 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005370 * Indicates whether this view is one of the set of scrollable containers in
5371 * its window.
5372 *
5373 * @return whether this view is one of the set of scrollable containers in
5374 * its window
5375 *
5376 * @attr ref android.R.styleable#View_isScrollContainer
5377 */
5378 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005379 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005380 }
5381
5382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 * Change whether this view is one of the set of scrollable containers in
5384 * its window. This will be used to determine whether the window can
5385 * resize or must pan when a soft input area is open -- scrollable
5386 * containers allow the window to use resize mode since the container
5387 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005388 *
5389 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 */
5391 public void setScrollContainer(boolean isScrollContainer) {
5392 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005393 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005395 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005397 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005399 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 mAttachInfo.mScrollContainers.remove(this);
5401 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005402 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 }
5404 }
5405
5406 /**
5407 * Returns the quality of the drawing cache.
5408 *
5409 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5410 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5411 *
5412 * @see #setDrawingCacheQuality(int)
5413 * @see #setDrawingCacheEnabled(boolean)
5414 * @see #isDrawingCacheEnabled()
5415 *
5416 * @attr ref android.R.styleable#View_drawingCacheQuality
5417 */
5418 public int getDrawingCacheQuality() {
5419 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5420 }
5421
5422 /**
5423 * Set the drawing cache quality of this view. This value is used only when the
5424 * drawing cache is enabled
5425 *
5426 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5427 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5428 *
5429 * @see #getDrawingCacheQuality()
5430 * @see #setDrawingCacheEnabled(boolean)
5431 * @see #isDrawingCacheEnabled()
5432 *
5433 * @attr ref android.R.styleable#View_drawingCacheQuality
5434 */
5435 public void setDrawingCacheQuality(int quality) {
5436 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5437 }
5438
5439 /**
5440 * Returns whether the screen should remain on, corresponding to the current
5441 * value of {@link #KEEP_SCREEN_ON}.
5442 *
5443 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5444 *
5445 * @see #setKeepScreenOn(boolean)
5446 *
5447 * @attr ref android.R.styleable#View_keepScreenOn
5448 */
5449 public boolean getKeepScreenOn() {
5450 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5451 }
5452
5453 /**
5454 * Controls whether the screen should remain on, modifying the
5455 * value of {@link #KEEP_SCREEN_ON}.
5456 *
5457 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5458 *
5459 * @see #getKeepScreenOn()
5460 *
5461 * @attr ref android.R.styleable#View_keepScreenOn
5462 */
5463 public void setKeepScreenOn(boolean keepScreenOn) {
5464 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5465 }
5466
5467 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005468 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5469 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 *
5471 * @attr ref android.R.styleable#View_nextFocusLeft
5472 */
5473 public int getNextFocusLeftId() {
5474 return mNextFocusLeftId;
5475 }
5476
5477 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005478 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5479 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5480 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 *
5482 * @attr ref android.R.styleable#View_nextFocusLeft
5483 */
5484 public void setNextFocusLeftId(int nextFocusLeftId) {
5485 mNextFocusLeftId = nextFocusLeftId;
5486 }
5487
5488 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005489 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5490 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 *
5492 * @attr ref android.R.styleable#View_nextFocusRight
5493 */
5494 public int getNextFocusRightId() {
5495 return mNextFocusRightId;
5496 }
5497
5498 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005499 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5500 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5501 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 *
5503 * @attr ref android.R.styleable#View_nextFocusRight
5504 */
5505 public void setNextFocusRightId(int nextFocusRightId) {
5506 mNextFocusRightId = nextFocusRightId;
5507 }
5508
5509 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005510 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5511 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 *
5513 * @attr ref android.R.styleable#View_nextFocusUp
5514 */
5515 public int getNextFocusUpId() {
5516 return mNextFocusUpId;
5517 }
5518
5519 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005520 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5521 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5522 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 *
5524 * @attr ref android.R.styleable#View_nextFocusUp
5525 */
5526 public void setNextFocusUpId(int nextFocusUpId) {
5527 mNextFocusUpId = nextFocusUpId;
5528 }
5529
5530 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005531 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5532 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 *
5534 * @attr ref android.R.styleable#View_nextFocusDown
5535 */
5536 public int getNextFocusDownId() {
5537 return mNextFocusDownId;
5538 }
5539
5540 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005541 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5542 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5543 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 *
5545 * @attr ref android.R.styleable#View_nextFocusDown
5546 */
5547 public void setNextFocusDownId(int nextFocusDownId) {
5548 mNextFocusDownId = nextFocusDownId;
5549 }
5550
5551 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005552 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5553 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5554 *
5555 * @attr ref android.R.styleable#View_nextFocusForward
5556 */
5557 public int getNextFocusForwardId() {
5558 return mNextFocusForwardId;
5559 }
5560
5561 /**
5562 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5563 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5564 * decide automatically.
5565 *
5566 * @attr ref android.R.styleable#View_nextFocusForward
5567 */
5568 public void setNextFocusForwardId(int nextFocusForwardId) {
5569 mNextFocusForwardId = nextFocusForwardId;
5570 }
5571
5572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 * Returns the visibility of this view and all of its ancestors
5574 *
5575 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5576 */
5577 public boolean isShown() {
5578 View current = this;
5579 //noinspection ConstantConditions
5580 do {
5581 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5582 return false;
5583 }
5584 ViewParent parent = current.mParent;
5585 if (parent == null) {
5586 return false; // We are not attached to the view root
5587 }
5588 if (!(parent instanceof View)) {
5589 return true;
5590 }
5591 current = (View) parent;
5592 } while (current != null);
5593
5594 return false;
5595 }
5596
5597 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005598 * Called by the view hierarchy when the content insets for a window have
5599 * changed, to allow it to adjust its content to fit within those windows.
5600 * The content insets tell you the space that the status bar, input method,
5601 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005603 * <p>You do not normally need to deal with this function, since the default
5604 * window decoration given to applications takes care of applying it to the
5605 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5606 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5607 * and your content can be placed under those system elements. You can then
5608 * use this method within your view hierarchy if you have parts of your UI
5609 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005611 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005612 * inset's to the view's padding, consuming that content (modifying the
5613 * insets to be 0), and returning true. This behavior is off by default, but can
5614 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5615 *
5616 * <p>This function's traversal down the hierarchy is depth-first. The same content
5617 * insets object is propagated down the hierarchy, so any changes made to it will
5618 * be seen by all following views (including potentially ones above in
5619 * the hierarchy since this is a depth-first traversal). The first view
5620 * that returns true will abort the entire traversal.
5621 *
5622 * <p>The default implementation works well for a situation where it is
5623 * used with a container that covers the entire window, allowing it to
5624 * apply the appropriate insets to its content on all edges. If you need
5625 * a more complicated layout (such as two different views fitting system
5626 * windows, one on the top of the window, and one on the bottom),
5627 * you can override the method and handle the insets however you would like.
5628 * Note that the insets provided by the framework are always relative to the
5629 * far edges of the window, not accounting for the location of the called view
5630 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005631 * where the layout will place the view, as it is done before layout happens.)
5632 *
5633 * <p>Note: unlike many View methods, there is no dispatch phase to this
5634 * call. If you are overriding it in a ViewGroup and want to allow the
5635 * call to continue to your children, you must be sure to call the super
5636 * implementation.
5637 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005638 * <p>Here is a sample layout that makes use of fitting system windows
5639 * to have controls for a video view placed inside of the window decorations
5640 * that it hides and shows. This can be used with code like the second
5641 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5642 *
5643 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5644 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005645 * @param insets Current content insets of the window. Prior to
5646 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5647 * the insets or else you and Android will be unhappy.
5648 *
5649 * @return Return true if this view applied the insets and it should not
5650 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005651 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005652 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005653 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 */
5655 protected boolean fitSystemWindows(Rect insets) {
5656 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005657 mUserPaddingStart = UNDEFINED_PADDING;
5658 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005659 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5660 || mAttachInfo == null
5661 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5662 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5663 return true;
5664 } else {
5665 internalSetPadding(0, 0, 0, 0);
5666 return false;
5667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 }
5669 return false;
5670 }
5671
5672 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005673 * Sets whether or not this view should account for system screen decorations
5674 * such as the status bar and inset its content; that is, controlling whether
5675 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5676 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005677 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005678 * <p>Note that if you are providing your own implementation of
5679 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5680 * flag to true -- your implementation will be overriding the default
5681 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005682 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005683 * @param fitSystemWindows If true, then the default implementation of
5684 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005685 *
5686 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005687 * @see #getFitsSystemWindows()
5688 * @see #fitSystemWindows(Rect)
5689 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005690 */
5691 public void setFitsSystemWindows(boolean fitSystemWindows) {
5692 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5693 }
5694
5695 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005696 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005697 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5698 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005699 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005700 * @return Returns true if the default implementation of
5701 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005702 *
5703 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005704 * @see #setFitsSystemWindows()
5705 * @see #fitSystemWindows(Rect)
5706 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005707 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005708 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005709 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5710 }
5711
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005712 /** @hide */
5713 public boolean fitsSystemWindows() {
5714 return getFitsSystemWindows();
5715 }
5716
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005717 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005718 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5719 */
5720 public void requestFitSystemWindows() {
5721 if (mParent != null) {
5722 mParent.requestFitSystemWindows();
5723 }
5724 }
5725
5726 /**
5727 * For use by PhoneWindow to make its own system window fitting optional.
5728 * @hide
5729 */
5730 public void makeOptionalFitsSystemWindows() {
5731 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5732 }
5733
5734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 * Returns the visibility status for this view.
5736 *
5737 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5738 * @attr ref android.R.styleable#View_visibility
5739 */
5740 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005741 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5742 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5743 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 })
5745 public int getVisibility() {
5746 return mViewFlags & VISIBILITY_MASK;
5747 }
5748
5749 /**
5750 * Set the enabled state of this view.
5751 *
5752 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5753 * @attr ref android.R.styleable#View_visibility
5754 */
5755 @RemotableViewMethod
5756 public void setVisibility(int visibility) {
5757 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005758 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 }
5760
5761 /**
5762 * Returns the enabled status for this view. The interpretation of the
5763 * enabled state varies by subclass.
5764 *
5765 * @return True if this view is enabled, false otherwise.
5766 */
5767 @ViewDebug.ExportedProperty
5768 public boolean isEnabled() {
5769 return (mViewFlags & ENABLED_MASK) == ENABLED;
5770 }
5771
5772 /**
5773 * Set the enabled state of this view. The interpretation of the enabled
5774 * state varies by subclass.
5775 *
5776 * @param enabled True if this view is enabled, false otherwise.
5777 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005778 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005780 if (enabled == isEnabled()) return;
5781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5783
5784 /*
5785 * The View most likely has to change its appearance, so refresh
5786 * the drawable state.
5787 */
5788 refreshDrawableState();
5789
5790 // Invalidate too, since the default behavior for views is to be
5791 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005792 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 }
5794
5795 /**
5796 * Set whether this view can receive the focus.
5797 *
5798 * Setting this to false will also ensure that this view is not focusable
5799 * in touch mode.
5800 *
5801 * @param focusable If true, this view can receive the focus.
5802 *
5803 * @see #setFocusableInTouchMode(boolean)
5804 * @attr ref android.R.styleable#View_focusable
5805 */
5806 public void setFocusable(boolean focusable) {
5807 if (!focusable) {
5808 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5809 }
5810 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5811 }
5812
5813 /**
5814 * Set whether this view can receive focus while in touch mode.
5815 *
5816 * Setting this to true will also ensure that this view is focusable.
5817 *
5818 * @param focusableInTouchMode If true, this view can receive the focus while
5819 * in touch mode.
5820 *
5821 * @see #setFocusable(boolean)
5822 * @attr ref android.R.styleable#View_focusableInTouchMode
5823 */
5824 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5825 // Focusable in touch mode should always be set before the focusable flag
5826 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5827 // which, in touch mode, will not successfully request focus on this view
5828 // because the focusable in touch mode flag is not set
5829 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5830 if (focusableInTouchMode) {
5831 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5832 }
5833 }
5834
5835 /**
5836 * Set whether this view should have sound effects enabled for events such as
5837 * clicking and touching.
5838 *
5839 * <p>You may wish to disable sound effects for a view if you already play sounds,
5840 * for instance, a dial key that plays dtmf tones.
5841 *
5842 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5843 * @see #isSoundEffectsEnabled()
5844 * @see #playSoundEffect(int)
5845 * @attr ref android.R.styleable#View_soundEffectsEnabled
5846 */
5847 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5848 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5849 }
5850
5851 /**
5852 * @return whether this view should have sound effects enabled for events such as
5853 * clicking and touching.
5854 *
5855 * @see #setSoundEffectsEnabled(boolean)
5856 * @see #playSoundEffect(int)
5857 * @attr ref android.R.styleable#View_soundEffectsEnabled
5858 */
5859 @ViewDebug.ExportedProperty
5860 public boolean isSoundEffectsEnabled() {
5861 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5862 }
5863
5864 /**
5865 * Set whether this view should have haptic feedback for events such as
5866 * long presses.
5867 *
5868 * <p>You may wish to disable haptic feedback if your view already controls
5869 * its own haptic feedback.
5870 *
5871 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5872 * @see #isHapticFeedbackEnabled()
5873 * @see #performHapticFeedback(int)
5874 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5875 */
5876 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5877 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5878 }
5879
5880 /**
5881 * @return whether this view should have haptic feedback enabled for events
5882 * long presses.
5883 *
5884 * @see #setHapticFeedbackEnabled(boolean)
5885 * @see #performHapticFeedback(int)
5886 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5887 */
5888 @ViewDebug.ExportedProperty
5889 public boolean isHapticFeedbackEnabled() {
5890 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5891 }
5892
5893 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005894 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005895 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005896 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5897 * {@link #LAYOUT_DIRECTION_RTL},
5898 * {@link #LAYOUT_DIRECTION_INHERIT} or
5899 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005900 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005901 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005902 *
5903 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005904 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005905 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005906 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5907 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5908 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5909 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005910 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005911 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005912 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005913 }
5914
5915 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005916 * Set the layout direction for this view. This will propagate a reset of layout direction
5917 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005918 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005919 * @param layoutDirection the layout direction to set. Should be one of:
5920 *
5921 * {@link #LAYOUT_DIRECTION_LTR},
5922 * {@link #LAYOUT_DIRECTION_RTL},
5923 * {@link #LAYOUT_DIRECTION_INHERIT},
5924 * {@link #LAYOUT_DIRECTION_LOCALE}.
5925 *
5926 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5927 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5928 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005929 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005930 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005931 */
5932 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005933 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005934 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005935 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005936 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005937 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005938 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005939 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005940 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005941 // We need to resolve all RTL properties as they all depend on layout direction
5942 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005943 requestLayout();
5944 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005945 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005946 }
5947
5948 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005949 * Returns the resolved layout direction for this view.
5950 *
5951 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005952 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005953 *
5954 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5955 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005956 *
5957 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005958 */
5959 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005960 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5961 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005962 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005963 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005964 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5965 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005966 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005967 return LAYOUT_DIRECTION_LTR;
5968 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005969 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5970 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005971 }
5972
5973 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005974 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5975 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005976 *
5977 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005978 *
5979 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005980 */
5981 @ViewDebug.ExportedProperty(category = "layout")
5982 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005983 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005984 }
5985
5986 /**
Adam Powell539ee872012-02-03 19:00:49 -08005987 * Indicates whether the view is currently tracking transient state that the
5988 * app should not need to concern itself with saving and restoring, but that
5989 * the framework should take special note to preserve when possible.
5990 *
Adam Powell785c4472012-05-02 21:25:39 -07005991 * <p>A view with transient state cannot be trivially rebound from an external
5992 * data source, such as an adapter binding item views in a list. This may be
5993 * because the view is performing an animation, tracking user selection
5994 * of content, or similar.</p>
5995 *
Adam Powell539ee872012-02-03 19:00:49 -08005996 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005997 */
5998 @ViewDebug.ExportedProperty(category = "layout")
5999 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006000 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08006001 }
6002
6003 /**
6004 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07006005 * framework should attempt to preserve when possible. This flag is reference counted,
6006 * so every call to setHasTransientState(true) should be paired with a later call
6007 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08006008 *
Adam Powell785c4472012-05-02 21:25:39 -07006009 * <p>A view with transient state cannot be trivially rebound from an external
6010 * data source, such as an adapter binding item views in a list. This may be
6011 * because the view is performing an animation, tracking user selection
6012 * of content, or similar.</p>
6013 *
Adam Powell539ee872012-02-03 19:00:49 -08006014 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08006015 */
6016 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07006017 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
6018 mTransientStateCount - 1;
6019 if (mTransientStateCount < 0) {
6020 mTransientStateCount = 0;
6021 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
6022 "unmatched pair of setHasTransientState calls");
6023 }
6024 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07006025 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07006026 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07006027 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
6028 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07006029 if (mParent != null) {
6030 try {
6031 mParent.childHasTransientStateChanged(this, hasTransientState);
6032 } catch (AbstractMethodError e) {
6033 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
6034 " does not fully implement ViewParent", e);
6035 }
Adam Powell539ee872012-02-03 19:00:49 -08006036 }
6037 }
6038 }
6039
6040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 * If this view doesn't do any drawing on its own, set this flag to
6042 * allow further optimizations. By default, this flag is not set on
6043 * View, but could be set on some View subclasses such as ViewGroup.
6044 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006045 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
6046 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 *
6048 * @param willNotDraw whether or not this View draw on its own
6049 */
6050 public void setWillNotDraw(boolean willNotDraw) {
6051 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
6052 }
6053
6054 /**
6055 * Returns whether or not this View draws on its own.
6056 *
6057 * @return true if this view has nothing to draw, false otherwise
6058 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006059 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 public boolean willNotDraw() {
6061 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6062 }
6063
6064 /**
6065 * When a View's drawing cache is enabled, drawing is redirected to an
6066 * offscreen bitmap. Some views, like an ImageView, must be able to
6067 * bypass this mechanism if they already draw a single bitmap, to avoid
6068 * unnecessary usage of the memory.
6069 *
6070 * @param willNotCacheDrawing true if this view does not cache its
6071 * drawing, false otherwise
6072 */
6073 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6074 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6075 }
6076
6077 /**
6078 * Returns whether or not this View can cache its drawing or not.
6079 *
6080 * @return true if this view does not cache its drawing, false otherwise
6081 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006082 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 public boolean willNotCacheDrawing() {
6084 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6085 }
6086
6087 /**
6088 * Indicates whether this view reacts to click events or not.
6089 *
6090 * @return true if the view is clickable, false otherwise
6091 *
6092 * @see #setClickable(boolean)
6093 * @attr ref android.R.styleable#View_clickable
6094 */
6095 @ViewDebug.ExportedProperty
6096 public boolean isClickable() {
6097 return (mViewFlags & CLICKABLE) == CLICKABLE;
6098 }
6099
6100 /**
6101 * Enables or disables click events for this view. When a view
6102 * is clickable it will change its state to "pressed" on every click.
6103 * Subclasses should set the view clickable to visually react to
6104 * user's clicks.
6105 *
6106 * @param clickable true to make the view clickable, false otherwise
6107 *
6108 * @see #isClickable()
6109 * @attr ref android.R.styleable#View_clickable
6110 */
6111 public void setClickable(boolean clickable) {
6112 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6113 }
6114
6115 /**
6116 * Indicates whether this view reacts to long click events or not.
6117 *
6118 * @return true if the view is long clickable, false otherwise
6119 *
6120 * @see #setLongClickable(boolean)
6121 * @attr ref android.R.styleable#View_longClickable
6122 */
6123 public boolean isLongClickable() {
6124 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6125 }
6126
6127 /**
6128 * Enables or disables long click events for this view. When a view is long
6129 * clickable it reacts to the user holding down the button for a longer
6130 * duration than a tap. This event can either launch the listener or a
6131 * context menu.
6132 *
6133 * @param longClickable true to make the view long clickable, false otherwise
6134 * @see #isLongClickable()
6135 * @attr ref android.R.styleable#View_longClickable
6136 */
6137 public void setLongClickable(boolean longClickable) {
6138 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6139 }
6140
6141 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006142 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 *
6144 * @see #isClickable()
6145 * @see #setClickable(boolean)
6146 *
6147 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6148 * the View's internal state from a previously set "pressed" state.
6149 */
6150 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006151 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006154 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006156 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006158
6159 if (needsRefresh) {
6160 refreshDrawableState();
6161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 dispatchSetPressed(pressed);
6163 }
6164
6165 /**
6166 * Dispatch setPressed to all of this View's children.
6167 *
6168 * @see #setPressed(boolean)
6169 *
6170 * @param pressed The new pressed state
6171 */
6172 protected void dispatchSetPressed(boolean pressed) {
6173 }
6174
6175 /**
6176 * Indicates whether the view is currently in pressed state. Unless
6177 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6178 * the pressed state.
6179 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006180 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 * @see #isClickable()
6182 * @see #setClickable(boolean)
6183 *
6184 * @return true if the view is currently pressed, false otherwise
6185 */
6186 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006187 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 }
6189
6190 /**
6191 * Indicates whether this view will save its state (that is,
6192 * whether its {@link #onSaveInstanceState} method will be called).
6193 *
6194 * @return Returns true if the view state saving is enabled, else false.
6195 *
6196 * @see #setSaveEnabled(boolean)
6197 * @attr ref android.R.styleable#View_saveEnabled
6198 */
6199 public boolean isSaveEnabled() {
6200 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6201 }
6202
6203 /**
6204 * Controls whether the saving of this view's state is
6205 * enabled (that is, whether its {@link #onSaveInstanceState} method
6206 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006207 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 * for its state to be saved. This flag can only disable the
6209 * saving of this view; any child views may still have their state saved.
6210 *
6211 * @param enabled Set to false to <em>disable</em> state saving, or true
6212 * (the default) to allow it.
6213 *
6214 * @see #isSaveEnabled()
6215 * @see #setId(int)
6216 * @see #onSaveInstanceState()
6217 * @attr ref android.R.styleable#View_saveEnabled
6218 */
6219 public void setSaveEnabled(boolean enabled) {
6220 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6221 }
6222
Jeff Brown85a31762010-09-01 17:01:00 -07006223 /**
6224 * Gets whether the framework should discard touches when the view's
6225 * window is obscured by another visible window.
6226 * Refer to the {@link View} security documentation for more details.
6227 *
6228 * @return True if touch filtering is enabled.
6229 *
6230 * @see #setFilterTouchesWhenObscured(boolean)
6231 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6232 */
6233 @ViewDebug.ExportedProperty
6234 public boolean getFilterTouchesWhenObscured() {
6235 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6236 }
6237
6238 /**
6239 * Sets whether the framework should discard touches when the view's
6240 * window is obscured by another visible window.
6241 * Refer to the {@link View} security documentation for more details.
6242 *
6243 * @param enabled True if touch filtering should be enabled.
6244 *
6245 * @see #getFilterTouchesWhenObscured
6246 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6247 */
6248 public void setFilterTouchesWhenObscured(boolean enabled) {
6249 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6250 FILTER_TOUCHES_WHEN_OBSCURED);
6251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252
6253 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006254 * Indicates whether the entire hierarchy under this view will save its
6255 * state when a state saving traversal occurs from its parent. The default
6256 * is true; if false, these views will not be saved unless
6257 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6258 *
6259 * @return Returns true if the view state saving from parent is enabled, else false.
6260 *
6261 * @see #setSaveFromParentEnabled(boolean)
6262 */
6263 public boolean isSaveFromParentEnabled() {
6264 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6265 }
6266
6267 /**
6268 * Controls whether the entire hierarchy under this view will save its
6269 * state when a state saving traversal occurs from its parent. The default
6270 * is true; if false, these views will not be saved unless
6271 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6272 *
6273 * @param enabled Set to false to <em>disable</em> state saving, or true
6274 * (the default) to allow it.
6275 *
6276 * @see #isSaveFromParentEnabled()
6277 * @see #setId(int)
6278 * @see #onSaveInstanceState()
6279 */
6280 public void setSaveFromParentEnabled(boolean enabled) {
6281 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6282 }
6283
6284
6285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 * Returns whether this View is able to take focus.
6287 *
6288 * @return True if this view can take focus, or false otherwise.
6289 * @attr ref android.R.styleable#View_focusable
6290 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006291 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 public final boolean isFocusable() {
6293 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6294 }
6295
6296 /**
6297 * When a view is focusable, it may not want to take focus when in touch mode.
6298 * For example, a button would like focus when the user is navigating via a D-pad
6299 * so that the user can click on it, but once the user starts touching the screen,
6300 * the button shouldn't take focus
6301 * @return Whether the view is focusable in touch mode.
6302 * @attr ref android.R.styleable#View_focusableInTouchMode
6303 */
6304 @ViewDebug.ExportedProperty
6305 public final boolean isFocusableInTouchMode() {
6306 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6307 }
6308
6309 /**
6310 * Find the nearest view in the specified direction that can take focus.
6311 * This does not actually give focus to that view.
6312 *
6313 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6314 *
6315 * @return The nearest focusable in the specified direction, or null if none
6316 * can be found.
6317 */
6318 public View focusSearch(int direction) {
6319 if (mParent != null) {
6320 return mParent.focusSearch(this, direction);
6321 } else {
6322 return null;
6323 }
6324 }
6325
6326 /**
6327 * This method is the last chance for the focused view and its ancestors to
6328 * respond to an arrow key. This is called when the focused view did not
6329 * consume the key internally, nor could the view system find a new view in
6330 * the requested direction to give focus to.
6331 *
6332 * @param focused The currently focused view.
6333 * @param direction The direction focus wants to move. One of FOCUS_UP,
6334 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6335 * @return True if the this view consumed this unhandled move.
6336 */
6337 public boolean dispatchUnhandledMove(View focused, int direction) {
6338 return false;
6339 }
6340
6341 /**
6342 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006343 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006345 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6346 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 * @return The user specified next view, or null if there is none.
6348 */
6349 View findUserSetNextFocus(View root, int direction) {
6350 switch (direction) {
6351 case FOCUS_LEFT:
6352 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006353 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 case FOCUS_RIGHT:
6355 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006356 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 case FOCUS_UP:
6358 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006359 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 case FOCUS_DOWN:
6361 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006362 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006363 case FOCUS_FORWARD:
6364 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006365 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006366 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006367 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006368 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006369 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006370 @Override
6371 public boolean apply(View t) {
6372 return t.mNextFocusForwardId == id;
6373 }
6374 });
6375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 }
6377 return null;
6378 }
6379
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006380 private View findViewInsideOutShouldExist(View root, int id) {
6381 if (mMatchIdPredicate == null) {
6382 mMatchIdPredicate = new MatchIdPredicate();
6383 }
6384 mMatchIdPredicate.mId = id;
6385 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006387 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 }
6389 return result;
6390 }
6391
6392 /**
6393 * Find and return all focusable views that are descendants of this view,
6394 * possibly including this view if it is focusable itself.
6395 *
6396 * @param direction The direction of the focus
6397 * @return A list of focusable views
6398 */
6399 public ArrayList<View> getFocusables(int direction) {
6400 ArrayList<View> result = new ArrayList<View>(24);
6401 addFocusables(result, direction);
6402 return result;
6403 }
6404
6405 /**
6406 * Add any focusable views that are descendants of this view (possibly
6407 * including this view if it is focusable itself) to views. If we are in touch mode,
6408 * only add views that are also focusable in touch mode.
6409 *
6410 * @param views Focusable views found so far
6411 * @param direction The direction of the focus
6412 */
6413 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006414 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416
svetoslavganov75986cf2009-05-14 22:28:01 -07006417 /**
6418 * Adds any focusable views that are descendants of this view (possibly
6419 * including this view if it is focusable itself) to views. This method
6420 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006421 * only views focusable in touch mode if we are in touch mode or
6422 * only views that can take accessibility focus if accessibility is enabeld
6423 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006424 *
6425 * @param views Focusable views found so far or null if all we are interested is
6426 * the number of focusables.
6427 * @param direction The direction of the focus.
6428 * @param focusableMode The type of focusables to be added.
6429 *
6430 * @see #FOCUSABLES_ALL
6431 * @see #FOCUSABLES_TOUCH_MODE
6432 */
6433 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006434 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006435 return;
6436 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006437 if (!isFocusable()) {
6438 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006439 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006440 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6441 && isInTouchMode() && !isFocusableInTouchMode()) {
6442 return;
6443 }
6444 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 }
6446
6447 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006448 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006449 * The search is performed by either the text that the View renders or the content
6450 * description that describes the view for accessibility purposes and the view does
6451 * not render or both. Clients can specify how the search is to be performed via
6452 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6453 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006454 *
6455 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006456 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006457 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006458 * @see #FIND_VIEWS_WITH_TEXT
6459 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6460 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006461 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006462 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006463 if (getAccessibilityNodeProvider() != null) {
6464 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6465 outViews.add(this);
6466 }
6467 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006468 && (searched != null && searched.length() > 0)
6469 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006470 String searchedLowerCase = searched.toString().toLowerCase();
6471 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6472 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6473 outViews.add(this);
6474 }
6475 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006476 }
6477
6478 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 * Find and return all touchable views that are descendants of this view,
6480 * possibly including this view if it is touchable itself.
6481 *
6482 * @return A list of touchable views
6483 */
6484 public ArrayList<View> getTouchables() {
6485 ArrayList<View> result = new ArrayList<View>();
6486 addTouchables(result);
6487 return result;
6488 }
6489
6490 /**
6491 * Add any touchable views that are descendants of this view (possibly
6492 * including this view if it is touchable itself) to views.
6493 *
6494 * @param views Touchable views found so far
6495 */
6496 public void addTouchables(ArrayList<View> views) {
6497 final int viewFlags = mViewFlags;
6498
6499 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6500 && (viewFlags & ENABLED_MASK) == ENABLED) {
6501 views.add(this);
6502 }
6503 }
6504
6505 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006506 * Returns whether this View is accessibility focused.
6507 *
6508 * @return True if this View is accessibility focused.
6509 */
6510 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006511 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006512 }
6513
6514 /**
6515 * Call this to try to give accessibility focus to this view.
6516 *
6517 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6518 * returns false or the view is no visible or the view already has accessibility
6519 * focus.
6520 *
6521 * See also {@link #focusSearch(int)}, which is what you call to say that you
6522 * have focus, and you want your parent to look for the next one.
6523 *
6524 * @return Whether this view actually took accessibility focus.
6525 *
6526 * @hide
6527 */
6528 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006529 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6530 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006531 return false;
6532 }
6533 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6534 return false;
6535 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006536 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6537 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006538 ViewRootImpl viewRootImpl = getViewRootImpl();
6539 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006540 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006541 }
6542 invalidate();
6543 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6544 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006545 return true;
6546 }
6547 return false;
6548 }
6549
6550 /**
6551 * Call this to try to clear accessibility focus of this view.
6552 *
6553 * See also {@link #focusSearch(int)}, which is what you call to say that you
6554 * have focus, and you want your parent to look for the next one.
6555 *
6556 * @hide
6557 */
6558 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006559 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6560 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006561 invalidate();
6562 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6563 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006564 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006565 // Clear the global reference of accessibility focus if this
6566 // view or any of its descendants had accessibility focus.
6567 ViewRootImpl viewRootImpl = getViewRootImpl();
6568 if (viewRootImpl != null) {
6569 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6570 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006571 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006572 }
6573 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006574 }
6575
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006576 private void sendAccessibilityHoverEvent(int eventType) {
6577 // Since we are not delivering to a client accessibility events from not
6578 // important views (unless the clinet request that) we need to fire the
6579 // event from the deepest view exposed to the client. As a consequence if
6580 // the user crosses a not exposed view the client will see enter and exit
6581 // of the exposed predecessor followed by and enter and exit of that same
6582 // predecessor when entering and exiting the not exposed descendant. This
6583 // is fine since the client has a clear idea which view is hovered at the
6584 // price of a couple more events being sent. This is a simple and
6585 // working solution.
6586 View source = this;
6587 while (true) {
6588 if (source.includeForAccessibility()) {
6589 source.sendAccessibilityEvent(eventType);
6590 return;
6591 }
6592 ViewParent parent = source.getParent();
6593 if (parent instanceof View) {
6594 source = (View) parent;
6595 } else {
6596 return;
6597 }
6598 }
6599 }
6600
Svetoslav Ganov42138042012-03-20 11:51:39 -07006601 /**
6602 * Clears accessibility focus without calling any callback methods
6603 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6604 * is used for clearing accessibility focus when giving this focus to
6605 * another view.
6606 */
6607 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006608 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6609 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006610 invalidate();
6611 }
6612 }
6613
6614 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 * Call this to try to give focus to a specific view or to one of its
6616 * descendants.
6617 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006618 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6619 * false), or if it is focusable and it is not focusable in touch mode
6620 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006622 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 * have focus, and you want your parent to look for the next one.
6624 *
6625 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6626 * {@link #FOCUS_DOWN} and <code>null</code>.
6627 *
6628 * @return Whether this view or one of its descendants actually took focus.
6629 */
6630 public final boolean requestFocus() {
6631 return requestFocus(View.FOCUS_DOWN);
6632 }
6633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 /**
6635 * Call this to try to give focus to a specific view or to one of its
6636 * descendants and give it a hint about what direction focus is heading.
6637 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006638 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6639 * false), or if it is focusable and it is not focusable in touch mode
6640 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006642 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 * have focus, and you want your parent to look for the next one.
6644 *
6645 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6646 * <code>null</code> set for the previously focused rectangle.
6647 *
6648 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6649 * @return Whether this view or one of its descendants actually took focus.
6650 */
6651 public final boolean requestFocus(int direction) {
6652 return requestFocus(direction, null);
6653 }
6654
6655 /**
6656 * Call this to try to give focus to a specific view or to one of its descendants
6657 * and give it hints about the direction and a specific rectangle that the focus
6658 * is coming from. The rectangle can help give larger views a finer grained hint
6659 * about where focus is coming from, and therefore, where to show selection, or
6660 * forward focus change internally.
6661 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006662 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6663 * false), or if it is focusable and it is not focusable in touch mode
6664 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 *
6666 * A View will not take focus if it is not visible.
6667 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006668 * A View will not take focus if one of its parents has
6669 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6670 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006672 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 * have focus, and you want your parent to look for the next one.
6674 *
6675 * You may wish to override this method if your custom {@link View} has an internal
6676 * {@link View} that it wishes to forward the request to.
6677 *
6678 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6679 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6680 * to give a finer grained hint about where focus is coming from. May be null
6681 * if there is no hint.
6682 * @return Whether this view or one of its descendants actually took focus.
6683 */
6684 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006685 return requestFocusNoSearch(direction, previouslyFocusedRect);
6686 }
6687
6688 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 // need to be focusable
6690 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6691 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6692 return false;
6693 }
6694
6695 // need to be focusable in touch mode if in touch mode
6696 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006697 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6698 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 }
6700
6701 // need to not have any parents blocking us
6702 if (hasAncestorThatBlocksDescendantFocus()) {
6703 return false;
6704 }
6705
6706 handleFocusGainInternal(direction, previouslyFocusedRect);
6707 return true;
6708 }
6709
6710 /**
6711 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6712 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6713 * touch mode to request focus when they are touched.
6714 *
6715 * @return Whether this view or one of its descendants actually took focus.
6716 *
6717 * @see #isInTouchMode()
6718 *
6719 */
6720 public final boolean requestFocusFromTouch() {
6721 // Leave touch mode if we need to
6722 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006723 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006724 if (viewRoot != null) {
6725 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006726 }
6727 }
6728 return requestFocus(View.FOCUS_DOWN);
6729 }
6730
6731 /**
6732 * @return Whether any ancestor of this view blocks descendant focus.
6733 */
6734 private boolean hasAncestorThatBlocksDescendantFocus() {
6735 ViewParent ancestor = mParent;
6736 while (ancestor instanceof ViewGroup) {
6737 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6738 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6739 return true;
6740 } else {
6741 ancestor = vgAncestor.getParent();
6742 }
6743 }
6744 return false;
6745 }
6746
6747 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006748 * Gets the mode for determining whether this View is important for accessibility
6749 * which is if it fires accessibility events and if it is reported to
6750 * accessibility services that query the screen.
6751 *
6752 * @return The mode for determining whether a View is important for accessibility.
6753 *
6754 * @attr ref android.R.styleable#View_importantForAccessibility
6755 *
6756 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6757 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6758 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6759 */
6760 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006761 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6762 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6763 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006764 })
6765 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006766 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6767 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006768 }
6769
6770 /**
6771 * Sets how to determine whether this view is important for accessibility
6772 * which is if it fires accessibility events and if it is reported to
6773 * accessibility services that query the screen.
6774 *
6775 * @param mode How to determine whether this view is important for accessibility.
6776 *
6777 * @attr ref android.R.styleable#View_importantForAccessibility
6778 *
6779 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6780 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6781 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6782 */
6783 public void setImportantForAccessibility(int mode) {
6784 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006785 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6786 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6787 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006788 notifyAccessibilityStateChanged();
6789 }
6790 }
6791
6792 /**
6793 * Gets whether this view should be exposed for accessibility.
6794 *
6795 * @return Whether the view is exposed for accessibility.
6796 *
6797 * @hide
6798 */
6799 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006800 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6801 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006802 switch (mode) {
6803 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6804 return true;
6805 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6806 return false;
6807 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006808 return isActionableForAccessibility() || hasListenersForAccessibility()
6809 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006810 default:
6811 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6812 + mode);
6813 }
6814 }
6815
6816 /**
6817 * Gets the parent for accessibility purposes. Note that the parent for
6818 * accessibility is not necessary the immediate parent. It is the first
6819 * predecessor that is important for accessibility.
6820 *
6821 * @return The parent for accessibility purposes.
6822 */
6823 public ViewParent getParentForAccessibility() {
6824 if (mParent instanceof View) {
6825 View parentView = (View) mParent;
6826 if (parentView.includeForAccessibility()) {
6827 return mParent;
6828 } else {
6829 return mParent.getParentForAccessibility();
6830 }
6831 }
6832 return null;
6833 }
6834
6835 /**
6836 * Adds the children of a given View for accessibility. Since some Views are
6837 * not important for accessibility the children for accessibility are not
6838 * necessarily direct children of the riew, rather they are the first level of
6839 * descendants important for accessibility.
6840 *
6841 * @param children The list of children for accessibility.
6842 */
6843 public void addChildrenForAccessibility(ArrayList<View> children) {
6844 if (includeForAccessibility()) {
6845 children.add(this);
6846 }
6847 }
6848
6849 /**
6850 * Whether to regard this view for accessibility. A view is regarded for
6851 * accessibility if it is important for accessibility or the querying
6852 * accessibility service has explicitly requested that view not
6853 * important for accessibility are regarded.
6854 *
6855 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006856 *
6857 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006858 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006859 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006860 if (mAttachInfo != null) {
Svetoslav Ganov80943d82013-01-02 10:25:37 -08006861 return (mAttachInfo.mAccessibilityFetchFlags
6862 & AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0
6863 || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006864 }
6865 return false;
6866 }
6867
6868 /**
6869 * Returns whether the View is considered actionable from
6870 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006871 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006872 *
6873 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006874 *
6875 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006876 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006877 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006878 return (isClickable() || isLongClickable() || isFocusable());
6879 }
6880
6881 /**
6882 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006883 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006884 *
6885 * @return True if the view is actionable for accessibility.
6886 */
6887 private boolean hasListenersForAccessibility() {
6888 ListenerInfo info = getListenerInfo();
6889 return mTouchDelegate != null || info.mOnKeyListener != null
6890 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6891 || info.mOnHoverListener != null || info.mOnDragListener != null;
6892 }
6893
6894 /**
6895 * Notifies accessibility services that some view's important for
6896 * accessibility state has changed. Note that such notifications
6897 * are made at most once every
6898 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6899 * to avoid unnecessary load to the system. Also once a view has
6900 * made a notifucation this method is a NOP until the notification has
6901 * been sent to clients.
6902 *
6903 * @hide
6904 *
6905 * TODO: Makse sure this method is called for any view state change
6906 * that is interesting for accessilility purposes.
6907 */
6908 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006909 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6910 return;
6911 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006912 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6913 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006914 if (mParent != null) {
6915 mParent.childAccessibilityStateChanged(this);
6916 }
6917 }
6918 }
6919
6920 /**
6921 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006922 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006923 *
6924 * @hide
6925 */
6926 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006927 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006928 }
6929
6930 /**
6931 * Performs the specified accessibility action on the view. For
6932 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006933 * <p>
6934 * If an {@link AccessibilityDelegate} has been specified via calling
6935 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6936 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6937 * is responsible for handling this call.
6938 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006939 *
6940 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006941 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006942 * @return Whether the action was performed.
6943 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006944 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006945 if (mAccessibilityDelegate != null) {
6946 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6947 } else {
6948 return performAccessibilityActionInternal(action, arguments);
6949 }
6950 }
6951
6952 /**
6953 * @see #performAccessibilityAction(int, Bundle)
6954 *
6955 * Note: Called from the default {@link AccessibilityDelegate}.
6956 */
6957 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006958 switch (action) {
6959 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006960 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006961 performClick();
6962 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006963 }
6964 } break;
6965 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6966 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006967 performLongClick();
6968 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006969 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006970 } break;
6971 case AccessibilityNodeInfo.ACTION_FOCUS: {
6972 if (!hasFocus()) {
6973 // Get out of touch mode since accessibility
6974 // wants to move focus around.
6975 getViewRootImpl().ensureTouchMode(false);
6976 return requestFocus();
6977 }
6978 } break;
6979 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6980 if (hasFocus()) {
6981 clearFocus();
6982 return !isFocused();
6983 }
6984 } break;
6985 case AccessibilityNodeInfo.ACTION_SELECT: {
6986 if (!isSelected()) {
6987 setSelected(true);
6988 return isSelected();
6989 }
6990 } break;
6991 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6992 if (isSelected()) {
6993 setSelected(false);
6994 return !isSelected();
6995 }
6996 } break;
6997 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006998 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006999 return requestAccessibilityFocus();
7000 }
7001 } break;
7002 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
7003 if (isAccessibilityFocused()) {
7004 clearAccessibilityFocus();
7005 return true;
7006 }
7007 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007008 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
7009 if (arguments != null) {
7010 final int granularity = arguments.getInt(
7011 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
7012 return nextAtGranularity(granularity);
7013 }
7014 } break;
7015 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
7016 if (arguments != null) {
7017 final int granularity = arguments.getInt(
7018 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
7019 return previousAtGranularity(granularity);
7020 }
7021 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007022 }
7023 return false;
7024 }
7025
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007026 private boolean nextAtGranularity(int granularity) {
7027 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007028 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007029 return false;
7030 }
7031 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7032 if (iterator == null) {
7033 return false;
7034 }
7035 final int current = getAccessibilityCursorPosition();
7036 final int[] range = iterator.following(current);
7037 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007038 return false;
7039 }
7040 final int start = range[0];
7041 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007042 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007043 sendViewTextTraversedAtGranularityEvent(
7044 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
7045 granularity, start, end);
7046 return true;
7047 }
7048
7049 private boolean previousAtGranularity(int granularity) {
7050 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007051 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007052 return false;
7053 }
7054 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7055 if (iterator == null) {
7056 return false;
7057 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007058 int current = getAccessibilityCursorPosition();
7059 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7060 current = text.length();
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007061 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007062 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7063 // When traversing by character we always put the cursor after the character
7064 // to ease edit and have to compensate before asking the for previous segment.
7065 current--;
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007066 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007067 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007068 final int[] range = iterator.preceding(current);
7069 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007070 return false;
7071 }
7072 final int start = range[0];
7073 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007074 // Always put the cursor after the character to ease edit.
7075 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7076 setAccessibilityCursorPosition(end);
7077 } else {
7078 setAccessibilityCursorPosition(start);
7079 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007080 sendViewTextTraversedAtGranularityEvent(
7081 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7082 granularity, start, end);
7083 return true;
7084 }
7085
7086 /**
7087 * Gets the text reported for accessibility purposes.
7088 *
7089 * @return The accessibility text.
7090 *
7091 * @hide
7092 */
7093 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007094 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007095 }
7096
7097 /**
7098 * @hide
7099 */
7100 public int getAccessibilityCursorPosition() {
7101 return mAccessibilityCursorPosition;
7102 }
7103
7104 /**
7105 * @hide
7106 */
7107 public void setAccessibilityCursorPosition(int position) {
7108 mAccessibilityCursorPosition = position;
7109 }
7110
7111 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7112 int fromIndex, int toIndex) {
7113 if (mParent == null) {
7114 return;
7115 }
7116 AccessibilityEvent event = AccessibilityEvent.obtain(
7117 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7118 onInitializeAccessibilityEvent(event);
7119 onPopulateAccessibilityEvent(event);
7120 event.setFromIndex(fromIndex);
7121 event.setToIndex(toIndex);
7122 event.setAction(action);
7123 event.setMovementGranularity(granularity);
7124 mParent.requestSendAccessibilityEvent(this, event);
7125 }
7126
7127 /**
7128 * @hide
7129 */
7130 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7131 switch (granularity) {
7132 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7133 CharSequence text = getIterableTextForAccessibility();
7134 if (text != null && text.length() > 0) {
7135 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007136 CharacterTextSegmentIterator.getInstance(
7137 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007138 iterator.initialize(text.toString());
7139 return iterator;
7140 }
7141 } break;
7142 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7143 CharSequence text = getIterableTextForAccessibility();
7144 if (text != null && text.length() > 0) {
7145 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007146 WordTextSegmentIterator.getInstance(
7147 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007148 iterator.initialize(text.toString());
7149 return iterator;
7150 }
7151 } break;
7152 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7153 CharSequence text = getIterableTextForAccessibility();
7154 if (text != null && text.length() > 0) {
7155 ParagraphTextSegmentIterator iterator =
7156 ParagraphTextSegmentIterator.getInstance();
7157 iterator.initialize(text.toString());
7158 return iterator;
7159 }
7160 } break;
7161 }
7162 return null;
7163 }
7164
Svetoslav Ganov42138042012-03-20 11:51:39 -07007165 /**
Romain Guya440b002010-02-24 15:57:54 -08007166 * @hide
7167 */
7168 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007169 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007170 clearDisplayList();
7171
Romain Guya440b002010-02-24 15:57:54 -08007172 onStartTemporaryDetach();
7173 }
7174
7175 /**
7176 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7178 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007179 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 */
7181 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007182 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007183 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007184 }
7185
7186 /**
7187 * @hide
7188 */
7189 public void dispatchFinishTemporaryDetach() {
7190 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 }
Romain Guy8506ab42009-06-11 17:35:47 -07007192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 /**
7194 * Called after {@link #onStartTemporaryDetach} when the container is done
7195 * changing the view.
7196 */
7197 public void onFinishTemporaryDetach() {
7198 }
Romain Guy8506ab42009-06-11 17:35:47 -07007199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007201 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7202 * for this view's window. Returns null if the view is not currently attached
7203 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007204 * just use the standard high-level event callbacks like
7205 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007206 */
7207 public KeyEvent.DispatcherState getKeyDispatcherState() {
7208 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7209 }
Joe Malin32736f02011-01-19 16:14:20 -08007210
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 * Dispatch a key event before it is processed by any input method
7213 * associated with the view hierarchy. This can be used to intercept
7214 * key events in special situations before the IME consumes them; a
7215 * typical example would be handling the BACK key to update the application's
7216 * UI instead of allowing the IME to see it and close itself.
7217 *
7218 * @param event The key event to be dispatched.
7219 * @return True if the event was handled, false otherwise.
7220 */
7221 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7222 return onKeyPreIme(event.getKeyCode(), event);
7223 }
7224
7225 /**
7226 * Dispatch a key event to the next view on the focus path. This path runs
7227 * from the top of the view tree down to the currently focused view. If this
7228 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7229 * the next node down the focus path. This method also fires any key
7230 * listeners.
7231 *
7232 * @param event The key event to be dispatched.
7233 * @return True if the event was handled, false otherwise.
7234 */
7235 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007236 if (mInputEventConsistencyVerifier != null) {
7237 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239
Jeff Brown21bc5c92011-02-28 18:27:14 -08007240 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007241 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007242 ListenerInfo li = mListenerInfo;
7243 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7244 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 return true;
7246 }
7247
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007248 if (event.dispatch(this, mAttachInfo != null
7249 ? mAttachInfo.mKeyDispatchState : null, this)) {
7250 return true;
7251 }
7252
7253 if (mInputEventConsistencyVerifier != null) {
7254 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7255 }
7256 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 }
7258
7259 /**
7260 * Dispatches a key shortcut event.
7261 *
7262 * @param event The key event to be dispatched.
7263 * @return True if the event was handled by the view, false otherwise.
7264 */
7265 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7266 return onKeyShortcut(event.getKeyCode(), event);
7267 }
7268
7269 /**
7270 * Pass the touch screen motion event down to the target view, or this
7271 * view if it is the target.
7272 *
7273 * @param event The motion event to be dispatched.
7274 * @return True if the event was handled by the view, false otherwise.
7275 */
7276 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007277 if (mInputEventConsistencyVerifier != null) {
7278 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7279 }
7280
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007281 if (onFilterTouchEventForSecurity(event)) {
7282 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007283 ListenerInfo li = mListenerInfo;
7284 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7285 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007286 return true;
7287 }
7288
7289 if (onTouchEvent(event)) {
7290 return true;
7291 }
Jeff Brown85a31762010-09-01 17:01:00 -07007292 }
7293
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007294 if (mInputEventConsistencyVerifier != null) {
7295 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007297 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 }
7299
7300 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007301 * Filter the touch event to apply security policies.
7302 *
7303 * @param event The motion event to be filtered.
7304 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007305 *
Jeff Brown85a31762010-09-01 17:01:00 -07007306 * @see #getFilterTouchesWhenObscured
7307 */
7308 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007309 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007310 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7311 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7312 // Window is obscured, drop this touch.
7313 return false;
7314 }
7315 return true;
7316 }
7317
7318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 * Pass a trackball motion event down to the focused view.
7320 *
7321 * @param event The motion event to be dispatched.
7322 * @return True if the event was handled by the view, false otherwise.
7323 */
7324 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007325 if (mInputEventConsistencyVerifier != null) {
7326 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7327 }
7328
Romain Guy02ccac62011-06-24 13:20:23 -07007329 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 }
7331
7332 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007333 * Dispatch a generic motion event.
7334 * <p>
7335 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7336 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007337 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007338 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007339 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007340 *
7341 * @param event The motion event to be dispatched.
7342 * @return True if the event was handled by the view, false otherwise.
7343 */
7344 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007345 if (mInputEventConsistencyVerifier != null) {
7346 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7347 }
7348
Jeff Browna032cc02011-03-07 16:56:21 -08007349 final int source = event.getSource();
7350 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7351 final int action = event.getAction();
7352 if (action == MotionEvent.ACTION_HOVER_ENTER
7353 || action == MotionEvent.ACTION_HOVER_MOVE
7354 || action == MotionEvent.ACTION_HOVER_EXIT) {
7355 if (dispatchHoverEvent(event)) {
7356 return true;
7357 }
7358 } else if (dispatchGenericPointerEvent(event)) {
7359 return true;
7360 }
7361 } else if (dispatchGenericFocusedEvent(event)) {
7362 return true;
7363 }
7364
Jeff Brown10b62902011-06-20 16:40:37 -07007365 if (dispatchGenericMotionEventInternal(event)) {
7366 return true;
7367 }
7368
7369 if (mInputEventConsistencyVerifier != null) {
7370 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7371 }
7372 return false;
7373 }
7374
7375 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007376 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007377 ListenerInfo li = mListenerInfo;
7378 if (li != null && li.mOnGenericMotionListener != null
7379 && (mViewFlags & ENABLED_MASK) == ENABLED
7380 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007381 return true;
7382 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007383
7384 if (onGenericMotionEvent(event)) {
7385 return true;
7386 }
7387
7388 if (mInputEventConsistencyVerifier != null) {
7389 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7390 }
7391 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007392 }
7393
7394 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007395 * Dispatch a hover event.
7396 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007397 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007398 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007399 * </p>
7400 *
7401 * @param event The motion event to be dispatched.
7402 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007403 */
7404 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007405 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007406 ListenerInfo li = mListenerInfo;
7407 if (li != null && li.mOnHoverListener != null
7408 && (mViewFlags & ENABLED_MASK) == ENABLED
7409 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007410 return true;
7411 }
7412
Jeff Browna032cc02011-03-07 16:56:21 -08007413 return onHoverEvent(event);
7414 }
7415
7416 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007417 * Returns true if the view has a child to which it has recently sent
7418 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7419 * it does not have a hovered child, then it must be the innermost hovered view.
7420 * @hide
7421 */
7422 protected boolean hasHoveredChild() {
7423 return false;
7424 }
7425
7426 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007427 * Dispatch a generic motion event to the view under the first pointer.
7428 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007429 * Do not call this method directly.
7430 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007431 * </p>
7432 *
7433 * @param event The motion event to be dispatched.
7434 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007435 */
7436 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7437 return false;
7438 }
7439
7440 /**
7441 * Dispatch a generic motion event to the currently focused view.
7442 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007443 * Do not call this method directly.
7444 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007445 * </p>
7446 *
7447 * @param event The motion event to be dispatched.
7448 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007449 */
7450 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7451 return false;
7452 }
7453
7454 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007455 * Dispatch a pointer event.
7456 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007457 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7458 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7459 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007460 * and should not be expected to handle other pointing device features.
7461 * </p>
7462 *
7463 * @param event The motion event to be dispatched.
7464 * @return True if the event was handled by the view, false otherwise.
7465 * @hide
7466 */
7467 public final boolean dispatchPointerEvent(MotionEvent event) {
7468 if (event.isTouchEvent()) {
7469 return dispatchTouchEvent(event);
7470 } else {
7471 return dispatchGenericMotionEvent(event);
7472 }
7473 }
7474
7475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 * Called when the window containing this view gains or loses window focus.
7477 * ViewGroups should override to route to their children.
7478 *
7479 * @param hasFocus True if the window containing this view now has focus,
7480 * false otherwise.
7481 */
7482 public void dispatchWindowFocusChanged(boolean hasFocus) {
7483 onWindowFocusChanged(hasFocus);
7484 }
7485
7486 /**
7487 * Called when the window containing this view gains or loses focus. Note
7488 * that this is separate from view focus: to receive key events, both
7489 * your view and its window must have focus. If a window is displayed
7490 * on top of yours that takes input focus, then your own window will lose
7491 * focus but the view focus will remain unchanged.
7492 *
7493 * @param hasWindowFocus True if the window containing this view now has
7494 * focus, false otherwise.
7495 */
7496 public void onWindowFocusChanged(boolean hasWindowFocus) {
7497 InputMethodManager imm = InputMethodManager.peekInstance();
7498 if (!hasWindowFocus) {
7499 if (isPressed()) {
7500 setPressed(false);
7501 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007502 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 imm.focusOut(this);
7504 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007505 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007506 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007507 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007508 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 imm.focusIn(this);
7510 }
7511 refreshDrawableState();
7512 }
7513
7514 /**
7515 * Returns true if this view is in a window that currently has window focus.
7516 * Note that this is not the same as the view itself having focus.
7517 *
7518 * @return True if this view is in a window that currently has window focus.
7519 */
7520 public boolean hasWindowFocus() {
7521 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7522 }
7523
7524 /**
Adam Powell326d8082009-12-09 15:10:07 -08007525 * Dispatch a view visibility change down the view hierarchy.
7526 * ViewGroups should override to route to their children.
7527 * @param changedView The view whose visibility changed. Could be 'this' or
7528 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007529 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7530 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007531 */
7532 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7533 onVisibilityChanged(changedView, visibility);
7534 }
7535
7536 /**
7537 * Called when the visibility of the view or an ancestor of the view is changed.
7538 * @param changedView The view whose visibility changed. Could be 'this' or
7539 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007540 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7541 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007542 */
7543 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007544 if (visibility == VISIBLE) {
7545 if (mAttachInfo != null) {
7546 initialAwakenScrollBars();
7547 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007548 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007549 }
7550 }
Adam Powell326d8082009-12-09 15:10:07 -08007551 }
7552
7553 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007554 * Dispatch a hint about whether this view is displayed. For instance, when
7555 * a View moves out of the screen, it might receives a display hint indicating
7556 * the view is not displayed. Applications should not <em>rely</em> on this hint
7557 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007558 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007559 * @param hint A hint about whether or not this view is displayed:
7560 * {@link #VISIBLE} or {@link #INVISIBLE}.
7561 */
7562 public void dispatchDisplayHint(int hint) {
7563 onDisplayHint(hint);
7564 }
7565
7566 /**
7567 * Gives this view a hint about whether is displayed or not. For instance, when
7568 * a View moves out of the screen, it might receives a display hint indicating
7569 * the view is not displayed. Applications should not <em>rely</em> on this hint
7570 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007571 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007572 * @param hint A hint about whether or not this view is displayed:
7573 * {@link #VISIBLE} or {@link #INVISIBLE}.
7574 */
7575 protected void onDisplayHint(int hint) {
7576 }
7577
7578 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 * Dispatch a window visibility change down the view hierarchy.
7580 * ViewGroups should override to route to their children.
7581 *
7582 * @param visibility The new visibility of the window.
7583 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007584 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 */
7586 public void dispatchWindowVisibilityChanged(int visibility) {
7587 onWindowVisibilityChanged(visibility);
7588 }
7589
7590 /**
7591 * Called when the window containing has change its visibility
7592 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7593 * that this tells you whether or not your window is being made visible
7594 * to the window manager; this does <em>not</em> tell you whether or not
7595 * your window is obscured by other windows on the screen, even if it
7596 * is itself visible.
7597 *
7598 * @param visibility The new visibility of the window.
7599 */
7600 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007601 if (visibility == VISIBLE) {
7602 initialAwakenScrollBars();
7603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 }
7605
7606 /**
7607 * Returns the current visibility of the window this view is attached to
7608 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7609 *
7610 * @return Returns the current visibility of the view's window.
7611 */
7612 public int getWindowVisibility() {
7613 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7614 }
7615
7616 /**
7617 * Retrieve the overall visible display size in which the window this view is
7618 * attached to has been positioned in. This takes into account screen
7619 * decorations above the window, for both cases where the window itself
7620 * is being position inside of them or the window is being placed under
7621 * then and covered insets are used for the window to position its content
7622 * inside. In effect, this tells you the available area where content can
7623 * be placed and remain visible to users.
7624 *
7625 * <p>This function requires an IPC back to the window manager to retrieve
7626 * the requested information, so should not be used in performance critical
7627 * code like drawing.
7628 *
7629 * @param outRect Filled in with the visible display frame. If the view
7630 * is not attached to a window, this is simply the raw display size.
7631 */
7632 public void getWindowVisibleDisplayFrame(Rect outRect) {
7633 if (mAttachInfo != null) {
7634 try {
7635 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7636 } catch (RemoteException e) {
7637 return;
7638 }
7639 // XXX This is really broken, and probably all needs to be done
7640 // in the window manager, and we need to know more about whether
7641 // we want the area behind or in front of the IME.
7642 final Rect insets = mAttachInfo.mVisibleInsets;
7643 outRect.left += insets.left;
7644 outRect.top += insets.top;
7645 outRect.right -= insets.right;
7646 outRect.bottom -= insets.bottom;
7647 return;
7648 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007649 // The view is not attached to a display so we don't have a context.
7650 // Make a best guess about the display size.
7651 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007652 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 }
7654
7655 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007656 * Dispatch a notification about a resource configuration change down
7657 * the view hierarchy.
7658 * ViewGroups should override to route to their children.
7659 *
7660 * @param newConfig The new resource configuration.
7661 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007662 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007663 */
7664 public void dispatchConfigurationChanged(Configuration newConfig) {
7665 onConfigurationChanged(newConfig);
7666 }
7667
7668 /**
7669 * Called when the current configuration of the resources being used
7670 * by the application have changed. You can use this to decide when
7671 * to reload resources that can changed based on orientation and other
7672 * configuration characterstics. You only need to use this if you are
7673 * not relying on the normal {@link android.app.Activity} mechanism of
7674 * recreating the activity instance upon a configuration change.
7675 *
7676 * @param newConfig The new resource configuration.
7677 */
7678 protected void onConfigurationChanged(Configuration newConfig) {
7679 }
7680
7681 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 * Private function to aggregate all per-view attributes in to the view
7683 * root.
7684 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007685 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7686 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 }
7688
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007689 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7690 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007691 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007692 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007693 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007694 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007695 ListenerInfo li = mListenerInfo;
7696 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007697 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
7700 }
7701
7702 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007703 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007704 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007705 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7706 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 ai.mRecomputeGlobalAttributes = true;
7708 }
7709 }
7710 }
7711
7712 /**
7713 * Returns whether the device is currently in touch mode. Touch mode is entered
7714 * once the user begins interacting with the device by touch, and affects various
7715 * things like whether focus is always visible to the user.
7716 *
7717 * @return Whether the device is in touch mode.
7718 */
7719 @ViewDebug.ExportedProperty
7720 public boolean isInTouchMode() {
7721 if (mAttachInfo != null) {
7722 return mAttachInfo.mInTouchMode;
7723 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007724 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 }
7726 }
7727
7728 /**
7729 * Returns the context the view is running in, through which it can
7730 * access the current theme, resources, etc.
7731 *
7732 * @return The view's Context.
7733 */
7734 @ViewDebug.CapturedViewProperty
7735 public final Context getContext() {
7736 return mContext;
7737 }
7738
7739 /**
7740 * Handle a key event before it is processed by any input method
7741 * associated with the view hierarchy. This can be used to intercept
7742 * key events in special situations before the IME consumes them; a
7743 * typical example would be handling the BACK key to update the application's
7744 * UI instead of allowing the IME to see it and close itself.
7745 *
7746 * @param keyCode The value in event.getKeyCode().
7747 * @param event Description of the key event.
7748 * @return If you handled the event, return true. If you want to allow the
7749 * event to be handled by the next receiver, return false.
7750 */
7751 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7752 return false;
7753 }
7754
7755 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007756 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7757 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7759 * is released, if the view is enabled and clickable.
7760 *
Jean Chalard405bc512012-05-29 19:12:34 +09007761 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7762 * although some may elect to do so in some situations. Do not rely on this to
7763 * catch software key presses.
7764 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 * @param keyCode A key code that represents the button pressed, from
7766 * {@link android.view.KeyEvent}.
7767 * @param event The KeyEvent object that defines the button action.
7768 */
7769 public boolean onKeyDown(int keyCode, KeyEvent event) {
7770 boolean result = false;
7771
7772 switch (keyCode) {
7773 case KeyEvent.KEYCODE_DPAD_CENTER:
7774 case KeyEvent.KEYCODE_ENTER: {
7775 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7776 return true;
7777 }
7778 // Long clickable items don't necessarily have to be clickable
7779 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7780 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7781 (event.getRepeatCount() == 0)) {
7782 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007783 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784 return true;
7785 }
7786 break;
7787 }
7788 }
7789 return result;
7790 }
7791
7792 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007793 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7794 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7795 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007796 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7797 * although some may elect to do so in some situations. Do not rely on this to
7798 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007799 */
7800 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7801 return false;
7802 }
7803
7804 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007805 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7806 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7808 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007809 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7810 * although some may elect to do so in some situations. Do not rely on this to
7811 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 *
7813 * @param keyCode A key code that represents the button pressed, from
7814 * {@link android.view.KeyEvent}.
7815 * @param event The KeyEvent object that defines the button action.
7816 */
7817 public boolean onKeyUp(int keyCode, KeyEvent event) {
7818 boolean result = false;
7819
7820 switch (keyCode) {
7821 case KeyEvent.KEYCODE_DPAD_CENTER:
7822 case KeyEvent.KEYCODE_ENTER: {
7823 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7824 return true;
7825 }
7826 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7827 setPressed(false);
7828
7829 if (!mHasPerformedLongPress) {
7830 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007831 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832
7833 result = performClick();
7834 }
7835 }
7836 break;
7837 }
7838 }
7839 return result;
7840 }
7841
7842 /**
7843 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7844 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7845 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007846 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7847 * although some may elect to do so in some situations. Do not rely on this to
7848 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 *
7850 * @param keyCode A key code that represents the button pressed, from
7851 * {@link android.view.KeyEvent}.
7852 * @param repeatCount The number of times the action was made.
7853 * @param event The KeyEvent object that defines the button action.
7854 */
7855 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7856 return false;
7857 }
7858
7859 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007860 * Called on the focused view when a key shortcut event is not handled.
7861 * Override this method to implement local key shortcuts for the View.
7862 * Key shortcuts can also be implemented by setting the
7863 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 *
7865 * @param keyCode The value in event.getKeyCode().
7866 * @param event Description of the key event.
7867 * @return If you handled the event, return true. If you want to allow the
7868 * event to be handled by the next receiver, return false.
7869 */
7870 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7871 return false;
7872 }
7873
7874 /**
7875 * Check whether the called view is a text editor, in which case it
7876 * would make sense to automatically display a soft input window for
7877 * it. Subclasses should override this if they implement
7878 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007879 * a call on that method would return a non-null InputConnection, and
7880 * they are really a first-class editor that the user would normally
7881 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007882 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007883 * <p>The default implementation always returns false. This does
7884 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7885 * will not be called or the user can not otherwise perform edits on your
7886 * view; it is just a hint to the system that this is not the primary
7887 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007888 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 * @return Returns true if this view is a text editor, else false.
7890 */
7891 public boolean onCheckIsTextEditor() {
7892 return false;
7893 }
Romain Guy8506ab42009-06-11 17:35:47 -07007894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 /**
7896 * Create a new InputConnection for an InputMethod to interact
7897 * with the view. The default implementation returns null, since it doesn't
7898 * support input methods. You can override this to implement such support.
7899 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 * <p>When implementing this, you probably also want to implement
7902 * {@link #onCheckIsTextEditor()} to indicate you will return a
7903 * non-null InputConnection.
7904 *
7905 * @param outAttrs Fill in with attribute information about the connection.
7906 */
7907 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7908 return null;
7909 }
7910
7911 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007912 * Called by the {@link android.view.inputmethod.InputMethodManager}
7913 * when a view who is not the current
7914 * input connection target is trying to make a call on the manager. The
7915 * default implementation returns false; you can override this to return
7916 * true for certain views if you are performing InputConnection proxying
7917 * to them.
7918 * @param view The View that is making the InputMethodManager call.
7919 * @return Return true to allow the call, false to reject.
7920 */
7921 public boolean checkInputConnectionProxy(View view) {
7922 return false;
7923 }
Romain Guy8506ab42009-06-11 17:35:47 -07007924
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007925 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007926 * Show the context menu for this view. It is not safe to hold on to the
7927 * menu after returning from this method.
7928 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007929 * You should normally not overload this method. Overload
7930 * {@link #onCreateContextMenu(ContextMenu)} or define an
7931 * {@link OnCreateContextMenuListener} to add items to the context menu.
7932 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007933 * @param menu The context menu to populate
7934 */
7935 public void createContextMenu(ContextMenu menu) {
7936 ContextMenuInfo menuInfo = getContextMenuInfo();
7937
7938 // Sets the current menu info so all items added to menu will have
7939 // my extra info set.
7940 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7941
7942 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007943 ListenerInfo li = mListenerInfo;
7944 if (li != null && li.mOnCreateContextMenuListener != null) {
7945 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 }
7947
7948 // Clear the extra information so subsequent items that aren't mine don't
7949 // have my extra info.
7950 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7951
7952 if (mParent != null) {
7953 mParent.createContextMenu(menu);
7954 }
7955 }
7956
7957 /**
7958 * Views should implement this if they have extra information to associate
7959 * with the context menu. The return result is supplied as a parameter to
7960 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7961 * callback.
7962 *
7963 * @return Extra information about the item for which the context menu
7964 * should be shown. This information will vary across different
7965 * subclasses of View.
7966 */
7967 protected ContextMenuInfo getContextMenuInfo() {
7968 return null;
7969 }
7970
7971 /**
7972 * Views should implement this if the view itself is going to add items to
7973 * the context menu.
7974 *
7975 * @param menu the context menu to populate
7976 */
7977 protected void onCreateContextMenu(ContextMenu menu) {
7978 }
7979
7980 /**
7981 * Implement this method to handle trackball motion events. The
7982 * <em>relative</em> movement of the trackball since the last event
7983 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7984 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7985 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7986 * they will often be fractional values, representing the more fine-grained
7987 * movement information available from a trackball).
7988 *
7989 * @param event The motion event.
7990 * @return True if the event was handled, false otherwise.
7991 */
7992 public boolean onTrackballEvent(MotionEvent event) {
7993 return false;
7994 }
7995
7996 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007997 * Implement this method to handle generic motion events.
7998 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007999 * Generic motion events describe joystick movements, mouse hovers, track pad
8000 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08008001 * {@link MotionEvent#getSource() source} of the motion event specifies
8002 * the class of input that was received. Implementations of this method
8003 * must examine the bits in the source before processing the event.
8004 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08008005 * </p><p>
8006 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
8007 * are delivered to the view under the pointer. All other generic motion events are
8008 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08008009 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07008010 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08008011 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08008012 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
8013 * // process the joystick movement...
8014 * return true;
8015 * }
8016 * }
8017 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
8018 * switch (event.getAction()) {
8019 * case MotionEvent.ACTION_HOVER_MOVE:
8020 * // process the mouse hover movement...
8021 * return true;
8022 * case MotionEvent.ACTION_SCROLL:
8023 * // process the scroll wheel movement...
8024 * return true;
8025 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08008026 * }
8027 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07008028 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08008029 *
8030 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08008031 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08008032 */
8033 public boolean onGenericMotionEvent(MotionEvent event) {
8034 return false;
8035 }
8036
8037 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008038 * Implement this method to handle hover events.
8039 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07008040 * This method is called whenever a pointer is hovering into, over, or out of the
8041 * bounds of a view and the view is not currently being touched.
8042 * Hover events are represented as pointer events with action
8043 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
8044 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
8045 * </p>
8046 * <ul>
8047 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8048 * when the pointer enters the bounds of the view.</li>
8049 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8050 * when the pointer has already entered the bounds of the view and has moved.</li>
8051 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8052 * when the pointer has exited the bounds of the view or when the pointer is
8053 * about to go down due to a button click, tap, or similar user action that
8054 * causes the view to be touched.</li>
8055 * </ul>
8056 * <p>
8057 * The view should implement this method to return true to indicate that it is
8058 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008059 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008060 * The default implementation calls {@link #setHovered} to update the hovered state
8061 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008062 * is enabled and is clickable. The default implementation also sends hover
8063 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008064 * </p>
8065 *
8066 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008067 * @return True if the view handled the hover event.
8068 *
8069 * @see #isHovered
8070 * @see #setHovered
8071 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008072 */
8073 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008074 // The root view may receive hover (or touch) events that are outside the bounds of
8075 // the window. This code ensures that we only send accessibility events for
8076 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008077 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008078 if (!mSendingHoverAccessibilityEvents) {
8079 if ((action == MotionEvent.ACTION_HOVER_ENTER
8080 || action == MotionEvent.ACTION_HOVER_MOVE)
8081 && !hasHoveredChild()
8082 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008083 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008084 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008085 }
8086 } else {
8087 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008088 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008089 && !pointInView(event.getX(), event.getY()))) {
8090 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008091 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008092 // If the window does not have input focus we take away accessibility
8093 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008094 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008095 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008096 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008097 }
Jeff Browna1b24182011-07-28 13:38:24 -07008098 }
8099
Jeff Brown87b7f802011-06-21 18:35:45 -07008100 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008101 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008102 case MotionEvent.ACTION_HOVER_ENTER:
8103 setHovered(true);
8104 break;
8105 case MotionEvent.ACTION_HOVER_EXIT:
8106 setHovered(false);
8107 break;
8108 }
Jeff Browna1b24182011-07-28 13:38:24 -07008109
8110 // Dispatch the event to onGenericMotionEvent before returning true.
8111 // This is to provide compatibility with existing applications that
8112 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8113 // break because of the new default handling for hoverable views
8114 // in onHoverEvent.
8115 // Note that onGenericMotionEvent will be called by default when
8116 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8117 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008118 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008119 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008120
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008121 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008122 }
8123
8124 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008125 * Returns true if the view should handle {@link #onHoverEvent}
8126 * by calling {@link #setHovered} to change its hovered state.
8127 *
8128 * @return True if the view is hoverable.
8129 */
8130 private boolean isHoverable() {
8131 final int viewFlags = mViewFlags;
8132 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8133 return false;
8134 }
8135
8136 return (viewFlags & CLICKABLE) == CLICKABLE
8137 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8138 }
8139
8140 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008141 * Returns true if the view is currently hovered.
8142 *
8143 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008144 *
8145 * @see #setHovered
8146 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008147 */
Jeff Brown10b62902011-06-20 16:40:37 -07008148 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008149 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008150 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008151 }
8152
8153 /**
8154 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008155 * <p>
8156 * Calling this method also changes the drawable state of the view. This
8157 * enables the view to react to hover by using different drawable resources
8158 * to change its appearance.
8159 * </p><p>
8160 * The {@link #onHoverChanged} method is called when the hovered state changes.
8161 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008162 *
8163 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008164 *
8165 * @see #isHovered
8166 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008167 */
8168 public void setHovered(boolean hovered) {
8169 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008170 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8171 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008172 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008173 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008174 }
8175 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008176 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8177 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008178 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008179 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008180 }
8181 }
8182 }
8183
8184 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008185 * Implement this method to handle hover state changes.
8186 * <p>
8187 * This method is called whenever the hover state changes as a result of a
8188 * call to {@link #setHovered}.
8189 * </p>
8190 *
8191 * @param hovered The current hover state, as returned by {@link #isHovered}.
8192 *
8193 * @see #isHovered
8194 * @see #setHovered
8195 */
8196 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008197 }
8198
8199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 * Implement this method to handle touch screen motion events.
8201 *
8202 * @param event The motion event.
8203 * @return True if the event was handled, false otherwise.
8204 */
8205 public boolean onTouchEvent(MotionEvent event) {
8206 final int viewFlags = mViewFlags;
8207
8208 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008209 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008210 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 // A disabled view that is clickable still consumes the touch
8213 // events, it just doesn't respond to them.
8214 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8215 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8216 }
8217
8218 if (mTouchDelegate != null) {
8219 if (mTouchDelegate.onTouchEvent(event)) {
8220 return true;
8221 }
8222 }
8223
8224 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8225 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8226 switch (event.getAction()) {
8227 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008228 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8229 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 // take focus if we don't have it already and we should in
8231 // touch mode.
8232 boolean focusTaken = false;
8233 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8234 focusTaken = requestFocus();
8235 }
8236
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008237 if (prepressed) {
8238 // The button is being released before we actually
8239 // showed it as pressed. Make it show the pressed
8240 // state now (before scheduling the click) to ensure
8241 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008242 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008243 }
Joe Malin32736f02011-01-19 16:14:20 -08008244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 if (!mHasPerformedLongPress) {
8246 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008247 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248
8249 // Only perform take click actions if we were in the pressed state
8250 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008251 // Use a Runnable and post this rather than calling
8252 // performClick directly. This lets other visual state
8253 // of the view update before click actions start.
8254 if (mPerformClick == null) {
8255 mPerformClick = new PerformClick();
8256 }
8257 if (!post(mPerformClick)) {
8258 performClick();
8259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 }
8261 }
8262
8263 if (mUnsetPressedState == null) {
8264 mUnsetPressedState = new UnsetPressedState();
8265 }
8266
Adam Powelle14579b2009-12-16 18:39:52 -08008267 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008268 postDelayed(mUnsetPressedState,
8269 ViewConfiguration.getPressedStateDuration());
8270 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 // If the post failed, unpress right now
8272 mUnsetPressedState.run();
8273 }
Adam Powelle14579b2009-12-16 18:39:52 -08008274 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 }
8276 break;
8277
8278 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008279 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008280
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008281 if (performButtonActionOnTouchDown(event)) {
8282 break;
8283 }
8284
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008285 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008286 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008287
8288 // For views inside a scrolling container, delay the pressed feedback for
8289 // a short period in case this is a scroll.
8290 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008291 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008292 if (mPendingCheckForTap == null) {
8293 mPendingCheckForTap = new CheckForTap();
8294 }
8295 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8296 } else {
8297 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008298 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008299 checkForLongClick(0);
8300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008301 break;
8302
8303 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008304 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008305 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008306 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 break;
8308
8309 case MotionEvent.ACTION_MOVE:
8310 final int x = (int) event.getX();
8311 final int y = (int) event.getY();
8312
8313 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008314 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008315 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008316 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008317 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008318 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008319 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320
Adam Powell4d6f0662012-02-21 15:11:11 -08008321 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 }
8324 break;
8325 }
8326 return true;
8327 }
8328
8329 return false;
8330 }
8331
8332 /**
Adam Powell10298662011-08-14 18:26:30 -07008333 * @hide
8334 */
8335 public boolean isInScrollingContainer() {
8336 ViewParent p = getParent();
8337 while (p != null && p instanceof ViewGroup) {
8338 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8339 return true;
8340 }
8341 p = p.getParent();
8342 }
8343 return false;
8344 }
8345
8346 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008347 * Remove the longpress detection timer.
8348 */
8349 private void removeLongPressCallback() {
8350 if (mPendingCheckForLongPress != null) {
8351 removeCallbacks(mPendingCheckForLongPress);
8352 }
8353 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008354
8355 /**
8356 * Remove the pending click action
8357 */
8358 private void removePerformClickCallback() {
8359 if (mPerformClick != null) {
8360 removeCallbacks(mPerformClick);
8361 }
8362 }
8363
Adam Powelle14579b2009-12-16 18:39:52 -08008364 /**
Romain Guya440b002010-02-24 15:57:54 -08008365 * Remove the prepress detection timer.
8366 */
8367 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008368 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008369 setPressed(false);
8370 removeCallbacks(mUnsetPressedState);
8371 }
8372 }
8373
8374 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008375 * Remove the tap detection timer.
8376 */
8377 private void removeTapCallback() {
8378 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008379 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008380 removeCallbacks(mPendingCheckForTap);
8381 }
8382 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008383
8384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 * Cancels a pending long press. Your subclass can use this if you
8386 * want the context menu to come up if the user presses and holds
8387 * at the same place, but you don't want it to come up if they press
8388 * and then move around enough to cause scrolling.
8389 */
8390 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008391 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008392
8393 /*
8394 * The prepressed state handled by the tap callback is a display
8395 * construct, but the tap callback will post a long press callback
8396 * less its own timeout. Remove it here.
8397 */
8398 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 }
8400
8401 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008402 * Remove the pending callback for sending a
8403 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8404 */
8405 private void removeSendViewScrolledAccessibilityEventCallback() {
8406 if (mSendViewScrolledAccessibilityEvent != null) {
8407 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008408 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008409 }
8410 }
8411
8412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 * Sets the TouchDelegate for this View.
8414 */
8415 public void setTouchDelegate(TouchDelegate delegate) {
8416 mTouchDelegate = delegate;
8417 }
8418
8419 /**
8420 * Gets the TouchDelegate for this View.
8421 */
8422 public TouchDelegate getTouchDelegate() {
8423 return mTouchDelegate;
8424 }
8425
8426 /**
8427 * Set flags controlling behavior of this view.
8428 *
8429 * @param flags Constant indicating the value which should be set
8430 * @param mask Constant indicating the bit range that should be changed
8431 */
8432 void setFlags(int flags, int mask) {
8433 int old = mViewFlags;
8434 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8435
8436 int changed = mViewFlags ^ old;
8437 if (changed == 0) {
8438 return;
8439 }
8440 int privateFlags = mPrivateFlags;
8441
8442 /* Check if the FOCUSABLE bit has changed */
8443 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008444 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008446 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 /* Give up focus if we are no longer focusable */
8448 clearFocus();
8449 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008450 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 /*
8452 * Tell the view system that we are now available to take focus
8453 * if no one else already has it.
8454 */
8455 if (mParent != null) mParent.focusableViewAvailable(this);
8456 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008457 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8458 notifyAccessibilityStateChanged();
8459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 }
8461
8462 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8463 if ((changed & VISIBILITY_MASK) != 0) {
8464 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008465 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008466 * it was not visible. Marking it drawn ensures that the invalidation will
8467 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008469 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008470 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471
8472 needGlobalAttributesUpdate(true);
8473
8474 // a view becoming visible is worth notifying the parent
8475 // about in case nothing has focus. even if this specific view
8476 // isn't focusable, it may contain something that is, so let
8477 // the root view try to give this focus if nothing else does.
8478 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8479 mParent.focusableViewAvailable(this);
8480 }
8481 }
8482 }
8483
8484 /* Check if the GONE bit has changed */
8485 if ((changed & GONE) != 0) {
8486 needGlobalAttributesUpdate(false);
8487 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488
Romain Guyecd80ee2009-12-03 17:13:02 -08008489 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8490 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008491 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008492 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008493 if (mParent instanceof View) {
8494 // GONE views noop invalidation, so invalidate the parent
8495 ((View) mParent).invalidate(true);
8496 }
8497 // Mark the view drawn to ensure that it gets invalidated properly the next
8498 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008499 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 }
8501 if (mAttachInfo != null) {
8502 mAttachInfo.mViewVisibilityChanged = true;
8503 }
8504 }
8505
8506 /* Check if the VISIBLE bit has changed */
8507 if ((changed & INVISIBLE) != 0) {
8508 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008509 /*
8510 * If this view is becoming invisible, set the DRAWN flag so that
8511 * the next invalidate() will not be skipped.
8512 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008513 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514
8515 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008516 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 if (getRootView() != this) {
8518 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008519 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 }
8521 }
8522 if (mAttachInfo != null) {
8523 mAttachInfo.mViewVisibilityChanged = true;
8524 }
8525 }
8526
Adam Powell326d8082009-12-09 15:10:07 -08008527 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008528 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008529 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8530 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008531 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008532 } else if (mParent != null) {
8533 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008534 }
Adam Powell326d8082009-12-09 15:10:07 -08008535 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8536 }
8537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008538 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8539 destroyDrawingCache();
8540 }
8541
8542 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8543 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008544 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008545 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 }
8547
8548 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8549 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008550 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 }
8552
8553 if ((changed & DRAW_MASK) != 0) {
8554 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008555 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008556 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8557 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008559 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008560 }
8561 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008562 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008563 }
8564 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008565 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 }
8567
8568 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008569 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 mParent.recomputeViewAttributes(this);
8571 }
8572 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008573
8574 if (AccessibilityManager.getInstance(mContext).isEnabled()
8575 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8576 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8577 notifyAccessibilityStateChanged();
8578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580
8581 /**
8582 * Change the view's z order in the tree, so it's on top of other sibling
8583 * views
8584 */
8585 public void bringToFront() {
8586 if (mParent != null) {
8587 mParent.bringChildToFront(this);
8588 }
8589 }
8590
8591 /**
8592 * This is called in response to an internal scroll in this view (i.e., the
8593 * view scrolled its own contents). This is typically as a result of
8594 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8595 * called.
8596 *
8597 * @param l Current horizontal scroll origin.
8598 * @param t Current vertical scroll origin.
8599 * @param oldl Previous horizontal scroll origin.
8600 * @param oldt Previous vertical scroll origin.
8601 */
8602 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008603 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8604 postSendViewScrolledAccessibilityEventCallback();
8605 }
8606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 mBackgroundSizeChanged = true;
8608
8609 final AttachInfo ai = mAttachInfo;
8610 if (ai != null) {
8611 ai.mViewScrollChanged = true;
8612 }
8613 }
8614
8615 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008616 * Interface definition for a callback to be invoked when the layout bounds of a view
8617 * changes due to layout processing.
8618 */
8619 public interface OnLayoutChangeListener {
8620 /**
8621 * Called when the focus state of a view has changed.
8622 *
8623 * @param v The view whose state has changed.
8624 * @param left The new value of the view's left property.
8625 * @param top The new value of the view's top property.
8626 * @param right The new value of the view's right property.
8627 * @param bottom The new value of the view's bottom property.
8628 * @param oldLeft The previous value of the view's left property.
8629 * @param oldTop The previous value of the view's top property.
8630 * @param oldRight The previous value of the view's right property.
8631 * @param oldBottom The previous value of the view's bottom property.
8632 */
8633 void onLayoutChange(View v, int left, int top, int right, int bottom,
8634 int oldLeft, int oldTop, int oldRight, int oldBottom);
8635 }
8636
8637 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 * This is called during layout when the size of this view has changed. If
8639 * you were just added to the view hierarchy, you're called with the old
8640 * values of 0.
8641 *
8642 * @param w Current width of this view.
8643 * @param h Current height of this view.
8644 * @param oldw Old width of this view.
8645 * @param oldh Old height of this view.
8646 */
8647 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8648 }
8649
8650 /**
8651 * Called by draw to draw the child views. This may be overridden
8652 * by derived classes to gain control just before its children are drawn
8653 * (but after its own view has been drawn).
8654 * @param canvas the canvas on which to draw the view
8655 */
8656 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 }
8659
8660 /**
8661 * Gets the parent of this view. Note that the parent is a
8662 * ViewParent and not necessarily a View.
8663 *
8664 * @return Parent of this view.
8665 */
8666 public final ViewParent getParent() {
8667 return mParent;
8668 }
8669
8670 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008671 * Set the horizontal scrolled position of your view. This will cause a call to
8672 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8673 * invalidated.
8674 * @param value the x position to scroll to
8675 */
8676 public void setScrollX(int value) {
8677 scrollTo(value, mScrollY);
8678 }
8679
8680 /**
8681 * Set the vertical scrolled position of your view. This will cause a call to
8682 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8683 * invalidated.
8684 * @param value the y position to scroll to
8685 */
8686 public void setScrollY(int value) {
8687 scrollTo(mScrollX, value);
8688 }
8689
8690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 * Return the scrolled left position of this view. This is the left edge of
8692 * the displayed part of your view. You do not need to draw any pixels
8693 * farther left, since those are outside of the frame of your view on
8694 * screen.
8695 *
8696 * @return The left edge of the displayed part of your view, in pixels.
8697 */
8698 public final int getScrollX() {
8699 return mScrollX;
8700 }
8701
8702 /**
8703 * Return the scrolled top position of this view. This is the top edge of
8704 * the displayed part of your view. You do not need to draw any pixels above
8705 * it, since those are outside of the frame of your view on screen.
8706 *
8707 * @return The top edge of the displayed part of your view, in pixels.
8708 */
8709 public final int getScrollY() {
8710 return mScrollY;
8711 }
8712
8713 /**
8714 * Return the width of the your view.
8715 *
8716 * @return The width of your view, in pixels.
8717 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008718 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 public final int getWidth() {
8720 return mRight - mLeft;
8721 }
8722
8723 /**
8724 * Return the height of your view.
8725 *
8726 * @return The height of your view, in pixels.
8727 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008728 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 public final int getHeight() {
8730 return mBottom - mTop;
8731 }
8732
8733 /**
8734 * Return the visible drawing bounds of your view. Fills in the output
8735 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008736 * getWidth(), and getHeight(). These bounds do not account for any
8737 * transformation properties currently set on the view, such as
8738 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 *
8740 * @param outRect The (scrolled) drawing bounds of the view.
8741 */
8742 public void getDrawingRect(Rect outRect) {
8743 outRect.left = mScrollX;
8744 outRect.top = mScrollY;
8745 outRect.right = mScrollX + (mRight - mLeft);
8746 outRect.bottom = mScrollY + (mBottom - mTop);
8747 }
8748
8749 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008750 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8751 * raw width component (that is the result is masked by
8752 * {@link #MEASURED_SIZE_MASK}).
8753 *
8754 * @return The raw measured width of this view.
8755 */
8756 public final int getMeasuredWidth() {
8757 return mMeasuredWidth & MEASURED_SIZE_MASK;
8758 }
8759
8760 /**
8761 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008762 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008763 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 * This should be used during measurement and layout calculations only. Use
8765 * {@link #getWidth()} to see how wide a view is after layout.
8766 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008767 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008769 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 return mMeasuredWidth;
8771 }
8772
8773 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008774 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8775 * raw width component (that is the result is masked by
8776 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008778 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 */
8780 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008781 return mMeasuredHeight & MEASURED_SIZE_MASK;
8782 }
8783
8784 /**
8785 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008786 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008787 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8788 * This should be used during measurement and layout calculations only. Use
8789 * {@link #getHeight()} to see how wide a view is after layout.
8790 *
8791 * @return The measured width of this view as a bit mask.
8792 */
8793 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008794 return mMeasuredHeight;
8795 }
8796
8797 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008798 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8799 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8800 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8801 * and the height component is at the shifted bits
8802 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8803 */
8804 public final int getMeasuredState() {
8805 return (mMeasuredWidth&MEASURED_STATE_MASK)
8806 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8807 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8808 }
8809
8810 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008811 * The transform matrix of this view, which is calculated based on the current
8812 * roation, scale, and pivot properties.
8813 *
8814 * @see #getRotation()
8815 * @see #getScaleX()
8816 * @see #getScaleY()
8817 * @see #getPivotX()
8818 * @see #getPivotY()
8819 * @return The current transform matrix for the view
8820 */
8821 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008822 if (mTransformationInfo != null) {
8823 updateMatrix();
8824 return mTransformationInfo.mMatrix;
8825 }
8826 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008827 }
8828
8829 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008830 * Utility function to determine if the value is far enough away from zero to be
8831 * considered non-zero.
8832 * @param value A floating point value to check for zero-ness
8833 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8834 */
8835 private static boolean nonzero(float value) {
8836 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8837 }
8838
8839 /**
Jeff Brown86671742010-09-30 20:00:15 -07008840 * Returns true if the transform matrix is the identity matrix.
8841 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008842 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008843 * @return True if the transform matrix is the identity matrix, false otherwise.
8844 */
Jeff Brown86671742010-09-30 20:00:15 -07008845 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008846 if (mTransformationInfo != null) {
8847 updateMatrix();
8848 return mTransformationInfo.mMatrixIsIdentity;
8849 }
8850 return true;
8851 }
8852
8853 void ensureTransformationInfo() {
8854 if (mTransformationInfo == null) {
8855 mTransformationInfo = new TransformationInfo();
8856 }
Jeff Brown86671742010-09-30 20:00:15 -07008857 }
8858
8859 /**
8860 * Recomputes the transform matrix if necessary.
8861 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008862 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008863 final TransformationInfo info = mTransformationInfo;
8864 if (info == null) {
8865 return;
8866 }
8867 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008868 // transform-related properties have changed since the last time someone
8869 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008870
8871 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008872 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008873 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8874 info.mPrevWidth = mRight - mLeft;
8875 info.mPrevHeight = mBottom - mTop;
8876 info.mPivotX = info.mPrevWidth / 2f;
8877 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008878 }
8879 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008880 info.mMatrix.reset();
8881 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8882 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8883 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8884 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008885 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008886 if (info.mCamera == null) {
8887 info.mCamera = new Camera();
8888 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008889 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008890 info.mCamera.save();
8891 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8892 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8893 info.mCamera.getMatrix(info.matrix3D);
8894 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8895 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8896 info.mPivotY + info.mTranslationY);
8897 info.mMatrix.postConcat(info.matrix3D);
8898 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008899 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008900 info.mMatrixDirty = false;
8901 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8902 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008903 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008904 }
8905
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008906 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008907 * Utility method to retrieve the inverse of the current mMatrix property.
8908 * We cache the matrix to avoid recalculating it when transform properties
8909 * have not changed.
8910 *
8911 * @return The inverse of the current matrix of this view.
8912 */
Jeff Brown86671742010-09-30 20:00:15 -07008913 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008914 final TransformationInfo info = mTransformationInfo;
8915 if (info != null) {
8916 updateMatrix();
8917 if (info.mInverseMatrixDirty) {
8918 if (info.mInverseMatrix == null) {
8919 info.mInverseMatrix = new Matrix();
8920 }
8921 info.mMatrix.invert(info.mInverseMatrix);
8922 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008923 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008924 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008925 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008926 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008927 }
8928
8929 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008930 * Gets the distance along the Z axis from the camera to this view.
8931 *
8932 * @see #setCameraDistance(float)
8933 *
8934 * @return The distance along the Z axis.
8935 */
8936 public float getCameraDistance() {
8937 ensureTransformationInfo();
8938 final float dpi = mResources.getDisplayMetrics().densityDpi;
8939 final TransformationInfo info = mTransformationInfo;
8940 if (info.mCamera == null) {
8941 info.mCamera = new Camera();
8942 info.matrix3D = new Matrix();
8943 }
8944 return -(info.mCamera.getLocationZ() * dpi);
8945 }
8946
8947 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008948 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8949 * views are drawn) from the camera to this view. The camera's distance
8950 * affects 3D transformations, for instance rotations around the X and Y
8951 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008952 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008953 * use a camera distance that's greater than the height (X axis rotation) or
8954 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008955 *
Romain Guya5364ee2011-02-24 14:46:04 -08008956 * <p>The distance of the camera from the view plane can have an affect on the
8957 * perspective distortion of the view when it is rotated around the x or y axis.
8958 * For example, a large distance will result in a large viewing angle, and there
8959 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008960 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008961 * also result in some drawing artifacts if the rotated view ends up partially
8962 * behind the camera (which is why the recommendation is to use a distance at
8963 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008964 *
Romain Guya5364ee2011-02-24 14:46:04 -08008965 * <p>The distance is expressed in "depth pixels." The default distance depends
8966 * on the screen density. For instance, on a medium density display, the
8967 * default distance is 1280. On a high density display, the default distance
8968 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008969 *
Romain Guya5364ee2011-02-24 14:46:04 -08008970 * <p>If you want to specify a distance that leads to visually consistent
8971 * results across various densities, use the following formula:</p>
8972 * <pre>
8973 * float scale = context.getResources().getDisplayMetrics().density;
8974 * view.setCameraDistance(distance * scale);
8975 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008976 *
Romain Guya5364ee2011-02-24 14:46:04 -08008977 * <p>The density scale factor of a high density display is 1.5,
8978 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008979 *
Romain Guya5364ee2011-02-24 14:46:04 -08008980 * @param distance The distance in "depth pixels", if negative the opposite
8981 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008982 *
8983 * @see #setRotationX(float)
8984 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008985 */
8986 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008987 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008988
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008989 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008990 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 final TransformationInfo info = mTransformationInfo;
8992 if (info.mCamera == null) {
8993 info.mCamera = new Camera();
8994 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008995 }
8996
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008997 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8998 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008999
Chet Haase9d1992d2012-03-13 11:03:25 -07009000 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009001 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07009002 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08009003 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009004 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009005 // View was rejected last time it was drawn by its parent; this may have changed
9006 invalidateParentIfNeeded();
9007 }
Romain Guya5364ee2011-02-24 14:46:04 -08009008 }
9009
9010 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009011 * The degrees that the view is rotated around the pivot point.
9012 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009013 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07009014 * @see #getPivotX()
9015 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009016 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 * @return The degrees of rotation.
9018 */
Chet Haasea5531132012-02-02 13:41:44 -08009019 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009020 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009021 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009022 }
9023
9024 /**
Chet Haase897247b2010-09-09 14:54:47 -07009025 * Sets the degrees that the view is rotated around the pivot point. Increasing values
9026 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07009027 *
9028 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009029 *
9030 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07009031 * @see #getPivotX()
9032 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009033 * @see #setRotationX(float)
9034 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08009035 *
9036 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07009037 */
9038 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009039 ensureTransformationInfo();
9040 final TransformationInfo info = mTransformationInfo;
9041 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009042 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009043 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009044 info.mRotation = rotation;
9045 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009046 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009047 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009048 mDisplayList.setRotation(rotation);
9049 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009050 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009051 // View was rejected last time it was drawn by its parent; this may have changed
9052 invalidateParentIfNeeded();
9053 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009054 }
9055 }
9056
9057 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009058 * The degrees that the view is rotated around the vertical axis through the pivot point.
9059 *
9060 * @see #getPivotX()
9061 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009062 * @see #setRotationY(float)
9063 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009064 * @return The degrees of Y rotation.
9065 */
Chet Haasea5531132012-02-02 13:41:44 -08009066 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009067 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009068 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009069 }
9070
9071 /**
Chet Haase897247b2010-09-09 14:54:47 -07009072 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9073 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9074 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009075 *
Romain Guya5364ee2011-02-24 14:46:04 -08009076 * When rotating large views, it is recommended to adjust the camera distance
9077 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009078 *
9079 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009080 *
9081 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009082 * @see #getPivotX()
9083 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009084 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009085 * @see #setRotationX(float)
9086 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009087 *
9088 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009089 */
9090 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009091 ensureTransformationInfo();
9092 final TransformationInfo info = mTransformationInfo;
9093 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009094 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009095 info.mRotationY = rotationY;
9096 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009097 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009098 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009099 mDisplayList.setRotationY(rotationY);
9100 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009101 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009102 // View was rejected last time it was drawn by its parent; this may have changed
9103 invalidateParentIfNeeded();
9104 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009105 }
9106 }
9107
9108 /**
9109 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9110 *
9111 * @see #getPivotX()
9112 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009113 * @see #setRotationX(float)
9114 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009115 * @return The degrees of X rotation.
9116 */
Chet Haasea5531132012-02-02 13:41:44 -08009117 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009118 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009119 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009120 }
9121
9122 /**
Chet Haase897247b2010-09-09 14:54:47 -07009123 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9124 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9125 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009126 *
Romain Guya5364ee2011-02-24 14:46:04 -08009127 * When rotating large views, it is recommended to adjust the camera distance
9128 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009129 *
9130 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009131 *
9132 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009133 * @see #getPivotX()
9134 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009135 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009136 * @see #setRotationY(float)
9137 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009138 *
9139 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009140 */
9141 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009142 ensureTransformationInfo();
9143 final TransformationInfo info = mTransformationInfo;
9144 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009145 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009146 info.mRotationX = rotationX;
9147 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009148 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009149 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009150 mDisplayList.setRotationX(rotationX);
9151 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009152 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009153 // View was rejected last time it was drawn by its parent; this may have changed
9154 invalidateParentIfNeeded();
9155 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009156 }
9157 }
9158
9159 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009160 * The amount that the view is scaled in x around the pivot point, as a proportion of
9161 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9162 *
Joe Onorato93162322010-09-16 15:42:01 -04009163 * <p>By default, this is 1.0f.
9164 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 * @see #getPivotX()
9166 * @see #getPivotY()
9167 * @return The scaling factor.
9168 */
Chet Haasea5531132012-02-02 13:41:44 -08009169 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009170 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009171 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009172 }
9173
9174 /**
9175 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9176 * the view's unscaled width. A value of 1 means that no scaling is applied.
9177 *
9178 * @param scaleX The scaling factor.
9179 * @see #getPivotX()
9180 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009181 *
9182 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009183 */
9184 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009185 ensureTransformationInfo();
9186 final TransformationInfo info = mTransformationInfo;
9187 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009188 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009189 info.mScaleX = scaleX;
9190 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009191 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009192 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009193 mDisplayList.setScaleX(scaleX);
9194 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009195 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009196 // View was rejected last time it was drawn by its parent; this may have changed
9197 invalidateParentIfNeeded();
9198 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 }
9200 }
9201
9202 /**
9203 * The amount that the view is scaled in y around the pivot point, as a proportion of
9204 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9205 *
Joe Onorato93162322010-09-16 15:42:01 -04009206 * <p>By default, this is 1.0f.
9207 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009208 * @see #getPivotX()
9209 * @see #getPivotY()
9210 * @return The scaling factor.
9211 */
Chet Haasea5531132012-02-02 13:41:44 -08009212 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009213 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009214 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009215 }
9216
9217 /**
9218 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9219 * the view's unscaled width. A value of 1 means that no scaling is applied.
9220 *
9221 * @param scaleY The scaling factor.
9222 * @see #getPivotX()
9223 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009224 *
9225 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009226 */
9227 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009228 ensureTransformationInfo();
9229 final TransformationInfo info = mTransformationInfo;
9230 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009231 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009232 info.mScaleY = scaleY;
9233 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009234 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009235 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009236 mDisplayList.setScaleY(scaleY);
9237 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009238 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009239 // View was rejected last time it was drawn by its parent; this may have changed
9240 invalidateParentIfNeeded();
9241 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009242 }
9243 }
9244
9245 /**
9246 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9247 * and {@link #setScaleX(float) scaled}.
9248 *
9249 * @see #getRotation()
9250 * @see #getScaleX()
9251 * @see #getScaleY()
9252 * @see #getPivotY()
9253 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009254 *
9255 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009256 */
Chet Haasea5531132012-02-02 13:41:44 -08009257 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009258 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009259 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009260 }
9261
9262 /**
9263 * Sets the x location of the point around which the view is
9264 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009265 * By default, the pivot point is centered on the object.
9266 * Setting this property disables this behavior and causes the view to use only the
9267 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009268 *
9269 * @param pivotX The x location of the pivot point.
9270 * @see #getRotation()
9271 * @see #getScaleX()
9272 * @see #getScaleY()
9273 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009274 *
9275 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009276 */
9277 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009278 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009279 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009280 final TransformationInfo info = mTransformationInfo;
9281 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009282 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009283 info.mPivotX = pivotX;
9284 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009285 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009286 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009287 mDisplayList.setPivotX(pivotX);
9288 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009289 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009290 // View was rejected last time it was drawn by its parent; this may have changed
9291 invalidateParentIfNeeded();
9292 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009293 }
9294 }
9295
9296 /**
9297 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9298 * and {@link #setScaleY(float) scaled}.
9299 *
9300 * @see #getRotation()
9301 * @see #getScaleX()
9302 * @see #getScaleY()
9303 * @see #getPivotY()
9304 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009305 *
9306 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009307 */
Chet Haasea5531132012-02-02 13:41:44 -08009308 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009309 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009310 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009311 }
9312
9313 /**
9314 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009315 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9316 * Setting this property disables this behavior and causes the view to use only the
9317 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009318 *
9319 * @param pivotY The y location of the pivot point.
9320 * @see #getRotation()
9321 * @see #getScaleX()
9322 * @see #getScaleY()
9323 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009324 *
9325 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009326 */
9327 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009328 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009329 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009330 final TransformationInfo info = mTransformationInfo;
9331 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009332 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009333 info.mPivotY = pivotY;
9334 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009335 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009336 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009337 mDisplayList.setPivotY(pivotY);
9338 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009339 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009340 // View was rejected last time it was drawn by its parent; this may have changed
9341 invalidateParentIfNeeded();
9342 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009343 }
9344 }
9345
9346 /**
9347 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9348 * completely transparent and 1 means the view is completely opaque.
9349 *
Joe Onorato93162322010-09-16 15:42:01 -04009350 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009351 * @return The opacity of the view.
9352 */
Chet Haasea5531132012-02-02 13:41:44 -08009353 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009354 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009355 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009356 }
9357
9358 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009359 * Returns whether this View has content which overlaps. This function, intended to be
9360 * overridden by specific View types, is an optimization when alpha is set on a view. If
9361 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9362 * and then composited it into place, which can be expensive. If the view has no overlapping
9363 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9364 * An example of overlapping rendering is a TextView with a background image, such as a
9365 * Button. An example of non-overlapping rendering is a TextView with no background, or
9366 * an ImageView with only the foreground image. The default implementation returns true;
9367 * subclasses should override if they have cases which can be optimized.
9368 *
9369 * @return true if the content in this view might overlap, false otherwise.
9370 */
9371 public boolean hasOverlappingRendering() {
9372 return true;
9373 }
9374
9375 /**
Romain Guy171c5922011-01-06 10:04:23 -08009376 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9377 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009378 *
Romain Guy171c5922011-01-06 10:04:23 -08009379 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9380 * responsible for applying the opacity itself. Otherwise, calling this method is
9381 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009382 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009383 *
Chet Haasea5531132012-02-02 13:41:44 -08009384 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9385 * performance implications. It is generally best to use the alpha property sparingly and
9386 * transiently, as in the case of fading animations.</p>
9387 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009388 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009389 *
Joe Malin32736f02011-01-19 16:14:20 -08009390 * @see #setLayerType(int, android.graphics.Paint)
9391 *
Chet Haase73066682010-11-29 15:55:32 -08009392 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009393 */
9394 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009395 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009396 if (mTransformationInfo.mAlpha != alpha) {
9397 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009398 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009399 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009400 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009401 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009402 invalidate(true);
9403 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009404 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009405 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009406 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009407 mDisplayList.setAlpha(alpha);
9408 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009409 }
Chet Haaseed032702010-10-01 14:05:54 -07009410 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009411 }
9412
9413 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009414 * Faster version of setAlpha() which performs the same steps except there are
9415 * no calls to invalidate(). The caller of this function should perform proper invalidation
9416 * on the parent and this object. The return value indicates whether the subclass handles
9417 * alpha (the return value for onSetAlpha()).
9418 *
9419 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009420 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9421 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009422 */
9423 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009424 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009425 if (mTransformationInfo.mAlpha != alpha) {
9426 mTransformationInfo.mAlpha = alpha;
9427 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9428 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009429 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009430 return true;
9431 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009432 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009433 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009434 mDisplayList.setAlpha(alpha);
9435 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009436 }
Chet Haasea00f3862011-02-22 06:34:40 -08009437 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009438 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009439 }
9440
9441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 * Top position of this view relative to its parent.
9443 *
9444 * @return The top of this view, in pixels.
9445 */
9446 @ViewDebug.CapturedViewProperty
9447 public final int getTop() {
9448 return mTop;
9449 }
9450
9451 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009452 * Sets the top position of this view relative to its parent. This method is meant to be called
9453 * by the layout system and should not generally be called otherwise, because the property
9454 * may be changed at any time by the layout.
9455 *
9456 * @param top The top of this view, in pixels.
9457 */
9458 public final void setTop(int top) {
9459 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009460 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009461 final boolean matrixIsIdentity = mTransformationInfo == null
9462 || mTransformationInfo.mMatrixIsIdentity;
9463 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009464 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009465 int minTop;
9466 int yLoc;
9467 if (top < mTop) {
9468 minTop = top;
9469 yLoc = top - mTop;
9470 } else {
9471 minTop = mTop;
9472 yLoc = 0;
9473 }
Chet Haasee9140a72011-02-16 16:23:29 -08009474 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009475 }
9476 } else {
9477 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009478 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009479 }
9480
Chet Haaseed032702010-10-01 14:05:54 -07009481 int width = mRight - mLeft;
9482 int oldHeight = mBottom - mTop;
9483
Chet Haase21cd1382010-09-01 17:42:29 -07009484 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009485 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009486 mDisplayList.setTop(mTop);
9487 }
Chet Haase21cd1382010-09-01 17:42:29 -07009488
Chet Haaseed032702010-10-01 14:05:54 -07009489 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9490
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009491 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009492 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009493 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009494 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009495 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009496 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009497 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009498 }
Chet Haase55dbb652010-12-21 20:15:08 -08009499 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009500 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009501 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009502 // View was rejected last time it was drawn by its parent; this may have changed
9503 invalidateParentIfNeeded();
9504 }
Chet Haase21cd1382010-09-01 17:42:29 -07009505 }
9506 }
9507
9508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 * Bottom position of this view relative to its parent.
9510 *
9511 * @return The bottom of this view, in pixels.
9512 */
9513 @ViewDebug.CapturedViewProperty
9514 public final int getBottom() {
9515 return mBottom;
9516 }
9517
9518 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009519 * True if this view has changed since the last time being drawn.
9520 *
9521 * @return The dirty state of this view.
9522 */
9523 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009524 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009525 }
9526
9527 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009528 * Sets the bottom position of this view relative to its parent. This method is meant to be
9529 * called by the layout system and should not generally be called otherwise, because the
9530 * property may be changed at any time by the layout.
9531 *
9532 * @param bottom The bottom of this view, in pixels.
9533 */
9534 public final void setBottom(int bottom) {
9535 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009536 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009537 final boolean matrixIsIdentity = mTransformationInfo == null
9538 || mTransformationInfo.mMatrixIsIdentity;
9539 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009540 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009541 int maxBottom;
9542 if (bottom < mBottom) {
9543 maxBottom = mBottom;
9544 } else {
9545 maxBottom = bottom;
9546 }
Chet Haasee9140a72011-02-16 16:23:29 -08009547 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009548 }
9549 } else {
9550 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009551 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009552 }
9553
Chet Haaseed032702010-10-01 14:05:54 -07009554 int width = mRight - mLeft;
9555 int oldHeight = mBottom - mTop;
9556
Chet Haase21cd1382010-09-01 17:42:29 -07009557 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009558 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009559 mDisplayList.setBottom(mBottom);
9560 }
Chet Haase21cd1382010-09-01 17:42:29 -07009561
Chet Haaseed032702010-10-01 14:05:54 -07009562 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9563
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009564 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009565 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009566 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009567 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009568 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009569 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009570 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009571 }
Chet Haase55dbb652010-12-21 20:15:08 -08009572 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009573 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009574 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009575 // View was rejected last time it was drawn by its parent; this may have changed
9576 invalidateParentIfNeeded();
9577 }
Chet Haase21cd1382010-09-01 17:42:29 -07009578 }
9579 }
9580
9581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009582 * Left position of this view relative to its parent.
9583 *
9584 * @return The left edge of this view, in pixels.
9585 */
9586 @ViewDebug.CapturedViewProperty
9587 public final int getLeft() {
9588 return mLeft;
9589 }
9590
9591 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009592 * Sets the left position of this view relative to its parent. This method is meant to be called
9593 * by the layout system and should not generally be called otherwise, because the property
9594 * may be changed at any time by the layout.
9595 *
9596 * @param left The bottom of this view, in pixels.
9597 */
9598 public final void setLeft(int left) {
9599 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009600 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009601 final boolean matrixIsIdentity = mTransformationInfo == null
9602 || mTransformationInfo.mMatrixIsIdentity;
9603 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009604 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009605 int minLeft;
9606 int xLoc;
9607 if (left < mLeft) {
9608 minLeft = left;
9609 xLoc = left - mLeft;
9610 } else {
9611 minLeft = mLeft;
9612 xLoc = 0;
9613 }
Chet Haasee9140a72011-02-16 16:23:29 -08009614 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009615 }
9616 } else {
9617 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009618 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009619 }
9620
Chet Haaseed032702010-10-01 14:05:54 -07009621 int oldWidth = mRight - mLeft;
9622 int height = mBottom - mTop;
9623
Chet Haase21cd1382010-09-01 17:42:29 -07009624 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009625 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009626 mDisplayList.setLeft(left);
9627 }
Chet Haase21cd1382010-09-01 17:42:29 -07009628
Chet Haaseed032702010-10-01 14:05:54 -07009629 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9630
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009631 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009632 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009633 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009634 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009635 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009636 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009637 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009638 }
Chet Haase55dbb652010-12-21 20:15:08 -08009639 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009640 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009641 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009642 // View was rejected last time it was drawn by its parent; this may have changed
9643 invalidateParentIfNeeded();
9644 }
Chet Haase21cd1382010-09-01 17:42:29 -07009645 }
9646 }
9647
9648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009649 * Right position of this view relative to its parent.
9650 *
9651 * @return The right edge of this view, in pixels.
9652 */
9653 @ViewDebug.CapturedViewProperty
9654 public final int getRight() {
9655 return mRight;
9656 }
9657
9658 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009659 * Sets the right position of this view relative to its parent. This method is meant to be called
9660 * by the layout system and should not generally be called otherwise, because the property
9661 * may be changed at any time by the layout.
9662 *
9663 * @param right The bottom of this view, in pixels.
9664 */
9665 public final void setRight(int right) {
9666 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009667 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009668 final boolean matrixIsIdentity = mTransformationInfo == null
9669 || mTransformationInfo.mMatrixIsIdentity;
9670 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009671 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009672 int maxRight;
9673 if (right < mRight) {
9674 maxRight = mRight;
9675 } else {
9676 maxRight = right;
9677 }
Chet Haasee9140a72011-02-16 16:23:29 -08009678 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009679 }
9680 } else {
9681 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009682 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009683 }
9684
Chet Haaseed032702010-10-01 14:05:54 -07009685 int oldWidth = mRight - mLeft;
9686 int height = mBottom - mTop;
9687
Chet Haase21cd1382010-09-01 17:42:29 -07009688 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009689 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009690 mDisplayList.setRight(mRight);
9691 }
Chet Haase21cd1382010-09-01 17:42:29 -07009692
Chet Haaseed032702010-10-01 14:05:54 -07009693 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9694
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009695 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009696 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009697 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009698 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009699 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009700 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009701 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009702 }
Chet Haase55dbb652010-12-21 20:15:08 -08009703 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009704 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009705 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009706 // View was rejected last time it was drawn by its parent; this may have changed
9707 invalidateParentIfNeeded();
9708 }
Chet Haase21cd1382010-09-01 17:42:29 -07009709 }
9710 }
9711
9712 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009713 * The visual x position of this view, in pixels. This is equivalent to the
9714 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009715 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009716 *
Chet Haasedf030d22010-07-30 17:22:38 -07009717 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009718 */
Chet Haasea5531132012-02-02 13:41:44 -08009719 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009720 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009721 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009722 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009723
Chet Haasedf030d22010-07-30 17:22:38 -07009724 /**
9725 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9726 * {@link #setTranslationX(float) translationX} property to be the difference between
9727 * the x value passed in and the current {@link #getLeft() left} property.
9728 *
9729 * @param x The visual x position of this view, in pixels.
9730 */
9731 public void setX(float x) {
9732 setTranslationX(x - mLeft);
9733 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009734
Chet Haasedf030d22010-07-30 17:22:38 -07009735 /**
9736 * The visual y position of this view, in pixels. This is equivalent to the
9737 * {@link #setTranslationY(float) translationY} property plus the current
9738 * {@link #getTop() top} property.
9739 *
9740 * @return The visual y position of this view, in pixels.
9741 */
Chet Haasea5531132012-02-02 13:41:44 -08009742 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009743 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009744 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009745 }
9746
9747 /**
9748 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9749 * {@link #setTranslationY(float) translationY} property to be the difference between
9750 * the y value passed in and the current {@link #getTop() top} property.
9751 *
9752 * @param y The visual y position of this view, in pixels.
9753 */
9754 public void setY(float y) {
9755 setTranslationY(y - mTop);
9756 }
9757
9758
9759 /**
9760 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9761 * This position is post-layout, in addition to wherever the object's
9762 * layout placed it.
9763 *
9764 * @return The horizontal position of this view relative to its left position, in pixels.
9765 */
Chet Haasea5531132012-02-02 13:41:44 -08009766 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009767 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009768 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009769 }
9770
9771 /**
9772 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9773 * This effectively positions the object post-layout, in addition to wherever the object's
9774 * layout placed it.
9775 *
9776 * @param translationX The horizontal position of this view relative to its left position,
9777 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009778 *
9779 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009780 */
9781 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009782 ensureTransformationInfo();
9783 final TransformationInfo info = mTransformationInfo;
9784 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009785 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009786 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009787 info.mTranslationX = translationX;
9788 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009789 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009790 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009791 mDisplayList.setTranslationX(translationX);
9792 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009793 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009794 // View was rejected last time it was drawn by its parent; this may have changed
9795 invalidateParentIfNeeded();
9796 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009797 }
9798 }
9799
9800 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009801 * The horizontal location of this view relative to its {@link #getTop() top} position.
9802 * This position is post-layout, in addition to wherever the object's
9803 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009804 *
Chet Haasedf030d22010-07-30 17:22:38 -07009805 * @return The vertical position of this view relative to its top position,
9806 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009807 */
Chet Haasea5531132012-02-02 13:41:44 -08009808 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009809 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009810 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009811 }
9812
9813 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009814 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9815 * This effectively positions the object post-layout, in addition to wherever the object's
9816 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009817 *
Chet Haasedf030d22010-07-30 17:22:38 -07009818 * @param translationY The vertical position of this view relative to its top position,
9819 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009820 *
9821 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009822 */
Chet Haasedf030d22010-07-30 17:22:38 -07009823 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009824 ensureTransformationInfo();
9825 final TransformationInfo info = mTransformationInfo;
9826 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009827 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009828 info.mTranslationY = translationY;
9829 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009830 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009831 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009832 mDisplayList.setTranslationY(translationY);
9833 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009834 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009835 // View was rejected last time it was drawn by its parent; this may have changed
9836 invalidateParentIfNeeded();
9837 }
Chet Haasedf030d22010-07-30 17:22:38 -07009838 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009839 }
9840
9841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 * Hit rectangle in parent's coordinates
9843 *
9844 * @param outRect The hit rectangle of the view.
9845 */
9846 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009847 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009848 final TransformationInfo info = mTransformationInfo;
9849 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009850 outRect.set(mLeft, mTop, mRight, mBottom);
9851 } else {
9852 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009853 tmpRect.set(-info.mPivotX, -info.mPivotY,
9854 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9855 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009856 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9857 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009858 }
9859 }
9860
9861 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009862 * Determines whether the given point, in local coordinates is inside the view.
9863 */
9864 /*package*/ final boolean pointInView(float localX, float localY) {
9865 return localX >= 0 && localX < (mRight - mLeft)
9866 && localY >= 0 && localY < (mBottom - mTop);
9867 }
9868
9869 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009870 * Utility method to determine whether the given point, in local coordinates,
9871 * is inside the view, where the area of the view is expanded by the slop factor.
9872 * This method is called while processing touch-move events to determine if the event
9873 * is still within the view.
9874 */
9875 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009876 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009877 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 }
9879
9880 /**
9881 * When a view has focus and the user navigates away from it, the next view is searched for
9882 * starting from the rectangle filled in by this method.
9883 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009884 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009885 * of the view. However, if your view maintains some idea of internal selection,
9886 * such as a cursor, or a selected row or column, you should override this method and
9887 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 *
9889 * @param r The rectangle to fill in, in this view's coordinates.
9890 */
9891 public void getFocusedRect(Rect r) {
9892 getDrawingRect(r);
9893 }
9894
9895 /**
9896 * If some part of this view is not clipped by any of its parents, then
9897 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009898 * coordinates (without taking possible View rotations into account), offset
9899 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9900 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 *
9902 * @param r If true is returned, r holds the global coordinates of the
9903 * visible portion of this view.
9904 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9905 * between this view and its root. globalOffet may be null.
9906 * @return true if r is non-empty (i.e. part of the view is visible at the
9907 * root level.
9908 */
9909 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9910 int width = mRight - mLeft;
9911 int height = mBottom - mTop;
9912 if (width > 0 && height > 0) {
9913 r.set(0, 0, width, height);
9914 if (globalOffset != null) {
9915 globalOffset.set(-mScrollX, -mScrollY);
9916 }
9917 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9918 }
9919 return false;
9920 }
9921
9922 public final boolean getGlobalVisibleRect(Rect r) {
9923 return getGlobalVisibleRect(r, null);
9924 }
9925
9926 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009927 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009928 if (getGlobalVisibleRect(r, offset)) {
9929 r.offset(-offset.x, -offset.y); // make r local
9930 return true;
9931 }
9932 return false;
9933 }
9934
9935 /**
9936 * Offset this view's vertical location by the specified number of pixels.
9937 *
9938 * @param offset the number of pixels to offset the view by
9939 */
9940 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009941 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009942 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009943 final boolean matrixIsIdentity = mTransformationInfo == null
9944 || mTransformationInfo.mMatrixIsIdentity;
9945 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009946 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009947 invalidateViewProperty(false, false);
9948 } else {
9949 final ViewParent p = mParent;
9950 if (p != null && mAttachInfo != null) {
9951 final Rect r = mAttachInfo.mTmpInvalRect;
9952 int minTop;
9953 int maxBottom;
9954 int yLoc;
9955 if (offset < 0) {
9956 minTop = mTop + offset;
9957 maxBottom = mBottom;
9958 yLoc = offset;
9959 } else {
9960 minTop = mTop;
9961 maxBottom = mBottom + offset;
9962 yLoc = 0;
9963 }
9964 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9965 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009966 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009967 }
9968 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009969 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009970 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009971
Chet Haasec3aa3612010-06-17 08:50:37 -07009972 mTop += offset;
9973 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009974 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009975 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009976 invalidateViewProperty(false, false);
9977 } else {
9978 if (!matrixIsIdentity) {
9979 invalidateViewProperty(false, true);
9980 }
9981 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009982 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 }
9985
9986 /**
9987 * Offset this view's horizontal location by the specified amount of pixels.
9988 *
9989 * @param offset the numer of pixels to offset the view by
9990 */
9991 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009992 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009993 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009994 final boolean matrixIsIdentity = mTransformationInfo == null
9995 || mTransformationInfo.mMatrixIsIdentity;
9996 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009997 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009998 invalidateViewProperty(false, false);
9999 } else {
10000 final ViewParent p = mParent;
10001 if (p != null && mAttachInfo != null) {
10002 final Rect r = mAttachInfo.mTmpInvalRect;
10003 int minLeft;
10004 int maxRight;
10005 if (offset < 0) {
10006 minLeft = mLeft + offset;
10007 maxRight = mRight;
10008 } else {
10009 minLeft = mLeft;
10010 maxRight = mRight + offset;
10011 }
10012 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
10013 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -070010014 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010015 }
10016 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -070010017 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -070010018 }
Romain Guy33e72ae2010-07-17 12:40:29 -070010019
Chet Haasec3aa3612010-06-17 08:50:37 -070010020 mLeft += offset;
10021 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -070010022 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080010023 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -070010024 invalidateViewProperty(false, false);
10025 } else {
10026 if (!matrixIsIdentity) {
10027 invalidateViewProperty(false, true);
10028 }
10029 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -080010030 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 }
10033
10034 /**
10035 * Get the LayoutParams associated with this view. All views should have
10036 * layout parameters. These supply parameters to the <i>parent</i> of this
10037 * view specifying how it should be arranged. There are many subclasses of
10038 * ViewGroup.LayoutParams, and these correspond to the different subclasses
10039 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -080010040 *
10041 * This method may return null if this View is not attached to a parent
10042 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
10043 * was not invoked successfully. When a View is attached to a parent
10044 * ViewGroup, this method must not return null.
10045 *
10046 * @return The LayoutParams associated with this view, or null if no
10047 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010049 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 public ViewGroup.LayoutParams getLayoutParams() {
10051 return mLayoutParams;
10052 }
10053
10054 /**
10055 * Set the layout parameters associated with this view. These supply
10056 * parameters to the <i>parent</i> of this view specifying how it should be
10057 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10058 * correspond to the different subclasses of ViewGroup that are responsible
10059 * for arranging their children.
10060 *
Romain Guy01c174b2011-02-22 11:51:06 -080010061 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 */
10063 public void setLayoutParams(ViewGroup.LayoutParams params) {
10064 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010065 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 }
10067 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010068 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010069 if (mParent instanceof ViewGroup) {
10070 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 requestLayout();
10073 }
10074
10075 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010076 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010077 *
10078 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010079 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010080 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010081 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010082 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010083 }
10084 }
10085
10086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010087 * Set the scrolled position of your view. This will cause a call to
10088 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10089 * invalidated.
10090 * @param x the x position to scroll to
10091 * @param y the y position to scroll to
10092 */
10093 public void scrollTo(int x, int y) {
10094 if (mScrollX != x || mScrollY != y) {
10095 int oldX = mScrollX;
10096 int oldY = mScrollY;
10097 mScrollX = x;
10098 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010099 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010101 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010102 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 }
10105 }
10106
10107 /**
10108 * Move the scrolled position of your view. This will cause a call to
10109 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10110 * invalidated.
10111 * @param x the amount of pixels to scroll by horizontally
10112 * @param y the amount of pixels to scroll by vertically
10113 */
10114 public void scrollBy(int x, int y) {
10115 scrollTo(mScrollX + x, mScrollY + y);
10116 }
10117
10118 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010119 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10120 * animation to fade the scrollbars out after a default delay. If a subclass
10121 * provides animated scrolling, the start delay should equal the duration
10122 * of the scrolling animation.</p>
10123 *
10124 * <p>The animation starts only if at least one of the scrollbars is
10125 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10126 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10127 * this method returns true, and false otherwise. If the animation is
10128 * started, this method calls {@link #invalidate()}; in that case the
10129 * caller should not call {@link #invalidate()}.</p>
10130 *
10131 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010132 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010133 *
10134 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10135 * and {@link #scrollTo(int, int)}.</p>
10136 *
10137 * @return true if the animation is played, false otherwise
10138 *
10139 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010140 * @see #scrollBy(int, int)
10141 * @see #scrollTo(int, int)
10142 * @see #isHorizontalScrollBarEnabled()
10143 * @see #isVerticalScrollBarEnabled()
10144 * @see #setHorizontalScrollBarEnabled(boolean)
10145 * @see #setVerticalScrollBarEnabled(boolean)
10146 */
10147 protected boolean awakenScrollBars() {
10148 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010149 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010150 }
10151
10152 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010153 * Trigger the scrollbars to draw.
10154 * This method differs from awakenScrollBars() only in its default duration.
10155 * initialAwakenScrollBars() will show the scroll bars for longer than
10156 * usual to give the user more of a chance to notice them.
10157 *
10158 * @return true if the animation is played, false otherwise.
10159 */
10160 private boolean initialAwakenScrollBars() {
10161 return mScrollCache != null &&
10162 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10163 }
10164
10165 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010166 * <p>
10167 * Trigger the scrollbars to draw. When invoked this method starts an
10168 * animation to fade the scrollbars out after a fixed delay. If a subclass
10169 * provides animated scrolling, the start delay should equal the duration of
10170 * the scrolling animation.
10171 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010172 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010173 * <p>
10174 * The animation starts only if at least one of the scrollbars is enabled,
10175 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10176 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10177 * this method returns true, and false otherwise. If the animation is
10178 * started, this method calls {@link #invalidate()}; in that case the caller
10179 * should not call {@link #invalidate()}.
10180 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010181 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010182 * <p>
10183 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010184 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010185 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010186 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010187 * @param startDelay the delay, in milliseconds, after which the animation
10188 * should start; when the delay is 0, the animation starts
10189 * immediately
10190 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010191 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010192 * @see #scrollBy(int, int)
10193 * @see #scrollTo(int, int)
10194 * @see #isHorizontalScrollBarEnabled()
10195 * @see #isVerticalScrollBarEnabled()
10196 * @see #setHorizontalScrollBarEnabled(boolean)
10197 * @see #setVerticalScrollBarEnabled(boolean)
10198 */
10199 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010200 return awakenScrollBars(startDelay, true);
10201 }
Joe Malin32736f02011-01-19 16:14:20 -080010202
Mike Cleron290947b2009-09-29 18:34:32 -070010203 /**
10204 * <p>
10205 * Trigger the scrollbars to draw. When invoked this method starts an
10206 * animation to fade the scrollbars out after a fixed delay. If a subclass
10207 * provides animated scrolling, the start delay should equal the duration of
10208 * the scrolling animation.
10209 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010210 *
Mike Cleron290947b2009-09-29 18:34:32 -070010211 * <p>
10212 * The animation starts only if at least one of the scrollbars is enabled,
10213 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10214 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10215 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010216 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010217 * is set to true; in that case the caller
10218 * should not call {@link #invalidate()}.
10219 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010220 *
Mike Cleron290947b2009-09-29 18:34:32 -070010221 * <p>
10222 * This method should be invoked everytime a subclass directly updates the
10223 * scroll parameters.
10224 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010225 *
Mike Cleron290947b2009-09-29 18:34:32 -070010226 * @param startDelay the delay, in milliseconds, after which the animation
10227 * should start; when the delay is 0, the animation starts
10228 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010229 *
Mike Cleron290947b2009-09-29 18:34:32 -070010230 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010231 *
Mike Cleron290947b2009-09-29 18:34:32 -070010232 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010233 *
Mike Cleron290947b2009-09-29 18:34:32 -070010234 * @see #scrollBy(int, int)
10235 * @see #scrollTo(int, int)
10236 * @see #isHorizontalScrollBarEnabled()
10237 * @see #isVerticalScrollBarEnabled()
10238 * @see #setHorizontalScrollBarEnabled(boolean)
10239 * @see #setVerticalScrollBarEnabled(boolean)
10240 */
10241 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010242 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010243
Mike Cleronf116bf82009-09-27 19:14:12 -070010244 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10245 return false;
10246 }
10247
10248 if (scrollCache.scrollBar == null) {
10249 scrollCache.scrollBar = new ScrollBarDrawable();
10250 }
10251
10252 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10253
Mike Cleron290947b2009-09-29 18:34:32 -070010254 if (invalidate) {
10255 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010256 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010257 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010258
10259 if (scrollCache.state == ScrollabilityCache.OFF) {
10260 // FIXME: this is copied from WindowManagerService.
10261 // We should get this value from the system when it
10262 // is possible to do so.
10263 final int KEY_REPEAT_FIRST_DELAY = 750;
10264 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10265 }
10266
10267 // Tell mScrollCache when we should start fading. This may
10268 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010269 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010270 scrollCache.fadeStartTime = fadeStartTime;
10271 scrollCache.state = ScrollabilityCache.ON;
10272
10273 // Schedule our fader to run, unscheduling any old ones first
10274 if (mAttachInfo != null) {
10275 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10276 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10277 }
10278
10279 return true;
10280 }
10281
10282 return false;
10283 }
10284
10285 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010286 * Do not invalidate views which are not visible and which are not running an animation. They
10287 * will not get drawn and they should not set dirty flags as if they will be drawn
10288 */
10289 private boolean skipInvalidate() {
10290 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10291 (!(mParent instanceof ViewGroup) ||
10292 !((ViewGroup) mParent).isViewTransitioning(this));
10293 }
10294 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010295 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010296 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10297 * in the future. This must be called from a UI thread. To call from a non-UI
10298 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010299 *
10300 * WARNING: This method is destructive to dirty.
10301 * @param dirty the rectangle representing the bounds of the dirty region
10302 */
10303 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010304 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010305 return;
10306 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010307 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10308 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10309 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10310 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10311 mPrivateFlags |= PFLAG_INVALIDATED;
10312 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 final ViewParent p = mParent;
10314 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010315 //noinspection PointlessBooleanExpression,ConstantConditions
10316 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10317 if (p != null && ai != null && ai.mHardwareAccelerated) {
10318 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010319 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010320 p.invalidateChild(this, null);
10321 return;
10322 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010324 if (p != null && ai != null) {
10325 final int scrollX = mScrollX;
10326 final int scrollY = mScrollY;
10327 final Rect r = ai.mTmpInvalRect;
10328 r.set(dirty.left - scrollX, dirty.top - scrollY,
10329 dirty.right - scrollX, dirty.bottom - scrollY);
10330 mParent.invalidateChild(this, r);
10331 }
10332 }
10333 }
10334
10335 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010336 * Mark the area defined by the rect (l,t,r,b) as needing to be drawn.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010338 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10339 * will be called at some point in the future. This must be called from
10340 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 * @param l the left position of the dirty region
10342 * @param t the top position of the dirty region
10343 * @param r the right position of the dirty region
10344 * @param b the bottom position of the dirty region
10345 */
10346 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010347 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010348 return;
10349 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010350 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10351 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10352 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10353 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10354 mPrivateFlags |= PFLAG_INVALIDATED;
10355 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 final ViewParent p = mParent;
10357 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010358 //noinspection PointlessBooleanExpression,ConstantConditions
10359 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10360 if (p != null && ai != null && ai.mHardwareAccelerated) {
10361 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010362 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010363 p.invalidateChild(this, null);
10364 return;
10365 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 if (p != null && ai != null && l < r && t < b) {
10368 final int scrollX = mScrollX;
10369 final int scrollY = mScrollY;
10370 final Rect tmpr = ai.mTmpInvalRect;
10371 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10372 p.invalidateChild(this, tmpr);
10373 }
10374 }
10375 }
10376
10377 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010378 * Invalidate the whole view. If the view is visible,
10379 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10380 * the future. This must be called from a UI thread. To call from a non-UI thread,
10381 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 */
10383 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010384 invalidate(true);
10385 }
Joe Malin32736f02011-01-19 16:14:20 -080010386
Chet Haaseed032702010-10-01 14:05:54 -070010387 /**
10388 * This is where the invalidate() work actually happens. A full invalidate()
10389 * causes the drawing cache to be invalidated, but this function can be called with
10390 * invalidateCache set to false to skip that invalidation step for cases that do not
10391 * need it (for example, a component that remains at the same dimensions with the same
10392 * content).
10393 *
10394 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10395 * well. This is usually true for a full invalidate, but may be set to false if the
10396 * View's contents or dimensions have not changed.
10397 */
Romain Guy849d0a32011-02-01 17:20:48 -080010398 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010399 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010400 return;
10401 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010402 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10403 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10404 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010405 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010406 mPrivateFlags &= ~PFLAG_DRAWN;
10407 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010408 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010409 mPrivateFlags |= PFLAG_INVALIDATED;
10410 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010412 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010413 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010414 //noinspection PointlessBooleanExpression,ConstantConditions
10415 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10416 if (p != null && ai != null && ai.mHardwareAccelerated) {
10417 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010418 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010419 p.invalidateChild(this, null);
10420 return;
10421 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010422 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 if (p != null && ai != null) {
10425 final Rect r = ai.mTmpInvalRect;
10426 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10427 // Don't call invalidate -- we don't want to internally scroll
10428 // our own bounds
10429 p.invalidateChild(this, r);
10430 }
10431 }
10432 }
10433
10434 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010435 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10436 * set any flags or handle all of the cases handled by the default invalidation methods.
10437 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10438 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10439 * walk up the hierarchy, transforming the dirty rect as necessary.
10440 *
10441 * The method also handles normal invalidation logic if display list properties are not
10442 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10443 * backup approach, to handle these cases used in the various property-setting methods.
10444 *
10445 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10446 * are not being used in this view
10447 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10448 * list properties are not being used in this view
10449 */
10450 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010451 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010452 if (invalidateParent) {
10453 invalidateParentCaches();
10454 }
10455 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010456 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010457 }
10458 invalidate(false);
10459 } else {
10460 final AttachInfo ai = mAttachInfo;
10461 final ViewParent p = mParent;
10462 if (p != null && ai != null) {
10463 final Rect r = ai.mTmpInvalRect;
10464 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10465 if (mParent instanceof ViewGroup) {
10466 ((ViewGroup) mParent).invalidateChildFast(this, r);
10467 } else {
10468 mParent.invalidateChild(this, r);
10469 }
10470 }
10471 }
10472 }
10473
10474 /**
10475 * Utility method to transform a given Rect by the current matrix of this view.
10476 */
10477 void transformRect(final Rect rect) {
10478 if (!getMatrix().isIdentity()) {
10479 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10480 boundingRect.set(rect);
10481 getMatrix().mapRect(boundingRect);
10482 rect.set((int) (boundingRect.left - 0.5f),
10483 (int) (boundingRect.top - 0.5f),
10484 (int) (boundingRect.right + 0.5f),
10485 (int) (boundingRect.bottom + 0.5f));
10486 }
10487 }
10488
10489 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010490 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010491 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10492 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010493 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10494 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010495 *
10496 * @hide
10497 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010498 protected void invalidateParentCaches() {
10499 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010500 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010501 }
10502 }
Joe Malin32736f02011-01-19 16:14:20 -080010503
Romain Guy0fd89bf2011-01-26 15:41:30 -080010504 /**
10505 * Used to indicate that the parent of this view should be invalidated. This functionality
10506 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10507 * which is necessary when various parent-managed properties of the view change, such as
10508 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10509 * an invalidation event to the parent.
10510 *
10511 * @hide
10512 */
10513 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010514 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010515 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010516 }
10517 }
10518
10519 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010520 * Indicates whether this View is opaque. An opaque View guarantees that it will
10521 * draw all the pixels overlapping its bounds using a fully opaque color.
10522 *
10523 * Subclasses of View should override this method whenever possible to indicate
10524 * whether an instance is opaque. Opaque Views are treated in a special way by
10525 * the View hierarchy, possibly allowing it to perform optimizations during
10526 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010527 *
Romain Guy24443ea2009-05-11 11:56:30 -070010528 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010529 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010530 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010531 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010532 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010533 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010534 }
10535
Adam Powell20232d02010-12-08 21:08:53 -080010536 /**
10537 * @hide
10538 */
10539 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010540 // Opaque if:
10541 // - Has a background
10542 // - Background is opaque
10543 // - Doesn't have scrollbars or scrollbars are inside overlay
10544
Philip Milne6c8ea062012-04-03 17:38:43 -070010545 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010546 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010547 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010548 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010549 }
10550
10551 final int flags = mViewFlags;
10552 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10553 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010554 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010555 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010556 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010557 }
10558 }
10559
10560 /**
10561 * @hide
10562 */
10563 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010564 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010565 }
10566
10567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 * @return A handler associated with the thread running the View. This
10569 * handler can be used to pump events in the UI events queue.
10570 */
10571 public Handler getHandler() {
10572 if (mAttachInfo != null) {
10573 return mAttachInfo.mHandler;
10574 }
10575 return null;
10576 }
10577
10578 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010579 * Gets the view root associated with the View.
10580 * @return The view root, or null if none.
10581 * @hide
10582 */
10583 public ViewRootImpl getViewRootImpl() {
10584 if (mAttachInfo != null) {
10585 return mAttachInfo.mViewRootImpl;
10586 }
10587 return null;
10588 }
10589
10590 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010591 * <p>Causes the Runnable to be added to the message queue.
10592 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010593 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 * @param action The Runnable that will be executed.
10595 *
10596 * @return Returns true if the Runnable was successfully placed in to the
10597 * message queue. Returns false on failure, usually because the
10598 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010599 *
10600 * @see #postDelayed
10601 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 */
10603 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010604 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010605 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010606 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010608 // Assume that post will succeed later
10609 ViewRootImpl.getRunQueue().post(action);
10610 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 }
10612
10613 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010614 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010616 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 * @param action The Runnable that will be executed.
10619 * @param delayMillis The delay (in milliseconds) until the Runnable
10620 * will be executed.
10621 *
10622 * @return true if the Runnable was successfully placed in to the
10623 * message queue. Returns false on failure, usually because the
10624 * looper processing the message queue is exiting. Note that a
10625 * result of true does not mean the Runnable will be processed --
10626 * if the looper is quit before the delivery time of the message
10627 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010628 *
10629 * @see #post
10630 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631 */
10632 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010633 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010634 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010635 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010637 // Assume that post will succeed later
10638 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10639 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 }
10641
10642 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010643 * <p>Causes the Runnable to execute on the next animation time step.
10644 * The runnable will be run on the user interface thread.</p>
10645 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010646 * @param action The Runnable that will be executed.
10647 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010648 * @see #postOnAnimationDelayed
10649 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010650 */
10651 public void postOnAnimation(Runnable action) {
10652 final AttachInfo attachInfo = mAttachInfo;
10653 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010654 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10655 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010656 } else {
10657 // Assume that post will succeed later
10658 ViewRootImpl.getRunQueue().post(action);
10659 }
10660 }
10661
10662 /**
10663 * <p>Causes the Runnable to execute on the next animation time step,
10664 * after the specified amount of time elapses.
10665 * The runnable will be run on the user interface thread.</p>
10666 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010667 * @param action The Runnable that will be executed.
10668 * @param delayMillis The delay (in milliseconds) until the Runnable
10669 * will be executed.
10670 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010671 * @see #postOnAnimation
10672 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010673 */
10674 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10675 final AttachInfo attachInfo = mAttachInfo;
10676 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010677 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10678 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010679 } else {
10680 // Assume that post will succeed later
10681 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10682 }
10683 }
10684
10685 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010686 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010687 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 * @param action The Runnable to remove from the message handling queue
10689 *
10690 * @return true if this view could ask the Handler to remove the Runnable,
10691 * false otherwise. When the returned value is true, the Runnable
10692 * may or may not have been actually removed from the message queue
10693 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010694 *
10695 * @see #post
10696 * @see #postDelayed
10697 * @see #postOnAnimation
10698 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 */
10700 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010701 if (action != null) {
10702 final AttachInfo attachInfo = mAttachInfo;
10703 if (attachInfo != null) {
10704 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010705 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10706 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010707 } else {
10708 // Assume that post will succeed later
10709 ViewRootImpl.getRunQueue().removeCallbacks(action);
10710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 return true;
10713 }
10714
10715 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010716 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10717 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010718 *
Romain Guye63a4f32011-08-11 11:33:31 -070010719 * <p>This method can be invoked from outside of the UI thread
10720 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010723 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010724 */
10725 public void postInvalidate() {
10726 postInvalidateDelayed(0);
10727 }
10728
10729 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010730 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10731 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010732 *
Romain Guye63a4f32011-08-11 11:33:31 -070010733 * <p>This method can be invoked from outside of the UI thread
10734 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 *
10736 * @param left The left coordinate of the rectangle to invalidate.
10737 * @param top The top coordinate of the rectangle to invalidate.
10738 * @param right The right coordinate of the rectangle to invalidate.
10739 * @param bottom The bottom coordinate of the rectangle to invalidate.
10740 *
10741 * @see #invalidate(int, int, int, int)
10742 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010743 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 */
10745 public void postInvalidate(int left, int top, int right, int bottom) {
10746 postInvalidateDelayed(0, left, top, right, bottom);
10747 }
10748
10749 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010750 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10751 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010752 *
Romain Guye63a4f32011-08-11 11:33:31 -070010753 * <p>This method can be invoked from outside of the UI thread
10754 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755 *
10756 * @param delayMilliseconds the duration in milliseconds to delay the
10757 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010758 *
10759 * @see #invalidate()
10760 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 */
10762 public void postInvalidateDelayed(long delayMilliseconds) {
10763 // We try only with the AttachInfo because there's no point in invalidating
10764 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010765 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010766 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010767 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 }
10769 }
10770
10771 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010772 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10773 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010774 *
Romain Guye63a4f32011-08-11 11:33:31 -070010775 * <p>This method can be invoked from outside of the UI thread
10776 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 *
10778 * @param delayMilliseconds the duration in milliseconds to delay the
10779 * invalidation by
10780 * @param left The left coordinate of the rectangle to invalidate.
10781 * @param top The top coordinate of the rectangle to invalidate.
10782 * @param right The right coordinate of the rectangle to invalidate.
10783 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010784 *
10785 * @see #invalidate(int, int, int, int)
10786 * @see #invalidate(Rect)
10787 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 */
10789 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10790 int right, int bottom) {
10791
10792 // We try only with the AttachInfo because there's no point in invalidating
10793 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010794 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010795 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010796 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010797 info.target = this;
10798 info.left = left;
10799 info.top = top;
10800 info.right = right;
10801 info.bottom = bottom;
10802
Jeff Browna175a5b2012-02-15 19:18:31 -080010803 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 }
10805 }
10806
10807 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010808 * <p>Cause an invalidate to happen on the next animation time step, typically the
10809 * next display frame.</p>
10810 *
10811 * <p>This method can be invoked from outside of the UI thread
10812 * only when this View is attached to a window.</p>
10813 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010814 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010815 */
10816 public void postInvalidateOnAnimation() {
10817 // We try only with the AttachInfo because there's no point in invalidating
10818 // if we are not attached to our window
10819 final AttachInfo attachInfo = mAttachInfo;
10820 if (attachInfo != null) {
10821 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10822 }
10823 }
10824
10825 /**
10826 * <p>Cause an invalidate of the specified area to happen on the next animation
10827 * time step, typically the next display frame.</p>
10828 *
10829 * <p>This method can be invoked from outside of the UI thread
10830 * only when this View is attached to a window.</p>
10831 *
10832 * @param left The left coordinate of the rectangle to invalidate.
10833 * @param top The top coordinate of the rectangle to invalidate.
10834 * @param right The right coordinate of the rectangle to invalidate.
10835 * @param bottom The bottom coordinate of the rectangle to invalidate.
10836 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010837 * @see #invalidate(int, int, int, int)
10838 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010839 */
10840 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10841 // We try only with the AttachInfo because there's no point in invalidating
10842 // if we are not attached to our window
10843 final AttachInfo attachInfo = mAttachInfo;
10844 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010845 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010846 info.target = this;
10847 info.left = left;
10848 info.top = top;
10849 info.right = right;
10850 info.bottom = bottom;
10851
10852 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10853 }
10854 }
10855
10856 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010857 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10858 * This event is sent at most once every
10859 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10860 */
10861 private void postSendViewScrolledAccessibilityEventCallback() {
10862 if (mSendViewScrolledAccessibilityEvent == null) {
10863 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10864 }
10865 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10866 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10867 postDelayed(mSendViewScrolledAccessibilityEvent,
10868 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10869 }
10870 }
10871
10872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 * Called by a parent to request that a child update its values for mScrollX
10874 * and mScrollY if necessary. This will typically be done if the child is
10875 * animating a scroll using a {@link android.widget.Scroller Scroller}
10876 * object.
10877 */
10878 public void computeScroll() {
10879 }
10880
10881 /**
10882 * <p>Indicate whether the horizontal edges are faded when the view is
10883 * scrolled horizontally.</p>
10884 *
10885 * @return true if the horizontal edges should are faded on scroll, false
10886 * otherwise
10887 *
10888 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010889 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010890 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010891 */
10892 public boolean isHorizontalFadingEdgeEnabled() {
10893 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10894 }
10895
10896 /**
10897 * <p>Define whether the horizontal edges should be faded when this view
10898 * is scrolled horizontally.</p>
10899 *
10900 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10901 * be faded when the view is scrolled
10902 * horizontally
10903 *
10904 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010905 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010906 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 */
10908 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10909 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10910 if (horizontalFadingEdgeEnabled) {
10911 initScrollCache();
10912 }
10913
10914 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10915 }
10916 }
10917
10918 /**
10919 * <p>Indicate whether the vertical edges are faded when the view is
10920 * scrolled horizontally.</p>
10921 *
10922 * @return true if the vertical edges should are faded on scroll, false
10923 * otherwise
10924 *
10925 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010926 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010927 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928 */
10929 public boolean isVerticalFadingEdgeEnabled() {
10930 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10931 }
10932
10933 /**
10934 * <p>Define whether the vertical edges should be faded when this view
10935 * is scrolled vertically.</p>
10936 *
10937 * @param verticalFadingEdgeEnabled true if the vertical edges should
10938 * be faded when the view is scrolled
10939 * vertically
10940 *
10941 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010942 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010943 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010944 */
10945 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10946 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10947 if (verticalFadingEdgeEnabled) {
10948 initScrollCache();
10949 }
10950
10951 mViewFlags ^= FADING_EDGE_VERTICAL;
10952 }
10953 }
10954
10955 /**
10956 * Returns the strength, or intensity, of the top faded edge. The strength is
10957 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10958 * returns 0.0 or 1.0 but no value in between.
10959 *
10960 * Subclasses should override this method to provide a smoother fade transition
10961 * when scrolling occurs.
10962 *
10963 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10964 */
10965 protected float getTopFadingEdgeStrength() {
10966 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10967 }
10968
10969 /**
10970 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10971 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10972 * returns 0.0 or 1.0 but no value in between.
10973 *
10974 * Subclasses should override this method to provide a smoother fade transition
10975 * when scrolling occurs.
10976 *
10977 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10978 */
10979 protected float getBottomFadingEdgeStrength() {
10980 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10981 computeVerticalScrollRange() ? 1.0f : 0.0f;
10982 }
10983
10984 /**
10985 * Returns the strength, or intensity, of the left faded edge. The strength is
10986 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10987 * returns 0.0 or 1.0 but no value in between.
10988 *
10989 * Subclasses should override this method to provide a smoother fade transition
10990 * when scrolling occurs.
10991 *
10992 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10993 */
10994 protected float getLeftFadingEdgeStrength() {
10995 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10996 }
10997
10998 /**
10999 * Returns the strength, or intensity, of the right faded edge. The strength is
11000 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11001 * returns 0.0 or 1.0 but no value in between.
11002 *
11003 * Subclasses should override this method to provide a smoother fade transition
11004 * when scrolling occurs.
11005 *
11006 * @return the intensity of the right fade as a float between 0.0f and 1.0f
11007 */
11008 protected float getRightFadingEdgeStrength() {
11009 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
11010 computeHorizontalScrollRange() ? 1.0f : 0.0f;
11011 }
11012
11013 /**
11014 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
11015 * scrollbar is not drawn by default.</p>
11016 *
11017 * @return true if the horizontal scrollbar should be painted, false
11018 * otherwise
11019 *
11020 * @see #setHorizontalScrollBarEnabled(boolean)
11021 */
11022 public boolean isHorizontalScrollBarEnabled() {
11023 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11024 }
11025
11026 /**
11027 * <p>Define whether the horizontal scrollbar should be drawn or not. The
11028 * scrollbar is not drawn by default.</p>
11029 *
11030 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
11031 * be painted
11032 *
11033 * @see #isHorizontalScrollBarEnabled()
11034 */
11035 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
11036 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
11037 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011038 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011039 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 }
11041 }
11042
11043 /**
11044 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
11045 * scrollbar is not drawn by default.</p>
11046 *
11047 * @return true if the vertical scrollbar should be painted, false
11048 * otherwise
11049 *
11050 * @see #setVerticalScrollBarEnabled(boolean)
11051 */
11052 public boolean isVerticalScrollBarEnabled() {
11053 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11054 }
11055
11056 /**
11057 * <p>Define whether the vertical scrollbar should be drawn or not. The
11058 * scrollbar is not drawn by default.</p>
11059 *
11060 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11061 * be painted
11062 *
11063 * @see #isVerticalScrollBarEnabled()
11064 */
11065 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11066 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11067 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011068 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011069 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 }
11071 }
11072
Adam Powell20232d02010-12-08 21:08:53 -080011073 /**
11074 * @hide
11075 */
11076 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011077 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078 }
Joe Malin32736f02011-01-19 16:14:20 -080011079
Mike Cleronfe81d382009-09-28 14:22:16 -070011080 /**
11081 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011082 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011083 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011084 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011085 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011086 */
11087 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11088 initScrollCache();
11089 final ScrollabilityCache scrollabilityCache = mScrollCache;
11090 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011091 if (fadeScrollbars) {
11092 scrollabilityCache.state = ScrollabilityCache.OFF;
11093 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011094 scrollabilityCache.state = ScrollabilityCache.ON;
11095 }
11096 }
Joe Malin32736f02011-01-19 16:14:20 -080011097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011098 /**
Joe Malin32736f02011-01-19 16:14:20 -080011099 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011100 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011101 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011102 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011103 *
11104 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011105 */
11106 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011107 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011108 }
Joe Malin32736f02011-01-19 16:14:20 -080011109
Mike Cleron52f0a642009-09-28 18:21:37 -070011110 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011111 *
11112 * Returns the delay before scrollbars fade.
11113 *
11114 * @return the delay before scrollbars fade
11115 *
11116 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11117 */
11118 public int getScrollBarDefaultDelayBeforeFade() {
11119 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11120 mScrollCache.scrollBarDefaultDelayBeforeFade;
11121 }
11122
11123 /**
11124 * Define the delay before scrollbars fade.
11125 *
11126 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11127 *
11128 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11129 */
11130 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11131 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11132 }
11133
11134 /**
11135 *
11136 * Returns the scrollbar fade duration.
11137 *
11138 * @return the scrollbar fade duration
11139 *
11140 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11141 */
11142 public int getScrollBarFadeDuration() {
11143 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11144 mScrollCache.scrollBarFadeDuration;
11145 }
11146
11147 /**
11148 * Define the scrollbar fade duration.
11149 *
11150 * @param scrollBarFadeDuration - the scrollbar fade duration
11151 *
11152 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11153 */
11154 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11155 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11156 }
11157
11158 /**
11159 *
11160 * Returns the scrollbar size.
11161 *
11162 * @return the scrollbar size
11163 *
11164 * @attr ref android.R.styleable#View_scrollbarSize
11165 */
11166 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011167 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011168 mScrollCache.scrollBarSize;
11169 }
11170
11171 /**
11172 * Define the scrollbar size.
11173 *
11174 * @param scrollBarSize - the scrollbar size
11175 *
11176 * @attr ref android.R.styleable#View_scrollbarSize
11177 */
11178 public void setScrollBarSize(int scrollBarSize) {
11179 getScrollCache().scrollBarSize = scrollBarSize;
11180 }
11181
11182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11184 * inset. When inset, they add to the padding of the view. And the scrollbars
11185 * can be drawn inside the padding area or on the edge of the view. For example,
11186 * if a view has a background drawable and you want to draw the scrollbars
11187 * inside the padding specified by the drawable, you can use
11188 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11189 * appear at the edge of the view, ignoring the padding, then you can use
11190 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11191 * @param style the style of the scrollbars. Should be one of
11192 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11193 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11194 * @see #SCROLLBARS_INSIDE_OVERLAY
11195 * @see #SCROLLBARS_INSIDE_INSET
11196 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11197 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011198 *
11199 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011200 */
11201 public void setScrollBarStyle(int style) {
11202 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11203 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011204 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011205 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 }
11207 }
11208
11209 /**
11210 * <p>Returns the current scrollbar style.</p>
11211 * @return the current scrollbar style
11212 * @see #SCROLLBARS_INSIDE_OVERLAY
11213 * @see #SCROLLBARS_INSIDE_INSET
11214 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11215 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011216 *
11217 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011219 @ViewDebug.ExportedProperty(mapping = {
11220 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11221 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11222 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11223 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11224 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 public int getScrollBarStyle() {
11226 return mViewFlags & SCROLLBARS_STYLE_MASK;
11227 }
11228
11229 /**
11230 * <p>Compute the horizontal range that the horizontal scrollbar
11231 * represents.</p>
11232 *
11233 * <p>The range is expressed in arbitrary units that must be the same as the
11234 * units used by {@link #computeHorizontalScrollExtent()} and
11235 * {@link #computeHorizontalScrollOffset()}.</p>
11236 *
11237 * <p>The default range is the drawing width of this view.</p>
11238 *
11239 * @return the total horizontal range represented by the horizontal
11240 * scrollbar
11241 *
11242 * @see #computeHorizontalScrollExtent()
11243 * @see #computeHorizontalScrollOffset()
11244 * @see android.widget.ScrollBarDrawable
11245 */
11246 protected int computeHorizontalScrollRange() {
11247 return getWidth();
11248 }
11249
11250 /**
11251 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11252 * within the horizontal range. This value is used to compute the position
11253 * of the thumb within the scrollbar's track.</p>
11254 *
11255 * <p>The range is expressed in arbitrary units that must be the same as the
11256 * units used by {@link #computeHorizontalScrollRange()} and
11257 * {@link #computeHorizontalScrollExtent()}.</p>
11258 *
11259 * <p>The default offset is the scroll offset of this view.</p>
11260 *
11261 * @return the horizontal offset of the scrollbar's thumb
11262 *
11263 * @see #computeHorizontalScrollRange()
11264 * @see #computeHorizontalScrollExtent()
11265 * @see android.widget.ScrollBarDrawable
11266 */
11267 protected int computeHorizontalScrollOffset() {
11268 return mScrollX;
11269 }
11270
11271 /**
11272 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11273 * within the horizontal range. This value is used to compute the length
11274 * of the thumb within the scrollbar's track.</p>
11275 *
11276 * <p>The range is expressed in arbitrary units that must be the same as the
11277 * units used by {@link #computeHorizontalScrollRange()} and
11278 * {@link #computeHorizontalScrollOffset()}.</p>
11279 *
11280 * <p>The default extent is the drawing width of this view.</p>
11281 *
11282 * @return the horizontal extent of the scrollbar's thumb
11283 *
11284 * @see #computeHorizontalScrollRange()
11285 * @see #computeHorizontalScrollOffset()
11286 * @see android.widget.ScrollBarDrawable
11287 */
11288 protected int computeHorizontalScrollExtent() {
11289 return getWidth();
11290 }
11291
11292 /**
11293 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11294 *
11295 * <p>The range is expressed in arbitrary units that must be the same as the
11296 * units used by {@link #computeVerticalScrollExtent()} and
11297 * {@link #computeVerticalScrollOffset()}.</p>
11298 *
11299 * @return the total vertical range represented by the vertical scrollbar
11300 *
11301 * <p>The default range is the drawing height of this view.</p>
11302 *
11303 * @see #computeVerticalScrollExtent()
11304 * @see #computeVerticalScrollOffset()
11305 * @see android.widget.ScrollBarDrawable
11306 */
11307 protected int computeVerticalScrollRange() {
11308 return getHeight();
11309 }
11310
11311 /**
11312 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11313 * within the horizontal range. This value is used to compute the position
11314 * of the thumb within the scrollbar's track.</p>
11315 *
11316 * <p>The range is expressed in arbitrary units that must be the same as the
11317 * units used by {@link #computeVerticalScrollRange()} and
11318 * {@link #computeVerticalScrollExtent()}.</p>
11319 *
11320 * <p>The default offset is the scroll offset of this view.</p>
11321 *
11322 * @return the vertical offset of the scrollbar's thumb
11323 *
11324 * @see #computeVerticalScrollRange()
11325 * @see #computeVerticalScrollExtent()
11326 * @see android.widget.ScrollBarDrawable
11327 */
11328 protected int computeVerticalScrollOffset() {
11329 return mScrollY;
11330 }
11331
11332 /**
11333 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11334 * within the vertical range. This value is used to compute the length
11335 * of the thumb within the scrollbar's track.</p>
11336 *
11337 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011338 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011339 * {@link #computeVerticalScrollOffset()}.</p>
11340 *
11341 * <p>The default extent is the drawing height of this view.</p>
11342 *
11343 * @return the vertical extent of the scrollbar's thumb
11344 *
11345 * @see #computeVerticalScrollRange()
11346 * @see #computeVerticalScrollOffset()
11347 * @see android.widget.ScrollBarDrawable
11348 */
11349 protected int computeVerticalScrollExtent() {
11350 return getHeight();
11351 }
11352
11353 /**
Adam Powell69159442011-06-13 17:53:06 -070011354 * Check if this view can be scrolled horizontally in a certain direction.
11355 *
11356 * @param direction Negative to check scrolling left, positive to check scrolling right.
11357 * @return true if this view can be scrolled in the specified direction, false otherwise.
11358 */
11359 public boolean canScrollHorizontally(int direction) {
11360 final int offset = computeHorizontalScrollOffset();
11361 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11362 if (range == 0) return false;
11363 if (direction < 0) {
11364 return offset > 0;
11365 } else {
11366 return offset < range - 1;
11367 }
11368 }
11369
11370 /**
11371 * Check if this view can be scrolled vertically in a certain direction.
11372 *
11373 * @param direction Negative to check scrolling up, positive to check scrolling down.
11374 * @return true if this view can be scrolled in the specified direction, false otherwise.
11375 */
11376 public boolean canScrollVertically(int direction) {
11377 final int offset = computeVerticalScrollOffset();
11378 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11379 if (range == 0) return false;
11380 if (direction < 0) {
11381 return offset > 0;
11382 } else {
11383 return offset < range - 1;
11384 }
11385 }
11386
11387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11389 * scrollbars are painted only if they have been awakened first.</p>
11390 *
11391 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011392 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011393 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011395 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 // scrollbars are drawn only when the animation is running
11397 final ScrollabilityCache cache = mScrollCache;
11398 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011399
Mike Cleronf116bf82009-09-27 19:14:12 -070011400 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011401
Mike Cleronf116bf82009-09-27 19:14:12 -070011402 if (state == ScrollabilityCache.OFF) {
11403 return;
11404 }
Joe Malin32736f02011-01-19 16:14:20 -080011405
Mike Cleronf116bf82009-09-27 19:14:12 -070011406 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011407
Mike Cleronf116bf82009-09-27 19:14:12 -070011408 if (state == ScrollabilityCache.FADING) {
11409 // We're fading -- get our fade interpolation
11410 if (cache.interpolatorValues == null) {
11411 cache.interpolatorValues = new float[1];
11412 }
Joe Malin32736f02011-01-19 16:14:20 -080011413
Mike Cleronf116bf82009-09-27 19:14:12 -070011414 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011415
Mike Cleronf116bf82009-09-27 19:14:12 -070011416 // Stops the animation if we're done
11417 if (cache.scrollBarInterpolator.timeToValues(values) ==
11418 Interpolator.Result.FREEZE_END) {
11419 cache.state = ScrollabilityCache.OFF;
11420 } else {
11421 cache.scrollBar.setAlpha(Math.round(values[0]));
11422 }
Joe Malin32736f02011-01-19 16:14:20 -080011423
11424 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011425 // drawing. We only want this when we're fading so that
11426 // we prevent excessive redraws
11427 invalidate = true;
11428 } else {
11429 // We're just on -- but we may have been fading before so
11430 // reset alpha
11431 cache.scrollBar.setAlpha(255);
11432 }
11433
Joe Malin32736f02011-01-19 16:14:20 -080011434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011435 final int viewFlags = mViewFlags;
11436
11437 final boolean drawHorizontalScrollBar =
11438 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11439 final boolean drawVerticalScrollBar =
11440 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11441 && !isVerticalScrollBarHidden();
11442
11443 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11444 final int width = mRight - mLeft;
11445 final int height = mBottom - mTop;
11446
11447 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448
Mike Reede8853fc2009-09-04 14:01:48 -040011449 final int scrollX = mScrollX;
11450 final int scrollY = mScrollY;
11451 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11452
Mike Cleronf116bf82009-09-27 19:14:12 -070011453 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011456 int size = scrollBar.getSize(false);
11457 if (size <= 0) {
11458 size = cache.scrollBarSize;
11459 }
11460
Mike Cleronf116bf82009-09-27 19:14:12 -070011461 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011462 computeHorizontalScrollOffset(),
11463 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011464 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011465 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011466 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011467 left = scrollX + (mPaddingLeft & inside);
11468 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11469 bottom = top + size;
11470 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11471 if (invalidate) {
11472 invalidate(left, top, right, bottom);
11473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011474 }
11475
11476 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011477 int size = scrollBar.getSize(true);
11478 if (size <= 0) {
11479 size = cache.scrollBarSize;
11480 }
11481
Mike Reede8853fc2009-09-04 14:01:48 -040011482 scrollBar.setParameters(computeVerticalScrollRange(),
11483 computeVerticalScrollOffset(),
11484 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011485 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11486 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11487 verticalScrollbarPosition = isLayoutRtl() ?
11488 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11489 }
11490 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011491 default:
Adam Powell20232d02010-12-08 21:08:53 -080011492 case SCROLLBAR_POSITION_RIGHT:
11493 left = scrollX + width - size - (mUserPaddingRight & inside);
11494 break;
11495 case SCROLLBAR_POSITION_LEFT:
11496 left = scrollX + (mUserPaddingLeft & inside);
11497 break;
11498 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011499 top = scrollY + (mPaddingTop & inside);
11500 right = left + size;
11501 bottom = scrollY + height - (mUserPaddingBottom & inside);
11502 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11503 if (invalidate) {
11504 invalidate(left, top, right, bottom);
11505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011506 }
11507 }
11508 }
11509 }
Romain Guy8506ab42009-06-11 17:35:47 -070011510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011512 * Override this if the vertical scrollbar needs to be hidden in a subclass, like when
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 * FastScroller is visible.
11514 * @return whether to temporarily hide the vertical scrollbar
11515 * @hide
11516 */
11517 protected boolean isVerticalScrollBarHidden() {
11518 return false;
11519 }
11520
11521 /**
11522 * <p>Draw the horizontal scrollbar if
11523 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11524 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011525 * @param canvas the canvas on which to draw the scrollbar
11526 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011527 *
11528 * @see #isHorizontalScrollBarEnabled()
11529 * @see #computeHorizontalScrollRange()
11530 * @see #computeHorizontalScrollExtent()
11531 * @see #computeHorizontalScrollOffset()
11532 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011533 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011534 */
Romain Guy8fb95422010-08-17 18:38:51 -070011535 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11536 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011537 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011538 scrollBar.draw(canvas);
11539 }
Mike Reede8853fc2009-09-04 14:01:48 -040011540
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011542 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11543 * returns true.</p>
11544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 * @param canvas the canvas on which to draw the scrollbar
11546 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 *
11548 * @see #isVerticalScrollBarEnabled()
11549 * @see #computeVerticalScrollRange()
11550 * @see #computeVerticalScrollExtent()
11551 * @see #computeVerticalScrollOffset()
11552 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011553 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011554 */
Romain Guy8fb95422010-08-17 18:38:51 -070011555 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11556 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011557 scrollBar.setBounds(l, t, r, b);
11558 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 }
11560
11561 /**
11562 * Implement this to do your drawing.
11563 *
11564 * @param canvas the canvas on which the background will be drawn
11565 */
11566 protected void onDraw(Canvas canvas) {
11567 }
11568
11569 /*
11570 * Caller is responsible for calling requestLayout if necessary.
11571 * (This allows addViewInLayout to not request a new layout.)
11572 */
11573 void assignParent(ViewParent parent) {
11574 if (mParent == null) {
11575 mParent = parent;
11576 } else if (parent == null) {
11577 mParent = null;
11578 } else {
11579 throw new RuntimeException("view " + this + " being added, but"
11580 + " it already has a parent");
11581 }
11582 }
11583
11584 /**
11585 * This is called when the view is attached to a window. At this point it
11586 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011587 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11588 * however it may be called any time before the first onDraw -- including
11589 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011590 *
11591 * @see #onDetachedFromWindow()
11592 */
11593 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011594 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011595 mParent.requestTransparentRegion(this);
11596 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011597
Dianne Hackborn4702a852012-08-17 15:18:29 -070011598 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011599 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011600 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011601 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011602
Chet Haasea9b61ac2010-12-20 07:40:25 -080011603 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011604
Svetoslav Ganov42138042012-03-20 11:51:39 -070011605 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011606 if (isFocused()) {
11607 InputMethodManager imm = InputMethodManager.peekInstance();
11608 imm.focusIn(this);
11609 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011610
Romain Guyfbb93fa2012-12-03 18:50:35 -080011611 if (mDisplayList != null) {
11612 mDisplayList.setDirty(false);
Romain Guy2a0f2282012-05-08 14:43:12 -070011613 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011614 }
Cibu Johny86666632010-02-22 13:01:02 -080011615
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011616 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011617 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011618 *
11619 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011620 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011621 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011622 if (!needRtlPropertiesResolution()) return;
11623
11624 // Order is important here: LayoutDirection MUST be resolved first
11625 if (!isLayoutDirectionResolved()) {
11626 resolveLayoutDirection();
11627 resolveLayoutParams();
11628 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011629 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011630 if (!isTextDirectionResolved()) {
11631 resolveTextDirection();
11632 }
11633 if (!isTextAlignmentResolved()) {
11634 resolveTextAlignment();
11635 }
11636 if (!isPaddingResolved()) {
11637 resolvePadding();
11638 }
11639 if (!isDrawablesResolved()) {
11640 resolveDrawables();
11641 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011642 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011643 }
11644
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011645 /**
11646 * Reset resolution of all RTL related properties.
11647 *
11648 * @hide
11649 */
11650 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011651 resetResolvedLayoutDirection();
11652 resetResolvedTextDirection();
11653 resetResolvedTextAlignment();
11654 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011655 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011656 }
11657
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011658 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011659 * @see #onScreenStateChanged(int)
11660 */
11661 void dispatchScreenStateChanged(int screenState) {
11662 onScreenStateChanged(screenState);
11663 }
11664
11665 /**
11666 * This method is called whenever the state of the screen this view is
11667 * attached to changes. A state change will usually occurs when the screen
11668 * turns on or off (whether it happens automatically or the user does it
11669 * manually.)
11670 *
11671 * @param screenState The new state of the screen. Can be either
11672 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11673 */
11674 public void onScreenStateChanged(int screenState) {
11675 }
11676
11677 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011678 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11679 */
11680 private boolean hasRtlSupport() {
11681 return mContext.getApplicationInfo().hasRtlSupport();
11682 }
11683
11684 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011685 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11686 * RTL not supported)
11687 */
11688 private boolean isRtlCompatibilityMode() {
11689 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11690 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11691 }
11692
11693 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011694 * @return true if RTL properties need resolution.
11695 */
11696 private boolean needRtlPropertiesResolution() {
11697 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11698 }
11699
11700 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011701 * Called when any RTL property (layout direction or text direction or text alignment) has
11702 * been changed.
11703 *
11704 * Subclasses need to override this method to take care of cached information that depends on the
11705 * resolved layout direction, or to inform child views that inherit their layout direction.
11706 *
11707 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011708 *
11709 * @param layoutDirection the direction of the layout
11710 *
11711 * @see #LAYOUT_DIRECTION_LTR
11712 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011713 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011714 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011715 }
11716
11717 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011718 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11719 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011720 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011721 * @return true if resolution has been done, false otherwise.
11722 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011723 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011724 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011725 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011726 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011727 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011728
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011729 if (hasRtlSupport()) {
11730 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011731 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11732 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011733 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011734 // We cannot resolve yet. LTR is by default and let the resolution happen again
11735 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011736 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011737
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011738 View parent = ((View) mParent);
11739 // Parent has not yet resolved, LTR is still the default
11740 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011741
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011742 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011743 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011744 }
11745 break;
11746 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011747 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011748 break;
11749 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011750 if((LAYOUT_DIRECTION_RTL ==
11751 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011752 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011753 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011754 break;
11755 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011756 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011757 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011758 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011759
11760 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011761 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011762 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011763 }
11764
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011765 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011766 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011767 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011768 * @return true if layout direction resolution can be done otherwise return false.
11769 *
11770 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011771 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011772 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011773 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011774 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011775 return (mParent != null) && (mParent instanceof ViewGroup) &&
11776 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011777 default:
11778 return true;
11779 }
11780 }
11781
11782 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011783 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11784 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011785 *
11786 * @hide
11787 */
11788 public void resetResolvedLayoutDirection() {
11789 // Reset the current resolved bits
11790 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011791 }
11792
11793 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011794 * @return true if the layout direction is inherited.
11795 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011796 * @hide
11797 */
11798 public boolean isLayoutDirectionInherited() {
11799 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11800 }
11801
11802 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011803 * @return true if layout direction has been resolved.
11804 */
11805 private boolean isLayoutDirectionResolved() {
11806 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11807 }
11808
11809 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011810 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011811 *
11812 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011813 */
11814 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011815 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011816 }
11817
11818 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011819 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011820 *
11821 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011822 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011823 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011824 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011825 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11826 // If start / end padding are defined, they will be resolved (hence overriding) to
11827 // left / right or right / left depending on the resolved layout direction.
11828 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011829 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011830 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011831 mUserPaddingLeft = mUserPaddingLeftInitial;
11832 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011833 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011834 switch (resolvedLayoutDirection) {
11835 case LAYOUT_DIRECTION_RTL:
11836 if (mUserPaddingStart != UNDEFINED_PADDING) {
11837 mUserPaddingRight = mUserPaddingStart;
11838 }
11839 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11840 mUserPaddingLeft = mUserPaddingEnd;
11841 }
11842 break;
11843 case LAYOUT_DIRECTION_LTR:
11844 default:
11845 if (mUserPaddingStart != UNDEFINED_PADDING) {
11846 mUserPaddingLeft = mUserPaddingStart;
11847 }
11848 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11849 mUserPaddingRight = mUserPaddingEnd;
11850 }
11851 }
11852
11853 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11854
11855 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11856 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011857 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011858 }
11859
Dianne Hackborn4702a852012-08-17 15:18:29 -070011860 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011861 }
11862
11863 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011864 * Reset the resolved layout direction.
11865 *
11866 * @hide
11867 */
11868 public void resetResolvedPadding() {
11869 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11870 }
11871
11872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 * This is called when the view is detached from a window. At this point it
11874 * no longer has a surface for drawing.
11875 *
11876 * @see #onAttachedToWindow()
11877 */
11878 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011879 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011880
Romain Guya440b002010-02-24 15:57:54 -080011881 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011882 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011883 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011884 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011886 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011887
Romain Guya998dff2012-03-23 18:58:36 -070011888 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011889
11890 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011891 if (mDisplayList != null) {
Romain Guyfbb93fa2012-12-03 18:50:35 -080011892 mDisplayList.setDirty(true);
Romain Guy2a0f2282012-05-08 14:43:12 -070011893 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011894 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011895 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011896 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011897 // Should never happen
11898 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011899 }
11900
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011901 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011902
Svetoslav Ganov42138042012-03-20 11:51:39 -070011903 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 }
11905
11906 /**
11907 * @return The number of times this view has been attached to a window
11908 */
11909 protected int getWindowAttachCount() {
11910 return mWindowAttachCount;
11911 }
11912
11913 /**
11914 * Retrieve a unique token identifying the window this view is attached to.
11915 * @return Return the window's token for use in
11916 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11917 */
11918 public IBinder getWindowToken() {
11919 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11920 }
11921
11922 /**
11923 * Retrieve a unique token identifying the top-level "real" window of
11924 * the window that this view is attached to. That is, this is like
11925 * {@link #getWindowToken}, except if the window this view in is a panel
11926 * window (attached to another containing window), then the token of
11927 * the containing window is returned instead.
11928 *
11929 * @return Returns the associated window token, either
11930 * {@link #getWindowToken()} or the containing window's token.
11931 */
11932 public IBinder getApplicationWindowToken() {
11933 AttachInfo ai = mAttachInfo;
11934 if (ai != null) {
11935 IBinder appWindowToken = ai.mPanelParentWindowToken;
11936 if (appWindowToken == null) {
11937 appWindowToken = ai.mWindowToken;
11938 }
11939 return appWindowToken;
11940 }
11941 return null;
11942 }
11943
11944 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011945 * Gets the logical display to which the view's window has been attached.
11946 *
11947 * @return The logical display, or null if the view is not currently attached to a window.
11948 */
11949 public Display getDisplay() {
11950 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11951 }
11952
11953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011954 * Retrieve private session object this view hierarchy is using to
11955 * communicate with the window manager.
11956 * @return the session object to communicate with the window manager
11957 */
11958 /*package*/ IWindowSession getWindowSession() {
11959 return mAttachInfo != null ? mAttachInfo.mSession : null;
11960 }
11961
11962 /**
11963 * @param info the {@link android.view.View.AttachInfo} to associated with
11964 * this view
11965 */
11966 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11967 //System.out.println("Attached! " + this);
11968 mAttachInfo = info;
11969 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011970 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011971 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011972 if (mFloatingTreeObserver != null) {
11973 info.mTreeObserver.merge(mFloatingTreeObserver);
11974 mFloatingTreeObserver = null;
11975 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011976 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011977 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011978 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011979 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011980 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011981 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011982
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011983 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011984 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011985 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011986 if (listeners != null && listeners.size() > 0) {
11987 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11988 // perform the dispatching. The iterator is a safe guard against listeners that
11989 // could mutate the list by calling the various add/remove methods. This prevents
11990 // the array from being modified while we iterate it.
11991 for (OnAttachStateChangeListener listener : listeners) {
11992 listener.onViewAttachedToWindow(this);
11993 }
11994 }
11995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 int vis = info.mWindowVisibility;
11997 if (vis != GONE) {
11998 onWindowVisibilityChanged(vis);
11999 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070012000 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012001 // If nobody has evaluated the drawable state yet, then do it now.
12002 refreshDrawableState();
12003 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070012004 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012005 }
12006
12007 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012008 AttachInfo info = mAttachInfo;
12009 if (info != null) {
12010 int vis = info.mWindowVisibility;
12011 if (vis != GONE) {
12012 onWindowVisibilityChanged(GONE);
12013 }
12014 }
12015
12016 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080012017
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012018 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080012019 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012020 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080012021 if (listeners != null && listeners.size() > 0) {
12022 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
12023 // perform the dispatching. The iterator is a safe guard against listeners that
12024 // could mutate the list by calling the various add/remove methods. This prevents
12025 // the array from being modified while we iterate it.
12026 for (OnAttachStateChangeListener listener : listeners) {
12027 listener.onViewDetachedFromWindow(this);
12028 }
12029 }
12030
Dianne Hackborn4702a852012-08-17 15:18:29 -070012031 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012033 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012034 }
Romain Guy01d5edc2011-01-28 11:28:53 -080012035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012036 mAttachInfo = null;
12037 }
12038
12039 /**
12040 * Store this view hierarchy's frozen state into the given container.
12041 *
12042 * @param container The SparseArray in which to save the view's state.
12043 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012044 * @see #restoreHierarchyState(android.util.SparseArray)
12045 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12046 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012047 */
12048 public void saveHierarchyState(SparseArray<Parcelable> container) {
12049 dispatchSaveInstanceState(container);
12050 }
12051
12052 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012053 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12054 * this view and its children. May be overridden to modify how freezing happens to a
12055 * view's children; for example, some views may want to not store state for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012056 *
12057 * @param container The SparseArray in which to save the view's state.
12058 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012059 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12060 * @see #saveHierarchyState(android.util.SparseArray)
12061 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012062 */
12063 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12064 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012065 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012067 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068 throw new IllegalStateException(
12069 "Derived class did not call super.onSaveInstanceState()");
12070 }
12071 if (state != null) {
12072 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12073 // + ": " + state);
12074 container.put(mID, state);
12075 }
12076 }
12077 }
12078
12079 /**
12080 * Hook allowing a view to generate a representation of its internal state
12081 * that can later be used to create a new instance with that same state.
12082 * This state should only contain information that is not persistent or can
12083 * not be reconstructed later. For example, you will never store your
12084 * current position on screen because that will be computed again when a
12085 * new instance of the view is placed in its view hierarchy.
12086 * <p>
12087 * Some examples of things you may store here: the current cursor position
12088 * in a text view (but usually not the text itself since that is stored in a
12089 * content provider or other persistent storage), the currently selected
12090 * item in a list view.
12091 *
12092 * @return Returns a Parcelable object containing the view's current dynamic
12093 * state, or null if there is nothing interesting to save. The
12094 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012095 * @see #onRestoreInstanceState(android.os.Parcelable)
12096 * @see #saveHierarchyState(android.util.SparseArray)
12097 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012098 * @see #setSaveEnabled(boolean)
12099 */
12100 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012101 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012102 return BaseSavedState.EMPTY_STATE;
12103 }
12104
12105 /**
12106 * Restore this view hierarchy's frozen state from the given container.
12107 *
12108 * @param container The SparseArray which holds previously frozen states.
12109 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012110 * @see #saveHierarchyState(android.util.SparseArray)
12111 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12112 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 */
12114 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12115 dispatchRestoreInstanceState(container);
12116 }
12117
12118 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012119 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12120 * state for this view and its children. May be overridden to modify how restoring
12121 * happens to a view's children; for example, some views may want to not store state
12122 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 *
12124 * @param container The SparseArray which holds previously saved state.
12125 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012126 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12127 * @see #restoreHierarchyState(android.util.SparseArray)
12128 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012129 */
12130 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12131 if (mID != NO_ID) {
12132 Parcelable state = container.get(mID);
12133 if (state != null) {
12134 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12135 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012136 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012138 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 throw new IllegalStateException(
12140 "Derived class did not call super.onRestoreInstanceState()");
12141 }
12142 }
12143 }
12144 }
12145
12146 /**
12147 * Hook allowing a view to re-apply a representation of its internal state that had previously
12148 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12149 * null state.
12150 *
12151 * @param state The frozen state that had previously been returned by
12152 * {@link #onSaveInstanceState}.
12153 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012154 * @see #onSaveInstanceState()
12155 * @see #restoreHierarchyState(android.util.SparseArray)
12156 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 */
12158 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012159 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012160 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012161 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12162 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012163 + "when two views of different type have the same id in the same hierarchy. "
12164 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012165 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012166 }
12167 }
12168
12169 /**
12170 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12171 *
12172 * @return the drawing start time in milliseconds
12173 */
12174 public long getDrawingTime() {
12175 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12176 }
12177
12178 /**
12179 * <p>Enables or disables the duplication of the parent's state into this view. When
12180 * duplication is enabled, this view gets its drawable state from its parent rather
12181 * than from its own internal properties.</p>
12182 *
12183 * <p>Note: in the current implementation, setting this property to true after the
12184 * view was added to a ViewGroup might have no effect at all. This property should
12185 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12186 *
12187 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12188 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012189 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012190 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12191 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192 *
12193 * @param enabled True to enable duplication of the parent's drawable state, false
12194 * to disable it.
12195 *
12196 * @see #getDrawableState()
12197 * @see #isDuplicateParentStateEnabled()
12198 */
12199 public void setDuplicateParentStateEnabled(boolean enabled) {
12200 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12201 }
12202
12203 /**
12204 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12205 *
12206 * @return True if this view's drawable state is duplicated from the parent,
12207 * false otherwise
12208 *
12209 * @see #getDrawableState()
12210 * @see #setDuplicateParentStateEnabled(boolean)
12211 */
12212 public boolean isDuplicateParentStateEnabled() {
12213 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12214 }
12215
12216 /**
Romain Guy171c5922011-01-06 10:04:23 -080012217 * <p>Specifies the type of layer backing this view. The layer can be
12218 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12219 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012220 *
Romain Guy171c5922011-01-06 10:04:23 -080012221 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12222 * instance that controls how the layer is composed on screen. The following
12223 * properties of the paint are taken into account when composing the layer:</p>
12224 * <ul>
12225 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12226 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12227 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12228 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012229 *
Romain Guy171c5922011-01-06 10:04:23 -080012230 * <p>If this view has an alpha value set to < 1.0 by calling
12231 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12232 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12233 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012234 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012235 *
Romain Guy171c5922011-01-06 10:04:23 -080012236 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12237 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12238 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012239 *
Chet Haased15ebf22012-09-05 11:40:29 -070012240 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012241 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12242 * {@link #LAYER_TYPE_HARDWARE}
12243 * @param paint The paint used to compose the layer. This argument is optional
12244 * and can be null. It is ignored when the layer type is
12245 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012246 *
12247 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012248 * @see #LAYER_TYPE_NONE
12249 * @see #LAYER_TYPE_SOFTWARE
12250 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012251 * @see #setAlpha(float)
12252 *
Romain Guy171c5922011-01-06 10:04:23 -080012253 * @attr ref android.R.styleable#View_layerType
12254 */
12255 public void setLayerType(int layerType, Paint paint) {
12256 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012257 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012258 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12259 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012260
Romain Guyd6cd5722011-01-17 14:42:41 -080012261 if (layerType == mLayerType) {
12262 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12263 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012264 invalidateParentCaches();
12265 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012266 }
12267 return;
12268 }
Romain Guy171c5922011-01-06 10:04:23 -080012269
12270 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012271 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012272 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012273 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012274 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012275 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012276 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012277 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012278 default:
12279 break;
Romain Guy171c5922011-01-06 10:04:23 -080012280 }
12281
12282 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012283 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12284 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12285 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012286
Romain Guy0fd89bf2011-01-26 15:41:30 -080012287 invalidateParentCaches();
12288 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012289 }
12290
12291 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012292 * Updates the {@link Paint} object used with the current layer (used only if the current
12293 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12294 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12295 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12296 * ensure that the view gets redrawn immediately.
12297 *
12298 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12299 * instance that controls how the layer is composed on screen. The following
12300 * properties of the paint are taken into account when composing the layer:</p>
12301 * <ul>
12302 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12303 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12304 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12305 * </ul>
12306 *
12307 * <p>If this view has an alpha value set to < 1.0 by calling
12308 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12309 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12310 * equivalent to setting a hardware layer on this view and providing a paint with
12311 * the desired alpha value.</p>
12312 *
12313 * @param paint The paint used to compose the layer. This argument is optional
12314 * and can be null. It is ignored when the layer type is
12315 * {@link #LAYER_TYPE_NONE}
12316 *
12317 * @see #setLayerType(int, android.graphics.Paint)
12318 */
12319 public void setLayerPaint(Paint paint) {
12320 int layerType = getLayerType();
12321 if (layerType != LAYER_TYPE_NONE) {
12322 mLayerPaint = paint == null ? new Paint() : paint;
12323 if (layerType == LAYER_TYPE_HARDWARE) {
12324 HardwareLayer layer = getHardwareLayer();
12325 if (layer != null) {
12326 layer.setLayerPaint(paint);
12327 }
12328 invalidateViewProperty(false, false);
12329 } else {
12330 invalidate();
12331 }
12332 }
12333 }
12334
12335 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012336 * Indicates whether this view has a static layer. A view with layer type
12337 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12338 * dynamic.
12339 */
12340 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012341 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012342 }
12343
12344 /**
Romain Guy171c5922011-01-06 10:04:23 -080012345 * Indicates what type of layer is currently associated with this view. By default
12346 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12347 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12348 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012349 *
Romain Guy171c5922011-01-06 10:04:23 -080012350 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12351 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012352 *
12353 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012354 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012355 * @see #LAYER_TYPE_NONE
12356 * @see #LAYER_TYPE_SOFTWARE
12357 * @see #LAYER_TYPE_HARDWARE
12358 */
12359 public int getLayerType() {
12360 return mLayerType;
12361 }
Joe Malin32736f02011-01-19 16:14:20 -080012362
Romain Guy6c319ca2011-01-11 14:29:25 -080012363 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012364 * Forces this view's layer to be created and this view to be rendered
12365 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12366 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012367 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012368 * This method can for instance be used to render a view into its layer before
12369 * starting an animation. If this view is complex, rendering into the layer
12370 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012371 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012372 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012373 *
12374 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012375 */
12376 public void buildLayer() {
12377 if (mLayerType == LAYER_TYPE_NONE) return;
12378
12379 if (mAttachInfo == null) {
12380 throw new IllegalStateException("This view must be attached to a window first");
12381 }
12382
12383 switch (mLayerType) {
12384 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012385 if (mAttachInfo.mHardwareRenderer != null &&
12386 mAttachInfo.mHardwareRenderer.isEnabled() &&
12387 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012388 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012389 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012390 break;
12391 case LAYER_TYPE_SOFTWARE:
12392 buildDrawingCache(true);
12393 break;
12394 }
12395 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012396
Romain Guyf1ae1062011-03-02 18:16:04 -080012397 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012398 * <p>Returns a hardware layer that can be used to draw this view again
12399 * without executing its draw method.</p>
12400 *
12401 * @return A HardwareLayer ready to render, or null if an error occurred.
12402 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012403 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012404 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12405 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012406 return null;
12407 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012408
Romain Guy9c4b79a2011-11-10 19:23:58 -080012409 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012410
12411 final int width = mRight - mLeft;
12412 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012413
Romain Guy6c319ca2011-01-11 14:29:25 -080012414 if (width == 0 || height == 0) {
12415 return null;
12416 }
12417
Dianne Hackborn4702a852012-08-17 15:18:29 -070012418 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012419 if (mHardwareLayer == null) {
12420 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12421 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012422 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012423 } else {
12424 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012425 if (mHardwareLayer.resize(width, height)) {
12426 mLocalDirtyRect.set(0, 0, width, height);
12427 }
Romain Guy846a5332012-07-11 17:44:57 -070012428 }
12429
12430 // This should not be necessary but applications that change
12431 // the parameters of their background drawable without calling
12432 // this.setBackground(Drawable) can leave the view in a bad state
12433 // (for instance isOpaque() returns true, but the background is
12434 // not opaque.)
12435 computeOpaqueFlags();
12436
12437 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012438 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012439 mHardwareLayer.setOpaque(opaque);
12440 mLocalDirtyRect.set(0, 0, width, height);
12441 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012442 }
12443
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012444 // The layer is not valid if the underlying GPU resources cannot be allocated
12445 if (!mHardwareLayer.isValid()) {
12446 return null;
12447 }
12448
Romain Guy11cb6422012-09-21 00:39:43 -070012449 mHardwareLayer.setLayerPaint(mLayerPaint);
12450 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12451 ViewRootImpl viewRoot = getViewRootImpl();
12452 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12453
Michael Jurka7e52caf2012-03-06 15:57:06 -080012454 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012455 }
12456
12457 return mHardwareLayer;
12458 }
Romain Guy171c5922011-01-06 10:04:23 -080012459
Romain Guy589b0bb2011-10-10 13:57:47 -070012460 /**
12461 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012462 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012463 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012464 *
12465 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012466 * @see #LAYER_TYPE_HARDWARE
12467 */
Romain Guya998dff2012-03-23 18:58:36 -070012468 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012469 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012470 AttachInfo info = mAttachInfo;
12471 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012472 info.mHardwareRenderer.isEnabled() &&
12473 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012474 mHardwareLayer.destroy();
12475 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012476
Chet Haase6a2d17f2012-09-30 12:14:13 -070012477 if (mDisplayList != null) {
12478 mDisplayList.reset();
12479 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012480 invalidate(true);
12481 invalidateParentCaches();
12482 }
Romain Guy65b345f2011-07-27 18:51:50 -070012483 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012484 }
Romain Guy65b345f2011-07-27 18:51:50 -070012485 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012486 }
12487
Romain Guy171c5922011-01-06 10:04:23 -080012488 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012489 * Destroys all hardware rendering resources. This method is invoked
12490 * when the system needs to reclaim resources. Upon execution of this
12491 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012492 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012493 * Note: you <strong>must</strong> call
12494 * <code>super.destroyHardwareResources()</code> when overriding
12495 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012496 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012497 * @hide
12498 */
12499 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012500 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012501 }
12502
12503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012504 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12505 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12506 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12507 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12508 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12509 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012510 *
Romain Guy171c5922011-01-06 10:04:23 -080012511 * <p>Enabling the drawing cache is similar to
12512 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012513 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12514 * drawing cache has no effect on rendering because the system uses a different mechanism
12515 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12516 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12517 * for information on how to enable software and hardware layers.</p>
12518 *
12519 * <p>This API can be used to manually generate
12520 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12521 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012522 *
12523 * @param enabled true to enable the drawing cache, false otherwise
12524 *
12525 * @see #isDrawingCacheEnabled()
12526 * @see #getDrawingCache()
12527 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012528 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012529 */
12530 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012531 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12533 }
12534
12535 /**
12536 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12537 *
12538 * @return true if the drawing cache is enabled
12539 *
12540 * @see #setDrawingCacheEnabled(boolean)
12541 * @see #getDrawingCache()
12542 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012543 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 public boolean isDrawingCacheEnabled() {
12545 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12546 }
12547
12548 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012549 * Debugging utility which recursively outputs the dirty state of a view and its
12550 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012551 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012552 * @hide
12553 */
Romain Guy676b1732011-02-14 14:45:33 -080012554 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012555 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012556 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12557 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12558 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12559 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012560 if (clear) {
12561 mPrivateFlags &= clearMask;
12562 }
12563 if (this instanceof ViewGroup) {
12564 ViewGroup parent = (ViewGroup) this;
12565 final int count = parent.getChildCount();
12566 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012567 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012568 child.outputDirtyFlags(indent + " ", clear, clearMask);
12569 }
12570 }
12571 }
12572
12573 /**
12574 * This method is used by ViewGroup to cause its children to restore or recreate their
12575 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12576 * to recreate its own display list, which would happen if it went through the normal
12577 * draw/dispatchDraw mechanisms.
12578 *
12579 * @hide
12580 */
12581 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012582
12583 /**
12584 * A view that is not attached or hardware accelerated cannot create a display list.
12585 * This method checks these conditions and returns the appropriate result.
12586 *
12587 * @return true if view has the ability to create a display list, false otherwise.
12588 *
12589 * @hide
12590 */
12591 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012592 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012593 }
Joe Malin32736f02011-01-19 16:14:20 -080012594
Chet Haasedaf98e92011-01-10 14:10:36 -080012595 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012596 * @return The HardwareRenderer associated with that view or null if hardware rendering
12597 * is not supported or this this has not been attached to a window.
12598 *
12599 * @hide
12600 */
12601 public HardwareRenderer getHardwareRenderer() {
12602 if (mAttachInfo != null) {
12603 return mAttachInfo.mHardwareRenderer;
12604 }
12605 return null;
12606 }
12607
12608 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012609 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12610 * Otherwise, the same display list will be returned (after having been rendered into
12611 * along the way, depending on the invalidation state of the view).
12612 *
12613 * @param displayList The previous version of this displayList, could be null.
12614 * @param isLayer Whether the requester of the display list is a layer. If so,
12615 * the view will avoid creating a layer inside the resulting display list.
12616 * @return A new or reused DisplayList object.
12617 */
12618 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12619 if (!canHaveDisplayList()) {
12620 return null;
12621 }
12622
Dianne Hackborn4702a852012-08-17 15:18:29 -070012623 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012624 displayList == null || !displayList.isValid() ||
12625 (!isLayer && mRecreateDisplayList))) {
12626 // Don't need to recreate the display list, just need to tell our
12627 // children to restore/recreate theirs
12628 if (displayList != null && displayList.isValid() &&
12629 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012630 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12631 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012632 dispatchGetDisplayList();
12633
12634 return displayList;
12635 }
12636
12637 if (!isLayer) {
12638 // If we got here, we're recreating it. Mark it as such to ensure that
12639 // we copy in child display lists into ours in drawChild()
12640 mRecreateDisplayList = true;
12641 }
12642 if (displayList == null) {
12643 final String name = getClass().getSimpleName();
12644 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12645 // If we're creating a new display list, make sure our parent gets invalidated
12646 // since they will need to recreate their display list to account for this
12647 // new child display list.
12648 invalidateParentCaches();
12649 }
12650
12651 boolean caching = false;
12652 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012653 int width = mRight - mLeft;
12654 int height = mBottom - mTop;
12655
12656 try {
12657 canvas.setViewport(width, height);
12658 // The dirty rect should always be null for a display list
12659 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012660 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012661 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012662 if (layerType == LAYER_TYPE_HARDWARE) {
12663 final HardwareLayer layer = getHardwareLayer();
12664 if (layer != null && layer.isValid()) {
12665 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12666 } else {
12667 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12668 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12669 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12670 }
12671 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012672 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012673 buildDrawingCache(true);
12674 Bitmap cache = getDrawingCache(true);
12675 if (cache != null) {
12676 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12677 caching = true;
12678 }
Chet Haasea1cff502012-02-21 13:43:44 -080012679 }
Chet Haasea1cff502012-02-21 13:43:44 -080012680 } else {
12681
12682 computeScroll();
12683
Chet Haasea1cff502012-02-21 13:43:44 -080012684 canvas.translate(-mScrollX, -mScrollY);
12685 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012686 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12687 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012688 }
12689
12690 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012691 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012692 dispatchDraw(canvas);
12693 } else {
12694 draw(canvas);
12695 }
12696 }
12697 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012698 canvas.onPostDraw();
12699
12700 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012701 displayList.setCaching(caching);
12702 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012703 displayList.setLeftTopRightBottom(0, 0, width, height);
12704 } else {
12705 setDisplayListProperties(displayList);
12706 }
12707 }
12708 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012709 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12710 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012711 }
12712
12713 return displayList;
12714 }
12715
12716 /**
12717 * Get the DisplayList for the HardwareLayer
12718 *
12719 * @param layer The HardwareLayer whose DisplayList we want
12720 * @return A DisplayList fopr the specified HardwareLayer
12721 */
12722 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12723 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12724 layer.setDisplayList(displayList);
12725 return displayList;
12726 }
12727
12728
12729 /**
Romain Guyb051e892010-09-28 19:09:36 -070012730 * <p>Returns a display list that can be used to draw this view again
12731 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012732 *
Romain Guyb051e892010-09-28 19:09:36 -070012733 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012734 *
12735 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012736 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012737 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012738 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012739 return mDisplayList;
12740 }
12741
Romain Guy38c2ece2012-05-24 14:20:56 -070012742 private void clearDisplayList() {
12743 if (mDisplayList != null) {
12744 mDisplayList.invalidate();
12745 mDisplayList.clear();
12746 }
12747 }
12748
Romain Guyb051e892010-09-28 19:09:36 -070012749 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012750 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012751 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012752 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012753 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012754 * @see #getDrawingCache(boolean)
12755 */
12756 public Bitmap getDrawingCache() {
12757 return getDrawingCache(false);
12758 }
12759
12760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012761 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12762 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12763 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12764 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12765 * request the drawing cache by calling this method and draw it on screen if the
12766 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012767 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012768 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12769 * this method will create a bitmap of the same size as this view. Because this bitmap
12770 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12771 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12772 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12773 * size than the view. This implies that your application must be able to handle this
12774 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012775 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012776 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12777 * the current density of the screen when the application is in compatibility
12778 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012779 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012780 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012781 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 * @see #setDrawingCacheEnabled(boolean)
12783 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012784 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012785 * @see #destroyDrawingCache()
12786 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012787 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012788 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12789 return null;
12790 }
12791 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012792 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793 }
Romain Guy02890fd2010-08-06 17:58:44 -070012794 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012795 }
12796
12797 /**
12798 * <p>Frees the resources used by the drawing cache. If you call
12799 * {@link #buildDrawingCache()} manually without calling
12800 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12801 * should cleanup the cache with this method afterwards.</p>
12802 *
12803 * @see #setDrawingCacheEnabled(boolean)
12804 * @see #buildDrawingCache()
12805 * @see #getDrawingCache()
12806 */
12807 public void destroyDrawingCache() {
12808 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012809 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810 mDrawingCache = null;
12811 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012812 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012813 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012814 mUnscaledDrawingCache = null;
12815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012816 }
12817
12818 /**
12819 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012820 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012821 * view will always be drawn on top of a solid color.
12822 *
12823 * @param color The background color to use for the drawing cache's bitmap
12824 *
12825 * @see #setDrawingCacheEnabled(boolean)
12826 * @see #buildDrawingCache()
12827 * @see #getDrawingCache()
12828 */
12829 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012830 if (color != mDrawingCacheBackgroundColor) {
12831 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012832 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012834 }
12835
12836 /**
12837 * @see #setDrawingCacheBackgroundColor(int)
12838 *
12839 * @return The background color to used for the drawing cache's bitmap
12840 */
12841 public int getDrawingCacheBackgroundColor() {
12842 return mDrawingCacheBackgroundColor;
12843 }
12844
12845 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012846 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012847 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012848 * @see #buildDrawingCache(boolean)
12849 */
12850 public void buildDrawingCache() {
12851 buildDrawingCache(false);
12852 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012853
Romain Guyfbd8f692009-06-26 14:51:58 -070012854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12856 *
12857 * <p>If you call {@link #buildDrawingCache()} manually without calling
12858 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12859 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012860 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012861 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12862 * this method will create a bitmap of the same size as this view. Because this bitmap
12863 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12864 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12865 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12866 * size than the view. This implies that your application must be able to handle this
12867 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012868 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012869 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12870 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012871 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012872 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 *
12874 * @see #getDrawingCache()
12875 * @see #destroyDrawingCache()
12876 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012877 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012878 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012879 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012880 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881
Romain Guy8506ab42009-06-11 17:35:47 -070012882 int width = mRight - mLeft;
12883 int height = mBottom - mTop;
12884
12885 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012886 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012887
Romain Guye1123222009-06-29 14:24:56 -070012888 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012889 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12890 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012892
12893 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012894 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012895 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012896
Romain Guyaf61cc42012-09-30 10:49:15 -070012897 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12898 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012899 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12900 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012901 if (width > 0 && height > 0) {
12902 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12903 + projectedBitmapSize + " bytes, only "
12904 + drawingCacheSize + " available");
12905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012907 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012908 return;
12909 }
12910
12911 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012912 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012913
12914 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 Bitmap.Config quality;
12916 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012917 // Never pick ARGB_4444 because it looks awful
12918 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012919 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12920 case DRAWING_CACHE_QUALITY_AUTO:
12921 quality = Bitmap.Config.ARGB_8888;
12922 break;
12923 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012924 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 break;
12926 case DRAWING_CACHE_QUALITY_HIGH:
12927 quality = Bitmap.Config.ARGB_8888;
12928 break;
12929 default:
12930 quality = Bitmap.Config.ARGB_8888;
12931 break;
12932 }
12933 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012934 // Optimization for translucent windows
12935 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012936 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 }
12938
12939 // Try to cleanup memory
12940 if (bitmap != null) bitmap.recycle();
12941
12942 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012943 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12944 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012945 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012946 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012947 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012948 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012949 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012950 }
Adam Powell26153a32010-11-08 15:22:27 -080012951 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012952 } catch (OutOfMemoryError e) {
12953 // If there is not enough memory to create the bitmap cache, just
12954 // ignore the issue as bitmap caches are not required to draw the
12955 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012956 if (autoScale) {
12957 mDrawingCache = null;
12958 } else {
12959 mUnscaledDrawingCache = null;
12960 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012961 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012962 return;
12963 }
12964
12965 clear = drawingCacheBackgroundColor != 0;
12966 }
12967
12968 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012969 if (attachInfo != null) {
12970 canvas = attachInfo.mCanvas;
12971 if (canvas == null) {
12972 canvas = new Canvas();
12973 }
12974 canvas.setBitmap(bitmap);
12975 // Temporarily clobber the cached Canvas in case one of our children
12976 // is also using a drawing cache. Without this, the children would
12977 // steal the canvas by attaching their own bitmap to it and bad, bad
12978 // thing would happen (invisible views, corrupted drawings, etc.)
12979 attachInfo.mCanvas = null;
12980 } else {
12981 // This case should hopefully never or seldom happen
12982 canvas = new Canvas(bitmap);
12983 }
12984
12985 if (clear) {
12986 bitmap.eraseColor(drawingCacheBackgroundColor);
12987 }
12988
12989 computeScroll();
12990 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012991
Romain Guye1123222009-06-29 14:24:56 -070012992 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012993 final float scale = attachInfo.mApplicationScale;
12994 canvas.scale(scale, scale);
12995 }
Joe Malin32736f02011-01-19 16:14:20 -080012996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012997 canvas.translate(-mScrollX, -mScrollY);
12998
Dianne Hackborn4702a852012-08-17 15:18:29 -070012999 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080013000 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
13001 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013002 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070013003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004
13005 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013006 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13007 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013008 dispatchDraw(canvas);
13009 } else {
13010 draw(canvas);
13011 }
13012
13013 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013014 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013015
13016 if (attachInfo != null) {
13017 // Restore the cached Canvas for our siblings
13018 attachInfo.mCanvas = canvas;
13019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013020 }
13021 }
13022
13023 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013024 * Create a snapshot of the view into a bitmap. We should probably make
13025 * some form of this public, but should think about the API.
13026 */
Romain Guy223ff5c2010-03-02 17:07:47 -080013027 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013028 int width = mRight - mLeft;
13029 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013030
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013031 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070013032 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013033 width = (int) ((width * scale) + 0.5f);
13034 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080013035
Dianne Hackborndde331c2012-08-03 14:01:57 -070013036 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
13037 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013038 if (bitmap == null) {
13039 throw new OutOfMemoryError();
13040 }
13041
Romain Guyc529d8d2011-09-06 15:01:39 -070013042 Resources resources = getResources();
13043 if (resources != null) {
13044 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
13045 }
Joe Malin32736f02011-01-19 16:14:20 -080013046
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013047 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013048 if (attachInfo != null) {
13049 canvas = attachInfo.mCanvas;
13050 if (canvas == null) {
13051 canvas = new Canvas();
13052 }
13053 canvas.setBitmap(bitmap);
13054 // Temporarily clobber the cached Canvas in case one of our children
13055 // is also using a drawing cache. Without this, the children would
13056 // steal the canvas by attaching their own bitmap to it and bad, bad
13057 // things would happen (invisible views, corrupted drawings, etc.)
13058 attachInfo.mCanvas = null;
13059 } else {
13060 // This case should hopefully never or seldom happen
13061 canvas = new Canvas(bitmap);
13062 }
13063
Romain Guy5bcdff42009-05-14 21:27:18 -070013064 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013065 bitmap.eraseColor(backgroundColor);
13066 }
13067
13068 computeScroll();
13069 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013070 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013071 canvas.translate(-mScrollX, -mScrollY);
13072
Romain Guy5bcdff42009-05-14 21:27:18 -070013073 // Temporarily remove the dirty mask
13074 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013075 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013076
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013077 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013078 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013079 dispatchDraw(canvas);
13080 } else {
13081 draw(canvas);
13082 }
13083
Romain Guy5bcdff42009-05-14 21:27:18 -070013084 mPrivateFlags = flags;
13085
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013086 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013087 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013088
13089 if (attachInfo != null) {
13090 // Restore the cached Canvas for our siblings
13091 attachInfo.mCanvas = canvas;
13092 }
Romain Guy8506ab42009-06-11 17:35:47 -070013093
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013094 return bitmap;
13095 }
13096
13097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013098 * Indicates whether this View is currently in edit mode. A View is usually
13099 * in edit mode when displayed within a developer tool. For instance, if
13100 * this View is being drawn by a visual user interface builder, this method
13101 * should return true.
13102 *
13103 * Subclasses should check the return value of this method to provide
13104 * different behaviors if their normal behavior might interfere with the
13105 * host environment. For instance: the class spawns a thread in its
13106 * constructor, the drawing code relies on device-specific features, etc.
13107 *
13108 * This method is usually checked in the drawing code of custom widgets.
13109 *
13110 * @return True if this View is in edit mode, false otherwise.
13111 */
13112 public boolean isInEditMode() {
13113 return false;
13114 }
13115
13116 /**
13117 * If the View draws content inside its padding and enables fading edges,
13118 * it needs to support padding offsets. Padding offsets are added to the
13119 * fading edges to extend the length of the fade so that it covers pixels
13120 * drawn inside the padding.
13121 *
13122 * Subclasses of this class should override this method if they need
13123 * to draw content inside the padding.
13124 *
13125 * @return True if padding offset must be applied, false otherwise.
13126 *
13127 * @see #getLeftPaddingOffset()
13128 * @see #getRightPaddingOffset()
13129 * @see #getTopPaddingOffset()
13130 * @see #getBottomPaddingOffset()
13131 *
13132 * @since CURRENT
13133 */
13134 protected boolean isPaddingOffsetRequired() {
13135 return false;
13136 }
13137
13138 /**
13139 * Amount by which to extend the left fading region. Called only when
13140 * {@link #isPaddingOffsetRequired()} returns true.
13141 *
13142 * @return The left padding offset in pixels.
13143 *
13144 * @see #isPaddingOffsetRequired()
13145 *
13146 * @since CURRENT
13147 */
13148 protected int getLeftPaddingOffset() {
13149 return 0;
13150 }
13151
13152 /**
13153 * Amount by which to extend the right fading region. Called only when
13154 * {@link #isPaddingOffsetRequired()} returns true.
13155 *
13156 * @return The right padding offset in pixels.
13157 *
13158 * @see #isPaddingOffsetRequired()
13159 *
13160 * @since CURRENT
13161 */
13162 protected int getRightPaddingOffset() {
13163 return 0;
13164 }
13165
13166 /**
13167 * Amount by which to extend the top fading region. Called only when
13168 * {@link #isPaddingOffsetRequired()} returns true.
13169 *
13170 * @return The top padding offset in pixels.
13171 *
13172 * @see #isPaddingOffsetRequired()
13173 *
13174 * @since CURRENT
13175 */
13176 protected int getTopPaddingOffset() {
13177 return 0;
13178 }
13179
13180 /**
13181 * Amount by which to extend the bottom fading region. Called only when
13182 * {@link #isPaddingOffsetRequired()} returns true.
13183 *
13184 * @return The bottom padding offset in pixels.
13185 *
13186 * @see #isPaddingOffsetRequired()
13187 *
13188 * @since CURRENT
13189 */
13190 protected int getBottomPaddingOffset() {
13191 return 0;
13192 }
13193
13194 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013195 * @hide
13196 * @param offsetRequired
13197 */
13198 protected int getFadeTop(boolean offsetRequired) {
13199 int top = mPaddingTop;
13200 if (offsetRequired) top += getTopPaddingOffset();
13201 return top;
13202 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013203
Romain Guyf2fc4602011-07-19 15:20:03 -070013204 /**
13205 * @hide
13206 * @param offsetRequired
13207 */
13208 protected int getFadeHeight(boolean offsetRequired) {
13209 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013210 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013211 return mBottom - mTop - mPaddingBottom - padding;
13212 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013213
Romain Guyf2fc4602011-07-19 15:20:03 -070013214 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013215 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013216 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013217 *
Romain Guy2bffd262010-09-12 17:40:02 -070013218 * <p>Even if this method returns true, it does not mean that every call
13219 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13220 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013221 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013222 * window is hardware accelerated,
13223 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13224 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013225 *
Romain Guy2bffd262010-09-12 17:40:02 -070013226 * @return True if the view is attached to a window and the window is
13227 * hardware accelerated; false in any other case.
13228 */
13229 public boolean isHardwareAccelerated() {
13230 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13231 }
Joe Malin32736f02011-01-19 16:14:20 -080013232
Romain Guy2bffd262010-09-12 17:40:02 -070013233 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013234 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13235 * case of an active Animation being run on the view.
13236 */
13237 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13238 Animation a, boolean scalingRequired) {
13239 Transformation invalidationTransform;
13240 final int flags = parent.mGroupFlags;
13241 final boolean initialized = a.isInitialized();
13242 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013243 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013244 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013245 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013246 onAnimationStart();
13247 }
13248
13249 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13250 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13251 if (parent.mInvalidationTransformation == null) {
13252 parent.mInvalidationTransformation = new Transformation();
13253 }
13254 invalidationTransform = parent.mInvalidationTransformation;
13255 a.getTransformation(drawingTime, invalidationTransform, 1f);
13256 } else {
13257 invalidationTransform = parent.mChildTransformation;
13258 }
Romain Guy393a52c2012-05-22 20:21:08 -070013259
Chet Haasebcca79a2012-02-14 08:45:14 -080013260 if (more) {
13261 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013262 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13263 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13264 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13265 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013266 // The child need to draw an animation, potentially offscreen, so
13267 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013268 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013269 parent.invalidate(mLeft, mTop, mRight, mBottom);
13270 }
13271 } else {
13272 if (parent.mInvalidateRegion == null) {
13273 parent.mInvalidateRegion = new RectF();
13274 }
13275 final RectF region = parent.mInvalidateRegion;
13276 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13277 invalidationTransform);
13278
13279 // The child need to draw an animation, potentially offscreen, so
13280 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013281 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013282
13283 final int left = mLeft + (int) region.left;
13284 final int top = mTop + (int) region.top;
13285 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13286 top + (int) (region.height() + .5f));
13287 }
13288 }
13289 return more;
13290 }
13291
Chet Haasea1cff502012-02-21 13:43:44 -080013292 /**
13293 * This method is called by getDisplayList() when a display list is created or re-rendered.
13294 * It sets or resets the current value of all properties on that display list (resetting is
13295 * necessary when a display list is being re-created, because we need to make sure that
13296 * previously-set transform values
13297 */
13298 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013299 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013300 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013301 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013302 if (mParent instanceof ViewGroup) {
13303 displayList.setClipChildren(
13304 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13305 }
Chet Haase9420abd2012-03-29 16:28:32 -070013306 float alpha = 1;
13307 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13308 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13309 ViewGroup parentVG = (ViewGroup) mParent;
13310 final boolean hasTransform =
13311 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13312 if (hasTransform) {
13313 Transformation transform = parentVG.mChildTransformation;
13314 final int transformType = parentVG.mChildTransformation.getTransformationType();
13315 if (transformType != Transformation.TYPE_IDENTITY) {
13316 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13317 alpha = transform.getAlpha();
13318 }
13319 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13320 displayList.setStaticMatrix(transform.getMatrix());
13321 }
13322 }
13323 }
Chet Haasea1cff502012-02-21 13:43:44 -080013324 }
13325 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013326 alpha *= mTransformationInfo.mAlpha;
13327 if (alpha < 1) {
13328 final int multipliedAlpha = (int) (255 * alpha);
13329 if (onSetAlpha(multipliedAlpha)) {
13330 alpha = 1;
13331 }
13332 }
13333 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013334 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13335 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13336 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13337 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013338 if (mTransformationInfo.mCamera == null) {
13339 mTransformationInfo.mCamera = new Camera();
13340 mTransformationInfo.matrix3D = new Matrix();
13341 }
13342 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013343 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013344 displayList.setPivotX(getPivotX());
13345 displayList.setPivotY(getPivotY());
13346 }
Chet Haase9420abd2012-03-29 16:28:32 -070013347 } else if (alpha < 1) {
13348 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013349 }
13350 }
13351 }
13352
Chet Haasebcca79a2012-02-14 08:45:14 -080013353 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013354 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13355 * This draw() method is an implementation detail and is not intended to be overridden or
13356 * to be called from anywhere else other than ViewGroup.drawChild().
13357 */
13358 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013359 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013360 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013361 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013362 final int flags = parent.mGroupFlags;
13363
Chet Haasea1cff502012-02-21 13:43:44 -080013364 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013365 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013366 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013367 }
13368
13369 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013370 boolean concatMatrix = false;
13371
13372 boolean scalingRequired = false;
13373 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013374 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013375
13376 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013377 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13378 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013379 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013380 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013381 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13382 } else {
13383 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13384 }
13385
Chet Haasebcca79a2012-02-14 08:45:14 -080013386 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013387 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013388 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013389 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013390 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013391 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013392 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013393 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013394 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013395 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013396 mDisplayList != null) {
13397 // No longer animating: clear out old animation matrix
13398 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013399 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013400 }
13401 if (!useDisplayListProperties &&
13402 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13403 final boolean hasTransform =
13404 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13405 if (hasTransform) {
13406 final int transformType = parent.mChildTransformation.getTransformationType();
13407 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13408 parent.mChildTransformation : null;
13409 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13410 }
Chet Haase64a48c12012-02-13 16:33:29 -080013411 }
13412 }
13413
13414 concatMatrix |= !childHasIdentityMatrix;
13415
13416 // Sets the flag as early as possible to allow draw() implementations
13417 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013418 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013419
Romain Guyfbb43212012-08-30 15:19:27 -070013420 if (!concatMatrix &&
13421 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13422 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013423 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013424 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13425 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013426 return more;
13427 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013428 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013429
13430 if (hardwareAccelerated) {
13431 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13432 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013433 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13434 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013435 }
13436
Chet Haase64a48c12012-02-13 16:33:29 -080013437 DisplayList displayList = null;
13438 Bitmap cache = null;
13439 boolean hasDisplayList = false;
13440 if (caching) {
13441 if (!hardwareAccelerated) {
13442 if (layerType != LAYER_TYPE_NONE) {
13443 layerType = LAYER_TYPE_SOFTWARE;
13444 buildDrawingCache(true);
13445 }
13446 cache = getDrawingCache(true);
13447 } else {
13448 switch (layerType) {
13449 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013450 if (useDisplayListProperties) {
13451 hasDisplayList = canHaveDisplayList();
13452 } else {
13453 buildDrawingCache(true);
13454 cache = getDrawingCache(true);
13455 }
Chet Haase64a48c12012-02-13 16:33:29 -080013456 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013457 case LAYER_TYPE_HARDWARE:
13458 if (useDisplayListProperties) {
13459 hasDisplayList = canHaveDisplayList();
13460 }
13461 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013462 case LAYER_TYPE_NONE:
13463 // Delay getting the display list until animation-driven alpha values are
13464 // set up and possibly passed on to the view
13465 hasDisplayList = canHaveDisplayList();
13466 break;
13467 }
13468 }
13469 }
Chet Haasea1cff502012-02-21 13:43:44 -080013470 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013471 if (useDisplayListProperties) {
13472 displayList = getDisplayList();
13473 if (!displayList.isValid()) {
13474 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13475 // to getDisplayList(), the display list will be marked invalid and we should not
13476 // try to use it again.
13477 displayList = null;
13478 hasDisplayList = false;
13479 useDisplayListProperties = false;
13480 }
13481 }
Chet Haase64a48c12012-02-13 16:33:29 -080013482
Chet Haase526057b2012-07-12 17:50:41 -070013483 int sx = 0;
13484 int sy = 0;
13485 if (!hasDisplayList) {
13486 computeScroll();
13487 sx = mScrollX;
13488 sy = mScrollY;
13489 }
13490
Chet Haase64a48c12012-02-13 16:33:29 -080013491 final boolean hasNoCache = cache == null || hasDisplayList;
13492 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13493 layerType != LAYER_TYPE_HARDWARE;
13494
Chet Haasea1cff502012-02-21 13:43:44 -080013495 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013496 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013497 restoreTo = canvas.save();
13498 }
Chet Haase64a48c12012-02-13 16:33:29 -080013499 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013500 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013501 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013502 if (!useDisplayListProperties) {
13503 canvas.translate(mLeft, mTop);
13504 }
Chet Haase64a48c12012-02-13 16:33:29 -080013505 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013506 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013507 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013508 restoreTo = canvas.save();
13509 }
Chet Haase64a48c12012-02-13 16:33:29 -080013510 // mAttachInfo cannot be null, otherwise scalingRequired == false
13511 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13512 canvas.scale(scale, scale);
13513 }
13514 }
13515
Chet Haasea1cff502012-02-21 13:43:44 -080013516 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013517 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013518 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013519 if (transformToApply != null || !childHasIdentityMatrix) {
13520 int transX = 0;
13521 int transY = 0;
13522
13523 if (offsetForScroll) {
13524 transX = -sx;
13525 transY = -sy;
13526 }
13527
13528 if (transformToApply != null) {
13529 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013530 if (useDisplayListProperties) {
13531 displayList.setAnimationMatrix(transformToApply.getMatrix());
13532 } else {
13533 // Undo the scroll translation, apply the transformation matrix,
13534 // then redo the scroll translate to get the correct result.
13535 canvas.translate(-transX, -transY);
13536 canvas.concat(transformToApply.getMatrix());
13537 canvas.translate(transX, transY);
13538 }
Chet Haasea1cff502012-02-21 13:43:44 -080013539 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013540 }
13541
13542 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013543 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013544 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013545 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013546 }
13547 }
13548
Chet Haasea1cff502012-02-21 13:43:44 -080013549 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013550 canvas.translate(-transX, -transY);
13551 canvas.concat(getMatrix());
13552 canvas.translate(transX, transY);
13553 }
13554 }
13555
Chet Haase21433372012-06-05 07:54:09 -070013556 // Deal with alpha if it is or used to be <1
13557 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013558 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013559 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013560 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013561 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013562 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013563 }
Chet Haasea1cff502012-02-21 13:43:44 -080013564 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013565 if (hasNoCache) {
13566 final int multipliedAlpha = (int) (255 * alpha);
13567 if (!onSetAlpha(multipliedAlpha)) {
13568 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013569 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013570 layerType != LAYER_TYPE_NONE) {
13571 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13572 }
Chet Haase9420abd2012-03-29 16:28:32 -070013573 if (useDisplayListProperties) {
13574 displayList.setAlpha(alpha * getAlpha());
13575 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013576 final int scrollX = hasDisplayList ? 0 : sx;
13577 final int scrollY = hasDisplayList ? 0 : sy;
13578 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13579 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013580 }
13581 } else {
13582 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013583 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013584 }
13585 }
13586 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013587 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013588 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013589 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013590 }
13591
Chet Haasea1cff502012-02-21 13:43:44 -080013592 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13593 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013594 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013595 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013596 } else {
13597 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013598 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013599 } else {
13600 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13601 }
13602 }
13603 }
13604
Chet Haase9420abd2012-03-29 16:28:32 -070013605 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013606 displayList = getDisplayList();
13607 if (!displayList.isValid()) {
13608 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13609 // to getDisplayList(), the display list will be marked invalid and we should not
13610 // try to use it again.
13611 displayList = null;
13612 hasDisplayList = false;
13613 }
13614 }
13615
13616 if (hasNoCache) {
13617 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013618 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013619 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013620 if (layer != null && layer.isValid()) {
13621 mLayerPaint.setAlpha((int) (alpha * 255));
13622 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13623 layerRendered = true;
13624 } else {
13625 final int scrollX = hasDisplayList ? 0 : sx;
13626 final int scrollY = hasDisplayList ? 0 : sy;
13627 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013628 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013629 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13630 }
13631 }
13632
13633 if (!layerRendered) {
13634 if (!hasDisplayList) {
13635 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013636 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13637 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013638 dispatchDraw(canvas);
13639 } else {
13640 draw(canvas);
13641 }
13642 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013643 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013644 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013645 }
13646 }
13647 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013648 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013649 Paint cachePaint;
13650
13651 if (layerType == LAYER_TYPE_NONE) {
13652 cachePaint = parent.mCachePaint;
13653 if (cachePaint == null) {
13654 cachePaint = new Paint();
13655 cachePaint.setDither(false);
13656 parent.mCachePaint = cachePaint;
13657 }
Chet Haase9420abd2012-03-29 16:28:32 -070013658 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013659 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013660 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13661 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013662 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013663 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013664 }
13665 } else {
13666 cachePaint = mLayerPaint;
13667 cachePaint.setAlpha((int) (alpha * 255));
13668 }
13669 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13670 }
13671
Chet Haasea1cff502012-02-21 13:43:44 -080013672 if (restoreTo >= 0) {
13673 canvas.restoreToCount(restoreTo);
13674 }
Chet Haase64a48c12012-02-13 16:33:29 -080013675
13676 if (a != null && !more) {
13677 if (!hardwareAccelerated && !a.getFillAfter()) {
13678 onSetAlpha(255);
13679 }
13680 parent.finishAnimatingView(this, a);
13681 }
13682
13683 if (more && hardwareAccelerated) {
13684 // invalidation is the trigger to recreate display lists, so if we're using
13685 // display lists to render, force an invalidate to allow the animation to
13686 // continue drawing another frame
13687 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013688 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013689 // alpha animations should cause the child to recreate its display list
13690 invalidate(true);
13691 }
13692 }
13693
13694 mRecreateDisplayList = false;
13695
13696 return more;
13697 }
13698
13699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013700 * Manually render this view (and all of its children) to the given Canvas.
13701 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013702 * called. When implementing a view, implement
13703 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13704 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013705 *
13706 * @param canvas The Canvas to which the View is rendered.
13707 */
13708 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013709 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013710 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013711 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013712 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013714 /*
13715 * Draw traversal performs several drawing steps which must be executed
13716 * in the appropriate order:
13717 *
13718 * 1. Draw the background
13719 * 2. If necessary, save the canvas' layers to prepare for fading
13720 * 3. Draw view's content
13721 * 4. Draw children
13722 * 5. If necessary, draw the fading edges and restore layers
13723 * 6. Draw decorations (scrollbars for instance)
13724 */
13725
13726 // Step 1, draw the background, if needed
13727 int saveCount;
13728
Romain Guy24443ea2009-05-11 11:56:30 -070013729 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013730 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013731 if (background != null) {
13732 final int scrollX = mScrollX;
13733 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013734
Romain Guy24443ea2009-05-11 11:56:30 -070013735 if (mBackgroundSizeChanged) {
13736 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13737 mBackgroundSizeChanged = false;
13738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739
Romain Guy24443ea2009-05-11 11:56:30 -070013740 if ((scrollX | scrollY) == 0) {
13741 background.draw(canvas);
13742 } else {
13743 canvas.translate(scrollX, scrollY);
13744 background.draw(canvas);
13745 canvas.translate(-scrollX, -scrollY);
13746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013747 }
13748 }
13749
13750 // skip step 2 & 5 if possible (common case)
13751 final int viewFlags = mViewFlags;
13752 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13753 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13754 if (!verticalEdges && !horizontalEdges) {
13755 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013756 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013757
13758 // Step 4, draw the children
13759 dispatchDraw(canvas);
13760
13761 // Step 6, draw decorations (scrollbars)
13762 onDrawScrollBars(canvas);
13763
13764 // we're done...
13765 return;
13766 }
13767
13768 /*
13769 * Here we do the full fledged routine...
13770 * (this is an uncommon case where speed matters less,
13771 * this is why we repeat some of the tests that have been
13772 * done above)
13773 */
13774
13775 boolean drawTop = false;
13776 boolean drawBottom = false;
13777 boolean drawLeft = false;
13778 boolean drawRight = false;
13779
13780 float topFadeStrength = 0.0f;
13781 float bottomFadeStrength = 0.0f;
13782 float leftFadeStrength = 0.0f;
13783 float rightFadeStrength = 0.0f;
13784
13785 // Step 2, save the canvas' layers
13786 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013787
13788 final boolean offsetRequired = isPaddingOffsetRequired();
13789 if (offsetRequired) {
13790 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791 }
13792
13793 int left = mScrollX + paddingLeft;
13794 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013795 int top = mScrollY + getFadeTop(offsetRequired);
13796 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013797
13798 if (offsetRequired) {
13799 right += getRightPaddingOffset();
13800 bottom += getBottomPaddingOffset();
13801 }
13802
13803 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013804 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013805 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013806
13807 // clip the fade length if top and bottom fades overlap
13808 // overlapping fades produce odd-looking artifacts
13809 if (verticalEdges && (top + length > bottom - length)) {
13810 length = (bottom - top) / 2;
13811 }
13812
13813 // also clip horizontal fades if necessary
13814 if (horizontalEdges && (left + length > right - length)) {
13815 length = (right - left) / 2;
13816 }
13817
13818 if (verticalEdges) {
13819 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013820 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013821 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013822 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013823 }
13824
13825 if (horizontalEdges) {
13826 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013827 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013828 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013829 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013830 }
13831
13832 saveCount = canvas.getSaveCount();
13833
13834 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013835 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013836 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13837
13838 if (drawTop) {
13839 canvas.saveLayer(left, top, right, top + length, null, flags);
13840 }
13841
13842 if (drawBottom) {
13843 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13844 }
13845
13846 if (drawLeft) {
13847 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13848 }
13849
13850 if (drawRight) {
13851 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13852 }
13853 } else {
13854 scrollabilityCache.setFadeColor(solidColor);
13855 }
13856
13857 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013858 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013859
13860 // Step 4, draw the children
13861 dispatchDraw(canvas);
13862
13863 // Step 5, draw the fade effect and restore layers
13864 final Paint p = scrollabilityCache.paint;
13865 final Matrix matrix = scrollabilityCache.matrix;
13866 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013867
13868 if (drawTop) {
13869 matrix.setScale(1, fadeHeight * topFadeStrength);
13870 matrix.postTranslate(left, top);
13871 fade.setLocalMatrix(matrix);
13872 canvas.drawRect(left, top, right, top + length, p);
13873 }
13874
13875 if (drawBottom) {
13876 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13877 matrix.postRotate(180);
13878 matrix.postTranslate(left, bottom);
13879 fade.setLocalMatrix(matrix);
13880 canvas.drawRect(left, bottom - length, right, bottom, p);
13881 }
13882
13883 if (drawLeft) {
13884 matrix.setScale(1, fadeHeight * leftFadeStrength);
13885 matrix.postRotate(-90);
13886 matrix.postTranslate(left, top);
13887 fade.setLocalMatrix(matrix);
13888 canvas.drawRect(left, top, left + length, bottom, p);
13889 }
13890
13891 if (drawRight) {
13892 matrix.setScale(1, fadeHeight * rightFadeStrength);
13893 matrix.postRotate(90);
13894 matrix.postTranslate(right, top);
13895 fade.setLocalMatrix(matrix);
13896 canvas.drawRect(right - length, top, right, bottom, p);
13897 }
13898
13899 canvas.restoreToCount(saveCount);
13900
13901 // Step 6, draw decorations (scrollbars)
13902 onDrawScrollBars(canvas);
13903 }
13904
13905 /**
13906 * Override this if your view is known to always be drawn on top of a solid color background,
13907 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13908 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13909 * should be set to 0xFF.
13910 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013911 * @see #setVerticalFadingEdgeEnabled(boolean)
13912 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013913 *
13914 * @return The known solid color background for this view, or 0 if the color may vary
13915 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013916 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013917 public int getSolidColor() {
13918 return 0;
13919 }
13920
13921 /**
13922 * Build a human readable string representation of the specified view flags.
13923 *
13924 * @param flags the view flags to convert to a string
13925 * @return a String representing the supplied flags
13926 */
13927 private static String printFlags(int flags) {
13928 String output = "";
13929 int numFlags = 0;
13930 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13931 output += "TAKES_FOCUS";
13932 numFlags++;
13933 }
13934
13935 switch (flags & VISIBILITY_MASK) {
13936 case INVISIBLE:
13937 if (numFlags > 0) {
13938 output += " ";
13939 }
13940 output += "INVISIBLE";
13941 // USELESS HERE numFlags++;
13942 break;
13943 case GONE:
13944 if (numFlags > 0) {
13945 output += " ";
13946 }
13947 output += "GONE";
13948 // USELESS HERE numFlags++;
13949 break;
13950 default:
13951 break;
13952 }
13953 return output;
13954 }
13955
13956 /**
13957 * Build a human readable string representation of the specified private
13958 * view flags.
13959 *
13960 * @param privateFlags the private view flags to convert to a string
13961 * @return a String representing the supplied flags
13962 */
13963 private static String printPrivateFlags(int privateFlags) {
13964 String output = "";
13965 int numFlags = 0;
13966
Dianne Hackborn4702a852012-08-17 15:18:29 -070013967 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013968 output += "WANTS_FOCUS";
13969 numFlags++;
13970 }
13971
Dianne Hackborn4702a852012-08-17 15:18:29 -070013972 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013973 if (numFlags > 0) {
13974 output += " ";
13975 }
13976 output += "FOCUSED";
13977 numFlags++;
13978 }
13979
Dianne Hackborn4702a852012-08-17 15:18:29 -070013980 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981 if (numFlags > 0) {
13982 output += " ";
13983 }
13984 output += "SELECTED";
13985 numFlags++;
13986 }
13987
Dianne Hackborn4702a852012-08-17 15:18:29 -070013988 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 if (numFlags > 0) {
13990 output += " ";
13991 }
13992 output += "IS_ROOT_NAMESPACE";
13993 numFlags++;
13994 }
13995
Dianne Hackborn4702a852012-08-17 15:18:29 -070013996 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013997 if (numFlags > 0) {
13998 output += " ";
13999 }
14000 output += "HAS_BOUNDS";
14001 numFlags++;
14002 }
14003
Dianne Hackborn4702a852012-08-17 15:18:29 -070014004 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005 if (numFlags > 0) {
14006 output += " ";
14007 }
14008 output += "DRAWN";
14009 // USELESS HERE numFlags++;
14010 }
14011 return output;
14012 }
14013
14014 /**
14015 * <p>Indicates whether or not this view's layout will be requested during
14016 * the next hierarchy layout pass.</p>
14017 *
14018 * @return true if the layout will be forced during next layout pass
14019 */
14020 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014021 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014022 }
14023
14024 /**
Philip Milne7b757812012-09-19 18:13:44 -070014025 * Return true if o is a ViewGroup that is laying out using optical bounds.
14026 * @hide
14027 */
14028 public static boolean isLayoutModeOptical(Object o) {
14029 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
14030 }
14031
14032 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
14033 Insets parentInsets = mParent instanceof View ?
14034 ((View) mParent).getOpticalInsets() : Insets.NONE;
14035 Insets childInsets = getOpticalInsets();
14036 return setFrame(
14037 left + parentInsets.left - childInsets.left,
14038 top + parentInsets.top - childInsets.top,
14039 right + parentInsets.left + childInsets.right,
14040 bottom + parentInsets.top + childInsets.bottom);
14041 }
14042
14043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014044 * Assign a size and position to a view and all of its
14045 * descendants
14046 *
14047 * <p>This is the second phase of the layout mechanism.
14048 * (The first is measuring). In this phase, each parent calls
14049 * layout on all of its children to position them.
14050 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014051 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052 *
Chet Haase9c087442011-01-12 16:20:16 -080014053 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014054 * Derived classes with children should override
14055 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014056 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014057 *
14058 * @param l Left position, relative to parent
14059 * @param t Top position, relative to parent
14060 * @param r Right position, relative to parent
14061 * @param b Bottom position, relative to parent
14062 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014063 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014064 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014065 int oldL = mLeft;
14066 int oldT = mTop;
14067 int oldB = mBottom;
14068 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014069 boolean changed = isLayoutModeOptical(mParent) ?
14070 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014071 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014073 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014074
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014075 ListenerInfo li = mListenerInfo;
14076 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014077 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014078 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014079 int numListeners = listenersCopy.size();
14080 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014081 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014082 }
14083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014084 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014085 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014086 }
14087
14088 /**
14089 * Called from layout when this view should
14090 * assign a size and position to each of its children.
14091 *
14092 * Derived classes with children should override
14093 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014094 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014095 * @param changed This is a new size or position for this view
14096 * @param left Left position, relative to parent
14097 * @param top Top position, relative to parent
14098 * @param right Right position, relative to parent
14099 * @param bottom Bottom position, relative to parent
14100 */
14101 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14102 }
14103
14104 /**
14105 * Assign a size and position to this view.
14106 *
14107 * This is called from layout.
14108 *
14109 * @param left Left position, relative to parent
14110 * @param top Top position, relative to parent
14111 * @param right Right position, relative to parent
14112 * @param bottom Bottom position, relative to parent
14113 * @return true if the new size and position are different than the
14114 * previous ones
14115 * {@hide}
14116 */
14117 protected boolean setFrame(int left, int top, int right, int bottom) {
14118 boolean changed = false;
14119
14120 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014121 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014122 + right + "," + bottom + ")");
14123 }
14124
14125 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14126 changed = true;
14127
14128 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014129 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 int oldWidth = mRight - mLeft;
14132 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014133 int newWidth = right - left;
14134 int newHeight = bottom - top;
14135 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14136
14137 // Invalidate our old position
14138 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014139
14140 mLeft = left;
14141 mTop = top;
14142 mRight = right;
14143 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014144 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014145 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014147
Dianne Hackborn4702a852012-08-17 15:18:29 -070014148 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150
Chet Haase75755e22011-07-18 17:48:25 -070014151 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014152 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014153 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014154 if (mTransformationInfo != null) {
14155 mTransformationInfo.mMatrixDirty = true;
14156 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14159 }
14160
14161 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14162 // If we are visible, force the DRAWN bit to on so that
14163 // this invalidate will go through (at least to our parent).
14164 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014165 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014166 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014167 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014168 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014169 // parent display list may need to be recreated based on a change in the bounds
14170 // of any child
14171 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 }
14173
14174 // Reset drawn bit to original value (invalidate turns it off)
14175 mPrivateFlags |= drawn;
14176
14177 mBackgroundSizeChanged = true;
14178 }
14179 return changed;
14180 }
14181
14182 /**
14183 * Finalize inflating a view from XML. This is called as the last phase
14184 * of inflation, after all child views have been added.
14185 *
14186 * <p>Even if the subclass overrides onFinishInflate, they should always be
14187 * sure to call the super method, so that we get called.
14188 */
14189 protected void onFinishInflate() {
14190 }
14191
14192 /**
14193 * Returns the resources associated with this view.
14194 *
14195 * @return Resources object.
14196 */
14197 public Resources getResources() {
14198 return mResources;
14199 }
14200
14201 /**
14202 * Invalidates the specified Drawable.
14203 *
14204 * @param drawable the drawable to invalidate
14205 */
14206 public void invalidateDrawable(Drawable drawable) {
14207 if (verifyDrawable(drawable)) {
14208 final Rect dirty = drawable.getBounds();
14209 final int scrollX = mScrollX;
14210 final int scrollY = mScrollY;
14211
14212 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14213 dirty.right + scrollX, dirty.bottom + scrollY);
14214 }
14215 }
14216
14217 /**
14218 * Schedules an action on a drawable to occur at a specified time.
14219 *
14220 * @param who the recipient of the action
14221 * @param what the action to run on the drawable
14222 * @param when the time at which the action must occur. Uses the
14223 * {@link SystemClock#uptimeMillis} timebase.
14224 */
14225 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014226 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014227 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014228 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014229 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14230 Choreographer.CALLBACK_ANIMATION, what, who,
14231 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014232 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014233 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 }
14236 }
14237
14238 /**
14239 * Cancels a scheduled action on a drawable.
14240 *
14241 * @param who the recipient of the action
14242 * @param what the action to cancel
14243 */
14244 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014245 if (verifyDrawable(who) && what != null) {
14246 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014247 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14248 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014249 } else {
14250 ViewRootImpl.getRunQueue().removeCallbacks(what);
14251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014252 }
14253 }
14254
14255 /**
14256 * Unschedule any events associated with the given Drawable. This can be
14257 * used when selecting a new Drawable into a view, so that the previous
14258 * one is completely unscheduled.
14259 *
14260 * @param who The Drawable to unschedule.
14261 *
14262 * @see #drawableStateChanged
14263 */
14264 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014265 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014266 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14267 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 }
14269 }
14270
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014271 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014272 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14273 * that the View directionality can and will be resolved before its Drawables.
14274 *
14275 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014276 *
14277 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014278 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014279 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014280 if (canResolveLayoutDirection()) {
14281 if (mBackground != null) {
14282 mBackground.setLayoutDirection(getLayoutDirection());
14283 }
14284 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14285 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014286 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014287 }
14288
14289 /**
14290 * Called when layout direction has been resolved.
14291 *
14292 * The default implementation does nothing.
14293 *
14294 * @param layoutDirection The resolved layout direction.
14295 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014296 * @see #LAYOUT_DIRECTION_LTR
14297 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014298 *
14299 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014300 */
14301 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014302 }
14303
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014304 /**
14305 * @hide
14306 */
14307 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014308 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14309 }
14310
14311 private boolean isDrawablesResolved() {
14312 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14313 }
14314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014315 /**
14316 * If your view subclass is displaying its own Drawable objects, it should
14317 * override this function and return true for any Drawable it is
14318 * displaying. This allows animations for those drawables to be
14319 * scheduled.
14320 *
14321 * <p>Be sure to call through to the super class when overriding this
14322 * function.
14323 *
14324 * @param who The Drawable to verify. Return true if it is one you are
14325 * displaying, else return the result of calling through to the
14326 * super class.
14327 *
14328 * @return boolean If true than the Drawable is being displayed in the
14329 * view; else false and it is not allowed to animate.
14330 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014331 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14332 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014333 */
14334 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014335 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014336 }
14337
14338 /**
14339 * This function is called whenever the state of the view changes in such
14340 * a way that it impacts the state of drawables being shown.
14341 *
14342 * <p>Be sure to call through to the superclass when overriding this
14343 * function.
14344 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014345 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014346 */
14347 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014348 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014349 if (d != null && d.isStateful()) {
14350 d.setState(getDrawableState());
14351 }
14352 }
14353
14354 /**
14355 * Call this to force a view to update its drawable state. This will cause
14356 * drawableStateChanged to be called on this view. Views that are interested
14357 * in the new state should call getDrawableState.
14358 *
14359 * @see #drawableStateChanged
14360 * @see #getDrawableState
14361 */
14362 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014363 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014364 drawableStateChanged();
14365
14366 ViewParent parent = mParent;
14367 if (parent != null) {
14368 parent.childDrawableStateChanged(this);
14369 }
14370 }
14371
14372 /**
14373 * Return an array of resource IDs of the drawable states representing the
14374 * current state of the view.
14375 *
14376 * @return The current drawable state
14377 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014378 * @see Drawable#setState(int[])
14379 * @see #drawableStateChanged()
14380 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014381 */
14382 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014383 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014384 return mDrawableState;
14385 } else {
14386 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014387 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014388 return mDrawableState;
14389 }
14390 }
14391
14392 /**
14393 * Generate the new {@link android.graphics.drawable.Drawable} state for
14394 * this view. This is called by the view
14395 * system when the cached Drawable state is determined to be invalid. To
14396 * retrieve the current state, you should use {@link #getDrawableState}.
14397 *
14398 * @param extraSpace if non-zero, this is the number of extra entries you
14399 * would like in the returned array in which you can place your own
14400 * states.
14401 *
14402 * @return Returns an array holding the current {@link Drawable} state of
14403 * the view.
14404 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014405 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014406 */
14407 protected int[] onCreateDrawableState(int extraSpace) {
14408 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14409 mParent instanceof View) {
14410 return ((View) mParent).onCreateDrawableState(extraSpace);
14411 }
14412
14413 int[] drawableState;
14414
14415 int privateFlags = mPrivateFlags;
14416
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014417 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014418 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014419 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14420 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014421 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014422 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014423 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014424 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14425 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014426 // This is set if HW acceleration is requested, even if the current
14427 // process doesn't allow it. This is just to allow app preview
14428 // windows to better match their app.
14429 viewStateIndex |= VIEW_STATE_ACCELERATED;
14430 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014431 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432
Christopher Tate3d4bf172011-03-28 16:16:46 -070014433 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014434 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14435 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014437 drawableState = VIEW_STATE_SETS[viewStateIndex];
14438
14439 //noinspection ConstantIfStatement
14440 if (false) {
14441 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14442 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014443 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014444 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14445 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014446 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014447 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448 + ": " + Arrays.toString(drawableState));
14449 }
14450
14451 if (extraSpace == 0) {
14452 return drawableState;
14453 }
14454
14455 final int[] fullState;
14456 if (drawableState != null) {
14457 fullState = new int[drawableState.length + extraSpace];
14458 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14459 } else {
14460 fullState = new int[extraSpace];
14461 }
14462
14463 return fullState;
14464 }
14465
14466 /**
14467 * Merge your own state values in <var>additionalState</var> into the base
14468 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014469 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014470 *
14471 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014472 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014473 * own additional state values.
14474 *
14475 * @param additionalState The additional state values you would like
14476 * added to <var>baseState</var>; this array is not modified.
14477 *
14478 * @return As a convenience, the <var>baseState</var> array you originally
14479 * passed into the function is returned.
14480 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014481 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014482 */
14483 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14484 final int N = baseState.length;
14485 int i = N - 1;
14486 while (i >= 0 && baseState[i] == 0) {
14487 i--;
14488 }
14489 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14490 return baseState;
14491 }
14492
14493 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014494 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14495 * on all Drawable objects associated with this view.
14496 */
14497 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014498 if (mBackground != null) {
14499 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014500 }
14501 }
14502
14503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 * Sets the background color for this view.
14505 * @param color the color of the background
14506 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014507 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014508 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014509 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014510 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014511 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014512 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014513 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014514 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516 }
14517
14518 /**
14519 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014520 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014521 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014523 * @attr ref android.R.styleable#View_background
14524 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014525 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 public void setBackgroundResource(int resid) {
14527 if (resid != 0 && resid == mBackgroundResource) {
14528 return;
14529 }
14530
14531 Drawable d= null;
14532 if (resid != 0) {
14533 d = mResources.getDrawable(resid);
14534 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014535 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014536
14537 mBackgroundResource = resid;
14538 }
14539
14540 /**
14541 * Set the background to a given Drawable, or remove the background. If the
14542 * background has padding, this View's padding is set to the background's
14543 * padding. However, when a background is removed, this View's padding isn't
14544 * touched. If setting the padding is desired, please use
14545 * {@link #setPadding(int, int, int, int)}.
14546 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014547 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014548 * background
14549 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014550 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014551 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014552 setBackgroundDrawable(background);
14553 }
14554
14555 /**
14556 * @deprecated use {@link #setBackground(Drawable)} instead
14557 */
14558 @Deprecated
14559 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014560 computeOpaqueFlags();
14561
Philip Milne6c8ea062012-04-03 17:38:43 -070014562 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014563 return;
14564 }
14565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014566 boolean requestLayout = false;
14567
14568 mBackgroundResource = 0;
14569
14570 /*
14571 * Regardless of whether we're setting a new background or not, we want
14572 * to clear the previous drawable.
14573 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014574 if (mBackground != null) {
14575 mBackground.setCallback(null);
14576 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014577 }
14578
Philip Milne6c8ea062012-04-03 17:38:43 -070014579 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014580 Rect padding = sThreadLocal.get();
14581 if (padding == null) {
14582 padding = new Rect();
14583 sThreadLocal.set(padding);
14584 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014585 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014586 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014587 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014588 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014589 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014590 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014591 mUserPaddingLeftInitial = padding.right;
14592 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014593 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014594 break;
14595 case LAYOUT_DIRECTION_LTR:
14596 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014597 mUserPaddingLeftInitial = padding.left;
14598 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014599 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014601 }
14602
14603 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14604 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014605 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14606 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 requestLayout = true;
14608 }
14609
Philip Milne6c8ea062012-04-03 17:38:43 -070014610 background.setCallback(this);
14611 if (background.isStateful()) {
14612 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014613 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014614 background.setVisible(getVisibility() == VISIBLE, false);
14615 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014616
Dianne Hackborn4702a852012-08-17 15:18:29 -070014617 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14618 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14619 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014620 requestLayout = true;
14621 }
14622 } else {
14623 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014624 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014625
Dianne Hackborn4702a852012-08-17 15:18:29 -070014626 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014627 /*
14628 * This view ONLY drew the background before and we're removing
14629 * the background, so now it won't draw anything
14630 * (hence we SKIP_DRAW)
14631 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014632 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14633 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014634 }
14635
14636 /*
14637 * When the background is set, we try to apply its padding to this
14638 * View. When the background is removed, we don't touch this View's
14639 * padding. This is noted in the Javadocs. Hence, we don't need to
14640 * requestLayout(), the invalidate() below is sufficient.
14641 */
14642
14643 // The old background's minimum size could have affected this
14644 // View's layout, so let's requestLayout
14645 requestLayout = true;
14646 }
14647
Romain Guy8f1344f52009-05-15 16:03:59 -070014648 computeOpaqueFlags();
14649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014650 if (requestLayout) {
14651 requestLayout();
14652 }
14653
14654 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014655 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014656 }
14657
14658 /**
14659 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014661 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014662 *
14663 * @see #setBackground(Drawable)
14664 *
14665 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014666 */
14667 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014668 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014669 }
14670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014671 /**
14672 * Sets the padding. The view may add on the space required to display
14673 * the scrollbars, depending on the style and visibility of the scrollbars.
14674 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14675 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14676 * from the values set in this call.
14677 *
14678 * @attr ref android.R.styleable#View_padding
14679 * @attr ref android.R.styleable#View_paddingBottom
14680 * @attr ref android.R.styleable#View_paddingLeft
14681 * @attr ref android.R.styleable#View_paddingRight
14682 * @attr ref android.R.styleable#View_paddingTop
14683 * @param left the left padding in pixels
14684 * @param top the top padding in pixels
14685 * @param right the right padding in pixels
14686 * @param bottom the bottom padding in pixels
14687 */
14688 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014689 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014690
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014691 mUserPaddingStart = UNDEFINED_PADDING;
14692 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014693
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014694 mUserPaddingLeftInitial = left;
14695 mUserPaddingRightInitial = right;
14696
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014697 internalSetPadding(left, top, right, bottom);
14698 }
14699
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014700 /**
14701 * @hide
14702 */
14703 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014704 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014705 mUserPaddingRight = right;
14706 mUserPaddingBottom = bottom;
14707
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014708 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014709 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014710
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014711 // Common case is there are no scroll bars.
14712 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014713 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014714 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014715 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014716 switch (mVerticalScrollbarPosition) {
14717 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014718 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014719 left += offset;
14720 } else {
14721 right += offset;
14722 }
14723 break;
Adam Powell20232d02010-12-08 21:08:53 -080014724 case SCROLLBAR_POSITION_RIGHT:
14725 right += offset;
14726 break;
14727 case SCROLLBAR_POSITION_LEFT:
14728 left += offset;
14729 break;
14730 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014731 }
Adam Powell20232d02010-12-08 21:08:53 -080014732 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014733 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14734 ? 0 : getHorizontalScrollbarHeight();
14735 }
14736 }
Romain Guy8506ab42009-06-11 17:35:47 -070014737
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014738 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014739 changed = true;
14740 mPaddingLeft = left;
14741 }
14742 if (mPaddingTop != top) {
14743 changed = true;
14744 mPaddingTop = top;
14745 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014746 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014747 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014748 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014749 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014750 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014751 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014752 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014753 }
14754
14755 if (changed) {
14756 requestLayout();
14757 }
14758 }
14759
14760 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014761 * Sets the relative padding. The view may add on the space required to display
14762 * the scrollbars, depending on the style and visibility of the scrollbars.
14763 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14764 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14765 * from the values set in this call.
14766 *
14767 * @attr ref android.R.styleable#View_padding
14768 * @attr ref android.R.styleable#View_paddingBottom
14769 * @attr ref android.R.styleable#View_paddingStart
14770 * @attr ref android.R.styleable#View_paddingEnd
14771 * @attr ref android.R.styleable#View_paddingTop
14772 * @param start the start padding in pixels
14773 * @param top the top padding in pixels
14774 * @param end the end padding in pixels
14775 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014776 */
14777 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014778 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014779
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014780 mUserPaddingStart = start;
14781 mUserPaddingEnd = end;
14782
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014783 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014784 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014785 mUserPaddingLeftInitial = end;
14786 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014787 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014788 break;
14789 case LAYOUT_DIRECTION_LTR:
14790 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014791 mUserPaddingLeftInitial = start;
14792 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014793 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014794 }
14795 }
14796
14797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798 * Returns the top padding of this view.
14799 *
14800 * @return the top padding in pixels
14801 */
14802 public int getPaddingTop() {
14803 return mPaddingTop;
14804 }
14805
14806 /**
14807 * Returns the bottom padding of this view. If there are inset and enabled
14808 * scrollbars, this value may include the space required to display the
14809 * scrollbars as well.
14810 *
14811 * @return the bottom padding in pixels
14812 */
14813 public int getPaddingBottom() {
14814 return mPaddingBottom;
14815 }
14816
14817 /**
14818 * Returns the left padding of this view. If there are inset and enabled
14819 * scrollbars, this value may include the space required to display the
14820 * scrollbars as well.
14821 *
14822 * @return the left padding in pixels
14823 */
14824 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014825 if (!isPaddingResolved()) {
14826 resolvePadding();
14827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014828 return mPaddingLeft;
14829 }
14830
14831 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014832 * Returns the start padding of this view depending on its resolved layout direction.
14833 * If there are inset and enabled scrollbars, this value may include the space
14834 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014835 *
14836 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014837 */
14838 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014839 if (!isPaddingResolved()) {
14840 resolvePadding();
14841 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014842 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014843 mPaddingRight : mPaddingLeft;
14844 }
14845
14846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014847 * Returns the right padding of this view. If there are inset and enabled
14848 * scrollbars, this value may include the space required to display the
14849 * scrollbars as well.
14850 *
14851 * @return the right padding in pixels
14852 */
14853 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014854 if (!isPaddingResolved()) {
14855 resolvePadding();
14856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014857 return mPaddingRight;
14858 }
14859
14860 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014861 * Returns the end padding of this view depending on its resolved layout direction.
14862 * If there are inset and enabled scrollbars, this value may include the space
14863 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014864 *
14865 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014866 */
14867 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014868 if (!isPaddingResolved()) {
14869 resolvePadding();
14870 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014871 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014872 mPaddingLeft : mPaddingRight;
14873 }
14874
14875 /**
14876 * Return if the padding as been set thru relative values
14877 * {@link #setPaddingRelative(int, int, int, int)} or thru
14878 * @attr ref android.R.styleable#View_paddingStart or
14879 * @attr ref android.R.styleable#View_paddingEnd
14880 *
14881 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014882 */
14883 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014884 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014885 }
14886
Philip Milne7b757812012-09-19 18:13:44 -070014887 Insets computeOpticalInsets() {
14888 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014889 }
14890
14891 /**
14892 * @hide
14893 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014894 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014895 if (isRtlCompatibilityMode()) {
14896 mPaddingLeft = mUserPaddingLeftInitial;
14897 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014898 return;
14899 }
14900 if (isLayoutRtl()) {
14901 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14902 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014903 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014904 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14905 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014906 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014907 }
14908
14909 /**
14910 * @hide
14911 */
Philip Milne7b757812012-09-19 18:13:44 -070014912 public Insets getOpticalInsets() {
14913 if (mLayoutInsets == null) {
14914 mLayoutInsets = computeOpticalInsets();
14915 }
14916 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070014917 }
14918
14919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 * Changes the selection state of this view. A view can be selected or not.
14921 * Note that selection is not the same as focus. Views are typically
14922 * selected in the context of an AdapterView like ListView or GridView;
14923 * the selected view is the view that is highlighted.
14924 *
14925 * @param selected true if the view must be selected, false otherwise
14926 */
14927 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014928 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14929 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014930 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014931 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014932 refreshDrawableState();
14933 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014934 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14935 notifyAccessibilityStateChanged();
14936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014937 }
14938 }
14939
14940 /**
14941 * Dispatch setSelected to all of this View's children.
14942 *
14943 * @see #setSelected(boolean)
14944 *
14945 * @param selected The new selected state
14946 */
14947 protected void dispatchSetSelected(boolean selected) {
14948 }
14949
14950 /**
14951 * Indicates the selection state of this view.
14952 *
14953 * @return true if the view is selected, false otherwise
14954 */
14955 @ViewDebug.ExportedProperty
14956 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014957 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014958 }
14959
14960 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014961 * Changes the activated state of this view. A view can be activated or not.
14962 * Note that activation is not the same as selection. Selection is
14963 * a transient property, representing the view (hierarchy) the user is
14964 * currently interacting with. Activation is a longer-term state that the
14965 * user can move views in and out of. For example, in a list view with
14966 * single or multiple selection enabled, the views in the current selection
14967 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14968 * here.) The activated state is propagated down to children of the view it
14969 * is set on.
14970 *
14971 * @param activated true if the view must be activated, false otherwise
14972 */
14973 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014974 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14975 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014976 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014977 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014978 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014979 }
14980 }
14981
14982 /**
14983 * Dispatch setActivated to all of this View's children.
14984 *
14985 * @see #setActivated(boolean)
14986 *
14987 * @param activated The new activated state
14988 */
14989 protected void dispatchSetActivated(boolean activated) {
14990 }
14991
14992 /**
14993 * Indicates the activation state of this view.
14994 *
14995 * @return true if the view is activated, false otherwise
14996 */
14997 @ViewDebug.ExportedProperty
14998 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014999 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015000 }
15001
15002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015003 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
15004 * observer can be used to get notifications when global events, like
15005 * layout, happen.
15006 *
15007 * The returned ViewTreeObserver observer is not guaranteed to remain
15008 * valid for the lifetime of this View. If the caller of this method keeps
15009 * a long-lived reference to ViewTreeObserver, it should always check for
15010 * the return value of {@link ViewTreeObserver#isAlive()}.
15011 *
15012 * @return The ViewTreeObserver for this view's hierarchy.
15013 */
15014 public ViewTreeObserver getViewTreeObserver() {
15015 if (mAttachInfo != null) {
15016 return mAttachInfo.mTreeObserver;
15017 }
15018 if (mFloatingTreeObserver == null) {
15019 mFloatingTreeObserver = new ViewTreeObserver();
15020 }
15021 return mFloatingTreeObserver;
15022 }
15023
15024 /**
15025 * <p>Finds the topmost view in the current view hierarchy.</p>
15026 *
15027 * @return the topmost view containing this view
15028 */
15029 public View getRootView() {
15030 if (mAttachInfo != null) {
15031 final View v = mAttachInfo.mRootView;
15032 if (v != null) {
15033 return v;
15034 }
15035 }
Romain Guy8506ab42009-06-11 17:35:47 -070015036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015037 View parent = this;
15038
15039 while (parent.mParent != null && parent.mParent instanceof View) {
15040 parent = (View) parent.mParent;
15041 }
15042
15043 return parent;
15044 }
15045
15046 /**
15047 * <p>Computes the coordinates of this view on the screen. The argument
15048 * must be an array of two integers. After the method returns, the array
15049 * contains the x and y location in that order.</p>
15050 *
15051 * @param location an array of two integers in which to hold the coordinates
15052 */
15053 public void getLocationOnScreen(int[] location) {
15054 getLocationInWindow(location);
15055
15056 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015057 if (info != null) {
15058 location[0] += info.mWindowLeft;
15059 location[1] += info.mWindowTop;
15060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015061 }
15062
15063 /**
15064 * <p>Computes the coordinates of this view in its window. The argument
15065 * must be an array of two integers. After the method returns, the array
15066 * contains the x and y location in that order.</p>
15067 *
15068 * @param location an array of two integers in which to hold the coordinates
15069 */
15070 public void getLocationInWindow(int[] location) {
15071 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015072 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015073 }
15074
Gilles Debunne6583ce52011-12-06 18:09:02 -080015075 if (mAttachInfo == null) {
15076 // When the view is not attached to a window, this method does not make sense
15077 location[0] = location[1] = 0;
15078 return;
15079 }
15080
Gilles Debunnecea45132011-11-24 02:19:27 +010015081 float[] position = mAttachInfo.mTmpTransformLocation;
15082 position[0] = position[1] = 0.0f;
15083
15084 if (!hasIdentityMatrix()) {
15085 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015087
Gilles Debunnecea45132011-11-24 02:19:27 +010015088 position[0] += mLeft;
15089 position[1] += mTop;
15090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015091 ViewParent viewParent = mParent;
15092 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015093 final View view = (View) viewParent;
15094
15095 position[0] -= view.mScrollX;
15096 position[1] -= view.mScrollY;
15097
15098 if (!view.hasIdentityMatrix()) {
15099 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015100 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015101
15102 position[0] += view.mLeft;
15103 position[1] += view.mTop;
15104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015105 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015106 }
Romain Guy8506ab42009-06-11 17:35:47 -070015107
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015108 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015109 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015110 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15111 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015112 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015113
15114 location[0] = (int) (position[0] + 0.5f);
15115 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015116 }
15117
15118 /**
15119 * {@hide}
15120 * @param id the id of the view to be found
15121 * @return the view of the specified id, null if cannot be found
15122 */
15123 protected View findViewTraversal(int id) {
15124 if (id == mID) {
15125 return this;
15126 }
15127 return null;
15128 }
15129
15130 /**
15131 * {@hide}
15132 * @param tag the tag of the view to be found
15133 * @return the view of specified tag, null if cannot be found
15134 */
15135 protected View findViewWithTagTraversal(Object tag) {
15136 if (tag != null && tag.equals(mTag)) {
15137 return this;
15138 }
15139 return null;
15140 }
15141
15142 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015143 * {@hide}
15144 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015145 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015146 * @return The first view that matches the predicate or null.
15147 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015148 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015149 if (predicate.apply(this)) {
15150 return this;
15151 }
15152 return null;
15153 }
15154
15155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015156 * Look for a child view with the given id. If this view has the given
15157 * id, return this view.
15158 *
15159 * @param id The id to search for.
15160 * @return The view that has the given id in the hierarchy or null
15161 */
15162 public final View findViewById(int id) {
15163 if (id < 0) {
15164 return null;
15165 }
15166 return findViewTraversal(id);
15167 }
15168
15169 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015170 * Finds a view by its unuque and stable accessibility id.
15171 *
15172 * @param accessibilityId The searched accessibility id.
15173 * @return The found view.
15174 */
15175 final View findViewByAccessibilityId(int accessibilityId) {
15176 if (accessibilityId < 0) {
15177 return null;
15178 }
15179 return findViewByAccessibilityIdTraversal(accessibilityId);
15180 }
15181
15182 /**
15183 * Performs the traversal to find a view by its unuque and stable accessibility id.
15184 *
15185 * <strong>Note:</strong>This method does not stop at the root namespace
15186 * boundary since the user can touch the screen at an arbitrary location
15187 * potentially crossing the root namespace bounday which will send an
15188 * accessibility event to accessibility services and they should be able
15189 * to obtain the event source. Also accessibility ids are guaranteed to be
15190 * unique in the window.
15191 *
15192 * @param accessibilityId The accessibility id.
15193 * @return The found view.
15194 */
15195 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15196 if (getAccessibilityViewId() == accessibilityId) {
15197 return this;
15198 }
15199 return null;
15200 }
15201
15202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015203 * Look for a child view with the given tag. If this view has the given
15204 * tag, return this view.
15205 *
15206 * @param tag The tag to search for, using "tag.equals(getTag())".
15207 * @return The View that has the given tag in the hierarchy or null
15208 */
15209 public final View findViewWithTag(Object tag) {
15210 if (tag == null) {
15211 return null;
15212 }
15213 return findViewWithTagTraversal(tag);
15214 }
15215
15216 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015217 * {@hide}
15218 * Look for a child view that matches the specified predicate.
15219 * If this view matches the predicate, return this view.
15220 *
15221 * @param predicate The predicate to evaluate.
15222 * @return The first view that matches the predicate or null.
15223 */
15224 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015225 return findViewByPredicateTraversal(predicate, null);
15226 }
15227
15228 /**
15229 * {@hide}
15230 * Look for a child view that matches the specified predicate,
15231 * starting with the specified view and its descendents and then
15232 * recusively searching the ancestors and siblings of that view
15233 * until this view is reached.
15234 *
15235 * This method is useful in cases where the predicate does not match
15236 * a single unique view (perhaps multiple views use the same id)
15237 * and we are trying to find the view that is "closest" in scope to the
15238 * starting view.
15239 *
15240 * @param start The view to start from.
15241 * @param predicate The predicate to evaluate.
15242 * @return The first view that matches the predicate or null.
15243 */
15244 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15245 View childToSkip = null;
15246 for (;;) {
15247 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15248 if (view != null || start == this) {
15249 return view;
15250 }
15251
15252 ViewParent parent = start.getParent();
15253 if (parent == null || !(parent instanceof View)) {
15254 return null;
15255 }
15256
15257 childToSkip = start;
15258 start = (View) parent;
15259 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015260 }
15261
15262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015263 * Sets the identifier for this view. The identifier does not have to be
15264 * unique in this view's hierarchy. The identifier should be a positive
15265 * number.
15266 *
15267 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015268 * @see #getId()
15269 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015270 *
15271 * @param id a number used to identify the view
15272 *
15273 * @attr ref android.R.styleable#View_id
15274 */
15275 public void setId(int id) {
15276 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015277 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15278 mID = generateViewId();
15279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015280 }
15281
15282 /**
15283 * {@hide}
15284 *
15285 * @param isRoot true if the view belongs to the root namespace, false
15286 * otherwise
15287 */
15288 public void setIsRootNamespace(boolean isRoot) {
15289 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015290 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015291 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015292 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015293 }
15294 }
15295
15296 /**
15297 * {@hide}
15298 *
15299 * @return true if the view belongs to the root namespace, false otherwise
15300 */
15301 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015302 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015303 }
15304
15305 /**
15306 * Returns this view's identifier.
15307 *
15308 * @return a positive integer used to identify the view or {@link #NO_ID}
15309 * if the view has no ID
15310 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015311 * @see #setId(int)
15312 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015313 * @attr ref android.R.styleable#View_id
15314 */
15315 @ViewDebug.CapturedViewProperty
15316 public int getId() {
15317 return mID;
15318 }
15319
15320 /**
15321 * Returns this view's tag.
15322 *
15323 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015324 *
15325 * @see #setTag(Object)
15326 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015327 */
15328 @ViewDebug.ExportedProperty
15329 public Object getTag() {
15330 return mTag;
15331 }
15332
15333 /**
15334 * Sets the tag associated with this view. A tag can be used to mark
15335 * a view in its hierarchy and does not have to be unique within the
15336 * hierarchy. Tags can also be used to store data within a view without
15337 * resorting to another data structure.
15338 *
15339 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015340 *
15341 * @see #getTag()
15342 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015343 */
15344 public void setTag(final Object tag) {
15345 mTag = tag;
15346 }
15347
15348 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015349 * Returns the tag associated with this view and the specified key.
15350 *
15351 * @param key The key identifying the tag
15352 *
15353 * @return the Object stored in this view as a tag
15354 *
15355 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015356 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015357 */
15358 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015359 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015360 return null;
15361 }
15362
15363 /**
15364 * Sets a tag associated with this view and a key. A tag can be used
15365 * to mark a view in its hierarchy and does not have to be unique within
15366 * the hierarchy. Tags can also be used to store data within a view
15367 * without resorting to another data structure.
15368 *
15369 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015370 * application to ensure it is unique (see the <a
15371 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15372 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015373 * the Android framework or not associated with any package will cause
15374 * an {@link IllegalArgumentException} to be thrown.
15375 *
15376 * @param key The key identifying the tag
15377 * @param tag An Object to tag the view with
15378 *
15379 * @throws IllegalArgumentException If they specified key is not valid
15380 *
15381 * @see #setTag(Object)
15382 * @see #getTag(int)
15383 */
15384 public void setTag(int key, final Object tag) {
15385 // If the package id is 0x00 or 0x01, it's either an undefined package
15386 // or a framework id
15387 if ((key >>> 24) < 2) {
15388 throw new IllegalArgumentException("The key must be an application-specific "
15389 + "resource id.");
15390 }
15391
Adam Powell2b2f6d62011-09-23 11:15:39 -070015392 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015393 }
15394
15395 /**
15396 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15397 * framework id.
15398 *
15399 * @hide
15400 */
15401 public void setTagInternal(int key, Object tag) {
15402 if ((key >>> 24) != 0x1) {
15403 throw new IllegalArgumentException("The key must be a framework-specific "
15404 + "resource id.");
15405 }
15406
Adam Powell2b2f6d62011-09-23 11:15:39 -070015407 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015408 }
15409
Adam Powell2b2f6d62011-09-23 11:15:39 -070015410 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015411 if (mKeyedTags == null) {
15412 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015413 }
15414
Adam Powell7db82ac2011-09-22 19:44:04 -070015415 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015416 }
15417
15418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015419 * Prints information about this view in the log output, with the tag
15420 * {@link #VIEW_LOG_TAG}.
15421 *
15422 * @hide
15423 */
15424 public void debug() {
15425 debug(0);
15426 }
15427
15428 /**
15429 * Prints information about this view in the log output, with the tag
15430 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15431 * indentation defined by the <code>depth</code>.
15432 *
15433 * @param depth the indentation level
15434 *
15435 * @hide
15436 */
15437 protected void debug(int depth) {
15438 String output = debugIndent(depth - 1);
15439
15440 output += "+ " + this;
15441 int id = getId();
15442 if (id != -1) {
15443 output += " (id=" + id + ")";
15444 }
15445 Object tag = getTag();
15446 if (tag != null) {
15447 output += " (tag=" + tag + ")";
15448 }
15449 Log.d(VIEW_LOG_TAG, output);
15450
Dianne Hackborn4702a852012-08-17 15:18:29 -070015451 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 output = debugIndent(depth) + " FOCUSED";
15453 Log.d(VIEW_LOG_TAG, output);
15454 }
15455
15456 output = debugIndent(depth);
15457 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15458 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15459 + "} ";
15460 Log.d(VIEW_LOG_TAG, output);
15461
15462 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15463 || mPaddingBottom != 0) {
15464 output = debugIndent(depth);
15465 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15466 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15467 Log.d(VIEW_LOG_TAG, output);
15468 }
15469
15470 output = debugIndent(depth);
15471 output += "mMeasureWidth=" + mMeasuredWidth +
15472 " mMeasureHeight=" + mMeasuredHeight;
15473 Log.d(VIEW_LOG_TAG, output);
15474
15475 output = debugIndent(depth);
15476 if (mLayoutParams == null) {
15477 output += "BAD! no layout params";
15478 } else {
15479 output = mLayoutParams.debug(output);
15480 }
15481 Log.d(VIEW_LOG_TAG, output);
15482
15483 output = debugIndent(depth);
15484 output += "flags={";
15485 output += View.printFlags(mViewFlags);
15486 output += "}";
15487 Log.d(VIEW_LOG_TAG, output);
15488
15489 output = debugIndent(depth);
15490 output += "privateFlags={";
15491 output += View.printPrivateFlags(mPrivateFlags);
15492 output += "}";
15493 Log.d(VIEW_LOG_TAG, output);
15494 }
15495
15496 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015497 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015498 *
15499 * @param depth the indentation level
15500 * @return a String containing (depth * 2 + 3) * 2 white spaces
15501 *
15502 * @hide
15503 */
15504 protected static String debugIndent(int depth) {
15505 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15506 for (int i = 0; i < (depth * 2) + 3; i++) {
15507 spaces.append(' ').append(' ');
15508 }
15509 return spaces.toString();
15510 }
15511
15512 /**
15513 * <p>Return the offset of the widget's text baseline from the widget's top
15514 * boundary. If this widget does not support baseline alignment, this
15515 * method returns -1. </p>
15516 *
15517 * @return the offset of the baseline within the widget's bounds or -1
15518 * if baseline alignment is not supported
15519 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015520 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015521 public int getBaseline() {
15522 return -1;
15523 }
15524
15525 /**
Chet Haase97140572012-09-13 14:56:47 -070015526 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15527 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15528 * a layout pass.
15529 *
15530 * @return whether the view hierarchy is currently undergoing a layout pass
15531 */
15532 public boolean isInLayout() {
15533 ViewRootImpl viewRoot = getViewRootImpl();
15534 return (viewRoot != null && viewRoot.isInLayout());
15535 }
15536
15537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015538 * Call this when something has changed which has invalidated the
15539 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015540 * tree. This should not be called while the view hierarchy is currently in a layout
15541 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15542 * end of the current layout pass (and then layout will run again) or after the current
15543 * frame is drawn and the next layout occurs.
15544 *
15545 * <p>Subclasses which override this method should call the superclass method to
15546 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015547 */
15548 public void requestLayout() {
Chet Haasecc699b42012-12-13 09:06:55 -080015549 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
15550 // Only trigger request-during-layout logic if this is the view requesting it,
15551 // not the views in its parent hierarchy
15552 ViewRootImpl viewRoot = getViewRootImpl();
15553 if (viewRoot != null && viewRoot.isInLayout()) {
15554 if (!viewRoot.requestLayoutDuringLayout(this)) {
15555 return;
15556 }
15557 }
15558 mAttachInfo.mViewRequestingLayout = this;
Chet Haase97140572012-09-13 14:56:47 -070015559 }
Chet Haasecc699b42012-12-13 09:06:55 -080015560
Dianne Hackborn4702a852012-08-17 15:18:29 -070015561 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15562 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015563
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015564 if (mParent != null && !mParent.isLayoutRequested()) {
15565 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015566 }
Chet Haasecc699b42012-12-13 09:06:55 -080015567 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
15568 mAttachInfo.mViewRequestingLayout = null;
15569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015570 }
15571
15572 /**
15573 * Forces this view to be laid out during the next layout pass.
15574 * This method does not call requestLayout() or forceLayout()
15575 * on the parent.
15576 */
15577 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015578 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15579 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015580 }
15581
15582 /**
15583 * <p>
15584 * This is called to find out how big a view should be. The parent
15585 * supplies constraint information in the width and height parameters.
15586 * </p>
15587 *
15588 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015589 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015590 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015591 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015592 * </p>
15593 *
15594 *
15595 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15596 * parent
15597 * @param heightMeasureSpec Vertical space requirements as imposed by the
15598 * parent
15599 *
15600 * @see #onMeasure(int, int)
15601 */
15602 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015603 boolean optical = isLayoutModeOptical(this);
15604 if (optical != isLayoutModeOptical(mParent)) {
15605 Insets insets = getOpticalInsets();
15606 int oWidth = insets.left + insets.right;
15607 int oHeight = insets.top + insets.bottom;
15608 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15609 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15610 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015611 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015612 widthMeasureSpec != mOldWidthMeasureSpec ||
15613 heightMeasureSpec != mOldHeightMeasureSpec) {
15614
15615 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015616 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015617
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015618 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015620 // measure ourselves, this should set the measured dimension flag back
15621 onMeasure(widthMeasureSpec, heightMeasureSpec);
15622
15623 // flag not set, setMeasuredDimension() was not invoked, we raise
15624 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015625 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015626 throw new IllegalStateException("onMeasure() did not set the"
15627 + " measured dimension by calling"
15628 + " setMeasuredDimension()");
15629 }
15630
Dianne Hackborn4702a852012-08-17 15:18:29 -070015631 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015632 }
15633
15634 mOldWidthMeasureSpec = widthMeasureSpec;
15635 mOldHeightMeasureSpec = heightMeasureSpec;
15636 }
15637
15638 /**
15639 * <p>
15640 * Measure the view and its content to determine the measured width and the
15641 * measured height. This method is invoked by {@link #measure(int, int)} and
15642 * should be overriden by subclasses to provide accurate and efficient
15643 * measurement of their contents.
15644 * </p>
15645 *
15646 * <p>
15647 * <strong>CONTRACT:</strong> When overriding this method, you
15648 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15649 * measured width and height of this view. Failure to do so will trigger an
15650 * <code>IllegalStateException</code>, thrown by
15651 * {@link #measure(int, int)}. Calling the superclass'
15652 * {@link #onMeasure(int, int)} is a valid use.
15653 * </p>
15654 *
15655 * <p>
15656 * The base class implementation of measure defaults to the background size,
15657 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15658 * override {@link #onMeasure(int, int)} to provide better measurements of
15659 * their content.
15660 * </p>
15661 *
15662 * <p>
15663 * If this method is overridden, it is the subclass's responsibility to make
15664 * sure the measured height and width are at least the view's minimum height
15665 * and width ({@link #getSuggestedMinimumHeight()} and
15666 * {@link #getSuggestedMinimumWidth()}).
15667 * </p>
15668 *
15669 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15670 * The requirements are encoded with
15671 * {@link android.view.View.MeasureSpec}.
15672 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15673 * The requirements are encoded with
15674 * {@link android.view.View.MeasureSpec}.
15675 *
15676 * @see #getMeasuredWidth()
15677 * @see #getMeasuredHeight()
15678 * @see #setMeasuredDimension(int, int)
15679 * @see #getSuggestedMinimumHeight()
15680 * @see #getSuggestedMinimumWidth()
15681 * @see android.view.View.MeasureSpec#getMode(int)
15682 * @see android.view.View.MeasureSpec#getSize(int)
15683 */
15684 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15685 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15686 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15687 }
15688
15689 /**
15690 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15691 * measured width and measured height. Failing to do so will trigger an
15692 * exception at measurement time.</p>
15693 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015694 * @param measuredWidth The measured width of this view. May be a complex
15695 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15696 * {@link #MEASURED_STATE_TOO_SMALL}.
15697 * @param measuredHeight The measured height of this view. May be a complex
15698 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15699 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015700 */
15701 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015702 boolean optical = isLayoutModeOptical(this);
15703 if (optical != isLayoutModeOptical(mParent)) {
15704 Insets insets = getOpticalInsets();
15705 int opticalWidth = insets.left + insets.right;
15706 int opticalHeight = insets.top + insets.bottom;
15707
15708 measuredWidth += optical ? opticalWidth : -opticalWidth;
15709 measuredHeight += optical ? opticalHeight : -opticalHeight;
15710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015711 mMeasuredWidth = measuredWidth;
15712 mMeasuredHeight = measuredHeight;
15713
Dianne Hackborn4702a852012-08-17 15:18:29 -070015714 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015715 }
15716
15717 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015718 * Merge two states as returned by {@link #getMeasuredState()}.
15719 * @param curState The current state as returned from a view or the result
15720 * of combining multiple views.
15721 * @param newState The new view state to combine.
15722 * @return Returns a new integer reflecting the combination of the two
15723 * states.
15724 */
15725 public static int combineMeasuredStates(int curState, int newState) {
15726 return curState | newState;
15727 }
15728
15729 /**
15730 * Version of {@link #resolveSizeAndState(int, int, int)}
15731 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15732 */
15733 public static int resolveSize(int size, int measureSpec) {
15734 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15735 }
15736
15737 /**
15738 * Utility to reconcile a desired size and state, with constraints imposed
15739 * by a MeasureSpec. Will take the desired size, unless a different size
15740 * is imposed by the constraints. The returned value is a compound integer,
15741 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15742 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15743 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015744 *
15745 * @param size How big the view wants to be
15746 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015747 * @return Size information bit mask as defined by
15748 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015749 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015750 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015751 int result = size;
15752 int specMode = MeasureSpec.getMode(measureSpec);
15753 int specSize = MeasureSpec.getSize(measureSpec);
15754 switch (specMode) {
15755 case MeasureSpec.UNSPECIFIED:
15756 result = size;
15757 break;
15758 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015759 if (specSize < size) {
15760 result = specSize | MEASURED_STATE_TOO_SMALL;
15761 } else {
15762 result = size;
15763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015764 break;
15765 case MeasureSpec.EXACTLY:
15766 result = specSize;
15767 break;
15768 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015769 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015770 }
15771
15772 /**
15773 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015774 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015775 * by the MeasureSpec.
15776 *
15777 * @param size Default size for this view
15778 * @param measureSpec Constraints imposed by the parent
15779 * @return The size this view should be.
15780 */
15781 public static int getDefaultSize(int size, int measureSpec) {
15782 int result = size;
15783 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015784 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015785
15786 switch (specMode) {
15787 case MeasureSpec.UNSPECIFIED:
15788 result = size;
15789 break;
15790 case MeasureSpec.AT_MOST:
15791 case MeasureSpec.EXACTLY:
15792 result = specSize;
15793 break;
15794 }
15795 return result;
15796 }
15797
15798 /**
15799 * Returns the suggested minimum height that the view should use. This
15800 * returns the maximum of the view's minimum height
15801 * and the background's minimum height
15802 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15803 * <p>
15804 * When being used in {@link #onMeasure(int, int)}, the caller should still
15805 * ensure the returned height is within the requirements of the parent.
15806 *
15807 * @return The suggested minimum height of the view.
15808 */
15809 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015810 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015812 }
15813
15814 /**
15815 * Returns the suggested minimum width that the view should use. This
15816 * returns the maximum of the view's minimum width)
15817 * and the background's minimum width
15818 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15819 * <p>
15820 * When being used in {@link #onMeasure(int, int)}, the caller should still
15821 * ensure the returned width is within the requirements of the parent.
15822 *
15823 * @return The suggested minimum width of the view.
15824 */
15825 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015826 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015828
Philip Milne6c8ea062012-04-03 17:38:43 -070015829 /**
15830 * Returns the minimum height of the view.
15831 *
15832 * @return the minimum height the view will try to be.
15833 *
15834 * @see #setMinimumHeight(int)
15835 *
15836 * @attr ref android.R.styleable#View_minHeight
15837 */
15838 public int getMinimumHeight() {
15839 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015840 }
15841
15842 /**
15843 * Sets the minimum height of the view. It is not guaranteed the view will
15844 * be able to achieve this minimum height (for example, if its parent layout
15845 * constrains it with less available height).
15846 *
15847 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015848 *
15849 * @see #getMinimumHeight()
15850 *
15851 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015852 */
15853 public void setMinimumHeight(int minHeight) {
15854 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015855 requestLayout();
15856 }
15857
15858 /**
15859 * Returns the minimum width of the view.
15860 *
15861 * @return the minimum width the view will try to be.
15862 *
15863 * @see #setMinimumWidth(int)
15864 *
15865 * @attr ref android.R.styleable#View_minWidth
15866 */
15867 public int getMinimumWidth() {
15868 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015869 }
15870
15871 /**
15872 * Sets the minimum width of the view. It is not guaranteed the view will
15873 * be able to achieve this minimum width (for example, if its parent layout
15874 * constrains it with less available width).
15875 *
15876 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015877 *
15878 * @see #getMinimumWidth()
15879 *
15880 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015881 */
15882 public void setMinimumWidth(int minWidth) {
15883 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015884 requestLayout();
15885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015886 }
15887
15888 /**
15889 * Get the animation currently associated with this view.
15890 *
15891 * @return The animation that is currently playing or
15892 * scheduled to play for this view.
15893 */
15894 public Animation getAnimation() {
15895 return mCurrentAnimation;
15896 }
15897
15898 /**
15899 * Start the specified animation now.
15900 *
15901 * @param animation the animation to start now
15902 */
15903 public void startAnimation(Animation animation) {
15904 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15905 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015906 invalidateParentCaches();
15907 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015908 }
15909
15910 /**
15911 * Cancels any animations for this view.
15912 */
15913 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015914 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015915 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015917 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015918 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015919 }
15920
15921 /**
15922 * Sets the next animation to play for this view.
15923 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015924 * {@link #startAnimation(android.view.animation.Animation)} instead.
15925 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015926 * control over the start time and invalidation, but you
15927 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015928 * 2) the view's parent (which controls animations on its children)
15929 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015930 * start.
15931 *
15932 * @param animation The next animation, or null.
15933 */
15934 public void setAnimation(Animation animation) {
15935 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015937 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015938 // If the screen is off assume the animation start time is now instead of
15939 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15940 // would cause the animation to start when the screen turns back on
15941 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15942 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15943 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015945 animation.reset();
15946 }
15947 }
15948
15949 /**
15950 * Invoked by a parent ViewGroup to notify the start of the animation
15951 * currently associated with this view. If you override this method,
15952 * always call super.onAnimationStart();
15953 *
15954 * @see #setAnimation(android.view.animation.Animation)
15955 * @see #getAnimation()
15956 */
15957 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015958 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015959 }
15960
15961 /**
15962 * Invoked by a parent ViewGroup to notify the end of the animation
15963 * currently associated with this view. If you override this method,
15964 * always call super.onAnimationEnd();
15965 *
15966 * @see #setAnimation(android.view.animation.Animation)
15967 * @see #getAnimation()
15968 */
15969 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015970 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015971 }
15972
15973 /**
15974 * Invoked if there is a Transform that involves alpha. Subclass that can
15975 * draw themselves with the specified alpha should return true, and then
15976 * respect that alpha when their onDraw() is called. If this returns false
15977 * then the view may be redirected to draw into an offscreen buffer to
15978 * fulfill the request, which will look fine, but may be slower than if the
15979 * subclass handles it internally. The default implementation returns false.
15980 *
15981 * @param alpha The alpha (0..255) to apply to the view's drawing
15982 * @return true if the view can draw with the specified alpha.
15983 */
15984 protected boolean onSetAlpha(int alpha) {
15985 return false;
15986 }
15987
15988 /**
15989 * This is used by the RootView to perform an optimization when
15990 * the view hierarchy contains one or several SurfaceView.
15991 * SurfaceView is always considered transparent, but its children are not,
15992 * therefore all View objects remove themselves from the global transparent
15993 * region (passed as a parameter to this function).
15994 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015995 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015996 *
15997 * @return Returns true if the effective visibility of the view at this
15998 * point is opaque, regardless of the transparent region; returns false
15999 * if it is possible for underlying windows to be seen behind the view.
16000 *
16001 * {@hide}
16002 */
16003 public boolean gatherTransparentRegion(Region region) {
16004 final AttachInfo attachInfo = mAttachInfo;
16005 if (region != null && attachInfo != null) {
16006 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070016007 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016008 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
16009 // remove it from the transparent region.
16010 final int[] location = attachInfo.mTransparentLocation;
16011 getLocationInWindow(location);
16012 region.op(location[0], location[1], location[0] + mRight - mLeft,
16013 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070016014 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016015 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
16016 // exists, so we remove the background drawable's non-transparent
16017 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070016018 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016019 }
16020 }
16021 return true;
16022 }
16023
16024 /**
16025 * Play a sound effect for this view.
16026 *
16027 * <p>The framework will play sound effects for some built in actions, such as
16028 * clicking, but you may wish to play these effects in your widget,
16029 * for instance, for internal navigation.
16030 *
16031 * <p>The sound effect will only be played if sound effects are enabled by the user, and
16032 * {@link #isSoundEffectsEnabled()} is true.
16033 *
16034 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
16035 */
16036 public void playSoundEffect(int soundConstant) {
16037 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
16038 return;
16039 }
16040 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
16041 }
16042
16043 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016044 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016045 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016046 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016047 *
16048 * <p>The framework will provide haptic feedback for some built in actions,
16049 * such as long presses, but you may wish to provide feedback for your
16050 * own widget.
16051 *
16052 * <p>The feedback will only be performed if
16053 * {@link #isHapticFeedbackEnabled()} is true.
16054 *
16055 * @param feedbackConstant One of the constants defined in
16056 * {@link HapticFeedbackConstants}
16057 */
16058 public boolean performHapticFeedback(int feedbackConstant) {
16059 return performHapticFeedback(feedbackConstant, 0);
16060 }
16061
16062 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016063 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016064 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016065 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016066 *
16067 * @param feedbackConstant One of the constants defined in
16068 * {@link HapticFeedbackConstants}
16069 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16070 */
16071 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16072 if (mAttachInfo == null) {
16073 return false;
16074 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016075 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016076 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016077 && !isHapticFeedbackEnabled()) {
16078 return false;
16079 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016080 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16081 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016082 }
16083
16084 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016085 * Request that the visibility of the status bar or other screen/window
16086 * decorations be changed.
16087 *
16088 * <p>This method is used to put the over device UI into temporary modes
16089 * where the user's attention is focused more on the application content,
16090 * by dimming or hiding surrounding system affordances. This is typically
16091 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16092 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16093 * to be placed behind the action bar (and with these flags other system
16094 * affordances) so that smooth transitions between hiding and showing them
16095 * can be done.
16096 *
16097 * <p>Two representative examples of the use of system UI visibility is
16098 * implementing a content browsing application (like a magazine reader)
16099 * and a video playing application.
16100 *
16101 * <p>The first code shows a typical implementation of a View in a content
16102 * browsing application. In this implementation, the application goes
16103 * into a content-oriented mode by hiding the status bar and action bar,
16104 * and putting the navigation elements into lights out mode. The user can
16105 * then interact with content while in this mode. Such an application should
16106 * provide an easy way for the user to toggle out of the mode (such as to
16107 * check information in the status bar or access notifications). In the
16108 * implementation here, this is done simply by tapping on the content.
16109 *
16110 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16111 * content}
16112 *
16113 * <p>This second code sample shows a typical implementation of a View
16114 * in a video playing application. In this situation, while the video is
16115 * playing the application would like to go into a complete full-screen mode,
16116 * to use as much of the display as possible for the video. When in this state
16117 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016118 * touching on the screen to pop the UI out of full screen mode. See
16119 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016120 *
16121 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16122 * content}
16123 *
16124 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16125 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16126 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16127 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016128 */
16129 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016130 if (visibility != mSystemUiVisibility) {
16131 mSystemUiVisibility = visibility;
16132 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16133 mParent.recomputeViewAttributes(this);
16134 }
Joe Onorato664644d2011-01-23 17:53:23 -080016135 }
16136 }
16137
16138 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016139 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16140 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16141 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16142 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16143 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016144 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016145 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016146 return mSystemUiVisibility;
16147 }
16148
Scott Mainec6331b2011-05-24 16:55:56 -070016149 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016150 * Returns the current system UI visibility that is currently set for
16151 * the entire window. This is the combination of the
16152 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16153 * views in the window.
16154 */
16155 public int getWindowSystemUiVisibility() {
16156 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16157 }
16158
16159 /**
16160 * Override to find out when the window's requested system UI visibility
16161 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16162 * This is different from the callbacks recieved through
16163 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16164 * in that this is only telling you about the local request of the window,
16165 * not the actual values applied by the system.
16166 */
16167 public void onWindowSystemUiVisibilityChanged(int visible) {
16168 }
16169
16170 /**
16171 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16172 * the view hierarchy.
16173 */
16174 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16175 onWindowSystemUiVisibilityChanged(visible);
16176 }
16177
16178 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016179 * Set a listener to receive callbacks when the visibility of the system bar changes.
16180 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16181 */
Joe Onorato664644d2011-01-23 17:53:23 -080016182 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016183 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016184 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16185 mParent.recomputeViewAttributes(this);
16186 }
16187 }
16188
16189 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016190 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16191 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016192 */
16193 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016194 ListenerInfo li = mListenerInfo;
16195 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16196 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016197 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016198 }
16199 }
16200
Dianne Hackborncf675782012-05-10 15:07:24 -070016201 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016202 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16203 if (val != mSystemUiVisibility) {
16204 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016205 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016206 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016207 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016208 }
16209
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016210 /** @hide */
16211 public void setDisabledSystemUiVisibility(int flags) {
16212 if (mAttachInfo != null) {
16213 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16214 mAttachInfo.mDisabledSystemUiVisibility = flags;
16215 if (mParent != null) {
16216 mParent.recomputeViewAttributes(this);
16217 }
16218 }
16219 }
16220 }
16221
Joe Onorato664644d2011-01-23 17:53:23 -080016222 /**
Joe Malin32736f02011-01-19 16:14:20 -080016223 * Creates an image that the system displays during the drag and drop
16224 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16225 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16226 * appearance as the given View. The default also positions the center of the drag shadow
16227 * directly under the touch point. If no View is provided (the constructor with no parameters
16228 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16229 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16230 * default is an invisible drag shadow.
16231 * <p>
16232 * You are not required to use the View you provide to the constructor as the basis of the
16233 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16234 * anything you want as the drag shadow.
16235 * </p>
16236 * <p>
16237 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16238 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16239 * size and position of the drag shadow. It uses this data to construct a
16240 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16241 * so that your application can draw the shadow image in the Canvas.
16242 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016243 *
16244 * <div class="special reference">
16245 * <h3>Developer Guides</h3>
16246 * <p>For a guide to implementing drag and drop features, read the
16247 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16248 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016249 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016250 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016251 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016252
16253 /**
Joe Malin32736f02011-01-19 16:14:20 -080016254 * Constructs a shadow image builder based on a View. By default, the resulting drag
16255 * shadow will have the same appearance and dimensions as the View, with the touch point
16256 * over the center of the View.
16257 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016258 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016259 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016260 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016261 }
16262
Christopher Tate17ed60c2011-01-18 12:50:26 -080016263 /**
16264 * Construct a shadow builder object with no associated View. This
16265 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16266 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16267 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016268 * reference to any View object. If they are not overridden, then the result is an
16269 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016270 */
16271 public DragShadowBuilder() {
16272 mView = new WeakReference<View>(null);
16273 }
16274
16275 /**
16276 * Returns the View object that had been passed to the
16277 * {@link #View.DragShadowBuilder(View)}
16278 * constructor. If that View parameter was {@code null} or if the
16279 * {@link #View.DragShadowBuilder()}
16280 * constructor was used to instantiate the builder object, this method will return
16281 * null.
16282 *
16283 * @return The View object associate with this builder object.
16284 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016285 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016286 final public View getView() {
16287 return mView.get();
16288 }
16289
Christopher Tate2c095f32010-10-04 14:13:40 -070016290 /**
Joe Malin32736f02011-01-19 16:14:20 -080016291 * Provides the metrics for the shadow image. These include the dimensions of
16292 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016293 * be centered under the touch location while dragging.
16294 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016295 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016296 * same as the dimensions of the View itself and centers the shadow under
16297 * the touch point.
16298 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016299 *
Joe Malin32736f02011-01-19 16:14:20 -080016300 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16301 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16302 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16303 * image.
16304 *
16305 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16306 * shadow image that should be underneath the touch point during the drag and drop
16307 * operation. Your application must set {@link android.graphics.Point#x} to the
16308 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016309 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016310 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016311 final View view = mView.get();
16312 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016313 shadowSize.set(view.getWidth(), view.getHeight());
16314 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016315 } else {
16316 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16317 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016318 }
16319
16320 /**
Joe Malin32736f02011-01-19 16:14:20 -080016321 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16322 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016323 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016324 *
Joe Malin32736f02011-01-19 16:14:20 -080016325 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016326 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016327 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016328 final View view = mView.get();
16329 if (view != null) {
16330 view.draw(canvas);
16331 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016332 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016333 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016334 }
16335 }
16336
16337 /**
Joe Malin32736f02011-01-19 16:14:20 -080016338 * Starts a drag and drop operation. When your application calls this method, it passes a
16339 * {@link android.view.View.DragShadowBuilder} object to the system. The
16340 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16341 * to get metrics for the drag shadow, and then calls the object's
16342 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16343 * <p>
16344 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16345 * drag events to all the View objects in your application that are currently visible. It does
16346 * this either by calling the View object's drag listener (an implementation of
16347 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16348 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16349 * Both are passed a {@link android.view.DragEvent} object that has a
16350 * {@link android.view.DragEvent#getAction()} value of
16351 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16352 * </p>
16353 * <p>
16354 * Your application can invoke startDrag() on any attached View object. The View object does not
16355 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16356 * be related to the View the user selected for dragging.
16357 * </p>
16358 * @param data A {@link android.content.ClipData} object pointing to the data to be
16359 * transferred by the drag and drop operation.
16360 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16361 * drag shadow.
16362 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16363 * drop operation. This Object is put into every DragEvent object sent by the system during the
16364 * current drag.
16365 * <p>
16366 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16367 * to the target Views. For example, it can contain flags that differentiate between a
16368 * a copy operation and a move operation.
16369 * </p>
16370 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16371 * so the parameter should be set to 0.
16372 * @return {@code true} if the method completes successfully, or
16373 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16374 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016375 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016376 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016377 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016378 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016379 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016380 }
16381 boolean okay = false;
16382
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016383 Point shadowSize = new Point();
16384 Point shadowTouchPoint = new Point();
16385 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016386
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016387 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16388 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16389 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016390 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016391
Chris Tatea32dcf72010-10-14 12:13:50 -070016392 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016393 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16394 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016395 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016396 Surface surface = new Surface();
16397 try {
16398 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016399 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016400 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016401 + " surface=" + surface);
16402 if (token != null) {
16403 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016404 try {
Chris Tate6b391282010-10-14 15:48:59 -070016405 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016406 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016407 } finally {
16408 surface.unlockCanvasAndPost(canvas);
16409 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016410
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016411 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016412
16413 // Cache the local state object for delivery with DragEvents
16414 root.setLocalDragState(myLocalState);
16415
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016416 // repurpose 'shadowSize' for the last touch point
16417 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016418
Christopher Tatea53146c2010-09-07 11:57:52 -070016419 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016420 shadowSize.x, shadowSize.y,
16421 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016422 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016423
16424 // Off and running! Release our local surface instance; the drag
16425 // shadow surface is now managed by the system process.
16426 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016427 }
16428 } catch (Exception e) {
16429 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16430 surface.destroy();
16431 }
16432
16433 return okay;
16434 }
16435
Christopher Tatea53146c2010-09-07 11:57:52 -070016436 /**
Joe Malin32736f02011-01-19 16:14:20 -080016437 * Handles drag events sent by the system following a call to
16438 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16439 *<p>
16440 * When the system calls this method, it passes a
16441 * {@link android.view.DragEvent} object. A call to
16442 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16443 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16444 * operation.
16445 * @param event The {@link android.view.DragEvent} sent by the system.
16446 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16447 * in DragEvent, indicating the type of drag event represented by this object.
16448 * @return {@code true} if the method was successful, otherwise {@code false}.
16449 * <p>
16450 * The method should return {@code true} in response to an action type of
16451 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16452 * operation.
16453 * </p>
16454 * <p>
16455 * The method should also return {@code true} in response to an action type of
16456 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16457 * {@code false} if it didn't.
16458 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016459 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016460 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016461 return false;
16462 }
16463
16464 /**
Joe Malin32736f02011-01-19 16:14:20 -080016465 * Detects if this View is enabled and has a drag event listener.
16466 * If both are true, then it calls the drag event listener with the
16467 * {@link android.view.DragEvent} it received. If the drag event listener returns
16468 * {@code true}, then dispatchDragEvent() returns {@code true}.
16469 * <p>
16470 * For all other cases, the method calls the
16471 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16472 * method and returns its result.
16473 * </p>
16474 * <p>
16475 * This ensures that a drag event is always consumed, even if the View does not have a drag
16476 * event listener. However, if the View has a listener and the listener returns true, then
16477 * onDragEvent() is not called.
16478 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016479 */
16480 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016481 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016482 ListenerInfo li = mListenerInfo;
16483 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16484 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016485 return true;
16486 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016487 return onDragEvent(event);
16488 }
16489
Christopher Tate3d4bf172011-03-28 16:16:46 -070016490 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016491 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016492 }
16493
Christopher Tatea53146c2010-09-07 11:57:52 -070016494 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016495 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16496 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016497 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016498 */
16499 public void onCloseSystemDialogs(String reason) {
16500 }
Joe Malin32736f02011-01-19 16:14:20 -080016501
Dianne Hackbornffa42482009-09-23 22:20:11 -070016502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016503 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016504 * update a Region being computed for
16505 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016506 * that any non-transparent parts of the Drawable are removed from the
16507 * given transparent region.
16508 *
16509 * @param dr The Drawable whose transparency is to be applied to the region.
16510 * @param region A Region holding the current transparency information,
16511 * where any parts of the region that are set are considered to be
16512 * transparent. On return, this region will be modified to have the
16513 * transparency information reduced by the corresponding parts of the
16514 * Drawable that are not transparent.
16515 * {@hide}
16516 */
16517 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16518 if (DBG) {
16519 Log.i("View", "Getting transparent region for: " + this);
16520 }
16521 final Region r = dr.getTransparentRegion();
16522 final Rect db = dr.getBounds();
16523 final AttachInfo attachInfo = mAttachInfo;
16524 if (r != null && attachInfo != null) {
16525 final int w = getRight()-getLeft();
16526 final int h = getBottom()-getTop();
16527 if (db.left > 0) {
16528 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16529 r.op(0, 0, db.left, h, Region.Op.UNION);
16530 }
16531 if (db.right < w) {
16532 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16533 r.op(db.right, 0, w, h, Region.Op.UNION);
16534 }
16535 if (db.top > 0) {
16536 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16537 r.op(0, 0, w, db.top, Region.Op.UNION);
16538 }
16539 if (db.bottom < h) {
16540 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16541 r.op(0, db.bottom, w, h, Region.Op.UNION);
16542 }
16543 final int[] location = attachInfo.mTransparentLocation;
16544 getLocationInWindow(location);
16545 r.translate(location[0], location[1]);
16546 region.op(r, Region.Op.INTERSECT);
16547 } else {
16548 region.op(db, Region.Op.DIFFERENCE);
16549 }
16550 }
16551
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016552 private void checkForLongClick(int delayOffset) {
16553 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16554 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016555
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016556 if (mPendingCheckForLongPress == null) {
16557 mPendingCheckForLongPress = new CheckForLongPress();
16558 }
16559 mPendingCheckForLongPress.rememberWindowAttachCount();
16560 postDelayed(mPendingCheckForLongPress,
16561 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016563 }
16564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016565 /**
16566 * Inflate a view from an XML resource. This convenience method wraps the {@link
16567 * LayoutInflater} class, which provides a full range of options for view inflation.
16568 *
16569 * @param context The Context object for your activity or application.
16570 * @param resource The resource ID to inflate
16571 * @param root A view group that will be the parent. Used to properly inflate the
16572 * layout_* parameters.
16573 * @see LayoutInflater
16574 */
16575 public static View inflate(Context context, int resource, ViewGroup root) {
16576 LayoutInflater factory = LayoutInflater.from(context);
16577 return factory.inflate(resource, root);
16578 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016580 /**
Adam Powell637d3372010-08-25 14:37:03 -070016581 * Scroll the view with standard behavior for scrolling beyond the normal
16582 * content boundaries. Views that call this method should override
16583 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16584 * results of an over-scroll operation.
16585 *
16586 * Views can use this method to handle any touch or fling-based scrolling.
16587 *
16588 * @param deltaX Change in X in pixels
16589 * @param deltaY Change in Y in pixels
16590 * @param scrollX Current X scroll value in pixels before applying deltaX
16591 * @param scrollY Current Y scroll value in pixels before applying deltaY
16592 * @param scrollRangeX Maximum content scroll range along the X axis
16593 * @param scrollRangeY Maximum content scroll range along the Y axis
16594 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16595 * along the X axis.
16596 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16597 * along the Y axis.
16598 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16599 * @return true if scrolling was clamped to an over-scroll boundary along either
16600 * axis, false otherwise.
16601 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016602 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016603 protected boolean overScrollBy(int deltaX, int deltaY,
16604 int scrollX, int scrollY,
16605 int scrollRangeX, int scrollRangeY,
16606 int maxOverScrollX, int maxOverScrollY,
16607 boolean isTouchEvent) {
16608 final int overScrollMode = mOverScrollMode;
16609 final boolean canScrollHorizontal =
16610 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16611 final boolean canScrollVertical =
16612 computeVerticalScrollRange() > computeVerticalScrollExtent();
16613 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16614 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16615 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16616 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16617
16618 int newScrollX = scrollX + deltaX;
16619 if (!overScrollHorizontal) {
16620 maxOverScrollX = 0;
16621 }
16622
16623 int newScrollY = scrollY + deltaY;
16624 if (!overScrollVertical) {
16625 maxOverScrollY = 0;
16626 }
16627
16628 // Clamp values if at the limits and record
16629 final int left = -maxOverScrollX;
16630 final int right = maxOverScrollX + scrollRangeX;
16631 final int top = -maxOverScrollY;
16632 final int bottom = maxOverScrollY + scrollRangeY;
16633
16634 boolean clampedX = false;
16635 if (newScrollX > right) {
16636 newScrollX = right;
16637 clampedX = true;
16638 } else if (newScrollX < left) {
16639 newScrollX = left;
16640 clampedX = true;
16641 }
16642
16643 boolean clampedY = false;
16644 if (newScrollY > bottom) {
16645 newScrollY = bottom;
16646 clampedY = true;
16647 } else if (newScrollY < top) {
16648 newScrollY = top;
16649 clampedY = true;
16650 }
16651
16652 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16653
16654 return clampedX || clampedY;
16655 }
16656
16657 /**
16658 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16659 * respond to the results of an over-scroll operation.
16660 *
16661 * @param scrollX New X scroll value in pixels
16662 * @param scrollY New Y scroll value in pixels
16663 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16664 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16665 */
16666 protected void onOverScrolled(int scrollX, int scrollY,
16667 boolean clampedX, boolean clampedY) {
16668 // Intentionally empty.
16669 }
16670
16671 /**
16672 * Returns the over-scroll mode for this view. The result will be
16673 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16674 * (allow over-scrolling only if the view content is larger than the container),
16675 * or {@link #OVER_SCROLL_NEVER}.
16676 *
16677 * @return This view's over-scroll mode.
16678 */
16679 public int getOverScrollMode() {
16680 return mOverScrollMode;
16681 }
16682
16683 /**
16684 * Set the over-scroll mode for this view. Valid over-scroll modes are
16685 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16686 * (allow over-scrolling only if the view content is larger than the container),
16687 * or {@link #OVER_SCROLL_NEVER}.
16688 *
16689 * Setting the over-scroll mode of a view will have an effect only if the
16690 * view is capable of scrolling.
16691 *
16692 * @param overScrollMode The new over-scroll mode for this view.
16693 */
16694 public void setOverScrollMode(int overScrollMode) {
16695 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16696 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16697 overScrollMode != OVER_SCROLL_NEVER) {
16698 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16699 }
16700 mOverScrollMode = overScrollMode;
16701 }
16702
16703 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016704 * Gets a scale factor that determines the distance the view should scroll
16705 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16706 * @return The vertical scroll scale factor.
16707 * @hide
16708 */
16709 protected float getVerticalScrollFactor() {
16710 if (mVerticalScrollFactor == 0) {
16711 TypedValue outValue = new TypedValue();
16712 if (!mContext.getTheme().resolveAttribute(
16713 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16714 throw new IllegalStateException(
16715 "Expected theme to define listPreferredItemHeight.");
16716 }
16717 mVerticalScrollFactor = outValue.getDimension(
16718 mContext.getResources().getDisplayMetrics());
16719 }
16720 return mVerticalScrollFactor;
16721 }
16722
16723 /**
16724 * Gets a scale factor that determines the distance the view should scroll
16725 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16726 * @return The horizontal scroll scale factor.
16727 * @hide
16728 */
16729 protected float getHorizontalScrollFactor() {
16730 // TODO: Should use something else.
16731 return getVerticalScrollFactor();
16732 }
16733
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016734 /**
16735 * Return the value specifying the text direction or policy that was set with
16736 * {@link #setTextDirection(int)}.
16737 *
16738 * @return the defined text direction. It can be one of:
16739 *
16740 * {@link #TEXT_DIRECTION_INHERIT},
16741 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16742 * {@link #TEXT_DIRECTION_ANY_RTL},
16743 * {@link #TEXT_DIRECTION_LTR},
16744 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016745 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016746 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016747 * @attr ref android.R.styleable#View_textDirection
16748 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016749 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016750 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016751 @ViewDebug.ExportedProperty(category = "text", mapping = {
16752 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16753 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16754 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16755 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16756 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16757 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16758 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016759 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016760 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016761 }
16762
16763 /**
16764 * Set the text direction.
16765 *
16766 * @param textDirection the direction to set. Should be one of:
16767 *
16768 * {@link #TEXT_DIRECTION_INHERIT},
16769 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16770 * {@link #TEXT_DIRECTION_ANY_RTL},
16771 * {@link #TEXT_DIRECTION_LTR},
16772 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016773 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016774 *
16775 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16776 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16777 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016778 *
16779 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016780 */
16781 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016782 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016783 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016784 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016785 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016786 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016787 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016788 // Do resolution
16789 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016790 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016791 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016792 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016793 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016794 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016795 }
16796 }
16797
16798 /**
16799 * Return the resolved text direction.
16800 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016801 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016802 *
Doug Feltcb3791202011-07-07 11:57:48 -070016803 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16804 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016805 * {@link #TEXT_DIRECTION_LTR},
16806 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016807 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016808 *
16809 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016810 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016811 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016812 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016813 }
16814
16815 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016816 * Resolve the text direction.
16817 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016818 * @return true if resolution has been done, false otherwise.
16819 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016820 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016821 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016822 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016823 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016824 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016825
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016826 if (hasRtlSupport()) {
16827 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016828 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016829 switch(textDirection) {
16830 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016831 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016832 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016833 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016834 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016835 return false;
16836 }
16837
16838 View parent = ((View) mParent);
16839 // Parent has not yet resolved, so we still return the default
16840 if (!parent.isTextDirectionResolved()) {
16841 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16842 // Resolution will need to happen again later
16843 return false;
16844 }
16845
16846 // Set current resolved direction to the same value as the parent's one
16847 final int parentResolvedDirection = parent.getTextDirection();
16848 switch (parentResolvedDirection) {
16849 case TEXT_DIRECTION_FIRST_STRONG:
16850 case TEXT_DIRECTION_ANY_RTL:
16851 case TEXT_DIRECTION_LTR:
16852 case TEXT_DIRECTION_RTL:
16853 case TEXT_DIRECTION_LOCALE:
16854 mPrivateFlags2 |=
16855 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16856 break;
16857 default:
16858 // Default resolved direction is "first strong" heuristic
16859 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016860 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016861 break;
16862 case TEXT_DIRECTION_FIRST_STRONG:
16863 case TEXT_DIRECTION_ANY_RTL:
16864 case TEXT_DIRECTION_LTR:
16865 case TEXT_DIRECTION_RTL:
16866 case TEXT_DIRECTION_LOCALE:
16867 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016868 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016869 break;
16870 default:
16871 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016872 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016873 }
16874 } else {
16875 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016876 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016877 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016878
16879 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016880 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016881 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016882 }
16883
16884 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016885 * Check if text direction resolution can be done.
16886 *
16887 * @return true if text direction resolution can be done otherwise return false.
16888 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016889 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016890 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016891 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016892 return (mParent != null) && (mParent instanceof View) &&
16893 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016894 default:
16895 return true;
16896 }
16897 }
16898
16899 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016900 * Reset resolved text direction. Text direction will be resolved during a call to
16901 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016902 *
16903 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016904 */
16905 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016906 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016907 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016908 // Set to default value
16909 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016910 }
16911
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016912 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016913 * @return true if text direction is inherited.
16914 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016915 * @hide
16916 */
16917 public boolean isTextDirectionInherited() {
16918 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16919 }
16920
16921 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016922 * @return true if text direction is resolved.
16923 */
16924 private boolean isTextDirectionResolved() {
16925 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16926 }
16927
16928 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016929 * Return the value specifying the text alignment or policy that was set with
16930 * {@link #setTextAlignment(int)}.
16931 *
16932 * @return the defined text alignment. It can be one of:
16933 *
16934 * {@link #TEXT_ALIGNMENT_INHERIT},
16935 * {@link #TEXT_ALIGNMENT_GRAVITY},
16936 * {@link #TEXT_ALIGNMENT_CENTER},
16937 * {@link #TEXT_ALIGNMENT_TEXT_START},
16938 * {@link #TEXT_ALIGNMENT_TEXT_END},
16939 * {@link #TEXT_ALIGNMENT_VIEW_START},
16940 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016941 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016942 * @attr ref android.R.styleable#View_textAlignment
16943 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016944 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016945 */
16946 @ViewDebug.ExportedProperty(category = "text", mapping = {
16947 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16948 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16949 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16950 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16951 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16952 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16953 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16954 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016955 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016956 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016957 }
16958
16959 /**
16960 * Set the text alignment.
16961 *
16962 * @param textAlignment The text alignment to set. Should be one of
16963 *
16964 * {@link #TEXT_ALIGNMENT_INHERIT},
16965 * {@link #TEXT_ALIGNMENT_GRAVITY},
16966 * {@link #TEXT_ALIGNMENT_CENTER},
16967 * {@link #TEXT_ALIGNMENT_TEXT_START},
16968 * {@link #TEXT_ALIGNMENT_TEXT_END},
16969 * {@link #TEXT_ALIGNMENT_VIEW_START},
16970 * {@link #TEXT_ALIGNMENT_VIEW_END}
16971 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016972 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16973 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16974 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16975 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016976 * @attr ref android.R.styleable#View_textAlignment
16977 */
16978 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016979 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016980 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016981 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016982 resetResolvedTextAlignment();
16983 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016984 mPrivateFlags2 |=
16985 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16986 // Do resolution
16987 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016988 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016989 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016990 // Refresh
16991 requestLayout();
16992 invalidate(true);
16993 }
16994 }
16995
16996 /**
16997 * Return the resolved text alignment.
16998 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016999 * @return the resolved text alignment. Returns one of:
17000 *
17001 * {@link #TEXT_ALIGNMENT_GRAVITY},
17002 * {@link #TEXT_ALIGNMENT_CENTER},
17003 * {@link #TEXT_ALIGNMENT_TEXT_START},
17004 * {@link #TEXT_ALIGNMENT_TEXT_END},
17005 * {@link #TEXT_ALIGNMENT_VIEW_START},
17006 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080017007 *
17008 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017009 */
17010 @ViewDebug.ExportedProperty(category = "text", mapping = {
17011 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
17012 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
17013 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
17014 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
17015 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
17016 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
17017 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
17018 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017019 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017020 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
17021 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017022 }
17023
17024 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017025 * Resolve the text alignment.
17026 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017027 * @return true if resolution has been done, false otherwise.
17028 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017029 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017030 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017031 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017032 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017033 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017034
17035 if (hasRtlSupport()) {
17036 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017037 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017038 switch (textAlignment) {
17039 case TEXT_ALIGNMENT_INHERIT:
17040 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017041 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017042 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070017043 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017044 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017045 return false;
17046 }
17047 View parent = (View) mParent;
17048
17049 // Parent has not yet resolved, so we still return the default
17050 if (!parent.isTextAlignmentResolved()) {
17051 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
17052 // Resolution will need to happen again later
17053 return false;
17054 }
17055
17056 final int parentResolvedTextAlignment = parent.getTextAlignment();
17057 switch (parentResolvedTextAlignment) {
17058 case TEXT_ALIGNMENT_GRAVITY:
17059 case TEXT_ALIGNMENT_TEXT_START:
17060 case TEXT_ALIGNMENT_TEXT_END:
17061 case TEXT_ALIGNMENT_CENTER:
17062 case TEXT_ALIGNMENT_VIEW_START:
17063 case TEXT_ALIGNMENT_VIEW_END:
17064 // Resolved text alignment is the same as the parent resolved
17065 // text alignment
17066 mPrivateFlags2 |=
17067 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17068 break;
17069 default:
17070 // Use default resolved text alignment
17071 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017072 }
17073 break;
17074 case TEXT_ALIGNMENT_GRAVITY:
17075 case TEXT_ALIGNMENT_TEXT_START:
17076 case TEXT_ALIGNMENT_TEXT_END:
17077 case TEXT_ALIGNMENT_CENTER:
17078 case TEXT_ALIGNMENT_VIEW_START:
17079 case TEXT_ALIGNMENT_VIEW_END:
17080 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017081 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017082 break;
17083 default:
17084 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017085 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017086 }
17087 } else {
17088 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017089 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017090 }
17091
17092 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017093 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017094 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017095 }
17096
17097 /**
17098 * Check if text alignment resolution can be done.
17099 *
17100 * @return true if text alignment resolution can be done otherwise return false.
17101 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017102 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017103 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017104 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017105 return (mParent != null) && (mParent instanceof View) &&
17106 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017107 default:
17108 return true;
17109 }
17110 }
17111
17112 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017113 * Reset resolved text alignment. Text alignment will be resolved during a call to
17114 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017115 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017116 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017117 */
17118 public void resetResolvedTextAlignment() {
17119 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017120 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017121 // Set to default
17122 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017123 }
17124
Adam Powella9108a22012-07-18 11:18:09 -070017125 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017126 * @return true if text alignment is inherited.
17127 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017128 * @hide
17129 */
17130 public boolean isTextAlignmentInherited() {
17131 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17132 }
17133
17134 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017135 * @return true if text alignment is resolved.
17136 */
17137 private boolean isTextAlignmentResolved() {
17138 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17139 }
17140
17141 /**
Adam Powella9108a22012-07-18 11:18:09 -070017142 * Generate a value suitable for use in {@link #setId(int)}.
17143 * This value will not collide with ID values generated at build time by aapt for R.id.
17144 *
17145 * @return a generated ID value
17146 */
17147 public static int generateViewId() {
17148 for (;;) {
17149 final int result = sNextGeneratedId.get();
17150 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17151 int newValue = result + 1;
17152 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17153 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17154 return result;
17155 }
17156 }
17157 }
17158
Chet Haaseb39f0512011-05-24 14:36:40 -070017159 //
17160 // Properties
17161 //
17162 /**
17163 * A Property wrapper around the <code>alpha</code> functionality handled by the
17164 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17165 */
Chet Haased47f1532011-12-16 11:18:52 -080017166 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017167 @Override
17168 public void setValue(View object, float value) {
17169 object.setAlpha(value);
17170 }
17171
17172 @Override
17173 public Float get(View object) {
17174 return object.getAlpha();
17175 }
17176 };
17177
17178 /**
17179 * A Property wrapper around the <code>translationX</code> functionality handled by the
17180 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17181 */
Chet Haased47f1532011-12-16 11:18:52 -080017182 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017183 @Override
17184 public void setValue(View object, float value) {
17185 object.setTranslationX(value);
17186 }
17187
17188 @Override
17189 public Float get(View object) {
17190 return object.getTranslationX();
17191 }
17192 };
17193
17194 /**
17195 * A Property wrapper around the <code>translationY</code> functionality handled by the
17196 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17197 */
Chet Haased47f1532011-12-16 11:18:52 -080017198 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017199 @Override
17200 public void setValue(View object, float value) {
17201 object.setTranslationY(value);
17202 }
17203
17204 @Override
17205 public Float get(View object) {
17206 return object.getTranslationY();
17207 }
17208 };
17209
17210 /**
17211 * A Property wrapper around the <code>x</code> functionality handled by the
17212 * {@link View#setX(float)} and {@link View#getX()} methods.
17213 */
Chet Haased47f1532011-12-16 11:18:52 -080017214 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017215 @Override
17216 public void setValue(View object, float value) {
17217 object.setX(value);
17218 }
17219
17220 @Override
17221 public Float get(View object) {
17222 return object.getX();
17223 }
17224 };
17225
17226 /**
17227 * A Property wrapper around the <code>y</code> functionality handled by the
17228 * {@link View#setY(float)} and {@link View#getY()} methods.
17229 */
Chet Haased47f1532011-12-16 11:18:52 -080017230 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017231 @Override
17232 public void setValue(View object, float value) {
17233 object.setY(value);
17234 }
17235
17236 @Override
17237 public Float get(View object) {
17238 return object.getY();
17239 }
17240 };
17241
17242 /**
17243 * A Property wrapper around the <code>rotation</code> functionality handled by the
17244 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17245 */
Chet Haased47f1532011-12-16 11:18:52 -080017246 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017247 @Override
17248 public void setValue(View object, float value) {
17249 object.setRotation(value);
17250 }
17251
17252 @Override
17253 public Float get(View object) {
17254 return object.getRotation();
17255 }
17256 };
17257
17258 /**
17259 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17260 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17261 */
Chet Haased47f1532011-12-16 11:18:52 -080017262 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017263 @Override
17264 public void setValue(View object, float value) {
17265 object.setRotationX(value);
17266 }
17267
17268 @Override
17269 public Float get(View object) {
17270 return object.getRotationX();
17271 }
17272 };
17273
17274 /**
17275 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17276 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17277 */
Chet Haased47f1532011-12-16 11:18:52 -080017278 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017279 @Override
17280 public void setValue(View object, float value) {
17281 object.setRotationY(value);
17282 }
17283
17284 @Override
17285 public Float get(View object) {
17286 return object.getRotationY();
17287 }
17288 };
17289
17290 /**
17291 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17292 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17293 */
Chet Haased47f1532011-12-16 11:18:52 -080017294 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017295 @Override
17296 public void setValue(View object, float value) {
17297 object.setScaleX(value);
17298 }
17299
17300 @Override
17301 public Float get(View object) {
17302 return object.getScaleX();
17303 }
17304 };
17305
17306 /**
17307 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17308 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17309 */
Chet Haased47f1532011-12-16 11:18:52 -080017310 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017311 @Override
17312 public void setValue(View object, float value) {
17313 object.setScaleY(value);
17314 }
17315
17316 @Override
17317 public Float get(View object) {
17318 return object.getScaleY();
17319 }
17320 };
17321
Jeff Brown33bbfd22011-02-24 20:55:35 -080017322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017323 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17324 * Each MeasureSpec represents a requirement for either the width or the height.
17325 * A MeasureSpec is comprised of a size and a mode. There are three possible
17326 * modes:
17327 * <dl>
17328 * <dt>UNSPECIFIED</dt>
17329 * <dd>
17330 * The parent has not imposed any constraint on the child. It can be whatever size
17331 * it wants.
17332 * </dd>
17333 *
17334 * <dt>EXACTLY</dt>
17335 * <dd>
17336 * The parent has determined an exact size for the child. The child is going to be
17337 * given those bounds regardless of how big it wants to be.
17338 * </dd>
17339 *
17340 * <dt>AT_MOST</dt>
17341 * <dd>
17342 * The child can be as large as it wants up to the specified size.
17343 * </dd>
17344 * </dl>
17345 *
17346 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17347 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17348 */
17349 public static class MeasureSpec {
17350 private static final int MODE_SHIFT = 30;
17351 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17352
17353 /**
17354 * Measure specification mode: The parent has not imposed any constraint
17355 * on the child. It can be whatever size it wants.
17356 */
17357 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17358
17359 /**
17360 * Measure specification mode: The parent has determined an exact size
17361 * for the child. The child is going to be given those bounds regardless
17362 * of how big it wants to be.
17363 */
17364 public static final int EXACTLY = 1 << MODE_SHIFT;
17365
17366 /**
17367 * Measure specification mode: The child can be as large as it wants up
17368 * to the specified size.
17369 */
17370 public static final int AT_MOST = 2 << MODE_SHIFT;
17371
17372 /**
17373 * Creates a measure specification based on the supplied size and mode.
17374 *
17375 * The mode must always be one of the following:
17376 * <ul>
17377 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17378 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17379 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17380 * </ul>
17381 *
Adam Powell2c8cc972012-12-07 18:04:51 -080017382 * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
17383 * implementation was such that the order of arguments did not matter
17384 * and overflow in either value could impact the resulting MeasureSpec.
17385 * {@link android.widget.RelativeLayout} was affected by this bug.
17386 * Apps targeting API levels greater than 17 will get the fixed, more strict
17387 * behavior.</p>
17388 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389 * @param size the size of the measure specification
17390 * @param mode the mode of the measure specification
17391 * @return the measure specification based on size and mode
17392 */
17393 public static int makeMeasureSpec(int size, int mode) {
Adam Powell7da4b732012-12-07 15:28:33 -080017394 if (sUseBrokenMakeMeasureSpec) {
17395 return size + mode;
17396 } else {
17397 return (size & ~MODE_MASK) | (mode & MODE_MASK);
17398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017399 }
17400
17401 /**
17402 * Extracts the mode from the supplied measure specification.
17403 *
17404 * @param measureSpec the measure specification to extract the mode from
17405 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17406 * {@link android.view.View.MeasureSpec#AT_MOST} or
17407 * {@link android.view.View.MeasureSpec#EXACTLY}
17408 */
17409 public static int getMode(int measureSpec) {
17410 return (measureSpec & MODE_MASK);
17411 }
17412
17413 /**
17414 * Extracts the size from the supplied measure specification.
17415 *
17416 * @param measureSpec the measure specification to extract the size from
17417 * @return the size in pixels defined in the supplied measure specification
17418 */
17419 public static int getSize(int measureSpec) {
17420 return (measureSpec & ~MODE_MASK);
17421 }
17422
Philip Milne7b757812012-09-19 18:13:44 -070017423 static int adjust(int measureSpec, int delta) {
17424 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17425 }
17426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017427 /**
17428 * Returns a String representation of the specified measure
17429 * specification.
17430 *
17431 * @param measureSpec the measure specification to convert to a String
17432 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17433 */
17434 public static String toString(int measureSpec) {
17435 int mode = getMode(measureSpec);
17436 int size = getSize(measureSpec);
17437
17438 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17439
17440 if (mode == UNSPECIFIED)
17441 sb.append("UNSPECIFIED ");
17442 else if (mode == EXACTLY)
17443 sb.append("EXACTLY ");
17444 else if (mode == AT_MOST)
17445 sb.append("AT_MOST ");
17446 else
17447 sb.append(mode).append(" ");
17448
17449 sb.append(size);
17450 return sb.toString();
17451 }
17452 }
17453
17454 class CheckForLongPress implements Runnable {
17455
17456 private int mOriginalWindowAttachCount;
17457
17458 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017459 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017460 && mOriginalWindowAttachCount == mWindowAttachCount) {
17461 if (performLongClick()) {
17462 mHasPerformedLongPress = true;
17463 }
17464 }
17465 }
17466
17467 public void rememberWindowAttachCount() {
17468 mOriginalWindowAttachCount = mWindowAttachCount;
17469 }
17470 }
Joe Malin32736f02011-01-19 16:14:20 -080017471
Adam Powelle14579b2009-12-16 18:39:52 -080017472 private final class CheckForTap implements Runnable {
17473 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017474 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017475 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017476 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017477 }
17478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017479
Adam Powella35d7682010-03-12 14:48:13 -080017480 private final class PerformClick implements Runnable {
17481 public void run() {
17482 performClick();
17483 }
17484 }
17485
Dianne Hackborn63042d62011-01-26 18:56:29 -080017486 /** @hide */
17487 public void hackTurnOffWindowResizeAnim(boolean off) {
17488 mAttachInfo.mTurnOffWindowResizeAnim = off;
17489 }
Joe Malin32736f02011-01-19 16:14:20 -080017490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017491 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017492 * This method returns a ViewPropertyAnimator object, which can be used to animate
17493 * specific properties on this View.
17494 *
17495 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17496 */
17497 public ViewPropertyAnimator animate() {
17498 if (mAnimator == null) {
17499 mAnimator = new ViewPropertyAnimator(this);
17500 }
17501 return mAnimator;
17502 }
17503
17504 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017505 * Interface definition for a callback to be invoked when a hardware key event is
17506 * dispatched to this view. The callback will be invoked before the key event is
17507 * given to the view. This is only useful for hardware keyboards; a software input
17508 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017509 */
17510 public interface OnKeyListener {
17511 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017512 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017513 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017514 * <p>Key presses in software keyboards will generally NOT trigger this method,
17515 * although some may elect to do so in some situations. Do not assume a
17516 * software input method has to be key-based; even if it is, it may use key presses
17517 * in a different way than you expect, so there is no way to reliably catch soft
17518 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017519 *
17520 * @param v The view the key has been dispatched to.
17521 * @param keyCode The code for the physical key that was pressed
17522 * @param event The KeyEvent object containing full information about
17523 * the event.
17524 * @return True if the listener has consumed the event, false otherwise.
17525 */
17526 boolean onKey(View v, int keyCode, KeyEvent event);
17527 }
17528
17529 /**
17530 * Interface definition for a callback to be invoked when a touch event is
17531 * dispatched to this view. The callback will be invoked before the touch
17532 * event is given to the view.
17533 */
17534 public interface OnTouchListener {
17535 /**
17536 * Called when a touch event is dispatched to a view. This allows listeners to
17537 * get a chance to respond before the target view.
17538 *
17539 * @param v The view the touch event has been dispatched to.
17540 * @param event The MotionEvent object containing full information about
17541 * the event.
17542 * @return True if the listener has consumed the event, false otherwise.
17543 */
17544 boolean onTouch(View v, MotionEvent event);
17545 }
17546
17547 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017548 * Interface definition for a callback to be invoked when a hover event is
17549 * dispatched to this view. The callback will be invoked before the hover
17550 * event is given to the view.
17551 */
17552 public interface OnHoverListener {
17553 /**
17554 * Called when a hover event is dispatched to a view. This allows listeners to
17555 * get a chance to respond before the target view.
17556 *
17557 * @param v The view the hover event has been dispatched to.
17558 * @param event The MotionEvent object containing full information about
17559 * the event.
17560 * @return True if the listener has consumed the event, false otherwise.
17561 */
17562 boolean onHover(View v, MotionEvent event);
17563 }
17564
17565 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017566 * Interface definition for a callback to be invoked when a generic motion event is
17567 * dispatched to this view. The callback will be invoked before the generic motion
17568 * event is given to the view.
17569 */
17570 public interface OnGenericMotionListener {
17571 /**
17572 * Called when a generic motion event is dispatched to a view. This allows listeners to
17573 * get a chance to respond before the target view.
17574 *
17575 * @param v The view the generic motion event has been dispatched to.
17576 * @param event The MotionEvent object containing full information about
17577 * the event.
17578 * @return True if the listener has consumed the event, false otherwise.
17579 */
17580 boolean onGenericMotion(View v, MotionEvent event);
17581 }
17582
17583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017584 * Interface definition for a callback to be invoked when a view has been clicked and held.
17585 */
17586 public interface OnLongClickListener {
17587 /**
17588 * Called when a view has been clicked and held.
17589 *
17590 * @param v The view that was clicked and held.
17591 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017592 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017593 */
17594 boolean onLongClick(View v);
17595 }
17596
17597 /**
Chris Tate32affef2010-10-18 15:29:21 -070017598 * Interface definition for a callback to be invoked when a drag is being dispatched
17599 * to this view. The callback will be invoked before the hosting view's own
17600 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17601 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017602 *
17603 * <div class="special reference">
17604 * <h3>Developer Guides</h3>
17605 * <p>For a guide to implementing drag and drop features, read the
17606 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17607 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017608 */
17609 public interface OnDragListener {
17610 /**
17611 * Called when a drag event is dispatched to a view. This allows listeners
17612 * to get a chance to override base View behavior.
17613 *
Joe Malin32736f02011-01-19 16:14:20 -080017614 * @param v The View that received the drag event.
17615 * @param event The {@link android.view.DragEvent} object for the drag event.
17616 * @return {@code true} if the drag event was handled successfully, or {@code false}
17617 * if the drag event was not handled. Note that {@code false} will trigger the View
17618 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017619 */
17620 boolean onDrag(View v, DragEvent event);
17621 }
17622
17623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017624 * Interface definition for a callback to be invoked when the focus state of
17625 * a view changed.
17626 */
17627 public interface OnFocusChangeListener {
17628 /**
17629 * Called when the focus state of a view has changed.
17630 *
17631 * @param v The view whose state has changed.
17632 * @param hasFocus The new focus state of v.
17633 */
17634 void onFocusChange(View v, boolean hasFocus);
17635 }
17636
17637 /**
17638 * Interface definition for a callback to be invoked when a view is clicked.
17639 */
17640 public interface OnClickListener {
17641 /**
17642 * Called when a view has been clicked.
17643 *
17644 * @param v The view that was clicked.
17645 */
17646 void onClick(View v);
17647 }
17648
17649 /**
17650 * Interface definition for a callback to be invoked when the context menu
17651 * for this view is being built.
17652 */
17653 public interface OnCreateContextMenuListener {
17654 /**
17655 * Called when the context menu for this view is being built. It is not
17656 * safe to hold onto the menu after this method returns.
17657 *
17658 * @param menu The context menu that is being built
17659 * @param v The view for which the context menu is being built
17660 * @param menuInfo Extra information about the item for which the
17661 * context menu should be shown. This information will vary
17662 * depending on the class of v.
17663 */
17664 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17665 }
17666
Joe Onorato664644d2011-01-23 17:53:23 -080017667 /**
17668 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017669 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017670 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017671 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017672 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017673 */
17674 public interface OnSystemUiVisibilityChangeListener {
17675 /**
17676 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017677 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017678 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017679 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17680 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17681 * This tells you the <strong>global</strong> state of these UI visibility
17682 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017683 */
17684 public void onSystemUiVisibilityChange(int visibility);
17685 }
17686
Adam Powell4afd62b2011-02-18 15:02:18 -080017687 /**
17688 * Interface definition for a callback to be invoked when this view is attached
17689 * or detached from its window.
17690 */
17691 public interface OnAttachStateChangeListener {
17692 /**
17693 * Called when the view is attached to a window.
17694 * @param v The view that was attached
17695 */
17696 public void onViewAttachedToWindow(View v);
17697 /**
17698 * Called when the view is detached from a window.
17699 * @param v The view that was detached
17700 */
17701 public void onViewDetachedFromWindow(View v);
17702 }
17703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017704 private final class UnsetPressedState implements Runnable {
17705 public void run() {
17706 setPressed(false);
17707 }
17708 }
17709
17710 /**
17711 * Base class for derived classes that want to save and restore their own
17712 * state in {@link android.view.View#onSaveInstanceState()}.
17713 */
17714 public static class BaseSavedState extends AbsSavedState {
17715 /**
17716 * Constructor used when reading from a parcel. Reads the state of the superclass.
17717 *
17718 * @param source
17719 */
17720 public BaseSavedState(Parcel source) {
17721 super(source);
17722 }
17723
17724 /**
17725 * Constructor called by derived classes when creating their SavedState objects
17726 *
17727 * @param superState The state of the superclass of this view
17728 */
17729 public BaseSavedState(Parcelable superState) {
17730 super(superState);
17731 }
17732
17733 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17734 new Parcelable.Creator<BaseSavedState>() {
17735 public BaseSavedState createFromParcel(Parcel in) {
17736 return new BaseSavedState(in);
17737 }
17738
17739 public BaseSavedState[] newArray(int size) {
17740 return new BaseSavedState[size];
17741 }
17742 };
17743 }
17744
17745 /**
17746 * A set of information given to a view when it is attached to its parent
17747 * window.
17748 */
17749 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017750 interface Callbacks {
17751 void playSoundEffect(int effectId);
17752 boolean performHapticFeedback(int effectId, boolean always);
17753 }
17754
17755 /**
17756 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17757 * to a Handler. This class contains the target (View) to invalidate and
17758 * the coordinates of the dirty rectangle.
17759 *
17760 * For performance purposes, this class also implements a pool of up to
17761 * POOL_LIMIT objects that get reused. This reduces memory allocations
17762 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017763 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017764 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017765 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017766
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017767 private static final SynchronizedPool<InvalidateInfo> sPool =
17768 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017769
17770 View target;
17771
17772 int left;
17773 int top;
17774 int right;
17775 int bottom;
17776
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017777 public static InvalidateInfo obtain() {
17778 InvalidateInfo instance = sPool.acquire();
17779 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070017780 }
17781
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017782 public void recycle() {
17783 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017784 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017785 }
17786 }
17787
17788 final IWindowSession mSession;
17789
17790 final IWindow mWindow;
17791
17792 final IBinder mWindowToken;
17793
Jeff Brown98365d72012-08-19 20:30:52 -070017794 final Display mDisplay;
17795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017796 final Callbacks mRootCallbacks;
17797
Romain Guy59a12ca2011-06-09 17:48:21 -070017798 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017800 /**
17801 * The top view of the hierarchy.
17802 */
17803 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017805 IBinder mPanelParentWindowToken;
17806 Surface mSurface;
17807
Romain Guyb051e892010-09-28 19:09:36 -070017808 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017809 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017810 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017811
Romain Guy7e4e5612012-03-05 14:37:29 -080017812 boolean mScreenOn;
17813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017814 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017815 * Scale factor used by the compatibility mode
17816 */
17817 float mApplicationScale;
17818
17819 /**
17820 * Indicates whether the application is in compatibility mode
17821 */
17822 boolean mScalingRequired;
17823
17824 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017825 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017826 */
17827 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017828
Dianne Hackborn63042d62011-01-26 18:56:29 -080017829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017830 * Left position of this view's window
17831 */
17832 int mWindowLeft;
17833
17834 /**
17835 * Top position of this view's window
17836 */
17837 int mWindowTop;
17838
17839 /**
Adam Powell26153a32010-11-08 15:22:27 -080017840 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017841 */
Adam Powell26153a32010-11-08 15:22:27 -080017842 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017843
17844 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017845 * For windows that are full-screen but using insets to layout inside
17846 * of the screen decorations, these are the current insets for the
17847 * content of the window.
17848 */
17849 final Rect mContentInsets = new Rect();
17850
17851 /**
17852 * For windows that are full-screen but using insets to layout inside
17853 * of the screen decorations, these are the current insets for the
17854 * actual visible parts of the window.
17855 */
17856 final Rect mVisibleInsets = new Rect();
17857
17858 /**
17859 * The internal insets given by this window. This value is
17860 * supplied by the client (through
17861 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17862 * be given to the window manager when changed to be used in laying
17863 * out windows behind it.
17864 */
17865 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17866 = new ViewTreeObserver.InternalInsetsInfo();
17867
17868 /**
17869 * All views in the window's hierarchy that serve as scroll containers,
17870 * used to determine if the window can be resized or must be panned
17871 * to adjust for a soft input area.
17872 */
17873 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17874
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017875 final KeyEvent.DispatcherState mKeyDispatchState
17876 = new KeyEvent.DispatcherState();
17877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017878 /**
17879 * Indicates whether the view's window currently has the focus.
17880 */
17881 boolean mHasWindowFocus;
17882
17883 /**
17884 * The current visibility of the window.
17885 */
17886 int mWindowVisibility;
17887
17888 /**
17889 * Indicates the time at which drawing started to occur.
17890 */
17891 long mDrawingTime;
17892
17893 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017894 * Indicates whether or not ignoring the DIRTY_MASK flags.
17895 */
17896 boolean mIgnoreDirtyState;
17897
17898 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017899 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17900 * to avoid clearing that flag prematurely.
17901 */
17902 boolean mSetIgnoreDirtyState = false;
17903
17904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017905 * Indicates whether the view's window is currently in touch mode.
17906 */
17907 boolean mInTouchMode;
17908
17909 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017910 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017911 * the next time it performs a traversal
17912 */
17913 boolean mRecomputeGlobalAttributes;
17914
17915 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017916 * Always report new attributes at next traversal.
17917 */
17918 boolean mForceReportNewAttributes;
17919
17920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017921 * Set during a traveral if any views want to keep the screen on.
17922 */
17923 boolean mKeepScreenOn;
17924
17925 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017926 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17927 */
17928 int mSystemUiVisibility;
17929
17930 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017931 * Hack to force certain system UI visibility flags to be cleared.
17932 */
17933 int mDisabledSystemUiVisibility;
17934
17935 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017936 * Last global system UI visibility reported by the window manager.
17937 */
17938 int mGlobalSystemUiVisibility;
17939
17940 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017941 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17942 * attached.
17943 */
17944 boolean mHasSystemUiListeners;
17945
17946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017947 * Set if the visibility of any views has changed.
17948 */
17949 boolean mViewVisibilityChanged;
17950
17951 /**
17952 * Set to true if a view has been scrolled.
17953 */
17954 boolean mViewScrollChanged;
17955
17956 /**
17957 * Global to the view hierarchy used as a temporary for dealing with
17958 * x/y points in the transparent region computations.
17959 */
17960 final int[] mTransparentLocation = new int[2];
17961
17962 /**
17963 * Global to the view hierarchy used as a temporary for dealing with
17964 * x/y points in the ViewGroup.invalidateChild implementation.
17965 */
17966 final int[] mInvalidateChildLocation = new int[2];
17967
Chet Haasec3aa3612010-06-17 08:50:37 -070017968
17969 /**
17970 * Global to the view hierarchy used as a temporary for dealing with
17971 * x/y location when view is transformed.
17972 */
17973 final float[] mTmpTransformLocation = new float[2];
17974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017975 /**
17976 * The view tree observer used to dispatch global events like
17977 * layout, pre-draw, touch mode change, etc.
17978 */
17979 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17980
17981 /**
17982 * A Canvas used by the view hierarchy to perform bitmap caching.
17983 */
17984 Canvas mCanvas;
17985
17986 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017987 * The view root impl.
17988 */
17989 final ViewRootImpl mViewRootImpl;
17990
17991 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017992 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017993 * handler can be used to pump events in the UI events queue.
17994 */
17995 final Handler mHandler;
17996
17997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017998 * Temporary for use in computing invalidate rectangles while
17999 * calling up the hierarchy.
18000 */
18001 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070018002
18003 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070018004 * Temporary for use in computing hit areas with transformed views
18005 */
18006 final RectF mTmpTransformRect = new RectF();
18007
18008 /**
Chet Haase599913d2012-07-23 16:22:05 -070018009 * Temporary for use in transforming invalidation rect
18010 */
18011 final Matrix mTmpMatrix = new Matrix();
18012
18013 /**
18014 * Temporary for use in transforming invalidation rect
18015 */
18016 final Transformation mTmpTransformation = new Transformation();
18017
18018 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070018019 * Temporary list for use in collecting focusable descendents of a view.
18020 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070018021 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070018022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018023 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070018024 * The id of the window for accessibility purposes.
18025 */
18026 int mAccessibilityWindowId = View.NO_ID;
18027
18028 /**
Svetoslav Ganov80943d82013-01-02 10:25:37 -080018029 * Flags related to accessibility processing.
18030 *
18031 * @see AccessibilityNodeInfo#FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
18032 * @see AccessibilityNodeInfo#FLAG_REPORT_VIEW_IDS
Svetoslav Ganov42138042012-03-20 11:51:39 -070018033 */
Svetoslav Ganov80943d82013-01-02 10:25:37 -080018034 int mAccessibilityFetchFlags;
Svetoslav Ganov42138042012-03-20 11:51:39 -070018035
18036 /**
18037 * The drawable for highlighting accessibility focus.
18038 */
18039 Drawable mAccessibilityFocusDrawable;
18040
18041 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070018042 * Show where the margins, bounds and layout bounds are for each view.
18043 */
Dianne Hackborna53de062012-05-08 18:53:51 -070018044 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070018045
18046 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070018047 * Point used to compute visible regions.
18048 */
18049 final Point mPoint = new Point();
18050
18051 /**
Chet Haasecc699b42012-12-13 09:06:55 -080018052 * Used to track which View originated a requestLayout() call, used when
18053 * requestLayout() is called during layout.
18054 */
18055 View mViewRequestingLayout;
18056
18057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018058 * Creates a new set of attachment information with the specified
18059 * events handler and thread.
18060 *
18061 * @param handler the events handler the view must use
18062 */
Jeff Brown98365d72012-08-19 20:30:52 -070018063 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080018064 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018065 mSession = session;
18066 mWindow = window;
18067 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070018068 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080018069 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018070 mHandler = handler;
18071 mRootCallbacks = effectPlayer;
18072 }
18073 }
18074
18075 /**
18076 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18077 * is supported. This avoids keeping too many unused fields in most
18078 * instances of View.</p>
18079 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018080 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018081
Mike Cleronf116bf82009-09-27 19:14:12 -070018082 /**
18083 * Scrollbars are not visible
18084 */
18085 public static final int OFF = 0;
18086
18087 /**
18088 * Scrollbars are visible
18089 */
18090 public static final int ON = 1;
18091
18092 /**
18093 * Scrollbars are fading away
18094 */
18095 public static final int FADING = 2;
18096
18097 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018099 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018100 public int scrollBarDefaultDelayBeforeFade;
18101 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018102
18103 public int scrollBarSize;
18104 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018105 public float[] interpolatorValues;
18106 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018107
18108 public final Paint paint;
18109 public final Matrix matrix;
18110 public Shader shader;
18111
Mike Cleronf116bf82009-09-27 19:14:12 -070018112 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18113
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018114 private static final float[] OPAQUE = { 255 };
18115 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018116
Mike Cleronf116bf82009-09-27 19:14:12 -070018117 /**
18118 * When fading should start. This time moves into the future every time
18119 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18120 */
18121 public long fadeStartTime;
18122
18123
18124 /**
18125 * The current state of the scrollbars: ON, OFF, or FADING
18126 */
18127 public int state = OFF;
18128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018129 private int mLastColor;
18130
Mike Cleronf116bf82009-09-27 19:14:12 -070018131 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018132 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18133 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018134 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18135 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018136
18137 paint = new Paint();
18138 matrix = new Matrix();
18139 // use use a height of 1, and then wack the matrix each time we
18140 // actually use it.
18141 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018142 paint.setShader(shader);
18143 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018144
Mike Cleronf116bf82009-09-27 19:14:12 -070018145 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018146 }
Romain Guy8506ab42009-06-11 17:35:47 -070018147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018148 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018149 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018150 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018151
Romain Guyd679b572012-08-29 21:49:00 -070018152 if (color != 0) {
18153 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18154 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18155 paint.setShader(shader);
18156 // Restore the default transfer mode (src_over)
18157 paint.setXfermode(null);
18158 } else {
18159 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18160 paint.setShader(shader);
18161 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018163 }
18164 }
Joe Malin32736f02011-01-19 16:14:20 -080018165
Mike Cleronf116bf82009-09-27 19:14:12 -070018166 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018167 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018168 if (now >= fadeStartTime) {
18169
18170 // the animation fades the scrollbars out by changing
18171 // the opacity (alpha) from fully opaque to fully
18172 // transparent
18173 int nextFrame = (int) now;
18174 int framesCount = 0;
18175
18176 Interpolator interpolator = scrollBarInterpolator;
18177
18178 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018179 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018180
18181 // End transparent
18182 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018183 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018184
18185 state = FADING;
18186
18187 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018188 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018189 }
18190 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018191 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018192
Svetoslav Ganova0156172011-06-26 17:55:44 -070018193 /**
18194 * Resuable callback for sending
18195 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18196 */
18197 private class SendViewScrolledAccessibilityEvent implements Runnable {
18198 public volatile boolean mIsPending;
18199
18200 public void run() {
18201 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18202 mIsPending = false;
18203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018204 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018205
18206 /**
18207 * <p>
18208 * This class represents a delegate that can be registered in a {@link View}
18209 * to enhance accessibility support via composition rather via inheritance.
18210 * It is specifically targeted to widget developers that extend basic View
18211 * classes i.e. classes in package android.view, that would like their
18212 * applications to be backwards compatible.
18213 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018214 * <div class="special reference">
18215 * <h3>Developer Guides</h3>
18216 * <p>For more information about making applications accessible, read the
18217 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18218 * developer guide.</p>
18219 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018220 * <p>
18221 * A scenario in which a developer would like to use an accessibility delegate
18222 * is overriding a method introduced in a later API version then the minimal API
18223 * version supported by the application. For example, the method
18224 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18225 * in API version 4 when the accessibility APIs were first introduced. If a
18226 * developer would like his application to run on API version 4 devices (assuming
18227 * all other APIs used by the application are version 4 or lower) and take advantage
18228 * of this method, instead of overriding the method which would break the application's
18229 * backwards compatibility, he can override the corresponding method in this
18230 * delegate and register the delegate in the target View if the API version of
18231 * the system is high enough i.e. the API version is same or higher to the API
18232 * version that introduced
18233 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18234 * </p>
18235 * <p>
18236 * Here is an example implementation:
18237 * </p>
18238 * <code><pre><p>
18239 * if (Build.VERSION.SDK_INT >= 14) {
18240 * // If the API version is equal of higher than the version in
18241 * // which onInitializeAccessibilityNodeInfo was introduced we
18242 * // register a delegate with a customized implementation.
18243 * View view = findViewById(R.id.view_id);
18244 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18245 * public void onInitializeAccessibilityNodeInfo(View host,
18246 * AccessibilityNodeInfo info) {
18247 * // Let the default implementation populate the info.
18248 * super.onInitializeAccessibilityNodeInfo(host, info);
18249 * // Set some other information.
18250 * info.setEnabled(host.isEnabled());
18251 * }
18252 * });
18253 * }
18254 * </code></pre></p>
18255 * <p>
18256 * This delegate contains methods that correspond to the accessibility methods
18257 * in View. If a delegate has been specified the implementation in View hands
18258 * off handling to the corresponding method in this delegate. The default
18259 * implementation the delegate methods behaves exactly as the corresponding
18260 * method in View for the case of no accessibility delegate been set. Hence,
18261 * to customize the behavior of a View method, clients can override only the
18262 * corresponding delegate method without altering the behavior of the rest
18263 * accessibility related methods of the host view.
18264 * </p>
18265 */
18266 public static class AccessibilityDelegate {
18267
18268 /**
18269 * Sends an accessibility event of the given type. If accessibility is not
18270 * enabled this method has no effect.
18271 * <p>
18272 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18273 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18274 * been set.
18275 * </p>
18276 *
18277 * @param host The View hosting the delegate.
18278 * @param eventType The type of the event to send.
18279 *
18280 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18281 */
18282 public void sendAccessibilityEvent(View host, int eventType) {
18283 host.sendAccessibilityEventInternal(eventType);
18284 }
18285
18286 /**
alanv8eeefef2012-05-07 16:57:53 -070018287 * Performs the specified accessibility action on the view. For
18288 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18289 * <p>
18290 * The default implementation behaves as
18291 * {@link View#performAccessibilityAction(int, Bundle)
18292 * View#performAccessibilityAction(int, Bundle)} for the case of
18293 * no accessibility delegate been set.
18294 * </p>
18295 *
18296 * @param action The action to perform.
18297 * @return Whether the action was performed.
18298 *
18299 * @see View#performAccessibilityAction(int, Bundle)
18300 * View#performAccessibilityAction(int, Bundle)
18301 */
18302 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18303 return host.performAccessibilityActionInternal(action, args);
18304 }
18305
18306 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018307 * Sends an accessibility event. This method behaves exactly as
18308 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18309 * empty {@link AccessibilityEvent} and does not perform a check whether
18310 * accessibility is enabled.
18311 * <p>
18312 * The default implementation behaves as
18313 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18314 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18315 * the case of no accessibility delegate been set.
18316 * </p>
18317 *
18318 * @param host The View hosting the delegate.
18319 * @param event The event to send.
18320 *
18321 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18322 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18323 */
18324 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18325 host.sendAccessibilityEventUncheckedInternal(event);
18326 }
18327
18328 /**
18329 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18330 * to its children for adding their text content to the event.
18331 * <p>
18332 * The default implementation behaves as
18333 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18334 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18335 * the case of no accessibility delegate been set.
18336 * </p>
18337 *
18338 * @param host The View hosting the delegate.
18339 * @param event The event.
18340 * @return True if the event population was completed.
18341 *
18342 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18343 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18344 */
18345 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18346 return host.dispatchPopulateAccessibilityEventInternal(event);
18347 }
18348
18349 /**
18350 * Gives a chance to the host View to populate the accessibility event with its
18351 * text content.
18352 * <p>
18353 * The default implementation behaves as
18354 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18355 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18356 * the case of no accessibility delegate been set.
18357 * </p>
18358 *
18359 * @param host The View hosting the delegate.
18360 * @param event The accessibility event which to populate.
18361 *
18362 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18363 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18364 */
18365 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18366 host.onPopulateAccessibilityEventInternal(event);
18367 }
18368
18369 /**
18370 * Initializes an {@link AccessibilityEvent} with information about the
18371 * the host View which is the event source.
18372 * <p>
18373 * The default implementation behaves as
18374 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18375 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18376 * the case of no accessibility delegate been set.
18377 * </p>
18378 *
18379 * @param host The View hosting the delegate.
18380 * @param event The event to initialize.
18381 *
18382 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18383 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18384 */
18385 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18386 host.onInitializeAccessibilityEventInternal(event);
18387 }
18388
18389 /**
18390 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18391 * <p>
18392 * The default implementation behaves as
18393 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18394 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18395 * the case of no accessibility delegate been set.
18396 * </p>
18397 *
18398 * @param host The View hosting the delegate.
18399 * @param info The instance to initialize.
18400 *
18401 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18402 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18403 */
18404 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18405 host.onInitializeAccessibilityNodeInfoInternal(info);
18406 }
18407
18408 /**
18409 * Called when a child of the host View has requested sending an
18410 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18411 * to augment the event.
18412 * <p>
18413 * The default implementation behaves as
18414 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18415 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18416 * the case of no accessibility delegate been set.
18417 * </p>
18418 *
18419 * @param host The View hosting the delegate.
18420 * @param child The child which requests sending the event.
18421 * @param event The event to be sent.
18422 * @return True if the event should be sent
18423 *
18424 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18425 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18426 */
18427 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18428 AccessibilityEvent event) {
18429 return host.onRequestSendAccessibilityEventInternal(child, event);
18430 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018431
18432 /**
18433 * Gets the provider for managing a virtual view hierarchy rooted at this View
18434 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18435 * that explore the window content.
18436 * <p>
18437 * The default implementation behaves as
18438 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18439 * the case of no accessibility delegate been set.
18440 * </p>
18441 *
18442 * @return The provider.
18443 *
18444 * @see AccessibilityNodeProvider
18445 */
18446 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18447 return null;
18448 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018449 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018450
18451 private class MatchIdPredicate implements Predicate<View> {
18452 public int mId;
18453
18454 @Override
18455 public boolean apply(View view) {
18456 return (view.mID == mId);
18457 }
18458 }
18459
18460 private class MatchLabelForPredicate implements Predicate<View> {
18461 private int mLabeledId;
18462
18463 @Override
18464 public boolean apply(View view) {
18465 return (view.mLabelForId == mLabeledId);
18466 }
18467 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018468
18469 /**
18470 * Dump all private flags in readable format, useful for documentation and
18471 * sanity checking.
18472 */
18473 private static void dumpFlags() {
18474 final HashMap<String, String> found = Maps.newHashMap();
18475 try {
18476 for (Field field : View.class.getDeclaredFields()) {
18477 final int modifiers = field.getModifiers();
18478 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18479 if (field.getType().equals(int.class)) {
18480 final int value = field.getInt(null);
18481 dumpFlag(found, field.getName(), value);
18482 } else if (field.getType().equals(int[].class)) {
18483 final int[] values = (int[]) field.get(null);
18484 for (int i = 0; i < values.length; i++) {
18485 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18486 }
18487 }
18488 }
18489 }
18490 } catch (IllegalAccessException e) {
18491 throw new RuntimeException(e);
18492 }
18493
18494 final ArrayList<String> keys = Lists.newArrayList();
18495 keys.addAll(found.keySet());
18496 Collections.sort(keys);
18497 for (String key : keys) {
18498 Log.d(VIEW_LOG_TAG, found.get(key));
18499 }
18500 }
18501
18502 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18503 // Sort flags by prefix, then by bits, always keeping unique keys
18504 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18505 final int prefix = name.indexOf('_');
18506 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18507 final String output = bits + " " + name;
18508 found.put(key, output);
18509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018510}