blob: b6b4c96ccf9592b306f02dd0c727415a5c7ccd32 [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
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
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;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * 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 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <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 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * 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 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * 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 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700669 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
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)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002089 * Shift for the bits in {@link #mPrivateFlags2} related to the
2090 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002091 */
2092 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2093
2094 /**
2095 * Automatically determine whether a view is important for accessibility.
2096 */
2097 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2098
2099 /**
2100 * The view is important for accessibility.
2101 */
2102 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2103
2104 /**
2105 * The view is not important for accessibility.
2106 */
2107 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2108
2109 /**
2110 * The default whether the view is important for accessiblity.
2111 */
2112 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2113
2114 /**
2115 * Mask for obtainig the bits which specify how to determine
2116 * whether a view is important for accessibility.
2117 */
2118 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2119 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2120 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2121
2122 /**
2123 * Flag indicating whether a view has accessibility focus.
2124 */
2125 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2126
2127 /**
2128 * Flag indicating whether a view state for accessibility has changed.
2129 */
2130 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002131
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002132 /**
2133 * Flag indicating that view has an animation set on it. This is used to track whether an
2134 * animation is cleared between successive frames, in order to tell the associated
2135 * DisplayList to clear its animation matrix.
2136 */
2137 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x10000000;
2138
Chet Haase1a3ab172012-05-11 08:41:20 -07002139 /**
2140 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2141 * is used to check whether later changes to the view's transform should invalidate the
2142 * view to force the quickReject test to run again.
2143 */
2144 static final int VIEW_QUICK_REJECTED = 0x20000000;
2145
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002146 // Accessiblity constants for mPrivateFlags2
2147
2148 /**
2149 * Shift for the bits in {@link #mPrivateFlags2} related to the
2150 * "accessibilityFocusable" attribute.
2151 */
2152 static final int ACCESSIBILITY_FOCUSABLE_SHIFT = 30;
2153
2154 /**
2155 * The system determines whether the view can take accessibility focus - default (recommended).
2156 * <p>
2157 * Such a view is consideted by the focus search if it is:
2158 * <ul>
2159 * <li>
2160 * Important for accessibility and actionable (clickable, long clickable, focusable)
2161 * </li>
2162 * <li>
2163 * Important for accessibility, not actionable (clickable, long clickable, focusable),
2164 * and does not have an actionable predecessor.
2165 * </li>
2166 * </ul>
2167 * An accessibility srvice can request putting accessibility focus on such a view.
2168 * </p>
2169 *
2170 * @hide
2171 */
2172 public static final int ACCESSIBILITY_FOCUSABLE_AUTO = 0x00000000;
2173
2174 /**
2175 * The view can take accessibility focus.
2176 * <p>
2177 * A view that can take accessibility focus is always considered during focus
2178 * search and an accessibility service can request putting accessibility focus
2179 * on it.
2180 * </p>
2181 *
2182 * @hide
2183 */
2184 public static final int ACCESSIBILITY_FOCUSABLE_YES = 0x00000001;
2185
2186 /**
2187 * The view can not take accessibility focus.
2188 * <p>
2189 * A view that can not take accessibility focus is never considered during focus
2190 * search and an accessibility service can not request putting accessibility focus
2191 * on it.
2192 * </p>
2193 *
2194 * @hide
2195 */
2196 public static final int ACCESSIBILITY_FOCUSABLE_NO = 0x00000002;
2197
2198 /**
2199 * The default whether the view is accessiblity focusable.
2200 */
2201 static final int ACCESSIBILITY_FOCUSABLE_DEFAULT = ACCESSIBILITY_FOCUSABLE_AUTO;
2202
2203 /**
2204 * Mask for obtainig the bits which specifies how to determine
2205 * whether a view is accessibility focusable.
2206 */
2207 static final int ACCESSIBILITY_FOCUSABLE_MASK = (ACCESSIBILITY_FOCUSABLE_AUTO
2208 | ACCESSIBILITY_FOCUSABLE_YES | ACCESSIBILITY_FOCUSABLE_NO)
2209 << ACCESSIBILITY_FOCUSABLE_SHIFT;
2210
2211
Christopher Tate3d4bf172011-03-28 16:16:46 -07002212 /* End of masks for mPrivateFlags2 */
2213
2214 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2215
Chet Haasedaf98e92011-01-10 14:10:36 -08002216 /**
Adam Powell637d3372010-08-25 14:37:03 -07002217 * Always allow a user to over-scroll this view, provided it is a
2218 * view that can scroll.
2219 *
2220 * @see #getOverScrollMode()
2221 * @see #setOverScrollMode(int)
2222 */
2223 public static final int OVER_SCROLL_ALWAYS = 0;
2224
2225 /**
2226 * Allow a user to over-scroll this view only if the content is large
2227 * enough to meaningfully scroll, provided it is a view that can scroll.
2228 *
2229 * @see #getOverScrollMode()
2230 * @see #setOverScrollMode(int)
2231 */
2232 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2233
2234 /**
2235 * Never allow a user to over-scroll this view.
2236 *
2237 * @see #getOverScrollMode()
2238 * @see #setOverScrollMode(int)
2239 */
2240 public static final int OVER_SCROLL_NEVER = 2;
2241
2242 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002243 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2244 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002245 *
Joe Malin32736f02011-01-19 16:14:20 -08002246 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002247 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002248 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002249
2250 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002251 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2252 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002253 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002254 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002255 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002256 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002257 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002258 *
Joe Malin32736f02011-01-19 16:14:20 -08002259 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002260 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002261 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2262
2263 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002264 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2265 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002266 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002267 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002268 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2269 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2270 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002271 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002272 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2273 * window flags) for displaying content using every last pixel on the display.
2274 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002275 * <p>There is a limitation: because navigation controls are so important, the least user
2276 * interaction will cause them to reappear immediately. When this happens, both
2277 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2278 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002279 *
2280 * @see #setSystemUiVisibility(int)
2281 */
2282 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2283
2284 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002285 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2286 * into the normal fullscreen mode so that its content can take over the screen
2287 * while still allowing the user to interact with the application.
2288 *
2289 * <p>This has the same visual effect as
2290 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2291 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2292 * meaning that non-critical screen decorations (such as the status bar) will be
2293 * hidden while the user is in the View's window, focusing the experience on
2294 * that content. Unlike the window flag, if you are using ActionBar in
2295 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2296 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2297 * hide the action bar.
2298 *
2299 * <p>This approach to going fullscreen is best used over the window flag when
2300 * it is a transient state -- that is, the application does this at certain
2301 * points in its user interaction where it wants to allow the user to focus
2302 * on content, but not as a continuous state. For situations where the application
2303 * would like to simply stay full screen the entire time (such as a game that
2304 * wants to take over the screen), the
2305 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2306 * is usually a better approach. The state set here will be removed by the system
2307 * in various situations (such as the user moving to another application) like
2308 * the other system UI states.
2309 *
2310 * <p>When using this flag, the application should provide some easy facility
2311 * for the user to go out of it. A common example would be in an e-book
2312 * reader, where tapping on the screen brings back whatever screen and UI
2313 * decorations that had been hidden while the user was immersed in reading
2314 * the book.
2315 *
2316 * @see #setSystemUiVisibility(int)
2317 */
2318 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2319
2320 /**
2321 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2322 * flags, we would like a stable view of the content insets given to
2323 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2324 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002325 * as a continuous state. In the stock Android UI this is the space for
2326 * the system bar, nav bar, and status bar, but not more transient elements
2327 * such as an input method.
2328 *
2329 * The stable layout your UI sees is based on the system UI modes you can
2330 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2331 * then you will get a stable layout for changes of the
2332 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2333 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2334 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2335 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2336 * with a stable layout. (Note that you should avoid using
2337 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2338 *
2339 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2340 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2341 * then a hidden status bar will be considered a "stable" state for purposes
2342 * here. This allows your UI to continually hide the status bar, while still
2343 * using the system UI flags to hide the action bar while still retaining
2344 * a stable layout. Note that changing the window fullscreen flag will never
2345 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002346 *
2347 * <p>If you are using ActionBar in
2348 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2349 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2350 * insets it adds to those given to the application.
2351 */
2352 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2353
2354 /**
2355 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2356 * to be layed out as if it has requested
2357 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2358 * allows it to avoid artifacts when switching in and out of that mode, at
2359 * the expense that some of its user interface may be covered by screen
2360 * decorations when they are shown. You can perform layout of your inner
2361 * UI elements to account for the navagation system UI through the
2362 * {@link #fitSystemWindows(Rect)} method.
2363 */
2364 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2365
2366 /**
2367 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2368 * to be layed out as if it has requested
2369 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2370 * allows it to avoid artifacts when switching in and out of that mode, at
2371 * the expense that some of its user interface may be covered by screen
2372 * decorations when they are shown. You can perform layout of your inner
2373 * UI elements to account for non-fullscreen system UI through the
2374 * {@link #fitSystemWindows(Rect)} method.
2375 */
2376 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2377
2378 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002379 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2380 */
2381 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2382
2383 /**
2384 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2385 */
2386 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002387
2388 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002389 * @hide
2390 *
2391 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2392 * out of the public fields to keep the undefined bits out of the developer's way.
2393 *
2394 * Flag to make the status bar not expandable. Unless you also
2395 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2396 */
2397 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2398
2399 /**
2400 * @hide
2401 *
2402 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2403 * out of the public fields to keep the undefined bits out of the developer's way.
2404 *
2405 * Flag to hide notification icons and scrolling ticker text.
2406 */
2407 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2408
2409 /**
2410 * @hide
2411 *
2412 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2413 * out of the public fields to keep the undefined bits out of the developer's way.
2414 *
2415 * Flag to disable incoming notification alerts. This will not block
2416 * icons, but it will block sound, vibrating and other visual or aural notifications.
2417 */
2418 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2419
2420 /**
2421 * @hide
2422 *
2423 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2424 * out of the public fields to keep the undefined bits out of the developer's way.
2425 *
2426 * Flag to hide only the scrolling ticker. Note that
2427 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2428 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2429 */
2430 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2431
2432 /**
2433 * @hide
2434 *
2435 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2436 * out of the public fields to keep the undefined bits out of the developer's way.
2437 *
2438 * Flag to hide the center system info area.
2439 */
2440 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2441
2442 /**
2443 * @hide
2444 *
2445 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2446 * out of the public fields to keep the undefined bits out of the developer's way.
2447 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002448 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002449 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2450 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002451 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002452
2453 /**
2454 * @hide
2455 *
2456 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2457 * out of the public fields to keep the undefined bits out of the developer's way.
2458 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002459 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002460 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2461 */
2462 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2463
2464 /**
2465 * @hide
2466 *
2467 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2468 * out of the public fields to keep the undefined bits out of the developer's way.
2469 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002470 * Flag to hide only the clock. You might use this if your activity has
2471 * its own clock making the status bar's clock redundant.
2472 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002473 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2474
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002475 /**
2476 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002477 *
2478 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2479 * out of the public fields to keep the undefined bits out of the developer's way.
2480 *
2481 * Flag to hide only the recent apps button. Don't use this
2482 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2483 */
2484 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2485
2486 /**
2487 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002488 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002489 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002490
2491 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002492 * These are the system UI flags that can be cleared by events outside
2493 * of an application. Currently this is just the ability to tap on the
2494 * screen while hiding the navigation bar to have it return.
2495 * @hide
2496 */
2497 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002498 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2499 | SYSTEM_UI_FLAG_FULLSCREEN;
2500
2501 /**
2502 * Flags that can impact the layout in relation to system UI.
2503 */
2504 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2505 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2506 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002507
2508 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002509 * Find views that render the specified text.
2510 *
2511 * @see #findViewsWithText(ArrayList, CharSequence, int)
2512 */
2513 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2514
2515 /**
2516 * Find find views that contain the specified content description.
2517 *
2518 * @see #findViewsWithText(ArrayList, CharSequence, int)
2519 */
2520 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2521
2522 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002523 * Find views that contain {@link AccessibilityNodeProvider}. Such
2524 * a View is a root of virtual view hierarchy and may contain the searched
2525 * text. If this flag is set Views with providers are automatically
2526 * added and it is a responsibility of the client to call the APIs of
2527 * the provider to determine whether the virtual tree rooted at this View
2528 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2529 * represeting the virtual views with this text.
2530 *
2531 * @see #findViewsWithText(ArrayList, CharSequence, int)
2532 *
2533 * @hide
2534 */
2535 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2536
2537 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002538 * Indicates that the screen has changed state and is now off.
2539 *
2540 * @see #onScreenStateChanged(int)
2541 */
2542 public static final int SCREEN_STATE_OFF = 0x0;
2543
2544 /**
2545 * Indicates that the screen has changed state and is now on.
2546 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002547 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002548 */
2549 public static final int SCREEN_STATE_ON = 0x1;
2550
2551 /**
Adam Powell637d3372010-08-25 14:37:03 -07002552 * Controls the over-scroll mode for this view.
2553 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2554 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2555 * and {@link #OVER_SCROLL_NEVER}.
2556 */
2557 private int mOverScrollMode;
2558
2559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 * The parent this view is attached to.
2561 * {@hide}
2562 *
2563 * @see #getParent()
2564 */
2565 protected ViewParent mParent;
2566
2567 /**
2568 * {@hide}
2569 */
2570 AttachInfo mAttachInfo;
2571
2572 /**
2573 * {@hide}
2574 */
Romain Guy809a7f62009-05-14 15:44:42 -07002575 @ViewDebug.ExportedProperty(flagMapping = {
2576 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2577 name = "FORCE_LAYOUT"),
2578 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2579 name = "LAYOUT_REQUIRED"),
2580 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002581 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002582 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2583 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2584 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2585 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2586 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002588 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589
2590 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002591 * This view's request for the visibility of the status bar.
2592 * @hide
2593 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002594 @ViewDebug.ExportedProperty(flagMapping = {
2595 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2596 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2597 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2598 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2599 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2600 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2601 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2602 equals = SYSTEM_UI_FLAG_VISIBLE,
2603 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2604 })
Joe Onorato664644d2011-01-23 17:53:23 -08002605 int mSystemUiVisibility;
2606
2607 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002608 * Reference count for transient state.
2609 * @see #setHasTransientState(boolean)
2610 */
2611 int mTransientStateCount = 0;
2612
2613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 * Count of how many windows this view has been attached to.
2615 */
2616 int mWindowAttachCount;
2617
2618 /**
2619 * The layout parameters associated with this view and used by the parent
2620 * {@link android.view.ViewGroup} to determine how this view should be
2621 * laid out.
2622 * {@hide}
2623 */
2624 protected ViewGroup.LayoutParams mLayoutParams;
2625
2626 /**
2627 * The view flags hold various views states.
2628 * {@hide}
2629 */
2630 @ViewDebug.ExportedProperty
2631 int mViewFlags;
2632
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002633 static class TransformationInfo {
2634 /**
2635 * The transform matrix for the View. This transform is calculated internally
2636 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2637 * is used by default. Do *not* use this variable directly; instead call
2638 * getMatrix(), which will automatically recalculate the matrix if necessary
2639 * to get the correct matrix based on the latest rotation and scale properties.
2640 */
2641 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002642
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002643 /**
2644 * The transform matrix for the View. This transform is calculated internally
2645 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2646 * is used by default. Do *not* use this variable directly; instead call
2647 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2648 * to get the correct matrix based on the latest rotation and scale properties.
2649 */
2650 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002651
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002652 /**
2653 * An internal 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.
2656 */
2657 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002658
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002659 /**
2660 * An internal variable that tracks whether we need to recalculate the
2661 * transform matrix, based on whether the rotation or scaleX/Y properties
2662 * have changed since the matrix was last calculated.
2663 */
2664 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002665
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002666 /**
2667 * A variable that tracks whether we need to recalculate the
2668 * transform matrix, based on whether the rotation or scaleX/Y properties
2669 * have changed since the matrix was last calculated. This variable
2670 * is only valid after a call to updateMatrix() or to a function that
2671 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2672 */
2673 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002674
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002675 /**
2676 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2677 */
2678 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002679
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002680 /**
2681 * This matrix is used when computing the matrix for 3D rotations.
2682 */
2683 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002684
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002685 /**
2686 * These prev values are used to recalculate a centered pivot point when necessary. The
2687 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2688 * set), so thes values are only used then as well.
2689 */
2690 private int mPrevWidth = -1;
2691 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002692
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002693 /**
2694 * The degrees rotation around the vertical axis through the pivot point.
2695 */
2696 @ViewDebug.ExportedProperty
2697 float mRotationY = 0f;
2698
2699 /**
2700 * The degrees rotation around the horizontal axis through the pivot point.
2701 */
2702 @ViewDebug.ExportedProperty
2703 float mRotationX = 0f;
2704
2705 /**
2706 * The degrees rotation around the pivot point.
2707 */
2708 @ViewDebug.ExportedProperty
2709 float mRotation = 0f;
2710
2711 /**
2712 * The amount of translation of the object away from its left property (post-layout).
2713 */
2714 @ViewDebug.ExportedProperty
2715 float mTranslationX = 0f;
2716
2717 /**
2718 * The amount of translation of the object away from its top property (post-layout).
2719 */
2720 @ViewDebug.ExportedProperty
2721 float mTranslationY = 0f;
2722
2723 /**
2724 * The amount of scale in the x direction around the pivot point. A
2725 * value of 1 means no scaling is applied.
2726 */
2727 @ViewDebug.ExportedProperty
2728 float mScaleX = 1f;
2729
2730 /**
2731 * The amount of scale in the y direction around the pivot point. A
2732 * value of 1 means no scaling is applied.
2733 */
2734 @ViewDebug.ExportedProperty
2735 float mScaleY = 1f;
2736
2737 /**
Chet Haasea33de552012-02-03 16:28:24 -08002738 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002739 */
2740 @ViewDebug.ExportedProperty
2741 float mPivotX = 0f;
2742
2743 /**
Chet Haasea33de552012-02-03 16:28:24 -08002744 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002745 */
2746 @ViewDebug.ExportedProperty
2747 float mPivotY = 0f;
2748
2749 /**
2750 * The opacity of the View. This is a value from 0 to 1, where 0 means
2751 * completely transparent and 1 means completely opaque.
2752 */
2753 @ViewDebug.ExportedProperty
2754 float mAlpha = 1f;
2755 }
2756
2757 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002758
Joe Malin32736f02011-01-19 16:14:20 -08002759 private boolean mLastIsOpaque;
2760
Chet Haasefd2b0022010-08-06 13:08:56 -07002761 /**
2762 * Convenience value to check for float values that are close enough to zero to be considered
2763 * zero.
2764 */
Romain Guy2542d192010-08-18 11:47:12 -07002765 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002766
2767 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 * The distance in pixels from the left edge of this view's parent
2769 * to the left 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 mLeft;
2774 /**
2775 * The distance in pixels from the left edge of this view's parent
2776 * to the right 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 mRight;
2781 /**
2782 * The distance in pixels from the top edge of this view's parent
2783 * to the top edge of this view.
2784 * {@hide}
2785 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002786 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 protected int mTop;
2788 /**
2789 * The distance in pixels from the top edge of this view's parent
2790 * to the bottom edge of this view.
2791 * {@hide}
2792 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002793 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 protected int mBottom;
2795
2796 /**
2797 * The offset, in pixels, by which the content of this view is scrolled
2798 * horizontally.
2799 * {@hide}
2800 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002801 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 protected int mScrollX;
2803 /**
2804 * The offset, in pixels, by which the content of this view is scrolled
2805 * vertically.
2806 * {@hide}
2807 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002808 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 protected int mScrollY;
2810
2811 /**
2812 * The left padding in pixels, that is the distance in pixels between the
2813 * left edge of this view and the left 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 mPaddingLeft;
2818 /**
2819 * The right padding in pixels, that is the distance in pixels between the
2820 * right edge of this view and the right 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 mPaddingRight;
2825 /**
2826 * The top padding in pixels, that is the distance in pixels between the
2827 * top edge of this view and the top edge of its content.
2828 * {@hide}
2829 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002830 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 protected int mPaddingTop;
2832 /**
2833 * The bottom padding in pixels, that is the distance in pixels between the
2834 * bottom edge of this view and the bottom edge of its content.
2835 * {@hide}
2836 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002837 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 protected int mPaddingBottom;
2839
2840 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002841 * The layout insets in pixels, that is the distance in pixels between the
2842 * visible edges of this view its bounds.
2843 */
2844 private Insets mLayoutInsets;
2845
2846 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002847 * Briefly describes the view and is primarily used for accessibility support.
2848 */
2849 private CharSequence mContentDescription;
2850
2851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002853 *
2854 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002856 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002857 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858
2859 /**
2860 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002861 *
2862 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002864 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002865 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002867 /**
Adam Powell20232d02010-12-08 21:08:53 -08002868 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002869 *
2870 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002871 */
2872 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002873 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002874
2875 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002876 * Cache if the user padding is relative.
2877 *
2878 */
2879 @ViewDebug.ExportedProperty(category = "padding")
2880 boolean mUserPaddingRelative;
2881
2882 /**
2883 * Cache the paddingStart set by the user to append to the scrollbar's size.
2884 *
2885 */
2886 @ViewDebug.ExportedProperty(category = "padding")
2887 int mUserPaddingStart;
2888
2889 /**
2890 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2891 *
2892 */
2893 @ViewDebug.ExportedProperty(category = "padding")
2894 int mUserPaddingEnd;
2895
2896 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002897 * @hide
2898 */
2899 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2900 /**
2901 * @hide
2902 */
2903 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904
Philip Milne6c8ea062012-04-03 17:38:43 -07002905 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906
2907 private int mBackgroundResource;
2908 private boolean mBackgroundSizeChanged;
2909
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002910 static class ListenerInfo {
2911 /**
2912 * Listener used to dispatch focus change events.
2913 * This field should be made private, so it is hidden from the SDK.
2914 * {@hide}
2915 */
2916 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002918 /**
2919 * Listeners for layout change events.
2920 */
2921 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002922
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002923 /**
2924 * Listeners for attach events.
2925 */
2926 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002927
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002928 /**
2929 * Listener used to dispatch click events.
2930 * This field should be made private, so it is hidden from the SDK.
2931 * {@hide}
2932 */
2933 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002935 /**
2936 * Listener used to dispatch long click events.
2937 * This field should be made private, so it is hidden from the SDK.
2938 * {@hide}
2939 */
2940 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002942 /**
2943 * Listener used to build the context menu.
2944 * This field should be made private, so it is hidden from the SDK.
2945 * {@hide}
2946 */
2947 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002949 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002951 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002953 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002954
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002955 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002956
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002957 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002958
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002959 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2960 }
2961
2962 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 /**
2965 * The application environment this view lives in.
2966 * This field should be made private, so it is hidden from the SDK.
2967 * {@hide}
2968 */
2969 protected Context mContext;
2970
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002971 private final Resources mResources;
2972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 private ScrollabilityCache mScrollCache;
2974
2975 private int[] mDrawableState = null;
2976
Romain Guy0211a0a2011-02-14 16:34:59 -08002977 /**
2978 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002979 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002980 * @hide
2981 */
2982 public boolean mCachingFailed;
2983
Romain Guy02890fd2010-08-06 17:58:44 -07002984 private Bitmap mDrawingCache;
2985 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002986 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002987 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988
2989 /**
2990 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2991 * the user may specify which view to go to next.
2992 */
2993 private int mNextFocusLeftId = View.NO_ID;
2994
2995 /**
2996 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2997 * the user may specify which view to go to next.
2998 */
2999 private int mNextFocusRightId = View.NO_ID;
3000
3001 /**
3002 * When this view has focus and the next focus is {@link #FOCUS_UP},
3003 * the user may specify which view to go to next.
3004 */
3005 private int mNextFocusUpId = View.NO_ID;
3006
3007 /**
3008 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3009 * the user may specify which view to go to next.
3010 */
3011 private int mNextFocusDownId = View.NO_ID;
3012
Jeff Brown4e6319b2010-12-13 10:36:51 -08003013 /**
3014 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3015 * the user may specify which view to go to next.
3016 */
3017 int mNextFocusForwardId = View.NO_ID;
3018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003020 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003021 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003022 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 private UnsetPressedState mUnsetPressedState;
3025
3026 /**
3027 * Whether the long press's action has been invoked. The tap's action is invoked on the
3028 * up event while a long press is invoked as soon as the long press duration is reached, so
3029 * a long press could be performed before the tap is checked, in which case the tap's action
3030 * should not be invoked.
3031 */
3032 private boolean mHasPerformedLongPress;
3033
3034 /**
3035 * The minimum height of the view. We'll try our best to have the height
3036 * of this view to at least this amount.
3037 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003038 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 private int mMinHeight;
3040
3041 /**
3042 * The minimum width of the view. We'll try our best to have the width
3043 * of this view to at least this amount.
3044 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003045 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 private int mMinWidth;
3047
3048 /**
3049 * The delegate to handle touch events that are physically in this view
3050 * but should be handled by another view.
3051 */
3052 private TouchDelegate mTouchDelegate = null;
3053
3054 /**
3055 * Solid color to use as a background when creating the drawing cache. Enables
3056 * the cache to use 16 bit bitmaps instead of 32 bit.
3057 */
3058 private int mDrawingCacheBackgroundColor = 0;
3059
3060 /**
3061 * Special tree observer used when mAttachInfo is null.
3062 */
3063 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003064
Adam Powelle14579b2009-12-16 18:39:52 -08003065 /**
3066 * Cache the touch slop from the context that created the view.
3067 */
3068 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003071 * Object that handles automatic animation of view properties.
3072 */
3073 private ViewPropertyAnimator mAnimator = null;
3074
3075 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003076 * Flag indicating that a drag can cross window boundaries. When
3077 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3078 * with this flag set, all visible applications will be able to participate
3079 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003080 *
3081 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003082 */
3083 public static final int DRAG_FLAG_GLOBAL = 1;
3084
3085 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003086 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3087 */
3088 private float mVerticalScrollFactor;
3089
3090 /**
Adam Powell20232d02010-12-08 21:08:53 -08003091 * Position of the vertical scroll bar.
3092 */
3093 private int mVerticalScrollbarPosition;
3094
3095 /**
3096 * Position the scroll bar at the default position as determined by the system.
3097 */
3098 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3099
3100 /**
3101 * Position the scroll bar along the left edge.
3102 */
3103 public static final int SCROLLBAR_POSITION_LEFT = 1;
3104
3105 /**
3106 * Position the scroll bar along the right edge.
3107 */
3108 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3109
3110 /**
Romain Guy171c5922011-01-06 10:04:23 -08003111 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003112 *
3113 * @see #getLayerType()
3114 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003115 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003116 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003117 */
3118 public static final int LAYER_TYPE_NONE = 0;
3119
3120 /**
3121 * <p>Indicates that the view has a software layer. A software layer is backed
3122 * by a bitmap and causes the view to be rendered using Android's software
3123 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003124 *
Romain Guy171c5922011-01-06 10:04:23 -08003125 * <p>Software layers have various usages:</p>
3126 * <p>When the application is not using hardware acceleration, a software layer
3127 * is useful to apply a specific color filter and/or blending mode and/or
3128 * translucency to a view and all its children.</p>
3129 * <p>When the application is using hardware acceleration, a software layer
3130 * is useful to render drawing primitives not supported by the hardware
3131 * accelerated pipeline. It can also be used to cache a complex view tree
3132 * into a texture and reduce the complexity of drawing operations. For instance,
3133 * when animating a complex view tree with a translation, a software layer can
3134 * be used to render the view tree only once.</p>
3135 * <p>Software layers should be avoided when the affected view tree updates
3136 * often. Every update will require to re-render the software layer, which can
3137 * potentially be slow (particularly when hardware acceleration is turned on
3138 * since the layer will have to be uploaded into a hardware texture after every
3139 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003140 *
3141 * @see #getLayerType()
3142 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003143 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003144 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003145 */
3146 public static final int LAYER_TYPE_SOFTWARE = 1;
3147
3148 /**
3149 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3150 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3151 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3152 * rendering pipeline, but only if hardware acceleration is turned on for the
3153 * view hierarchy. When hardware acceleration is turned off, hardware layers
3154 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003155 *
Romain Guy171c5922011-01-06 10:04:23 -08003156 * <p>A hardware layer is useful to apply a specific color filter and/or
3157 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003158 * <p>A hardware layer can be used to cache a complex view tree into a
3159 * texture and reduce the complexity of drawing operations. For instance,
3160 * when animating a complex view tree with a translation, a hardware layer can
3161 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003162 * <p>A hardware layer can also be used to increase the rendering quality when
3163 * rotation transformations are applied on a view. It can also be used to
3164 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003165 *
3166 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003167 * @see #setLayerType(int, android.graphics.Paint)
3168 * @see #LAYER_TYPE_NONE
3169 * @see #LAYER_TYPE_SOFTWARE
3170 */
3171 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003172
Romain Guy3aaff3a2011-01-12 14:18:47 -08003173 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3174 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3175 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3176 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3177 })
Romain Guy171c5922011-01-06 10:04:23 -08003178 int mLayerType = LAYER_TYPE_NONE;
3179 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003180 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003181
3182 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003183 * Set to true when the view is sending hover accessibility events because it
3184 * is the innermost hovered view.
3185 */
3186 private boolean mSendingHoverAccessibilityEvents;
3187
3188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 * Simple constructor to use when creating a view from code.
3190 *
3191 * @param context The Context the view is running in, through which it can
3192 * access the current theme, resources, etc.
3193 */
3194 public View(Context context) {
3195 mContext = context;
3196 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003197 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003198 // Set layout and text direction defaults
3199 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003200 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003201 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07003202 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT) |
3203 (ACCESSIBILITY_FOCUSABLE_DEFAULT << ACCESSIBILITY_FOCUSABLE_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003204 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003205 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003206 mUserPaddingStart = -1;
3207 mUserPaddingEnd = -1;
3208 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 }
3210
3211 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003212 * Delegate for injecting accessiblity functionality.
3213 */
3214 AccessibilityDelegate mAccessibilityDelegate;
3215
3216 /**
3217 * Consistency verifier for debugging purposes.
3218 * @hide
3219 */
3220 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3221 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3222 new InputEventConsistencyVerifier(this, 0) : null;
3223
3224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 * Constructor that is called when inflating a view from XML. This is called
3226 * when a view is being constructed from an XML file, supplying attributes
3227 * that were specified in the XML file. This version uses a default style of
3228 * 0, so the only attribute values applied are those in the Context's Theme
3229 * and the given AttributeSet.
3230 *
3231 * <p>
3232 * The method onFinishInflate() will be called after all children have been
3233 * added.
3234 *
3235 * @param context The Context the view is running in, through which it can
3236 * access the current theme, resources, etc.
3237 * @param attrs The attributes of the XML tag that is inflating the view.
3238 * @see #View(Context, AttributeSet, int)
3239 */
3240 public View(Context context, AttributeSet attrs) {
3241 this(context, attrs, 0);
3242 }
3243
3244 /**
3245 * Perform inflation from XML and apply a class-specific base style. This
3246 * constructor of View allows subclasses to use their own base style when
3247 * they are inflating. For example, a Button class's constructor would call
3248 * this version of the super class constructor and supply
3249 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3250 * the theme's button style to modify all of the base view attributes (in
3251 * particular its background) as well as the Button class's attributes.
3252 *
3253 * @param context The Context the view is running in, through which it can
3254 * access the current theme, resources, etc.
3255 * @param attrs The attributes of the XML tag that is inflating the view.
3256 * @param defStyle The default style to apply to this view. If 0, no style
3257 * will be applied (beyond what is included in the theme). This may
3258 * either be an attribute resource, whose value will be retrieved
3259 * from the current theme, or an explicit style resource.
3260 * @see #View(Context, AttributeSet)
3261 */
3262 public View(Context context, AttributeSet attrs, int defStyle) {
3263 this(context);
3264
3265 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3266 defStyle, 0);
3267
3268 Drawable background = null;
3269
3270 int leftPadding = -1;
3271 int topPadding = -1;
3272 int rightPadding = -1;
3273 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003274 int startPadding = -1;
3275 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276
3277 int padding = -1;
3278
3279 int viewFlagValues = 0;
3280 int viewFlagMasks = 0;
3281
3282 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 int x = 0;
3285 int y = 0;
3286
Chet Haase73066682010-11-29 15:55:32 -08003287 float tx = 0;
3288 float ty = 0;
3289 float rotation = 0;
3290 float rotationX = 0;
3291 float rotationY = 0;
3292 float sx = 1f;
3293 float sy = 1f;
3294 boolean transformSet = false;
3295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3297
Adam Powell637d3372010-08-25 14:37:03 -07003298 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 final int N = a.getIndexCount();
3300 for (int i = 0; i < N; i++) {
3301 int attr = a.getIndex(i);
3302 switch (attr) {
3303 case com.android.internal.R.styleable.View_background:
3304 background = a.getDrawable(attr);
3305 break;
3306 case com.android.internal.R.styleable.View_padding:
3307 padding = a.getDimensionPixelSize(attr, -1);
3308 break;
3309 case com.android.internal.R.styleable.View_paddingLeft:
3310 leftPadding = a.getDimensionPixelSize(attr, -1);
3311 break;
3312 case com.android.internal.R.styleable.View_paddingTop:
3313 topPadding = a.getDimensionPixelSize(attr, -1);
3314 break;
3315 case com.android.internal.R.styleable.View_paddingRight:
3316 rightPadding = a.getDimensionPixelSize(attr, -1);
3317 break;
3318 case com.android.internal.R.styleable.View_paddingBottom:
3319 bottomPadding = a.getDimensionPixelSize(attr, -1);
3320 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003321 case com.android.internal.R.styleable.View_paddingStart:
3322 startPadding = a.getDimensionPixelSize(attr, -1);
3323 break;
3324 case com.android.internal.R.styleable.View_paddingEnd:
3325 endPadding = a.getDimensionPixelSize(attr, -1);
3326 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 case com.android.internal.R.styleable.View_scrollX:
3328 x = a.getDimensionPixelOffset(attr, 0);
3329 break;
3330 case com.android.internal.R.styleable.View_scrollY:
3331 y = a.getDimensionPixelOffset(attr, 0);
3332 break;
Chet Haase73066682010-11-29 15:55:32 -08003333 case com.android.internal.R.styleable.View_alpha:
3334 setAlpha(a.getFloat(attr, 1f));
3335 break;
3336 case com.android.internal.R.styleable.View_transformPivotX:
3337 setPivotX(a.getDimensionPixelOffset(attr, 0));
3338 break;
3339 case com.android.internal.R.styleable.View_transformPivotY:
3340 setPivotY(a.getDimensionPixelOffset(attr, 0));
3341 break;
3342 case com.android.internal.R.styleable.View_translationX:
3343 tx = a.getDimensionPixelOffset(attr, 0);
3344 transformSet = true;
3345 break;
3346 case com.android.internal.R.styleable.View_translationY:
3347 ty = a.getDimensionPixelOffset(attr, 0);
3348 transformSet = true;
3349 break;
3350 case com.android.internal.R.styleable.View_rotation:
3351 rotation = a.getFloat(attr, 0);
3352 transformSet = true;
3353 break;
3354 case com.android.internal.R.styleable.View_rotationX:
3355 rotationX = a.getFloat(attr, 0);
3356 transformSet = true;
3357 break;
3358 case com.android.internal.R.styleable.View_rotationY:
3359 rotationY = a.getFloat(attr, 0);
3360 transformSet = true;
3361 break;
3362 case com.android.internal.R.styleable.View_scaleX:
3363 sx = a.getFloat(attr, 1f);
3364 transformSet = true;
3365 break;
3366 case com.android.internal.R.styleable.View_scaleY:
3367 sy = a.getFloat(attr, 1f);
3368 transformSet = true;
3369 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 case com.android.internal.R.styleable.View_id:
3371 mID = a.getResourceId(attr, NO_ID);
3372 break;
3373 case com.android.internal.R.styleable.View_tag:
3374 mTag = a.getText(attr);
3375 break;
3376 case com.android.internal.R.styleable.View_fitsSystemWindows:
3377 if (a.getBoolean(attr, false)) {
3378 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3379 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3380 }
3381 break;
3382 case com.android.internal.R.styleable.View_focusable:
3383 if (a.getBoolean(attr, false)) {
3384 viewFlagValues |= FOCUSABLE;
3385 viewFlagMasks |= FOCUSABLE_MASK;
3386 }
3387 break;
3388 case com.android.internal.R.styleable.View_focusableInTouchMode:
3389 if (a.getBoolean(attr, false)) {
3390 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3391 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3392 }
3393 break;
3394 case com.android.internal.R.styleable.View_clickable:
3395 if (a.getBoolean(attr, false)) {
3396 viewFlagValues |= CLICKABLE;
3397 viewFlagMasks |= CLICKABLE;
3398 }
3399 break;
3400 case com.android.internal.R.styleable.View_longClickable:
3401 if (a.getBoolean(attr, false)) {
3402 viewFlagValues |= LONG_CLICKABLE;
3403 viewFlagMasks |= LONG_CLICKABLE;
3404 }
3405 break;
3406 case com.android.internal.R.styleable.View_saveEnabled:
3407 if (!a.getBoolean(attr, true)) {
3408 viewFlagValues |= SAVE_DISABLED;
3409 viewFlagMasks |= SAVE_DISABLED_MASK;
3410 }
3411 break;
3412 case com.android.internal.R.styleable.View_duplicateParentState:
3413 if (a.getBoolean(attr, false)) {
3414 viewFlagValues |= DUPLICATE_PARENT_STATE;
3415 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3416 }
3417 break;
3418 case com.android.internal.R.styleable.View_visibility:
3419 final int visibility = a.getInt(attr, 0);
3420 if (visibility != 0) {
3421 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3422 viewFlagMasks |= VISIBILITY_MASK;
3423 }
3424 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003425 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003426 // Clear any layout direction flags (included resolved bits) already set
3427 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3428 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003429 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003430 final int value = (layoutDirection != -1) ?
3431 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3432 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003433 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 case com.android.internal.R.styleable.View_drawingCacheQuality:
3435 final int cacheQuality = a.getInt(attr, 0);
3436 if (cacheQuality != 0) {
3437 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3438 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3439 }
3440 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003441 case com.android.internal.R.styleable.View_contentDescription:
3442 mContentDescription = a.getString(attr);
3443 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3445 if (!a.getBoolean(attr, true)) {
3446 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3447 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3448 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003449 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3451 if (!a.getBoolean(attr, true)) {
3452 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3453 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3454 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003455 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 case R.styleable.View_scrollbars:
3457 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3458 if (scrollbars != SCROLLBARS_NONE) {
3459 viewFlagValues |= scrollbars;
3460 viewFlagMasks |= SCROLLBARS_MASK;
3461 initializeScrollbars(a);
3462 }
3463 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003464 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003466 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3467 // Ignore the attribute starting with ICS
3468 break;
3469 }
3470 // With builds < ICS, fall through and apply fading edges
3471 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3473 if (fadingEdge != FADING_EDGE_NONE) {
3474 viewFlagValues |= fadingEdge;
3475 viewFlagMasks |= FADING_EDGE_MASK;
3476 initializeFadingEdge(a);
3477 }
3478 break;
3479 case R.styleable.View_scrollbarStyle:
3480 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3481 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3482 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3483 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3484 }
3485 break;
3486 case R.styleable.View_isScrollContainer:
3487 setScrollContainer = true;
3488 if (a.getBoolean(attr, false)) {
3489 setScrollContainer(true);
3490 }
3491 break;
3492 case com.android.internal.R.styleable.View_keepScreenOn:
3493 if (a.getBoolean(attr, false)) {
3494 viewFlagValues |= KEEP_SCREEN_ON;
3495 viewFlagMasks |= KEEP_SCREEN_ON;
3496 }
3497 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003498 case R.styleable.View_filterTouchesWhenObscured:
3499 if (a.getBoolean(attr, false)) {
3500 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3501 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3502 }
3503 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 case R.styleable.View_nextFocusLeft:
3505 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3506 break;
3507 case R.styleable.View_nextFocusRight:
3508 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3509 break;
3510 case R.styleable.View_nextFocusUp:
3511 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3512 break;
3513 case R.styleable.View_nextFocusDown:
3514 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3515 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003516 case R.styleable.View_nextFocusForward:
3517 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3518 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 case R.styleable.View_minWidth:
3520 mMinWidth = a.getDimensionPixelSize(attr, 0);
3521 break;
3522 case R.styleable.View_minHeight:
3523 mMinHeight = a.getDimensionPixelSize(attr, 0);
3524 break;
Romain Guy9a817362009-05-01 10:57:14 -07003525 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003526 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003527 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003528 + "be used within a restricted context");
3529 }
3530
Romain Guy9a817362009-05-01 10:57:14 -07003531 final String handlerName = a.getString(attr);
3532 if (handlerName != null) {
3533 setOnClickListener(new OnClickListener() {
3534 private Method mHandler;
3535
3536 public void onClick(View v) {
3537 if (mHandler == null) {
3538 try {
3539 mHandler = getContext().getClass().getMethod(handlerName,
3540 View.class);
3541 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003542 int id = getId();
3543 String idText = id == NO_ID ? "" : " with id '"
3544 + getContext().getResources().getResourceEntryName(
3545 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003546 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003547 handlerName + "(View) in the activity "
3548 + getContext().getClass() + " for onClick handler"
3549 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003550 }
3551 }
3552
3553 try {
3554 mHandler.invoke(getContext(), View.this);
3555 } catch (IllegalAccessException e) {
3556 throw new IllegalStateException("Could not execute non "
3557 + "public method of the activity", e);
3558 } catch (InvocationTargetException e) {
3559 throw new IllegalStateException("Could not execute "
3560 + "method of the activity", e);
3561 }
3562 }
3563 });
3564 }
3565 break;
Adam Powell637d3372010-08-25 14:37:03 -07003566 case R.styleable.View_overScrollMode:
3567 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3568 break;
Adam Powell20232d02010-12-08 21:08:53 -08003569 case R.styleable.View_verticalScrollbarPosition:
3570 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3571 break;
Romain Guy171c5922011-01-06 10:04:23 -08003572 case R.styleable.View_layerType:
3573 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3574 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003575 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003576 // Clear any text direction flag already set
3577 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3578 // Set the text direction flags depending on the value of the attribute
3579 final int textDirection = a.getInt(attr, -1);
3580 if (textDirection != -1) {
3581 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3582 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003583 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003584 case R.styleable.View_textAlignment:
3585 // Clear any text alignment flag already set
3586 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3587 // Set the text alignment flag depending on the value of the attribute
3588 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3589 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3590 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003591 case R.styleable.View_importantForAccessibility:
3592 setImportantForAccessibility(a.getInt(attr,
3593 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
3595 }
3596
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003597 a.recycle();
3598
Adam Powell637d3372010-08-25 14:37:03 -07003599 setOverScrollMode(overScrollMode);
3600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003602 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 }
3604
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003605 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3606 // layout direction). Those cached values will be used later during padding resolution.
3607 mUserPaddingStart = startPadding;
3608 mUserPaddingEnd = endPadding;
3609
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003610 updateUserPaddingRelative();
3611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 if (padding >= 0) {
3613 leftPadding = padding;
3614 topPadding = padding;
3615 rightPadding = padding;
3616 bottomPadding = padding;
3617 }
3618
3619 // If the user specified the padding (either with android:padding or
3620 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3621 // use the default padding or the padding from the background drawable
3622 // (stored at this point in mPadding*)
3623 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3624 topPadding >= 0 ? topPadding : mPaddingTop,
3625 rightPadding >= 0 ? rightPadding : mPaddingRight,
3626 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3627
3628 if (viewFlagMasks != 0) {
3629 setFlags(viewFlagValues, viewFlagMasks);
3630 }
3631
3632 // Needs to be called after mViewFlags is set
3633 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3634 recomputePadding();
3635 }
3636
3637 if (x != 0 || y != 0) {
3638 scrollTo(x, y);
3639 }
3640
Chet Haase73066682010-11-29 15:55:32 -08003641 if (transformSet) {
3642 setTranslationX(tx);
3643 setTranslationY(ty);
3644 setRotation(rotation);
3645 setRotationX(rotationX);
3646 setRotationY(rotationY);
3647 setScaleX(sx);
3648 setScaleY(sy);
3649 }
3650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3652 setScrollContainer(true);
3653 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003654
3655 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 }
3657
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003658 private void updateUserPaddingRelative() {
3659 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3660 }
3661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 /**
3663 * Non-public constructor for use in testing
3664 */
3665 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003666 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 }
3668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 /**
3670 * <p>
3671 * Initializes the fading edges from a given set of styled attributes. This
3672 * method should be called by subclasses that need fading edges and when an
3673 * instance of these subclasses is created programmatically rather than
3674 * being inflated from XML. This method is automatically called when the XML
3675 * is inflated.
3676 * </p>
3677 *
3678 * @param a the styled attributes set to initialize the fading edges from
3679 */
3680 protected void initializeFadingEdge(TypedArray a) {
3681 initScrollCache();
3682
3683 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3684 R.styleable.View_fadingEdgeLength,
3685 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3686 }
3687
3688 /**
3689 * Returns the size of the vertical faded edges used to indicate that more
3690 * content in this view is visible.
3691 *
3692 * @return The size in pixels of the vertical faded edge or 0 if vertical
3693 * faded edges are not enabled for this view.
3694 * @attr ref android.R.styleable#View_fadingEdgeLength
3695 */
3696 public int getVerticalFadingEdgeLength() {
3697 if (isVerticalFadingEdgeEnabled()) {
3698 ScrollabilityCache cache = mScrollCache;
3699 if (cache != null) {
3700 return cache.fadingEdgeLength;
3701 }
3702 }
3703 return 0;
3704 }
3705
3706 /**
3707 * Set the size of the faded edge used to indicate that more content in this
3708 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003709 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3710 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3711 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 *
3713 * @param length The size in pixels of the faded edge used to indicate that more
3714 * content in this view is visible.
3715 */
3716 public void setFadingEdgeLength(int length) {
3717 initScrollCache();
3718 mScrollCache.fadingEdgeLength = length;
3719 }
3720
3721 /**
3722 * Returns the size of the horizontal faded edges used to indicate that more
3723 * content in this view is visible.
3724 *
3725 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3726 * faded edges are not enabled for this view.
3727 * @attr ref android.R.styleable#View_fadingEdgeLength
3728 */
3729 public int getHorizontalFadingEdgeLength() {
3730 if (isHorizontalFadingEdgeEnabled()) {
3731 ScrollabilityCache cache = mScrollCache;
3732 if (cache != null) {
3733 return cache.fadingEdgeLength;
3734 }
3735 }
3736 return 0;
3737 }
3738
3739 /**
3740 * Returns the width of the vertical scrollbar.
3741 *
3742 * @return The width in pixels of the vertical scrollbar or 0 if there
3743 * is no vertical scrollbar.
3744 */
3745 public int getVerticalScrollbarWidth() {
3746 ScrollabilityCache cache = mScrollCache;
3747 if (cache != null) {
3748 ScrollBarDrawable scrollBar = cache.scrollBar;
3749 if (scrollBar != null) {
3750 int size = scrollBar.getSize(true);
3751 if (size <= 0) {
3752 size = cache.scrollBarSize;
3753 }
3754 return size;
3755 }
3756 return 0;
3757 }
3758 return 0;
3759 }
3760
3761 /**
3762 * Returns the height of the horizontal scrollbar.
3763 *
3764 * @return The height in pixels of the horizontal scrollbar or 0 if
3765 * there is no horizontal scrollbar.
3766 */
3767 protected int getHorizontalScrollbarHeight() {
3768 ScrollabilityCache cache = mScrollCache;
3769 if (cache != null) {
3770 ScrollBarDrawable scrollBar = cache.scrollBar;
3771 if (scrollBar != null) {
3772 int size = scrollBar.getSize(false);
3773 if (size <= 0) {
3774 size = cache.scrollBarSize;
3775 }
3776 return size;
3777 }
3778 return 0;
3779 }
3780 return 0;
3781 }
3782
3783 /**
3784 * <p>
3785 * Initializes the scrollbars from a given set of styled attributes. This
3786 * method should be called by subclasses that need scrollbars and when an
3787 * instance of these subclasses is created programmatically rather than
3788 * being inflated from XML. This method is automatically called when the XML
3789 * is inflated.
3790 * </p>
3791 *
3792 * @param a the styled attributes set to initialize the scrollbars from
3793 */
3794 protected void initializeScrollbars(TypedArray a) {
3795 initScrollCache();
3796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003798
Mike Cleronf116bf82009-09-27 19:14:12 -07003799 if (scrollabilityCache.scrollBar == null) {
3800 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3801 }
Joe Malin32736f02011-01-19 16:14:20 -08003802
Romain Guy8bda2482010-03-02 11:42:11 -08003803 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804
Mike Cleronf116bf82009-09-27 19:14:12 -07003805 if (!fadeScrollbars) {
3806 scrollabilityCache.state = ScrollabilityCache.ON;
3807 }
3808 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003809
3810
Mike Cleronf116bf82009-09-27 19:14:12 -07003811 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3812 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3813 .getScrollBarFadeDuration());
3814 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3815 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3816 ViewConfiguration.getScrollDefaultDelay());
3817
Joe Malin32736f02011-01-19 16:14:20 -08003818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3820 com.android.internal.R.styleable.View_scrollbarSize,
3821 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3822
3823 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3824 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3825
3826 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3827 if (thumb != null) {
3828 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3829 }
3830
3831 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3832 false);
3833 if (alwaysDraw) {
3834 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3835 }
3836
3837 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3838 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3839
3840 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3841 if (thumb != null) {
3842 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3843 }
3844
3845 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3846 false);
3847 if (alwaysDraw) {
3848 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3849 }
3850
3851 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003852 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 }
3854
3855 /**
3856 * <p>
3857 * Initalizes the scrollability cache if necessary.
3858 * </p>
3859 */
3860 private void initScrollCache() {
3861 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003862 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
3864 }
3865
Philip Milne6c8ea062012-04-03 17:38:43 -07003866 private ScrollabilityCache getScrollCache() {
3867 initScrollCache();
3868 return mScrollCache;
3869 }
3870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 /**
Adam Powell20232d02010-12-08 21:08:53 -08003872 * Set the position of the vertical scroll bar. Should be one of
3873 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3874 * {@link #SCROLLBAR_POSITION_RIGHT}.
3875 *
3876 * @param position Where the vertical scroll bar should be positioned.
3877 */
3878 public void setVerticalScrollbarPosition(int position) {
3879 if (mVerticalScrollbarPosition != position) {
3880 mVerticalScrollbarPosition = position;
3881 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003882 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003883 }
3884 }
3885
3886 /**
3887 * @return The position where the vertical scroll bar will show, if applicable.
3888 * @see #setVerticalScrollbarPosition(int)
3889 */
3890 public int getVerticalScrollbarPosition() {
3891 return mVerticalScrollbarPosition;
3892 }
3893
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003894 ListenerInfo getListenerInfo() {
3895 if (mListenerInfo != null) {
3896 return mListenerInfo;
3897 }
3898 mListenerInfo = new ListenerInfo();
3899 return mListenerInfo;
3900 }
3901
Adam Powell20232d02010-12-08 21:08:53 -08003902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 * Register a callback to be invoked when focus of this view changed.
3904 *
3905 * @param l The callback that will run.
3906 */
3907 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003908 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 }
3910
3911 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003912 * Add a listener that will be called when the bounds of the view change due to
3913 * layout processing.
3914 *
3915 * @param listener The listener that will be called when layout bounds change.
3916 */
3917 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003918 ListenerInfo li = getListenerInfo();
3919 if (li.mOnLayoutChangeListeners == null) {
3920 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003921 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003922 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3923 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003924 }
Chet Haase21cd1382010-09-01 17:42:29 -07003925 }
3926
3927 /**
3928 * Remove a listener for layout changes.
3929 *
3930 * @param listener The listener for layout bounds change.
3931 */
3932 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003933 ListenerInfo li = mListenerInfo;
3934 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003935 return;
3936 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003937 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003938 }
3939
3940 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003941 * Add a listener for attach state changes.
3942 *
3943 * This listener will be called whenever this view is attached or detached
3944 * from a window. Remove the listener using
3945 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3946 *
3947 * @param listener Listener to attach
3948 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3949 */
3950 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 ListenerInfo li = getListenerInfo();
3952 if (li.mOnAttachStateChangeListeners == null) {
3953 li.mOnAttachStateChangeListeners
3954 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003955 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003956 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003957 }
3958
3959 /**
3960 * Remove a listener for attach state changes. The listener will receive no further
3961 * notification of window attach/detach events.
3962 *
3963 * @param listener Listener to remove
3964 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3965 */
3966 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003967 ListenerInfo li = mListenerInfo;
3968 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003969 return;
3970 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003971 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003972 }
3973
3974 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 * Returns the focus-change callback registered for this view.
3976 *
3977 * @return The callback, or null if one is not registered.
3978 */
3979 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003980 ListenerInfo li = mListenerInfo;
3981 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 }
3983
3984 /**
3985 * Register a callback to be invoked when this view is clicked. If this view is not
3986 * clickable, it becomes clickable.
3987 *
3988 * @param l The callback that will run
3989 *
3990 * @see #setClickable(boolean)
3991 */
3992 public void setOnClickListener(OnClickListener l) {
3993 if (!isClickable()) {
3994 setClickable(true);
3995 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003996 getListenerInfo().mOnClickListener = l;
3997 }
3998
3999 /**
4000 * Return whether this view has an attached OnClickListener. Returns
4001 * true if there is a listener, false if there is none.
4002 */
4003 public boolean hasOnClickListeners() {
4004 ListenerInfo li = mListenerInfo;
4005 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 }
4007
4008 /**
4009 * Register a callback to be invoked when this view is clicked and held. If this view is not
4010 * long clickable, it becomes long clickable.
4011 *
4012 * @param l The callback that will run
4013 *
4014 * @see #setLongClickable(boolean)
4015 */
4016 public void setOnLongClickListener(OnLongClickListener l) {
4017 if (!isLongClickable()) {
4018 setLongClickable(true);
4019 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004020 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
4022
4023 /**
4024 * Register a callback to be invoked when the context menu for this view is
4025 * being built. If this view is not long clickable, it becomes long clickable.
4026 *
4027 * @param l The callback that will run
4028 *
4029 */
4030 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4031 if (!isLongClickable()) {
4032 setLongClickable(true);
4033 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004034 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 }
4036
4037 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 * Call this view's OnClickListener, if it is defined. Performs all normal
4039 * actions associated with clicking: reporting accessibility event, playing
4040 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 *
4042 * @return True there was an assigned OnClickListener that was called, false
4043 * otherwise is returned.
4044 */
4045 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004046 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4047
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004048 ListenerInfo li = mListenerInfo;
4049 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004051 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 return true;
4053 }
4054
4055 return false;
4056 }
4057
4058 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004059 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4060 * this only calls the listener, and does not do any associated clicking
4061 * actions like reporting an accessibility event.
4062 *
4063 * @return True there was an assigned OnClickListener that was called, false
4064 * otherwise is returned.
4065 */
4066 public boolean callOnClick() {
4067 ListenerInfo li = mListenerInfo;
4068 if (li != null && li.mOnClickListener != null) {
4069 li.mOnClickListener.onClick(this);
4070 return true;
4071 }
4072 return false;
4073 }
4074
4075 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004076 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4077 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004079 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 */
4081 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004082 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004085 ListenerInfo li = mListenerInfo;
4086 if (li != null && li.mOnLongClickListener != null) {
4087 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 }
4089 if (!handled) {
4090 handled = showContextMenu();
4091 }
4092 if (handled) {
4093 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4094 }
4095 return handled;
4096 }
4097
4098 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004099 * Performs button-related actions during a touch down event.
4100 *
4101 * @param event The event.
4102 * @return True if the down was consumed.
4103 *
4104 * @hide
4105 */
4106 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4107 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4108 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4109 return true;
4110 }
4111 }
4112 return false;
4113 }
4114
4115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 * Bring up the context menu for this view.
4117 *
4118 * @return Whether a context menu was displayed.
4119 */
4120 public boolean showContextMenu() {
4121 return getParent().showContextMenuForChild(this);
4122 }
4123
4124 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004125 * Bring up the context menu for this view, referring to the item under the specified point.
4126 *
4127 * @param x The referenced x coordinate.
4128 * @param y The referenced y coordinate.
4129 * @param metaState The keyboard modifiers that were pressed.
4130 * @return Whether a context menu was displayed.
4131 *
4132 * @hide
4133 */
4134 public boolean showContextMenu(float x, float y, int metaState) {
4135 return showContextMenu();
4136 }
4137
4138 /**
Adam Powell6e346362010-07-23 10:18:23 -07004139 * Start an action mode.
4140 *
4141 * @param callback Callback that will control the lifecycle of the action mode
4142 * @return The new action mode if it is started, null otherwise
4143 *
4144 * @see ActionMode
4145 */
4146 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004147 ViewParent parent = getParent();
4148 if (parent == null) return null;
4149 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004150 }
4151
4152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 * Register a callback to be invoked when a key is pressed in this view.
4154 * @param l the key listener to attach to this view
4155 */
4156 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004157 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
4159
4160 /**
4161 * Register a callback to be invoked when a touch event is sent to this view.
4162 * @param l the touch listener to attach to this view
4163 */
4164 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004165 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
4167
4168 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004169 * Register a callback to be invoked when a generic motion event is sent to this view.
4170 * @param l the generic motion listener to attach to this view
4171 */
4172 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004173 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004174 }
4175
4176 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004177 * Register a callback to be invoked when a hover event is sent to this view.
4178 * @param l the hover listener to attach to this view
4179 */
4180 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004181 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004182 }
4183
4184 /**
Joe Malin32736f02011-01-19 16:14:20 -08004185 * Register a drag event listener callback object for this View. The parameter is
4186 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4187 * View, the system calls the
4188 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4189 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004190 */
4191 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004192 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004193 }
4194
4195 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004196 * Give this view focus. This will cause
4197 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 *
4199 * Note: this does not check whether this {@link View} should get focus, it just
4200 * gives it focus no matter what. It should only be called internally by framework
4201 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4202 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004203 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4204 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 * focus moved when requestFocus() is called. It may not always
4206 * apply, in which case use the default View.FOCUS_DOWN.
4207 * @param previouslyFocusedRect The rectangle of the view that had focus
4208 * prior in this View's coordinate system.
4209 */
4210 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4211 if (DBG) {
4212 System.out.println(this + " requestFocus()");
4213 }
4214
4215 if ((mPrivateFlags & FOCUSED) == 0) {
4216 mPrivateFlags |= FOCUSED;
4217
4218 if (mParent != null) {
4219 mParent.requestChildFocus(this, this);
4220 }
4221
4222 onFocusChanged(true, direction, previouslyFocusedRect);
4223 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004224
4225 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4226 notifyAccessibilityStateChanged();
4227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 }
4229 }
4230
4231 /**
4232 * Request that a rectangle of this view be visible on the screen,
4233 * scrolling if necessary just enough.
4234 *
4235 * <p>A View should call this if it maintains some notion of which part
4236 * of its content is interesting. For example, a text editing view
4237 * should call this when its cursor moves.
4238 *
4239 * @param rectangle The rectangle.
4240 * @return Whether any parent scrolled.
4241 */
4242 public boolean requestRectangleOnScreen(Rect rectangle) {
4243 return requestRectangleOnScreen(rectangle, false);
4244 }
4245
4246 /**
4247 * Request that a rectangle of this view be visible on the screen,
4248 * scrolling if necessary just enough.
4249 *
4250 * <p>A View should call this if it maintains some notion of which part
4251 * of its content is interesting. For example, a text editing view
4252 * should call this when its cursor moves.
4253 *
4254 * <p>When <code>immediate</code> is set to true, scrolling will not be
4255 * animated.
4256 *
4257 * @param rectangle The rectangle.
4258 * @param immediate True to forbid animated scrolling, false otherwise
4259 * @return Whether any parent scrolled.
4260 */
4261 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4262 View child = this;
4263 ViewParent parent = mParent;
4264 boolean scrolled = false;
4265 while (parent != null) {
4266 scrolled |= parent.requestChildRectangleOnScreen(child,
4267 rectangle, immediate);
4268
4269 // offset rect so next call has the rectangle in the
4270 // coordinate system of its direct child.
4271 rectangle.offset(child.getLeft(), child.getTop());
4272 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4273
4274 if (!(parent instanceof View)) {
4275 break;
4276 }
Romain Guy8506ab42009-06-11 17:35:47 -07004277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 child = (View) parent;
4279 parent = child.getParent();
4280 }
4281 return scrolled;
4282 }
4283
4284 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004285 * Called when this view wants to give up focus. If focus is cleared
4286 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4287 * <p>
4288 * <strong>Note:</strong> When a View clears focus the framework is trying
4289 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004290 * View is the first from the top that can take focus, then all callbacks
4291 * related to clearing focus will be invoked after wich the framework will
4292 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004293 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 */
4295 public void clearFocus() {
4296 if (DBG) {
4297 System.out.println(this + " clearFocus()");
4298 }
4299
4300 if ((mPrivateFlags & FOCUSED) != 0) {
4301 mPrivateFlags &= ~FOCUSED;
4302
4303 if (mParent != null) {
4304 mParent.clearChildFocus(this);
4305 }
4306
4307 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004310
4311 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004312
4313 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4314 notifyAccessibilityStateChanged();
4315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 }
4317 }
4318
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004319 void ensureInputFocusOnFirstFocusable() {
4320 View root = getRootView();
4321 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004322 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 }
4324 }
4325
4326 /**
4327 * Called internally by the view system when a new view is getting focus.
4328 * This is what clears the old focus.
4329 */
4330 void unFocus() {
4331 if (DBG) {
4332 System.out.println(this + " unFocus()");
4333 }
4334
4335 if ((mPrivateFlags & FOCUSED) != 0) {
4336 mPrivateFlags &= ~FOCUSED;
4337
4338 onFocusChanged(false, 0, null);
4339 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004340
4341 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4342 notifyAccessibilityStateChanged();
4343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 }
4345 }
4346
4347 /**
4348 * Returns true if this view has focus iteself, or is the ancestor of the
4349 * view that has focus.
4350 *
4351 * @return True if this view has or contains focus, false otherwise.
4352 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004353 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 public boolean hasFocus() {
4355 return (mPrivateFlags & FOCUSED) != 0;
4356 }
4357
4358 /**
4359 * Returns true if this view is focusable or if it contains a reachable View
4360 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4361 * is a View whose parents do not block descendants focus.
4362 *
4363 * Only {@link #VISIBLE} views are considered focusable.
4364 *
4365 * @return True if the view is focusable or if the view contains a focusable
4366 * View, false otherwise.
4367 *
4368 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4369 */
4370 public boolean hasFocusable() {
4371 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4372 }
4373
4374 /**
4375 * Called by the view system when the focus state of this view changes.
4376 * When the focus change event is caused by directional navigation, direction
4377 * and previouslyFocusedRect provide insight into where the focus is coming from.
4378 * When overriding, be sure to call up through to the super class so that
4379 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 * @param gainFocus True if the View has focus; false otherwise.
4382 * @param direction The direction focus has moved when requestFocus()
4383 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004384 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4385 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4386 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4388 * system, of the previously focused view. If applicable, this will be
4389 * passed in as finer grained information about where the focus is coming
4390 * from (in addition to direction). Will be <code>null</code> otherwise.
4391 */
4392 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004393 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004394 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4395 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004396 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004397 }
4398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 InputMethodManager imm = InputMethodManager.peekInstance();
4400 if (!gainFocus) {
4401 if (isPressed()) {
4402 setPressed(false);
4403 }
4404 if (imm != null && mAttachInfo != null
4405 && mAttachInfo.mHasWindowFocus) {
4406 imm.focusOut(this);
4407 }
Romain Guya2431d02009-04-30 16:30:00 -07004408 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 } else if (imm != null && mAttachInfo != null
4410 && mAttachInfo.mHasWindowFocus) {
4411 imm.focusIn(this);
4412 }
Romain Guy8506ab42009-06-11 17:35:47 -07004413
Romain Guy0fd89bf2011-01-26 15:41:30 -08004414 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004415 ListenerInfo li = mListenerInfo;
4416 if (li != null && li.mOnFocusChangeListener != null) {
4417 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 }
Joe Malin32736f02011-01-19 16:14:20 -08004419
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004420 if (mAttachInfo != null) {
4421 mAttachInfo.mKeyDispatchState.reset(this);
4422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 }
4424
4425 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004426 * Sends an accessibility event of the given type. If accessiiblity is
4427 * not enabled this method has no effect. The default implementation calls
4428 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4429 * to populate information about the event source (this View), then calls
4430 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4431 * populate the text content of the event source including its descendants,
4432 * and last calls
4433 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4434 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004435 * <p>
4436 * If an {@link AccessibilityDelegate} has been specified via calling
4437 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4438 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4439 * responsible for handling this call.
4440 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004441 *
Scott Mainb303d832011-10-12 16:45:18 -07004442 * @param eventType The type of the event to send, as defined by several types from
4443 * {@link android.view.accessibility.AccessibilityEvent}, such as
4444 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4445 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004446 *
4447 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4448 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4449 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004450 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004451 */
4452 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004453 if (mAccessibilityDelegate != null) {
4454 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4455 } else {
4456 sendAccessibilityEventInternal(eventType);
4457 }
4458 }
4459
4460 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004461 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4462 * {@link AccessibilityEvent} to make an announcement which is related to some
4463 * sort of a context change for which none of the events representing UI transitions
4464 * is a good fit. For example, announcing a new page in a book. If accessibility
4465 * is not enabled this method does nothing.
4466 *
4467 * @param text The announcement text.
4468 */
4469 public void announceForAccessibility(CharSequence text) {
4470 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4471 AccessibilityEvent event = AccessibilityEvent.obtain(
4472 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4473 event.getText().add(text);
4474 sendAccessibilityEventUnchecked(event);
4475 }
4476 }
4477
4478 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004479 * @see #sendAccessibilityEvent(int)
4480 *
4481 * Note: Called from the default {@link AccessibilityDelegate}.
4482 */
4483 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004484 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4485 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4486 }
4487 }
4488
4489 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004490 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4491 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004492 * perform a check whether accessibility is enabled.
4493 * <p>
4494 * If an {@link AccessibilityDelegate} has been specified via calling
4495 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4496 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4497 * is responsible for handling this call.
4498 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004499 *
4500 * @param event The event to send.
4501 *
4502 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004503 */
4504 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004505 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004506 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004507 } else {
4508 sendAccessibilityEventUncheckedInternal(event);
4509 }
4510 }
4511
4512 /**
4513 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4514 *
4515 * Note: Called from the default {@link AccessibilityDelegate}.
4516 */
4517 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004518 if (!isShown()) {
4519 return;
4520 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004521 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004522 // Only a subset of accessibility events populates text content.
4523 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4524 dispatchPopulateAccessibilityEvent(event);
4525 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004526 // Intercept accessibility focus events fired by virtual nodes to keep
4527 // track of accessibility focus position in such nodes.
4528 final int eventType = event.getEventType();
4529 switch (eventType) {
4530 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4531 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4532 event.getSourceNodeId());
4533 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4534 ViewRootImpl viewRootImpl = getViewRootImpl();
4535 if (viewRootImpl != null) {
4536 viewRootImpl.setAccessibilityFocusedHost(this);
4537 }
4538 }
4539 } break;
4540 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4541 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4542 event.getSourceNodeId());
4543 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4544 ViewRootImpl viewRootImpl = getViewRootImpl();
4545 if (viewRootImpl != null) {
4546 viewRootImpl.setAccessibilityFocusedHost(null);
4547 }
4548 }
4549 } break;
4550 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004551 // In the beginning we called #isShown(), so we know that getParent() is not null.
4552 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004553 }
4554
4555 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004556 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4557 * to its children for adding their text content to the event. Note that the
4558 * event text is populated in a separate dispatch path since we add to the
4559 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004560 * A typical implementation will call
4561 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4562 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4563 * on each child. Override this method if custom population of the event text
4564 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004565 * <p>
4566 * If an {@link AccessibilityDelegate} has been specified via calling
4567 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4568 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4569 * is responsible for handling this call.
4570 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004571 * <p>
4572 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4573 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4574 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004575 *
4576 * @param event The event.
4577 *
4578 * @return True if the event population was completed.
4579 */
4580 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004581 if (mAccessibilityDelegate != null) {
4582 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4583 } else {
4584 return dispatchPopulateAccessibilityEventInternal(event);
4585 }
4586 }
4587
4588 /**
4589 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4590 *
4591 * Note: Called from the default {@link AccessibilityDelegate}.
4592 */
4593 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004594 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004595 return false;
4596 }
4597
4598 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004599 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004601 * text content. While this method is free to modify event
4602 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004603 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4604 * <p>
4605 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004606 * to the text added by the super implementation:
4607 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004608 * super.onPopulateAccessibilityEvent(event);
4609 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4610 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4611 * mCurrentDate.getTimeInMillis(), flags);
4612 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004613 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004614 * <p>
4615 * If an {@link AccessibilityDelegate} has been specified via calling
4616 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4617 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4618 * is responsible for handling this call.
4619 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004620 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4621 * information to the event, in case the default implementation has basic information to add.
4622 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004623 *
4624 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004625 *
4626 * @see #sendAccessibilityEvent(int)
4627 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004628 */
4629 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004630 if (mAccessibilityDelegate != null) {
4631 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4632 } else {
4633 onPopulateAccessibilityEventInternal(event);
4634 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004635 }
4636
4637 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004638 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4639 *
4640 * Note: Called from the default {@link AccessibilityDelegate}.
4641 */
4642 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4643
4644 }
4645
4646 /**
4647 * Initializes an {@link AccessibilityEvent} with information about
4648 * this View which is the event source. In other words, the source of
4649 * an accessibility event is the view whose state change triggered firing
4650 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004651 * <p>
4652 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004653 * to properties set by the super implementation:
4654 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4655 * super.onInitializeAccessibilityEvent(event);
4656 * event.setPassword(true);
4657 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 * <p>
4659 * If an {@link AccessibilityDelegate} has been specified via calling
4660 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4661 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4662 * is responsible for handling this call.
4663 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004664 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4665 * information to the event, in case the default implementation has basic information to add.
4666 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004667 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004668 *
4669 * @see #sendAccessibilityEvent(int)
4670 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4671 */
4672 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004673 if (mAccessibilityDelegate != null) {
4674 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4675 } else {
4676 onInitializeAccessibilityEventInternal(event);
4677 }
4678 }
4679
4680 /**
4681 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4682 *
4683 * Note: Called from the default {@link AccessibilityDelegate}.
4684 */
4685 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004686 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004687 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004688 event.setPackageName(getContext().getPackageName());
4689 event.setEnabled(isEnabled());
4690 event.setContentDescription(mContentDescription);
4691
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004692 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004693 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004694 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4695 FOCUSABLES_ALL);
4696 event.setItemCount(focusablesTempList.size());
4697 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4698 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004699 }
4700 }
4701
4702 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004703 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4704 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4705 * This method is responsible for obtaining an accessibility node info from a
4706 * pool of reusable instances and calling
4707 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4708 * initialize the former.
4709 * <p>
4710 * Note: The client is responsible for recycling the obtained instance by calling
4711 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4712 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004713 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004714 * @return A populated {@link AccessibilityNodeInfo}.
4715 *
4716 * @see AccessibilityNodeInfo
4717 */
4718 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004719 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4720 if (provider != null) {
4721 return provider.createAccessibilityNodeInfo(View.NO_ID);
4722 } else {
4723 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4724 onInitializeAccessibilityNodeInfo(info);
4725 return info;
4726 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004727 }
4728
4729 /**
4730 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4731 * The base implementation sets:
4732 * <ul>
4733 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004734 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4735 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004736 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4737 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4738 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4739 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4740 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4741 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4742 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4743 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4744 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4745 * </ul>
4746 * <p>
4747 * Subclasses should override this method, call the super implementation,
4748 * and set additional attributes.
4749 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004750 * <p>
4751 * If an {@link AccessibilityDelegate} has been specified via calling
4752 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4753 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4754 * is responsible for handling this call.
4755 * </p>
4756 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004757 * @param info The instance to initialize.
4758 */
4759 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004760 if (mAccessibilityDelegate != null) {
4761 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4762 } else {
4763 onInitializeAccessibilityNodeInfoInternal(info);
4764 }
4765 }
4766
4767 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004768 * Gets the location of this view in screen coordintates.
4769 *
4770 * @param outRect The output location
4771 */
4772 private void getBoundsOnScreen(Rect outRect) {
4773 if (mAttachInfo == null) {
4774 return;
4775 }
4776
4777 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004778 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004779
4780 if (!hasIdentityMatrix()) {
4781 getMatrix().mapRect(position);
4782 }
4783
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004784 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004785
4786 ViewParent parent = mParent;
4787 while (parent instanceof View) {
4788 View parentView = (View) parent;
4789
4790 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4791
4792 if (!parentView.hasIdentityMatrix()) {
4793 parentView.getMatrix().mapRect(position);
4794 }
4795
4796 position.offset(parentView.mLeft, parentView.mTop);
4797
4798 parent = parentView.mParent;
4799 }
4800
4801 if (parent instanceof ViewRootImpl) {
4802 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4803 position.offset(0, -viewRootImpl.mCurScrollY);
4804 }
4805
4806 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4807
4808 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4809 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4810 }
4811
4812 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004813 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4814 *
4815 * Note: Called from the default {@link AccessibilityDelegate}.
4816 */
4817 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004818 Rect bounds = mAttachInfo.mTmpInvalRect;
4819 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004820 info.setBoundsInParent(bounds);
4821
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004822 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004823 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004824
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004825 ViewParent parent = getParentForAccessibility();
4826 if (parent instanceof View) {
4827 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004828 }
4829
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004830 info.setVisibleToUser(isVisibleToUser());
4831
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004832 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004833 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004834 info.setContentDescription(getContentDescription());
4835
4836 info.setEnabled(isEnabled());
4837 info.setClickable(isClickable());
4838 info.setFocusable(isFocusable());
4839 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004840 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004841 info.setSelected(isSelected());
4842 info.setLongClickable(isLongClickable());
4843
4844 // TODO: These make sense only if we are in an AdapterView but all
4845 // views can be selected. Maybe from accessiiblity perspective
4846 // we should report as selectable view in an AdapterView.
4847 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4848 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4849
4850 if (isFocusable()) {
4851 if (isFocused()) {
4852 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4853 } else {
4854 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4855 }
4856 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004857
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004858 if (!isAccessibilityFocused()) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07004859 final int mode = getAccessibilityFocusable();
4860 if (mode == ACCESSIBILITY_FOCUSABLE_YES || mode == ACCESSIBILITY_FOCUSABLE_AUTO) {
4861 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4862 }
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004863 } else {
4864 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4865 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004866
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004867 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004868 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4869 }
4870
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004871 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004872 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4873 }
4874
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004875 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004876 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4877 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4878 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004879 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4880 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004881 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004882 }
4883
4884 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004885 * Computes whether this view is visible to the user. Such a view is
4886 * attached, visible, all its predecessors are visible, it is not clipped
4887 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004888 *
4889 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004890 *
4891 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004892 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004893 protected boolean isVisibleToUser() {
4894 return isVisibleToUser(null);
4895 }
4896
4897 /**
4898 * Computes whether the given portion of this view is visible to the user. Such a view is
4899 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4900 * the specified portion is not clipped entirely by its predecessors.
4901 *
4902 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4903 * <code>null</code>, and the entire view will be tested in this case.
4904 * When <code>true</code> is returned by the function, the actual visible
4905 * region will be stored in this parameter; that is, if boundInView is fully
4906 * contained within the view, no modification will be made, otherwise regions
4907 * outside of the visible area of the view will be clipped.
4908 *
4909 * @return Whether the specified portion of the view is visible on the screen.
4910 *
4911 * @hide
4912 */
4913 protected boolean isVisibleToUser(Rect boundInView) {
4914 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4915 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004916 // The first two checks are made also made by isShown() which
4917 // however traverses the tree up to the parent to catch that.
4918 // Therefore, we do some fail fast check to minimize the up
4919 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004920 boolean isVisible = mAttachInfo != null
4921 && mAttachInfo.mWindowVisibility == View.VISIBLE
4922 && getAlpha() > 0
4923 && isShown()
4924 && getGlobalVisibleRect(visibleRect, offset);
4925 if (isVisible && boundInView != null) {
4926 visibleRect.offset(-offset.x, -offset.y);
4927 isVisible &= boundInView.intersect(visibleRect);
4928 }
4929 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004930 }
4931
4932 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004933 * Sets a delegate for implementing accessibility support via compositon as
4934 * opposed to inheritance. The delegate's primary use is for implementing
4935 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4936 *
4937 * @param delegate The delegate instance.
4938 *
4939 * @see AccessibilityDelegate
4940 */
4941 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4942 mAccessibilityDelegate = delegate;
4943 }
4944
4945 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004946 * Gets the provider for managing a virtual view hierarchy rooted at this View
4947 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4948 * that explore the window content.
4949 * <p>
4950 * If this method returns an instance, this instance is responsible for managing
4951 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4952 * View including the one representing the View itself. Similarly the returned
4953 * instance is responsible for performing accessibility actions on any virtual
4954 * view or the root view itself.
4955 * </p>
4956 * <p>
4957 * If an {@link AccessibilityDelegate} has been specified via calling
4958 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4959 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4960 * is responsible for handling this call.
4961 * </p>
4962 *
4963 * @return The provider.
4964 *
4965 * @see AccessibilityNodeProvider
4966 */
4967 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4968 if (mAccessibilityDelegate != null) {
4969 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4970 } else {
4971 return null;
4972 }
4973 }
4974
4975 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004976 * Gets the unique identifier of this view on the screen for accessibility purposes.
4977 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4978 *
4979 * @return The view accessibility id.
4980 *
4981 * @hide
4982 */
4983 public int getAccessibilityViewId() {
4984 if (mAccessibilityViewId == NO_ID) {
4985 mAccessibilityViewId = sNextAccessibilityViewId++;
4986 }
4987 return mAccessibilityViewId;
4988 }
4989
4990 /**
4991 * Gets the unique identifier of the window in which this View reseides.
4992 *
4993 * @return The window accessibility id.
4994 *
4995 * @hide
4996 */
4997 public int getAccessibilityWindowId() {
4998 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4999 }
5000
5001 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005002 * Gets the {@link View} description. It briefly describes the view and is
5003 * primarily used for accessibility support. Set this property to enable
5004 * better accessibility support for your application. This is especially
5005 * true for views that do not have textual representation (For example,
5006 * ImageButton).
5007 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005008 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005009 *
5010 * @attr ref android.R.styleable#View_contentDescription
5011 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005012 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005013 public CharSequence getContentDescription() {
5014 return mContentDescription;
5015 }
5016
5017 /**
5018 * Sets the {@link View} description. It briefly describes the view and is
5019 * primarily used for accessibility support. Set this property to enable
5020 * better accessibility support for your application. This is especially
5021 * true for views that do not have textual representation (For example,
5022 * ImageButton).
5023 *
5024 * @param contentDescription The content description.
5025 *
5026 * @attr ref android.R.styleable#View_contentDescription
5027 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005028 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005029 public void setContentDescription(CharSequence contentDescription) {
5030 mContentDescription = contentDescription;
5031 }
5032
5033 /**
Romain Guya2431d02009-04-30 16:30:00 -07005034 * Invoked whenever this view loses focus, either by losing window focus or by losing
5035 * focus within its window. This method can be used to clear any state tied to the
5036 * focus. For instance, if a button is held pressed with the trackball and the window
5037 * loses focus, this method can be used to cancel the press.
5038 *
5039 * Subclasses of View overriding this method should always call super.onFocusLost().
5040 *
5041 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005042 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005043 *
5044 * @hide pending API council approval
5045 */
5046 protected void onFocusLost() {
5047 resetPressedState();
5048 }
5049
5050 private void resetPressedState() {
5051 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5052 return;
5053 }
5054
5055 if (isPressed()) {
5056 setPressed(false);
5057
5058 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005059 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005060 }
5061 }
5062 }
5063
5064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 * Returns true if this view has focus
5066 *
5067 * @return True if this view has focus, false otherwise.
5068 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005069 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 public boolean isFocused() {
5071 return (mPrivateFlags & FOCUSED) != 0;
5072 }
5073
5074 /**
5075 * Find the view in the hierarchy rooted at this view that currently has
5076 * focus.
5077 *
5078 * @return The view that currently has focus, or null if no focused view can
5079 * be found.
5080 */
5081 public View findFocus() {
5082 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5083 }
5084
5085 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005086 * Indicates whether this view is one of the set of scrollable containers in
5087 * its window.
5088 *
5089 * @return whether this view is one of the set of scrollable containers in
5090 * its window
5091 *
5092 * @attr ref android.R.styleable#View_isScrollContainer
5093 */
5094 public boolean isScrollContainer() {
5095 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5096 }
5097
5098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 * Change whether this view is one of the set of scrollable containers in
5100 * its window. This will be used to determine whether the window can
5101 * resize or must pan when a soft input area is open -- scrollable
5102 * containers allow the window to use resize mode since the container
5103 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005104 *
5105 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 */
5107 public void setScrollContainer(boolean isScrollContainer) {
5108 if (isScrollContainer) {
5109 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5110 mAttachInfo.mScrollContainers.add(this);
5111 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5112 }
5113 mPrivateFlags |= SCROLL_CONTAINER;
5114 } else {
5115 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5116 mAttachInfo.mScrollContainers.remove(this);
5117 }
5118 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5119 }
5120 }
5121
5122 /**
5123 * Returns the quality of the drawing cache.
5124 *
5125 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5126 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5127 *
5128 * @see #setDrawingCacheQuality(int)
5129 * @see #setDrawingCacheEnabled(boolean)
5130 * @see #isDrawingCacheEnabled()
5131 *
5132 * @attr ref android.R.styleable#View_drawingCacheQuality
5133 */
5134 public int getDrawingCacheQuality() {
5135 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5136 }
5137
5138 /**
5139 * Set the drawing cache quality of this view. This value is used only when the
5140 * drawing cache is enabled
5141 *
5142 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5143 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5144 *
5145 * @see #getDrawingCacheQuality()
5146 * @see #setDrawingCacheEnabled(boolean)
5147 * @see #isDrawingCacheEnabled()
5148 *
5149 * @attr ref android.R.styleable#View_drawingCacheQuality
5150 */
5151 public void setDrawingCacheQuality(int quality) {
5152 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5153 }
5154
5155 /**
5156 * Returns whether the screen should remain on, corresponding to the current
5157 * value of {@link #KEEP_SCREEN_ON}.
5158 *
5159 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5160 *
5161 * @see #setKeepScreenOn(boolean)
5162 *
5163 * @attr ref android.R.styleable#View_keepScreenOn
5164 */
5165 public boolean getKeepScreenOn() {
5166 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5167 }
5168
5169 /**
5170 * Controls whether the screen should remain on, modifying the
5171 * value of {@link #KEEP_SCREEN_ON}.
5172 *
5173 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5174 *
5175 * @see #getKeepScreenOn()
5176 *
5177 * @attr ref android.R.styleable#View_keepScreenOn
5178 */
5179 public void setKeepScreenOn(boolean keepScreenOn) {
5180 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5181 }
5182
5183 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005184 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5185 * @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 -08005186 *
5187 * @attr ref android.R.styleable#View_nextFocusLeft
5188 */
5189 public int getNextFocusLeftId() {
5190 return mNextFocusLeftId;
5191 }
5192
5193 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005194 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5195 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5196 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 *
5198 * @attr ref android.R.styleable#View_nextFocusLeft
5199 */
5200 public void setNextFocusLeftId(int nextFocusLeftId) {
5201 mNextFocusLeftId = nextFocusLeftId;
5202 }
5203
5204 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005205 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5206 * @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 -08005207 *
5208 * @attr ref android.R.styleable#View_nextFocusRight
5209 */
5210 public int getNextFocusRightId() {
5211 return mNextFocusRightId;
5212 }
5213
5214 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005215 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5216 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5217 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 *
5219 * @attr ref android.R.styleable#View_nextFocusRight
5220 */
5221 public void setNextFocusRightId(int nextFocusRightId) {
5222 mNextFocusRightId = nextFocusRightId;
5223 }
5224
5225 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005226 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5227 * @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 -08005228 *
5229 * @attr ref android.R.styleable#View_nextFocusUp
5230 */
5231 public int getNextFocusUpId() {
5232 return mNextFocusUpId;
5233 }
5234
5235 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005236 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5237 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5238 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 *
5240 * @attr ref android.R.styleable#View_nextFocusUp
5241 */
5242 public void setNextFocusUpId(int nextFocusUpId) {
5243 mNextFocusUpId = nextFocusUpId;
5244 }
5245
5246 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005247 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5248 * @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 -08005249 *
5250 * @attr ref android.R.styleable#View_nextFocusDown
5251 */
5252 public int getNextFocusDownId() {
5253 return mNextFocusDownId;
5254 }
5255
5256 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005257 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5258 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5259 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 *
5261 * @attr ref android.R.styleable#View_nextFocusDown
5262 */
5263 public void setNextFocusDownId(int nextFocusDownId) {
5264 mNextFocusDownId = nextFocusDownId;
5265 }
5266
5267 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005268 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5269 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5270 *
5271 * @attr ref android.R.styleable#View_nextFocusForward
5272 */
5273 public int getNextFocusForwardId() {
5274 return mNextFocusForwardId;
5275 }
5276
5277 /**
5278 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5279 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5280 * decide automatically.
5281 *
5282 * @attr ref android.R.styleable#View_nextFocusForward
5283 */
5284 public void setNextFocusForwardId(int nextFocusForwardId) {
5285 mNextFocusForwardId = nextFocusForwardId;
5286 }
5287
5288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 * Returns the visibility of this view and all of its ancestors
5290 *
5291 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5292 */
5293 public boolean isShown() {
5294 View current = this;
5295 //noinspection ConstantConditions
5296 do {
5297 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5298 return false;
5299 }
5300 ViewParent parent = current.mParent;
5301 if (parent == null) {
5302 return false; // We are not attached to the view root
5303 }
5304 if (!(parent instanceof View)) {
5305 return true;
5306 }
5307 current = (View) parent;
5308 } while (current != null);
5309
5310 return false;
5311 }
5312
5313 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005314 * Called by the view hierarchy when the content insets for a window have
5315 * changed, to allow it to adjust its content to fit within those windows.
5316 * The content insets tell you the space that the status bar, input method,
5317 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005319 * <p>You do not normally need to deal with this function, since the default
5320 * window decoration given to applications takes care of applying it to the
5321 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5322 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5323 * and your content can be placed under those system elements. You can then
5324 * use this method within your view hierarchy if you have parts of your UI
5325 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005327 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005328 * inset's to the view's padding, consuming that content (modifying the
5329 * insets to be 0), and returning true. This behavior is off by default, but can
5330 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5331 *
5332 * <p>This function's traversal down the hierarchy is depth-first. The same content
5333 * insets object is propagated down the hierarchy, so any changes made to it will
5334 * be seen by all following views (including potentially ones above in
5335 * the hierarchy since this is a depth-first traversal). The first view
5336 * that returns true will abort the entire traversal.
5337 *
5338 * <p>The default implementation works well for a situation where it is
5339 * used with a container that covers the entire window, allowing it to
5340 * apply the appropriate insets to its content on all edges. If you need
5341 * a more complicated layout (such as two different views fitting system
5342 * windows, one on the top of the window, and one on the bottom),
5343 * you can override the method and handle the insets however you would like.
5344 * Note that the insets provided by the framework are always relative to the
5345 * far edges of the window, not accounting for the location of the called view
5346 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005347 * where the layout will place the view, as it is done before layout happens.)
5348 *
5349 * <p>Note: unlike many View methods, there is no dispatch phase to this
5350 * call. If you are overriding it in a ViewGroup and want to allow the
5351 * call to continue to your children, you must be sure to call the super
5352 * implementation.
5353 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005354 * <p>Here is a sample layout that makes use of fitting system windows
5355 * to have controls for a video view placed inside of the window decorations
5356 * that it hides and shows. This can be used with code like the second
5357 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5358 *
5359 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5360 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005361 * @param insets Current content insets of the window. Prior to
5362 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5363 * the insets or else you and Android will be unhappy.
5364 *
5365 * @return Return true if this view applied the insets and it should not
5366 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005367 * @see #getFitsSystemWindows()
5368 * @see #setFitsSystemWindows()
5369 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 */
5371 protected boolean fitSystemWindows(Rect insets) {
5372 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005373 mUserPaddingStart = -1;
5374 mUserPaddingEnd = -1;
5375 mUserPaddingRelative = false;
5376 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5377 || mAttachInfo == null
5378 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5379 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5380 return true;
5381 } else {
5382 internalSetPadding(0, 0, 0, 0);
5383 return false;
5384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 }
5386 return false;
5387 }
5388
5389 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005390 * Sets whether or not this view should account for system screen decorations
5391 * such as the status bar and inset its content; that is, controlling whether
5392 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5393 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005394 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005395 * <p>Note that if you are providing your own implementation of
5396 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5397 * flag to true -- your implementation will be overriding the default
5398 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005399 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005400 * @param fitSystemWindows If true, then the default implementation of
5401 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005402 *
5403 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005404 * @see #getFitsSystemWindows()
5405 * @see #fitSystemWindows(Rect)
5406 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005407 */
5408 public void setFitsSystemWindows(boolean fitSystemWindows) {
5409 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5410 }
5411
5412 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005413 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005414 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5415 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005416 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005417 * @return Returns true if the default implementation of
5418 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005419 *
5420 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005421 * @see #setFitsSystemWindows()
5422 * @see #fitSystemWindows(Rect)
5423 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005424 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005425 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005426 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5427 }
5428
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005429 /** @hide */
5430 public boolean fitsSystemWindows() {
5431 return getFitsSystemWindows();
5432 }
5433
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005434 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005435 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5436 */
5437 public void requestFitSystemWindows() {
5438 if (mParent != null) {
5439 mParent.requestFitSystemWindows();
5440 }
5441 }
5442
5443 /**
5444 * For use by PhoneWindow to make its own system window fitting optional.
5445 * @hide
5446 */
5447 public void makeOptionalFitsSystemWindows() {
5448 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5449 }
5450
5451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 * Returns the visibility status for this view.
5453 *
5454 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5455 * @attr ref android.R.styleable#View_visibility
5456 */
5457 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005458 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5459 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5460 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 })
5462 public int getVisibility() {
5463 return mViewFlags & VISIBILITY_MASK;
5464 }
5465
5466 /**
5467 * Set the enabled state of this view.
5468 *
5469 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5470 * @attr ref android.R.styleable#View_visibility
5471 */
5472 @RemotableViewMethod
5473 public void setVisibility(int visibility) {
5474 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005475 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 }
5477
5478 /**
5479 * Returns the enabled status for this view. The interpretation of the
5480 * enabled state varies by subclass.
5481 *
5482 * @return True if this view is enabled, false otherwise.
5483 */
5484 @ViewDebug.ExportedProperty
5485 public boolean isEnabled() {
5486 return (mViewFlags & ENABLED_MASK) == ENABLED;
5487 }
5488
5489 /**
5490 * Set the enabled state of this view. The interpretation of the enabled
5491 * state varies by subclass.
5492 *
5493 * @param enabled True if this view is enabled, false otherwise.
5494 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005495 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005497 if (enabled == isEnabled()) return;
5498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5500
5501 /*
5502 * The View most likely has to change its appearance, so refresh
5503 * the drawable state.
5504 */
5505 refreshDrawableState();
5506
5507 // Invalidate too, since the default behavior for views is to be
5508 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005509 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 }
5511
5512 /**
5513 * Set whether this view can receive the focus.
5514 *
5515 * Setting this to false will also ensure that this view is not focusable
5516 * in touch mode.
5517 *
5518 * @param focusable If true, this view can receive the focus.
5519 *
5520 * @see #setFocusableInTouchMode(boolean)
5521 * @attr ref android.R.styleable#View_focusable
5522 */
5523 public void setFocusable(boolean focusable) {
5524 if (!focusable) {
5525 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5526 }
5527 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5528 }
5529
5530 /**
5531 * Set whether this view can receive focus while in touch mode.
5532 *
5533 * Setting this to true will also ensure that this view is focusable.
5534 *
5535 * @param focusableInTouchMode If true, this view can receive the focus while
5536 * in touch mode.
5537 *
5538 * @see #setFocusable(boolean)
5539 * @attr ref android.R.styleable#View_focusableInTouchMode
5540 */
5541 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5542 // Focusable in touch mode should always be set before the focusable flag
5543 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5544 // which, in touch mode, will not successfully request focus on this view
5545 // because the focusable in touch mode flag is not set
5546 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5547 if (focusableInTouchMode) {
5548 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5549 }
5550 }
5551
5552 /**
5553 * Set whether this view should have sound effects enabled for events such as
5554 * clicking and touching.
5555 *
5556 * <p>You may wish to disable sound effects for a view if you already play sounds,
5557 * for instance, a dial key that plays dtmf tones.
5558 *
5559 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5560 * @see #isSoundEffectsEnabled()
5561 * @see #playSoundEffect(int)
5562 * @attr ref android.R.styleable#View_soundEffectsEnabled
5563 */
5564 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5565 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5566 }
5567
5568 /**
5569 * @return whether this view should have sound effects enabled for events such as
5570 * clicking and touching.
5571 *
5572 * @see #setSoundEffectsEnabled(boolean)
5573 * @see #playSoundEffect(int)
5574 * @attr ref android.R.styleable#View_soundEffectsEnabled
5575 */
5576 @ViewDebug.ExportedProperty
5577 public boolean isSoundEffectsEnabled() {
5578 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5579 }
5580
5581 /**
5582 * Set whether this view should have haptic feedback for events such as
5583 * long presses.
5584 *
5585 * <p>You may wish to disable haptic feedback if your view already controls
5586 * its own haptic feedback.
5587 *
5588 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5589 * @see #isHapticFeedbackEnabled()
5590 * @see #performHapticFeedback(int)
5591 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5592 */
5593 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5594 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5595 }
5596
5597 /**
5598 * @return whether this view should have haptic feedback enabled for events
5599 * long presses.
5600 *
5601 * @see #setHapticFeedbackEnabled(boolean)
5602 * @see #performHapticFeedback(int)
5603 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5604 */
5605 @ViewDebug.ExportedProperty
5606 public boolean isHapticFeedbackEnabled() {
5607 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5608 }
5609
5610 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005611 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005612 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005613 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5614 * {@link #LAYOUT_DIRECTION_RTL},
5615 * {@link #LAYOUT_DIRECTION_INHERIT} or
5616 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005617 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005618 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005619 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005620 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005621 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005622 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5623 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5624 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5625 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005626 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005627 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005628 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005629 }
5630
5631 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005632 * Set the layout direction for this view. This will propagate a reset of layout direction
5633 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005634 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005635 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5636 * {@link #LAYOUT_DIRECTION_RTL},
5637 * {@link #LAYOUT_DIRECTION_INHERIT} or
5638 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005639 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005640 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005641 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005642 */
5643 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005644 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005645 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005646 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005647 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005648 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005649 // Set the new layout direction (filtered) and ask for a layout pass
5650 mPrivateFlags2 |=
5651 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5652 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005653 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005654 }
5655
5656 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005657 * Returns the resolved layout direction for this view.
5658 *
5659 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005660 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005661 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005662 */
5663 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005664 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5665 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005666 })
5667 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005668 // The layout diretion will be resolved only if needed
5669 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5670 resolveLayoutDirection();
5671 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005672 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005673 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5674 }
5675
5676 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005677 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5678 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005679 *
5680 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005681 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005682 */
5683 @ViewDebug.ExportedProperty(category = "layout")
5684 public boolean isLayoutRtl() {
5685 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5686 }
5687
5688 /**
Adam Powell539ee872012-02-03 19:00:49 -08005689 * Indicates whether the view is currently tracking transient state that the
5690 * app should not need to concern itself with saving and restoring, but that
5691 * the framework should take special note to preserve when possible.
5692 *
Adam Powell785c4472012-05-02 21:25:39 -07005693 * <p>A view with transient state cannot be trivially rebound from an external
5694 * data source, such as an adapter binding item views in a list. This may be
5695 * because the view is performing an animation, tracking user selection
5696 * of content, or similar.</p>
5697 *
Adam Powell539ee872012-02-03 19:00:49 -08005698 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005699 */
5700 @ViewDebug.ExportedProperty(category = "layout")
5701 public boolean hasTransientState() {
5702 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5703 }
5704
5705 /**
5706 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005707 * framework should attempt to preserve when possible. This flag is reference counted,
5708 * so every call to setHasTransientState(true) should be paired with a later call
5709 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005710 *
Adam Powell785c4472012-05-02 21:25:39 -07005711 * <p>A view with transient state cannot be trivially rebound from an external
5712 * data source, such as an adapter binding item views in a list. This may be
5713 * because the view is performing an animation, tracking user selection
5714 * of content, or similar.</p>
5715 *
Adam Powell539ee872012-02-03 19:00:49 -08005716 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005717 */
5718 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005719 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5720 mTransientStateCount - 1;
5721 if (mTransientStateCount < 0) {
5722 mTransientStateCount = 0;
5723 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5724 "unmatched pair of setHasTransientState calls");
5725 }
5726 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005727 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005728 // update flag if we've just incremented up from 0 or decremented down to 0
5729 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5730 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5731 if (mParent != null) {
5732 try {
5733 mParent.childHasTransientStateChanged(this, hasTransientState);
5734 } catch (AbstractMethodError e) {
5735 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5736 " does not fully implement ViewParent", e);
5737 }
Adam Powell539ee872012-02-03 19:00:49 -08005738 }
5739 }
5740 }
5741
5742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 * If this view doesn't do any drawing on its own, set this flag to
5744 * allow further optimizations. By default, this flag is not set on
5745 * View, but could be set on some View subclasses such as ViewGroup.
5746 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005747 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5748 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 *
5750 * @param willNotDraw whether or not this View draw on its own
5751 */
5752 public void setWillNotDraw(boolean willNotDraw) {
5753 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5754 }
5755
5756 /**
5757 * Returns whether or not this View draws on its own.
5758 *
5759 * @return true if this view has nothing to draw, false otherwise
5760 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005761 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 public boolean willNotDraw() {
5763 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5764 }
5765
5766 /**
5767 * When a View's drawing cache is enabled, drawing is redirected to an
5768 * offscreen bitmap. Some views, like an ImageView, must be able to
5769 * bypass this mechanism if they already draw a single bitmap, to avoid
5770 * unnecessary usage of the memory.
5771 *
5772 * @param willNotCacheDrawing true if this view does not cache its
5773 * drawing, false otherwise
5774 */
5775 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5776 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5777 }
5778
5779 /**
5780 * Returns whether or not this View can cache its drawing or not.
5781 *
5782 * @return true if this view does not cache its drawing, false otherwise
5783 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005784 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 public boolean willNotCacheDrawing() {
5786 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5787 }
5788
5789 /**
5790 * Indicates whether this view reacts to click events or not.
5791 *
5792 * @return true if the view is clickable, false otherwise
5793 *
5794 * @see #setClickable(boolean)
5795 * @attr ref android.R.styleable#View_clickable
5796 */
5797 @ViewDebug.ExportedProperty
5798 public boolean isClickable() {
5799 return (mViewFlags & CLICKABLE) == CLICKABLE;
5800 }
5801
5802 /**
5803 * Enables or disables click events for this view. When a view
5804 * is clickable it will change its state to "pressed" on every click.
5805 * Subclasses should set the view clickable to visually react to
5806 * user's clicks.
5807 *
5808 * @param clickable true to make the view clickable, false otherwise
5809 *
5810 * @see #isClickable()
5811 * @attr ref android.R.styleable#View_clickable
5812 */
5813 public void setClickable(boolean clickable) {
5814 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5815 }
5816
5817 /**
5818 * Indicates whether this view reacts to long click events or not.
5819 *
5820 * @return true if the view is long clickable, false otherwise
5821 *
5822 * @see #setLongClickable(boolean)
5823 * @attr ref android.R.styleable#View_longClickable
5824 */
5825 public boolean isLongClickable() {
5826 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5827 }
5828
5829 /**
5830 * Enables or disables long click events for this view. When a view is long
5831 * clickable it reacts to the user holding down the button for a longer
5832 * duration than a tap. This event can either launch the listener or a
5833 * context menu.
5834 *
5835 * @param longClickable true to make the view long clickable, false otherwise
5836 * @see #isLongClickable()
5837 * @attr ref android.R.styleable#View_longClickable
5838 */
5839 public void setLongClickable(boolean longClickable) {
5840 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5841 }
5842
5843 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005844 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 *
5846 * @see #isClickable()
5847 * @see #setClickable(boolean)
5848 *
5849 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5850 * the View's internal state from a previously set "pressed" state.
5851 */
5852 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005853 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 if (pressed) {
5856 mPrivateFlags |= PRESSED;
5857 } else {
5858 mPrivateFlags &= ~PRESSED;
5859 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005860
5861 if (needsRefresh) {
5862 refreshDrawableState();
5863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 dispatchSetPressed(pressed);
5865 }
5866
5867 /**
5868 * Dispatch setPressed to all of this View's children.
5869 *
5870 * @see #setPressed(boolean)
5871 *
5872 * @param pressed The new pressed state
5873 */
5874 protected void dispatchSetPressed(boolean pressed) {
5875 }
5876
5877 /**
5878 * Indicates whether the view is currently in pressed state. Unless
5879 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5880 * the pressed state.
5881 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005882 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 * @see #isClickable()
5884 * @see #setClickable(boolean)
5885 *
5886 * @return true if the view is currently pressed, false otherwise
5887 */
5888 public boolean isPressed() {
5889 return (mPrivateFlags & PRESSED) == PRESSED;
5890 }
5891
5892 /**
5893 * Indicates whether this view will save its state (that is,
5894 * whether its {@link #onSaveInstanceState} method will be called).
5895 *
5896 * @return Returns true if the view state saving is enabled, else false.
5897 *
5898 * @see #setSaveEnabled(boolean)
5899 * @attr ref android.R.styleable#View_saveEnabled
5900 */
5901 public boolean isSaveEnabled() {
5902 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5903 }
5904
5905 /**
5906 * Controls whether the saving of this view's state is
5907 * enabled (that is, whether its {@link #onSaveInstanceState} method
5908 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005909 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 * for its state to be saved. This flag can only disable the
5911 * saving of this view; any child views may still have their state saved.
5912 *
5913 * @param enabled Set to false to <em>disable</em> state saving, or true
5914 * (the default) to allow it.
5915 *
5916 * @see #isSaveEnabled()
5917 * @see #setId(int)
5918 * @see #onSaveInstanceState()
5919 * @attr ref android.R.styleable#View_saveEnabled
5920 */
5921 public void setSaveEnabled(boolean enabled) {
5922 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5923 }
5924
Jeff Brown85a31762010-09-01 17:01:00 -07005925 /**
5926 * Gets whether the framework should discard touches when the view's
5927 * window is obscured by another visible window.
5928 * Refer to the {@link View} security documentation for more details.
5929 *
5930 * @return True if touch filtering is enabled.
5931 *
5932 * @see #setFilterTouchesWhenObscured(boolean)
5933 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5934 */
5935 @ViewDebug.ExportedProperty
5936 public boolean getFilterTouchesWhenObscured() {
5937 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5938 }
5939
5940 /**
5941 * Sets whether the framework should discard touches when the view's
5942 * window is obscured by another visible window.
5943 * Refer to the {@link View} security documentation for more details.
5944 *
5945 * @param enabled True if touch filtering should be enabled.
5946 *
5947 * @see #getFilterTouchesWhenObscured
5948 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5949 */
5950 public void setFilterTouchesWhenObscured(boolean enabled) {
5951 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5952 FILTER_TOUCHES_WHEN_OBSCURED);
5953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954
5955 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005956 * Indicates whether the entire hierarchy under this view will save its
5957 * state when a state saving traversal occurs from its parent. The default
5958 * is true; if false, these views will not be saved unless
5959 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5960 *
5961 * @return Returns true if the view state saving from parent is enabled, else false.
5962 *
5963 * @see #setSaveFromParentEnabled(boolean)
5964 */
5965 public boolean isSaveFromParentEnabled() {
5966 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5967 }
5968
5969 /**
5970 * Controls whether the entire hierarchy under this view will save its
5971 * state when a state saving traversal occurs from its parent. The default
5972 * is true; if false, these views will not be saved unless
5973 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5974 *
5975 * @param enabled Set to false to <em>disable</em> state saving, or true
5976 * (the default) to allow it.
5977 *
5978 * @see #isSaveFromParentEnabled()
5979 * @see #setId(int)
5980 * @see #onSaveInstanceState()
5981 */
5982 public void setSaveFromParentEnabled(boolean enabled) {
5983 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5984 }
5985
5986
5987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 * Returns whether this View is able to take focus.
5989 *
5990 * @return True if this view can take focus, or false otherwise.
5991 * @attr ref android.R.styleable#View_focusable
5992 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005993 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 public final boolean isFocusable() {
5995 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5996 }
5997
5998 /**
5999 * When a view is focusable, it may not want to take focus when in touch mode.
6000 * For example, a button would like focus when the user is navigating via a D-pad
6001 * so that the user can click on it, but once the user starts touching the screen,
6002 * the button shouldn't take focus
6003 * @return Whether the view is focusable in touch mode.
6004 * @attr ref android.R.styleable#View_focusableInTouchMode
6005 */
6006 @ViewDebug.ExportedProperty
6007 public final boolean isFocusableInTouchMode() {
6008 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6009 }
6010
6011 /**
6012 * Find the nearest view in the specified direction that can take focus.
6013 * This does not actually give focus to that view.
6014 *
6015 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6016 *
6017 * @return The nearest focusable in the specified direction, or null if none
6018 * can be found.
6019 */
6020 public View focusSearch(int direction) {
6021 if (mParent != null) {
6022 return mParent.focusSearch(this, direction);
6023 } else {
6024 return null;
6025 }
6026 }
6027
6028 /**
6029 * This method is the last chance for the focused view and its ancestors to
6030 * respond to an arrow key. This is called when the focused view did not
6031 * consume the key internally, nor could the view system find a new view in
6032 * the requested direction to give focus to.
6033 *
6034 * @param focused The currently focused view.
6035 * @param direction The direction focus wants to move. One of FOCUS_UP,
6036 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6037 * @return True if the this view consumed this unhandled move.
6038 */
6039 public boolean dispatchUnhandledMove(View focused, int direction) {
6040 return false;
6041 }
6042
6043 /**
6044 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006045 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006047 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6048 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 * @return The user specified next view, or null if there is none.
6050 */
6051 View findUserSetNextFocus(View root, int direction) {
6052 switch (direction) {
6053 case FOCUS_LEFT:
6054 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006055 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 case FOCUS_RIGHT:
6057 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006058 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 case FOCUS_UP:
6060 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006061 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 case FOCUS_DOWN:
6063 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006064 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006065 case FOCUS_FORWARD:
6066 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006067 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006068 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006069 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006070 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006071 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006072 @Override
6073 public boolean apply(View t) {
6074 return t.mNextFocusForwardId == id;
6075 }
6076 });
6077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 }
6079 return null;
6080 }
6081
Jeff Brown4dfbec22011-08-15 14:55:37 -07006082 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6083 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6084 @Override
6085 public boolean apply(View t) {
6086 return t.mID == childViewId;
6087 }
6088 });
6089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 if (result == null) {
6091 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6092 + "by user for id " + childViewId);
6093 }
6094 return result;
6095 }
6096
6097 /**
6098 * Find and return all focusable views that are descendants of this view,
6099 * possibly including this view if it is focusable itself.
6100 *
6101 * @param direction The direction of the focus
6102 * @return A list of focusable views
6103 */
6104 public ArrayList<View> getFocusables(int direction) {
6105 ArrayList<View> result = new ArrayList<View>(24);
6106 addFocusables(result, direction);
6107 return result;
6108 }
6109
6110 /**
6111 * Add any focusable views that are descendants of this view (possibly
6112 * including this view if it is focusable itself) to views. If we are in touch mode,
6113 * only add views that are also focusable in touch mode.
6114 *
6115 * @param views Focusable views found so far
6116 * @param direction The direction of the focus
6117 */
6118 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006119 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121
svetoslavganov75986cf2009-05-14 22:28:01 -07006122 /**
6123 * Adds any focusable views that are descendants of this view (possibly
6124 * including this view if it is focusable itself) to views. This method
6125 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006126 * only views focusable in touch mode if we are in touch mode or
6127 * only views that can take accessibility focus if accessibility is enabeld
6128 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006129 *
6130 * @param views Focusable views found so far or null if all we are interested is
6131 * the number of focusables.
6132 * @param direction The direction of the focus.
6133 * @param focusableMode The type of focusables to be added.
6134 *
6135 * @see #FOCUSABLES_ALL
6136 * @see #FOCUSABLES_TOUCH_MODE
6137 */
6138 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006139 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006140 return;
6141 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006142 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006143 if (isAccessibilityFocusable()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006144 views.add(this);
6145 return;
6146 }
6147 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006148 if (!isFocusable()) {
6149 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006150 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006151 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6152 && isInTouchMode() && !isFocusableInTouchMode()) {
6153 return;
6154 }
6155 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 }
6157
6158 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006159 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006160 * The search is performed by either the text that the View renders or the content
6161 * description that describes the view for accessibility purposes and the view does
6162 * not render or both. Clients can specify how the search is to be performed via
6163 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6164 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006165 *
6166 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006167 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006168 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006169 * @see #FIND_VIEWS_WITH_TEXT
6170 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6171 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006172 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006173 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006174 if (getAccessibilityNodeProvider() != null) {
6175 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6176 outViews.add(this);
6177 }
6178 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006179 && (searched != null && searched.length() > 0)
6180 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006181 String searchedLowerCase = searched.toString().toLowerCase();
6182 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6183 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6184 outViews.add(this);
6185 }
6186 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006187 }
6188
6189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 * Find and return all touchable views that are descendants of this view,
6191 * possibly including this view if it is touchable itself.
6192 *
6193 * @return A list of touchable views
6194 */
6195 public ArrayList<View> getTouchables() {
6196 ArrayList<View> result = new ArrayList<View>();
6197 addTouchables(result);
6198 return result;
6199 }
6200
6201 /**
6202 * Add any touchable views that are descendants of this view (possibly
6203 * including this view if it is touchable itself) to views.
6204 *
6205 * @param views Touchable views found so far
6206 */
6207 public void addTouchables(ArrayList<View> views) {
6208 final int viewFlags = mViewFlags;
6209
6210 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6211 && (viewFlags & ENABLED_MASK) == ENABLED) {
6212 views.add(this);
6213 }
6214 }
6215
6216 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006217 * Returns whether this View is accessibility focused.
6218 *
6219 * @return True if this View is accessibility focused.
6220 */
6221 boolean isAccessibilityFocused() {
6222 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6223 }
6224
6225 /**
6226 * Call this to try to give accessibility focus to this view.
6227 *
6228 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6229 * returns false or the view is no visible or the view already has accessibility
6230 * focus.
6231 *
6232 * See also {@link #focusSearch(int)}, which is what you call to say that you
6233 * have focus, and you want your parent to look for the next one.
6234 *
6235 * @return Whether this view actually took accessibility focus.
6236 *
6237 * @hide
6238 */
6239 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006240 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6241 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006242 return false;
6243 }
6244 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6245 return false;
6246 }
6247 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6248 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6249 ViewRootImpl viewRootImpl = getViewRootImpl();
6250 if (viewRootImpl != null) {
6251 viewRootImpl.setAccessibilityFocusedHost(this);
6252 }
6253 invalidate();
6254 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6255 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006256 return true;
6257 }
6258 return false;
6259 }
6260
6261 /**
6262 * Call this to try to clear accessibility focus of this view.
6263 *
6264 * See also {@link #focusSearch(int)}, which is what you call to say that you
6265 * have focus, and you want your parent to look for the next one.
6266 *
6267 * @hide
6268 */
6269 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006270 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6271 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006272 invalidate();
6273 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6274 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006275 // Clear the text navigation state.
6276 setAccessibilityCursorPosition(-1);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006277 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006278 // Clear the global reference of accessibility focus if this
6279 // view or any of its descendants had accessibility focus.
6280 ViewRootImpl viewRootImpl = getViewRootImpl();
6281 if (viewRootImpl != null) {
6282 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6283 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6284 viewRootImpl.setAccessibilityFocusedHost(null);
6285 }
6286 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006287 }
6288
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006289 private void requestAccessibilityFocusFromHover() {
6290 if (includeForAccessibility() && isActionableForAccessibility()) {
6291 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006292 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006293 } else {
6294 if (mParent != null) {
6295 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6296 if (nextFocus != null) {
6297 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006298 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006299 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006300 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006301 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006302 }
6303
6304 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006305 * @hide
6306 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006307 public boolean canTakeAccessibilityFocusFromHover() {
6308 if (includeForAccessibility() && isActionableForAccessibility()) {
6309 return true;
6310 }
6311 if (mParent != null) {
6312 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006313 }
6314 return false;
6315 }
6316
6317 /**
6318 * Clears accessibility focus without calling any callback methods
6319 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6320 * is used for clearing accessibility focus when giving this focus to
6321 * another view.
6322 */
6323 void clearAccessibilityFocusNoCallbacks() {
6324 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6325 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6326 invalidate();
6327 }
6328 }
6329
6330 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 * Call this to try to give focus to a specific view or to one of its
6332 * descendants.
6333 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006334 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6335 * false), or if it is focusable and it is not focusable in touch mode
6336 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006338 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 * have focus, and you want your parent to look for the next one.
6340 *
6341 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6342 * {@link #FOCUS_DOWN} and <code>null</code>.
6343 *
6344 * @return Whether this view or one of its descendants actually took focus.
6345 */
6346 public final boolean requestFocus() {
6347 return requestFocus(View.FOCUS_DOWN);
6348 }
6349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 /**
6351 * Call this to try to give focus to a specific view or to one of its
6352 * descendants and give it a hint about what direction focus is heading.
6353 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006354 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6355 * false), or if it is focusable and it is not focusable in touch mode
6356 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006358 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 * have focus, and you want your parent to look for the next one.
6360 *
6361 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6362 * <code>null</code> set for the previously focused rectangle.
6363 *
6364 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6365 * @return Whether this view or one of its descendants actually took focus.
6366 */
6367 public final boolean requestFocus(int direction) {
6368 return requestFocus(direction, null);
6369 }
6370
6371 /**
6372 * Call this to try to give focus to a specific view or to one of its descendants
6373 * and give it hints about the direction and a specific rectangle that the focus
6374 * is coming from. The rectangle can help give larger views a finer grained hint
6375 * about where focus is coming from, and therefore, where to show selection, or
6376 * forward focus change internally.
6377 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006378 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6379 * false), or if it is focusable and it is not focusable in touch mode
6380 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 *
6382 * A View will not take focus if it is not visible.
6383 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006384 * A View will not take focus if one of its parents has
6385 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6386 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006388 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 * have focus, and you want your parent to look for the next one.
6390 *
6391 * You may wish to override this method if your custom {@link View} has an internal
6392 * {@link View} that it wishes to forward the request to.
6393 *
6394 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6395 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6396 * to give a finer grained hint about where focus is coming from. May be null
6397 * if there is no hint.
6398 * @return Whether this view or one of its descendants actually took focus.
6399 */
6400 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006401 return requestFocusNoSearch(direction, previouslyFocusedRect);
6402 }
6403
6404 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 // need to be focusable
6406 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6407 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6408 return false;
6409 }
6410
6411 // need to be focusable in touch mode if in touch mode
6412 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006413 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6414 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 }
6416
6417 // need to not have any parents blocking us
6418 if (hasAncestorThatBlocksDescendantFocus()) {
6419 return false;
6420 }
6421
6422 handleFocusGainInternal(direction, previouslyFocusedRect);
6423 return true;
6424 }
6425
6426 /**
6427 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6428 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6429 * touch mode to request focus when they are touched.
6430 *
6431 * @return Whether this view or one of its descendants actually took focus.
6432 *
6433 * @see #isInTouchMode()
6434 *
6435 */
6436 public final boolean requestFocusFromTouch() {
6437 // Leave touch mode if we need to
6438 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006439 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006440 if (viewRoot != null) {
6441 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 }
6443 }
6444 return requestFocus(View.FOCUS_DOWN);
6445 }
6446
6447 /**
6448 * @return Whether any ancestor of this view blocks descendant focus.
6449 */
6450 private boolean hasAncestorThatBlocksDescendantFocus() {
6451 ViewParent ancestor = mParent;
6452 while (ancestor instanceof ViewGroup) {
6453 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6454 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6455 return true;
6456 } else {
6457 ancestor = vgAncestor.getParent();
6458 }
6459 }
6460 return false;
6461 }
6462
6463 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006464 * Gets the mode for determining whether this View is important for accessibility
6465 * which is if it fires accessibility events and if it is reported to
6466 * accessibility services that query the screen.
6467 *
6468 * @return The mode for determining whether a View is important for accessibility.
6469 *
6470 * @attr ref android.R.styleable#View_importantForAccessibility
6471 *
6472 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6473 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6474 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6475 */
6476 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006477 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6478 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6479 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006480 })
6481 public int getImportantForAccessibility() {
6482 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6483 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6484 }
6485
6486 /**
6487 * Sets how to determine whether this view is important for accessibility
6488 * which is if it fires accessibility events and if it is reported to
6489 * accessibility services that query the screen.
6490 *
6491 * @param mode How to determine whether this view is important for accessibility.
6492 *
6493 * @attr ref android.R.styleable#View_importantForAccessibility
6494 *
6495 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6496 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6497 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6498 */
6499 public void setImportantForAccessibility(int mode) {
6500 if (mode != getImportantForAccessibility()) {
6501 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6502 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6503 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6504 notifyAccessibilityStateChanged();
6505 }
6506 }
6507
6508 /**
6509 * Gets whether this view should be exposed for accessibility.
6510 *
6511 * @return Whether the view is exposed for accessibility.
6512 *
6513 * @hide
6514 */
6515 public boolean isImportantForAccessibility() {
6516 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6517 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6518 switch (mode) {
6519 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6520 return true;
6521 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6522 return false;
6523 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6524 return isActionableForAccessibility() || hasListenersForAccessibility();
6525 default:
6526 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6527 + mode);
6528 }
6529 }
6530
6531 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006532 * Gets the mode for determining whether this View can take accessibility focus.
6533 *
6534 * @return The mode for determining whether a View can take accessibility focus.
6535 *
6536 * @attr ref android.R.styleable#View_accessibilityFocusable
6537 *
6538 * @see #ACCESSIBILITY_FOCUSABLE_YES
6539 * @see #ACCESSIBILITY_FOCUSABLE_NO
6540 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6541 *
6542 * @hide
6543 */
6544 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6545 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_AUTO, to = "auto"),
6546 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_YES, to = "yes"),
6547 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_NO, to = "no")
6548 })
6549 public int getAccessibilityFocusable() {
6550 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK) >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6551 }
6552
6553 /**
6554 * Sets how to determine whether this view can take accessibility focus.
6555 *
6556 * @param mode How to determine whether this view can take accessibility focus.
6557 *
6558 * @attr ref android.R.styleable#View_accessibilityFocusable
6559 *
6560 * @see #ACCESSIBILITY_FOCUSABLE_YES
6561 * @see #ACCESSIBILITY_FOCUSABLE_NO
6562 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6563 *
6564 * @hide
6565 */
6566 public void setAccessibilityFocusable(int mode) {
6567 if (mode != getAccessibilityFocusable()) {
6568 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSABLE_MASK;
6569 mPrivateFlags2 |= (mode << ACCESSIBILITY_FOCUSABLE_SHIFT)
6570 & ACCESSIBILITY_FOCUSABLE_MASK;
6571 notifyAccessibilityStateChanged();
6572 }
6573 }
6574
6575 /**
6576 * Gets whether this view can take accessibility focus.
6577 *
6578 * @return Whether the view can take accessibility focus.
6579 *
6580 * @hide
6581 */
6582 public boolean isAccessibilityFocusable() {
6583 final int mode = (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK)
6584 >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6585 switch (mode) {
6586 case ACCESSIBILITY_FOCUSABLE_YES:
6587 return true;
6588 case ACCESSIBILITY_FOCUSABLE_NO:
6589 return false;
6590 case ACCESSIBILITY_FOCUSABLE_AUTO:
6591 return canTakeAccessibilityFocusFromHover()
6592 || getAccessibilityNodeProvider() != null;
6593 default:
6594 throw new IllegalArgumentException("Unknow accessibility focusable mode: " + mode);
6595 }
6596 }
6597
6598 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006599 * Gets the parent for accessibility purposes. Note that the parent for
6600 * accessibility is not necessary the immediate parent. It is the first
6601 * predecessor that is important for accessibility.
6602 *
6603 * @return The parent for accessibility purposes.
6604 */
6605 public ViewParent getParentForAccessibility() {
6606 if (mParent instanceof View) {
6607 View parentView = (View) mParent;
6608 if (parentView.includeForAccessibility()) {
6609 return mParent;
6610 } else {
6611 return mParent.getParentForAccessibility();
6612 }
6613 }
6614 return null;
6615 }
6616
6617 /**
6618 * Adds the children of a given View for accessibility. Since some Views are
6619 * not important for accessibility the children for accessibility are not
6620 * necessarily direct children of the riew, rather they are the first level of
6621 * descendants important for accessibility.
6622 *
6623 * @param children The list of children for accessibility.
6624 */
6625 public void addChildrenForAccessibility(ArrayList<View> children) {
6626 if (includeForAccessibility()) {
6627 children.add(this);
6628 }
6629 }
6630
6631 /**
6632 * Whether to regard this view for accessibility. A view is regarded for
6633 * accessibility if it is important for accessibility or the querying
6634 * accessibility service has explicitly requested that view not
6635 * important for accessibility are regarded.
6636 *
6637 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006638 *
6639 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006640 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006641 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006642 if (mAttachInfo != null) {
6643 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006644 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006645 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006646 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006647 }
6648 return false;
6649 }
6650
6651 /**
6652 * Returns whether the View is considered actionable from
6653 * accessibility perspective. Such view are important for
6654 * accessiiblity.
6655 *
6656 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006657 *
6658 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006659 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006660 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006661 return (isClickable() || isLongClickable() || isFocusable());
6662 }
6663
6664 /**
6665 * Returns whether the View has registered callbacks wich makes it
6666 * important for accessiiblity.
6667 *
6668 * @return True if the view is actionable for accessibility.
6669 */
6670 private boolean hasListenersForAccessibility() {
6671 ListenerInfo info = getListenerInfo();
6672 return mTouchDelegate != null || info.mOnKeyListener != null
6673 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6674 || info.mOnHoverListener != null || info.mOnDragListener != null;
6675 }
6676
6677 /**
6678 * Notifies accessibility services that some view's important for
6679 * accessibility state has changed. Note that such notifications
6680 * are made at most once every
6681 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6682 * to avoid unnecessary load to the system. Also once a view has
6683 * made a notifucation this method is a NOP until the notification has
6684 * been sent to clients.
6685 *
6686 * @hide
6687 *
6688 * TODO: Makse sure this method is called for any view state change
6689 * that is interesting for accessilility purposes.
6690 */
6691 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006692 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6693 return;
6694 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006695 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6696 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6697 if (mParent != null) {
6698 mParent.childAccessibilityStateChanged(this);
6699 }
6700 }
6701 }
6702
6703 /**
6704 * Reset the state indicating the this view has requested clients
6705 * interested in its accessiblity state to be notified.
6706 *
6707 * @hide
6708 */
6709 public void resetAccessibilityStateChanged() {
6710 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6711 }
6712
6713 /**
6714 * Performs the specified accessibility action on the view. For
6715 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006716 * <p>
6717 * If an {@link AccessibilityDelegate} has been specified via calling
6718 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6719 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6720 * is responsible for handling this call.
6721 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006722 *
6723 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006724 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006725 * @return Whether the action was performed.
6726 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006727 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006728 if (mAccessibilityDelegate != null) {
6729 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6730 } else {
6731 return performAccessibilityActionInternal(action, arguments);
6732 }
6733 }
6734
6735 /**
6736 * @see #performAccessibilityAction(int, Bundle)
6737 *
6738 * Note: Called from the default {@link AccessibilityDelegate}.
6739 */
6740 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006741 switch (action) {
6742 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006743 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006744 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006745 }
6746 } break;
6747 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6748 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006749 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006750 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006751 } break;
6752 case AccessibilityNodeInfo.ACTION_FOCUS: {
6753 if (!hasFocus()) {
6754 // Get out of touch mode since accessibility
6755 // wants to move focus around.
6756 getViewRootImpl().ensureTouchMode(false);
6757 return requestFocus();
6758 }
6759 } break;
6760 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6761 if (hasFocus()) {
6762 clearFocus();
6763 return !isFocused();
6764 }
6765 } break;
6766 case AccessibilityNodeInfo.ACTION_SELECT: {
6767 if (!isSelected()) {
6768 setSelected(true);
6769 return isSelected();
6770 }
6771 } break;
6772 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6773 if (isSelected()) {
6774 setSelected(false);
6775 return !isSelected();
6776 }
6777 } break;
6778 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006779 final int mode = getAccessibilityFocusable();
6780 if (!isAccessibilityFocused()
6781 && (mode == ACCESSIBILITY_FOCUSABLE_YES
6782 || mode == ACCESSIBILITY_FOCUSABLE_AUTO)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006783 return requestAccessibilityFocus();
6784 }
6785 } break;
6786 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6787 if (isAccessibilityFocused()) {
6788 clearAccessibilityFocus();
6789 return true;
6790 }
6791 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006792 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6793 if (arguments != null) {
6794 final int granularity = arguments.getInt(
6795 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6796 return nextAtGranularity(granularity);
6797 }
6798 } break;
6799 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6800 if (arguments != null) {
6801 final int granularity = arguments.getInt(
6802 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6803 return previousAtGranularity(granularity);
6804 }
6805 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006806 }
6807 return false;
6808 }
6809
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006810 private boolean nextAtGranularity(int granularity) {
6811 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006812 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006813 return false;
6814 }
6815 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6816 if (iterator == null) {
6817 return false;
6818 }
6819 final int current = getAccessibilityCursorPosition();
6820 final int[] range = iterator.following(current);
6821 if (range == null) {
6822 setAccessibilityCursorPosition(-1);
6823 return false;
6824 }
6825 final int start = range[0];
6826 final int end = range[1];
6827 setAccessibilityCursorPosition(start);
6828 sendViewTextTraversedAtGranularityEvent(
6829 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6830 granularity, start, end);
6831 return true;
6832 }
6833
6834 private boolean previousAtGranularity(int granularity) {
6835 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006836 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006837 return false;
6838 }
6839 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6840 if (iterator == null) {
6841 return false;
6842 }
6843 final int selectionStart = getAccessibilityCursorPosition();
6844 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6845 final int[] range = iterator.preceding(current);
6846 if (range == null) {
6847 setAccessibilityCursorPosition(-1);
6848 return false;
6849 }
6850 final int start = range[0];
6851 final int end = range[1];
6852 setAccessibilityCursorPosition(end);
6853 sendViewTextTraversedAtGranularityEvent(
6854 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6855 granularity, start, end);
6856 return true;
6857 }
6858
6859 /**
6860 * Gets the text reported for accessibility purposes.
6861 *
6862 * @return The accessibility text.
6863 *
6864 * @hide
6865 */
6866 public CharSequence getIterableTextForAccessibility() {
6867 return mContentDescription;
6868 }
6869
6870 /**
6871 * @hide
6872 */
6873 public int getAccessibilityCursorPosition() {
6874 return mAccessibilityCursorPosition;
6875 }
6876
6877 /**
6878 * @hide
6879 */
6880 public void setAccessibilityCursorPosition(int position) {
6881 mAccessibilityCursorPosition = position;
6882 }
6883
6884 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6885 int fromIndex, int toIndex) {
6886 if (mParent == null) {
6887 return;
6888 }
6889 AccessibilityEvent event = AccessibilityEvent.obtain(
6890 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6891 onInitializeAccessibilityEvent(event);
6892 onPopulateAccessibilityEvent(event);
6893 event.setFromIndex(fromIndex);
6894 event.setToIndex(toIndex);
6895 event.setAction(action);
6896 event.setMovementGranularity(granularity);
6897 mParent.requestSendAccessibilityEvent(this, event);
6898 }
6899
6900 /**
6901 * @hide
6902 */
6903 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6904 switch (granularity) {
6905 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6906 CharSequence text = getIterableTextForAccessibility();
6907 if (text != null && text.length() > 0) {
6908 CharacterTextSegmentIterator iterator =
6909 CharacterTextSegmentIterator.getInstance(mContext);
6910 iterator.initialize(text.toString());
6911 return iterator;
6912 }
6913 } break;
6914 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6915 CharSequence text = getIterableTextForAccessibility();
6916 if (text != null && text.length() > 0) {
6917 WordTextSegmentIterator iterator =
6918 WordTextSegmentIterator.getInstance(mContext);
6919 iterator.initialize(text.toString());
6920 return iterator;
6921 }
6922 } break;
6923 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6924 CharSequence text = getIterableTextForAccessibility();
6925 if (text != null && text.length() > 0) {
6926 ParagraphTextSegmentIterator iterator =
6927 ParagraphTextSegmentIterator.getInstance();
6928 iterator.initialize(text.toString());
6929 return iterator;
6930 }
6931 } break;
6932 }
6933 return null;
6934 }
6935
Svetoslav Ganov42138042012-03-20 11:51:39 -07006936 /**
Romain Guya440b002010-02-24 15:57:54 -08006937 * @hide
6938 */
6939 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006940 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006941 onStartTemporaryDetach();
6942 }
6943
6944 /**
6945 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6947 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006948 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 */
6950 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006951 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006952 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006953 }
6954
6955 /**
6956 * @hide
6957 */
6958 public void dispatchFinishTemporaryDetach() {
6959 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 }
Romain Guy8506ab42009-06-11 17:35:47 -07006961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 /**
6963 * Called after {@link #onStartTemporaryDetach} when the container is done
6964 * changing the view.
6965 */
6966 public void onFinishTemporaryDetach() {
6967 }
Romain Guy8506ab42009-06-11 17:35:47 -07006968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006970 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6971 * for this view's window. Returns null if the view is not currently attached
6972 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006973 * just use the standard high-level event callbacks like
6974 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006975 */
6976 public KeyEvent.DispatcherState getKeyDispatcherState() {
6977 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6978 }
Joe Malin32736f02011-01-19 16:14:20 -08006979
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 * Dispatch a key event before it is processed by any input method
6982 * associated with the view hierarchy. This can be used to intercept
6983 * key events in special situations before the IME consumes them; a
6984 * typical example would be handling the BACK key to update the application's
6985 * UI instead of allowing the IME to see it and close itself.
6986 *
6987 * @param event The key event to be dispatched.
6988 * @return True if the event was handled, false otherwise.
6989 */
6990 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6991 return onKeyPreIme(event.getKeyCode(), event);
6992 }
6993
6994 /**
6995 * Dispatch a key event to the next view on the focus path. This path runs
6996 * from the top of the view tree down to the currently focused view. If this
6997 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6998 * the next node down the focus path. This method also fires any key
6999 * listeners.
7000 *
7001 * @param event The key event to be dispatched.
7002 * @return True if the event was handled, false otherwise.
7003 */
7004 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007005 if (mInputEventConsistencyVerifier != null) {
7006 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008
Jeff Brown21bc5c92011-02-28 18:27:14 -08007009 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007010 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007011 ListenerInfo li = mListenerInfo;
7012 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7013 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 return true;
7015 }
7016
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007017 if (event.dispatch(this, mAttachInfo != null
7018 ? mAttachInfo.mKeyDispatchState : null, this)) {
7019 return true;
7020 }
7021
7022 if (mInputEventConsistencyVerifier != null) {
7023 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7024 }
7025 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 }
7027
7028 /**
7029 * Dispatches a key shortcut event.
7030 *
7031 * @param event The key event to be dispatched.
7032 * @return True if the event was handled by the view, false otherwise.
7033 */
7034 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7035 return onKeyShortcut(event.getKeyCode(), event);
7036 }
7037
7038 /**
7039 * Pass the touch screen motion event down to the target view, or this
7040 * view if it is the target.
7041 *
7042 * @param event The motion event to be dispatched.
7043 * @return True if the event was handled by the view, false otherwise.
7044 */
7045 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007046 if (mInputEventConsistencyVerifier != null) {
7047 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7048 }
7049
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007050 if (onFilterTouchEventForSecurity(event)) {
7051 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007052 ListenerInfo li = mListenerInfo;
7053 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7054 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007055 return true;
7056 }
7057
7058 if (onTouchEvent(event)) {
7059 return true;
7060 }
Jeff Brown85a31762010-09-01 17:01:00 -07007061 }
7062
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007063 if (mInputEventConsistencyVerifier != null) {
7064 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007065 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007066 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 }
7068
7069 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007070 * Filter the touch event to apply security policies.
7071 *
7072 * @param event The motion event to be filtered.
7073 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007074 *
Jeff Brown85a31762010-09-01 17:01:00 -07007075 * @see #getFilterTouchesWhenObscured
7076 */
7077 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007078 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007079 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7080 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7081 // Window is obscured, drop this touch.
7082 return false;
7083 }
7084 return true;
7085 }
7086
7087 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 * Pass a trackball motion event down to the focused view.
7089 *
7090 * @param event The motion event to be dispatched.
7091 * @return True if the event was handled by the view, false otherwise.
7092 */
7093 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007094 if (mInputEventConsistencyVerifier != null) {
7095 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7096 }
7097
Romain Guy02ccac62011-06-24 13:20:23 -07007098 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 }
7100
7101 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007102 * Dispatch a generic motion event.
7103 * <p>
7104 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7105 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007106 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007107 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007108 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007109 *
7110 * @param event The motion event to be dispatched.
7111 * @return True if the event was handled by the view, false otherwise.
7112 */
7113 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007114 if (mInputEventConsistencyVerifier != null) {
7115 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7116 }
7117
Jeff Browna032cc02011-03-07 16:56:21 -08007118 final int source = event.getSource();
7119 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7120 final int action = event.getAction();
7121 if (action == MotionEvent.ACTION_HOVER_ENTER
7122 || action == MotionEvent.ACTION_HOVER_MOVE
7123 || action == MotionEvent.ACTION_HOVER_EXIT) {
7124 if (dispatchHoverEvent(event)) {
7125 return true;
7126 }
7127 } else if (dispatchGenericPointerEvent(event)) {
7128 return true;
7129 }
7130 } else if (dispatchGenericFocusedEvent(event)) {
7131 return true;
7132 }
7133
Jeff Brown10b62902011-06-20 16:40:37 -07007134 if (dispatchGenericMotionEventInternal(event)) {
7135 return true;
7136 }
7137
7138 if (mInputEventConsistencyVerifier != null) {
7139 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7140 }
7141 return false;
7142 }
7143
7144 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007145 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007146 ListenerInfo li = mListenerInfo;
7147 if (li != null && li.mOnGenericMotionListener != null
7148 && (mViewFlags & ENABLED_MASK) == ENABLED
7149 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007150 return true;
7151 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007152
7153 if (onGenericMotionEvent(event)) {
7154 return true;
7155 }
7156
7157 if (mInputEventConsistencyVerifier != null) {
7158 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7159 }
7160 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007161 }
7162
7163 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007164 * Dispatch a hover event.
7165 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007166 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007167 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007168 * </p>
7169 *
7170 * @param event The motion event to be dispatched.
7171 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007172 */
7173 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007174 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007175 ListenerInfo li = mListenerInfo;
7176 if (li != null && li.mOnHoverListener != null
7177 && (mViewFlags & ENABLED_MASK) == ENABLED
7178 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007179 return true;
7180 }
7181
Jeff Browna032cc02011-03-07 16:56:21 -08007182 return onHoverEvent(event);
7183 }
7184
7185 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007186 * Returns true if the view has a child to which it has recently sent
7187 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7188 * it does not have a hovered child, then it must be the innermost hovered view.
7189 * @hide
7190 */
7191 protected boolean hasHoveredChild() {
7192 return false;
7193 }
7194
7195 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007196 * Dispatch a generic motion event to the view under the first pointer.
7197 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007198 * Do not call this method directly.
7199 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007200 * </p>
7201 *
7202 * @param event The motion event to be dispatched.
7203 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007204 */
7205 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7206 return false;
7207 }
7208
7209 /**
7210 * Dispatch a generic motion event to the currently focused view.
7211 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007212 * Do not call this method directly.
7213 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007214 * </p>
7215 *
7216 * @param event The motion event to be dispatched.
7217 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007218 */
7219 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7220 return false;
7221 }
7222
7223 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007224 * Dispatch a pointer event.
7225 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007226 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7227 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7228 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007229 * and should not be expected to handle other pointing device features.
7230 * </p>
7231 *
7232 * @param event The motion event to be dispatched.
7233 * @return True if the event was handled by the view, false otherwise.
7234 * @hide
7235 */
7236 public final boolean dispatchPointerEvent(MotionEvent event) {
7237 if (event.isTouchEvent()) {
7238 return dispatchTouchEvent(event);
7239 } else {
7240 return dispatchGenericMotionEvent(event);
7241 }
7242 }
7243
7244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 * Called when the window containing this view gains or loses window focus.
7246 * ViewGroups should override to route to their children.
7247 *
7248 * @param hasFocus True if the window containing this view now has focus,
7249 * false otherwise.
7250 */
7251 public void dispatchWindowFocusChanged(boolean hasFocus) {
7252 onWindowFocusChanged(hasFocus);
7253 }
7254
7255 /**
7256 * Called when the window containing this view gains or loses focus. Note
7257 * that this is separate from view focus: to receive key events, both
7258 * your view and its window must have focus. If a window is displayed
7259 * on top of yours that takes input focus, then your own window will lose
7260 * focus but the view focus will remain unchanged.
7261 *
7262 * @param hasWindowFocus True if the window containing this view now has
7263 * focus, false otherwise.
7264 */
7265 public void onWindowFocusChanged(boolean hasWindowFocus) {
7266 InputMethodManager imm = InputMethodManager.peekInstance();
7267 if (!hasWindowFocus) {
7268 if (isPressed()) {
7269 setPressed(false);
7270 }
7271 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7272 imm.focusOut(this);
7273 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007274 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007275 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007276 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7278 imm.focusIn(this);
7279 }
7280 refreshDrawableState();
7281 }
7282
7283 /**
7284 * Returns true if this view is in a window that currently has window focus.
7285 * Note that this is not the same as the view itself having focus.
7286 *
7287 * @return True if this view is in a window that currently has window focus.
7288 */
7289 public boolean hasWindowFocus() {
7290 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7291 }
7292
7293 /**
Adam Powell326d8082009-12-09 15:10:07 -08007294 * Dispatch a view visibility change down the view hierarchy.
7295 * ViewGroups should override to route to their children.
7296 * @param changedView The view whose visibility changed. Could be 'this' or
7297 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007298 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7299 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007300 */
7301 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7302 onVisibilityChanged(changedView, visibility);
7303 }
7304
7305 /**
7306 * Called when the visibility of the view or an ancestor of the view is changed.
7307 * @param changedView The view whose visibility changed. Could be 'this' or
7308 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007309 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7310 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007311 */
7312 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007313 if (visibility == VISIBLE) {
7314 if (mAttachInfo != null) {
7315 initialAwakenScrollBars();
7316 } else {
7317 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7318 }
7319 }
Adam Powell326d8082009-12-09 15:10:07 -08007320 }
7321
7322 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007323 * Dispatch a hint about whether this view is displayed. For instance, when
7324 * a View moves out of the screen, it might receives a display hint indicating
7325 * the view is not displayed. Applications should not <em>rely</em> on this hint
7326 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007327 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007328 * @param hint A hint about whether or not this view is displayed:
7329 * {@link #VISIBLE} or {@link #INVISIBLE}.
7330 */
7331 public void dispatchDisplayHint(int hint) {
7332 onDisplayHint(hint);
7333 }
7334
7335 /**
7336 * Gives this view a hint about whether is displayed or not. For instance, when
7337 * a View moves out of the screen, it might receives a display hint indicating
7338 * the view is not displayed. Applications should not <em>rely</em> on this hint
7339 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007340 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007341 * @param hint A hint about whether or not this view is displayed:
7342 * {@link #VISIBLE} or {@link #INVISIBLE}.
7343 */
7344 protected void onDisplayHint(int hint) {
7345 }
7346
7347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007348 * Dispatch a window visibility change down the view hierarchy.
7349 * ViewGroups should override to route to their children.
7350 *
7351 * @param visibility The new visibility of the window.
7352 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007353 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 */
7355 public void dispatchWindowVisibilityChanged(int visibility) {
7356 onWindowVisibilityChanged(visibility);
7357 }
7358
7359 /**
7360 * Called when the window containing has change its visibility
7361 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7362 * that this tells you whether or not your window is being made visible
7363 * to the window manager; this does <em>not</em> tell you whether or not
7364 * your window is obscured by other windows on the screen, even if it
7365 * is itself visible.
7366 *
7367 * @param visibility The new visibility of the window.
7368 */
7369 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007370 if (visibility == VISIBLE) {
7371 initialAwakenScrollBars();
7372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 }
7374
7375 /**
7376 * Returns the current visibility of the window this view is attached to
7377 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7378 *
7379 * @return Returns the current visibility of the view's window.
7380 */
7381 public int getWindowVisibility() {
7382 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7383 }
7384
7385 /**
7386 * Retrieve the overall visible display size in which the window this view is
7387 * attached to has been positioned in. This takes into account screen
7388 * decorations above the window, for both cases where the window itself
7389 * is being position inside of them or the window is being placed under
7390 * then and covered insets are used for the window to position its content
7391 * inside. In effect, this tells you the available area where content can
7392 * be placed and remain visible to users.
7393 *
7394 * <p>This function requires an IPC back to the window manager to retrieve
7395 * the requested information, so should not be used in performance critical
7396 * code like drawing.
7397 *
7398 * @param outRect Filled in with the visible display frame. If the view
7399 * is not attached to a window, this is simply the raw display size.
7400 */
7401 public void getWindowVisibleDisplayFrame(Rect outRect) {
7402 if (mAttachInfo != null) {
7403 try {
7404 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7405 } catch (RemoteException e) {
7406 return;
7407 }
7408 // XXX This is really broken, and probably all needs to be done
7409 // in the window manager, and we need to know more about whether
7410 // we want the area behind or in front of the IME.
7411 final Rect insets = mAttachInfo.mVisibleInsets;
7412 outRect.left += insets.left;
7413 outRect.top += insets.top;
7414 outRect.right -= insets.right;
7415 outRect.bottom -= insets.bottom;
7416 return;
7417 }
7418 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007419 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 }
7421
7422 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007423 * Dispatch a notification about a resource configuration change down
7424 * the view hierarchy.
7425 * ViewGroups should override to route to their children.
7426 *
7427 * @param newConfig The new resource configuration.
7428 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007429 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007430 */
7431 public void dispatchConfigurationChanged(Configuration newConfig) {
7432 onConfigurationChanged(newConfig);
7433 }
7434
7435 /**
7436 * Called when the current configuration of the resources being used
7437 * by the application have changed. You can use this to decide when
7438 * to reload resources that can changed based on orientation and other
7439 * configuration characterstics. You only need to use this if you are
7440 * not relying on the normal {@link android.app.Activity} mechanism of
7441 * recreating the activity instance upon a configuration change.
7442 *
7443 * @param newConfig The new resource configuration.
7444 */
7445 protected void onConfigurationChanged(Configuration newConfig) {
7446 }
7447
7448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007449 * Private function to aggregate all per-view attributes in to the view
7450 * root.
7451 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007452 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7453 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 }
7455
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007456 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7457 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007458 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007459 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007460 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007461 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007462 ListenerInfo li = mListenerInfo;
7463 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007464 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 }
7467 }
7468
7469 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007470 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007472 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7473 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 ai.mRecomputeGlobalAttributes = true;
7475 }
7476 }
7477 }
7478
7479 /**
7480 * Returns whether the device is currently in touch mode. Touch mode is entered
7481 * once the user begins interacting with the device by touch, and affects various
7482 * things like whether focus is always visible to the user.
7483 *
7484 * @return Whether the device is in touch mode.
7485 */
7486 @ViewDebug.ExportedProperty
7487 public boolean isInTouchMode() {
7488 if (mAttachInfo != null) {
7489 return mAttachInfo.mInTouchMode;
7490 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007491 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 }
7493 }
7494
7495 /**
7496 * Returns the context the view is running in, through which it can
7497 * access the current theme, resources, etc.
7498 *
7499 * @return The view's Context.
7500 */
7501 @ViewDebug.CapturedViewProperty
7502 public final Context getContext() {
7503 return mContext;
7504 }
7505
7506 /**
7507 * Handle a key event before it is processed by any input method
7508 * associated with the view hierarchy. This can be used to intercept
7509 * key events in special situations before the IME consumes them; a
7510 * typical example would be handling the BACK key to update the application's
7511 * UI instead of allowing the IME to see it and close itself.
7512 *
7513 * @param keyCode The value in event.getKeyCode().
7514 * @param event Description of the key event.
7515 * @return If you handled the event, return true. If you want to allow the
7516 * event to be handled by the next receiver, return false.
7517 */
7518 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7519 return false;
7520 }
7521
7522 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007523 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7524 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7526 * is released, if the view is enabled and clickable.
7527 *
7528 * @param keyCode A key code that represents the button pressed, from
7529 * {@link android.view.KeyEvent}.
7530 * @param event The KeyEvent object that defines the button action.
7531 */
7532 public boolean onKeyDown(int keyCode, KeyEvent event) {
7533 boolean result = false;
7534
7535 switch (keyCode) {
7536 case KeyEvent.KEYCODE_DPAD_CENTER:
7537 case KeyEvent.KEYCODE_ENTER: {
7538 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7539 return true;
7540 }
7541 // Long clickable items don't necessarily have to be clickable
7542 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7543 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7544 (event.getRepeatCount() == 0)) {
7545 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007546 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 return true;
7548 }
7549 break;
7550 }
7551 }
7552 return result;
7553 }
7554
7555 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007556 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7557 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7558 * the event).
7559 */
7560 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7561 return false;
7562 }
7563
7564 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007565 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7566 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7568 * {@link KeyEvent#KEYCODE_ENTER} is released.
7569 *
7570 * @param keyCode A key code that represents the button pressed, from
7571 * {@link android.view.KeyEvent}.
7572 * @param event The KeyEvent object that defines the button action.
7573 */
7574 public boolean onKeyUp(int keyCode, KeyEvent event) {
7575 boolean result = false;
7576
7577 switch (keyCode) {
7578 case KeyEvent.KEYCODE_DPAD_CENTER:
7579 case KeyEvent.KEYCODE_ENTER: {
7580 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7581 return true;
7582 }
7583 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7584 setPressed(false);
7585
7586 if (!mHasPerformedLongPress) {
7587 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007588 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589
7590 result = performClick();
7591 }
7592 }
7593 break;
7594 }
7595 }
7596 return result;
7597 }
7598
7599 /**
7600 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7601 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7602 * the event).
7603 *
7604 * @param keyCode A key code that represents the button pressed, from
7605 * {@link android.view.KeyEvent}.
7606 * @param repeatCount The number of times the action was made.
7607 * @param event The KeyEvent object that defines the button action.
7608 */
7609 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7610 return false;
7611 }
7612
7613 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007614 * Called on the focused view when a key shortcut event is not handled.
7615 * Override this method to implement local key shortcuts for the View.
7616 * Key shortcuts can also be implemented by setting the
7617 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 *
7619 * @param keyCode The value in event.getKeyCode().
7620 * @param event Description of the key event.
7621 * @return If you handled the event, return true. If you want to allow the
7622 * event to be handled by the next receiver, return false.
7623 */
7624 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7625 return false;
7626 }
7627
7628 /**
7629 * Check whether the called view is a text editor, in which case it
7630 * would make sense to automatically display a soft input window for
7631 * it. Subclasses should override this if they implement
7632 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007633 * a call on that method would return a non-null InputConnection, and
7634 * they are really a first-class editor that the user would normally
7635 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007636 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007637 * <p>The default implementation always returns false. This does
7638 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7639 * will not be called or the user can not otherwise perform edits on your
7640 * view; it is just a hint to the system that this is not the primary
7641 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007643 * @return Returns true if this view is a text editor, else false.
7644 */
7645 public boolean onCheckIsTextEditor() {
7646 return false;
7647 }
Romain Guy8506ab42009-06-11 17:35:47 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 /**
7650 * Create a new InputConnection for an InputMethod to interact
7651 * with the view. The default implementation returns null, since it doesn't
7652 * support input methods. You can override this to implement such support.
7653 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007654 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 * <p>When implementing this, you probably also want to implement
7656 * {@link #onCheckIsTextEditor()} to indicate you will return a
7657 * non-null InputConnection.
7658 *
7659 * @param outAttrs Fill in with attribute information about the connection.
7660 */
7661 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7662 return null;
7663 }
7664
7665 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007666 * Called by the {@link android.view.inputmethod.InputMethodManager}
7667 * when a view who is not the current
7668 * input connection target is trying to make a call on the manager. The
7669 * default implementation returns false; you can override this to return
7670 * true for certain views if you are performing InputConnection proxying
7671 * to them.
7672 * @param view The View that is making the InputMethodManager call.
7673 * @return Return true to allow the call, false to reject.
7674 */
7675 public boolean checkInputConnectionProxy(View view) {
7676 return false;
7677 }
Romain Guy8506ab42009-06-11 17:35:47 -07007678
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 * Show the context menu for this view. It is not safe to hold on to the
7681 * menu after returning from this method.
7682 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007683 * You should normally not overload this method. Overload
7684 * {@link #onCreateContextMenu(ContextMenu)} or define an
7685 * {@link OnCreateContextMenuListener} to add items to the context menu.
7686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 * @param menu The context menu to populate
7688 */
7689 public void createContextMenu(ContextMenu menu) {
7690 ContextMenuInfo menuInfo = getContextMenuInfo();
7691
7692 // Sets the current menu info so all items added to menu will have
7693 // my extra info set.
7694 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7695
7696 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007697 ListenerInfo li = mListenerInfo;
7698 if (li != null && li.mOnCreateContextMenuListener != null) {
7699 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700 }
7701
7702 // Clear the extra information so subsequent items that aren't mine don't
7703 // have my extra info.
7704 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7705
7706 if (mParent != null) {
7707 mParent.createContextMenu(menu);
7708 }
7709 }
7710
7711 /**
7712 * Views should implement this if they have extra information to associate
7713 * with the context menu. The return result is supplied as a parameter to
7714 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7715 * callback.
7716 *
7717 * @return Extra information about the item for which the context menu
7718 * should be shown. This information will vary across different
7719 * subclasses of View.
7720 */
7721 protected ContextMenuInfo getContextMenuInfo() {
7722 return null;
7723 }
7724
7725 /**
7726 * Views should implement this if the view itself is going to add items to
7727 * the context menu.
7728 *
7729 * @param menu the context menu to populate
7730 */
7731 protected void onCreateContextMenu(ContextMenu menu) {
7732 }
7733
7734 /**
7735 * Implement this method to handle trackball motion events. The
7736 * <em>relative</em> movement of the trackball since the last event
7737 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7738 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7739 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7740 * they will often be fractional values, representing the more fine-grained
7741 * movement information available from a trackball).
7742 *
7743 * @param event The motion event.
7744 * @return True if the event was handled, false otherwise.
7745 */
7746 public boolean onTrackballEvent(MotionEvent event) {
7747 return false;
7748 }
7749
7750 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007751 * Implement this method to handle generic motion events.
7752 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007753 * Generic motion events describe joystick movements, mouse hovers, track pad
7754 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007755 * {@link MotionEvent#getSource() source} of the motion event specifies
7756 * the class of input that was received. Implementations of this method
7757 * must examine the bits in the source before processing the event.
7758 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007759 * </p><p>
7760 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7761 * are delivered to the view under the pointer. All other generic motion events are
7762 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007763 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007764 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007765 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007766 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7767 * // process the joystick movement...
7768 * return true;
7769 * }
7770 * }
7771 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7772 * switch (event.getAction()) {
7773 * case MotionEvent.ACTION_HOVER_MOVE:
7774 * // process the mouse hover movement...
7775 * return true;
7776 * case MotionEvent.ACTION_SCROLL:
7777 * // process the scroll wheel movement...
7778 * return true;
7779 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007780 * }
7781 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007782 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007783 *
7784 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007785 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007786 */
7787 public boolean onGenericMotionEvent(MotionEvent event) {
7788 return false;
7789 }
7790
7791 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007792 * Implement this method to handle hover events.
7793 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007794 * This method is called whenever a pointer is hovering into, over, or out of the
7795 * bounds of a view and the view is not currently being touched.
7796 * Hover events are represented as pointer events with action
7797 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7798 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7799 * </p>
7800 * <ul>
7801 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7802 * when the pointer enters the bounds of the view.</li>
7803 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7804 * when the pointer has already entered the bounds of the view and has moved.</li>
7805 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7806 * when the pointer has exited the bounds of the view or when the pointer is
7807 * about to go down due to a button click, tap, or similar user action that
7808 * causes the view to be touched.</li>
7809 * </ul>
7810 * <p>
7811 * The view should implement this method to return true to indicate that it is
7812 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007813 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007814 * The default implementation calls {@link #setHovered} to update the hovered state
7815 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007816 * is enabled and is clickable. The default implementation also sends hover
7817 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007818 * </p>
7819 *
7820 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007821 * @return True if the view handled the hover event.
7822 *
7823 * @see #isHovered
7824 * @see #setHovered
7825 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007826 */
7827 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007828 // The root view may receive hover (or touch) events that are outside the bounds of
7829 // the window. This code ensures that we only send accessibility events for
7830 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007831 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007832 if (!mSendingHoverAccessibilityEvents) {
7833 if ((action == MotionEvent.ACTION_HOVER_ENTER
7834 || action == MotionEvent.ACTION_HOVER_MOVE)
7835 && !hasHoveredChild()
7836 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007837 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007838 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007839 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007840 }
7841 } else {
7842 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007843 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007844 && !pointInView(event.getX(), event.getY()))) {
7845 mSendingHoverAccessibilityEvents = false;
7846 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007847 // If the window does not have input focus we take away accessibility
7848 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007849 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007850 getViewRootImpl().setAccessibilityFocusedHost(null);
7851 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007852 }
Jeff Browna1b24182011-07-28 13:38:24 -07007853 }
7854
Jeff Brown87b7f802011-06-21 18:35:45 -07007855 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007856 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007857 case MotionEvent.ACTION_HOVER_ENTER:
7858 setHovered(true);
7859 break;
7860 case MotionEvent.ACTION_HOVER_EXIT:
7861 setHovered(false);
7862 break;
7863 }
Jeff Browna1b24182011-07-28 13:38:24 -07007864
7865 // Dispatch the event to onGenericMotionEvent before returning true.
7866 // This is to provide compatibility with existing applications that
7867 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7868 // break because of the new default handling for hoverable views
7869 // in onHoverEvent.
7870 // Note that onGenericMotionEvent will be called by default when
7871 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7872 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007873 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007874 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007875
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007876 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007877 }
7878
7879 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007880 * Returns true if the view should handle {@link #onHoverEvent}
7881 * by calling {@link #setHovered} to change its hovered state.
7882 *
7883 * @return True if the view is hoverable.
7884 */
7885 private boolean isHoverable() {
7886 final int viewFlags = mViewFlags;
7887 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7888 return false;
7889 }
7890
7891 return (viewFlags & CLICKABLE) == CLICKABLE
7892 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7893 }
7894
7895 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007896 * Returns true if the view is currently hovered.
7897 *
7898 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007899 *
7900 * @see #setHovered
7901 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007902 */
Jeff Brown10b62902011-06-20 16:40:37 -07007903 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007904 public boolean isHovered() {
7905 return (mPrivateFlags & HOVERED) != 0;
7906 }
7907
7908 /**
7909 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007910 * <p>
7911 * Calling this method also changes the drawable state of the view. This
7912 * enables the view to react to hover by using different drawable resources
7913 * to change its appearance.
7914 * </p><p>
7915 * The {@link #onHoverChanged} method is called when the hovered state changes.
7916 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007917 *
7918 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007919 *
7920 * @see #isHovered
7921 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007922 */
7923 public void setHovered(boolean hovered) {
7924 if (hovered) {
7925 if ((mPrivateFlags & HOVERED) == 0) {
7926 mPrivateFlags |= HOVERED;
7927 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007928 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007929 }
7930 } else {
7931 if ((mPrivateFlags & HOVERED) != 0) {
7932 mPrivateFlags &= ~HOVERED;
7933 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007934 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007935 }
7936 }
7937 }
7938
7939 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007940 * Implement this method to handle hover state changes.
7941 * <p>
7942 * This method is called whenever the hover state changes as a result of a
7943 * call to {@link #setHovered}.
7944 * </p>
7945 *
7946 * @param hovered The current hover state, as returned by {@link #isHovered}.
7947 *
7948 * @see #isHovered
7949 * @see #setHovered
7950 */
7951 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007952 }
7953
7954 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955 * Implement this method to handle touch screen motion events.
7956 *
7957 * @param event The motion event.
7958 * @return True if the event was handled, false otherwise.
7959 */
7960 public boolean onTouchEvent(MotionEvent event) {
7961 final int viewFlags = mViewFlags;
7962
7963 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007964 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007965 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007967 // A disabled view that is clickable still consumes the touch
7968 // events, it just doesn't respond to them.
7969 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7970 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7971 }
7972
7973 if (mTouchDelegate != null) {
7974 if (mTouchDelegate.onTouchEvent(event)) {
7975 return true;
7976 }
7977 }
7978
7979 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7980 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7981 switch (event.getAction()) {
7982 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007983 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7984 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 // take focus if we don't have it already and we should in
7986 // touch mode.
7987 boolean focusTaken = false;
7988 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7989 focusTaken = requestFocus();
7990 }
7991
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007992 if (prepressed) {
7993 // The button is being released before we actually
7994 // showed it as pressed. Make it show the pressed
7995 // state now (before scheduling the click) to ensure
7996 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007997 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007998 }
Joe Malin32736f02011-01-19 16:14:20 -08007999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 if (!mHasPerformedLongPress) {
8001 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008002 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008003
8004 // Only perform take click actions if we were in the pressed state
8005 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008006 // Use a Runnable and post this rather than calling
8007 // performClick directly. This lets other visual state
8008 // of the view update before click actions start.
8009 if (mPerformClick == null) {
8010 mPerformClick = new PerformClick();
8011 }
8012 if (!post(mPerformClick)) {
8013 performClick();
8014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 }
8016 }
8017
8018 if (mUnsetPressedState == null) {
8019 mUnsetPressedState = new UnsetPressedState();
8020 }
8021
Adam Powelle14579b2009-12-16 18:39:52 -08008022 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008023 postDelayed(mUnsetPressedState,
8024 ViewConfiguration.getPressedStateDuration());
8025 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 // If the post failed, unpress right now
8027 mUnsetPressedState.run();
8028 }
Adam Powelle14579b2009-12-16 18:39:52 -08008029 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 }
8031 break;
8032
8033 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008034 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008035
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008036 if (performButtonActionOnTouchDown(event)) {
8037 break;
8038 }
8039
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008040 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008041 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008042
8043 // For views inside a scrolling container, delay the pressed feedback for
8044 // a short period in case this is a scroll.
8045 if (isInScrollingContainer) {
8046 mPrivateFlags |= PREPRESSED;
8047 if (mPendingCheckForTap == null) {
8048 mPendingCheckForTap = new CheckForTap();
8049 }
8050 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8051 } else {
8052 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008053 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008054 checkForLongClick(0);
8055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 break;
8057
8058 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008059 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008060 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 break;
8062
8063 case MotionEvent.ACTION_MOVE:
8064 final int x = (int) event.getX();
8065 final int y = (int) event.getY();
8066
8067 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008068 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008070 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008072 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008073 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074
Adam Powell4d6f0662012-02-21 15:11:11 -08008075 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 }
8078 break;
8079 }
8080 return true;
8081 }
8082
8083 return false;
8084 }
8085
8086 /**
Adam Powell10298662011-08-14 18:26:30 -07008087 * @hide
8088 */
8089 public boolean isInScrollingContainer() {
8090 ViewParent p = getParent();
8091 while (p != null && p instanceof ViewGroup) {
8092 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8093 return true;
8094 }
8095 p = p.getParent();
8096 }
8097 return false;
8098 }
8099
8100 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008101 * Remove the longpress detection timer.
8102 */
8103 private void removeLongPressCallback() {
8104 if (mPendingCheckForLongPress != null) {
8105 removeCallbacks(mPendingCheckForLongPress);
8106 }
8107 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008108
8109 /**
8110 * Remove the pending click action
8111 */
8112 private void removePerformClickCallback() {
8113 if (mPerformClick != null) {
8114 removeCallbacks(mPerformClick);
8115 }
8116 }
8117
Adam Powelle14579b2009-12-16 18:39:52 -08008118 /**
Romain Guya440b002010-02-24 15:57:54 -08008119 * Remove the prepress detection timer.
8120 */
8121 private void removeUnsetPressCallback() {
8122 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8123 setPressed(false);
8124 removeCallbacks(mUnsetPressedState);
8125 }
8126 }
8127
8128 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008129 * Remove the tap detection timer.
8130 */
8131 private void removeTapCallback() {
8132 if (mPendingCheckForTap != null) {
8133 mPrivateFlags &= ~PREPRESSED;
8134 removeCallbacks(mPendingCheckForTap);
8135 }
8136 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008137
8138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 * Cancels a pending long press. Your subclass can use this if you
8140 * want the context menu to come up if the user presses and holds
8141 * at the same place, but you don't want it to come up if they press
8142 * and then move around enough to cause scrolling.
8143 */
8144 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008145 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008146
8147 /*
8148 * The prepressed state handled by the tap callback is a display
8149 * construct, but the tap callback will post a long press callback
8150 * less its own timeout. Remove it here.
8151 */
8152 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153 }
8154
8155 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008156 * Remove the pending callback for sending a
8157 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8158 */
8159 private void removeSendViewScrolledAccessibilityEventCallback() {
8160 if (mSendViewScrolledAccessibilityEvent != null) {
8161 removeCallbacks(mSendViewScrolledAccessibilityEvent);
8162 }
8163 }
8164
8165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 * Sets the TouchDelegate for this View.
8167 */
8168 public void setTouchDelegate(TouchDelegate delegate) {
8169 mTouchDelegate = delegate;
8170 }
8171
8172 /**
8173 * Gets the TouchDelegate for this View.
8174 */
8175 public TouchDelegate getTouchDelegate() {
8176 return mTouchDelegate;
8177 }
8178
8179 /**
8180 * Set flags controlling behavior of this view.
8181 *
8182 * @param flags Constant indicating the value which should be set
8183 * @param mask Constant indicating the bit range that should be changed
8184 */
8185 void setFlags(int flags, int mask) {
8186 int old = mViewFlags;
8187 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8188
8189 int changed = mViewFlags ^ old;
8190 if (changed == 0) {
8191 return;
8192 }
8193 int privateFlags = mPrivateFlags;
8194
8195 /* Check if the FOCUSABLE bit has changed */
8196 if (((changed & FOCUSABLE_MASK) != 0) &&
8197 ((privateFlags & HAS_BOUNDS) !=0)) {
8198 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8199 && ((privateFlags & FOCUSED) != 0)) {
8200 /* Give up focus if we are no longer focusable */
8201 clearFocus();
8202 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8203 && ((privateFlags & FOCUSED) == 0)) {
8204 /*
8205 * Tell the view system that we are now available to take focus
8206 * if no one else already has it.
8207 */
8208 if (mParent != null) mParent.focusableViewAvailable(this);
8209 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008210 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8211 notifyAccessibilityStateChanged();
8212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 }
8214
8215 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8216 if ((changed & VISIBILITY_MASK) != 0) {
8217 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008218 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008219 * it was not visible. Marking it drawn ensures that the invalidation will
8220 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008222 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008223 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224
8225 needGlobalAttributesUpdate(true);
8226
8227 // a view becoming visible is worth notifying the parent
8228 // about in case nothing has focus. even if this specific view
8229 // isn't focusable, it may contain something that is, so let
8230 // the root view try to give this focus if nothing else does.
8231 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8232 mParent.focusableViewAvailable(this);
8233 }
8234 }
8235 }
8236
8237 /* Check if the GONE bit has changed */
8238 if ((changed & GONE) != 0) {
8239 needGlobalAttributesUpdate(false);
8240 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241
Romain Guyecd80ee2009-12-03 17:13:02 -08008242 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8243 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008244 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008245 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008246 if (mParent instanceof View) {
8247 // GONE views noop invalidation, so invalidate the parent
8248 ((View) mParent).invalidate(true);
8249 }
8250 // Mark the view drawn to ensure that it gets invalidated properly the next
8251 // time it is visible and gets invalidated
8252 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 }
8254 if (mAttachInfo != null) {
8255 mAttachInfo.mViewVisibilityChanged = true;
8256 }
8257 }
8258
8259 /* Check if the VISIBLE bit has changed */
8260 if ((changed & INVISIBLE) != 0) {
8261 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008262 /*
8263 * If this view is becoming invisible, set the DRAWN flag so that
8264 * the next invalidate() will not be skipped.
8265 */
8266 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267
8268 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008269 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 if (getRootView() != this) {
8271 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008272 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 }
8274 }
8275 if (mAttachInfo != null) {
8276 mAttachInfo.mViewVisibilityChanged = true;
8277 }
8278 }
8279
Adam Powell326d8082009-12-09 15:10:07 -08008280 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008281 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008282 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8283 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008284 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008285 } else if (mParent != null) {
8286 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008287 }
Adam Powell326d8082009-12-09 15:10:07 -08008288 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8289 }
8290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8292 destroyDrawingCache();
8293 }
8294
8295 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8296 destroyDrawingCache();
8297 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008298 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299 }
8300
8301 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8302 destroyDrawingCache();
8303 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8304 }
8305
8306 if ((changed & DRAW_MASK) != 0) {
8307 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008308 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 mPrivateFlags &= ~SKIP_DRAW;
8310 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8311 } else {
8312 mPrivateFlags |= SKIP_DRAW;
8313 }
8314 } else {
8315 mPrivateFlags &= ~SKIP_DRAW;
8316 }
8317 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008318 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008319 }
8320
8321 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008322 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 mParent.recomputeViewAttributes(this);
8324 }
8325 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008326
8327 if (AccessibilityManager.getInstance(mContext).isEnabled()
8328 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8329 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8330 notifyAccessibilityStateChanged();
8331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 }
8333
8334 /**
8335 * Change the view's z order in the tree, so it's on top of other sibling
8336 * views
8337 */
8338 public void bringToFront() {
8339 if (mParent != null) {
8340 mParent.bringChildToFront(this);
8341 }
8342 }
8343
8344 /**
8345 * This is called in response to an internal scroll in this view (i.e., the
8346 * view scrolled its own contents). This is typically as a result of
8347 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8348 * called.
8349 *
8350 * @param l Current horizontal scroll origin.
8351 * @param t Current vertical scroll origin.
8352 * @param oldl Previous horizontal scroll origin.
8353 * @param oldt Previous vertical scroll origin.
8354 */
8355 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008356 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8357 postSendViewScrolledAccessibilityEventCallback();
8358 }
8359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 mBackgroundSizeChanged = true;
8361
8362 final AttachInfo ai = mAttachInfo;
8363 if (ai != null) {
8364 ai.mViewScrollChanged = true;
8365 }
8366 }
8367
8368 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008369 * Interface definition for a callback to be invoked when the layout bounds of a view
8370 * changes due to layout processing.
8371 */
8372 public interface OnLayoutChangeListener {
8373 /**
8374 * Called when the focus state of a view has changed.
8375 *
8376 * @param v The view whose state has changed.
8377 * @param left The new value of the view's left property.
8378 * @param top The new value of the view's top property.
8379 * @param right The new value of the view's right property.
8380 * @param bottom The new value of the view's bottom property.
8381 * @param oldLeft The previous value of the view's left property.
8382 * @param oldTop The previous value of the view's top property.
8383 * @param oldRight The previous value of the view's right property.
8384 * @param oldBottom The previous value of the view's bottom property.
8385 */
8386 void onLayoutChange(View v, int left, int top, int right, int bottom,
8387 int oldLeft, int oldTop, int oldRight, int oldBottom);
8388 }
8389
8390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 * This is called during layout when the size of this view has changed. If
8392 * you were just added to the view hierarchy, you're called with the old
8393 * values of 0.
8394 *
8395 * @param w Current width of this view.
8396 * @param h Current height of this view.
8397 * @param oldw Old width of this view.
8398 * @param oldh Old height of this view.
8399 */
8400 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8401 }
8402
8403 /**
8404 * Called by draw to draw the child views. This may be overridden
8405 * by derived classes to gain control just before its children are drawn
8406 * (but after its own view has been drawn).
8407 * @param canvas the canvas on which to draw the view
8408 */
8409 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 }
8412
8413 /**
8414 * Gets the parent of this view. Note that the parent is a
8415 * ViewParent and not necessarily a View.
8416 *
8417 * @return Parent of this view.
8418 */
8419 public final ViewParent getParent() {
8420 return mParent;
8421 }
8422
8423 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008424 * Set the horizontal scrolled position of your view. This will cause a call to
8425 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8426 * invalidated.
8427 * @param value the x position to scroll to
8428 */
8429 public void setScrollX(int value) {
8430 scrollTo(value, mScrollY);
8431 }
8432
8433 /**
8434 * Set the vertical scrolled position of your view. This will cause a call to
8435 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8436 * invalidated.
8437 * @param value the y position to scroll to
8438 */
8439 public void setScrollY(int value) {
8440 scrollTo(mScrollX, value);
8441 }
8442
8443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 * Return the scrolled left position of this view. This is the left edge of
8445 * the displayed part of your view. You do not need to draw any pixels
8446 * farther left, since those are outside of the frame of your view on
8447 * screen.
8448 *
8449 * @return The left edge of the displayed part of your view, in pixels.
8450 */
8451 public final int getScrollX() {
8452 return mScrollX;
8453 }
8454
8455 /**
8456 * Return the scrolled top position of this view. This is the top edge of
8457 * the displayed part of your view. You do not need to draw any pixels above
8458 * it, since those are outside of the frame of your view on screen.
8459 *
8460 * @return The top edge of the displayed part of your view, in pixels.
8461 */
8462 public final int getScrollY() {
8463 return mScrollY;
8464 }
8465
8466 /**
8467 * Return the width of the your view.
8468 *
8469 * @return The width of your view, in pixels.
8470 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008471 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 public final int getWidth() {
8473 return mRight - mLeft;
8474 }
8475
8476 /**
8477 * Return the height of your view.
8478 *
8479 * @return The height of your view, in pixels.
8480 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008481 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008482 public final int getHeight() {
8483 return mBottom - mTop;
8484 }
8485
8486 /**
8487 * Return the visible drawing bounds of your view. Fills in the output
8488 * rectangle with the values from getScrollX(), getScrollY(),
8489 * getWidth(), and getHeight().
8490 *
8491 * @param outRect The (scrolled) drawing bounds of the view.
8492 */
8493 public void getDrawingRect(Rect outRect) {
8494 outRect.left = mScrollX;
8495 outRect.top = mScrollY;
8496 outRect.right = mScrollX + (mRight - mLeft);
8497 outRect.bottom = mScrollY + (mBottom - mTop);
8498 }
8499
8500 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008501 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8502 * raw width component (that is the result is masked by
8503 * {@link #MEASURED_SIZE_MASK}).
8504 *
8505 * @return The raw measured width of this view.
8506 */
8507 public final int getMeasuredWidth() {
8508 return mMeasuredWidth & MEASURED_SIZE_MASK;
8509 }
8510
8511 /**
8512 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008513 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008514 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 * This should be used during measurement and layout calculations only. Use
8516 * {@link #getWidth()} to see how wide a view is after layout.
8517 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008518 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008520 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 return mMeasuredWidth;
8522 }
8523
8524 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008525 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8526 * raw width component (that is the result is masked by
8527 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008529 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 */
8531 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008532 return mMeasuredHeight & MEASURED_SIZE_MASK;
8533 }
8534
8535 /**
8536 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008537 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008538 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8539 * This should be used during measurement and layout calculations only. Use
8540 * {@link #getHeight()} to see how wide a view is after layout.
8541 *
8542 * @return The measured width of this view as a bit mask.
8543 */
8544 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008545 return mMeasuredHeight;
8546 }
8547
8548 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008549 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8550 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8551 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8552 * and the height component is at the shifted bits
8553 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8554 */
8555 public final int getMeasuredState() {
8556 return (mMeasuredWidth&MEASURED_STATE_MASK)
8557 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8558 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8559 }
8560
8561 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008562 * The transform matrix of this view, which is calculated based on the current
8563 * roation, scale, and pivot properties.
8564 *
8565 * @see #getRotation()
8566 * @see #getScaleX()
8567 * @see #getScaleY()
8568 * @see #getPivotX()
8569 * @see #getPivotY()
8570 * @return The current transform matrix for the view
8571 */
8572 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008573 if (mTransformationInfo != null) {
8574 updateMatrix();
8575 return mTransformationInfo.mMatrix;
8576 }
8577 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008578 }
8579
8580 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008581 * Utility function to determine if the value is far enough away from zero to be
8582 * considered non-zero.
8583 * @param value A floating point value to check for zero-ness
8584 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8585 */
8586 private static boolean nonzero(float value) {
8587 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8588 }
8589
8590 /**
Jeff Brown86671742010-09-30 20:00:15 -07008591 * Returns true if the transform matrix is the identity matrix.
8592 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008593 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008594 * @return True if the transform matrix is the identity matrix, false otherwise.
8595 */
Jeff Brown86671742010-09-30 20:00:15 -07008596 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008597 if (mTransformationInfo != null) {
8598 updateMatrix();
8599 return mTransformationInfo.mMatrixIsIdentity;
8600 }
8601 return true;
8602 }
8603
8604 void ensureTransformationInfo() {
8605 if (mTransformationInfo == null) {
8606 mTransformationInfo = new TransformationInfo();
8607 }
Jeff Brown86671742010-09-30 20:00:15 -07008608 }
8609
8610 /**
8611 * Recomputes the transform matrix if necessary.
8612 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008613 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008614 final TransformationInfo info = mTransformationInfo;
8615 if (info == null) {
8616 return;
8617 }
8618 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008619 // transform-related properties have changed since the last time someone
8620 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008621
8622 // Figure out if we need to update the pivot point
8623 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008624 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8625 info.mPrevWidth = mRight - mLeft;
8626 info.mPrevHeight = mBottom - mTop;
8627 info.mPivotX = info.mPrevWidth / 2f;
8628 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008629 }
8630 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008631 info.mMatrix.reset();
8632 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8633 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8634 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8635 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008636 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008637 if (info.mCamera == null) {
8638 info.mCamera = new Camera();
8639 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008640 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008641 info.mCamera.save();
8642 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8643 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8644 info.mCamera.getMatrix(info.matrix3D);
8645 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8646 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8647 info.mPivotY + info.mTranslationY);
8648 info.mMatrix.postConcat(info.matrix3D);
8649 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008650 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008651 info.mMatrixDirty = false;
8652 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8653 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008654 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008655 }
8656
8657 /**
8658 * Utility method to retrieve the inverse of the current mMatrix property.
8659 * We cache the matrix to avoid recalculating it when transform properties
8660 * have not changed.
8661 *
8662 * @return The inverse of the current matrix of this view.
8663 */
Jeff Brown86671742010-09-30 20:00:15 -07008664 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008665 final TransformationInfo info = mTransformationInfo;
8666 if (info != null) {
8667 updateMatrix();
8668 if (info.mInverseMatrixDirty) {
8669 if (info.mInverseMatrix == null) {
8670 info.mInverseMatrix = new Matrix();
8671 }
8672 info.mMatrix.invert(info.mInverseMatrix);
8673 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008674 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008675 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008676 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008677 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008678 }
8679
8680 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008681 * Gets the distance along the Z axis from the camera to this view.
8682 *
8683 * @see #setCameraDistance(float)
8684 *
8685 * @return The distance along the Z axis.
8686 */
8687 public float getCameraDistance() {
8688 ensureTransformationInfo();
8689 final float dpi = mResources.getDisplayMetrics().densityDpi;
8690 final TransformationInfo info = mTransformationInfo;
8691 if (info.mCamera == null) {
8692 info.mCamera = new Camera();
8693 info.matrix3D = new Matrix();
8694 }
8695 return -(info.mCamera.getLocationZ() * dpi);
8696 }
8697
8698 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008699 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8700 * views are drawn) from the camera to this view. The camera's distance
8701 * affects 3D transformations, for instance rotations around the X and Y
8702 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008703 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008704 * use a camera distance that's greater than the height (X axis rotation) or
8705 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008706 *
Romain Guya5364ee2011-02-24 14:46:04 -08008707 * <p>The distance of the camera from the view plane can have an affect on the
8708 * perspective distortion of the view when it is rotated around the x or y axis.
8709 * For example, a large distance will result in a large viewing angle, and there
8710 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008711 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008712 * also result in some drawing artifacts if the rotated view ends up partially
8713 * behind the camera (which is why the recommendation is to use a distance at
8714 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008715 *
Romain Guya5364ee2011-02-24 14:46:04 -08008716 * <p>The distance is expressed in "depth pixels." The default distance depends
8717 * on the screen density. For instance, on a medium density display, the
8718 * default distance is 1280. On a high density display, the default distance
8719 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008720 *
Romain Guya5364ee2011-02-24 14:46:04 -08008721 * <p>If you want to specify a distance that leads to visually consistent
8722 * results across various densities, use the following formula:</p>
8723 * <pre>
8724 * float scale = context.getResources().getDisplayMetrics().density;
8725 * view.setCameraDistance(distance * scale);
8726 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008727 *
Romain Guya5364ee2011-02-24 14:46:04 -08008728 * <p>The density scale factor of a high density display is 1.5,
8729 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008730 *
Romain Guya5364ee2011-02-24 14:46:04 -08008731 * @param distance The distance in "depth pixels", if negative the opposite
8732 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008733 *
8734 * @see #setRotationX(float)
8735 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008736 */
8737 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008738 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008739
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008740 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008741 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008742 final TransformationInfo info = mTransformationInfo;
8743 if (info.mCamera == null) {
8744 info.mCamera = new Camera();
8745 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008746 }
8747
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008748 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8749 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008750
Chet Haase9d1992d2012-03-13 11:03:25 -07008751 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008752 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008753 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008754 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008755 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8756 // View was rejected last time it was drawn by its parent; this may have changed
8757 invalidateParentIfNeeded();
8758 }
Romain Guya5364ee2011-02-24 14:46:04 -08008759 }
8760
8761 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008762 * The degrees that the view is rotated around the pivot point.
8763 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008764 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008765 * @see #getPivotX()
8766 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008767 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008768 * @return The degrees of rotation.
8769 */
Chet Haasea5531132012-02-02 13:41:44 -08008770 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008771 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008772 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008773 }
8774
8775 /**
Chet Haase897247b2010-09-09 14:54:47 -07008776 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8777 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008778 *
8779 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008780 *
8781 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008782 * @see #getPivotX()
8783 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008784 * @see #setRotationX(float)
8785 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008786 *
8787 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008788 */
8789 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008790 ensureTransformationInfo();
8791 final TransformationInfo info = mTransformationInfo;
8792 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008793 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008794 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008795 info.mRotation = rotation;
8796 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008797 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008798 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008799 mDisplayList.setRotation(rotation);
8800 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008801 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8802 // View was rejected last time it was drawn by its parent; this may have changed
8803 invalidateParentIfNeeded();
8804 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008805 }
8806 }
8807
8808 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008809 * The degrees that the view is rotated around the vertical axis through the pivot point.
8810 *
8811 * @see #getPivotX()
8812 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008813 * @see #setRotationY(float)
8814 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008815 * @return The degrees of Y rotation.
8816 */
Chet Haasea5531132012-02-02 13:41:44 -08008817 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008818 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008819 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008820 }
8821
8822 /**
Chet Haase897247b2010-09-09 14:54:47 -07008823 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8824 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8825 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008826 *
Romain Guya5364ee2011-02-24 14:46:04 -08008827 * When rotating large views, it is recommended to adjust the camera distance
8828 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008829 *
8830 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008831 *
8832 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008833 * @see #getPivotX()
8834 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008835 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008836 * @see #setRotationX(float)
8837 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008838 *
8839 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008840 */
8841 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 ensureTransformationInfo();
8843 final TransformationInfo info = mTransformationInfo;
8844 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008845 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008846 info.mRotationY = rotationY;
8847 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008848 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008849 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008850 mDisplayList.setRotationY(rotationY);
8851 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008852 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8853 // View was rejected last time it was drawn by its parent; this may have changed
8854 invalidateParentIfNeeded();
8855 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008856 }
8857 }
8858
8859 /**
8860 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8861 *
8862 * @see #getPivotX()
8863 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008864 * @see #setRotationX(float)
8865 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008866 * @return The degrees of X rotation.
8867 */
Chet Haasea5531132012-02-02 13:41:44 -08008868 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008869 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008870 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008871 }
8872
8873 /**
Chet Haase897247b2010-09-09 14:54:47 -07008874 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8875 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8876 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008877 *
Romain Guya5364ee2011-02-24 14:46:04 -08008878 * When rotating large views, it is recommended to adjust the camera distance
8879 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008880 *
8881 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008882 *
8883 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008884 * @see #getPivotX()
8885 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008886 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008887 * @see #setRotationY(float)
8888 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008889 *
8890 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008891 */
8892 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008893 ensureTransformationInfo();
8894 final TransformationInfo info = mTransformationInfo;
8895 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008896 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 info.mRotationX = rotationX;
8898 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008899 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008900 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008901 mDisplayList.setRotationX(rotationX);
8902 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008903 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8904 // View was rejected last time it was drawn by its parent; this may have changed
8905 invalidateParentIfNeeded();
8906 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008907 }
8908 }
8909
8910 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008911 * The amount that the view is scaled in x around the pivot point, as a proportion of
8912 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8913 *
Joe Onorato93162322010-09-16 15:42:01 -04008914 * <p>By default, this is 1.0f.
8915 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008916 * @see #getPivotX()
8917 * @see #getPivotY()
8918 * @return The scaling factor.
8919 */
Chet Haasea5531132012-02-02 13:41:44 -08008920 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008921 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008922 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008923 }
8924
8925 /**
8926 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8927 * the view's unscaled width. A value of 1 means that no scaling is applied.
8928 *
8929 * @param scaleX The scaling factor.
8930 * @see #getPivotX()
8931 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008932 *
8933 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008934 */
8935 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008936 ensureTransformationInfo();
8937 final TransformationInfo info = mTransformationInfo;
8938 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008939 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008940 info.mScaleX = scaleX;
8941 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008942 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008943 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008944 mDisplayList.setScaleX(scaleX);
8945 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008946 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8947 // View was rejected last time it was drawn by its parent; this may have changed
8948 invalidateParentIfNeeded();
8949 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 }
8951 }
8952
8953 /**
8954 * The amount that the view is scaled in y around the pivot point, as a proportion of
8955 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8956 *
Joe Onorato93162322010-09-16 15:42:01 -04008957 * <p>By default, this is 1.0f.
8958 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008959 * @see #getPivotX()
8960 * @see #getPivotY()
8961 * @return The scaling factor.
8962 */
Chet Haasea5531132012-02-02 13:41:44 -08008963 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008964 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008965 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 }
8967
8968 /**
8969 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8970 * the view's unscaled width. A value of 1 means that no scaling is applied.
8971 *
8972 * @param scaleY The scaling factor.
8973 * @see #getPivotX()
8974 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008975 *
8976 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008977 */
8978 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008979 ensureTransformationInfo();
8980 final TransformationInfo info = mTransformationInfo;
8981 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008982 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008983 info.mScaleY = scaleY;
8984 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008985 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008986 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008987 mDisplayList.setScaleY(scaleY);
8988 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008989 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8990 // View was rejected last time it was drawn by its parent; this may have changed
8991 invalidateParentIfNeeded();
8992 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008993 }
8994 }
8995
8996 /**
8997 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8998 * and {@link #setScaleX(float) scaled}.
8999 *
9000 * @see #getRotation()
9001 * @see #getScaleX()
9002 * @see #getScaleY()
9003 * @see #getPivotY()
9004 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009005 *
9006 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009007 */
Chet Haasea5531132012-02-02 13:41:44 -08009008 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009009 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009010 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009011 }
9012
9013 /**
9014 * Sets the x location of the point around which the view is
9015 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009016 * By default, the pivot point is centered on the object.
9017 * Setting this property disables this behavior and causes the view to use only the
9018 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009019 *
9020 * @param pivotX The x location of the pivot point.
9021 * @see #getRotation()
9022 * @see #getScaleX()
9023 * @see #getScaleY()
9024 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009025 *
9026 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009027 */
9028 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009029 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009030 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009031 final TransformationInfo info = mTransformationInfo;
9032 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009033 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009034 info.mPivotX = pivotX;
9035 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009036 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009037 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009038 mDisplayList.setPivotX(pivotX);
9039 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009040 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9041 // View was rejected last time it was drawn by its parent; this may have changed
9042 invalidateParentIfNeeded();
9043 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009044 }
9045 }
9046
9047 /**
9048 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9049 * and {@link #setScaleY(float) scaled}.
9050 *
9051 * @see #getRotation()
9052 * @see #getScaleX()
9053 * @see #getScaleY()
9054 * @see #getPivotY()
9055 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009056 *
9057 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009058 */
Chet Haasea5531132012-02-02 13:41:44 -08009059 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009060 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009061 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009062 }
9063
9064 /**
9065 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009066 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9067 * Setting this property disables this behavior and causes the view to use only the
9068 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009069 *
9070 * @param pivotY The y location of the pivot point.
9071 * @see #getRotation()
9072 * @see #getScaleX()
9073 * @see #getScaleY()
9074 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009075 *
9076 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009077 */
9078 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009079 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009080 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009081 final TransformationInfo info = mTransformationInfo;
9082 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009083 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009084 info.mPivotY = pivotY;
9085 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009086 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009087 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009088 mDisplayList.setPivotY(pivotY);
9089 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009090 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9091 // View was rejected last time it was drawn by its parent; this may have changed
9092 invalidateParentIfNeeded();
9093 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009094 }
9095 }
9096
9097 /**
9098 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9099 * completely transparent and 1 means the view is completely opaque.
9100 *
Joe Onorato93162322010-09-16 15:42:01 -04009101 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009102 * @return The opacity of the view.
9103 */
Chet Haasea5531132012-02-02 13:41:44 -08009104 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009105 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009106 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009107 }
9108
9109 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009110 * Returns whether this View has content which overlaps. This function, intended to be
9111 * overridden by specific View types, is an optimization when alpha is set on a view. If
9112 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9113 * and then composited it into place, which can be expensive. If the view has no overlapping
9114 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9115 * An example of overlapping rendering is a TextView with a background image, such as a
9116 * Button. An example of non-overlapping rendering is a TextView with no background, or
9117 * an ImageView with only the foreground image. The default implementation returns true;
9118 * subclasses should override if they have cases which can be optimized.
9119 *
9120 * @return true if the content in this view might overlap, false otherwise.
9121 */
9122 public boolean hasOverlappingRendering() {
9123 return true;
9124 }
9125
9126 /**
Romain Guy171c5922011-01-06 10:04:23 -08009127 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9128 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009129 *
Romain Guy171c5922011-01-06 10:04:23 -08009130 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9131 * responsible for applying the opacity itself. Otherwise, calling this method is
9132 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009133 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009134 *
Chet Haasea5531132012-02-02 13:41:44 -08009135 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9136 * performance implications. It is generally best to use the alpha property sparingly and
9137 * transiently, as in the case of fading animations.</p>
9138 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009139 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009140 *
Joe Malin32736f02011-01-19 16:14:20 -08009141 * @see #setLayerType(int, android.graphics.Paint)
9142 *
Chet Haase73066682010-11-29 15:55:32 -08009143 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009144 */
9145 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009146 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009147 if (mTransformationInfo.mAlpha != alpha) {
9148 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009149 if (onSetAlpha((int) (alpha * 255))) {
9150 mPrivateFlags |= ALPHA_SET;
9151 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009152 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009153 invalidate(true);
9154 } else {
9155 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009156 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009157 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009158 mDisplayList.setAlpha(alpha);
9159 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009160 }
Chet Haaseed032702010-10-01 14:05:54 -07009161 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009162 }
9163
9164 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009165 * Faster version of setAlpha() which performs the same steps except there are
9166 * no calls to invalidate(). The caller of this function should perform proper invalidation
9167 * on the parent and this object. The return value indicates whether the subclass handles
9168 * alpha (the return value for onSetAlpha()).
9169 *
9170 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009171 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9172 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009173 */
9174 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009175 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009176 if (mTransformationInfo.mAlpha != alpha) {
9177 mTransformationInfo.mAlpha = alpha;
9178 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9179 if (subclassHandlesAlpha) {
9180 mPrivateFlags |= ALPHA_SET;
9181 return true;
9182 } else {
9183 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009184 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009185 mDisplayList.setAlpha(alpha);
9186 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009187 }
Chet Haasea00f3862011-02-22 06:34:40 -08009188 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009189 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009190 }
9191
9192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193 * Top position of this view relative to its parent.
9194 *
9195 * @return The top of this view, in pixels.
9196 */
9197 @ViewDebug.CapturedViewProperty
9198 public final int getTop() {
9199 return mTop;
9200 }
9201
9202 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009203 * Sets the top position of this view relative to its parent. This method is meant to be called
9204 * by the layout system and should not generally be called otherwise, because the property
9205 * may be changed at any time by the layout.
9206 *
9207 * @param top The top of this view, in pixels.
9208 */
9209 public final void setTop(int top) {
9210 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009211 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009212 final boolean matrixIsIdentity = mTransformationInfo == null
9213 || mTransformationInfo.mMatrixIsIdentity;
9214 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009215 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009216 int minTop;
9217 int yLoc;
9218 if (top < mTop) {
9219 minTop = top;
9220 yLoc = top - mTop;
9221 } else {
9222 minTop = mTop;
9223 yLoc = 0;
9224 }
Chet Haasee9140a72011-02-16 16:23:29 -08009225 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009226 }
9227 } else {
9228 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009229 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009230 }
9231
Chet Haaseed032702010-10-01 14:05:54 -07009232 int width = mRight - mLeft;
9233 int oldHeight = mBottom - mTop;
9234
Chet Haase21cd1382010-09-01 17:42:29 -07009235 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009236 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009237 mDisplayList.setTop(mTop);
9238 }
Chet Haase21cd1382010-09-01 17:42:29 -07009239
Chet Haaseed032702010-10-01 14:05:54 -07009240 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9241
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009242 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009243 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9244 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009245 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009246 }
Chet Haase21cd1382010-09-01 17:42:29 -07009247 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009248 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009249 }
Chet Haase55dbb652010-12-21 20:15:08 -08009250 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009251 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009252 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9253 // View was rejected last time it was drawn by its parent; this may have changed
9254 invalidateParentIfNeeded();
9255 }
Chet Haase21cd1382010-09-01 17:42:29 -07009256 }
9257 }
9258
9259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 * Bottom position of this view relative to its parent.
9261 *
9262 * @return The bottom of this view, in pixels.
9263 */
9264 @ViewDebug.CapturedViewProperty
9265 public final int getBottom() {
9266 return mBottom;
9267 }
9268
9269 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009270 * True if this view has changed since the last time being drawn.
9271 *
9272 * @return The dirty state of this view.
9273 */
9274 public boolean isDirty() {
9275 return (mPrivateFlags & DIRTY_MASK) != 0;
9276 }
9277
9278 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009279 * Sets the bottom position of this view relative to its parent. This method is meant to be
9280 * called by the layout system and should not generally be called otherwise, because the
9281 * property may be changed at any time by the layout.
9282 *
9283 * @param bottom The bottom of this view, in pixels.
9284 */
9285 public final void setBottom(int bottom) {
9286 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009287 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009288 final boolean matrixIsIdentity = mTransformationInfo == null
9289 || mTransformationInfo.mMatrixIsIdentity;
9290 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009291 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009292 int maxBottom;
9293 if (bottom < mBottom) {
9294 maxBottom = mBottom;
9295 } else {
9296 maxBottom = bottom;
9297 }
Chet Haasee9140a72011-02-16 16:23:29 -08009298 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009299 }
9300 } else {
9301 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009302 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009303 }
9304
Chet Haaseed032702010-10-01 14:05:54 -07009305 int width = mRight - mLeft;
9306 int oldHeight = mBottom - mTop;
9307
Chet Haase21cd1382010-09-01 17:42:29 -07009308 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009309 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009310 mDisplayList.setBottom(mBottom);
9311 }
Chet Haase21cd1382010-09-01 17:42:29 -07009312
Chet Haaseed032702010-10-01 14:05:54 -07009313 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9314
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009315 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009316 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9317 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009318 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009319 }
Chet Haase21cd1382010-09-01 17:42:29 -07009320 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009321 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009322 }
Chet Haase55dbb652010-12-21 20:15:08 -08009323 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009324 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009325 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9326 // View was rejected last time it was drawn by its parent; this may have changed
9327 invalidateParentIfNeeded();
9328 }
Chet Haase21cd1382010-09-01 17:42:29 -07009329 }
9330 }
9331
9332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 * Left position of this view relative to its parent.
9334 *
9335 * @return The left edge of this view, in pixels.
9336 */
9337 @ViewDebug.CapturedViewProperty
9338 public final int getLeft() {
9339 return mLeft;
9340 }
9341
9342 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009343 * Sets the left position of this view relative to its parent. This method is meant to be called
9344 * by the layout system and should not generally be called otherwise, because the property
9345 * may be changed at any time by the layout.
9346 *
9347 * @param left The bottom of this view, in pixels.
9348 */
9349 public final void setLeft(int left) {
9350 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009351 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009352 final boolean matrixIsIdentity = mTransformationInfo == null
9353 || mTransformationInfo.mMatrixIsIdentity;
9354 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009355 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009356 int minLeft;
9357 int xLoc;
9358 if (left < mLeft) {
9359 minLeft = left;
9360 xLoc = left - mLeft;
9361 } else {
9362 minLeft = mLeft;
9363 xLoc = 0;
9364 }
Chet Haasee9140a72011-02-16 16:23:29 -08009365 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009366 }
9367 } else {
9368 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009369 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009370 }
9371
Chet Haaseed032702010-10-01 14:05:54 -07009372 int oldWidth = mRight - mLeft;
9373 int height = mBottom - mTop;
9374
Chet Haase21cd1382010-09-01 17:42:29 -07009375 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009376 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009377 mDisplayList.setLeft(left);
9378 }
Chet Haase21cd1382010-09-01 17:42:29 -07009379
Chet Haaseed032702010-10-01 14:05:54 -07009380 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9381
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009382 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009383 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9384 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009385 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009386 }
Chet Haase21cd1382010-09-01 17:42:29 -07009387 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009388 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009389 }
Chet Haase55dbb652010-12-21 20:15:08 -08009390 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009391 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009392 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9393 // View was rejected last time it was drawn by its parent; this may have changed
9394 invalidateParentIfNeeded();
9395 }
Chet Haase21cd1382010-09-01 17:42:29 -07009396 }
9397 }
9398
9399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009400 * Right position of this view relative to its parent.
9401 *
9402 * @return The right edge of this view, in pixels.
9403 */
9404 @ViewDebug.CapturedViewProperty
9405 public final int getRight() {
9406 return mRight;
9407 }
9408
9409 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009410 * Sets the right position of this view relative to its parent. This method is meant to be called
9411 * by the layout system and should not generally be called otherwise, because the property
9412 * may be changed at any time by the layout.
9413 *
9414 * @param right The bottom of this view, in pixels.
9415 */
9416 public final void setRight(int right) {
9417 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009418 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009419 final boolean matrixIsIdentity = mTransformationInfo == null
9420 || mTransformationInfo.mMatrixIsIdentity;
9421 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009422 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009423 int maxRight;
9424 if (right < mRight) {
9425 maxRight = mRight;
9426 } else {
9427 maxRight = right;
9428 }
Chet Haasee9140a72011-02-16 16:23:29 -08009429 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009430 }
9431 } else {
9432 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009433 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009434 }
9435
Chet Haaseed032702010-10-01 14:05:54 -07009436 int oldWidth = mRight - mLeft;
9437 int height = mBottom - mTop;
9438
Chet Haase21cd1382010-09-01 17:42:29 -07009439 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009440 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009441 mDisplayList.setRight(mRight);
9442 }
Chet Haase21cd1382010-09-01 17:42:29 -07009443
Chet Haaseed032702010-10-01 14:05:54 -07009444 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9445
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009446 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009447 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9448 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009449 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009450 }
Chet Haase21cd1382010-09-01 17:42:29 -07009451 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009452 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009453 }
Chet Haase55dbb652010-12-21 20:15:08 -08009454 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009455 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009456 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9457 // View was rejected last time it was drawn by its parent; this may have changed
9458 invalidateParentIfNeeded();
9459 }
Chet Haase21cd1382010-09-01 17:42:29 -07009460 }
9461 }
9462
9463 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009464 * The visual x position of this view, in pixels. This is equivalent to the
9465 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009466 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009467 *
Chet Haasedf030d22010-07-30 17:22:38 -07009468 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009469 */
Chet Haasea5531132012-02-02 13:41:44 -08009470 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009471 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009472 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009473 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009474
Chet Haasedf030d22010-07-30 17:22:38 -07009475 /**
9476 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9477 * {@link #setTranslationX(float) translationX} property to be the difference between
9478 * the x value passed in and the current {@link #getLeft() left} property.
9479 *
9480 * @param x The visual x position of this view, in pixels.
9481 */
9482 public void setX(float x) {
9483 setTranslationX(x - mLeft);
9484 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009485
Chet Haasedf030d22010-07-30 17:22:38 -07009486 /**
9487 * The visual y position of this view, in pixels. This is equivalent to the
9488 * {@link #setTranslationY(float) translationY} property plus the current
9489 * {@link #getTop() top} property.
9490 *
9491 * @return The visual y position of this view, in pixels.
9492 */
Chet Haasea5531132012-02-02 13:41:44 -08009493 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009494 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009495 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009496 }
9497
9498 /**
9499 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9500 * {@link #setTranslationY(float) translationY} property to be the difference between
9501 * the y value passed in and the current {@link #getTop() top} property.
9502 *
9503 * @param y The visual y position of this view, in pixels.
9504 */
9505 public void setY(float y) {
9506 setTranslationY(y - mTop);
9507 }
9508
9509
9510 /**
9511 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9512 * This position is post-layout, in addition to wherever the object's
9513 * layout placed it.
9514 *
9515 * @return The horizontal position of this view relative to its left position, in pixels.
9516 */
Chet Haasea5531132012-02-02 13:41:44 -08009517 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009518 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009519 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009520 }
9521
9522 /**
9523 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9524 * This effectively positions the object post-layout, in addition to wherever the object's
9525 * layout placed it.
9526 *
9527 * @param translationX The horizontal position of this view relative to its left position,
9528 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009529 *
9530 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009531 */
9532 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009533 ensureTransformationInfo();
9534 final TransformationInfo info = mTransformationInfo;
9535 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009536 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009537 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009538 info.mTranslationX = translationX;
9539 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009540 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009541 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009542 mDisplayList.setTranslationX(translationX);
9543 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009544 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9545 // View was rejected last time it was drawn by its parent; this may have changed
9546 invalidateParentIfNeeded();
9547 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009548 }
9549 }
9550
9551 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009552 * The horizontal location of this view relative to its {@link #getTop() top} position.
9553 * This position is post-layout, in addition to wherever the object's
9554 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009555 *
Chet Haasedf030d22010-07-30 17:22:38 -07009556 * @return The vertical position of this view relative to its top position,
9557 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009558 */
Chet Haasea5531132012-02-02 13:41:44 -08009559 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009560 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009561 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009562 }
9563
9564 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009565 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9566 * This effectively positions the object post-layout, in addition to wherever the object's
9567 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009568 *
Chet Haasedf030d22010-07-30 17:22:38 -07009569 * @param translationY The vertical position of this view relative to its top position,
9570 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009571 *
9572 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009573 */
Chet Haasedf030d22010-07-30 17:22:38 -07009574 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009575 ensureTransformationInfo();
9576 final TransformationInfo info = mTransformationInfo;
9577 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009578 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009579 info.mTranslationY = translationY;
9580 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009581 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009582 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009583 mDisplayList.setTranslationY(translationY);
9584 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009585 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9586 // View was rejected last time it was drawn by its parent; this may have changed
9587 invalidateParentIfNeeded();
9588 }
Chet Haasedf030d22010-07-30 17:22:38 -07009589 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009590 }
9591
9592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009593 * Hit rectangle in parent's coordinates
9594 *
9595 * @param outRect The hit rectangle of the view.
9596 */
9597 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009598 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009599 final TransformationInfo info = mTransformationInfo;
9600 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009601 outRect.set(mLeft, mTop, mRight, mBottom);
9602 } else {
9603 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009604 tmpRect.set(-info.mPivotX, -info.mPivotY,
9605 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9606 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009607 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9608 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009609 }
9610 }
9611
9612 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009613 * Determines whether the given point, in local coordinates is inside the view.
9614 */
9615 /*package*/ final boolean pointInView(float localX, float localY) {
9616 return localX >= 0 && localX < (mRight - mLeft)
9617 && localY >= 0 && localY < (mBottom - mTop);
9618 }
9619
9620 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009621 * Utility method to determine whether the given point, in local coordinates,
9622 * is inside the view, where the area of the view is expanded by the slop factor.
9623 * This method is called while processing touch-move events to determine if the event
9624 * is still within the view.
9625 */
9626 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009627 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009628 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 }
9630
9631 /**
9632 * When a view has focus and the user navigates away from it, the next view is searched for
9633 * starting from the rectangle filled in by this method.
9634 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009635 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9636 * of the view. However, if your view maintains some idea of internal selection,
9637 * such as a cursor, or a selected row or column, you should override this method and
9638 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 *
9640 * @param r The rectangle to fill in, in this view's coordinates.
9641 */
9642 public void getFocusedRect(Rect r) {
9643 getDrawingRect(r);
9644 }
9645
9646 /**
9647 * If some part of this view is not clipped by any of its parents, then
9648 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009649 * coordinates (without taking possible View rotations into account), offset
9650 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9651 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009652 *
9653 * @param r If true is returned, r holds the global coordinates of the
9654 * visible portion of this view.
9655 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9656 * between this view and its root. globalOffet may be null.
9657 * @return true if r is non-empty (i.e. part of the view is visible at the
9658 * root level.
9659 */
9660 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9661 int width = mRight - mLeft;
9662 int height = mBottom - mTop;
9663 if (width > 0 && height > 0) {
9664 r.set(0, 0, width, height);
9665 if (globalOffset != null) {
9666 globalOffset.set(-mScrollX, -mScrollY);
9667 }
9668 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9669 }
9670 return false;
9671 }
9672
9673 public final boolean getGlobalVisibleRect(Rect r) {
9674 return getGlobalVisibleRect(r, null);
9675 }
9676
9677 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009678 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 if (getGlobalVisibleRect(r, offset)) {
9680 r.offset(-offset.x, -offset.y); // make r local
9681 return true;
9682 }
9683 return false;
9684 }
9685
9686 /**
9687 * Offset this view's vertical location by the specified number of pixels.
9688 *
9689 * @param offset the number of pixels to offset the view by
9690 */
9691 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009692 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009693 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009694 final boolean matrixIsIdentity = mTransformationInfo == null
9695 || mTransformationInfo.mMatrixIsIdentity;
9696 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009697 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009698 invalidateViewProperty(false, false);
9699 } else {
9700 final ViewParent p = mParent;
9701 if (p != null && mAttachInfo != null) {
9702 final Rect r = mAttachInfo.mTmpInvalRect;
9703 int minTop;
9704 int maxBottom;
9705 int yLoc;
9706 if (offset < 0) {
9707 minTop = mTop + offset;
9708 maxBottom = mBottom;
9709 yLoc = offset;
9710 } else {
9711 minTop = mTop;
9712 maxBottom = mBottom + offset;
9713 yLoc = 0;
9714 }
9715 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9716 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009717 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009718 }
9719 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009720 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009721 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009722
Chet Haasec3aa3612010-06-17 08:50:37 -07009723 mTop += offset;
9724 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009725 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009726 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009727 invalidateViewProperty(false, false);
9728 } else {
9729 if (!matrixIsIdentity) {
9730 invalidateViewProperty(false, true);
9731 }
9732 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009733 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009735 }
9736
9737 /**
9738 * Offset this view's horizontal location by the specified amount of pixels.
9739 *
9740 * @param offset the numer of pixels to offset the view by
9741 */
9742 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009743 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009744 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009745 final boolean matrixIsIdentity = mTransformationInfo == null
9746 || mTransformationInfo.mMatrixIsIdentity;
9747 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009748 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009749 invalidateViewProperty(false, false);
9750 } else {
9751 final ViewParent p = mParent;
9752 if (p != null && mAttachInfo != null) {
9753 final Rect r = mAttachInfo.mTmpInvalRect;
9754 int minLeft;
9755 int maxRight;
9756 if (offset < 0) {
9757 minLeft = mLeft + offset;
9758 maxRight = mRight;
9759 } else {
9760 minLeft = mLeft;
9761 maxRight = mRight + offset;
9762 }
9763 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9764 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009765 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009766 }
9767 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009768 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009769 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009770
Chet Haasec3aa3612010-06-17 08:50:37 -07009771 mLeft += offset;
9772 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009773 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009774 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009775 invalidateViewProperty(false, false);
9776 } else {
9777 if (!matrixIsIdentity) {
9778 invalidateViewProperty(false, true);
9779 }
9780 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009781 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 }
9784
9785 /**
9786 * Get the LayoutParams associated with this view. All views should have
9787 * layout parameters. These supply parameters to the <i>parent</i> of this
9788 * view specifying how it should be arranged. There are many subclasses of
9789 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9790 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009791 *
9792 * This method may return null if this View is not attached to a parent
9793 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9794 * was not invoked successfully. When a View is attached to a parent
9795 * ViewGroup, this method must not return null.
9796 *
9797 * @return The LayoutParams associated with this view, or null if no
9798 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009800 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 public ViewGroup.LayoutParams getLayoutParams() {
9802 return mLayoutParams;
9803 }
9804
9805 /**
9806 * Set the layout parameters associated with this view. These supply
9807 * parameters to the <i>parent</i> of this view specifying how it should be
9808 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9809 * correspond to the different subclasses of ViewGroup that are responsible
9810 * for arranging their children.
9811 *
Romain Guy01c174b2011-02-22 11:51:06 -08009812 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 */
9814 public void setLayoutParams(ViewGroup.LayoutParams params) {
9815 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009816 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 }
9818 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009819 if (mParent instanceof ViewGroup) {
9820 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 requestLayout();
9823 }
9824
9825 /**
9826 * Set the scrolled position of your view. This will cause a call to
9827 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9828 * invalidated.
9829 * @param x the x position to scroll to
9830 * @param y the y position to scroll to
9831 */
9832 public void scrollTo(int x, int y) {
9833 if (mScrollX != x || mScrollY != y) {
9834 int oldX = mScrollX;
9835 int oldY = mScrollY;
9836 mScrollX = x;
9837 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009838 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009840 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009841 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009843 }
9844 }
9845
9846 /**
9847 * Move the scrolled position of your view. This will cause a call to
9848 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9849 * invalidated.
9850 * @param x the amount of pixels to scroll by horizontally
9851 * @param y the amount of pixels to scroll by vertically
9852 */
9853 public void scrollBy(int x, int y) {
9854 scrollTo(mScrollX + x, mScrollY + y);
9855 }
9856
9857 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009858 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9859 * animation to fade the scrollbars out after a default delay. If a subclass
9860 * provides animated scrolling, the start delay should equal the duration
9861 * of the scrolling animation.</p>
9862 *
9863 * <p>The animation starts only if at least one of the scrollbars is
9864 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9865 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9866 * this method returns true, and false otherwise. If the animation is
9867 * started, this method calls {@link #invalidate()}; in that case the
9868 * caller should not call {@link #invalidate()}.</p>
9869 *
9870 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009871 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009872 *
9873 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9874 * and {@link #scrollTo(int, int)}.</p>
9875 *
9876 * @return true if the animation is played, false otherwise
9877 *
9878 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009879 * @see #scrollBy(int, int)
9880 * @see #scrollTo(int, int)
9881 * @see #isHorizontalScrollBarEnabled()
9882 * @see #isVerticalScrollBarEnabled()
9883 * @see #setHorizontalScrollBarEnabled(boolean)
9884 * @see #setVerticalScrollBarEnabled(boolean)
9885 */
9886 protected boolean awakenScrollBars() {
9887 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009888 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009889 }
9890
9891 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009892 * Trigger the scrollbars to draw.
9893 * This method differs from awakenScrollBars() only in its default duration.
9894 * initialAwakenScrollBars() will show the scroll bars for longer than
9895 * usual to give the user more of a chance to notice them.
9896 *
9897 * @return true if the animation is played, false otherwise.
9898 */
9899 private boolean initialAwakenScrollBars() {
9900 return mScrollCache != null &&
9901 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9902 }
9903
9904 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009905 * <p>
9906 * Trigger the scrollbars to draw. When invoked this method starts an
9907 * animation to fade the scrollbars out after a fixed delay. If a subclass
9908 * provides animated scrolling, the start delay should equal the duration of
9909 * the scrolling animation.
9910 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009911 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009912 * <p>
9913 * The animation starts only if at least one of the scrollbars is enabled,
9914 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9915 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9916 * this method returns true, and false otherwise. If the animation is
9917 * started, this method calls {@link #invalidate()}; in that case the caller
9918 * should not call {@link #invalidate()}.
9919 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009920 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009921 * <p>
9922 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009923 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009924 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009925 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009926 * @param startDelay the delay, in milliseconds, after which the animation
9927 * should start; when the delay is 0, the animation starts
9928 * immediately
9929 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009930 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009931 * @see #scrollBy(int, int)
9932 * @see #scrollTo(int, int)
9933 * @see #isHorizontalScrollBarEnabled()
9934 * @see #isVerticalScrollBarEnabled()
9935 * @see #setHorizontalScrollBarEnabled(boolean)
9936 * @see #setVerticalScrollBarEnabled(boolean)
9937 */
9938 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009939 return awakenScrollBars(startDelay, true);
9940 }
Joe Malin32736f02011-01-19 16:14:20 -08009941
Mike Cleron290947b2009-09-29 18:34:32 -07009942 /**
9943 * <p>
9944 * Trigger the scrollbars to draw. When invoked this method starts an
9945 * animation to fade the scrollbars out after a fixed delay. If a subclass
9946 * provides animated scrolling, the start delay should equal the duration of
9947 * the scrolling animation.
9948 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009949 *
Mike Cleron290947b2009-09-29 18:34:32 -07009950 * <p>
9951 * The animation starts only if at least one of the scrollbars is enabled,
9952 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9953 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9954 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009955 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009956 * is set to true; in that case the caller
9957 * should not call {@link #invalidate()}.
9958 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009959 *
Mike Cleron290947b2009-09-29 18:34:32 -07009960 * <p>
9961 * This method should be invoked everytime a subclass directly updates the
9962 * scroll parameters.
9963 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009964 *
Mike Cleron290947b2009-09-29 18:34:32 -07009965 * @param startDelay the delay, in milliseconds, after which the animation
9966 * should start; when the delay is 0, the animation starts
9967 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009968 *
Mike Cleron290947b2009-09-29 18:34:32 -07009969 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009970 *
Mike Cleron290947b2009-09-29 18:34:32 -07009971 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009972 *
Mike Cleron290947b2009-09-29 18:34:32 -07009973 * @see #scrollBy(int, int)
9974 * @see #scrollTo(int, int)
9975 * @see #isHorizontalScrollBarEnabled()
9976 * @see #isVerticalScrollBarEnabled()
9977 * @see #setHorizontalScrollBarEnabled(boolean)
9978 * @see #setVerticalScrollBarEnabled(boolean)
9979 */
9980 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009981 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009982
Mike Cleronf116bf82009-09-27 19:14:12 -07009983 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9984 return false;
9985 }
9986
9987 if (scrollCache.scrollBar == null) {
9988 scrollCache.scrollBar = new ScrollBarDrawable();
9989 }
9990
9991 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9992
Mike Cleron290947b2009-09-29 18:34:32 -07009993 if (invalidate) {
9994 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009995 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009996 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009997
9998 if (scrollCache.state == ScrollabilityCache.OFF) {
9999 // FIXME: this is copied from WindowManagerService.
10000 // We should get this value from the system when it
10001 // is possible to do so.
10002 final int KEY_REPEAT_FIRST_DELAY = 750;
10003 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10004 }
10005
10006 // Tell mScrollCache when we should start fading. This may
10007 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010008 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010009 scrollCache.fadeStartTime = fadeStartTime;
10010 scrollCache.state = ScrollabilityCache.ON;
10011
10012 // Schedule our fader to run, unscheduling any old ones first
10013 if (mAttachInfo != null) {
10014 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10015 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10016 }
10017
10018 return true;
10019 }
10020
10021 return false;
10022 }
10023
10024 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010025 * Do not invalidate views which are not visible and which are not running an animation. They
10026 * will not get drawn and they should not set dirty flags as if they will be drawn
10027 */
10028 private boolean skipInvalidate() {
10029 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10030 (!(mParent instanceof ViewGroup) ||
10031 !((ViewGroup) mParent).isViewTransitioning(this));
10032 }
10033 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010034 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010035 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10036 * in the future. This must be called from a UI thread. To call from a non-UI
10037 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 *
10039 * WARNING: This method is destructive to dirty.
10040 * @param dirty the rectangle representing the bounds of the dirty region
10041 */
10042 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010043 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010044 return;
10045 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010046 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010047 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10048 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010049 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010050 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010051 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 final ViewParent p = mParent;
10053 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010054 //noinspection PointlessBooleanExpression,ConstantConditions
10055 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10056 if (p != null && ai != null && ai.mHardwareAccelerated) {
10057 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010058 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010059 p.invalidateChild(this, null);
10060 return;
10061 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 if (p != null && ai != null) {
10064 final int scrollX = mScrollX;
10065 final int scrollY = mScrollY;
10066 final Rect r = ai.mTmpInvalRect;
10067 r.set(dirty.left - scrollX, dirty.top - scrollY,
10068 dirty.right - scrollX, dirty.bottom - scrollY);
10069 mParent.invalidateChild(this, r);
10070 }
10071 }
10072 }
10073
10074 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010075 * 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 -080010076 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010077 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10078 * will be called at some point in the future. This must be called from
10079 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 * @param l the left position of the dirty region
10081 * @param t the top position of the dirty region
10082 * @param r the right position of the dirty region
10083 * @param b the bottom position of the dirty region
10084 */
10085 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010086 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010087 return;
10088 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010089 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010090 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10091 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010093 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010094 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 final ViewParent p = mParent;
10096 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010097 //noinspection PointlessBooleanExpression,ConstantConditions
10098 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10099 if (p != null && ai != null && ai.mHardwareAccelerated) {
10100 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010101 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010102 p.invalidateChild(this, null);
10103 return;
10104 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010106 if (p != null && ai != null && l < r && t < b) {
10107 final int scrollX = mScrollX;
10108 final int scrollY = mScrollY;
10109 final Rect tmpr = ai.mTmpInvalRect;
10110 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10111 p.invalidateChild(this, tmpr);
10112 }
10113 }
10114 }
10115
10116 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010117 * Invalidate the whole view. If the view is visible,
10118 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10119 * the future. This must be called from a UI thread. To call from a non-UI thread,
10120 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 */
10122 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010123 invalidate(true);
10124 }
Joe Malin32736f02011-01-19 16:14:20 -080010125
Chet Haaseed032702010-10-01 14:05:54 -070010126 /**
10127 * This is where the invalidate() work actually happens. A full invalidate()
10128 * causes the drawing cache to be invalidated, but this function can be called with
10129 * invalidateCache set to false to skip that invalidation step for cases that do not
10130 * need it (for example, a component that remains at the same dimensions with the same
10131 * content).
10132 *
10133 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10134 * well. This is usually true for a full invalidate, but may be set to false if the
10135 * View's contents or dimensions have not changed.
10136 */
Romain Guy849d0a32011-02-01 17:20:48 -080010137 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010138 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010139 return;
10140 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010141 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010142 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010143 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10144 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010145 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010146 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010147 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010148 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010149 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010152 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010153 //noinspection PointlessBooleanExpression,ConstantConditions
10154 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10155 if (p != null && ai != null && ai.mHardwareAccelerated) {
10156 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010157 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010158 p.invalidateChild(this, null);
10159 return;
10160 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010161 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 if (p != null && ai != null) {
10164 final Rect r = ai.mTmpInvalRect;
10165 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10166 // Don't call invalidate -- we don't want to internally scroll
10167 // our own bounds
10168 p.invalidateChild(this, r);
10169 }
10170 }
10171 }
10172
10173 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010174 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10175 * set any flags or handle all of the cases handled by the default invalidation methods.
10176 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10177 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10178 * walk up the hierarchy, transforming the dirty rect as necessary.
10179 *
10180 * The method also handles normal invalidation logic if display list properties are not
10181 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10182 * backup approach, to handle these cases used in the various property-setting methods.
10183 *
10184 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10185 * are not being used in this view
10186 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10187 * list properties are not being used in this view
10188 */
10189 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010190 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010191 if (invalidateParent) {
10192 invalidateParentCaches();
10193 }
10194 if (forceRedraw) {
10195 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10196 }
10197 invalidate(false);
10198 } else {
10199 final AttachInfo ai = mAttachInfo;
10200 final ViewParent p = mParent;
10201 if (p != null && ai != null) {
10202 final Rect r = ai.mTmpInvalRect;
10203 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10204 if (mParent instanceof ViewGroup) {
10205 ((ViewGroup) mParent).invalidateChildFast(this, r);
10206 } else {
10207 mParent.invalidateChild(this, r);
10208 }
10209 }
10210 }
10211 }
10212
10213 /**
10214 * Utility method to transform a given Rect by the current matrix of this view.
10215 */
10216 void transformRect(final Rect rect) {
10217 if (!getMatrix().isIdentity()) {
10218 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10219 boundingRect.set(rect);
10220 getMatrix().mapRect(boundingRect);
10221 rect.set((int) (boundingRect.left - 0.5f),
10222 (int) (boundingRect.top - 0.5f),
10223 (int) (boundingRect.right + 0.5f),
10224 (int) (boundingRect.bottom + 0.5f));
10225 }
10226 }
10227
10228 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010229 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010230 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10231 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010232 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10233 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010234 *
10235 * @hide
10236 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010237 protected void invalidateParentCaches() {
10238 if (mParent instanceof View) {
10239 ((View) mParent).mPrivateFlags |= INVALIDATED;
10240 }
10241 }
Joe Malin32736f02011-01-19 16:14:20 -080010242
Romain Guy0fd89bf2011-01-26 15:41:30 -080010243 /**
10244 * Used to indicate that the parent of this view should be invalidated. This functionality
10245 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10246 * which is necessary when various parent-managed properties of the view change, such as
10247 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10248 * an invalidation event to the parent.
10249 *
10250 * @hide
10251 */
10252 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010253 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010254 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010255 }
10256 }
10257
10258 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010259 * Indicates whether this View is opaque. An opaque View guarantees that it will
10260 * draw all the pixels overlapping its bounds using a fully opaque color.
10261 *
10262 * Subclasses of View should override this method whenever possible to indicate
10263 * whether an instance is opaque. Opaque Views are treated in a special way by
10264 * the View hierarchy, possibly allowing it to perform optimizations during
10265 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010266 *
Romain Guy24443ea2009-05-11 11:56:30 -070010267 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010268 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010269 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010270 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010271 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010272 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10273 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010274 }
10275
Adam Powell20232d02010-12-08 21:08:53 -080010276 /**
10277 * @hide
10278 */
10279 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010280 // Opaque if:
10281 // - Has a background
10282 // - Background is opaque
10283 // - Doesn't have scrollbars or scrollbars are inside overlay
10284
Philip Milne6c8ea062012-04-03 17:38:43 -070010285 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010286 mPrivateFlags |= OPAQUE_BACKGROUND;
10287 } else {
10288 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10289 }
10290
10291 final int flags = mViewFlags;
10292 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10293 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10294 mPrivateFlags |= OPAQUE_SCROLLBARS;
10295 } else {
10296 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10297 }
10298 }
10299
10300 /**
10301 * @hide
10302 */
10303 protected boolean hasOpaqueScrollbars() {
10304 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010305 }
10306
10307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010308 * @return A handler associated with the thread running the View. This
10309 * handler can be used to pump events in the UI events queue.
10310 */
10311 public Handler getHandler() {
10312 if (mAttachInfo != null) {
10313 return mAttachInfo.mHandler;
10314 }
10315 return null;
10316 }
10317
10318 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010319 * Gets the view root associated with the View.
10320 * @return The view root, or null if none.
10321 * @hide
10322 */
10323 public ViewRootImpl getViewRootImpl() {
10324 if (mAttachInfo != null) {
10325 return mAttachInfo.mViewRootImpl;
10326 }
10327 return null;
10328 }
10329
10330 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010331 * <p>Causes the Runnable to be added to the message queue.
10332 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010333 *
Romain Guye63a4f32011-08-11 11:33:31 -070010334 * <p>This method can be invoked from outside of the UI thread
10335 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 *
10337 * @param action The Runnable that will be executed.
10338 *
10339 * @return Returns true if the Runnable was successfully placed in to the
10340 * message queue. Returns false on failure, usually because the
10341 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010342 *
10343 * @see #postDelayed
10344 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 */
10346 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010347 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010348 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010349 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010351 // Assume that post will succeed later
10352 ViewRootImpl.getRunQueue().post(action);
10353 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 }
10355
10356 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010357 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010359 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010360 *
Romain Guye63a4f32011-08-11 11:33:31 -070010361 * <p>This method can be invoked from outside of the UI thread
10362 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 *
10364 * @param action The Runnable that will be executed.
10365 * @param delayMillis The delay (in milliseconds) until the Runnable
10366 * will be executed.
10367 *
10368 * @return true if the Runnable was successfully placed in to the
10369 * message queue. Returns false on failure, usually because the
10370 * looper processing the message queue is exiting. Note that a
10371 * result of true does not mean the Runnable will be processed --
10372 * if the looper is quit before the delivery time of the message
10373 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010374 *
10375 * @see #post
10376 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 */
10378 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010379 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010380 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010381 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010383 // Assume that post will succeed later
10384 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10385 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 }
10387
10388 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010389 * <p>Causes the Runnable to execute on the next animation time step.
10390 * The runnable will be run on the user interface thread.</p>
10391 *
10392 * <p>This method can be invoked from outside of the UI thread
10393 * only when this View is attached to a window.</p>
10394 *
10395 * @param action The Runnable that will be executed.
10396 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010397 * @see #postOnAnimationDelayed
10398 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010399 */
10400 public void postOnAnimation(Runnable action) {
10401 final AttachInfo attachInfo = mAttachInfo;
10402 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010403 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10404 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010405 } else {
10406 // Assume that post will succeed later
10407 ViewRootImpl.getRunQueue().post(action);
10408 }
10409 }
10410
10411 /**
10412 * <p>Causes the Runnable to execute on the next animation time step,
10413 * after the specified amount of time elapses.
10414 * The runnable will be run on the user interface thread.</p>
10415 *
10416 * <p>This method can be invoked from outside of the UI thread
10417 * only when this View is attached to a window.</p>
10418 *
10419 * @param action The Runnable that will be executed.
10420 * @param delayMillis The delay (in milliseconds) until the Runnable
10421 * will be executed.
10422 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010423 * @see #postOnAnimation
10424 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010425 */
10426 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10427 final AttachInfo attachInfo = mAttachInfo;
10428 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010429 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10430 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010431 } else {
10432 // Assume that post will succeed later
10433 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10434 }
10435 }
10436
10437 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010438 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010439 *
Romain Guye63a4f32011-08-11 11:33:31 -070010440 * <p>This method can be invoked from outside of the UI thread
10441 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 *
10443 * @param action The Runnable to remove from the message handling queue
10444 *
10445 * @return true if this view could ask the Handler to remove the Runnable,
10446 * false otherwise. When the returned value is true, the Runnable
10447 * may or may not have been actually removed from the message queue
10448 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010449 *
10450 * @see #post
10451 * @see #postDelayed
10452 * @see #postOnAnimation
10453 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010454 */
10455 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010456 if (action != null) {
10457 final AttachInfo attachInfo = mAttachInfo;
10458 if (attachInfo != null) {
10459 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010460 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10461 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010462 } else {
10463 // Assume that post will succeed later
10464 ViewRootImpl.getRunQueue().removeCallbacks(action);
10465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 return true;
10468 }
10469
10470 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010471 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10472 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 *
Romain Guye63a4f32011-08-11 11:33:31 -070010474 * <p>This method can be invoked from outside of the UI thread
10475 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010478 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 */
10480 public void postInvalidate() {
10481 postInvalidateDelayed(0);
10482 }
10483
10484 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010485 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10486 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010487 *
Romain Guye63a4f32011-08-11 11:33:31 -070010488 * <p>This method can be invoked from outside of the UI thread
10489 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 *
10491 * @param left The left coordinate of the rectangle to invalidate.
10492 * @param top The top coordinate of the rectangle to invalidate.
10493 * @param right The right coordinate of the rectangle to invalidate.
10494 * @param bottom The bottom coordinate of the rectangle to invalidate.
10495 *
10496 * @see #invalidate(int, int, int, int)
10497 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010498 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010499 */
10500 public void postInvalidate(int left, int top, int right, int bottom) {
10501 postInvalidateDelayed(0, left, top, right, bottom);
10502 }
10503
10504 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010505 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10506 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010507 *
Romain Guye63a4f32011-08-11 11:33:31 -070010508 * <p>This method can be invoked from outside of the UI thread
10509 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 *
10511 * @param delayMilliseconds the duration in milliseconds to delay the
10512 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010513 *
10514 * @see #invalidate()
10515 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 */
10517 public void postInvalidateDelayed(long delayMilliseconds) {
10518 // We try only with the AttachInfo because there's no point in invalidating
10519 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010520 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010521 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010522 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 }
10524 }
10525
10526 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010527 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10528 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010529 *
Romain Guye63a4f32011-08-11 11:33:31 -070010530 * <p>This method can be invoked from outside of the UI thread
10531 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532 *
10533 * @param delayMilliseconds the duration in milliseconds to delay the
10534 * invalidation by
10535 * @param left The left coordinate of the rectangle to invalidate.
10536 * @param top The top coordinate of the rectangle to invalidate.
10537 * @param right The right coordinate of the rectangle to invalidate.
10538 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010539 *
10540 * @see #invalidate(int, int, int, int)
10541 * @see #invalidate(Rect)
10542 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 */
10544 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10545 int right, int bottom) {
10546
10547 // We try only with the AttachInfo because there's no point in invalidating
10548 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010549 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010550 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10552 info.target = this;
10553 info.left = left;
10554 info.top = top;
10555 info.right = right;
10556 info.bottom = bottom;
10557
Jeff Browna175a5b2012-02-15 19:18:31 -080010558 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 }
10560 }
10561
10562 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010563 * <p>Cause an invalidate to happen on the next animation time step, typically the
10564 * next display frame.</p>
10565 *
10566 * <p>This method can be invoked from outside of the UI thread
10567 * only when this View is attached to a window.</p>
10568 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010569 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010570 */
10571 public void postInvalidateOnAnimation() {
10572 // We try only with the AttachInfo because there's no point in invalidating
10573 // if we are not attached to our window
10574 final AttachInfo attachInfo = mAttachInfo;
10575 if (attachInfo != null) {
10576 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10577 }
10578 }
10579
10580 /**
10581 * <p>Cause an invalidate of the specified area to happen on the next animation
10582 * time step, typically the next display frame.</p>
10583 *
10584 * <p>This method can be invoked from outside of the UI thread
10585 * only when this View is attached to a window.</p>
10586 *
10587 * @param left The left coordinate of the rectangle to invalidate.
10588 * @param top The top coordinate of the rectangle to invalidate.
10589 * @param right The right coordinate of the rectangle to invalidate.
10590 * @param bottom The bottom coordinate of the rectangle to invalidate.
10591 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010592 * @see #invalidate(int, int, int, int)
10593 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010594 */
10595 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10596 // We try only with the AttachInfo because there's no point in invalidating
10597 // if we are not attached to our window
10598 final AttachInfo attachInfo = mAttachInfo;
10599 if (attachInfo != null) {
10600 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10601 info.target = this;
10602 info.left = left;
10603 info.top = top;
10604 info.right = right;
10605 info.bottom = bottom;
10606
10607 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10608 }
10609 }
10610
10611 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010612 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10613 * This event is sent at most once every
10614 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10615 */
10616 private void postSendViewScrolledAccessibilityEventCallback() {
10617 if (mSendViewScrolledAccessibilityEvent == null) {
10618 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10619 }
10620 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10621 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10622 postDelayed(mSendViewScrolledAccessibilityEvent,
10623 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10624 }
10625 }
10626
10627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010628 * Called by a parent to request that a child update its values for mScrollX
10629 * and mScrollY if necessary. This will typically be done if the child is
10630 * animating a scroll using a {@link android.widget.Scroller Scroller}
10631 * object.
10632 */
10633 public void computeScroll() {
10634 }
10635
10636 /**
10637 * <p>Indicate whether the horizontal edges are faded when the view is
10638 * scrolled horizontally.</p>
10639 *
10640 * @return true if the horizontal edges should are faded on scroll, false
10641 * otherwise
10642 *
10643 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010644 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010645 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 */
10647 public boolean isHorizontalFadingEdgeEnabled() {
10648 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10649 }
10650
10651 /**
10652 * <p>Define whether the horizontal edges should be faded when this view
10653 * is scrolled horizontally.</p>
10654 *
10655 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10656 * be faded when the view is scrolled
10657 * horizontally
10658 *
10659 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010660 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010661 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 */
10663 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10664 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10665 if (horizontalFadingEdgeEnabled) {
10666 initScrollCache();
10667 }
10668
10669 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10670 }
10671 }
10672
10673 /**
10674 * <p>Indicate whether the vertical edges are faded when the view is
10675 * scrolled horizontally.</p>
10676 *
10677 * @return true if the vertical edges should are faded on scroll, false
10678 * otherwise
10679 *
10680 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010681 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010682 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683 */
10684 public boolean isVerticalFadingEdgeEnabled() {
10685 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10686 }
10687
10688 /**
10689 * <p>Define whether the vertical edges should be faded when this view
10690 * is scrolled vertically.</p>
10691 *
10692 * @param verticalFadingEdgeEnabled true if the vertical edges should
10693 * be faded when the view is scrolled
10694 * vertically
10695 *
10696 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010697 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010698 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 */
10700 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10701 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10702 if (verticalFadingEdgeEnabled) {
10703 initScrollCache();
10704 }
10705
10706 mViewFlags ^= FADING_EDGE_VERTICAL;
10707 }
10708 }
10709
10710 /**
10711 * Returns the strength, or intensity, of the top faded edge. The strength is
10712 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10713 * returns 0.0 or 1.0 but no value in between.
10714 *
10715 * Subclasses should override this method to provide a smoother fade transition
10716 * when scrolling occurs.
10717 *
10718 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10719 */
10720 protected float getTopFadingEdgeStrength() {
10721 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10722 }
10723
10724 /**
10725 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10726 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10727 * returns 0.0 or 1.0 but no value in between.
10728 *
10729 * Subclasses should override this method to provide a smoother fade transition
10730 * when scrolling occurs.
10731 *
10732 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10733 */
10734 protected float getBottomFadingEdgeStrength() {
10735 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10736 computeVerticalScrollRange() ? 1.0f : 0.0f;
10737 }
10738
10739 /**
10740 * Returns the strength, or intensity, of the left faded edge. The strength is
10741 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10742 * returns 0.0 or 1.0 but no value in between.
10743 *
10744 * Subclasses should override this method to provide a smoother fade transition
10745 * when scrolling occurs.
10746 *
10747 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10748 */
10749 protected float getLeftFadingEdgeStrength() {
10750 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10751 }
10752
10753 /**
10754 * Returns the strength, or intensity, of the right faded edge. The strength is
10755 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10756 * returns 0.0 or 1.0 but no value in between.
10757 *
10758 * Subclasses should override this method to provide a smoother fade transition
10759 * when scrolling occurs.
10760 *
10761 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10762 */
10763 protected float getRightFadingEdgeStrength() {
10764 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10765 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10766 }
10767
10768 /**
10769 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10770 * scrollbar is not drawn by default.</p>
10771 *
10772 * @return true if the horizontal scrollbar should be painted, false
10773 * otherwise
10774 *
10775 * @see #setHorizontalScrollBarEnabled(boolean)
10776 */
10777 public boolean isHorizontalScrollBarEnabled() {
10778 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10779 }
10780
10781 /**
10782 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10783 * scrollbar is not drawn by default.</p>
10784 *
10785 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10786 * be painted
10787 *
10788 * @see #isHorizontalScrollBarEnabled()
10789 */
10790 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10791 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10792 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010793 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010794 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 }
10796 }
10797
10798 /**
10799 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10800 * scrollbar is not drawn by default.</p>
10801 *
10802 * @return true if the vertical scrollbar should be painted, false
10803 * otherwise
10804 *
10805 * @see #setVerticalScrollBarEnabled(boolean)
10806 */
10807 public boolean isVerticalScrollBarEnabled() {
10808 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10809 }
10810
10811 /**
10812 * <p>Define whether the vertical scrollbar should be drawn or not. The
10813 * scrollbar is not drawn by default.</p>
10814 *
10815 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10816 * be painted
10817 *
10818 * @see #isVerticalScrollBarEnabled()
10819 */
10820 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10821 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10822 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010823 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010824 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 }
10826 }
10827
Adam Powell20232d02010-12-08 21:08:53 -080010828 /**
10829 * @hide
10830 */
10831 protected void recomputePadding() {
10832 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010833 }
Joe Malin32736f02011-01-19 16:14:20 -080010834
Mike Cleronfe81d382009-09-28 14:22:16 -070010835 /**
10836 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010837 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010838 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010839 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010840 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010841 */
10842 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10843 initScrollCache();
10844 final ScrollabilityCache scrollabilityCache = mScrollCache;
10845 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010846 if (fadeScrollbars) {
10847 scrollabilityCache.state = ScrollabilityCache.OFF;
10848 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010849 scrollabilityCache.state = ScrollabilityCache.ON;
10850 }
10851 }
Joe Malin32736f02011-01-19 16:14:20 -080010852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 /**
Joe Malin32736f02011-01-19 16:14:20 -080010854 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010855 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010856 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010857 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010858 *
10859 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010860 */
10861 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010862 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010863 }
Joe Malin32736f02011-01-19 16:14:20 -080010864
Mike Cleron52f0a642009-09-28 18:21:37 -070010865 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010866 *
10867 * Returns the delay before scrollbars fade.
10868 *
10869 * @return the delay before scrollbars fade
10870 *
10871 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10872 */
10873 public int getScrollBarDefaultDelayBeforeFade() {
10874 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10875 mScrollCache.scrollBarDefaultDelayBeforeFade;
10876 }
10877
10878 /**
10879 * Define the delay before scrollbars fade.
10880 *
10881 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10882 *
10883 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10884 */
10885 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10886 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10887 }
10888
10889 /**
10890 *
10891 * Returns the scrollbar fade duration.
10892 *
10893 * @return the scrollbar fade duration
10894 *
10895 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10896 */
10897 public int getScrollBarFadeDuration() {
10898 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10899 mScrollCache.scrollBarFadeDuration;
10900 }
10901
10902 /**
10903 * Define the scrollbar fade duration.
10904 *
10905 * @param scrollBarFadeDuration - the scrollbar fade duration
10906 *
10907 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10908 */
10909 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10910 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10911 }
10912
10913 /**
10914 *
10915 * Returns the scrollbar size.
10916 *
10917 * @return the scrollbar size
10918 *
10919 * @attr ref android.R.styleable#View_scrollbarSize
10920 */
10921 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010922 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010923 mScrollCache.scrollBarSize;
10924 }
10925
10926 /**
10927 * Define the scrollbar size.
10928 *
10929 * @param scrollBarSize - the scrollbar size
10930 *
10931 * @attr ref android.R.styleable#View_scrollbarSize
10932 */
10933 public void setScrollBarSize(int scrollBarSize) {
10934 getScrollCache().scrollBarSize = scrollBarSize;
10935 }
10936
10937 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10939 * inset. When inset, they add to the padding of the view. And the scrollbars
10940 * can be drawn inside the padding area or on the edge of the view. For example,
10941 * if a view has a background drawable and you want to draw the scrollbars
10942 * inside the padding specified by the drawable, you can use
10943 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10944 * appear at the edge of the view, ignoring the padding, then you can use
10945 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10946 * @param style the style of the scrollbars. Should be one of
10947 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10948 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10949 * @see #SCROLLBARS_INSIDE_OVERLAY
10950 * @see #SCROLLBARS_INSIDE_INSET
10951 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10952 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010953 *
10954 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 */
10956 public void setScrollBarStyle(int style) {
10957 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10958 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010959 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010960 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961 }
10962 }
10963
10964 /**
10965 * <p>Returns the current scrollbar style.</p>
10966 * @return the current scrollbar style
10967 * @see #SCROLLBARS_INSIDE_OVERLAY
10968 * @see #SCROLLBARS_INSIDE_INSET
10969 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10970 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010971 *
10972 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010973 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010974 @ViewDebug.ExportedProperty(mapping = {
10975 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10976 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10977 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10978 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10979 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980 public int getScrollBarStyle() {
10981 return mViewFlags & SCROLLBARS_STYLE_MASK;
10982 }
10983
10984 /**
10985 * <p>Compute the horizontal range that the horizontal scrollbar
10986 * represents.</p>
10987 *
10988 * <p>The range is expressed in arbitrary units that must be the same as the
10989 * units used by {@link #computeHorizontalScrollExtent()} and
10990 * {@link #computeHorizontalScrollOffset()}.</p>
10991 *
10992 * <p>The default range is the drawing width of this view.</p>
10993 *
10994 * @return the total horizontal range represented by the horizontal
10995 * scrollbar
10996 *
10997 * @see #computeHorizontalScrollExtent()
10998 * @see #computeHorizontalScrollOffset()
10999 * @see android.widget.ScrollBarDrawable
11000 */
11001 protected int computeHorizontalScrollRange() {
11002 return getWidth();
11003 }
11004
11005 /**
11006 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11007 * within the horizontal range. This value is used to compute the position
11008 * of the thumb within the scrollbar's track.</p>
11009 *
11010 * <p>The range is expressed in arbitrary units that must be the same as the
11011 * units used by {@link #computeHorizontalScrollRange()} and
11012 * {@link #computeHorizontalScrollExtent()}.</p>
11013 *
11014 * <p>The default offset is the scroll offset of this view.</p>
11015 *
11016 * @return the horizontal offset of the scrollbar's thumb
11017 *
11018 * @see #computeHorizontalScrollRange()
11019 * @see #computeHorizontalScrollExtent()
11020 * @see android.widget.ScrollBarDrawable
11021 */
11022 protected int computeHorizontalScrollOffset() {
11023 return mScrollX;
11024 }
11025
11026 /**
11027 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11028 * within the horizontal range. This value is used to compute the length
11029 * of the thumb within the scrollbar's track.</p>
11030 *
11031 * <p>The range is expressed in arbitrary units that must be the same as the
11032 * units used by {@link #computeHorizontalScrollRange()} and
11033 * {@link #computeHorizontalScrollOffset()}.</p>
11034 *
11035 * <p>The default extent is the drawing width of this view.</p>
11036 *
11037 * @return the horizontal extent of the scrollbar's thumb
11038 *
11039 * @see #computeHorizontalScrollRange()
11040 * @see #computeHorizontalScrollOffset()
11041 * @see android.widget.ScrollBarDrawable
11042 */
11043 protected int computeHorizontalScrollExtent() {
11044 return getWidth();
11045 }
11046
11047 /**
11048 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11049 *
11050 * <p>The range is expressed in arbitrary units that must be the same as the
11051 * units used by {@link #computeVerticalScrollExtent()} and
11052 * {@link #computeVerticalScrollOffset()}.</p>
11053 *
11054 * @return the total vertical range represented by the vertical scrollbar
11055 *
11056 * <p>The default range is the drawing height of this view.</p>
11057 *
11058 * @see #computeVerticalScrollExtent()
11059 * @see #computeVerticalScrollOffset()
11060 * @see android.widget.ScrollBarDrawable
11061 */
11062 protected int computeVerticalScrollRange() {
11063 return getHeight();
11064 }
11065
11066 /**
11067 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11068 * within the horizontal range. This value is used to compute the position
11069 * of the thumb within the scrollbar's track.</p>
11070 *
11071 * <p>The range is expressed in arbitrary units that must be the same as the
11072 * units used by {@link #computeVerticalScrollRange()} and
11073 * {@link #computeVerticalScrollExtent()}.</p>
11074 *
11075 * <p>The default offset is the scroll offset of this view.</p>
11076 *
11077 * @return the vertical offset of the scrollbar's thumb
11078 *
11079 * @see #computeVerticalScrollRange()
11080 * @see #computeVerticalScrollExtent()
11081 * @see android.widget.ScrollBarDrawable
11082 */
11083 protected int computeVerticalScrollOffset() {
11084 return mScrollY;
11085 }
11086
11087 /**
11088 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11089 * within the vertical range. This value is used to compute the length
11090 * of the thumb within the scrollbar's track.</p>
11091 *
11092 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011093 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011094 * {@link #computeVerticalScrollOffset()}.</p>
11095 *
11096 * <p>The default extent is the drawing height of this view.</p>
11097 *
11098 * @return the vertical extent of the scrollbar's thumb
11099 *
11100 * @see #computeVerticalScrollRange()
11101 * @see #computeVerticalScrollOffset()
11102 * @see android.widget.ScrollBarDrawable
11103 */
11104 protected int computeVerticalScrollExtent() {
11105 return getHeight();
11106 }
11107
11108 /**
Adam Powell69159442011-06-13 17:53:06 -070011109 * Check if this view can be scrolled horizontally in a certain direction.
11110 *
11111 * @param direction Negative to check scrolling left, positive to check scrolling right.
11112 * @return true if this view can be scrolled in the specified direction, false otherwise.
11113 */
11114 public boolean canScrollHorizontally(int direction) {
11115 final int offset = computeHorizontalScrollOffset();
11116 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11117 if (range == 0) return false;
11118 if (direction < 0) {
11119 return offset > 0;
11120 } else {
11121 return offset < range - 1;
11122 }
11123 }
11124
11125 /**
11126 * Check if this view can be scrolled vertically in a certain direction.
11127 *
11128 * @param direction Negative to check scrolling up, positive to check scrolling down.
11129 * @return true if this view can be scrolled in the specified direction, false otherwise.
11130 */
11131 public boolean canScrollVertically(int direction) {
11132 final int offset = computeVerticalScrollOffset();
11133 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11134 if (range == 0) return false;
11135 if (direction < 0) {
11136 return offset > 0;
11137 } else {
11138 return offset < range - 1;
11139 }
11140 }
11141
11142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11144 * scrollbars are painted only if they have been awakened first.</p>
11145 *
11146 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011147 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011148 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011150 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 // scrollbars are drawn only when the animation is running
11152 final ScrollabilityCache cache = mScrollCache;
11153 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011154
Mike Cleronf116bf82009-09-27 19:14:12 -070011155 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011156
Mike Cleronf116bf82009-09-27 19:14:12 -070011157 if (state == ScrollabilityCache.OFF) {
11158 return;
11159 }
Joe Malin32736f02011-01-19 16:14:20 -080011160
Mike Cleronf116bf82009-09-27 19:14:12 -070011161 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011162
Mike Cleronf116bf82009-09-27 19:14:12 -070011163 if (state == ScrollabilityCache.FADING) {
11164 // We're fading -- get our fade interpolation
11165 if (cache.interpolatorValues == null) {
11166 cache.interpolatorValues = new float[1];
11167 }
Joe Malin32736f02011-01-19 16:14:20 -080011168
Mike Cleronf116bf82009-09-27 19:14:12 -070011169 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011170
Mike Cleronf116bf82009-09-27 19:14:12 -070011171 // Stops the animation if we're done
11172 if (cache.scrollBarInterpolator.timeToValues(values) ==
11173 Interpolator.Result.FREEZE_END) {
11174 cache.state = ScrollabilityCache.OFF;
11175 } else {
11176 cache.scrollBar.setAlpha(Math.round(values[0]));
11177 }
Joe Malin32736f02011-01-19 16:14:20 -080011178
11179 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011180 // drawing. We only want this when we're fading so that
11181 // we prevent excessive redraws
11182 invalidate = true;
11183 } else {
11184 // We're just on -- but we may have been fading before so
11185 // reset alpha
11186 cache.scrollBar.setAlpha(255);
11187 }
11188
Joe Malin32736f02011-01-19 16:14:20 -080011189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011190 final int viewFlags = mViewFlags;
11191
11192 final boolean drawHorizontalScrollBar =
11193 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11194 final boolean drawVerticalScrollBar =
11195 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11196 && !isVerticalScrollBarHidden();
11197
11198 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11199 final int width = mRight - mLeft;
11200 final int height = mBottom - mTop;
11201
11202 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203
Mike Reede8853fc2009-09-04 14:01:48 -040011204 final int scrollX = mScrollX;
11205 final int scrollY = mScrollY;
11206 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11207
Mike Cleronf116bf82009-09-27 19:14:12 -070011208 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011211 int size = scrollBar.getSize(false);
11212 if (size <= 0) {
11213 size = cache.scrollBarSize;
11214 }
11215
Mike Cleronf116bf82009-09-27 19:14:12 -070011216 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011217 computeHorizontalScrollOffset(),
11218 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011219 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011220 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011221 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011222 left = scrollX + (mPaddingLeft & inside);
11223 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11224 bottom = top + size;
11225 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11226 if (invalidate) {
11227 invalidate(left, top, right, bottom);
11228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011229 }
11230
11231 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011232 int size = scrollBar.getSize(true);
11233 if (size <= 0) {
11234 size = cache.scrollBarSize;
11235 }
11236
Mike Reede8853fc2009-09-04 14:01:48 -040011237 scrollBar.setParameters(computeVerticalScrollRange(),
11238 computeVerticalScrollOffset(),
11239 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011240 switch (mVerticalScrollbarPosition) {
11241 default:
11242 case SCROLLBAR_POSITION_DEFAULT:
11243 case SCROLLBAR_POSITION_RIGHT:
11244 left = scrollX + width - size - (mUserPaddingRight & inside);
11245 break;
11246 case SCROLLBAR_POSITION_LEFT:
11247 left = scrollX + (mUserPaddingLeft & inside);
11248 break;
11249 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011250 top = scrollY + (mPaddingTop & inside);
11251 right = left + size;
11252 bottom = scrollY + height - (mUserPaddingBottom & inside);
11253 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11254 if (invalidate) {
11255 invalidate(left, top, right, bottom);
11256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 }
11258 }
11259 }
11260 }
Romain Guy8506ab42009-06-11 17:35:47 -070011261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011263 * 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 -080011264 * FastScroller is visible.
11265 * @return whether to temporarily hide the vertical scrollbar
11266 * @hide
11267 */
11268 protected boolean isVerticalScrollBarHidden() {
11269 return false;
11270 }
11271
11272 /**
11273 * <p>Draw the horizontal scrollbar if
11274 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011276 * @param canvas the canvas on which to draw the scrollbar
11277 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278 *
11279 * @see #isHorizontalScrollBarEnabled()
11280 * @see #computeHorizontalScrollRange()
11281 * @see #computeHorizontalScrollExtent()
11282 * @see #computeHorizontalScrollOffset()
11283 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011284 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011285 */
Romain Guy8fb95422010-08-17 18:38:51 -070011286 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11287 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011288 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011289 scrollBar.draw(canvas);
11290 }
Mike Reede8853fc2009-09-04 14:01:48 -040011291
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011292 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011293 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11294 * returns true.</p>
11295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 * @param canvas the canvas on which to draw the scrollbar
11297 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011298 *
11299 * @see #isVerticalScrollBarEnabled()
11300 * @see #computeVerticalScrollRange()
11301 * @see #computeVerticalScrollExtent()
11302 * @see #computeVerticalScrollOffset()
11303 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011304 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 */
Romain Guy8fb95422010-08-17 18:38:51 -070011306 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11307 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011308 scrollBar.setBounds(l, t, r, b);
11309 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 }
11311
11312 /**
11313 * Implement this to do your drawing.
11314 *
11315 * @param canvas the canvas on which the background will be drawn
11316 */
11317 protected void onDraw(Canvas canvas) {
11318 }
11319
11320 /*
11321 * Caller is responsible for calling requestLayout if necessary.
11322 * (This allows addViewInLayout to not request a new layout.)
11323 */
11324 void assignParent(ViewParent parent) {
11325 if (mParent == null) {
11326 mParent = parent;
11327 } else if (parent == null) {
11328 mParent = null;
11329 } else {
11330 throw new RuntimeException("view " + this + " being added, but"
11331 + " it already has a parent");
11332 }
11333 }
11334
11335 /**
11336 * This is called when the view is attached to a window. At this point it
11337 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011338 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11339 * however it may be called any time before the first onDraw -- including
11340 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011341 *
11342 * @see #onDetachedFromWindow()
11343 */
11344 protected void onAttachedToWindow() {
11345 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11346 mParent.requestTransparentRegion(this);
11347 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011348
Adam Powell8568c3a2010-04-19 14:26:11 -070011349 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11350 initialAwakenScrollBars();
11351 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11352 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011353
Chet Haasea9b61ac2010-12-20 07:40:25 -080011354 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011355
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011356 // Order is important here: LayoutDirection MUST be resolved before Padding
11357 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011358 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011359 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011360 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011361 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011362
Svetoslav Ganov42138042012-03-20 11:51:39 -070011363 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011364 if (isFocused()) {
11365 InputMethodManager imm = InputMethodManager.peekInstance();
11366 imm.focusIn(this);
11367 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011368
11369 if (mAttachInfo != null && mDisplayList != null) {
11370 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11371 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011372 }
Cibu Johny86666632010-02-22 13:01:02 -080011373
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011374 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011375 * @see #onScreenStateChanged(int)
11376 */
11377 void dispatchScreenStateChanged(int screenState) {
11378 onScreenStateChanged(screenState);
11379 }
11380
11381 /**
11382 * This method is called whenever the state of the screen this view is
11383 * attached to changes. A state change will usually occurs when the screen
11384 * turns on or off (whether it happens automatically or the user does it
11385 * manually.)
11386 *
11387 * @param screenState The new state of the screen. Can be either
11388 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11389 */
11390 public void onScreenStateChanged(int screenState) {
11391 }
11392
11393 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011394 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11395 */
11396 private boolean hasRtlSupport() {
11397 return mContext.getApplicationInfo().hasRtlSupport();
11398 }
11399
11400 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011401 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11402 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011403 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011404 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011405 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011406 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011407 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011408 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011409
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011410 if (hasRtlSupport()) {
11411 // Set resolved depending on layout direction
11412 switch (getLayoutDirection()) {
11413 case LAYOUT_DIRECTION_INHERIT:
11414 // If this is root view, no need to look at parent's layout dir.
11415 if (canResolveLayoutDirection()) {
11416 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011417
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011418 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11419 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11420 }
11421 } else {
11422 // Nothing to do, LTR by default
11423 }
11424 break;
11425 case LAYOUT_DIRECTION_RTL:
11426 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11427 break;
11428 case LAYOUT_DIRECTION_LOCALE:
11429 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011430 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11431 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011432 break;
11433 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011434 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011435 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011436 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011437
11438 // Set to resolved
11439 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011440 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011441 // Resolve padding
11442 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011443 }
11444
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011445 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011446 * Called when layout direction has been resolved.
11447 *
11448 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011449 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011450 */
11451 public void onResolvedLayoutDirectionChanged() {
11452 }
11453
11454 /**
11455 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011456 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011457 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011458 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011459 // If the user specified the absolute padding (either with android:padding or
11460 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11461 // use the default padding or the padding from the background drawable
11462 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011463 int resolvedLayoutDirection = getResolvedLayoutDirection();
11464 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011465 case LAYOUT_DIRECTION_RTL:
11466 // Start user padding override Right user padding. Otherwise, if Right user
11467 // padding is not defined, use the default Right padding. If Right user padding
11468 // is defined, just use it.
11469 if (mUserPaddingStart >= 0) {
11470 mUserPaddingRight = mUserPaddingStart;
11471 } else if (mUserPaddingRight < 0) {
11472 mUserPaddingRight = mPaddingRight;
11473 }
11474 if (mUserPaddingEnd >= 0) {
11475 mUserPaddingLeft = mUserPaddingEnd;
11476 } else if (mUserPaddingLeft < 0) {
11477 mUserPaddingLeft = mPaddingLeft;
11478 }
11479 break;
11480 case LAYOUT_DIRECTION_LTR:
11481 default:
11482 // Start user padding override Left user padding. Otherwise, if Left user
11483 // padding is not defined, use the default left padding. If Left user padding
11484 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011485 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011486 mUserPaddingLeft = mUserPaddingStart;
11487 } else if (mUserPaddingLeft < 0) {
11488 mUserPaddingLeft = mPaddingLeft;
11489 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011490 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011491 mUserPaddingRight = mUserPaddingEnd;
11492 } else if (mUserPaddingRight < 0) {
11493 mUserPaddingRight = mPaddingRight;
11494 }
11495 }
11496
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011497 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11498
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011499 if(isPaddingRelative()) {
11500 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11501 } else {
11502 recomputePadding();
11503 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011504 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011505 }
11506
11507 /**
11508 * Resolve padding depending on the layout direction. Subclasses that care about
11509 * padding resolution should override this method. The default implementation does
11510 * nothing.
11511 *
11512 * @param layoutDirection the direction of the layout
11513 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011514 * @see {@link #LAYOUT_DIRECTION_LTR}
11515 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011516 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011517 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011518 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011519 }
11520
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011521 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011522 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011523 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011524 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011525 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011526 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011527 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011528 switch (getLayoutDirection()) {
11529 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011530 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011531 default:
11532 return true;
11533 }
11534 }
11535
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011536 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011537 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11538 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011539 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011540 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011541 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011542 // Reset the current resolved bits
11543 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011544 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011545 // Reset also the text direction
11546 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011547 }
11548
11549 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011550 * Called during reset of resolved layout direction.
11551 *
11552 * Subclasses need to override this method to clear cached information that depends on the
11553 * resolved layout direction, or to inform child views that inherit their layout direction.
11554 *
11555 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011556 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011557 */
11558 public void onResolvedLayoutDirectionReset() {
11559 }
11560
11561 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011562 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011563 *
11564 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011565 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011566 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011567 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011568 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011569 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 }
11571
11572 /**
11573 * This is called when the view is detached from a window. At this point it
11574 * no longer has a surface for drawing.
11575 *
11576 * @see #onAttachedToWindow()
11577 */
11578 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011579 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011580
Romain Guya440b002010-02-24 15:57:54 -080011581 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011582 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011583 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011584 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011586 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011587
Romain Guya998dff2012-03-23 18:58:36 -070011588 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011589
11590 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011591 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011592 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011593 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011594 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011595 } else {
11596 if (mDisplayList != null) {
11597 // Should never happen
11598 mDisplayList.invalidate();
11599 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011600 }
11601
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011602 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011603
11604 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011605 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011606 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011607 }
11608
11609 /**
11610 * @return The number of times this view has been attached to a window
11611 */
11612 protected int getWindowAttachCount() {
11613 return mWindowAttachCount;
11614 }
11615
11616 /**
11617 * Retrieve a unique token identifying the window this view is attached to.
11618 * @return Return the window's token for use in
11619 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11620 */
11621 public IBinder getWindowToken() {
11622 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11623 }
11624
11625 /**
11626 * Retrieve a unique token identifying the top-level "real" window of
11627 * the window that this view is attached to. That is, this is like
11628 * {@link #getWindowToken}, except if the window this view in is a panel
11629 * window (attached to another containing window), then the token of
11630 * the containing window is returned instead.
11631 *
11632 * @return Returns the associated window token, either
11633 * {@link #getWindowToken()} or the containing window's token.
11634 */
11635 public IBinder getApplicationWindowToken() {
11636 AttachInfo ai = mAttachInfo;
11637 if (ai != null) {
11638 IBinder appWindowToken = ai.mPanelParentWindowToken;
11639 if (appWindowToken == null) {
11640 appWindowToken = ai.mWindowToken;
11641 }
11642 return appWindowToken;
11643 }
11644 return null;
11645 }
11646
11647 /**
11648 * Retrieve private session object this view hierarchy is using to
11649 * communicate with the window manager.
11650 * @return the session object to communicate with the window manager
11651 */
11652 /*package*/ IWindowSession getWindowSession() {
11653 return mAttachInfo != null ? mAttachInfo.mSession : null;
11654 }
11655
11656 /**
11657 * @param info the {@link android.view.View.AttachInfo} to associated with
11658 * this view
11659 */
11660 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11661 //System.out.println("Attached! " + this);
11662 mAttachInfo = info;
11663 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011664 // We will need to evaluate the drawable state at least once.
11665 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011666 if (mFloatingTreeObserver != null) {
11667 info.mTreeObserver.merge(mFloatingTreeObserver);
11668 mFloatingTreeObserver = null;
11669 }
11670 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11671 mAttachInfo.mScrollContainers.add(this);
11672 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11673 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011674 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011676
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011677 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011678 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011679 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011680 if (listeners != null && listeners.size() > 0) {
11681 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11682 // perform the dispatching. The iterator is a safe guard against listeners that
11683 // could mutate the list by calling the various add/remove methods. This prevents
11684 // the array from being modified while we iterate it.
11685 for (OnAttachStateChangeListener listener : listeners) {
11686 listener.onViewAttachedToWindow(this);
11687 }
11688 }
11689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011690 int vis = info.mWindowVisibility;
11691 if (vis != GONE) {
11692 onWindowVisibilityChanged(vis);
11693 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011694 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11695 // If nobody has evaluated the drawable state yet, then do it now.
11696 refreshDrawableState();
11697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 }
11699
11700 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 AttachInfo info = mAttachInfo;
11702 if (info != null) {
11703 int vis = info.mWindowVisibility;
11704 if (vis != GONE) {
11705 onWindowVisibilityChanged(GONE);
11706 }
11707 }
11708
11709 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011710
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011711 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011712 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011713 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011714 if (listeners != null && listeners.size() > 0) {
11715 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11716 // perform the dispatching. The iterator is a safe guard against listeners that
11717 // could mutate the list by calling the various add/remove methods. This prevents
11718 // the array from being modified while we iterate it.
11719 for (OnAttachStateChangeListener listener : listeners) {
11720 listener.onViewDetachedFromWindow(this);
11721 }
11722 }
11723
Romain Guy01d5edc2011-01-28 11:28:53 -080011724 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725 mAttachInfo.mScrollContainers.remove(this);
11726 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11727 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011729 mAttachInfo = null;
11730 }
11731
11732 /**
11733 * Store this view hierarchy's frozen state into the given container.
11734 *
11735 * @param container The SparseArray in which to save the view's state.
11736 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011737 * @see #restoreHierarchyState(android.util.SparseArray)
11738 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11739 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011740 */
11741 public void saveHierarchyState(SparseArray<Parcelable> container) {
11742 dispatchSaveInstanceState(container);
11743 }
11744
11745 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011746 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11747 * this view and its children. May be overridden to modify how freezing happens to a
11748 * 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 -080011749 *
11750 * @param container The SparseArray in which to save the view's state.
11751 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011752 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11753 * @see #saveHierarchyState(android.util.SparseArray)
11754 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 */
11756 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11757 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11758 mPrivateFlags &= ~SAVE_STATE_CALLED;
11759 Parcelable state = onSaveInstanceState();
11760 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11761 throw new IllegalStateException(
11762 "Derived class did not call super.onSaveInstanceState()");
11763 }
11764 if (state != null) {
11765 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11766 // + ": " + state);
11767 container.put(mID, state);
11768 }
11769 }
11770 }
11771
11772 /**
11773 * Hook allowing a view to generate a representation of its internal state
11774 * that can later be used to create a new instance with that same state.
11775 * This state should only contain information that is not persistent or can
11776 * not be reconstructed later. For example, you will never store your
11777 * current position on screen because that will be computed again when a
11778 * new instance of the view is placed in its view hierarchy.
11779 * <p>
11780 * Some examples of things you may store here: the current cursor position
11781 * in a text view (but usually not the text itself since that is stored in a
11782 * content provider or other persistent storage), the currently selected
11783 * item in a list view.
11784 *
11785 * @return Returns a Parcelable object containing the view's current dynamic
11786 * state, or null if there is nothing interesting to save. The
11787 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011788 * @see #onRestoreInstanceState(android.os.Parcelable)
11789 * @see #saveHierarchyState(android.util.SparseArray)
11790 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011791 * @see #setSaveEnabled(boolean)
11792 */
11793 protected Parcelable onSaveInstanceState() {
11794 mPrivateFlags |= SAVE_STATE_CALLED;
11795 return BaseSavedState.EMPTY_STATE;
11796 }
11797
11798 /**
11799 * Restore this view hierarchy's frozen state from the given container.
11800 *
11801 * @param container The SparseArray which holds previously frozen states.
11802 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011803 * @see #saveHierarchyState(android.util.SparseArray)
11804 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11805 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011806 */
11807 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11808 dispatchRestoreInstanceState(container);
11809 }
11810
11811 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011812 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11813 * state for this view and its children. May be overridden to modify how restoring
11814 * happens to a view's children; for example, some views may want to not store state
11815 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011816 *
11817 * @param container The SparseArray which holds previously saved state.
11818 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011819 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11820 * @see #restoreHierarchyState(android.util.SparseArray)
11821 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822 */
11823 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11824 if (mID != NO_ID) {
11825 Parcelable state = container.get(mID);
11826 if (state != null) {
11827 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11828 // + ": " + state);
11829 mPrivateFlags &= ~SAVE_STATE_CALLED;
11830 onRestoreInstanceState(state);
11831 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11832 throw new IllegalStateException(
11833 "Derived class did not call super.onRestoreInstanceState()");
11834 }
11835 }
11836 }
11837 }
11838
11839 /**
11840 * Hook allowing a view to re-apply a representation of its internal state that had previously
11841 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11842 * null state.
11843 *
11844 * @param state The frozen state that had previously been returned by
11845 * {@link #onSaveInstanceState}.
11846 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011847 * @see #onSaveInstanceState()
11848 * @see #restoreHierarchyState(android.util.SparseArray)
11849 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 */
11851 protected void onRestoreInstanceState(Parcelable state) {
11852 mPrivateFlags |= SAVE_STATE_CALLED;
11853 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011854 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11855 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011856 + "when two views of different type have the same id in the same hierarchy. "
11857 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011858 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 }
11860 }
11861
11862 /**
11863 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11864 *
11865 * @return the drawing start time in milliseconds
11866 */
11867 public long getDrawingTime() {
11868 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11869 }
11870
11871 /**
11872 * <p>Enables or disables the duplication of the parent's state into this view. When
11873 * duplication is enabled, this view gets its drawable state from its parent rather
11874 * than from its own internal properties.</p>
11875 *
11876 * <p>Note: in the current implementation, setting this property to true after the
11877 * view was added to a ViewGroup might have no effect at all. This property should
11878 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11879 *
11880 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11881 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011882 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011883 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11884 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011885 *
11886 * @param enabled True to enable duplication of the parent's drawable state, false
11887 * to disable it.
11888 *
11889 * @see #getDrawableState()
11890 * @see #isDuplicateParentStateEnabled()
11891 */
11892 public void setDuplicateParentStateEnabled(boolean enabled) {
11893 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11894 }
11895
11896 /**
11897 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11898 *
11899 * @return True if this view's drawable state is duplicated from the parent,
11900 * false otherwise
11901 *
11902 * @see #getDrawableState()
11903 * @see #setDuplicateParentStateEnabled(boolean)
11904 */
11905 public boolean isDuplicateParentStateEnabled() {
11906 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11907 }
11908
11909 /**
Romain Guy171c5922011-01-06 10:04:23 -080011910 * <p>Specifies the type of layer backing this view. The layer can be
11911 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11912 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011913 *
Romain Guy171c5922011-01-06 10:04:23 -080011914 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11915 * instance that controls how the layer is composed on screen. The following
11916 * properties of the paint are taken into account when composing the layer:</p>
11917 * <ul>
11918 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11919 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11920 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11921 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011922 *
Romain Guy171c5922011-01-06 10:04:23 -080011923 * <p>If this view has an alpha value set to < 1.0 by calling
11924 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11925 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11926 * equivalent to setting a hardware layer on this view and providing a paint with
11927 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011928 *
Romain Guy171c5922011-01-06 10:04:23 -080011929 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11930 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11931 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011932 *
Romain Guy171c5922011-01-06 10:04:23 -080011933 * @param layerType The ype of layer to use with this view, must be one of
11934 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11935 * {@link #LAYER_TYPE_HARDWARE}
11936 * @param paint The paint used to compose the layer. This argument is optional
11937 * and can be null. It is ignored when the layer type is
11938 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011939 *
11940 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011941 * @see #LAYER_TYPE_NONE
11942 * @see #LAYER_TYPE_SOFTWARE
11943 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011944 * @see #setAlpha(float)
11945 *
Romain Guy171c5922011-01-06 10:04:23 -080011946 * @attr ref android.R.styleable#View_layerType
11947 */
11948 public void setLayerType(int layerType, Paint paint) {
11949 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011950 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011951 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11952 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011953
Romain Guyd6cd5722011-01-17 14:42:41 -080011954 if (layerType == mLayerType) {
11955 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11956 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011957 invalidateParentCaches();
11958 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011959 }
11960 return;
11961 }
Romain Guy171c5922011-01-06 10:04:23 -080011962
11963 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011964 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011965 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011966 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011967 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011968 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011969 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011970 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011971 default:
11972 break;
Romain Guy171c5922011-01-06 10:04:23 -080011973 }
11974
11975 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011976 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11977 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11978 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011979
Romain Guy0fd89bf2011-01-26 15:41:30 -080011980 invalidateParentCaches();
11981 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011982 }
11983
11984 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011985 * Indicates whether this view has a static layer. A view with layer type
11986 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11987 * dynamic.
11988 */
11989 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011990 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011991 }
11992
11993 /**
Romain Guy171c5922011-01-06 10:04:23 -080011994 * Indicates what type of layer is currently associated with this view. By default
11995 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11996 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11997 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011998 *
Romain Guy171c5922011-01-06 10:04:23 -080011999 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12000 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012001 *
12002 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012003 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012004 * @see #LAYER_TYPE_NONE
12005 * @see #LAYER_TYPE_SOFTWARE
12006 * @see #LAYER_TYPE_HARDWARE
12007 */
12008 public int getLayerType() {
12009 return mLayerType;
12010 }
Joe Malin32736f02011-01-19 16:14:20 -080012011
Romain Guy6c319ca2011-01-11 14:29:25 -080012012 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012013 * Forces this view's layer to be created and this view to be rendered
12014 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12015 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012016 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012017 * This method can for instance be used to render a view into its layer before
12018 * starting an animation. If this view is complex, rendering into the layer
12019 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012020 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012021 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012022 *
12023 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012024 */
12025 public void buildLayer() {
12026 if (mLayerType == LAYER_TYPE_NONE) return;
12027
12028 if (mAttachInfo == null) {
12029 throw new IllegalStateException("This view must be attached to a window first");
12030 }
12031
12032 switch (mLayerType) {
12033 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012034 if (mAttachInfo.mHardwareRenderer != null &&
12035 mAttachInfo.mHardwareRenderer.isEnabled() &&
12036 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012037 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012038 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012039 break;
12040 case LAYER_TYPE_SOFTWARE:
12041 buildDrawingCache(true);
12042 break;
12043 }
12044 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012045
Romain Guy9c4b79a2011-11-10 19:23:58 -080012046 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12047 void flushLayer() {
12048 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12049 mHardwareLayer.flush();
12050 }
12051 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012052
12053 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012054 * <p>Returns a hardware layer that can be used to draw this view again
12055 * without executing its draw method.</p>
12056 *
12057 * @return A HardwareLayer ready to render, or null if an error occurred.
12058 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012059 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012060 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12061 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012062 return null;
12063 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012064
Romain Guy9c4b79a2011-11-10 19:23:58 -080012065 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012066
12067 final int width = mRight - mLeft;
12068 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012069
Romain Guy6c319ca2011-01-11 14:29:25 -080012070 if (width == 0 || height == 0) {
12071 return null;
12072 }
12073
12074 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12075 if (mHardwareLayer == null) {
12076 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12077 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012078 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012079 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12080 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012081 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012082 }
12083
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012084 // The layer is not valid if the underlying GPU resources cannot be allocated
12085 if (!mHardwareLayer.isValid()) {
12086 return null;
12087 }
12088
Chet Haasea1cff502012-02-21 13:43:44 -080012089 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012090 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012091 }
12092
12093 return mHardwareLayer;
12094 }
Romain Guy171c5922011-01-06 10:04:23 -080012095
Romain Guy589b0bb2011-10-10 13:57:47 -070012096 /**
12097 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012098 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012099 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012100 *
12101 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012102 * @see #LAYER_TYPE_HARDWARE
12103 */
Romain Guya998dff2012-03-23 18:58:36 -070012104 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012105 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012106 AttachInfo info = mAttachInfo;
12107 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012108 info.mHardwareRenderer.isEnabled() &&
12109 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012110 mHardwareLayer.destroy();
12111 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012112
Romain Guy9c4b79a2011-11-10 19:23:58 -080012113 invalidate(true);
12114 invalidateParentCaches();
12115 }
Romain Guy65b345f2011-07-27 18:51:50 -070012116 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012117 }
Romain Guy65b345f2011-07-27 18:51:50 -070012118 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012119 }
12120
Romain Guy171c5922011-01-06 10:04:23 -080012121 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012122 * Destroys all hardware rendering resources. This method is invoked
12123 * when the system needs to reclaim resources. Upon execution of this
12124 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012125 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012126 * Note: you <strong>must</strong> call
12127 * <code>super.destroyHardwareResources()</code> when overriding
12128 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012129 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012130 * @hide
12131 */
12132 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012133 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012134 }
12135
12136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12138 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12139 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12140 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12141 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12142 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012143 *
Romain Guy171c5922011-01-06 10:04:23 -080012144 * <p>Enabling the drawing cache is similar to
12145 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012146 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12147 * drawing cache has no effect on rendering because the system uses a different mechanism
12148 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12149 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12150 * for information on how to enable software and hardware layers.</p>
12151 *
12152 * <p>This API can be used to manually generate
12153 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12154 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 *
12156 * @param enabled true to enable the drawing cache, false otherwise
12157 *
12158 * @see #isDrawingCacheEnabled()
12159 * @see #getDrawingCache()
12160 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012161 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 */
12163 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012164 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012165 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12166 }
12167
12168 /**
12169 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12170 *
12171 * @return true if the drawing cache is enabled
12172 *
12173 * @see #setDrawingCacheEnabled(boolean)
12174 * @see #getDrawingCache()
12175 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012176 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177 public boolean isDrawingCacheEnabled() {
12178 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12179 }
12180
12181 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012182 * Debugging utility which recursively outputs the dirty state of a view and its
12183 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012184 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012185 * @hide
12186 */
Romain Guy676b1732011-02-14 14:45:33 -080012187 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012188 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12189 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12190 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12191 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12192 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12193 if (clear) {
12194 mPrivateFlags &= clearMask;
12195 }
12196 if (this instanceof ViewGroup) {
12197 ViewGroup parent = (ViewGroup) this;
12198 final int count = parent.getChildCount();
12199 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012200 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012201 child.outputDirtyFlags(indent + " ", clear, clearMask);
12202 }
12203 }
12204 }
12205
12206 /**
12207 * This method is used by ViewGroup to cause its children to restore or recreate their
12208 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12209 * to recreate its own display list, which would happen if it went through the normal
12210 * draw/dispatchDraw mechanisms.
12211 *
12212 * @hide
12213 */
12214 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012215
12216 /**
12217 * A view that is not attached or hardware accelerated cannot create a display list.
12218 * This method checks these conditions and returns the appropriate result.
12219 *
12220 * @return true if view has the ability to create a display list, false otherwise.
12221 *
12222 * @hide
12223 */
12224 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012225 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012226 }
Joe Malin32736f02011-01-19 16:14:20 -080012227
Chet Haasedaf98e92011-01-10 14:10:36 -080012228 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012229 * @return The HardwareRenderer associated with that view or null if hardware rendering
12230 * is not supported or this this has not been attached to a window.
12231 *
12232 * @hide
12233 */
12234 public HardwareRenderer getHardwareRenderer() {
12235 if (mAttachInfo != null) {
12236 return mAttachInfo.mHardwareRenderer;
12237 }
12238 return null;
12239 }
12240
12241 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012242 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12243 * Otherwise, the same display list will be returned (after having been rendered into
12244 * along the way, depending on the invalidation state of the view).
12245 *
12246 * @param displayList The previous version of this displayList, could be null.
12247 * @param isLayer Whether the requester of the display list is a layer. If so,
12248 * the view will avoid creating a layer inside the resulting display list.
12249 * @return A new or reused DisplayList object.
12250 */
12251 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12252 if (!canHaveDisplayList()) {
12253 return null;
12254 }
12255
12256 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12257 displayList == null || !displayList.isValid() ||
12258 (!isLayer && mRecreateDisplayList))) {
12259 // Don't need to recreate the display list, just need to tell our
12260 // children to restore/recreate theirs
12261 if (displayList != null && displayList.isValid() &&
12262 !isLayer && !mRecreateDisplayList) {
12263 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12264 mPrivateFlags &= ~DIRTY_MASK;
12265 dispatchGetDisplayList();
12266
12267 return displayList;
12268 }
12269
12270 if (!isLayer) {
12271 // If we got here, we're recreating it. Mark it as such to ensure that
12272 // we copy in child display lists into ours in drawChild()
12273 mRecreateDisplayList = true;
12274 }
12275 if (displayList == null) {
12276 final String name = getClass().getSimpleName();
12277 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12278 // If we're creating a new display list, make sure our parent gets invalidated
12279 // since they will need to recreate their display list to account for this
12280 // new child display list.
12281 invalidateParentCaches();
12282 }
12283
12284 boolean caching = false;
12285 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012286 int width = mRight - mLeft;
12287 int height = mBottom - mTop;
12288
12289 try {
12290 canvas.setViewport(width, height);
12291 // The dirty rect should always be null for a display list
12292 canvas.onPreDraw(null);
12293 int layerType = (
12294 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12295 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012296 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012297 if (layerType == LAYER_TYPE_HARDWARE) {
12298 final HardwareLayer layer = getHardwareLayer();
12299 if (layer != null && layer.isValid()) {
12300 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12301 } else {
12302 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12303 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12304 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12305 }
12306 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012307 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012308 buildDrawingCache(true);
12309 Bitmap cache = getDrawingCache(true);
12310 if (cache != null) {
12311 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12312 caching = true;
12313 }
Chet Haasea1cff502012-02-21 13:43:44 -080012314 }
Chet Haasea1cff502012-02-21 13:43:44 -080012315 } else {
12316
12317 computeScroll();
12318
Chet Haasea1cff502012-02-21 13:43:44 -080012319 canvas.translate(-mScrollX, -mScrollY);
12320 if (!isLayer) {
12321 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12322 mPrivateFlags &= ~DIRTY_MASK;
12323 }
12324
12325 // Fast path for layouts with no backgrounds
12326 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12327 dispatchDraw(canvas);
12328 } else {
12329 draw(canvas);
12330 }
12331 }
12332 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012333 canvas.onPostDraw();
12334
12335 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012336 displayList.setCaching(caching);
12337 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012338 displayList.setLeftTopRightBottom(0, 0, width, height);
12339 } else {
12340 setDisplayListProperties(displayList);
12341 }
12342 }
12343 } else if (!isLayer) {
12344 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12345 mPrivateFlags &= ~DIRTY_MASK;
12346 }
12347
12348 return displayList;
12349 }
12350
12351 /**
12352 * Get the DisplayList for the HardwareLayer
12353 *
12354 * @param layer The HardwareLayer whose DisplayList we want
12355 * @return A DisplayList fopr the specified HardwareLayer
12356 */
12357 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12358 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12359 layer.setDisplayList(displayList);
12360 return displayList;
12361 }
12362
12363
12364 /**
Romain Guyb051e892010-09-28 19:09:36 -070012365 * <p>Returns a display list that can be used to draw this view again
12366 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012367 *
Romain Guyb051e892010-09-28 19:09:36 -070012368 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012369 *
12370 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012371 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012372 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012373 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012374 return mDisplayList;
12375 }
12376
12377 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012378 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012379 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012380 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012381 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012382 * @see #getDrawingCache(boolean)
12383 */
12384 public Bitmap getDrawingCache() {
12385 return getDrawingCache(false);
12386 }
12387
12388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12390 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12391 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12392 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12393 * request the drawing cache by calling this method and draw it on screen if the
12394 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012395 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012396 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12397 * this method will create a bitmap of the same size as this view. Because this bitmap
12398 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12399 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12400 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12401 * size than the view. This implies that your application must be able to handle this
12402 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012403 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012404 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12405 * the current density of the screen when the application is in compatibility
12406 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012407 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012408 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012410 * @see #setDrawingCacheEnabled(boolean)
12411 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012412 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 * @see #destroyDrawingCache()
12414 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012415 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012416 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12417 return null;
12418 }
12419 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012420 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 }
Romain Guy02890fd2010-08-06 17:58:44 -070012422 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 }
12424
12425 /**
12426 * <p>Frees the resources used by the drawing cache. If you call
12427 * {@link #buildDrawingCache()} manually without calling
12428 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12429 * should cleanup the cache with this method afterwards.</p>
12430 *
12431 * @see #setDrawingCacheEnabled(boolean)
12432 * @see #buildDrawingCache()
12433 * @see #getDrawingCache()
12434 */
12435 public void destroyDrawingCache() {
12436 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012437 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012438 mDrawingCache = null;
12439 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012440 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012441 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012442 mUnscaledDrawingCache = null;
12443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012444 }
12445
12446 /**
12447 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012448 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 * view will always be drawn on top of a solid color.
12450 *
12451 * @param color The background color to use for the drawing cache's bitmap
12452 *
12453 * @see #setDrawingCacheEnabled(boolean)
12454 * @see #buildDrawingCache()
12455 * @see #getDrawingCache()
12456 */
12457 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012458 if (color != mDrawingCacheBackgroundColor) {
12459 mDrawingCacheBackgroundColor = color;
12460 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012462 }
12463
12464 /**
12465 * @see #setDrawingCacheBackgroundColor(int)
12466 *
12467 * @return The background color to used for the drawing cache's bitmap
12468 */
12469 public int getDrawingCacheBackgroundColor() {
12470 return mDrawingCacheBackgroundColor;
12471 }
12472
12473 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012474 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012475 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012476 * @see #buildDrawingCache(boolean)
12477 */
12478 public void buildDrawingCache() {
12479 buildDrawingCache(false);
12480 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012481
Romain Guyfbd8f692009-06-26 14:51:58 -070012482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12484 *
12485 * <p>If you call {@link #buildDrawingCache()} manually without calling
12486 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12487 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012488 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012489 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12490 * this method will create a bitmap of the same size as this view. Because this bitmap
12491 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12492 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12493 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12494 * size than the view. This implies that your application must be able to handle this
12495 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012496 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012497 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12498 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012499 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012500 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012501 *
12502 * @see #getDrawingCache()
12503 * @see #destroyDrawingCache()
12504 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012505 public void buildDrawingCache(boolean autoScale) {
12506 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012507 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012508 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509
Romain Guy8506ab42009-06-11 17:35:47 -070012510 int width = mRight - mLeft;
12511 int height = mBottom - mTop;
12512
12513 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012514 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012515
Romain Guye1123222009-06-29 14:24:56 -070012516 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012517 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12518 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012520
12521 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012522 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012523 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012524
12525 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012526 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012527 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12529 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012530 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 return;
12532 }
12533
12534 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012535 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536
12537 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012538 Bitmap.Config quality;
12539 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012540 // Never pick ARGB_4444 because it looks awful
12541 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012542 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12543 case DRAWING_CACHE_QUALITY_AUTO:
12544 quality = Bitmap.Config.ARGB_8888;
12545 break;
12546 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012547 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012548 break;
12549 case DRAWING_CACHE_QUALITY_HIGH:
12550 quality = Bitmap.Config.ARGB_8888;
12551 break;
12552 default:
12553 quality = Bitmap.Config.ARGB_8888;
12554 break;
12555 }
12556 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012557 // Optimization for translucent windows
12558 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012559 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 }
12561
12562 // Try to cleanup memory
12563 if (bitmap != null) bitmap.recycle();
12564
12565 try {
12566 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012567 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012568 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012569 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012570 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012571 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012572 }
Adam Powell26153a32010-11-08 15:22:27 -080012573 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012574 } catch (OutOfMemoryError e) {
12575 // If there is not enough memory to create the bitmap cache, just
12576 // ignore the issue as bitmap caches are not required to draw the
12577 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012578 if (autoScale) {
12579 mDrawingCache = null;
12580 } else {
12581 mUnscaledDrawingCache = null;
12582 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012583 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012584 return;
12585 }
12586
12587 clear = drawingCacheBackgroundColor != 0;
12588 }
12589
12590 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012591 if (attachInfo != null) {
12592 canvas = attachInfo.mCanvas;
12593 if (canvas == null) {
12594 canvas = new Canvas();
12595 }
12596 canvas.setBitmap(bitmap);
12597 // Temporarily clobber the cached Canvas in case one of our children
12598 // is also using a drawing cache. Without this, the children would
12599 // steal the canvas by attaching their own bitmap to it and bad, bad
12600 // thing would happen (invisible views, corrupted drawings, etc.)
12601 attachInfo.mCanvas = null;
12602 } else {
12603 // This case should hopefully never or seldom happen
12604 canvas = new Canvas(bitmap);
12605 }
12606
12607 if (clear) {
12608 bitmap.eraseColor(drawingCacheBackgroundColor);
12609 }
12610
12611 computeScroll();
12612 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012613
Romain Guye1123222009-06-29 14:24:56 -070012614 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012615 final float scale = attachInfo.mApplicationScale;
12616 canvas.scale(scale, scale);
12617 }
Joe Malin32736f02011-01-19 16:14:20 -080012618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619 canvas.translate(-mScrollX, -mScrollY);
12620
Romain Guy5bcdff42009-05-14 21:27:18 -070012621 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012622 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12623 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012624 mPrivateFlags |= DRAWING_CACHE_VALID;
12625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012626
12627 // Fast path for layouts with no backgrounds
12628 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012629 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012630 dispatchDraw(canvas);
12631 } else {
12632 draw(canvas);
12633 }
12634
12635 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012636 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012637
12638 if (attachInfo != null) {
12639 // Restore the cached Canvas for our siblings
12640 attachInfo.mCanvas = canvas;
12641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012642 }
12643 }
12644
12645 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012646 * Create a snapshot of the view into a bitmap. We should probably make
12647 * some form of this public, but should think about the API.
12648 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012649 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012650 int width = mRight - mLeft;
12651 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012652
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012653 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012654 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012655 width = (int) ((width * scale) + 0.5f);
12656 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012657
Romain Guy8c11e312009-09-14 15:15:30 -070012658 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012659 if (bitmap == null) {
12660 throw new OutOfMemoryError();
12661 }
12662
Romain Guyc529d8d2011-09-06 15:01:39 -070012663 Resources resources = getResources();
12664 if (resources != null) {
12665 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12666 }
Joe Malin32736f02011-01-19 16:14:20 -080012667
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012668 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012669 if (attachInfo != null) {
12670 canvas = attachInfo.mCanvas;
12671 if (canvas == null) {
12672 canvas = new Canvas();
12673 }
12674 canvas.setBitmap(bitmap);
12675 // Temporarily clobber the cached Canvas in case one of our children
12676 // is also using a drawing cache. Without this, the children would
12677 // steal the canvas by attaching their own bitmap to it and bad, bad
12678 // things would happen (invisible views, corrupted drawings, etc.)
12679 attachInfo.mCanvas = null;
12680 } else {
12681 // This case should hopefully never or seldom happen
12682 canvas = new Canvas(bitmap);
12683 }
12684
Romain Guy5bcdff42009-05-14 21:27:18 -070012685 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012686 bitmap.eraseColor(backgroundColor);
12687 }
12688
12689 computeScroll();
12690 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012691 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012692 canvas.translate(-mScrollX, -mScrollY);
12693
Romain Guy5bcdff42009-05-14 21:27:18 -070012694 // Temporarily remove the dirty mask
12695 int flags = mPrivateFlags;
12696 mPrivateFlags &= ~DIRTY_MASK;
12697
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012698 // Fast path for layouts with no backgrounds
12699 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12700 dispatchDraw(canvas);
12701 } else {
12702 draw(canvas);
12703 }
12704
Romain Guy5bcdff42009-05-14 21:27:18 -070012705 mPrivateFlags = flags;
12706
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012707 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012708 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012709
12710 if (attachInfo != null) {
12711 // Restore the cached Canvas for our siblings
12712 attachInfo.mCanvas = canvas;
12713 }
Romain Guy8506ab42009-06-11 17:35:47 -070012714
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012715 return bitmap;
12716 }
12717
12718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012719 * Indicates whether this View is currently in edit mode. A View is usually
12720 * in edit mode when displayed within a developer tool. For instance, if
12721 * this View is being drawn by a visual user interface builder, this method
12722 * should return true.
12723 *
12724 * Subclasses should check the return value of this method to provide
12725 * different behaviors if their normal behavior might interfere with the
12726 * host environment. For instance: the class spawns a thread in its
12727 * constructor, the drawing code relies on device-specific features, etc.
12728 *
12729 * This method is usually checked in the drawing code of custom widgets.
12730 *
12731 * @return True if this View is in edit mode, false otherwise.
12732 */
12733 public boolean isInEditMode() {
12734 return false;
12735 }
12736
12737 /**
12738 * If the View draws content inside its padding and enables fading edges,
12739 * it needs to support padding offsets. Padding offsets are added to the
12740 * fading edges to extend the length of the fade so that it covers pixels
12741 * drawn inside the padding.
12742 *
12743 * Subclasses of this class should override this method if they need
12744 * to draw content inside the padding.
12745 *
12746 * @return True if padding offset must be applied, false otherwise.
12747 *
12748 * @see #getLeftPaddingOffset()
12749 * @see #getRightPaddingOffset()
12750 * @see #getTopPaddingOffset()
12751 * @see #getBottomPaddingOffset()
12752 *
12753 * @since CURRENT
12754 */
12755 protected boolean isPaddingOffsetRequired() {
12756 return false;
12757 }
12758
12759 /**
12760 * Amount by which to extend the left fading region. Called only when
12761 * {@link #isPaddingOffsetRequired()} returns true.
12762 *
12763 * @return The left padding offset in pixels.
12764 *
12765 * @see #isPaddingOffsetRequired()
12766 *
12767 * @since CURRENT
12768 */
12769 protected int getLeftPaddingOffset() {
12770 return 0;
12771 }
12772
12773 /**
12774 * Amount by which to extend the right fading region. Called only when
12775 * {@link #isPaddingOffsetRequired()} returns true.
12776 *
12777 * @return The right padding offset in pixels.
12778 *
12779 * @see #isPaddingOffsetRequired()
12780 *
12781 * @since CURRENT
12782 */
12783 protected int getRightPaddingOffset() {
12784 return 0;
12785 }
12786
12787 /**
12788 * Amount by which to extend the top fading region. Called only when
12789 * {@link #isPaddingOffsetRequired()} returns true.
12790 *
12791 * @return The top padding offset in pixels.
12792 *
12793 * @see #isPaddingOffsetRequired()
12794 *
12795 * @since CURRENT
12796 */
12797 protected int getTopPaddingOffset() {
12798 return 0;
12799 }
12800
12801 /**
12802 * Amount by which to extend the bottom fading region. Called only when
12803 * {@link #isPaddingOffsetRequired()} returns true.
12804 *
12805 * @return The bottom padding offset in pixels.
12806 *
12807 * @see #isPaddingOffsetRequired()
12808 *
12809 * @since CURRENT
12810 */
12811 protected int getBottomPaddingOffset() {
12812 return 0;
12813 }
12814
12815 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012816 * @hide
12817 * @param offsetRequired
12818 */
12819 protected int getFadeTop(boolean offsetRequired) {
12820 int top = mPaddingTop;
12821 if (offsetRequired) top += getTopPaddingOffset();
12822 return top;
12823 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012824
Romain Guyf2fc4602011-07-19 15:20:03 -070012825 /**
12826 * @hide
12827 * @param offsetRequired
12828 */
12829 protected int getFadeHeight(boolean offsetRequired) {
12830 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012831 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012832 return mBottom - mTop - mPaddingBottom - padding;
12833 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012834
Romain Guyf2fc4602011-07-19 15:20:03 -070012835 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012836 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012837 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012838 *
Romain Guy2bffd262010-09-12 17:40:02 -070012839 * <p>Even if this method returns true, it does not mean that every call
12840 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12841 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012842 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012843 * window is hardware accelerated,
12844 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12845 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012846 *
Romain Guy2bffd262010-09-12 17:40:02 -070012847 * @return True if the view is attached to a window and the window is
12848 * hardware accelerated; false in any other case.
12849 */
12850 public boolean isHardwareAccelerated() {
12851 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12852 }
Joe Malin32736f02011-01-19 16:14:20 -080012853
Romain Guy2bffd262010-09-12 17:40:02 -070012854 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012855 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12856 * case of an active Animation being run on the view.
12857 */
12858 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12859 Animation a, boolean scalingRequired) {
12860 Transformation invalidationTransform;
12861 final int flags = parent.mGroupFlags;
12862 final boolean initialized = a.isInitialized();
12863 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012864 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012865 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012866 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012867 onAnimationStart();
12868 }
12869
12870 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12871 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12872 if (parent.mInvalidationTransformation == null) {
12873 parent.mInvalidationTransformation = new Transformation();
12874 }
12875 invalidationTransform = parent.mInvalidationTransformation;
12876 a.getTransformation(drawingTime, invalidationTransform, 1f);
12877 } else {
12878 invalidationTransform = parent.mChildTransformation;
12879 }
Romain Guy393a52c2012-05-22 20:21:08 -070012880
Chet Haasebcca79a2012-02-14 08:45:14 -080012881 if (more) {
12882 if (!a.willChangeBounds()) {
12883 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12884 parent.FLAG_OPTIMIZE_INVALIDATE) {
12885 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12886 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12887 // The child need to draw an animation, potentially offscreen, so
12888 // make sure we do not cancel invalidate requests
12889 parent.mPrivateFlags |= DRAW_ANIMATION;
12890 parent.invalidate(mLeft, mTop, mRight, mBottom);
12891 }
12892 } else {
12893 if (parent.mInvalidateRegion == null) {
12894 parent.mInvalidateRegion = new RectF();
12895 }
12896 final RectF region = parent.mInvalidateRegion;
12897 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12898 invalidationTransform);
12899
12900 // The child need to draw an animation, potentially offscreen, so
12901 // make sure we do not cancel invalidate requests
12902 parent.mPrivateFlags |= DRAW_ANIMATION;
12903
12904 final int left = mLeft + (int) region.left;
12905 final int top = mTop + (int) region.top;
12906 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12907 top + (int) (region.height() + .5f));
12908 }
12909 }
12910 return more;
12911 }
12912
Chet Haasea1cff502012-02-21 13:43:44 -080012913 /**
12914 * This method is called by getDisplayList() when a display list is created or re-rendered.
12915 * It sets or resets the current value of all properties on that display list (resetting is
12916 * necessary when a display list is being re-created, because we need to make sure that
12917 * previously-set transform values
12918 */
12919 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012920 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012921 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012922 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012923 if (mParent instanceof ViewGroup) {
12924 displayList.setClipChildren(
12925 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12926 }
Chet Haase9420abd2012-03-29 16:28:32 -070012927 float alpha = 1;
12928 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12929 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12930 ViewGroup parentVG = (ViewGroup) mParent;
12931 final boolean hasTransform =
12932 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12933 if (hasTransform) {
12934 Transformation transform = parentVG.mChildTransformation;
12935 final int transformType = parentVG.mChildTransformation.getTransformationType();
12936 if (transformType != Transformation.TYPE_IDENTITY) {
12937 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12938 alpha = transform.getAlpha();
12939 }
12940 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12941 displayList.setStaticMatrix(transform.getMatrix());
12942 }
12943 }
12944 }
Chet Haasea1cff502012-02-21 13:43:44 -080012945 }
12946 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012947 alpha *= mTransformationInfo.mAlpha;
12948 if (alpha < 1) {
12949 final int multipliedAlpha = (int) (255 * alpha);
12950 if (onSetAlpha(multipliedAlpha)) {
12951 alpha = 1;
12952 }
12953 }
12954 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012955 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12956 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12957 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12958 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012959 if (mTransformationInfo.mCamera == null) {
12960 mTransformationInfo.mCamera = new Camera();
12961 mTransformationInfo.matrix3D = new Matrix();
12962 }
12963 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012964 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12965 displayList.setPivotX(getPivotX());
12966 displayList.setPivotY(getPivotY());
12967 }
Chet Haase9420abd2012-03-29 16:28:32 -070012968 } else if (alpha < 1) {
12969 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012970 }
12971 }
12972 }
12973
Chet Haasebcca79a2012-02-14 08:45:14 -080012974 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012975 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12976 * This draw() method is an implementation detail and is not intended to be overridden or
12977 * to be called from anywhere else other than ViewGroup.drawChild().
12978 */
12979 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012980 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012981 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012982 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012983 final int flags = parent.mGroupFlags;
12984
Chet Haasea1cff502012-02-21 13:43:44 -080012985 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012986 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012987 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012988 }
12989
12990 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012991 boolean concatMatrix = false;
12992
12993 boolean scalingRequired = false;
12994 boolean caching;
12995 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12996
12997 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012998 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12999 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013000 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013001 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013002 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13003 } else {
13004 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13005 }
13006
Chet Haasebcca79a2012-02-14 08:45:14 -080013007 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013008 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013009 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013010 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013011 if (concatMatrix) {
13012 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
13013 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013014 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013015 } else {
13016 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
13017 mDisplayList != null) {
13018 // No longer animating: clear out old animation matrix
13019 mDisplayList.setAnimationMatrix(null);
13020 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
13021 }
13022 if (!useDisplayListProperties &&
13023 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13024 final boolean hasTransform =
13025 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13026 if (hasTransform) {
13027 final int transformType = parent.mChildTransformation.getTransformationType();
13028 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13029 parent.mChildTransformation : null;
13030 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13031 }
Chet Haase64a48c12012-02-13 16:33:29 -080013032 }
13033 }
13034
13035 concatMatrix |= !childHasIdentityMatrix;
13036
13037 // Sets the flag as early as possible to allow draw() implementations
13038 // to call invalidate() successfully when doing animations
13039 mPrivateFlags |= DRAWN;
13040
Chet Haasebcca79a2012-02-14 08:45:14 -080013041 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013042 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013043 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013044 return more;
13045 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013046 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013047
13048 if (hardwareAccelerated) {
13049 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13050 // retain the flag's value temporarily in the mRecreateDisplayList flag
13051 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13052 mPrivateFlags &= ~INVALIDATED;
13053 }
13054
13055 computeScroll();
13056
13057 final int sx = mScrollX;
13058 final int sy = mScrollY;
13059
13060 DisplayList displayList = null;
13061 Bitmap cache = null;
13062 boolean hasDisplayList = false;
13063 if (caching) {
13064 if (!hardwareAccelerated) {
13065 if (layerType != LAYER_TYPE_NONE) {
13066 layerType = LAYER_TYPE_SOFTWARE;
13067 buildDrawingCache(true);
13068 }
13069 cache = getDrawingCache(true);
13070 } else {
13071 switch (layerType) {
13072 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013073 if (useDisplayListProperties) {
13074 hasDisplayList = canHaveDisplayList();
13075 } else {
13076 buildDrawingCache(true);
13077 cache = getDrawingCache(true);
13078 }
Chet Haase64a48c12012-02-13 16:33:29 -080013079 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013080 case LAYER_TYPE_HARDWARE:
13081 if (useDisplayListProperties) {
13082 hasDisplayList = canHaveDisplayList();
13083 }
13084 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013085 case LAYER_TYPE_NONE:
13086 // Delay getting the display list until animation-driven alpha values are
13087 // set up and possibly passed on to the view
13088 hasDisplayList = canHaveDisplayList();
13089 break;
13090 }
13091 }
13092 }
Chet Haasea1cff502012-02-21 13:43:44 -080013093 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013094 if (useDisplayListProperties) {
13095 displayList = getDisplayList();
13096 if (!displayList.isValid()) {
13097 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13098 // to getDisplayList(), the display list will be marked invalid and we should not
13099 // try to use it again.
13100 displayList = null;
13101 hasDisplayList = false;
13102 useDisplayListProperties = false;
13103 }
13104 }
Chet Haase64a48c12012-02-13 16:33:29 -080013105
13106 final boolean hasNoCache = cache == null || hasDisplayList;
13107 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13108 layerType != LAYER_TYPE_HARDWARE;
13109
Chet Haasea1cff502012-02-21 13:43:44 -080013110 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013111 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013112 restoreTo = canvas.save();
13113 }
Chet Haase64a48c12012-02-13 16:33:29 -080013114 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013115 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013116 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013117 if (!useDisplayListProperties) {
13118 canvas.translate(mLeft, mTop);
13119 }
Chet Haase64a48c12012-02-13 16:33:29 -080013120 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013121 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013122 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013123 restoreTo = canvas.save();
13124 }
Chet Haase64a48c12012-02-13 16:33:29 -080013125 // mAttachInfo cannot be null, otherwise scalingRequired == false
13126 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13127 canvas.scale(scale, scale);
13128 }
13129 }
13130
Chet Haasea1cff502012-02-21 13:43:44 -080013131 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013132 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080013133 if (transformToApply != null || !childHasIdentityMatrix) {
13134 int transX = 0;
13135 int transY = 0;
13136
13137 if (offsetForScroll) {
13138 transX = -sx;
13139 transY = -sy;
13140 }
13141
13142 if (transformToApply != null) {
13143 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013144 if (useDisplayListProperties) {
13145 displayList.setAnimationMatrix(transformToApply.getMatrix());
13146 } else {
13147 // Undo the scroll translation, apply the transformation matrix,
13148 // then redo the scroll translate to get the correct result.
13149 canvas.translate(-transX, -transY);
13150 canvas.concat(transformToApply.getMatrix());
13151 canvas.translate(transX, transY);
13152 }
Chet Haasea1cff502012-02-21 13:43:44 -080013153 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013154 }
13155
13156 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013157 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013158 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080013159 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013160 }
13161 }
13162
Chet Haasea1cff502012-02-21 13:43:44 -080013163 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013164 canvas.translate(-transX, -transY);
13165 canvas.concat(getMatrix());
13166 canvas.translate(transX, transY);
13167 }
13168 }
13169
Chet Haase9420abd2012-03-29 16:28:32 -070013170 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013171 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013172 if (hasNoCache) {
13173 final int multipliedAlpha = (int) (255 * alpha);
13174 if (!onSetAlpha(multipliedAlpha)) {
13175 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013176 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013177 layerType != LAYER_TYPE_NONE) {
13178 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13179 }
Chet Haase9420abd2012-03-29 16:28:32 -070013180 if (useDisplayListProperties) {
13181 displayList.setAlpha(alpha * getAlpha());
13182 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013183 final int scrollX = hasDisplayList ? 0 : sx;
13184 final int scrollY = hasDisplayList ? 0 : sy;
13185 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13186 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013187 }
13188 } else {
13189 // Alpha is handled by the child directly, clobber the layer's alpha
13190 mPrivateFlags |= ALPHA_SET;
13191 }
13192 }
13193 }
13194 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13195 onSetAlpha(255);
13196 mPrivateFlags &= ~ALPHA_SET;
13197 }
13198
Chet Haasea1cff502012-02-21 13:43:44 -080013199 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13200 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013201 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013202 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013203 } else {
13204 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013205 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013206 } else {
13207 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13208 }
13209 }
13210 }
13211
Chet Haase9420abd2012-03-29 16:28:32 -070013212 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013213 displayList = getDisplayList();
13214 if (!displayList.isValid()) {
13215 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13216 // to getDisplayList(), the display list will be marked invalid and we should not
13217 // try to use it again.
13218 displayList = null;
13219 hasDisplayList = false;
13220 }
13221 }
13222
13223 if (hasNoCache) {
13224 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013225 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013226 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013227 if (layer != null && layer.isValid()) {
13228 mLayerPaint.setAlpha((int) (alpha * 255));
13229 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13230 layerRendered = true;
13231 } else {
13232 final int scrollX = hasDisplayList ? 0 : sx;
13233 final int scrollY = hasDisplayList ? 0 : sy;
13234 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013235 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013236 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13237 }
13238 }
13239
13240 if (!layerRendered) {
13241 if (!hasDisplayList) {
13242 // Fast path for layouts with no backgrounds
13243 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013244 mPrivateFlags &= ~DIRTY_MASK;
13245 dispatchDraw(canvas);
13246 } else {
13247 draw(canvas);
13248 }
13249 } else {
13250 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013251 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013252 }
13253 }
13254 } else if (cache != null) {
13255 mPrivateFlags &= ~DIRTY_MASK;
13256 Paint cachePaint;
13257
13258 if (layerType == LAYER_TYPE_NONE) {
13259 cachePaint = parent.mCachePaint;
13260 if (cachePaint == null) {
13261 cachePaint = new Paint();
13262 cachePaint.setDither(false);
13263 parent.mCachePaint = cachePaint;
13264 }
Chet Haase9420abd2012-03-29 16:28:32 -070013265 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013266 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013267 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13268 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013269 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013270 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013271 }
13272 } else {
13273 cachePaint = mLayerPaint;
13274 cachePaint.setAlpha((int) (alpha * 255));
13275 }
13276 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13277 }
13278
Chet Haasea1cff502012-02-21 13:43:44 -080013279 if (restoreTo >= 0) {
13280 canvas.restoreToCount(restoreTo);
13281 }
Chet Haase64a48c12012-02-13 16:33:29 -080013282
13283 if (a != null && !more) {
13284 if (!hardwareAccelerated && !a.getFillAfter()) {
13285 onSetAlpha(255);
13286 }
13287 parent.finishAnimatingView(this, a);
13288 }
13289
13290 if (more && hardwareAccelerated) {
13291 // invalidation is the trigger to recreate display lists, so if we're using
13292 // display lists to render, force an invalidate to allow the animation to
13293 // continue drawing another frame
13294 parent.invalidate(true);
13295 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13296 // alpha animations should cause the child to recreate its display list
13297 invalidate(true);
13298 }
13299 }
13300
13301 mRecreateDisplayList = false;
13302
13303 return more;
13304 }
13305
13306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013307 * Manually render this view (and all of its children) to the given Canvas.
13308 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013309 * called. When implementing a view, implement
13310 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13311 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312 *
13313 * @param canvas The Canvas to which the View is rendered.
13314 */
13315 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013316 final int privateFlags = mPrivateFlags;
13317 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13318 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13319 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013321 /*
13322 * Draw traversal performs several drawing steps which must be executed
13323 * in the appropriate order:
13324 *
13325 * 1. Draw the background
13326 * 2. If necessary, save the canvas' layers to prepare for fading
13327 * 3. Draw view's content
13328 * 4. Draw children
13329 * 5. If necessary, draw the fading edges and restore layers
13330 * 6. Draw decorations (scrollbars for instance)
13331 */
13332
13333 // Step 1, draw the background, if needed
13334 int saveCount;
13335
Romain Guy24443ea2009-05-11 11:56:30 -070013336 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013337 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013338 if (background != null) {
13339 final int scrollX = mScrollX;
13340 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013341
Romain Guy24443ea2009-05-11 11:56:30 -070013342 if (mBackgroundSizeChanged) {
13343 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13344 mBackgroundSizeChanged = false;
13345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013346
Romain Guy24443ea2009-05-11 11:56:30 -070013347 if ((scrollX | scrollY) == 0) {
13348 background.draw(canvas);
13349 } else {
13350 canvas.translate(scrollX, scrollY);
13351 background.draw(canvas);
13352 canvas.translate(-scrollX, -scrollY);
13353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013354 }
13355 }
13356
13357 // skip step 2 & 5 if possible (common case)
13358 final int viewFlags = mViewFlags;
13359 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13360 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13361 if (!verticalEdges && !horizontalEdges) {
13362 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013363 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013364
13365 // Step 4, draw the children
13366 dispatchDraw(canvas);
13367
13368 // Step 6, draw decorations (scrollbars)
13369 onDrawScrollBars(canvas);
13370
13371 // we're done...
13372 return;
13373 }
13374
13375 /*
13376 * Here we do the full fledged routine...
13377 * (this is an uncommon case where speed matters less,
13378 * this is why we repeat some of the tests that have been
13379 * done above)
13380 */
13381
13382 boolean drawTop = false;
13383 boolean drawBottom = false;
13384 boolean drawLeft = false;
13385 boolean drawRight = false;
13386
13387 float topFadeStrength = 0.0f;
13388 float bottomFadeStrength = 0.0f;
13389 float leftFadeStrength = 0.0f;
13390 float rightFadeStrength = 0.0f;
13391
13392 // Step 2, save the canvas' layers
13393 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013394
13395 final boolean offsetRequired = isPaddingOffsetRequired();
13396 if (offsetRequired) {
13397 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013398 }
13399
13400 int left = mScrollX + paddingLeft;
13401 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013402 int top = mScrollY + getFadeTop(offsetRequired);
13403 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013404
13405 if (offsetRequired) {
13406 right += getRightPaddingOffset();
13407 bottom += getBottomPaddingOffset();
13408 }
13409
13410 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013411 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013412 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013413
13414 // clip the fade length if top and bottom fades overlap
13415 // overlapping fades produce odd-looking artifacts
13416 if (verticalEdges && (top + length > bottom - length)) {
13417 length = (bottom - top) / 2;
13418 }
13419
13420 // also clip horizontal fades if necessary
13421 if (horizontalEdges && (left + length > right - length)) {
13422 length = (right - left) / 2;
13423 }
13424
13425 if (verticalEdges) {
13426 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013427 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013428 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013429 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013430 }
13431
13432 if (horizontalEdges) {
13433 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013434 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013435 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013436 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013437 }
13438
13439 saveCount = canvas.getSaveCount();
13440
13441 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013442 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013443 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13444
13445 if (drawTop) {
13446 canvas.saveLayer(left, top, right, top + length, null, flags);
13447 }
13448
13449 if (drawBottom) {
13450 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13451 }
13452
13453 if (drawLeft) {
13454 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13455 }
13456
13457 if (drawRight) {
13458 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13459 }
13460 } else {
13461 scrollabilityCache.setFadeColor(solidColor);
13462 }
13463
13464 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013465 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013466
13467 // Step 4, draw the children
13468 dispatchDraw(canvas);
13469
13470 // Step 5, draw the fade effect and restore layers
13471 final Paint p = scrollabilityCache.paint;
13472 final Matrix matrix = scrollabilityCache.matrix;
13473 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013474
13475 if (drawTop) {
13476 matrix.setScale(1, fadeHeight * topFadeStrength);
13477 matrix.postTranslate(left, top);
13478 fade.setLocalMatrix(matrix);
13479 canvas.drawRect(left, top, right, top + length, p);
13480 }
13481
13482 if (drawBottom) {
13483 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13484 matrix.postRotate(180);
13485 matrix.postTranslate(left, bottom);
13486 fade.setLocalMatrix(matrix);
13487 canvas.drawRect(left, bottom - length, right, bottom, p);
13488 }
13489
13490 if (drawLeft) {
13491 matrix.setScale(1, fadeHeight * leftFadeStrength);
13492 matrix.postRotate(-90);
13493 matrix.postTranslate(left, top);
13494 fade.setLocalMatrix(matrix);
13495 canvas.drawRect(left, top, left + length, bottom, p);
13496 }
13497
13498 if (drawRight) {
13499 matrix.setScale(1, fadeHeight * rightFadeStrength);
13500 matrix.postRotate(90);
13501 matrix.postTranslate(right, top);
13502 fade.setLocalMatrix(matrix);
13503 canvas.drawRect(right - length, top, right, bottom, p);
13504 }
13505
13506 canvas.restoreToCount(saveCount);
13507
13508 // Step 6, draw decorations (scrollbars)
13509 onDrawScrollBars(canvas);
13510 }
13511
13512 /**
13513 * Override this if your view is known to always be drawn on top of a solid color background,
13514 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13515 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13516 * should be set to 0xFF.
13517 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013518 * @see #setVerticalFadingEdgeEnabled(boolean)
13519 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013520 *
13521 * @return The known solid color background for this view, or 0 if the color may vary
13522 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013523 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524 public int getSolidColor() {
13525 return 0;
13526 }
13527
13528 /**
13529 * Build a human readable string representation of the specified view flags.
13530 *
13531 * @param flags the view flags to convert to a string
13532 * @return a String representing the supplied flags
13533 */
13534 private static String printFlags(int flags) {
13535 String output = "";
13536 int numFlags = 0;
13537 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13538 output += "TAKES_FOCUS";
13539 numFlags++;
13540 }
13541
13542 switch (flags & VISIBILITY_MASK) {
13543 case INVISIBLE:
13544 if (numFlags > 0) {
13545 output += " ";
13546 }
13547 output += "INVISIBLE";
13548 // USELESS HERE numFlags++;
13549 break;
13550 case GONE:
13551 if (numFlags > 0) {
13552 output += " ";
13553 }
13554 output += "GONE";
13555 // USELESS HERE numFlags++;
13556 break;
13557 default:
13558 break;
13559 }
13560 return output;
13561 }
13562
13563 /**
13564 * Build a human readable string representation of the specified private
13565 * view flags.
13566 *
13567 * @param privateFlags the private view flags to convert to a string
13568 * @return a String representing the supplied flags
13569 */
13570 private static String printPrivateFlags(int privateFlags) {
13571 String output = "";
13572 int numFlags = 0;
13573
13574 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13575 output += "WANTS_FOCUS";
13576 numFlags++;
13577 }
13578
13579 if ((privateFlags & FOCUSED) == FOCUSED) {
13580 if (numFlags > 0) {
13581 output += " ";
13582 }
13583 output += "FOCUSED";
13584 numFlags++;
13585 }
13586
13587 if ((privateFlags & SELECTED) == SELECTED) {
13588 if (numFlags > 0) {
13589 output += " ";
13590 }
13591 output += "SELECTED";
13592 numFlags++;
13593 }
13594
13595 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13596 if (numFlags > 0) {
13597 output += " ";
13598 }
13599 output += "IS_ROOT_NAMESPACE";
13600 numFlags++;
13601 }
13602
13603 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13604 if (numFlags > 0) {
13605 output += " ";
13606 }
13607 output += "HAS_BOUNDS";
13608 numFlags++;
13609 }
13610
13611 if ((privateFlags & DRAWN) == DRAWN) {
13612 if (numFlags > 0) {
13613 output += " ";
13614 }
13615 output += "DRAWN";
13616 // USELESS HERE numFlags++;
13617 }
13618 return output;
13619 }
13620
13621 /**
13622 * <p>Indicates whether or not this view's layout will be requested during
13623 * the next hierarchy layout pass.</p>
13624 *
13625 * @return true if the layout will be forced during next layout pass
13626 */
13627 public boolean isLayoutRequested() {
13628 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13629 }
13630
13631 /**
13632 * Assign a size and position to a view and all of its
13633 * descendants
13634 *
13635 * <p>This is the second phase of the layout mechanism.
13636 * (The first is measuring). In this phase, each parent calls
13637 * layout on all of its children to position them.
13638 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013639 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 *
Chet Haase9c087442011-01-12 16:20:16 -080013641 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013642 * Derived classes with children should override
13643 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013644 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 *
13646 * @param l Left position, relative to parent
13647 * @param t Top position, relative to parent
13648 * @param r Right position, relative to parent
13649 * @param b Bottom position, relative to parent
13650 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013651 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013652 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013653 int oldL = mLeft;
13654 int oldT = mTop;
13655 int oldB = mBottom;
13656 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013657 boolean changed = setFrame(l, t, r, b);
13658 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013659 onLayout(changed, l, t, r, b);
13660 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013661
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013662 ListenerInfo li = mListenerInfo;
13663 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013664 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013665 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013666 int numListeners = listenersCopy.size();
13667 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013668 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013669 }
13670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 }
13672 mPrivateFlags &= ~FORCE_LAYOUT;
13673 }
13674
13675 /**
13676 * Called from layout when this view should
13677 * assign a size and position to each of its children.
13678 *
13679 * Derived classes with children should override
13680 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013681 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013682 * @param changed This is a new size or position for this view
13683 * @param left Left position, relative to parent
13684 * @param top Top position, relative to parent
13685 * @param right Right position, relative to parent
13686 * @param bottom Bottom position, relative to parent
13687 */
13688 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13689 }
13690
13691 /**
13692 * Assign a size and position to this view.
13693 *
13694 * This is called from layout.
13695 *
13696 * @param left Left position, relative to parent
13697 * @param top Top position, relative to parent
13698 * @param right Right position, relative to parent
13699 * @param bottom Bottom position, relative to parent
13700 * @return true if the new size and position are different than the
13701 * previous ones
13702 * {@hide}
13703 */
13704 protected boolean setFrame(int left, int top, int right, int bottom) {
13705 boolean changed = false;
13706
13707 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013708 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013709 + right + "," + bottom + ")");
13710 }
13711
13712 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13713 changed = true;
13714
13715 // Remember our drawn bit
13716 int drawn = mPrivateFlags & DRAWN;
13717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013718 int oldWidth = mRight - mLeft;
13719 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013720 int newWidth = right - left;
13721 int newHeight = bottom - top;
13722 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13723
13724 // Invalidate our old position
13725 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013726
13727 mLeft = left;
13728 mTop = top;
13729 mRight = right;
13730 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013731 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013732 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013734
13735 mPrivateFlags |= HAS_BOUNDS;
13736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737
Chet Haase75755e22011-07-18 17:48:25 -070013738 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013739 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13740 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013741 if (mTransformationInfo != null) {
13742 mTransformationInfo.mMatrixDirty = true;
13743 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13746 }
13747
13748 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13749 // If we are visible, force the DRAWN bit to on so that
13750 // this invalidate will go through (at least to our parent).
13751 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013752 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 // the DRAWN bit.
13754 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013755 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013756 // parent display list may need to be recreated based on a change in the bounds
13757 // of any child
13758 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013759 }
13760
13761 // Reset drawn bit to original value (invalidate turns it off)
13762 mPrivateFlags |= drawn;
13763
13764 mBackgroundSizeChanged = true;
13765 }
13766 return changed;
13767 }
13768
13769 /**
13770 * Finalize inflating a view from XML. This is called as the last phase
13771 * of inflation, after all child views have been added.
13772 *
13773 * <p>Even if the subclass overrides onFinishInflate, they should always be
13774 * sure to call the super method, so that we get called.
13775 */
13776 protected void onFinishInflate() {
13777 }
13778
13779 /**
13780 * Returns the resources associated with this view.
13781 *
13782 * @return Resources object.
13783 */
13784 public Resources getResources() {
13785 return mResources;
13786 }
13787
13788 /**
13789 * Invalidates the specified Drawable.
13790 *
13791 * @param drawable the drawable to invalidate
13792 */
13793 public void invalidateDrawable(Drawable drawable) {
13794 if (verifyDrawable(drawable)) {
13795 final Rect dirty = drawable.getBounds();
13796 final int scrollX = mScrollX;
13797 final int scrollY = mScrollY;
13798
13799 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13800 dirty.right + scrollX, dirty.bottom + scrollY);
13801 }
13802 }
13803
13804 /**
13805 * Schedules an action on a drawable to occur at a specified time.
13806 *
13807 * @param who the recipient of the action
13808 * @param what the action to run on the drawable
13809 * @param when the time at which the action must occur. Uses the
13810 * {@link SystemClock#uptimeMillis} timebase.
13811 */
13812 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013813 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013814 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013815 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013816 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13817 Choreographer.CALLBACK_ANIMATION, what, who,
13818 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013819 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013820 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013822 }
13823 }
13824
13825 /**
13826 * Cancels a scheduled action on a drawable.
13827 *
13828 * @param who the recipient of the action
13829 * @param what the action to cancel
13830 */
13831 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013832 if (verifyDrawable(who) && what != null) {
13833 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013834 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13835 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013836 } else {
13837 ViewRootImpl.getRunQueue().removeCallbacks(what);
13838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839 }
13840 }
13841
13842 /**
13843 * Unschedule any events associated with the given Drawable. This can be
13844 * used when selecting a new Drawable into a view, so that the previous
13845 * one is completely unscheduled.
13846 *
13847 * @param who The Drawable to unschedule.
13848 *
13849 * @see #drawableStateChanged
13850 */
13851 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013852 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013853 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13854 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013855 }
13856 }
13857
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013858 /**
13859 * Return the layout direction of a given Drawable.
13860 *
13861 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013862 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013863 */
13864 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013865 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013866 }
13867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013868 /**
13869 * If your view subclass is displaying its own Drawable objects, it should
13870 * override this function and return true for any Drawable it is
13871 * displaying. This allows animations for those drawables to be
13872 * scheduled.
13873 *
13874 * <p>Be sure to call through to the super class when overriding this
13875 * function.
13876 *
13877 * @param who The Drawable to verify. Return true if it is one you are
13878 * displaying, else return the result of calling through to the
13879 * super class.
13880 *
13881 * @return boolean If true than the Drawable is being displayed in the
13882 * view; else false and it is not allowed to animate.
13883 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013884 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13885 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013886 */
13887 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013888 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013889 }
13890
13891 /**
13892 * This function is called whenever the state of the view changes in such
13893 * a way that it impacts the state of drawables being shown.
13894 *
13895 * <p>Be sure to call through to the superclass when overriding this
13896 * function.
13897 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013898 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013899 */
13900 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013901 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013902 if (d != null && d.isStateful()) {
13903 d.setState(getDrawableState());
13904 }
13905 }
13906
13907 /**
13908 * Call this to force a view to update its drawable state. This will cause
13909 * drawableStateChanged to be called on this view. Views that are interested
13910 * in the new state should call getDrawableState.
13911 *
13912 * @see #drawableStateChanged
13913 * @see #getDrawableState
13914 */
13915 public void refreshDrawableState() {
13916 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13917 drawableStateChanged();
13918
13919 ViewParent parent = mParent;
13920 if (parent != null) {
13921 parent.childDrawableStateChanged(this);
13922 }
13923 }
13924
13925 /**
13926 * Return an array of resource IDs of the drawable states representing the
13927 * current state of the view.
13928 *
13929 * @return The current drawable state
13930 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013931 * @see Drawable#setState(int[])
13932 * @see #drawableStateChanged()
13933 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 */
13935 public final int[] getDrawableState() {
13936 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13937 return mDrawableState;
13938 } else {
13939 mDrawableState = onCreateDrawableState(0);
13940 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13941 return mDrawableState;
13942 }
13943 }
13944
13945 /**
13946 * Generate the new {@link android.graphics.drawable.Drawable} state for
13947 * this view. This is called by the view
13948 * system when the cached Drawable state is determined to be invalid. To
13949 * retrieve the current state, you should use {@link #getDrawableState}.
13950 *
13951 * @param extraSpace if non-zero, this is the number of extra entries you
13952 * would like in the returned array in which you can place your own
13953 * states.
13954 *
13955 * @return Returns an array holding the current {@link Drawable} state of
13956 * the view.
13957 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013958 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013959 */
13960 protected int[] onCreateDrawableState(int extraSpace) {
13961 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13962 mParent instanceof View) {
13963 return ((View) mParent).onCreateDrawableState(extraSpace);
13964 }
13965
13966 int[] drawableState;
13967
13968 int privateFlags = mPrivateFlags;
13969
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013970 int viewStateIndex = 0;
13971 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13972 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13973 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013974 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013975 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13976 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013977 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13978 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013979 // This is set if HW acceleration is requested, even if the current
13980 // process doesn't allow it. This is just to allow app preview
13981 // windows to better match their app.
13982 viewStateIndex |= VIEW_STATE_ACCELERATED;
13983 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013984 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013985
Christopher Tate3d4bf172011-03-28 16:16:46 -070013986 final int privateFlags2 = mPrivateFlags2;
13987 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13988 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013990 drawableState = VIEW_STATE_SETS[viewStateIndex];
13991
13992 //noinspection ConstantIfStatement
13993 if (false) {
13994 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13995 Log.i("View", toString()
13996 + " pressed=" + ((privateFlags & PRESSED) != 0)
13997 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13998 + " fo=" + hasFocus()
13999 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014000 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014001 + ": " + Arrays.toString(drawableState));
14002 }
14003
14004 if (extraSpace == 0) {
14005 return drawableState;
14006 }
14007
14008 final int[] fullState;
14009 if (drawableState != null) {
14010 fullState = new int[drawableState.length + extraSpace];
14011 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14012 } else {
14013 fullState = new int[extraSpace];
14014 }
14015
14016 return fullState;
14017 }
14018
14019 /**
14020 * Merge your own state values in <var>additionalState</var> into the base
14021 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014022 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014023 *
14024 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014025 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014026 * own additional state values.
14027 *
14028 * @param additionalState The additional state values you would like
14029 * added to <var>baseState</var>; this array is not modified.
14030 *
14031 * @return As a convenience, the <var>baseState</var> array you originally
14032 * passed into the function is returned.
14033 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014034 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014035 */
14036 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14037 final int N = baseState.length;
14038 int i = N - 1;
14039 while (i >= 0 && baseState[i] == 0) {
14040 i--;
14041 }
14042 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14043 return baseState;
14044 }
14045
14046 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014047 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14048 * on all Drawable objects associated with this view.
14049 */
14050 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014051 if (mBackground != null) {
14052 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014053 }
14054 }
14055
14056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014057 * Sets the background color for this view.
14058 * @param color the color of the background
14059 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014060 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014062 if (mBackground instanceof ColorDrawable) {
14063 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014064 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014065 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 }
14068
14069 /**
14070 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014071 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 * @attr ref android.R.styleable#View_background
14075 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014076 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 public void setBackgroundResource(int resid) {
14078 if (resid != 0 && resid == mBackgroundResource) {
14079 return;
14080 }
14081
14082 Drawable d= null;
14083 if (resid != 0) {
14084 d = mResources.getDrawable(resid);
14085 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014086 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087
14088 mBackgroundResource = resid;
14089 }
14090
14091 /**
14092 * Set the background to a given Drawable, or remove the background. If the
14093 * background has padding, this View's padding is set to the background's
14094 * padding. However, when a background is removed, this View's padding isn't
14095 * touched. If setting the padding is desired, please use
14096 * {@link #setPadding(int, int, int, int)}.
14097 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014098 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014099 * background
14100 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014101 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014102 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014103 setBackgroundDrawable(background);
14104 }
14105
14106 /**
14107 * @deprecated use {@link #setBackground(Drawable)} instead
14108 */
14109 @Deprecated
14110 public void setBackgroundDrawable(Drawable background) {
14111 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014112 return;
14113 }
14114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014115 boolean requestLayout = false;
14116
14117 mBackgroundResource = 0;
14118
14119 /*
14120 * Regardless of whether we're setting a new background or not, we want
14121 * to clear the previous drawable.
14122 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014123 if (mBackground != null) {
14124 mBackground.setCallback(null);
14125 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014126 }
14127
Philip Milne6c8ea062012-04-03 17:38:43 -070014128 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014129 Rect padding = sThreadLocal.get();
14130 if (padding == null) {
14131 padding = new Rect();
14132 sThreadLocal.set(padding);
14133 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014134 if (background.getPadding(padding)) {
14135 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014136 case LAYOUT_DIRECTION_RTL:
14137 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14138 break;
14139 case LAYOUT_DIRECTION_LTR:
14140 default:
14141 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014143 }
14144
14145 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14146 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014147 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14148 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149 requestLayout = true;
14150 }
14151
Philip Milne6c8ea062012-04-03 17:38:43 -070014152 background.setCallback(this);
14153 if (background.isStateful()) {
14154 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014155 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014156 background.setVisible(getVisibility() == VISIBLE, false);
14157 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158
14159 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14160 mPrivateFlags &= ~SKIP_DRAW;
14161 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14162 requestLayout = true;
14163 }
14164 } else {
14165 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014166 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014167
14168 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14169 /*
14170 * This view ONLY drew the background before and we're removing
14171 * the background, so now it won't draw anything
14172 * (hence we SKIP_DRAW)
14173 */
14174 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14175 mPrivateFlags |= SKIP_DRAW;
14176 }
14177
14178 /*
14179 * When the background is set, we try to apply its padding to this
14180 * View. When the background is removed, we don't touch this View's
14181 * padding. This is noted in the Javadocs. Hence, we don't need to
14182 * requestLayout(), the invalidate() below is sufficient.
14183 */
14184
14185 // The old background's minimum size could have affected this
14186 // View's layout, so let's requestLayout
14187 requestLayout = true;
14188 }
14189
Romain Guy8f1344f52009-05-15 16:03:59 -070014190 computeOpaqueFlags();
14191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014192 if (requestLayout) {
14193 requestLayout();
14194 }
14195
14196 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014197 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014198 }
14199
14200 /**
14201 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014203 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014204 *
14205 * @see #setBackground(Drawable)
14206 *
14207 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014208 */
14209 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014210 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014211 }
14212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014213 /**
14214 * Sets the padding. The view may add on the space required to display
14215 * the scrollbars, depending on the style and visibility of the scrollbars.
14216 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14217 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14218 * from the values set in this call.
14219 *
14220 * @attr ref android.R.styleable#View_padding
14221 * @attr ref android.R.styleable#View_paddingBottom
14222 * @attr ref android.R.styleable#View_paddingLeft
14223 * @attr ref android.R.styleable#View_paddingRight
14224 * @attr ref android.R.styleable#View_paddingTop
14225 * @param left the left padding in pixels
14226 * @param top the top padding in pixels
14227 * @param right the right padding in pixels
14228 * @param bottom the bottom padding in pixels
14229 */
14230 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014231 mUserPaddingStart = -1;
14232 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014233 mUserPaddingRelative = false;
14234
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014235 internalSetPadding(left, top, right, bottom);
14236 }
14237
14238 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014239 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 mUserPaddingRight = right;
14241 mUserPaddingBottom = bottom;
14242
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014243 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014244 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014245
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014246 // Common case is there are no scroll bars.
14247 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014248 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014249 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014250 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014251 switch (mVerticalScrollbarPosition) {
14252 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014253 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14254 left += offset;
14255 } else {
14256 right += offset;
14257 }
14258 break;
Adam Powell20232d02010-12-08 21:08:53 -080014259 case SCROLLBAR_POSITION_RIGHT:
14260 right += offset;
14261 break;
14262 case SCROLLBAR_POSITION_LEFT:
14263 left += offset;
14264 break;
14265 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014266 }
Adam Powell20232d02010-12-08 21:08:53 -080014267 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014268 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14269 ? 0 : getHorizontalScrollbarHeight();
14270 }
14271 }
Romain Guy8506ab42009-06-11 17:35:47 -070014272
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014273 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274 changed = true;
14275 mPaddingLeft = left;
14276 }
14277 if (mPaddingTop != top) {
14278 changed = true;
14279 mPaddingTop = top;
14280 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014281 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014283 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014284 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014285 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014286 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014287 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014288 }
14289
14290 if (changed) {
14291 requestLayout();
14292 }
14293 }
14294
14295 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014296 * Sets the relative padding. The view may add on the space required to display
14297 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014298 * from the values set in this call.
14299 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014300 * @param start the start padding in pixels
14301 * @param top the top padding in pixels
14302 * @param end the end padding in pixels
14303 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014304 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014305 */
14306 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014307 mUserPaddingStart = start;
14308 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014309 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014310
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014311 switch(getResolvedLayoutDirection()) {
14312 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014313 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014314 break;
14315 case LAYOUT_DIRECTION_LTR:
14316 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014317 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014318 }
14319 }
14320
14321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014322 * Returns the top padding of this view.
14323 *
14324 * @return the top padding in pixels
14325 */
14326 public int getPaddingTop() {
14327 return mPaddingTop;
14328 }
14329
14330 /**
14331 * Returns the bottom padding of this view. If there are inset and enabled
14332 * scrollbars, this value may include the space required to display the
14333 * scrollbars as well.
14334 *
14335 * @return the bottom padding in pixels
14336 */
14337 public int getPaddingBottom() {
14338 return mPaddingBottom;
14339 }
14340
14341 /**
14342 * Returns the left padding of this view. If there are inset and enabled
14343 * scrollbars, this value may include the space required to display the
14344 * scrollbars as well.
14345 *
14346 * @return the left padding in pixels
14347 */
14348 public int getPaddingLeft() {
14349 return mPaddingLeft;
14350 }
14351
14352 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014353 * Returns the start padding of this view depending on its resolved layout direction.
14354 * If there are inset and enabled scrollbars, this value may include the space
14355 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014356 *
14357 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014358 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014359 */
14360 public int getPaddingStart() {
14361 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14362 mPaddingRight : mPaddingLeft;
14363 }
14364
14365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014366 * Returns the right padding of this view. If there are inset and enabled
14367 * scrollbars, this value may include the space required to display the
14368 * scrollbars as well.
14369 *
14370 * @return the right padding in pixels
14371 */
14372 public int getPaddingRight() {
14373 return mPaddingRight;
14374 }
14375
14376 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014377 * Returns the end padding of this view depending on its resolved layout direction.
14378 * If there are inset and enabled scrollbars, this value may include the space
14379 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014380 *
14381 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014382 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014383 */
14384 public int getPaddingEnd() {
14385 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14386 mPaddingLeft : mPaddingRight;
14387 }
14388
14389 /**
14390 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014391 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014392 *
14393 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014394 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014395 */
14396 public boolean isPaddingRelative() {
14397 return mUserPaddingRelative;
14398 }
14399
14400 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014401 * @hide
14402 */
Philip Milne7a23b492012-04-24 22:12:36 -070014403 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014404 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014405 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014406 }
14407 return mLayoutInsets;
14408 }
14409
14410 /**
14411 * @hide
14412 */
14413 public void setLayoutInsets(Insets layoutInsets) {
14414 mLayoutInsets = layoutInsets;
14415 }
14416
14417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014418 * Changes the selection state of this view. A view can be selected or not.
14419 * Note that selection is not the same as focus. Views are typically
14420 * selected in the context of an AdapterView like ListView or GridView;
14421 * the selected view is the view that is highlighted.
14422 *
14423 * @param selected true if the view must be selected, false otherwise
14424 */
14425 public void setSelected(boolean selected) {
14426 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14427 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014428 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014429 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014430 refreshDrawableState();
14431 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014432 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14433 notifyAccessibilityStateChanged();
14434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014435 }
14436 }
14437
14438 /**
14439 * Dispatch setSelected to all of this View's children.
14440 *
14441 * @see #setSelected(boolean)
14442 *
14443 * @param selected The new selected state
14444 */
14445 protected void dispatchSetSelected(boolean selected) {
14446 }
14447
14448 /**
14449 * Indicates the selection state of this view.
14450 *
14451 * @return true if the view is selected, false otherwise
14452 */
14453 @ViewDebug.ExportedProperty
14454 public boolean isSelected() {
14455 return (mPrivateFlags & SELECTED) != 0;
14456 }
14457
14458 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014459 * Changes the activated state of this view. A view can be activated or not.
14460 * Note that activation is not the same as selection. Selection is
14461 * a transient property, representing the view (hierarchy) the user is
14462 * currently interacting with. Activation is a longer-term state that the
14463 * user can move views in and out of. For example, in a list view with
14464 * single or multiple selection enabled, the views in the current selection
14465 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14466 * here.) The activated state is propagated down to children of the view it
14467 * is set on.
14468 *
14469 * @param activated true if the view must be activated, false otherwise
14470 */
14471 public void setActivated(boolean activated) {
14472 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14473 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014474 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014475 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014476 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014477 }
14478 }
14479
14480 /**
14481 * Dispatch setActivated to all of this View's children.
14482 *
14483 * @see #setActivated(boolean)
14484 *
14485 * @param activated The new activated state
14486 */
14487 protected void dispatchSetActivated(boolean activated) {
14488 }
14489
14490 /**
14491 * Indicates the activation state of this view.
14492 *
14493 * @return true if the view is activated, false otherwise
14494 */
14495 @ViewDebug.ExportedProperty
14496 public boolean isActivated() {
14497 return (mPrivateFlags & ACTIVATED) != 0;
14498 }
14499
14500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14502 * observer can be used to get notifications when global events, like
14503 * layout, happen.
14504 *
14505 * The returned ViewTreeObserver observer is not guaranteed to remain
14506 * valid for the lifetime of this View. If the caller of this method keeps
14507 * a long-lived reference to ViewTreeObserver, it should always check for
14508 * the return value of {@link ViewTreeObserver#isAlive()}.
14509 *
14510 * @return The ViewTreeObserver for this view's hierarchy.
14511 */
14512 public ViewTreeObserver getViewTreeObserver() {
14513 if (mAttachInfo != null) {
14514 return mAttachInfo.mTreeObserver;
14515 }
14516 if (mFloatingTreeObserver == null) {
14517 mFloatingTreeObserver = new ViewTreeObserver();
14518 }
14519 return mFloatingTreeObserver;
14520 }
14521
14522 /**
14523 * <p>Finds the topmost view in the current view hierarchy.</p>
14524 *
14525 * @return the topmost view containing this view
14526 */
14527 public View getRootView() {
14528 if (mAttachInfo != null) {
14529 final View v = mAttachInfo.mRootView;
14530 if (v != null) {
14531 return v;
14532 }
14533 }
Romain Guy8506ab42009-06-11 17:35:47 -070014534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014535 View parent = this;
14536
14537 while (parent.mParent != null && parent.mParent instanceof View) {
14538 parent = (View) parent.mParent;
14539 }
14540
14541 return parent;
14542 }
14543
14544 /**
14545 * <p>Computes the coordinates of this view on the screen. The argument
14546 * must be an array of two integers. After the method returns, the array
14547 * contains the x and y location in that order.</p>
14548 *
14549 * @param location an array of two integers in which to hold the coordinates
14550 */
14551 public void getLocationOnScreen(int[] location) {
14552 getLocationInWindow(location);
14553
14554 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014555 if (info != null) {
14556 location[0] += info.mWindowLeft;
14557 location[1] += info.mWindowTop;
14558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014559 }
14560
14561 /**
14562 * <p>Computes the coordinates of this view in its window. The argument
14563 * must be an array of two integers. After the method returns, the array
14564 * contains the x and y location in that order.</p>
14565 *
14566 * @param location an array of two integers in which to hold the coordinates
14567 */
14568 public void getLocationInWindow(int[] location) {
14569 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014570 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014571 }
14572
Gilles Debunne6583ce52011-12-06 18:09:02 -080014573 if (mAttachInfo == null) {
14574 // When the view is not attached to a window, this method does not make sense
14575 location[0] = location[1] = 0;
14576 return;
14577 }
14578
Gilles Debunnecea45132011-11-24 02:19:27 +010014579 float[] position = mAttachInfo.mTmpTransformLocation;
14580 position[0] = position[1] = 0.0f;
14581
14582 if (!hasIdentityMatrix()) {
14583 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014585
Gilles Debunnecea45132011-11-24 02:19:27 +010014586 position[0] += mLeft;
14587 position[1] += mTop;
14588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014589 ViewParent viewParent = mParent;
14590 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014591 final View view = (View) viewParent;
14592
14593 position[0] -= view.mScrollX;
14594 position[1] -= view.mScrollY;
14595
14596 if (!view.hasIdentityMatrix()) {
14597 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014598 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014599
14600 position[0] += view.mLeft;
14601 position[1] += view.mTop;
14602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014603 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014604 }
Romain Guy8506ab42009-06-11 17:35:47 -070014605
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014606 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014608 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14609 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014610 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014611
14612 location[0] = (int) (position[0] + 0.5f);
14613 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014614 }
14615
14616 /**
14617 * {@hide}
14618 * @param id the id of the view to be found
14619 * @return the view of the specified id, null if cannot be found
14620 */
14621 protected View findViewTraversal(int id) {
14622 if (id == mID) {
14623 return this;
14624 }
14625 return null;
14626 }
14627
14628 /**
14629 * {@hide}
14630 * @param tag the tag of the view to be found
14631 * @return the view of specified tag, null if cannot be found
14632 */
14633 protected View findViewWithTagTraversal(Object tag) {
14634 if (tag != null && tag.equals(mTag)) {
14635 return this;
14636 }
14637 return null;
14638 }
14639
14640 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014641 * {@hide}
14642 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014643 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014644 * @return The first view that matches the predicate or null.
14645 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014646 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014647 if (predicate.apply(this)) {
14648 return this;
14649 }
14650 return null;
14651 }
14652
14653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014654 * Look for a child view with the given id. If this view has the given
14655 * id, return this view.
14656 *
14657 * @param id The id to search for.
14658 * @return The view that has the given id in the hierarchy or null
14659 */
14660 public final View findViewById(int id) {
14661 if (id < 0) {
14662 return null;
14663 }
14664 return findViewTraversal(id);
14665 }
14666
14667 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014668 * Finds a view by its unuque and stable accessibility id.
14669 *
14670 * @param accessibilityId The searched accessibility id.
14671 * @return The found view.
14672 */
14673 final View findViewByAccessibilityId(int accessibilityId) {
14674 if (accessibilityId < 0) {
14675 return null;
14676 }
14677 return findViewByAccessibilityIdTraversal(accessibilityId);
14678 }
14679
14680 /**
14681 * Performs the traversal to find a view by its unuque and stable accessibility id.
14682 *
14683 * <strong>Note:</strong>This method does not stop at the root namespace
14684 * boundary since the user can touch the screen at an arbitrary location
14685 * potentially crossing the root namespace bounday which will send an
14686 * accessibility event to accessibility services and they should be able
14687 * to obtain the event source. Also accessibility ids are guaranteed to be
14688 * unique in the window.
14689 *
14690 * @param accessibilityId The accessibility id.
14691 * @return The found view.
14692 */
14693 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14694 if (getAccessibilityViewId() == accessibilityId) {
14695 return this;
14696 }
14697 return null;
14698 }
14699
14700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014701 * Look for a child view with the given tag. If this view has the given
14702 * tag, return this view.
14703 *
14704 * @param tag The tag to search for, using "tag.equals(getTag())".
14705 * @return The View that has the given tag in the hierarchy or null
14706 */
14707 public final View findViewWithTag(Object tag) {
14708 if (tag == null) {
14709 return null;
14710 }
14711 return findViewWithTagTraversal(tag);
14712 }
14713
14714 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014715 * {@hide}
14716 * Look for a child view that matches the specified predicate.
14717 * If this view matches the predicate, return this view.
14718 *
14719 * @param predicate The predicate to evaluate.
14720 * @return The first view that matches the predicate or null.
14721 */
14722 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014723 return findViewByPredicateTraversal(predicate, null);
14724 }
14725
14726 /**
14727 * {@hide}
14728 * Look for a child view that matches the specified predicate,
14729 * starting with the specified view and its descendents and then
14730 * recusively searching the ancestors and siblings of that view
14731 * until this view is reached.
14732 *
14733 * This method is useful in cases where the predicate does not match
14734 * a single unique view (perhaps multiple views use the same id)
14735 * and we are trying to find the view that is "closest" in scope to the
14736 * starting view.
14737 *
14738 * @param start The view to start from.
14739 * @param predicate The predicate to evaluate.
14740 * @return The first view that matches the predicate or null.
14741 */
14742 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14743 View childToSkip = null;
14744 for (;;) {
14745 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14746 if (view != null || start == this) {
14747 return view;
14748 }
14749
14750 ViewParent parent = start.getParent();
14751 if (parent == null || !(parent instanceof View)) {
14752 return null;
14753 }
14754
14755 childToSkip = start;
14756 start = (View) parent;
14757 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014758 }
14759
14760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014761 * Sets the identifier for this view. The identifier does not have to be
14762 * unique in this view's hierarchy. The identifier should be a positive
14763 * number.
14764 *
14765 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014766 * @see #getId()
14767 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014768 *
14769 * @param id a number used to identify the view
14770 *
14771 * @attr ref android.R.styleable#View_id
14772 */
14773 public void setId(int id) {
14774 mID = id;
14775 }
14776
14777 /**
14778 * {@hide}
14779 *
14780 * @param isRoot true if the view belongs to the root namespace, false
14781 * otherwise
14782 */
14783 public void setIsRootNamespace(boolean isRoot) {
14784 if (isRoot) {
14785 mPrivateFlags |= IS_ROOT_NAMESPACE;
14786 } else {
14787 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14788 }
14789 }
14790
14791 /**
14792 * {@hide}
14793 *
14794 * @return true if the view belongs to the root namespace, false otherwise
14795 */
14796 public boolean isRootNamespace() {
14797 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14798 }
14799
14800 /**
14801 * Returns this view's identifier.
14802 *
14803 * @return a positive integer used to identify the view or {@link #NO_ID}
14804 * if the view has no ID
14805 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014806 * @see #setId(int)
14807 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014808 * @attr ref android.R.styleable#View_id
14809 */
14810 @ViewDebug.CapturedViewProperty
14811 public int getId() {
14812 return mID;
14813 }
14814
14815 /**
14816 * Returns this view's tag.
14817 *
14818 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014819 *
14820 * @see #setTag(Object)
14821 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014822 */
14823 @ViewDebug.ExportedProperty
14824 public Object getTag() {
14825 return mTag;
14826 }
14827
14828 /**
14829 * Sets the tag associated with this view. A tag can be used to mark
14830 * a view in its hierarchy and does not have to be unique within the
14831 * hierarchy. Tags can also be used to store data within a view without
14832 * resorting to another data structure.
14833 *
14834 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014835 *
14836 * @see #getTag()
14837 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 */
14839 public void setTag(final Object tag) {
14840 mTag = tag;
14841 }
14842
14843 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014844 * Returns the tag associated with this view and the specified key.
14845 *
14846 * @param key The key identifying the tag
14847 *
14848 * @return the Object stored in this view as a tag
14849 *
14850 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014851 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014852 */
14853 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014854 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014855 return null;
14856 }
14857
14858 /**
14859 * Sets a tag associated with this view and a key. A tag can be used
14860 * to mark a view in its hierarchy and does not have to be unique within
14861 * the hierarchy. Tags can also be used to store data within a view
14862 * without resorting to another data structure.
14863 *
14864 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014865 * application to ensure it is unique (see the <a
14866 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14867 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014868 * the Android framework or not associated with any package will cause
14869 * an {@link IllegalArgumentException} to be thrown.
14870 *
14871 * @param key The key identifying the tag
14872 * @param tag An Object to tag the view with
14873 *
14874 * @throws IllegalArgumentException If they specified key is not valid
14875 *
14876 * @see #setTag(Object)
14877 * @see #getTag(int)
14878 */
14879 public void setTag(int key, final Object tag) {
14880 // If the package id is 0x00 or 0x01, it's either an undefined package
14881 // or a framework id
14882 if ((key >>> 24) < 2) {
14883 throw new IllegalArgumentException("The key must be an application-specific "
14884 + "resource id.");
14885 }
14886
Adam Powell2b2f6d62011-09-23 11:15:39 -070014887 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014888 }
14889
14890 /**
14891 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14892 * framework id.
14893 *
14894 * @hide
14895 */
14896 public void setTagInternal(int key, Object tag) {
14897 if ((key >>> 24) != 0x1) {
14898 throw new IllegalArgumentException("The key must be a framework-specific "
14899 + "resource id.");
14900 }
14901
Adam Powell2b2f6d62011-09-23 11:15:39 -070014902 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014903 }
14904
Adam Powell2b2f6d62011-09-23 11:15:39 -070014905 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014906 if (mKeyedTags == null) {
14907 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014908 }
14909
Adam Powell7db82ac2011-09-22 19:44:04 -070014910 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014911 }
14912
14913 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014914 * Prints information about this view in the log output, with the tag
14915 * {@link #VIEW_LOG_TAG}.
14916 *
14917 * @hide
14918 */
14919 public void debug() {
14920 debug(0);
14921 }
14922
14923 /**
14924 * Prints information about this view in the log output, with the tag
14925 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14926 * indentation defined by the <code>depth</code>.
14927 *
14928 * @param depth the indentation level
14929 *
14930 * @hide
14931 */
14932 protected void debug(int depth) {
14933 String output = debugIndent(depth - 1);
14934
14935 output += "+ " + this;
14936 int id = getId();
14937 if (id != -1) {
14938 output += " (id=" + id + ")";
14939 }
14940 Object tag = getTag();
14941 if (tag != null) {
14942 output += " (tag=" + tag + ")";
14943 }
14944 Log.d(VIEW_LOG_TAG, output);
14945
14946 if ((mPrivateFlags & FOCUSED) != 0) {
14947 output = debugIndent(depth) + " FOCUSED";
14948 Log.d(VIEW_LOG_TAG, output);
14949 }
14950
14951 output = debugIndent(depth);
14952 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14953 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14954 + "} ";
14955 Log.d(VIEW_LOG_TAG, output);
14956
14957 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14958 || mPaddingBottom != 0) {
14959 output = debugIndent(depth);
14960 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14961 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14962 Log.d(VIEW_LOG_TAG, output);
14963 }
14964
14965 output = debugIndent(depth);
14966 output += "mMeasureWidth=" + mMeasuredWidth +
14967 " mMeasureHeight=" + mMeasuredHeight;
14968 Log.d(VIEW_LOG_TAG, output);
14969
14970 output = debugIndent(depth);
14971 if (mLayoutParams == null) {
14972 output += "BAD! no layout params";
14973 } else {
14974 output = mLayoutParams.debug(output);
14975 }
14976 Log.d(VIEW_LOG_TAG, output);
14977
14978 output = debugIndent(depth);
14979 output += "flags={";
14980 output += View.printFlags(mViewFlags);
14981 output += "}";
14982 Log.d(VIEW_LOG_TAG, output);
14983
14984 output = debugIndent(depth);
14985 output += "privateFlags={";
14986 output += View.printPrivateFlags(mPrivateFlags);
14987 output += "}";
14988 Log.d(VIEW_LOG_TAG, output);
14989 }
14990
14991 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014992 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014993 *
14994 * @param depth the indentation level
14995 * @return a String containing (depth * 2 + 3) * 2 white spaces
14996 *
14997 * @hide
14998 */
14999 protected static String debugIndent(int depth) {
15000 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15001 for (int i = 0; i < (depth * 2) + 3; i++) {
15002 spaces.append(' ').append(' ');
15003 }
15004 return spaces.toString();
15005 }
15006
15007 /**
15008 * <p>Return the offset of the widget's text baseline from the widget's top
15009 * boundary. If this widget does not support baseline alignment, this
15010 * method returns -1. </p>
15011 *
15012 * @return the offset of the baseline within the widget's bounds or -1
15013 * if baseline alignment is not supported
15014 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015015 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015016 public int getBaseline() {
15017 return -1;
15018 }
15019
15020 /**
15021 * Call this when something has changed which has invalidated the
15022 * layout of this view. This will schedule a layout pass of the view
15023 * tree.
15024 */
15025 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015026 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015027 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015028
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015029 if (mLayoutParams != null) {
15030 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15031 }
15032
15033 if (mParent != null && !mParent.isLayoutRequested()) {
15034 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015035 }
15036 }
15037
15038 /**
15039 * Forces this view to be laid out during the next layout pass.
15040 * This method does not call requestLayout() or forceLayout()
15041 * on the parent.
15042 */
15043 public void forceLayout() {
15044 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015045 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015046 }
15047
15048 /**
15049 * <p>
15050 * This is called to find out how big a view should be. The parent
15051 * supplies constraint information in the width and height parameters.
15052 * </p>
15053 *
15054 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015055 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015056 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015057 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015058 * </p>
15059 *
15060 *
15061 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15062 * parent
15063 * @param heightMeasureSpec Vertical space requirements as imposed by the
15064 * parent
15065 *
15066 * @see #onMeasure(int, int)
15067 */
15068 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15069 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15070 widthMeasureSpec != mOldWidthMeasureSpec ||
15071 heightMeasureSpec != mOldHeightMeasureSpec) {
15072
15073 // first clears the measured dimension flag
15074 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015076 // measure ourselves, this should set the measured dimension flag back
15077 onMeasure(widthMeasureSpec, heightMeasureSpec);
15078
15079 // flag not set, setMeasuredDimension() was not invoked, we raise
15080 // an exception to warn the developer
15081 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15082 throw new IllegalStateException("onMeasure() did not set the"
15083 + " measured dimension by calling"
15084 + " setMeasuredDimension()");
15085 }
15086
15087 mPrivateFlags |= LAYOUT_REQUIRED;
15088 }
15089
15090 mOldWidthMeasureSpec = widthMeasureSpec;
15091 mOldHeightMeasureSpec = heightMeasureSpec;
15092 }
15093
15094 /**
15095 * <p>
15096 * Measure the view and its content to determine the measured width and the
15097 * measured height. This method is invoked by {@link #measure(int, int)} and
15098 * should be overriden by subclasses to provide accurate and efficient
15099 * measurement of their contents.
15100 * </p>
15101 *
15102 * <p>
15103 * <strong>CONTRACT:</strong> When overriding this method, you
15104 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15105 * measured width and height of this view. Failure to do so will trigger an
15106 * <code>IllegalStateException</code>, thrown by
15107 * {@link #measure(int, int)}. Calling the superclass'
15108 * {@link #onMeasure(int, int)} is a valid use.
15109 * </p>
15110 *
15111 * <p>
15112 * The base class implementation of measure defaults to the background size,
15113 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15114 * override {@link #onMeasure(int, int)} to provide better measurements of
15115 * their content.
15116 * </p>
15117 *
15118 * <p>
15119 * If this method is overridden, it is the subclass's responsibility to make
15120 * sure the measured height and width are at least the view's minimum height
15121 * and width ({@link #getSuggestedMinimumHeight()} and
15122 * {@link #getSuggestedMinimumWidth()}).
15123 * </p>
15124 *
15125 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15126 * The requirements are encoded with
15127 * {@link android.view.View.MeasureSpec}.
15128 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15129 * The requirements are encoded with
15130 * {@link android.view.View.MeasureSpec}.
15131 *
15132 * @see #getMeasuredWidth()
15133 * @see #getMeasuredHeight()
15134 * @see #setMeasuredDimension(int, int)
15135 * @see #getSuggestedMinimumHeight()
15136 * @see #getSuggestedMinimumWidth()
15137 * @see android.view.View.MeasureSpec#getMode(int)
15138 * @see android.view.View.MeasureSpec#getSize(int)
15139 */
15140 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15141 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15142 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15143 }
15144
15145 /**
15146 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15147 * measured width and measured height. Failing to do so will trigger an
15148 * exception at measurement time.</p>
15149 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015150 * @param measuredWidth The measured width of this view. May be a complex
15151 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15152 * {@link #MEASURED_STATE_TOO_SMALL}.
15153 * @param measuredHeight The measured height of this view. May be a complex
15154 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15155 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015156 */
15157 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15158 mMeasuredWidth = measuredWidth;
15159 mMeasuredHeight = measuredHeight;
15160
15161 mPrivateFlags |= MEASURED_DIMENSION_SET;
15162 }
15163
15164 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015165 * Merge two states as returned by {@link #getMeasuredState()}.
15166 * @param curState The current state as returned from a view or the result
15167 * of combining multiple views.
15168 * @param newState The new view state to combine.
15169 * @return Returns a new integer reflecting the combination of the two
15170 * states.
15171 */
15172 public static int combineMeasuredStates(int curState, int newState) {
15173 return curState | newState;
15174 }
15175
15176 /**
15177 * Version of {@link #resolveSizeAndState(int, int, int)}
15178 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15179 */
15180 public static int resolveSize(int size, int measureSpec) {
15181 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15182 }
15183
15184 /**
15185 * Utility to reconcile a desired size and state, with constraints imposed
15186 * by a MeasureSpec. Will take the desired size, unless a different size
15187 * is imposed by the constraints. The returned value is a compound integer,
15188 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15189 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15190 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015191 *
15192 * @param size How big the view wants to be
15193 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015194 * @return Size information bit mask as defined by
15195 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015196 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015197 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015198 int result = size;
15199 int specMode = MeasureSpec.getMode(measureSpec);
15200 int specSize = MeasureSpec.getSize(measureSpec);
15201 switch (specMode) {
15202 case MeasureSpec.UNSPECIFIED:
15203 result = size;
15204 break;
15205 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015206 if (specSize < size) {
15207 result = specSize | MEASURED_STATE_TOO_SMALL;
15208 } else {
15209 result = size;
15210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015211 break;
15212 case MeasureSpec.EXACTLY:
15213 result = specSize;
15214 break;
15215 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015216 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015217 }
15218
15219 /**
15220 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015221 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015222 * by the MeasureSpec.
15223 *
15224 * @param size Default size for this view
15225 * @param measureSpec Constraints imposed by the parent
15226 * @return The size this view should be.
15227 */
15228 public static int getDefaultSize(int size, int measureSpec) {
15229 int result = size;
15230 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015231 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015232
15233 switch (specMode) {
15234 case MeasureSpec.UNSPECIFIED:
15235 result = size;
15236 break;
15237 case MeasureSpec.AT_MOST:
15238 case MeasureSpec.EXACTLY:
15239 result = specSize;
15240 break;
15241 }
15242 return result;
15243 }
15244
15245 /**
15246 * Returns the suggested minimum height that the view should use. This
15247 * returns the maximum of the view's minimum height
15248 * and the background's minimum height
15249 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15250 * <p>
15251 * When being used in {@link #onMeasure(int, int)}, the caller should still
15252 * ensure the returned height is within the requirements of the parent.
15253 *
15254 * @return The suggested minimum height of the view.
15255 */
15256 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015257 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015259 }
15260
15261 /**
15262 * Returns the suggested minimum width that the view should use. This
15263 * returns the maximum of the view's minimum width)
15264 * and the background's minimum width
15265 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15266 * <p>
15267 * When being used in {@link #onMeasure(int, int)}, the caller should still
15268 * ensure the returned width is within the requirements of the parent.
15269 *
15270 * @return The suggested minimum width of the view.
15271 */
15272 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015273 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015275
Philip Milne6c8ea062012-04-03 17:38:43 -070015276 /**
15277 * Returns the minimum height of the view.
15278 *
15279 * @return the minimum height the view will try to be.
15280 *
15281 * @see #setMinimumHeight(int)
15282 *
15283 * @attr ref android.R.styleable#View_minHeight
15284 */
15285 public int getMinimumHeight() {
15286 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015287 }
15288
15289 /**
15290 * Sets the minimum height of the view. It is not guaranteed the view will
15291 * be able to achieve this minimum height (for example, if its parent layout
15292 * constrains it with less available height).
15293 *
15294 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015295 *
15296 * @see #getMinimumHeight()
15297 *
15298 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015299 */
15300 public void setMinimumHeight(int minHeight) {
15301 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015302 requestLayout();
15303 }
15304
15305 /**
15306 * Returns the minimum width of the view.
15307 *
15308 * @return the minimum width the view will try to be.
15309 *
15310 * @see #setMinimumWidth(int)
15311 *
15312 * @attr ref android.R.styleable#View_minWidth
15313 */
15314 public int getMinimumWidth() {
15315 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015316 }
15317
15318 /**
15319 * Sets the minimum width of the view. It is not guaranteed the view will
15320 * be able to achieve this minimum width (for example, if its parent layout
15321 * constrains it with less available width).
15322 *
15323 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015324 *
15325 * @see #getMinimumWidth()
15326 *
15327 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015328 */
15329 public void setMinimumWidth(int minWidth) {
15330 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015331 requestLayout();
15332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015333 }
15334
15335 /**
15336 * Get the animation currently associated with this view.
15337 *
15338 * @return The animation that is currently playing or
15339 * scheduled to play for this view.
15340 */
15341 public Animation getAnimation() {
15342 return mCurrentAnimation;
15343 }
15344
15345 /**
15346 * Start the specified animation now.
15347 *
15348 * @param animation the animation to start now
15349 */
15350 public void startAnimation(Animation animation) {
15351 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15352 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015353 invalidateParentCaches();
15354 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015355 }
15356
15357 /**
15358 * Cancels any animations for this view.
15359 */
15360 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015361 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015362 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015364 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015365 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015366 }
15367
15368 /**
15369 * Sets the next animation to play for this view.
15370 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015371 * {@link #startAnimation(android.view.animation.Animation)} instead.
15372 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015373 * control over the start time and invalidation, but you
15374 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015375 * 2) the view's parent (which controls animations on its children)
15376 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015377 * start.
15378 *
15379 * @param animation The next animation, or null.
15380 */
15381 public void setAnimation(Animation animation) {
15382 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015384 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015385 // If the screen is off assume the animation start time is now instead of
15386 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15387 // would cause the animation to start when the screen turns back on
15388 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15389 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15390 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015392 animation.reset();
15393 }
15394 }
15395
15396 /**
15397 * Invoked by a parent ViewGroup to notify the start of the animation
15398 * currently associated with this view. If you override this method,
15399 * always call super.onAnimationStart();
15400 *
15401 * @see #setAnimation(android.view.animation.Animation)
15402 * @see #getAnimation()
15403 */
15404 protected void onAnimationStart() {
15405 mPrivateFlags |= ANIMATION_STARTED;
15406 }
15407
15408 /**
15409 * Invoked by a parent ViewGroup to notify the end of the animation
15410 * currently associated with this view. If you override this method,
15411 * always call super.onAnimationEnd();
15412 *
15413 * @see #setAnimation(android.view.animation.Animation)
15414 * @see #getAnimation()
15415 */
15416 protected void onAnimationEnd() {
15417 mPrivateFlags &= ~ANIMATION_STARTED;
15418 }
15419
15420 /**
15421 * Invoked if there is a Transform that involves alpha. Subclass that can
15422 * draw themselves with the specified alpha should return true, and then
15423 * respect that alpha when their onDraw() is called. If this returns false
15424 * then the view may be redirected to draw into an offscreen buffer to
15425 * fulfill the request, which will look fine, but may be slower than if the
15426 * subclass handles it internally. The default implementation returns false.
15427 *
15428 * @param alpha The alpha (0..255) to apply to the view's drawing
15429 * @return true if the view can draw with the specified alpha.
15430 */
15431 protected boolean onSetAlpha(int alpha) {
15432 return false;
15433 }
15434
15435 /**
15436 * This is used by the RootView to perform an optimization when
15437 * the view hierarchy contains one or several SurfaceView.
15438 * SurfaceView is always considered transparent, but its children are not,
15439 * therefore all View objects remove themselves from the global transparent
15440 * region (passed as a parameter to this function).
15441 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015442 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015443 *
15444 * @return Returns true if the effective visibility of the view at this
15445 * point is opaque, regardless of the transparent region; returns false
15446 * if it is possible for underlying windows to be seen behind the view.
15447 *
15448 * {@hide}
15449 */
15450 public boolean gatherTransparentRegion(Region region) {
15451 final AttachInfo attachInfo = mAttachInfo;
15452 if (region != null && attachInfo != null) {
15453 final int pflags = mPrivateFlags;
15454 if ((pflags & SKIP_DRAW) == 0) {
15455 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15456 // remove it from the transparent region.
15457 final int[] location = attachInfo.mTransparentLocation;
15458 getLocationInWindow(location);
15459 region.op(location[0], location[1], location[0] + mRight - mLeft,
15460 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015461 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015462 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15463 // exists, so we remove the background drawable's non-transparent
15464 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015465 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015466 }
15467 }
15468 return true;
15469 }
15470
15471 /**
15472 * Play a sound effect for this view.
15473 *
15474 * <p>The framework will play sound effects for some built in actions, such as
15475 * clicking, but you may wish to play these effects in your widget,
15476 * for instance, for internal navigation.
15477 *
15478 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15479 * {@link #isSoundEffectsEnabled()} is true.
15480 *
15481 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15482 */
15483 public void playSoundEffect(int soundConstant) {
15484 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15485 return;
15486 }
15487 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15488 }
15489
15490 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015491 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015492 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015493 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015494 *
15495 * <p>The framework will provide haptic feedback for some built in actions,
15496 * such as long presses, but you may wish to provide feedback for your
15497 * own widget.
15498 *
15499 * <p>The feedback will only be performed if
15500 * {@link #isHapticFeedbackEnabled()} is true.
15501 *
15502 * @param feedbackConstant One of the constants defined in
15503 * {@link HapticFeedbackConstants}
15504 */
15505 public boolean performHapticFeedback(int feedbackConstant) {
15506 return performHapticFeedback(feedbackConstant, 0);
15507 }
15508
15509 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015510 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015511 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015512 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015513 *
15514 * @param feedbackConstant One of the constants defined in
15515 * {@link HapticFeedbackConstants}
15516 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15517 */
15518 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15519 if (mAttachInfo == null) {
15520 return false;
15521 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015522 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015523 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015524 && !isHapticFeedbackEnabled()) {
15525 return false;
15526 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015527 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15528 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015529 }
15530
15531 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015532 * Request that the visibility of the status bar or other screen/window
15533 * decorations be changed.
15534 *
15535 * <p>This method is used to put the over device UI into temporary modes
15536 * where the user's attention is focused more on the application content,
15537 * by dimming or hiding surrounding system affordances. This is typically
15538 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15539 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15540 * to be placed behind the action bar (and with these flags other system
15541 * affordances) so that smooth transitions between hiding and showing them
15542 * can be done.
15543 *
15544 * <p>Two representative examples of the use of system UI visibility is
15545 * implementing a content browsing application (like a magazine reader)
15546 * and a video playing application.
15547 *
15548 * <p>The first code shows a typical implementation of a View in a content
15549 * browsing application. In this implementation, the application goes
15550 * into a content-oriented mode by hiding the status bar and action bar,
15551 * and putting the navigation elements into lights out mode. The user can
15552 * then interact with content while in this mode. Such an application should
15553 * provide an easy way for the user to toggle out of the mode (such as to
15554 * check information in the status bar or access notifications). In the
15555 * implementation here, this is done simply by tapping on the content.
15556 *
15557 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15558 * content}
15559 *
15560 * <p>This second code sample shows a typical implementation of a View
15561 * in a video playing application. In this situation, while the video is
15562 * playing the application would like to go into a complete full-screen mode,
15563 * to use as much of the display as possible for the video. When in this state
15564 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015565 * touching on the screen to pop the UI out of full screen mode. See
15566 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015567 *
15568 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15569 * content}
15570 *
15571 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15572 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15573 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15574 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015575 */
15576 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015577 if (visibility != mSystemUiVisibility) {
15578 mSystemUiVisibility = visibility;
15579 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15580 mParent.recomputeViewAttributes(this);
15581 }
Joe Onorato664644d2011-01-23 17:53:23 -080015582 }
15583 }
15584
15585 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015586 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15587 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15588 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15589 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15590 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015591 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015592 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015593 return mSystemUiVisibility;
15594 }
15595
Scott Mainec6331b2011-05-24 16:55:56 -070015596 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015597 * Returns the current system UI visibility that is currently set for
15598 * the entire window. This is the combination of the
15599 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15600 * views in the window.
15601 */
15602 public int getWindowSystemUiVisibility() {
15603 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15604 }
15605
15606 /**
15607 * Override to find out when the window's requested system UI visibility
15608 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15609 * This is different from the callbacks recieved through
15610 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15611 * in that this is only telling you about the local request of the window,
15612 * not the actual values applied by the system.
15613 */
15614 public void onWindowSystemUiVisibilityChanged(int visible) {
15615 }
15616
15617 /**
15618 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15619 * the view hierarchy.
15620 */
15621 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15622 onWindowSystemUiVisibilityChanged(visible);
15623 }
15624
15625 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015626 * Set a listener to receive callbacks when the visibility of the system bar changes.
15627 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15628 */
Joe Onorato664644d2011-01-23 17:53:23 -080015629 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015630 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015631 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15632 mParent.recomputeViewAttributes(this);
15633 }
15634 }
15635
15636 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015637 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15638 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015639 */
15640 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015641 ListenerInfo li = mListenerInfo;
15642 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15643 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015644 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015645 }
15646 }
15647
Dianne Hackborncf675782012-05-10 15:07:24 -070015648 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015649 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15650 if (val != mSystemUiVisibility) {
15651 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015652 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015653 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015654 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015655 }
15656
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015657 /** @hide */
15658 public void setDisabledSystemUiVisibility(int flags) {
15659 if (mAttachInfo != null) {
15660 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15661 mAttachInfo.mDisabledSystemUiVisibility = flags;
15662 if (mParent != null) {
15663 mParent.recomputeViewAttributes(this);
15664 }
15665 }
15666 }
15667 }
15668
Joe Onorato664644d2011-01-23 17:53:23 -080015669 /**
Joe Malin32736f02011-01-19 16:14:20 -080015670 * Creates an image that the system displays during the drag and drop
15671 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15672 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15673 * appearance as the given View. The default also positions the center of the drag shadow
15674 * directly under the touch point. If no View is provided (the constructor with no parameters
15675 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15676 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15677 * default is an invisible drag shadow.
15678 * <p>
15679 * You are not required to use the View you provide to the constructor as the basis of the
15680 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15681 * anything you want as the drag shadow.
15682 * </p>
15683 * <p>
15684 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15685 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15686 * size and position of the drag shadow. It uses this data to construct a
15687 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15688 * so that your application can draw the shadow image in the Canvas.
15689 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015690 *
15691 * <div class="special reference">
15692 * <h3>Developer Guides</h3>
15693 * <p>For a guide to implementing drag and drop features, read the
15694 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15695 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015696 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015697 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015698 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015699
15700 /**
Joe Malin32736f02011-01-19 16:14:20 -080015701 * Constructs a shadow image builder based on a View. By default, the resulting drag
15702 * shadow will have the same appearance and dimensions as the View, with the touch point
15703 * over the center of the View.
15704 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015705 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015706 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015707 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015708 }
15709
Christopher Tate17ed60c2011-01-18 12:50:26 -080015710 /**
15711 * Construct a shadow builder object with no associated View. This
15712 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15713 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15714 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015715 * reference to any View object. If they are not overridden, then the result is an
15716 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015717 */
15718 public DragShadowBuilder() {
15719 mView = new WeakReference<View>(null);
15720 }
15721
15722 /**
15723 * Returns the View object that had been passed to the
15724 * {@link #View.DragShadowBuilder(View)}
15725 * constructor. If that View parameter was {@code null} or if the
15726 * {@link #View.DragShadowBuilder()}
15727 * constructor was used to instantiate the builder object, this method will return
15728 * null.
15729 *
15730 * @return The View object associate with this builder object.
15731 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015732 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015733 final public View getView() {
15734 return mView.get();
15735 }
15736
Christopher Tate2c095f32010-10-04 14:13:40 -070015737 /**
Joe Malin32736f02011-01-19 16:14:20 -080015738 * Provides the metrics for the shadow image. These include the dimensions of
15739 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015740 * be centered under the touch location while dragging.
15741 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015742 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015743 * same as the dimensions of the View itself and centers the shadow under
15744 * the touch point.
15745 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015746 *
Joe Malin32736f02011-01-19 16:14:20 -080015747 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15748 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15749 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15750 * image.
15751 *
15752 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15753 * shadow image that should be underneath the touch point during the drag and drop
15754 * operation. Your application must set {@link android.graphics.Point#x} to the
15755 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015756 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015757 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015758 final View view = mView.get();
15759 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015760 shadowSize.set(view.getWidth(), view.getHeight());
15761 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015762 } else {
15763 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15764 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015765 }
15766
15767 /**
Joe Malin32736f02011-01-19 16:14:20 -080015768 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15769 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015770 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015771 *
Joe Malin32736f02011-01-19 16:14:20 -080015772 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015773 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015774 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015775 final View view = mView.get();
15776 if (view != null) {
15777 view.draw(canvas);
15778 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015779 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015780 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015781 }
15782 }
15783
15784 /**
Joe Malin32736f02011-01-19 16:14:20 -080015785 * Starts a drag and drop operation. When your application calls this method, it passes a
15786 * {@link android.view.View.DragShadowBuilder} object to the system. The
15787 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15788 * to get metrics for the drag shadow, and then calls the object's
15789 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15790 * <p>
15791 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15792 * drag events to all the View objects in your application that are currently visible. It does
15793 * this either by calling the View object's drag listener (an implementation of
15794 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15795 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15796 * Both are passed a {@link android.view.DragEvent} object that has a
15797 * {@link android.view.DragEvent#getAction()} value of
15798 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15799 * </p>
15800 * <p>
15801 * Your application can invoke startDrag() on any attached View object. The View object does not
15802 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15803 * be related to the View the user selected for dragging.
15804 * </p>
15805 * @param data A {@link android.content.ClipData} object pointing to the data to be
15806 * transferred by the drag and drop operation.
15807 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15808 * drag shadow.
15809 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15810 * drop operation. This Object is put into every DragEvent object sent by the system during the
15811 * current drag.
15812 * <p>
15813 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15814 * to the target Views. For example, it can contain flags that differentiate between a
15815 * a copy operation and a move operation.
15816 * </p>
15817 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15818 * so the parameter should be set to 0.
15819 * @return {@code true} if the method completes successfully, or
15820 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15821 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015822 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015823 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015824 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015825 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015826 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015827 }
15828 boolean okay = false;
15829
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015830 Point shadowSize = new Point();
15831 Point shadowTouchPoint = new Point();
15832 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015833
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015834 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15835 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15836 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015837 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015838
Chris Tatea32dcf72010-10-14 12:13:50 -070015839 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015840 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15841 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015842 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015843 Surface surface = new Surface();
15844 try {
15845 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015846 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015847 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015848 + " surface=" + surface);
15849 if (token != null) {
15850 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015851 try {
Chris Tate6b391282010-10-14 15:48:59 -070015852 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015853 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015854 } finally {
15855 surface.unlockCanvasAndPost(canvas);
15856 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015857
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015858 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015859
15860 // Cache the local state object for delivery with DragEvents
15861 root.setLocalDragState(myLocalState);
15862
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015863 // repurpose 'shadowSize' for the last touch point
15864 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015865
Christopher Tatea53146c2010-09-07 11:57:52 -070015866 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015867 shadowSize.x, shadowSize.y,
15868 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015869 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015870
15871 // Off and running! Release our local surface instance; the drag
15872 // shadow surface is now managed by the system process.
15873 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015874 }
15875 } catch (Exception e) {
15876 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15877 surface.destroy();
15878 }
15879
15880 return okay;
15881 }
15882
Christopher Tatea53146c2010-09-07 11:57:52 -070015883 /**
Joe Malin32736f02011-01-19 16:14:20 -080015884 * Handles drag events sent by the system following a call to
15885 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15886 *<p>
15887 * When the system calls this method, it passes a
15888 * {@link android.view.DragEvent} object. A call to
15889 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15890 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15891 * operation.
15892 * @param event The {@link android.view.DragEvent} sent by the system.
15893 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15894 * in DragEvent, indicating the type of drag event represented by this object.
15895 * @return {@code true} if the method was successful, otherwise {@code false}.
15896 * <p>
15897 * The method should return {@code true} in response to an action type of
15898 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15899 * operation.
15900 * </p>
15901 * <p>
15902 * The method should also return {@code true} in response to an action type of
15903 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15904 * {@code false} if it didn't.
15905 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015906 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015907 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015908 return false;
15909 }
15910
15911 /**
Joe Malin32736f02011-01-19 16:14:20 -080015912 * Detects if this View is enabled and has a drag event listener.
15913 * If both are true, then it calls the drag event listener with the
15914 * {@link android.view.DragEvent} it received. If the drag event listener returns
15915 * {@code true}, then dispatchDragEvent() returns {@code true}.
15916 * <p>
15917 * For all other cases, the method calls the
15918 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15919 * method and returns its result.
15920 * </p>
15921 * <p>
15922 * This ensures that a drag event is always consumed, even if the View does not have a drag
15923 * event listener. However, if the View has a listener and the listener returns true, then
15924 * onDragEvent() is not called.
15925 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015926 */
15927 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015928 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015929 ListenerInfo li = mListenerInfo;
15930 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15931 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015932 return true;
15933 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015934 return onDragEvent(event);
15935 }
15936
Christopher Tate3d4bf172011-03-28 16:16:46 -070015937 boolean canAcceptDrag() {
15938 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15939 }
15940
Christopher Tatea53146c2010-09-07 11:57:52 -070015941 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015942 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15943 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015944 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015945 */
15946 public void onCloseSystemDialogs(String reason) {
15947 }
Joe Malin32736f02011-01-19 16:14:20 -080015948
Dianne Hackbornffa42482009-09-23 22:20:11 -070015949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015950 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015951 * update a Region being computed for
15952 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015953 * that any non-transparent parts of the Drawable are removed from the
15954 * given transparent region.
15955 *
15956 * @param dr The Drawable whose transparency is to be applied to the region.
15957 * @param region A Region holding the current transparency information,
15958 * where any parts of the region that are set are considered to be
15959 * transparent. On return, this region will be modified to have the
15960 * transparency information reduced by the corresponding parts of the
15961 * Drawable that are not transparent.
15962 * {@hide}
15963 */
15964 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15965 if (DBG) {
15966 Log.i("View", "Getting transparent region for: " + this);
15967 }
15968 final Region r = dr.getTransparentRegion();
15969 final Rect db = dr.getBounds();
15970 final AttachInfo attachInfo = mAttachInfo;
15971 if (r != null && attachInfo != null) {
15972 final int w = getRight()-getLeft();
15973 final int h = getBottom()-getTop();
15974 if (db.left > 0) {
15975 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15976 r.op(0, 0, db.left, h, Region.Op.UNION);
15977 }
15978 if (db.right < w) {
15979 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15980 r.op(db.right, 0, w, h, Region.Op.UNION);
15981 }
15982 if (db.top > 0) {
15983 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15984 r.op(0, 0, w, db.top, Region.Op.UNION);
15985 }
15986 if (db.bottom < h) {
15987 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15988 r.op(0, db.bottom, w, h, Region.Op.UNION);
15989 }
15990 final int[] location = attachInfo.mTransparentLocation;
15991 getLocationInWindow(location);
15992 r.translate(location[0], location[1]);
15993 region.op(r, Region.Op.INTERSECT);
15994 } else {
15995 region.op(db, Region.Op.DIFFERENCE);
15996 }
15997 }
15998
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015999 private void checkForLongClick(int delayOffset) {
16000 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16001 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016002
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016003 if (mPendingCheckForLongPress == null) {
16004 mPendingCheckForLongPress = new CheckForLongPress();
16005 }
16006 mPendingCheckForLongPress.rememberWindowAttachCount();
16007 postDelayed(mPendingCheckForLongPress,
16008 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016010 }
16011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016012 /**
16013 * Inflate a view from an XML resource. This convenience method wraps the {@link
16014 * LayoutInflater} class, which provides a full range of options for view inflation.
16015 *
16016 * @param context The Context object for your activity or application.
16017 * @param resource The resource ID to inflate
16018 * @param root A view group that will be the parent. Used to properly inflate the
16019 * layout_* parameters.
16020 * @see LayoutInflater
16021 */
16022 public static View inflate(Context context, int resource, ViewGroup root) {
16023 LayoutInflater factory = LayoutInflater.from(context);
16024 return factory.inflate(resource, root);
16025 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016027 /**
Adam Powell637d3372010-08-25 14:37:03 -070016028 * Scroll the view with standard behavior for scrolling beyond the normal
16029 * content boundaries. Views that call this method should override
16030 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16031 * results of an over-scroll operation.
16032 *
16033 * Views can use this method to handle any touch or fling-based scrolling.
16034 *
16035 * @param deltaX Change in X in pixels
16036 * @param deltaY Change in Y in pixels
16037 * @param scrollX Current X scroll value in pixels before applying deltaX
16038 * @param scrollY Current Y scroll value in pixels before applying deltaY
16039 * @param scrollRangeX Maximum content scroll range along the X axis
16040 * @param scrollRangeY Maximum content scroll range along the Y axis
16041 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16042 * along the X axis.
16043 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16044 * along the Y axis.
16045 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16046 * @return true if scrolling was clamped to an over-scroll boundary along either
16047 * axis, false otherwise.
16048 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016049 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016050 protected boolean overScrollBy(int deltaX, int deltaY,
16051 int scrollX, int scrollY,
16052 int scrollRangeX, int scrollRangeY,
16053 int maxOverScrollX, int maxOverScrollY,
16054 boolean isTouchEvent) {
16055 final int overScrollMode = mOverScrollMode;
16056 final boolean canScrollHorizontal =
16057 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16058 final boolean canScrollVertical =
16059 computeVerticalScrollRange() > computeVerticalScrollExtent();
16060 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16061 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16062 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16063 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16064
16065 int newScrollX = scrollX + deltaX;
16066 if (!overScrollHorizontal) {
16067 maxOverScrollX = 0;
16068 }
16069
16070 int newScrollY = scrollY + deltaY;
16071 if (!overScrollVertical) {
16072 maxOverScrollY = 0;
16073 }
16074
16075 // Clamp values if at the limits and record
16076 final int left = -maxOverScrollX;
16077 final int right = maxOverScrollX + scrollRangeX;
16078 final int top = -maxOverScrollY;
16079 final int bottom = maxOverScrollY + scrollRangeY;
16080
16081 boolean clampedX = false;
16082 if (newScrollX > right) {
16083 newScrollX = right;
16084 clampedX = true;
16085 } else if (newScrollX < left) {
16086 newScrollX = left;
16087 clampedX = true;
16088 }
16089
16090 boolean clampedY = false;
16091 if (newScrollY > bottom) {
16092 newScrollY = bottom;
16093 clampedY = true;
16094 } else if (newScrollY < top) {
16095 newScrollY = top;
16096 clampedY = true;
16097 }
16098
16099 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16100
16101 return clampedX || clampedY;
16102 }
16103
16104 /**
16105 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16106 * respond to the results of an over-scroll operation.
16107 *
16108 * @param scrollX New X scroll value in pixels
16109 * @param scrollY New Y scroll value in pixels
16110 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16111 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16112 */
16113 protected void onOverScrolled(int scrollX, int scrollY,
16114 boolean clampedX, boolean clampedY) {
16115 // Intentionally empty.
16116 }
16117
16118 /**
16119 * Returns the over-scroll mode for this view. The result will be
16120 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16121 * (allow over-scrolling only if the view content is larger than the container),
16122 * or {@link #OVER_SCROLL_NEVER}.
16123 *
16124 * @return This view's over-scroll mode.
16125 */
16126 public int getOverScrollMode() {
16127 return mOverScrollMode;
16128 }
16129
16130 /**
16131 * Set the over-scroll mode for this view. Valid over-scroll modes are
16132 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16133 * (allow over-scrolling only if the view content is larger than the container),
16134 * or {@link #OVER_SCROLL_NEVER}.
16135 *
16136 * Setting the over-scroll mode of a view will have an effect only if the
16137 * view is capable of scrolling.
16138 *
16139 * @param overScrollMode The new over-scroll mode for this view.
16140 */
16141 public void setOverScrollMode(int overScrollMode) {
16142 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16143 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16144 overScrollMode != OVER_SCROLL_NEVER) {
16145 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16146 }
16147 mOverScrollMode = overScrollMode;
16148 }
16149
16150 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016151 * Gets a scale factor that determines the distance the view should scroll
16152 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16153 * @return The vertical scroll scale factor.
16154 * @hide
16155 */
16156 protected float getVerticalScrollFactor() {
16157 if (mVerticalScrollFactor == 0) {
16158 TypedValue outValue = new TypedValue();
16159 if (!mContext.getTheme().resolveAttribute(
16160 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16161 throw new IllegalStateException(
16162 "Expected theme to define listPreferredItemHeight.");
16163 }
16164 mVerticalScrollFactor = outValue.getDimension(
16165 mContext.getResources().getDisplayMetrics());
16166 }
16167 return mVerticalScrollFactor;
16168 }
16169
16170 /**
16171 * Gets a scale factor that determines the distance the view should scroll
16172 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16173 * @return The horizontal scroll scale factor.
16174 * @hide
16175 */
16176 protected float getHorizontalScrollFactor() {
16177 // TODO: Should use something else.
16178 return getVerticalScrollFactor();
16179 }
16180
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016181 /**
16182 * Return the value specifying the text direction or policy that was set with
16183 * {@link #setTextDirection(int)}.
16184 *
16185 * @return the defined text direction. It can be one of:
16186 *
16187 * {@link #TEXT_DIRECTION_INHERIT},
16188 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16189 * {@link #TEXT_DIRECTION_ANY_RTL},
16190 * {@link #TEXT_DIRECTION_LTR},
16191 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016192 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016193 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016194 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016195 @ViewDebug.ExportedProperty(category = "text", mapping = {
16196 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16197 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16198 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16199 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16200 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16201 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16202 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016203 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016204 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016205 }
16206
16207 /**
16208 * Set the text direction.
16209 *
16210 * @param textDirection the direction to set. Should be one of:
16211 *
16212 * {@link #TEXT_DIRECTION_INHERIT},
16213 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16214 * {@link #TEXT_DIRECTION_ANY_RTL},
16215 * {@link #TEXT_DIRECTION_LTR},
16216 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016217 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016218 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016219 */
16220 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016221 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016222 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016223 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016224 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016225 // Set the new text direction
16226 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016227 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016228 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016229 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016230 }
16231 }
16232
16233 /**
16234 * Return the resolved text direction.
16235 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016236 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16237 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16238 * up the parent chain of the view. if there is no parent, then it will return the default
16239 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16240 *
16241 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016242 *
Doug Feltcb3791202011-07-07 11:57:48 -070016243 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16244 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016245 * {@link #TEXT_DIRECTION_LTR},
16246 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016247 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016248 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016249 */
16250 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016251 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016252 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016253 resolveTextDirection();
16254 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016255 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016256 }
16257
16258 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016259 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16260 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016261 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016262 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016263 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016264 // Reset any previous text direction resolution
16265 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16266
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016267 if (hasRtlSupport()) {
16268 // Set resolved text direction flag depending on text direction flag
16269 final int textDirection = getTextDirection();
16270 switch(textDirection) {
16271 case TEXT_DIRECTION_INHERIT:
16272 if (canResolveTextDirection()) {
16273 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016274
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016275 // Set current resolved direction to the same value as the parent's one
16276 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16277 switch (parentResolvedDirection) {
16278 case TEXT_DIRECTION_FIRST_STRONG:
16279 case TEXT_DIRECTION_ANY_RTL:
16280 case TEXT_DIRECTION_LTR:
16281 case TEXT_DIRECTION_RTL:
16282 case TEXT_DIRECTION_LOCALE:
16283 mPrivateFlags2 |=
16284 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16285 break;
16286 default:
16287 // Default resolved direction is "first strong" heuristic
16288 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16289 }
16290 } else {
16291 // We cannot do the resolution if there is no parent, so use the default one
16292 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016293 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016294 break;
16295 case TEXT_DIRECTION_FIRST_STRONG:
16296 case TEXT_DIRECTION_ANY_RTL:
16297 case TEXT_DIRECTION_LTR:
16298 case TEXT_DIRECTION_RTL:
16299 case TEXT_DIRECTION_LOCALE:
16300 // Resolved direction is the same as text direction
16301 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16302 break;
16303 default:
16304 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016305 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016306 }
16307 } else {
16308 // Default resolved direction is "first strong" heuristic
16309 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016310 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016311
16312 // Set to resolved
16313 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016314 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016315 }
16316
16317 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016318 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016319 * resolution should override this method.
16320 *
16321 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016322 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016323 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016324 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016325 }
16326
16327 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016328 * Check if text direction resolution can be done.
16329 *
16330 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016331 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016332 */
16333 public boolean canResolveTextDirection() {
16334 switch (getTextDirection()) {
16335 case TEXT_DIRECTION_INHERIT:
16336 return (mParent != null) && (mParent instanceof ViewGroup);
16337 default:
16338 return true;
16339 }
16340 }
16341
16342 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016343 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016344 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016345 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016346 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016347 */
16348 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016349 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016350 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016351 }
16352
16353 /**
16354 * Called when text direction is reset. Subclasses that care about text direction reset should
16355 * override this method and do a reset of the text direction of their children. The default
16356 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016357 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016358 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016359 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016360 }
16361
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016362 /**
16363 * Return the value specifying the text alignment or policy that was set with
16364 * {@link #setTextAlignment(int)}.
16365 *
16366 * @return the defined text alignment. It can be one of:
16367 *
16368 * {@link #TEXT_ALIGNMENT_INHERIT},
16369 * {@link #TEXT_ALIGNMENT_GRAVITY},
16370 * {@link #TEXT_ALIGNMENT_CENTER},
16371 * {@link #TEXT_ALIGNMENT_TEXT_START},
16372 * {@link #TEXT_ALIGNMENT_TEXT_END},
16373 * {@link #TEXT_ALIGNMENT_VIEW_START},
16374 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016375 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016376 */
16377 @ViewDebug.ExportedProperty(category = "text", mapping = {
16378 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16379 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16380 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16381 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16382 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16383 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16384 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16385 })
16386 public int getTextAlignment() {
16387 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16388 }
16389
16390 /**
16391 * Set the text alignment.
16392 *
16393 * @param textAlignment The text alignment to set. Should be one of
16394 *
16395 * {@link #TEXT_ALIGNMENT_INHERIT},
16396 * {@link #TEXT_ALIGNMENT_GRAVITY},
16397 * {@link #TEXT_ALIGNMENT_CENTER},
16398 * {@link #TEXT_ALIGNMENT_TEXT_START},
16399 * {@link #TEXT_ALIGNMENT_TEXT_END},
16400 * {@link #TEXT_ALIGNMENT_VIEW_START},
16401 * {@link #TEXT_ALIGNMENT_VIEW_END}
16402 *
16403 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016404 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016405 */
16406 public void setTextAlignment(int textAlignment) {
16407 if (textAlignment != getTextAlignment()) {
16408 // Reset the current and resolved text alignment
16409 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16410 resetResolvedTextAlignment();
16411 // Set the new text alignment
16412 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16413 // Refresh
16414 requestLayout();
16415 invalidate(true);
16416 }
16417 }
16418
16419 /**
16420 * Return the resolved text alignment.
16421 *
16422 * The resolved text alignment. This needs resolution if the value is
16423 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16424 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16425 *
16426 * @return the resolved text alignment. Returns one of:
16427 *
16428 * {@link #TEXT_ALIGNMENT_GRAVITY},
16429 * {@link #TEXT_ALIGNMENT_CENTER},
16430 * {@link #TEXT_ALIGNMENT_TEXT_START},
16431 * {@link #TEXT_ALIGNMENT_TEXT_END},
16432 * {@link #TEXT_ALIGNMENT_VIEW_START},
16433 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016434 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016435 */
16436 @ViewDebug.ExportedProperty(category = "text", mapping = {
16437 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16438 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16439 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16440 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16441 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16442 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16443 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16444 })
16445 public int getResolvedTextAlignment() {
16446 // If text alignment is not resolved, then resolve it
16447 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16448 resolveTextAlignment();
16449 }
16450 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16451 }
16452
16453 /**
16454 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16455 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016456 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016457 */
16458 public void resolveTextAlignment() {
16459 // Reset any previous text alignment resolution
16460 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16461
16462 if (hasRtlSupport()) {
16463 // Set resolved text alignment flag depending on text alignment flag
16464 final int textAlignment = getTextAlignment();
16465 switch (textAlignment) {
16466 case TEXT_ALIGNMENT_INHERIT:
16467 // Check if we can resolve the text alignment
16468 if (canResolveLayoutDirection() && mParent instanceof View) {
16469 View view = (View) mParent;
16470
16471 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16472 switch (parentResolvedTextAlignment) {
16473 case TEXT_ALIGNMENT_GRAVITY:
16474 case TEXT_ALIGNMENT_TEXT_START:
16475 case TEXT_ALIGNMENT_TEXT_END:
16476 case TEXT_ALIGNMENT_CENTER:
16477 case TEXT_ALIGNMENT_VIEW_START:
16478 case TEXT_ALIGNMENT_VIEW_END:
16479 // Resolved text alignment is the same as the parent resolved
16480 // text alignment
16481 mPrivateFlags2 |=
16482 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16483 break;
16484 default:
16485 // Use default resolved text alignment
16486 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16487 }
16488 }
16489 else {
16490 // We cannot do the resolution if there is no parent so use the default
16491 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16492 }
16493 break;
16494 case TEXT_ALIGNMENT_GRAVITY:
16495 case TEXT_ALIGNMENT_TEXT_START:
16496 case TEXT_ALIGNMENT_TEXT_END:
16497 case TEXT_ALIGNMENT_CENTER:
16498 case TEXT_ALIGNMENT_VIEW_START:
16499 case TEXT_ALIGNMENT_VIEW_END:
16500 // Resolved text alignment is the same as text alignment
16501 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16502 break;
16503 default:
16504 // Use default resolved text alignment
16505 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16506 }
16507 } else {
16508 // Use default resolved text alignment
16509 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16510 }
16511
16512 // Set the resolved
16513 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16514 onResolvedTextAlignmentChanged();
16515 }
16516
16517 /**
16518 * Check if text alignment resolution can be done.
16519 *
16520 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016521 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016522 */
16523 public boolean canResolveTextAlignment() {
16524 switch (getTextAlignment()) {
16525 case TEXT_DIRECTION_INHERIT:
16526 return (mParent != null);
16527 default:
16528 return true;
16529 }
16530 }
16531
16532 /**
16533 * Called when text alignment has been resolved. Subclasses that care about text alignment
16534 * resolution should override this method.
16535 *
16536 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016537 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016538 */
16539 public void onResolvedTextAlignmentChanged() {
16540 }
16541
16542 /**
16543 * Reset resolved text alignment. Text alignment can be resolved with a call to
16544 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16545 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016546 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016547 */
16548 public void resetResolvedTextAlignment() {
16549 // Reset any previous text alignment resolution
16550 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16551 onResolvedTextAlignmentReset();
16552 }
16553
16554 /**
16555 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16556 * override this method and do a reset of the text alignment of their children. The default
16557 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016558 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016559 */
16560 public void onResolvedTextAlignmentReset() {
16561 }
16562
Chet Haaseb39f0512011-05-24 14:36:40 -070016563 //
16564 // Properties
16565 //
16566 /**
16567 * A Property wrapper around the <code>alpha</code> functionality handled by the
16568 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16569 */
Chet Haased47f1532011-12-16 11:18:52 -080016570 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016571 @Override
16572 public void setValue(View object, float value) {
16573 object.setAlpha(value);
16574 }
16575
16576 @Override
16577 public Float get(View object) {
16578 return object.getAlpha();
16579 }
16580 };
16581
16582 /**
16583 * A Property wrapper around the <code>translationX</code> functionality handled by the
16584 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16585 */
Chet Haased47f1532011-12-16 11:18:52 -080016586 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016587 @Override
16588 public void setValue(View object, float value) {
16589 object.setTranslationX(value);
16590 }
16591
16592 @Override
16593 public Float get(View object) {
16594 return object.getTranslationX();
16595 }
16596 };
16597
16598 /**
16599 * A Property wrapper around the <code>translationY</code> functionality handled by the
16600 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16601 */
Chet Haased47f1532011-12-16 11:18:52 -080016602 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016603 @Override
16604 public void setValue(View object, float value) {
16605 object.setTranslationY(value);
16606 }
16607
16608 @Override
16609 public Float get(View object) {
16610 return object.getTranslationY();
16611 }
16612 };
16613
16614 /**
16615 * A Property wrapper around the <code>x</code> functionality handled by the
16616 * {@link View#setX(float)} and {@link View#getX()} methods.
16617 */
Chet Haased47f1532011-12-16 11:18:52 -080016618 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016619 @Override
16620 public void setValue(View object, float value) {
16621 object.setX(value);
16622 }
16623
16624 @Override
16625 public Float get(View object) {
16626 return object.getX();
16627 }
16628 };
16629
16630 /**
16631 * A Property wrapper around the <code>y</code> functionality handled by the
16632 * {@link View#setY(float)} and {@link View#getY()} methods.
16633 */
Chet Haased47f1532011-12-16 11:18:52 -080016634 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016635 @Override
16636 public void setValue(View object, float value) {
16637 object.setY(value);
16638 }
16639
16640 @Override
16641 public Float get(View object) {
16642 return object.getY();
16643 }
16644 };
16645
16646 /**
16647 * A Property wrapper around the <code>rotation</code> functionality handled by the
16648 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16649 */
Chet Haased47f1532011-12-16 11:18:52 -080016650 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016651 @Override
16652 public void setValue(View object, float value) {
16653 object.setRotation(value);
16654 }
16655
16656 @Override
16657 public Float get(View object) {
16658 return object.getRotation();
16659 }
16660 };
16661
16662 /**
16663 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16664 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16665 */
Chet Haased47f1532011-12-16 11:18:52 -080016666 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016667 @Override
16668 public void setValue(View object, float value) {
16669 object.setRotationX(value);
16670 }
16671
16672 @Override
16673 public Float get(View object) {
16674 return object.getRotationX();
16675 }
16676 };
16677
16678 /**
16679 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16680 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16681 */
Chet Haased47f1532011-12-16 11:18:52 -080016682 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016683 @Override
16684 public void setValue(View object, float value) {
16685 object.setRotationY(value);
16686 }
16687
16688 @Override
16689 public Float get(View object) {
16690 return object.getRotationY();
16691 }
16692 };
16693
16694 /**
16695 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16696 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16697 */
Chet Haased47f1532011-12-16 11:18:52 -080016698 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016699 @Override
16700 public void setValue(View object, float value) {
16701 object.setScaleX(value);
16702 }
16703
16704 @Override
16705 public Float get(View object) {
16706 return object.getScaleX();
16707 }
16708 };
16709
16710 /**
16711 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16712 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16713 */
Chet Haased47f1532011-12-16 11:18:52 -080016714 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016715 @Override
16716 public void setValue(View object, float value) {
16717 object.setScaleY(value);
16718 }
16719
16720 @Override
16721 public Float get(View object) {
16722 return object.getScaleY();
16723 }
16724 };
16725
Jeff Brown33bbfd22011-02-24 20:55:35 -080016726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016727 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16728 * Each MeasureSpec represents a requirement for either the width or the height.
16729 * A MeasureSpec is comprised of a size and a mode. There are three possible
16730 * modes:
16731 * <dl>
16732 * <dt>UNSPECIFIED</dt>
16733 * <dd>
16734 * The parent has not imposed any constraint on the child. It can be whatever size
16735 * it wants.
16736 * </dd>
16737 *
16738 * <dt>EXACTLY</dt>
16739 * <dd>
16740 * The parent has determined an exact size for the child. The child is going to be
16741 * given those bounds regardless of how big it wants to be.
16742 * </dd>
16743 *
16744 * <dt>AT_MOST</dt>
16745 * <dd>
16746 * The child can be as large as it wants up to the specified size.
16747 * </dd>
16748 * </dl>
16749 *
16750 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16751 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16752 */
16753 public static class MeasureSpec {
16754 private static final int MODE_SHIFT = 30;
16755 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16756
16757 /**
16758 * Measure specification mode: The parent has not imposed any constraint
16759 * on the child. It can be whatever size it wants.
16760 */
16761 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16762
16763 /**
16764 * Measure specification mode: The parent has determined an exact size
16765 * for the child. The child is going to be given those bounds regardless
16766 * of how big it wants to be.
16767 */
16768 public static final int EXACTLY = 1 << MODE_SHIFT;
16769
16770 /**
16771 * Measure specification mode: The child can be as large as it wants up
16772 * to the specified size.
16773 */
16774 public static final int AT_MOST = 2 << MODE_SHIFT;
16775
16776 /**
16777 * Creates a measure specification based on the supplied size and mode.
16778 *
16779 * The mode must always be one of the following:
16780 * <ul>
16781 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16782 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16783 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16784 * </ul>
16785 *
16786 * @param size the size of the measure specification
16787 * @param mode the mode of the measure specification
16788 * @return the measure specification based on size and mode
16789 */
16790 public static int makeMeasureSpec(int size, int mode) {
16791 return size + mode;
16792 }
16793
16794 /**
16795 * Extracts the mode from the supplied measure specification.
16796 *
16797 * @param measureSpec the measure specification to extract the mode from
16798 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16799 * {@link android.view.View.MeasureSpec#AT_MOST} or
16800 * {@link android.view.View.MeasureSpec#EXACTLY}
16801 */
16802 public static int getMode(int measureSpec) {
16803 return (measureSpec & MODE_MASK);
16804 }
16805
16806 /**
16807 * Extracts the size from the supplied measure specification.
16808 *
16809 * @param measureSpec the measure specification to extract the size from
16810 * @return the size in pixels defined in the supplied measure specification
16811 */
16812 public static int getSize(int measureSpec) {
16813 return (measureSpec & ~MODE_MASK);
16814 }
16815
16816 /**
16817 * Returns a String representation of the specified measure
16818 * specification.
16819 *
16820 * @param measureSpec the measure specification to convert to a String
16821 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16822 */
16823 public static String toString(int measureSpec) {
16824 int mode = getMode(measureSpec);
16825 int size = getSize(measureSpec);
16826
16827 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16828
16829 if (mode == UNSPECIFIED)
16830 sb.append("UNSPECIFIED ");
16831 else if (mode == EXACTLY)
16832 sb.append("EXACTLY ");
16833 else if (mode == AT_MOST)
16834 sb.append("AT_MOST ");
16835 else
16836 sb.append(mode).append(" ");
16837
16838 sb.append(size);
16839 return sb.toString();
16840 }
16841 }
16842
16843 class CheckForLongPress implements Runnable {
16844
16845 private int mOriginalWindowAttachCount;
16846
16847 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016848 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016849 && mOriginalWindowAttachCount == mWindowAttachCount) {
16850 if (performLongClick()) {
16851 mHasPerformedLongPress = true;
16852 }
16853 }
16854 }
16855
16856 public void rememberWindowAttachCount() {
16857 mOriginalWindowAttachCount = mWindowAttachCount;
16858 }
16859 }
Joe Malin32736f02011-01-19 16:14:20 -080016860
Adam Powelle14579b2009-12-16 18:39:52 -080016861 private final class CheckForTap implements Runnable {
16862 public void run() {
16863 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016864 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016865 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016866 }
16867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016868
Adam Powella35d7682010-03-12 14:48:13 -080016869 private final class PerformClick implements Runnable {
16870 public void run() {
16871 performClick();
16872 }
16873 }
16874
Dianne Hackborn63042d62011-01-26 18:56:29 -080016875 /** @hide */
16876 public void hackTurnOffWindowResizeAnim(boolean off) {
16877 mAttachInfo.mTurnOffWindowResizeAnim = off;
16878 }
Joe Malin32736f02011-01-19 16:14:20 -080016879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016880 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016881 * This method returns a ViewPropertyAnimator object, which can be used to animate
16882 * specific properties on this View.
16883 *
16884 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16885 */
16886 public ViewPropertyAnimator animate() {
16887 if (mAnimator == null) {
16888 mAnimator = new ViewPropertyAnimator(this);
16889 }
16890 return mAnimator;
16891 }
16892
16893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016894 * Interface definition for a callback to be invoked when a key event is
16895 * dispatched to this view. The callback will be invoked before the key
16896 * event is given to the view.
16897 */
16898 public interface OnKeyListener {
16899 /**
16900 * Called when a key is dispatched to a view. This allows listeners to
16901 * get a chance to respond before the target view.
16902 *
16903 * @param v The view the key has been dispatched to.
16904 * @param keyCode The code for the physical key that was pressed
16905 * @param event The KeyEvent object containing full information about
16906 * the event.
16907 * @return True if the listener has consumed the event, false otherwise.
16908 */
16909 boolean onKey(View v, int keyCode, KeyEvent event);
16910 }
16911
16912 /**
16913 * Interface definition for a callback to be invoked when a touch event is
16914 * dispatched to this view. The callback will be invoked before the touch
16915 * event is given to the view.
16916 */
16917 public interface OnTouchListener {
16918 /**
16919 * Called when a touch event is dispatched to a view. This allows listeners to
16920 * get a chance to respond before the target view.
16921 *
16922 * @param v The view the touch event has been dispatched to.
16923 * @param event The MotionEvent object containing full information about
16924 * the event.
16925 * @return True if the listener has consumed the event, false otherwise.
16926 */
16927 boolean onTouch(View v, MotionEvent event);
16928 }
16929
16930 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016931 * Interface definition for a callback to be invoked when a hover event is
16932 * dispatched to this view. The callback will be invoked before the hover
16933 * event is given to the view.
16934 */
16935 public interface OnHoverListener {
16936 /**
16937 * Called when a hover event is dispatched to a view. This allows listeners to
16938 * get a chance to respond before the target view.
16939 *
16940 * @param v The view the hover event has been dispatched to.
16941 * @param event The MotionEvent object containing full information about
16942 * the event.
16943 * @return True if the listener has consumed the event, false otherwise.
16944 */
16945 boolean onHover(View v, MotionEvent event);
16946 }
16947
16948 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016949 * Interface definition for a callback to be invoked when a generic motion event is
16950 * dispatched to this view. The callback will be invoked before the generic motion
16951 * event is given to the view.
16952 */
16953 public interface OnGenericMotionListener {
16954 /**
16955 * Called when a generic motion event is dispatched to a view. This allows listeners to
16956 * get a chance to respond before the target view.
16957 *
16958 * @param v The view the generic motion event has been dispatched to.
16959 * @param event The MotionEvent object containing full information about
16960 * the event.
16961 * @return True if the listener has consumed the event, false otherwise.
16962 */
16963 boolean onGenericMotion(View v, MotionEvent event);
16964 }
16965
16966 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016967 * Interface definition for a callback to be invoked when a view has been clicked and held.
16968 */
16969 public interface OnLongClickListener {
16970 /**
16971 * Called when a view has been clicked and held.
16972 *
16973 * @param v The view that was clicked and held.
16974 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016975 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016976 */
16977 boolean onLongClick(View v);
16978 }
16979
16980 /**
Chris Tate32affef2010-10-18 15:29:21 -070016981 * Interface definition for a callback to be invoked when a drag is being dispatched
16982 * to this view. The callback will be invoked before the hosting view's own
16983 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16984 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016985 *
16986 * <div class="special reference">
16987 * <h3>Developer Guides</h3>
16988 * <p>For a guide to implementing drag and drop features, read the
16989 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16990 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016991 */
16992 public interface OnDragListener {
16993 /**
16994 * Called when a drag event is dispatched to a view. This allows listeners
16995 * to get a chance to override base View behavior.
16996 *
Joe Malin32736f02011-01-19 16:14:20 -080016997 * @param v The View that received the drag event.
16998 * @param event The {@link android.view.DragEvent} object for the drag event.
16999 * @return {@code true} if the drag event was handled successfully, or {@code false}
17000 * if the drag event was not handled. Note that {@code false} will trigger the View
17001 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017002 */
17003 boolean onDrag(View v, DragEvent event);
17004 }
17005
17006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017007 * Interface definition for a callback to be invoked when the focus state of
17008 * a view changed.
17009 */
17010 public interface OnFocusChangeListener {
17011 /**
17012 * Called when the focus state of a view has changed.
17013 *
17014 * @param v The view whose state has changed.
17015 * @param hasFocus The new focus state of v.
17016 */
17017 void onFocusChange(View v, boolean hasFocus);
17018 }
17019
17020 /**
17021 * Interface definition for a callback to be invoked when a view is clicked.
17022 */
17023 public interface OnClickListener {
17024 /**
17025 * Called when a view has been clicked.
17026 *
17027 * @param v The view that was clicked.
17028 */
17029 void onClick(View v);
17030 }
17031
17032 /**
17033 * Interface definition for a callback to be invoked when the context menu
17034 * for this view is being built.
17035 */
17036 public interface OnCreateContextMenuListener {
17037 /**
17038 * Called when the context menu for this view is being built. It is not
17039 * safe to hold onto the menu after this method returns.
17040 *
17041 * @param menu The context menu that is being built
17042 * @param v The view for which the context menu is being built
17043 * @param menuInfo Extra information about the item for which the
17044 * context menu should be shown. This information will vary
17045 * depending on the class of v.
17046 */
17047 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17048 }
17049
Joe Onorato664644d2011-01-23 17:53:23 -080017050 /**
17051 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017052 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017053 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017054 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017055 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017056 */
17057 public interface OnSystemUiVisibilityChangeListener {
17058 /**
17059 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017060 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017061 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017062 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17063 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17064 * This tells you the <strong>global</strong> state of these UI visibility
17065 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017066 */
17067 public void onSystemUiVisibilityChange(int visibility);
17068 }
17069
Adam Powell4afd62b2011-02-18 15:02:18 -080017070 /**
17071 * Interface definition for a callback to be invoked when this view is attached
17072 * or detached from its window.
17073 */
17074 public interface OnAttachStateChangeListener {
17075 /**
17076 * Called when the view is attached to a window.
17077 * @param v The view that was attached
17078 */
17079 public void onViewAttachedToWindow(View v);
17080 /**
17081 * Called when the view is detached from a window.
17082 * @param v The view that was detached
17083 */
17084 public void onViewDetachedFromWindow(View v);
17085 }
17086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017087 private final class UnsetPressedState implements Runnable {
17088 public void run() {
17089 setPressed(false);
17090 }
17091 }
17092
17093 /**
17094 * Base class for derived classes that want to save and restore their own
17095 * state in {@link android.view.View#onSaveInstanceState()}.
17096 */
17097 public static class BaseSavedState extends AbsSavedState {
17098 /**
17099 * Constructor used when reading from a parcel. Reads the state of the superclass.
17100 *
17101 * @param source
17102 */
17103 public BaseSavedState(Parcel source) {
17104 super(source);
17105 }
17106
17107 /**
17108 * Constructor called by derived classes when creating their SavedState objects
17109 *
17110 * @param superState The state of the superclass of this view
17111 */
17112 public BaseSavedState(Parcelable superState) {
17113 super(superState);
17114 }
17115
17116 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17117 new Parcelable.Creator<BaseSavedState>() {
17118 public BaseSavedState createFromParcel(Parcel in) {
17119 return new BaseSavedState(in);
17120 }
17121
17122 public BaseSavedState[] newArray(int size) {
17123 return new BaseSavedState[size];
17124 }
17125 };
17126 }
17127
17128 /**
17129 * A set of information given to a view when it is attached to its parent
17130 * window.
17131 */
17132 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017133 interface Callbacks {
17134 void playSoundEffect(int effectId);
17135 boolean performHapticFeedback(int effectId, boolean always);
17136 }
17137
17138 /**
17139 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17140 * to a Handler. This class contains the target (View) to invalidate and
17141 * the coordinates of the dirty rectangle.
17142 *
17143 * For performance purposes, this class also implements a pool of up to
17144 * POOL_LIMIT objects that get reused. This reduces memory allocations
17145 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017146 */
Romain Guyd928d682009-03-31 17:52:16 -070017147 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017148 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017149 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17150 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017151 public InvalidateInfo newInstance() {
17152 return new InvalidateInfo();
17153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017154
Romain Guyd928d682009-03-31 17:52:16 -070017155 public void onAcquired(InvalidateInfo element) {
17156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017157
Romain Guyd928d682009-03-31 17:52:16 -070017158 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017159 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017160 }
17161 }, POOL_LIMIT)
17162 );
17163
17164 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017165 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017166
17167 View target;
17168
17169 int left;
17170 int top;
17171 int right;
17172 int bottom;
17173
Romain Guyd928d682009-03-31 17:52:16 -070017174 public void setNextPoolable(InvalidateInfo element) {
17175 mNext = element;
17176 }
17177
17178 public InvalidateInfo getNextPoolable() {
17179 return mNext;
17180 }
17181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017182 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017183 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017184 }
17185
17186 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017187 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017188 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017189
17190 public boolean isPooled() {
17191 return mIsPooled;
17192 }
17193
17194 public void setPooled(boolean isPooled) {
17195 mIsPooled = isPooled;
17196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017197 }
17198
17199 final IWindowSession mSession;
17200
17201 final IWindow mWindow;
17202
17203 final IBinder mWindowToken;
17204
17205 final Callbacks mRootCallbacks;
17206
Romain Guy59a12ca2011-06-09 17:48:21 -070017207 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017209 /**
17210 * The top view of the hierarchy.
17211 */
17212 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017214 IBinder mPanelParentWindowToken;
17215 Surface mSurface;
17216
Romain Guyb051e892010-09-28 19:09:36 -070017217 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017218 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017219 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017220
Romain Guy7e4e5612012-03-05 14:37:29 -080017221 boolean mScreenOn;
17222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017223 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017224 * Scale factor used by the compatibility mode
17225 */
17226 float mApplicationScale;
17227
17228 /**
17229 * Indicates whether the application is in compatibility mode
17230 */
17231 boolean mScalingRequired;
17232
17233 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017234 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017235 */
17236 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017237
Dianne Hackborn63042d62011-01-26 18:56:29 -080017238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017239 * Left position of this view's window
17240 */
17241 int mWindowLeft;
17242
17243 /**
17244 * Top position of this view's window
17245 */
17246 int mWindowTop;
17247
17248 /**
Adam Powell26153a32010-11-08 15:22:27 -080017249 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017250 */
Adam Powell26153a32010-11-08 15:22:27 -080017251 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017252
17253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017254 * For windows that are full-screen but using insets to layout inside
17255 * of the screen decorations, these are the current insets for the
17256 * content of the window.
17257 */
17258 final Rect mContentInsets = new Rect();
17259
17260 /**
17261 * For windows that are full-screen but using insets to layout inside
17262 * of the screen decorations, these are the current insets for the
17263 * actual visible parts of the window.
17264 */
17265 final Rect mVisibleInsets = new Rect();
17266
17267 /**
17268 * The internal insets given by this window. This value is
17269 * supplied by the client (through
17270 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17271 * be given to the window manager when changed to be used in laying
17272 * out windows behind it.
17273 */
17274 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17275 = new ViewTreeObserver.InternalInsetsInfo();
17276
17277 /**
17278 * All views in the window's hierarchy that serve as scroll containers,
17279 * used to determine if the window can be resized or must be panned
17280 * to adjust for a soft input area.
17281 */
17282 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17283
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017284 final KeyEvent.DispatcherState mKeyDispatchState
17285 = new KeyEvent.DispatcherState();
17286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017287 /**
17288 * Indicates whether the view's window currently has the focus.
17289 */
17290 boolean mHasWindowFocus;
17291
17292 /**
17293 * The current visibility of the window.
17294 */
17295 int mWindowVisibility;
17296
17297 /**
17298 * Indicates the time at which drawing started to occur.
17299 */
17300 long mDrawingTime;
17301
17302 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017303 * Indicates whether or not ignoring the DIRTY_MASK flags.
17304 */
17305 boolean mIgnoreDirtyState;
17306
17307 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017308 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17309 * to avoid clearing that flag prematurely.
17310 */
17311 boolean mSetIgnoreDirtyState = false;
17312
17313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017314 * Indicates whether the view's window is currently in touch mode.
17315 */
17316 boolean mInTouchMode;
17317
17318 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017319 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017320 * the next time it performs a traversal
17321 */
17322 boolean mRecomputeGlobalAttributes;
17323
17324 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017325 * Always report new attributes at next traversal.
17326 */
17327 boolean mForceReportNewAttributes;
17328
17329 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017330 * Set during a traveral if any views want to keep the screen on.
17331 */
17332 boolean mKeepScreenOn;
17333
17334 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017335 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17336 */
17337 int mSystemUiVisibility;
17338
17339 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017340 * Hack to force certain system UI visibility flags to be cleared.
17341 */
17342 int mDisabledSystemUiVisibility;
17343
17344 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017345 * Last global system UI visibility reported by the window manager.
17346 */
17347 int mGlobalSystemUiVisibility;
17348
17349 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017350 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17351 * attached.
17352 */
17353 boolean mHasSystemUiListeners;
17354
17355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017356 * Set if the visibility of any views has changed.
17357 */
17358 boolean mViewVisibilityChanged;
17359
17360 /**
17361 * Set to true if a view has been scrolled.
17362 */
17363 boolean mViewScrollChanged;
17364
17365 /**
17366 * Global to the view hierarchy used as a temporary for dealing with
17367 * x/y points in the transparent region computations.
17368 */
17369 final int[] mTransparentLocation = new int[2];
17370
17371 /**
17372 * Global to the view hierarchy used as a temporary for dealing with
17373 * x/y points in the ViewGroup.invalidateChild implementation.
17374 */
17375 final int[] mInvalidateChildLocation = new int[2];
17376
Chet Haasec3aa3612010-06-17 08:50:37 -070017377
17378 /**
17379 * Global to the view hierarchy used as a temporary for dealing with
17380 * x/y location when view is transformed.
17381 */
17382 final float[] mTmpTransformLocation = new float[2];
17383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017384 /**
17385 * The view tree observer used to dispatch global events like
17386 * layout, pre-draw, touch mode change, etc.
17387 */
17388 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17389
17390 /**
17391 * A Canvas used by the view hierarchy to perform bitmap caching.
17392 */
17393 Canvas mCanvas;
17394
17395 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017396 * The view root impl.
17397 */
17398 final ViewRootImpl mViewRootImpl;
17399
17400 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017401 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017402 * handler can be used to pump events in the UI events queue.
17403 */
17404 final Handler mHandler;
17405
17406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017407 * Temporary for use in computing invalidate rectangles while
17408 * calling up the hierarchy.
17409 */
17410 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017411
17412 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017413 * Temporary for use in computing hit areas with transformed views
17414 */
17415 final RectF mTmpTransformRect = new RectF();
17416
17417 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017418 * Temporary list for use in collecting focusable descendents of a view.
17419 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017420 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017422 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017423 * The id of the window for accessibility purposes.
17424 */
17425 int mAccessibilityWindowId = View.NO_ID;
17426
17427 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017428 * Whether to ingore not exposed for accessibility Views when
17429 * reporting the view tree to accessibility services.
17430 */
17431 boolean mIncludeNotImportantViews;
17432
17433 /**
17434 * The drawable for highlighting accessibility focus.
17435 */
17436 Drawable mAccessibilityFocusDrawable;
17437
17438 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017439 * Show where the margins, bounds and layout bounds are for each view.
17440 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017441 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017442
17443 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017444 * Point used to compute visible regions.
17445 */
17446 final Point mPoint = new Point();
17447
17448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017449 * Creates a new set of attachment information with the specified
17450 * events handler and thread.
17451 *
17452 * @param handler the events handler the view must use
17453 */
17454 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017455 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017456 mSession = session;
17457 mWindow = window;
17458 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017459 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017460 mHandler = handler;
17461 mRootCallbacks = effectPlayer;
17462 }
17463 }
17464
17465 /**
17466 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17467 * is supported. This avoids keeping too many unused fields in most
17468 * instances of View.</p>
17469 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017470 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017471
Mike Cleronf116bf82009-09-27 19:14:12 -070017472 /**
17473 * Scrollbars are not visible
17474 */
17475 public static final int OFF = 0;
17476
17477 /**
17478 * Scrollbars are visible
17479 */
17480 public static final int ON = 1;
17481
17482 /**
17483 * Scrollbars are fading away
17484 */
17485 public static final int FADING = 2;
17486
17487 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017489 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017490 public int scrollBarDefaultDelayBeforeFade;
17491 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017492
17493 public int scrollBarSize;
17494 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017495 public float[] interpolatorValues;
17496 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017497
17498 public final Paint paint;
17499 public final Matrix matrix;
17500 public Shader shader;
17501
Mike Cleronf116bf82009-09-27 19:14:12 -070017502 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17503
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017504 private static final float[] OPAQUE = { 255 };
17505 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017506
Mike Cleronf116bf82009-09-27 19:14:12 -070017507 /**
17508 * When fading should start. This time moves into the future every time
17509 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17510 */
17511 public long fadeStartTime;
17512
17513
17514 /**
17515 * The current state of the scrollbars: ON, OFF, or FADING
17516 */
17517 public int state = OFF;
17518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017519 private int mLastColor;
17520
Mike Cleronf116bf82009-09-27 19:14:12 -070017521 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017522 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17523 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017524 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17525 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017526
17527 paint = new Paint();
17528 matrix = new Matrix();
17529 // use use a height of 1, and then wack the matrix each time we
17530 // actually use it.
17531 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017533 paint.setShader(shader);
17534 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017535 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017536 }
Romain Guy8506ab42009-06-11 17:35:47 -070017537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017538 public void setFadeColor(int color) {
17539 if (color != 0 && color != mLastColor) {
17540 mLastColor = color;
17541 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017542
Romain Guye55e1a72009-08-27 10:42:26 -070017543 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17544 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017546 paint.setShader(shader);
17547 // Restore the default transfer mode (src_over)
17548 paint.setXfermode(null);
17549 }
17550 }
Joe Malin32736f02011-01-19 16:14:20 -080017551
Mike Cleronf116bf82009-09-27 19:14:12 -070017552 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017553 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017554 if (now >= fadeStartTime) {
17555
17556 // the animation fades the scrollbars out by changing
17557 // the opacity (alpha) from fully opaque to fully
17558 // transparent
17559 int nextFrame = (int) now;
17560 int framesCount = 0;
17561
17562 Interpolator interpolator = scrollBarInterpolator;
17563
17564 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017565 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017566
17567 // End transparent
17568 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017569 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017570
17571 state = FADING;
17572
17573 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017574 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017575 }
17576 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017577 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017578
Svetoslav Ganova0156172011-06-26 17:55:44 -070017579 /**
17580 * Resuable callback for sending
17581 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17582 */
17583 private class SendViewScrolledAccessibilityEvent implements Runnable {
17584 public volatile boolean mIsPending;
17585
17586 public void run() {
17587 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17588 mIsPending = false;
17589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017590 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017591
17592 /**
17593 * <p>
17594 * This class represents a delegate that can be registered in a {@link View}
17595 * to enhance accessibility support via composition rather via inheritance.
17596 * It is specifically targeted to widget developers that extend basic View
17597 * classes i.e. classes in package android.view, that would like their
17598 * applications to be backwards compatible.
17599 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017600 * <div class="special reference">
17601 * <h3>Developer Guides</h3>
17602 * <p>For more information about making applications accessible, read the
17603 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17604 * developer guide.</p>
17605 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017606 * <p>
17607 * A scenario in which a developer would like to use an accessibility delegate
17608 * is overriding a method introduced in a later API version then the minimal API
17609 * version supported by the application. For example, the method
17610 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17611 * in API version 4 when the accessibility APIs were first introduced. If a
17612 * developer would like his application to run on API version 4 devices (assuming
17613 * all other APIs used by the application are version 4 or lower) and take advantage
17614 * of this method, instead of overriding the method which would break the application's
17615 * backwards compatibility, he can override the corresponding method in this
17616 * delegate and register the delegate in the target View if the API version of
17617 * the system is high enough i.e. the API version is same or higher to the API
17618 * version that introduced
17619 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17620 * </p>
17621 * <p>
17622 * Here is an example implementation:
17623 * </p>
17624 * <code><pre><p>
17625 * if (Build.VERSION.SDK_INT >= 14) {
17626 * // If the API version is equal of higher than the version in
17627 * // which onInitializeAccessibilityNodeInfo was introduced we
17628 * // register a delegate with a customized implementation.
17629 * View view = findViewById(R.id.view_id);
17630 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17631 * public void onInitializeAccessibilityNodeInfo(View host,
17632 * AccessibilityNodeInfo info) {
17633 * // Let the default implementation populate the info.
17634 * super.onInitializeAccessibilityNodeInfo(host, info);
17635 * // Set some other information.
17636 * info.setEnabled(host.isEnabled());
17637 * }
17638 * });
17639 * }
17640 * </code></pre></p>
17641 * <p>
17642 * This delegate contains methods that correspond to the accessibility methods
17643 * in View. If a delegate has been specified the implementation in View hands
17644 * off handling to the corresponding method in this delegate. The default
17645 * implementation the delegate methods behaves exactly as the corresponding
17646 * method in View for the case of no accessibility delegate been set. Hence,
17647 * to customize the behavior of a View method, clients can override only the
17648 * corresponding delegate method without altering the behavior of the rest
17649 * accessibility related methods of the host view.
17650 * </p>
17651 */
17652 public static class AccessibilityDelegate {
17653
17654 /**
17655 * Sends an accessibility event of the given type. If accessibility is not
17656 * enabled this method has no effect.
17657 * <p>
17658 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17659 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17660 * been set.
17661 * </p>
17662 *
17663 * @param host The View hosting the delegate.
17664 * @param eventType The type of the event to send.
17665 *
17666 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17667 */
17668 public void sendAccessibilityEvent(View host, int eventType) {
17669 host.sendAccessibilityEventInternal(eventType);
17670 }
17671
17672 /**
alanv8eeefef2012-05-07 16:57:53 -070017673 * Performs the specified accessibility action on the view. For
17674 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17675 * <p>
17676 * The default implementation behaves as
17677 * {@link View#performAccessibilityAction(int, Bundle)
17678 * View#performAccessibilityAction(int, Bundle)} for the case of
17679 * no accessibility delegate been set.
17680 * </p>
17681 *
17682 * @param action The action to perform.
17683 * @return Whether the action was performed.
17684 *
17685 * @see View#performAccessibilityAction(int, Bundle)
17686 * View#performAccessibilityAction(int, Bundle)
17687 */
17688 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17689 return host.performAccessibilityActionInternal(action, args);
17690 }
17691
17692 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017693 * Sends an accessibility event. This method behaves exactly as
17694 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17695 * empty {@link AccessibilityEvent} and does not perform a check whether
17696 * accessibility is enabled.
17697 * <p>
17698 * The default implementation behaves as
17699 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17700 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17701 * the case of no accessibility delegate been set.
17702 * </p>
17703 *
17704 * @param host The View hosting the delegate.
17705 * @param event The event to send.
17706 *
17707 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17708 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17709 */
17710 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17711 host.sendAccessibilityEventUncheckedInternal(event);
17712 }
17713
17714 /**
17715 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17716 * to its children for adding their text content to the event.
17717 * <p>
17718 * The default implementation behaves as
17719 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17720 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17721 * the case of no accessibility delegate been set.
17722 * </p>
17723 *
17724 * @param host The View hosting the delegate.
17725 * @param event The event.
17726 * @return True if the event population was completed.
17727 *
17728 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17729 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17730 */
17731 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17732 return host.dispatchPopulateAccessibilityEventInternal(event);
17733 }
17734
17735 /**
17736 * Gives a chance to the host View to populate the accessibility event with its
17737 * text content.
17738 * <p>
17739 * The default implementation behaves as
17740 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17741 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17742 * the case of no accessibility delegate been set.
17743 * </p>
17744 *
17745 * @param host The View hosting the delegate.
17746 * @param event The accessibility event which to populate.
17747 *
17748 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17749 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17750 */
17751 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17752 host.onPopulateAccessibilityEventInternal(event);
17753 }
17754
17755 /**
17756 * Initializes an {@link AccessibilityEvent} with information about the
17757 * the host View which is the event source.
17758 * <p>
17759 * The default implementation behaves as
17760 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17761 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17762 * the case of no accessibility delegate been set.
17763 * </p>
17764 *
17765 * @param host The View hosting the delegate.
17766 * @param event The event to initialize.
17767 *
17768 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17769 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17770 */
17771 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17772 host.onInitializeAccessibilityEventInternal(event);
17773 }
17774
17775 /**
17776 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17777 * <p>
17778 * The default implementation behaves as
17779 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17780 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17781 * the case of no accessibility delegate been set.
17782 * </p>
17783 *
17784 * @param host The View hosting the delegate.
17785 * @param info The instance to initialize.
17786 *
17787 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17788 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17789 */
17790 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17791 host.onInitializeAccessibilityNodeInfoInternal(info);
17792 }
17793
17794 /**
17795 * Called when a child of the host View has requested sending an
17796 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17797 * to augment the event.
17798 * <p>
17799 * The default implementation behaves as
17800 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17801 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17802 * the case of no accessibility delegate been set.
17803 * </p>
17804 *
17805 * @param host The View hosting the delegate.
17806 * @param child The child which requests sending the event.
17807 * @param event The event to be sent.
17808 * @return True if the event should be sent
17809 *
17810 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17811 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17812 */
17813 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17814 AccessibilityEvent event) {
17815 return host.onRequestSendAccessibilityEventInternal(child, event);
17816 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017817
17818 /**
17819 * Gets the provider for managing a virtual view hierarchy rooted at this View
17820 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17821 * that explore the window content.
17822 * <p>
17823 * The default implementation behaves as
17824 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17825 * the case of no accessibility delegate been set.
17826 * </p>
17827 *
17828 * @return The provider.
17829 *
17830 * @see AccessibilityNodeProvider
17831 */
17832 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17833 return null;
17834 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017836}