blob: 7c6a7c520b52dbae4c9722ecf5c3ac0ae7633791 [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>
Jean Chalard405bc512012-05-29 19:12:34 +0900207 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900212 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * </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 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001599 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001600
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 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002133 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2134 * is used to check whether later changes to the view's transform should invalidate the
2135 * view to force the quickReject test to run again.
2136 */
Chet Haase21433372012-06-05 07:54:09 -07002137 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002138
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002139 // Accessiblity constants for mPrivateFlags2
2140
2141 /**
2142 * Shift for the bits in {@link #mPrivateFlags2} related to the
2143 * "accessibilityFocusable" attribute.
2144 */
Chet Haase21433372012-06-05 07:54:09 -07002145 static final int ACCESSIBILITY_FOCUSABLE_SHIFT = 29;
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002146
2147 /**
2148 * The system determines whether the view can take accessibility focus - default (recommended).
2149 * <p>
2150 * Such a view is consideted by the focus search if it is:
2151 * <ul>
2152 * <li>
2153 * Important for accessibility and actionable (clickable, long clickable, focusable)
2154 * </li>
2155 * <li>
2156 * Important for accessibility, not actionable (clickable, long clickable, focusable),
2157 * and does not have an actionable predecessor.
2158 * </li>
2159 * </ul>
2160 * An accessibility srvice can request putting accessibility focus on such a view.
2161 * </p>
2162 *
2163 * @hide
2164 */
2165 public static final int ACCESSIBILITY_FOCUSABLE_AUTO = 0x00000000;
2166
2167 /**
2168 * The view can take accessibility focus.
2169 * <p>
2170 * A view that can take accessibility focus is always considered during focus
2171 * search and an accessibility service can request putting accessibility focus
2172 * on it.
2173 * </p>
2174 *
2175 * @hide
2176 */
2177 public static final int ACCESSIBILITY_FOCUSABLE_YES = 0x00000001;
2178
2179 /**
2180 * The view can not take accessibility focus.
2181 * <p>
2182 * A view that can not take accessibility focus is never considered during focus
2183 * search and an accessibility service can not request putting accessibility focus
2184 * on it.
2185 * </p>
2186 *
2187 * @hide
2188 */
2189 public static final int ACCESSIBILITY_FOCUSABLE_NO = 0x00000002;
2190
2191 /**
2192 * The default whether the view is accessiblity focusable.
2193 */
2194 static final int ACCESSIBILITY_FOCUSABLE_DEFAULT = ACCESSIBILITY_FOCUSABLE_AUTO;
2195
2196 /**
2197 * Mask for obtainig the bits which specifies how to determine
2198 * whether a view is accessibility focusable.
2199 */
2200 static final int ACCESSIBILITY_FOCUSABLE_MASK = (ACCESSIBILITY_FOCUSABLE_AUTO
2201 | ACCESSIBILITY_FOCUSABLE_YES | ACCESSIBILITY_FOCUSABLE_NO)
2202 << ACCESSIBILITY_FOCUSABLE_SHIFT;
2203
2204
Christopher Tate3d4bf172011-03-28 16:16:46 -07002205 /* End of masks for mPrivateFlags2 */
2206
Chet Haase21433372012-06-05 07:54:09 -07002207 /* Masks for mPrivateFlags3 */
2208
2209 /**
2210 * Flag indicating that view has a transform animation set on it. This is used to track whether
2211 * an animation is cleared between successive frames, in order to tell the associated
2212 * DisplayList to clear its animation matrix.
2213 */
2214 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2215
2216 /**
2217 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2218 * animation is cleared between successive frames, in order to tell the associated
2219 * DisplayList to restore its alpha value.
2220 */
2221 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2222
2223
2224 /* End of masks for mPrivateFlags3 */
2225
Christopher Tate3d4bf172011-03-28 16:16:46 -07002226 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2227
Chet Haasedaf98e92011-01-10 14:10:36 -08002228 /**
Adam Powell637d3372010-08-25 14:37:03 -07002229 * Always allow a user to over-scroll this view, provided it is a
2230 * view that can scroll.
2231 *
2232 * @see #getOverScrollMode()
2233 * @see #setOverScrollMode(int)
2234 */
2235 public static final int OVER_SCROLL_ALWAYS = 0;
2236
2237 /**
2238 * Allow a user to over-scroll this view only if the content is large
2239 * enough to meaningfully scroll, provided it is a view that can scroll.
2240 *
2241 * @see #getOverScrollMode()
2242 * @see #setOverScrollMode(int)
2243 */
2244 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2245
2246 /**
2247 * Never allow a user to over-scroll this view.
2248 *
2249 * @see #getOverScrollMode()
2250 * @see #setOverScrollMode(int)
2251 */
2252 public static final int OVER_SCROLL_NEVER = 2;
2253
2254 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002255 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2256 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002257 *
Joe Malin32736f02011-01-19 16:14:20 -08002258 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002259 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002260 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002261
2262 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002263 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2264 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002265 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002266 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002267 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002268 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002269 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002270 *
Joe Malin32736f02011-01-19 16:14:20 -08002271 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002272 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002273 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2274
2275 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002276 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2277 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002278 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002279 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002280 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2281 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2282 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002283 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002284 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2285 * window flags) for displaying content using every last pixel on the display.
2286 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002287 * <p>There is a limitation: because navigation controls are so important, the least user
2288 * interaction will cause them to reappear immediately. When this happens, both
2289 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2290 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002291 *
2292 * @see #setSystemUiVisibility(int)
2293 */
2294 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2295
2296 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002297 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2298 * into the normal fullscreen mode so that its content can take over the screen
2299 * while still allowing the user to interact with the application.
2300 *
2301 * <p>This has the same visual effect as
2302 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2303 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2304 * meaning that non-critical screen decorations (such as the status bar) will be
2305 * hidden while the user is in the View's window, focusing the experience on
2306 * that content. Unlike the window flag, if you are using ActionBar in
2307 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2308 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2309 * hide the action bar.
2310 *
2311 * <p>This approach to going fullscreen is best used over the window flag when
2312 * it is a transient state -- that is, the application does this at certain
2313 * points in its user interaction where it wants to allow the user to focus
2314 * on content, but not as a continuous state. For situations where the application
2315 * would like to simply stay full screen the entire time (such as a game that
2316 * wants to take over the screen), the
2317 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2318 * is usually a better approach. The state set here will be removed by the system
2319 * in various situations (such as the user moving to another application) like
2320 * the other system UI states.
2321 *
2322 * <p>When using this flag, the application should provide some easy facility
2323 * for the user to go out of it. A common example would be in an e-book
2324 * reader, where tapping on the screen brings back whatever screen and UI
2325 * decorations that had been hidden while the user was immersed in reading
2326 * the book.
2327 *
2328 * @see #setSystemUiVisibility(int)
2329 */
2330 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2331
2332 /**
2333 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2334 * flags, we would like a stable view of the content insets given to
2335 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2336 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002337 * as a continuous state. In the stock Android UI this is the space for
2338 * the system bar, nav bar, and status bar, but not more transient elements
2339 * such as an input method.
2340 *
2341 * The stable layout your UI sees is based on the system UI modes you can
2342 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2343 * then you will get a stable layout for changes of the
2344 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2345 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2346 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2347 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2348 * with a stable layout. (Note that you should avoid using
2349 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2350 *
2351 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2352 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2353 * then a hidden status bar will be considered a "stable" state for purposes
2354 * here. This allows your UI to continually hide the status bar, while still
2355 * using the system UI flags to hide the action bar while still retaining
2356 * a stable layout. Note that changing the window fullscreen flag will never
2357 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002358 *
2359 * <p>If you are using ActionBar in
2360 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2361 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2362 * insets it adds to those given to the application.
2363 */
2364 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
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_HIDE_NAVIGATION}, 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 the navagation system UI through the
2374 * {@link #fitSystemWindows(Rect)} method.
2375 */
2376 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2377
2378 /**
2379 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2380 * to be layed out as if it has requested
2381 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2382 * allows it to avoid artifacts when switching in and out of that mode, at
2383 * the expense that some of its user interface may be covered by screen
2384 * decorations when they are shown. You can perform layout of your inner
2385 * UI elements to account for non-fullscreen system UI through the
2386 * {@link #fitSystemWindows(Rect)} method.
2387 */
2388 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2389
2390 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002391 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2392 */
2393 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2394
2395 /**
2396 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2397 */
2398 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002399
2400 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002401 * @hide
2402 *
2403 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2404 * out of the public fields to keep the undefined bits out of the developer's way.
2405 *
2406 * Flag to make the status bar not expandable. Unless you also
2407 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2408 */
2409 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2410
2411 /**
2412 * @hide
2413 *
2414 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2415 * out of the public fields to keep the undefined bits out of the developer's way.
2416 *
2417 * Flag to hide notification icons and scrolling ticker text.
2418 */
2419 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2420
2421 /**
2422 * @hide
2423 *
2424 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2425 * out of the public fields to keep the undefined bits out of the developer's way.
2426 *
2427 * Flag to disable incoming notification alerts. This will not block
2428 * icons, but it will block sound, vibrating and other visual or aural notifications.
2429 */
2430 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
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 only the scrolling ticker. Note that
2439 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2440 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2441 */
2442 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2443
2444 /**
2445 * @hide
2446 *
2447 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2448 * out of the public fields to keep the undefined bits out of the developer's way.
2449 *
2450 * Flag to hide the center system info area.
2451 */
2452 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2453
2454 /**
2455 * @hide
2456 *
2457 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2458 * out of the public fields to keep the undefined bits out of the developer's way.
2459 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002460 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002461 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2462 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002463 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002464
2465 /**
2466 * @hide
2467 *
2468 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2469 * out of the public fields to keep the undefined bits out of the developer's way.
2470 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002471 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002472 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2473 */
2474 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2475
2476 /**
2477 * @hide
2478 *
2479 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2480 * out of the public fields to keep the undefined bits out of the developer's way.
2481 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002482 * Flag to hide only the clock. You might use this if your activity has
2483 * its own clock making the status bar's clock redundant.
2484 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002485 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2486
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002487 /**
2488 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002489 *
2490 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2491 * out of the public fields to keep the undefined bits out of the developer's way.
2492 *
2493 * Flag to hide only the recent apps button. Don't use this
2494 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2495 */
2496 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2497
2498 /**
2499 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002500 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002501 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002502
2503 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002504 * These are the system UI flags that can be cleared by events outside
2505 * of an application. Currently this is just the ability to tap on the
2506 * screen while hiding the navigation bar to have it return.
2507 * @hide
2508 */
2509 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002510 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2511 | SYSTEM_UI_FLAG_FULLSCREEN;
2512
2513 /**
2514 * Flags that can impact the layout in relation to system UI.
2515 */
2516 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2517 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2518 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002519
2520 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002521 * Find views that render the specified text.
2522 *
2523 * @see #findViewsWithText(ArrayList, CharSequence, int)
2524 */
2525 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2526
2527 /**
2528 * Find find views that contain the specified content description.
2529 *
2530 * @see #findViewsWithText(ArrayList, CharSequence, int)
2531 */
2532 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2533
2534 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002535 * Find views that contain {@link AccessibilityNodeProvider}. Such
2536 * a View is a root of virtual view hierarchy and may contain the searched
2537 * text. If this flag is set Views with providers are automatically
2538 * added and it is a responsibility of the client to call the APIs of
2539 * the provider to determine whether the virtual tree rooted at this View
2540 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2541 * represeting the virtual views with this text.
2542 *
2543 * @see #findViewsWithText(ArrayList, CharSequence, int)
2544 *
2545 * @hide
2546 */
2547 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2548
2549 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002550 * The undefined cursor position.
2551 */
2552 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2553
2554 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002555 * Indicates that the screen has changed state and is now off.
2556 *
2557 * @see #onScreenStateChanged(int)
2558 */
2559 public static final int SCREEN_STATE_OFF = 0x0;
2560
2561 /**
2562 * Indicates that the screen has changed state and is now on.
2563 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002564 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002565 */
2566 public static final int SCREEN_STATE_ON = 0x1;
2567
2568 /**
Adam Powell637d3372010-08-25 14:37:03 -07002569 * Controls the over-scroll mode for this view.
2570 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2571 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2572 * and {@link #OVER_SCROLL_NEVER}.
2573 */
2574 private int mOverScrollMode;
2575
2576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 * The parent this view is attached to.
2578 * {@hide}
2579 *
2580 * @see #getParent()
2581 */
2582 protected ViewParent mParent;
2583
2584 /**
2585 * {@hide}
2586 */
2587 AttachInfo mAttachInfo;
2588
2589 /**
2590 * {@hide}
2591 */
Romain Guy809a7f62009-05-14 15:44:42 -07002592 @ViewDebug.ExportedProperty(flagMapping = {
2593 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2594 name = "FORCE_LAYOUT"),
2595 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2596 name = "LAYOUT_REQUIRED"),
2597 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002598 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002599 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2600 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2601 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2602 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2603 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002605 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002606 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607
2608 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002609 * This view's request for the visibility of the status bar.
2610 * @hide
2611 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002612 @ViewDebug.ExportedProperty(flagMapping = {
2613 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2614 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2615 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2616 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2617 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2618 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2619 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2620 equals = SYSTEM_UI_FLAG_VISIBLE,
2621 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2622 })
Joe Onorato664644d2011-01-23 17:53:23 -08002623 int mSystemUiVisibility;
2624
2625 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002626 * Reference count for transient state.
2627 * @see #setHasTransientState(boolean)
2628 */
2629 int mTransientStateCount = 0;
2630
2631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 * Count of how many windows this view has been attached to.
2633 */
2634 int mWindowAttachCount;
2635
2636 /**
2637 * The layout parameters associated with this view and used by the parent
2638 * {@link android.view.ViewGroup} to determine how this view should be
2639 * laid out.
2640 * {@hide}
2641 */
2642 protected ViewGroup.LayoutParams mLayoutParams;
2643
2644 /**
2645 * The view flags hold various views states.
2646 * {@hide}
2647 */
2648 @ViewDebug.ExportedProperty
2649 int mViewFlags;
2650
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002651 static class TransformationInfo {
2652 /**
2653 * The transform matrix for the View. This transform is calculated internally
2654 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2655 * is used by default. Do *not* use this variable directly; instead call
2656 * getMatrix(), which will automatically recalculate the matrix if necessary
2657 * to get the correct matrix based on the latest rotation and scale properties.
2658 */
2659 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002660
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002661 /**
2662 * The transform matrix for the View. This transform is calculated internally
2663 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2664 * is used by default. Do *not* use this variable directly; instead call
2665 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2666 * to get the correct matrix based on the latest rotation and scale properties.
2667 */
2668 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002669
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002670 /**
2671 * An internal variable that tracks whether we need to recalculate the
2672 * transform matrix, based on whether the rotation or scaleX/Y properties
2673 * have changed since the matrix was last calculated.
2674 */
2675 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002676
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002677 /**
2678 * An internal variable that tracks whether we need to recalculate the
2679 * transform matrix, based on whether the rotation or scaleX/Y properties
2680 * have changed since the matrix was last calculated.
2681 */
2682 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002683
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002684 /**
2685 * A variable that tracks whether we need to recalculate the
2686 * transform matrix, based on whether the rotation or scaleX/Y properties
2687 * have changed since the matrix was last calculated. This variable
2688 * is only valid after a call to updateMatrix() or to a function that
2689 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2690 */
2691 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002692
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002693 /**
2694 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2695 */
2696 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002697
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002698 /**
2699 * This matrix is used when computing the matrix for 3D rotations.
2700 */
2701 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002702
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002703 /**
2704 * These prev values are used to recalculate a centered pivot point when necessary. The
2705 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2706 * set), so thes values are only used then as well.
2707 */
2708 private int mPrevWidth = -1;
2709 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002710
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002711 /**
2712 * The degrees rotation around the vertical axis through the pivot point.
2713 */
2714 @ViewDebug.ExportedProperty
2715 float mRotationY = 0f;
2716
2717 /**
2718 * The degrees rotation around the horizontal axis through the pivot point.
2719 */
2720 @ViewDebug.ExportedProperty
2721 float mRotationX = 0f;
2722
2723 /**
2724 * The degrees rotation around the pivot point.
2725 */
2726 @ViewDebug.ExportedProperty
2727 float mRotation = 0f;
2728
2729 /**
2730 * The amount of translation of the object away from its left property (post-layout).
2731 */
2732 @ViewDebug.ExportedProperty
2733 float mTranslationX = 0f;
2734
2735 /**
2736 * The amount of translation of the object away from its top property (post-layout).
2737 */
2738 @ViewDebug.ExportedProperty
2739 float mTranslationY = 0f;
2740
2741 /**
2742 * The amount of scale in the x direction around the pivot point. A
2743 * value of 1 means no scaling is applied.
2744 */
2745 @ViewDebug.ExportedProperty
2746 float mScaleX = 1f;
2747
2748 /**
2749 * The amount of scale in the y direction around the pivot point. A
2750 * value of 1 means no scaling is applied.
2751 */
2752 @ViewDebug.ExportedProperty
2753 float mScaleY = 1f;
2754
2755 /**
Chet Haasea33de552012-02-03 16:28:24 -08002756 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002757 */
2758 @ViewDebug.ExportedProperty
2759 float mPivotX = 0f;
2760
2761 /**
Chet Haasea33de552012-02-03 16:28:24 -08002762 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002763 */
2764 @ViewDebug.ExportedProperty
2765 float mPivotY = 0f;
2766
2767 /**
2768 * The opacity of the View. This is a value from 0 to 1, where 0 means
2769 * completely transparent and 1 means completely opaque.
2770 */
2771 @ViewDebug.ExportedProperty
2772 float mAlpha = 1f;
2773 }
2774
2775 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002776
Joe Malin32736f02011-01-19 16:14:20 -08002777 private boolean mLastIsOpaque;
2778
Chet Haasefd2b0022010-08-06 13:08:56 -07002779 /**
2780 * Convenience value to check for float values that are close enough to zero to be considered
2781 * zero.
2782 */
Romain Guy2542d192010-08-18 11:47:12 -07002783 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002784
2785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 * The distance in pixels from the left edge of this view's parent
2787 * to the left edge of this view.
2788 * {@hide}
2789 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002790 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 protected int mLeft;
2792 /**
2793 * The distance in pixels from the left edge of this view's parent
2794 * to the right edge of this view.
2795 * {@hide}
2796 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002797 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 protected int mRight;
2799 /**
2800 * The distance in pixels from the top edge of this view's parent
2801 * to the top edge of this view.
2802 * {@hide}
2803 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002804 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 protected int mTop;
2806 /**
2807 * The distance in pixels from the top edge of this view's parent
2808 * to the bottom edge of this view.
2809 * {@hide}
2810 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002811 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 protected int mBottom;
2813
2814 /**
2815 * The offset, in pixels, by which the content of this view is scrolled
2816 * horizontally.
2817 * {@hide}
2818 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002819 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 protected int mScrollX;
2821 /**
2822 * The offset, in pixels, by which the content of this view is scrolled
2823 * vertically.
2824 * {@hide}
2825 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002826 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 protected int mScrollY;
2828
2829 /**
2830 * The left padding in pixels, that is the distance in pixels between the
2831 * left edge of this view and the left edge of its content.
2832 * {@hide}
2833 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002834 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 protected int mPaddingLeft;
2836 /**
2837 * The right padding in pixels, that is the distance in pixels between the
2838 * right edge of this view and the right edge of its content.
2839 * {@hide}
2840 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002841 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 protected int mPaddingRight;
2843 /**
2844 * The top padding in pixels, that is the distance in pixels between the
2845 * top edge of this view and the top edge of its content.
2846 * {@hide}
2847 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002848 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 protected int mPaddingTop;
2850 /**
2851 * The bottom padding in pixels, that is the distance in pixels between the
2852 * bottom edge of this view and the bottom edge of its content.
2853 * {@hide}
2854 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002855 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 protected int mPaddingBottom;
2857
2858 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002859 * The layout insets in pixels, that is the distance in pixels between the
2860 * visible edges of this view its bounds.
2861 */
2862 private Insets mLayoutInsets;
2863
2864 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002865 * Briefly describes the view and is primarily used for accessibility support.
2866 */
2867 private CharSequence mContentDescription;
2868
2869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002871 *
2872 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002874 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002875 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876
2877 /**
2878 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002879 *
2880 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002882 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002883 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002885 /**
Adam Powell20232d02010-12-08 21:08:53 -08002886 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002887 *
2888 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002889 */
2890 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002891 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002892
2893 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002894 * Cache if the user padding is relative.
2895 *
2896 */
2897 @ViewDebug.ExportedProperty(category = "padding")
2898 boolean mUserPaddingRelative;
2899
2900 /**
2901 * Cache the paddingStart set by the user to append to the scrollbar's size.
2902 *
2903 */
2904 @ViewDebug.ExportedProperty(category = "padding")
2905 int mUserPaddingStart;
2906
2907 /**
2908 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2909 *
2910 */
2911 @ViewDebug.ExportedProperty(category = "padding")
2912 int mUserPaddingEnd;
2913
2914 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002915 * @hide
2916 */
2917 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2918 /**
2919 * @hide
2920 */
2921 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
Philip Milne6c8ea062012-04-03 17:38:43 -07002923 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924
2925 private int mBackgroundResource;
2926 private boolean mBackgroundSizeChanged;
2927
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002928 static class ListenerInfo {
2929 /**
2930 * Listener used to dispatch focus change events.
2931 * This field should be made private, so it is hidden from the SDK.
2932 * {@hide}
2933 */
2934 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002936 /**
2937 * Listeners for layout change events.
2938 */
2939 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002940
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002941 /**
2942 * Listeners for attach events.
2943 */
2944 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002945
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002946 /**
2947 * Listener used to dispatch click events.
2948 * This field should be made private, so it is hidden from the SDK.
2949 * {@hide}
2950 */
2951 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002953 /**
2954 * Listener used to dispatch long click events.
2955 * This field should be made private, so it is hidden from the SDK.
2956 * {@hide}
2957 */
2958 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002960 /**
2961 * Listener used to build the context menu.
2962 * This field should be made private, so it is hidden from the SDK.
2963 * {@hide}
2964 */
2965 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002967 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002969 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002971 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002972
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002973 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002974
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002975 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002976
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002977 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2978 }
2979
2980 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 /**
2983 * The application environment this view lives in.
2984 * This field should be made private, so it is hidden from the SDK.
2985 * {@hide}
2986 */
2987 protected Context mContext;
2988
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002989 private final Resources mResources;
2990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 private ScrollabilityCache mScrollCache;
2992
2993 private int[] mDrawableState = null;
2994
Romain Guy0211a0a2011-02-14 16:34:59 -08002995 /**
2996 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002997 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002998 * @hide
2999 */
3000 public boolean mCachingFailed;
3001
Romain Guy02890fd2010-08-06 17:58:44 -07003002 private Bitmap mDrawingCache;
3003 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08003004 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003005 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006
3007 /**
3008 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3009 * the user may specify which view to go to next.
3010 */
3011 private int mNextFocusLeftId = View.NO_ID;
3012
3013 /**
3014 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3015 * the user may specify which view to go to next.
3016 */
3017 private int mNextFocusRightId = View.NO_ID;
3018
3019 /**
3020 * When this view has focus and the next focus is {@link #FOCUS_UP},
3021 * the user may specify which view to go to next.
3022 */
3023 private int mNextFocusUpId = View.NO_ID;
3024
3025 /**
3026 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3027 * the user may specify which view to go to next.
3028 */
3029 private int mNextFocusDownId = View.NO_ID;
3030
Jeff Brown4e6319b2010-12-13 10:36:51 -08003031 /**
3032 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3033 * the user may specify which view to go to next.
3034 */
3035 int mNextFocusForwardId = View.NO_ID;
3036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003038 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003039 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003040 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 private UnsetPressedState mUnsetPressedState;
3043
3044 /**
3045 * Whether the long press's action has been invoked. The tap's action is invoked on the
3046 * up event while a long press is invoked as soon as the long press duration is reached, so
3047 * a long press could be performed before the tap is checked, in which case the tap's action
3048 * should not be invoked.
3049 */
3050 private boolean mHasPerformedLongPress;
3051
3052 /**
3053 * The minimum height of the view. We'll try our best to have the height
3054 * of this view to at least this amount.
3055 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003056 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 private int mMinHeight;
3058
3059 /**
3060 * The minimum width of the view. We'll try our best to have the width
3061 * of this view to at least this amount.
3062 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003063 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 private int mMinWidth;
3065
3066 /**
3067 * The delegate to handle touch events that are physically in this view
3068 * but should be handled by another view.
3069 */
3070 private TouchDelegate mTouchDelegate = null;
3071
3072 /**
3073 * Solid color to use as a background when creating the drawing cache. Enables
3074 * the cache to use 16 bit bitmaps instead of 32 bit.
3075 */
3076 private int mDrawingCacheBackgroundColor = 0;
3077
3078 /**
3079 * Special tree observer used when mAttachInfo is null.
3080 */
3081 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003082
Adam Powelle14579b2009-12-16 18:39:52 -08003083 /**
3084 * Cache the touch slop from the context that created the view.
3085 */
3086 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003089 * Object that handles automatic animation of view properties.
3090 */
3091 private ViewPropertyAnimator mAnimator = null;
3092
3093 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003094 * Flag indicating that a drag can cross window boundaries. When
3095 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3096 * with this flag set, all visible applications will be able to participate
3097 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003098 *
3099 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003100 */
3101 public static final int DRAG_FLAG_GLOBAL = 1;
3102
3103 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003104 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3105 */
3106 private float mVerticalScrollFactor;
3107
3108 /**
Adam Powell20232d02010-12-08 21:08:53 -08003109 * Position of the vertical scroll bar.
3110 */
3111 private int mVerticalScrollbarPosition;
3112
3113 /**
3114 * Position the scroll bar at the default position as determined by the system.
3115 */
3116 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3117
3118 /**
3119 * Position the scroll bar along the left edge.
3120 */
3121 public static final int SCROLLBAR_POSITION_LEFT = 1;
3122
3123 /**
3124 * Position the scroll bar along the right edge.
3125 */
3126 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3127
3128 /**
Romain Guy171c5922011-01-06 10:04:23 -08003129 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003130 *
3131 * @see #getLayerType()
3132 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003133 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003134 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003135 */
3136 public static final int LAYER_TYPE_NONE = 0;
3137
3138 /**
3139 * <p>Indicates that the view has a software layer. A software layer is backed
3140 * by a bitmap and causes the view to be rendered using Android's software
3141 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003142 *
Romain Guy171c5922011-01-06 10:04:23 -08003143 * <p>Software layers have various usages:</p>
3144 * <p>When the application is not using hardware acceleration, a software layer
3145 * is useful to apply a specific color filter and/or blending mode and/or
3146 * translucency to a view and all its children.</p>
3147 * <p>When the application is using hardware acceleration, a software layer
3148 * is useful to render drawing primitives not supported by the hardware
3149 * accelerated pipeline. It can also be used to cache a complex view tree
3150 * into a texture and reduce the complexity of drawing operations. For instance,
3151 * when animating a complex view tree with a translation, a software layer can
3152 * be used to render the view tree only once.</p>
3153 * <p>Software layers should be avoided when the affected view tree updates
3154 * often. Every update will require to re-render the software layer, which can
3155 * potentially be slow (particularly when hardware acceleration is turned on
3156 * since the layer will have to be uploaded into a hardware texture after every
3157 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003158 *
3159 * @see #getLayerType()
3160 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003161 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003162 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003163 */
3164 public static final int LAYER_TYPE_SOFTWARE = 1;
3165
3166 /**
3167 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3168 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3169 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3170 * rendering pipeline, but only if hardware acceleration is turned on for the
3171 * view hierarchy. When hardware acceleration is turned off, hardware layers
3172 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003173 *
Romain Guy171c5922011-01-06 10:04:23 -08003174 * <p>A hardware layer is useful to apply a specific color filter and/or
3175 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003176 * <p>A hardware layer can be used to cache a complex view tree into a
3177 * texture and reduce the complexity of drawing operations. For instance,
3178 * when animating a complex view tree with a translation, a hardware layer can
3179 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003180 * <p>A hardware layer can also be used to increase the rendering quality when
3181 * rotation transformations are applied on a view. It can also be used to
3182 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003183 *
3184 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003185 * @see #setLayerType(int, android.graphics.Paint)
3186 * @see #LAYER_TYPE_NONE
3187 * @see #LAYER_TYPE_SOFTWARE
3188 */
3189 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003190
Romain Guy3aaff3a2011-01-12 14:18:47 -08003191 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3192 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3193 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3194 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3195 })
Romain Guy171c5922011-01-06 10:04:23 -08003196 int mLayerType = LAYER_TYPE_NONE;
3197 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003198 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003199
3200 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003201 * Set to true when the view is sending hover accessibility events because it
3202 * is the innermost hovered view.
3203 */
3204 private boolean mSendingHoverAccessibilityEvents;
3205
3206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 * Simple constructor to use when creating a view from code.
3208 *
3209 * @param context The Context the view is running in, through which it can
3210 * access the current theme, resources, etc.
3211 */
3212 public View(Context context) {
3213 mContext = context;
3214 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003215 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003216 // Set layout and text direction defaults
3217 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003218 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003219 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07003220 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT) |
3221 (ACCESSIBILITY_FOCUSABLE_DEFAULT << ACCESSIBILITY_FOCUSABLE_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003222 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003223 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003224 mUserPaddingStart = -1;
3225 mUserPaddingEnd = -1;
3226 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228
3229 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003230 * Delegate for injecting accessiblity functionality.
3231 */
3232 AccessibilityDelegate mAccessibilityDelegate;
3233
3234 /**
3235 * Consistency verifier for debugging purposes.
3236 * @hide
3237 */
3238 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3239 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3240 new InputEventConsistencyVerifier(this, 0) : null;
3241
3242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 * Constructor that is called when inflating a view from XML. This is called
3244 * when a view is being constructed from an XML file, supplying attributes
3245 * that were specified in the XML file. This version uses a default style of
3246 * 0, so the only attribute values applied are those in the Context's Theme
3247 * and the given AttributeSet.
3248 *
3249 * <p>
3250 * The method onFinishInflate() will be called after all children have been
3251 * added.
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 * @see #View(Context, AttributeSet, int)
3257 */
3258 public View(Context context, AttributeSet attrs) {
3259 this(context, attrs, 0);
3260 }
3261
3262 /**
3263 * Perform inflation from XML and apply a class-specific base style. This
3264 * constructor of View allows subclasses to use their own base style when
3265 * they are inflating. For example, a Button class's constructor would call
3266 * this version of the super class constructor and supply
3267 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3268 * the theme's button style to modify all of the base view attributes (in
3269 * particular its background) as well as the Button class's attributes.
3270 *
3271 * @param context The Context the view is running in, through which it can
3272 * access the current theme, resources, etc.
3273 * @param attrs The attributes of the XML tag that is inflating the view.
3274 * @param defStyle The default style to apply to this view. If 0, no style
3275 * will be applied (beyond what is included in the theme). This may
3276 * either be an attribute resource, whose value will be retrieved
3277 * from the current theme, or an explicit style resource.
3278 * @see #View(Context, AttributeSet)
3279 */
3280 public View(Context context, AttributeSet attrs, int defStyle) {
3281 this(context);
3282
3283 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3284 defStyle, 0);
3285
3286 Drawable background = null;
3287
3288 int leftPadding = -1;
3289 int topPadding = -1;
3290 int rightPadding = -1;
3291 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003292 int startPadding = -1;
3293 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294
3295 int padding = -1;
3296
3297 int viewFlagValues = 0;
3298 int viewFlagMasks = 0;
3299
3300 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 int x = 0;
3303 int y = 0;
3304
Chet Haase73066682010-11-29 15:55:32 -08003305 float tx = 0;
3306 float ty = 0;
3307 float rotation = 0;
3308 float rotationX = 0;
3309 float rotationY = 0;
3310 float sx = 1f;
3311 float sy = 1f;
3312 boolean transformSet = false;
3313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3315
Adam Powell637d3372010-08-25 14:37:03 -07003316 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 final int N = a.getIndexCount();
3318 for (int i = 0; i < N; i++) {
3319 int attr = a.getIndex(i);
3320 switch (attr) {
3321 case com.android.internal.R.styleable.View_background:
3322 background = a.getDrawable(attr);
3323 break;
3324 case com.android.internal.R.styleable.View_padding:
3325 padding = a.getDimensionPixelSize(attr, -1);
3326 break;
3327 case com.android.internal.R.styleable.View_paddingLeft:
3328 leftPadding = a.getDimensionPixelSize(attr, -1);
3329 break;
3330 case com.android.internal.R.styleable.View_paddingTop:
3331 topPadding = a.getDimensionPixelSize(attr, -1);
3332 break;
3333 case com.android.internal.R.styleable.View_paddingRight:
3334 rightPadding = a.getDimensionPixelSize(attr, -1);
3335 break;
3336 case com.android.internal.R.styleable.View_paddingBottom:
3337 bottomPadding = a.getDimensionPixelSize(attr, -1);
3338 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003339 case com.android.internal.R.styleable.View_paddingStart:
3340 startPadding = a.getDimensionPixelSize(attr, -1);
3341 break;
3342 case com.android.internal.R.styleable.View_paddingEnd:
3343 endPadding = a.getDimensionPixelSize(attr, -1);
3344 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 case com.android.internal.R.styleable.View_scrollX:
3346 x = a.getDimensionPixelOffset(attr, 0);
3347 break;
3348 case com.android.internal.R.styleable.View_scrollY:
3349 y = a.getDimensionPixelOffset(attr, 0);
3350 break;
Chet Haase73066682010-11-29 15:55:32 -08003351 case com.android.internal.R.styleable.View_alpha:
3352 setAlpha(a.getFloat(attr, 1f));
3353 break;
3354 case com.android.internal.R.styleable.View_transformPivotX:
3355 setPivotX(a.getDimensionPixelOffset(attr, 0));
3356 break;
3357 case com.android.internal.R.styleable.View_transformPivotY:
3358 setPivotY(a.getDimensionPixelOffset(attr, 0));
3359 break;
3360 case com.android.internal.R.styleable.View_translationX:
3361 tx = a.getDimensionPixelOffset(attr, 0);
3362 transformSet = true;
3363 break;
3364 case com.android.internal.R.styleable.View_translationY:
3365 ty = a.getDimensionPixelOffset(attr, 0);
3366 transformSet = true;
3367 break;
3368 case com.android.internal.R.styleable.View_rotation:
3369 rotation = a.getFloat(attr, 0);
3370 transformSet = true;
3371 break;
3372 case com.android.internal.R.styleable.View_rotationX:
3373 rotationX = a.getFloat(attr, 0);
3374 transformSet = true;
3375 break;
3376 case com.android.internal.R.styleable.View_rotationY:
3377 rotationY = a.getFloat(attr, 0);
3378 transformSet = true;
3379 break;
3380 case com.android.internal.R.styleable.View_scaleX:
3381 sx = a.getFloat(attr, 1f);
3382 transformSet = true;
3383 break;
3384 case com.android.internal.R.styleable.View_scaleY:
3385 sy = a.getFloat(attr, 1f);
3386 transformSet = true;
3387 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 case com.android.internal.R.styleable.View_id:
3389 mID = a.getResourceId(attr, NO_ID);
3390 break;
3391 case com.android.internal.R.styleable.View_tag:
3392 mTag = a.getText(attr);
3393 break;
3394 case com.android.internal.R.styleable.View_fitsSystemWindows:
3395 if (a.getBoolean(attr, false)) {
3396 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3397 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3398 }
3399 break;
3400 case com.android.internal.R.styleable.View_focusable:
3401 if (a.getBoolean(attr, false)) {
3402 viewFlagValues |= FOCUSABLE;
3403 viewFlagMasks |= FOCUSABLE_MASK;
3404 }
3405 break;
3406 case com.android.internal.R.styleable.View_focusableInTouchMode:
3407 if (a.getBoolean(attr, false)) {
3408 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3409 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3410 }
3411 break;
3412 case com.android.internal.R.styleable.View_clickable:
3413 if (a.getBoolean(attr, false)) {
3414 viewFlagValues |= CLICKABLE;
3415 viewFlagMasks |= CLICKABLE;
3416 }
3417 break;
3418 case com.android.internal.R.styleable.View_longClickable:
3419 if (a.getBoolean(attr, false)) {
3420 viewFlagValues |= LONG_CLICKABLE;
3421 viewFlagMasks |= LONG_CLICKABLE;
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_saveEnabled:
3425 if (!a.getBoolean(attr, true)) {
3426 viewFlagValues |= SAVE_DISABLED;
3427 viewFlagMasks |= SAVE_DISABLED_MASK;
3428 }
3429 break;
3430 case com.android.internal.R.styleable.View_duplicateParentState:
3431 if (a.getBoolean(attr, false)) {
3432 viewFlagValues |= DUPLICATE_PARENT_STATE;
3433 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3434 }
3435 break;
3436 case com.android.internal.R.styleable.View_visibility:
3437 final int visibility = a.getInt(attr, 0);
3438 if (visibility != 0) {
3439 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3440 viewFlagMasks |= VISIBILITY_MASK;
3441 }
3442 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003443 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003444 // Clear any layout direction flags (included resolved bits) already set
3445 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3446 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003447 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003448 final int value = (layoutDirection != -1) ?
3449 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3450 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003451 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 case com.android.internal.R.styleable.View_drawingCacheQuality:
3453 final int cacheQuality = a.getInt(attr, 0);
3454 if (cacheQuality != 0) {
3455 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3456 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3457 }
3458 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003459 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003460 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003461 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3463 if (!a.getBoolean(attr, true)) {
3464 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3465 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3466 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003467 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3469 if (!a.getBoolean(attr, true)) {
3470 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3471 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3472 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003473 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 case R.styleable.View_scrollbars:
3475 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3476 if (scrollbars != SCROLLBARS_NONE) {
3477 viewFlagValues |= scrollbars;
3478 viewFlagMasks |= SCROLLBARS_MASK;
3479 initializeScrollbars(a);
3480 }
3481 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003482 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003484 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3485 // Ignore the attribute starting with ICS
3486 break;
3487 }
3488 // With builds < ICS, fall through and apply fading edges
3489 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3491 if (fadingEdge != FADING_EDGE_NONE) {
3492 viewFlagValues |= fadingEdge;
3493 viewFlagMasks |= FADING_EDGE_MASK;
3494 initializeFadingEdge(a);
3495 }
3496 break;
3497 case R.styleable.View_scrollbarStyle:
3498 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3499 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3500 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3501 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3502 }
3503 break;
3504 case R.styleable.View_isScrollContainer:
3505 setScrollContainer = true;
3506 if (a.getBoolean(attr, false)) {
3507 setScrollContainer(true);
3508 }
3509 break;
3510 case com.android.internal.R.styleable.View_keepScreenOn:
3511 if (a.getBoolean(attr, false)) {
3512 viewFlagValues |= KEEP_SCREEN_ON;
3513 viewFlagMasks |= KEEP_SCREEN_ON;
3514 }
3515 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003516 case R.styleable.View_filterTouchesWhenObscured:
3517 if (a.getBoolean(attr, false)) {
3518 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3519 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3520 }
3521 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 case R.styleable.View_nextFocusLeft:
3523 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3524 break;
3525 case R.styleable.View_nextFocusRight:
3526 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3527 break;
3528 case R.styleable.View_nextFocusUp:
3529 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3530 break;
3531 case R.styleable.View_nextFocusDown:
3532 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3533 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003534 case R.styleable.View_nextFocusForward:
3535 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3536 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 case R.styleable.View_minWidth:
3538 mMinWidth = a.getDimensionPixelSize(attr, 0);
3539 break;
3540 case R.styleable.View_minHeight:
3541 mMinHeight = a.getDimensionPixelSize(attr, 0);
3542 break;
Romain Guy9a817362009-05-01 10:57:14 -07003543 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003544 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003545 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003546 + "be used within a restricted context");
3547 }
3548
Romain Guy9a817362009-05-01 10:57:14 -07003549 final String handlerName = a.getString(attr);
3550 if (handlerName != null) {
3551 setOnClickListener(new OnClickListener() {
3552 private Method mHandler;
3553
3554 public void onClick(View v) {
3555 if (mHandler == null) {
3556 try {
3557 mHandler = getContext().getClass().getMethod(handlerName,
3558 View.class);
3559 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003560 int id = getId();
3561 String idText = id == NO_ID ? "" : " with id '"
3562 + getContext().getResources().getResourceEntryName(
3563 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003564 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003565 handlerName + "(View) in the activity "
3566 + getContext().getClass() + " for onClick handler"
3567 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003568 }
3569 }
3570
3571 try {
3572 mHandler.invoke(getContext(), View.this);
3573 } catch (IllegalAccessException e) {
3574 throw new IllegalStateException("Could not execute non "
3575 + "public method of the activity", e);
3576 } catch (InvocationTargetException e) {
3577 throw new IllegalStateException("Could not execute "
3578 + "method of the activity", e);
3579 }
3580 }
3581 });
3582 }
3583 break;
Adam Powell637d3372010-08-25 14:37:03 -07003584 case R.styleable.View_overScrollMode:
3585 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3586 break;
Adam Powell20232d02010-12-08 21:08:53 -08003587 case R.styleable.View_verticalScrollbarPosition:
3588 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3589 break;
Romain Guy171c5922011-01-06 10:04:23 -08003590 case R.styleable.View_layerType:
3591 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3592 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003593 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003594 // Clear any text direction flag already set
3595 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3596 // Set the text direction flags depending on the value of the attribute
3597 final int textDirection = a.getInt(attr, -1);
3598 if (textDirection != -1) {
3599 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3600 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003601 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003602 case R.styleable.View_textAlignment:
3603 // Clear any text alignment flag already set
3604 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3605 // Set the text alignment flag depending on the value of the attribute
3606 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3607 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3608 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003609 case R.styleable.View_importantForAccessibility:
3610 setImportantForAccessibility(a.getInt(attr,
3611 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613 }
3614
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003615 a.recycle();
3616
Adam Powell637d3372010-08-25 14:37:03 -07003617 setOverScrollMode(overScrollMode);
3618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003620 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 }
3622
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003623 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3624 // layout direction). Those cached values will be used later during padding resolution.
3625 mUserPaddingStart = startPadding;
3626 mUserPaddingEnd = endPadding;
3627
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003628 updateUserPaddingRelative();
3629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 if (padding >= 0) {
3631 leftPadding = padding;
3632 topPadding = padding;
3633 rightPadding = padding;
3634 bottomPadding = padding;
3635 }
3636
3637 // If the user specified the padding (either with android:padding or
3638 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3639 // use the default padding or the padding from the background drawable
3640 // (stored at this point in mPadding*)
3641 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3642 topPadding >= 0 ? topPadding : mPaddingTop,
3643 rightPadding >= 0 ? rightPadding : mPaddingRight,
3644 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3645
3646 if (viewFlagMasks != 0) {
3647 setFlags(viewFlagValues, viewFlagMasks);
3648 }
3649
3650 // Needs to be called after mViewFlags is set
3651 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3652 recomputePadding();
3653 }
3654
3655 if (x != 0 || y != 0) {
3656 scrollTo(x, y);
3657 }
3658
Chet Haase73066682010-11-29 15:55:32 -08003659 if (transformSet) {
3660 setTranslationX(tx);
3661 setTranslationY(ty);
3662 setRotation(rotation);
3663 setRotationX(rotationX);
3664 setRotationY(rotationY);
3665 setScaleX(sx);
3666 setScaleY(sy);
3667 }
3668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3670 setScrollContainer(true);
3671 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003672
3673 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003676 private void updateUserPaddingRelative() {
3677 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3678 }
3679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 /**
3681 * Non-public constructor for use in testing
3682 */
3683 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003684 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 /**
3688 * <p>
3689 * Initializes the fading edges from a given set of styled attributes. This
3690 * method should be called by subclasses that need fading edges and when an
3691 * instance of these subclasses is created programmatically rather than
3692 * being inflated from XML. This method is automatically called when the XML
3693 * is inflated.
3694 * </p>
3695 *
3696 * @param a the styled attributes set to initialize the fading edges from
3697 */
3698 protected void initializeFadingEdge(TypedArray a) {
3699 initScrollCache();
3700
3701 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3702 R.styleable.View_fadingEdgeLength,
3703 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3704 }
3705
3706 /**
3707 * Returns the size of the vertical faded edges used to indicate that more
3708 * content in this view is visible.
3709 *
3710 * @return The size in pixels of the vertical faded edge or 0 if vertical
3711 * faded edges are not enabled for this view.
3712 * @attr ref android.R.styleable#View_fadingEdgeLength
3713 */
3714 public int getVerticalFadingEdgeLength() {
3715 if (isVerticalFadingEdgeEnabled()) {
3716 ScrollabilityCache cache = mScrollCache;
3717 if (cache != null) {
3718 return cache.fadingEdgeLength;
3719 }
3720 }
3721 return 0;
3722 }
3723
3724 /**
3725 * Set the size of the faded edge used to indicate that more content in this
3726 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003727 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3728 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3729 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 *
3731 * @param length The size in pixels of the faded edge used to indicate that more
3732 * content in this view is visible.
3733 */
3734 public void setFadingEdgeLength(int length) {
3735 initScrollCache();
3736 mScrollCache.fadingEdgeLength = length;
3737 }
3738
3739 /**
3740 * Returns the size of the horizontal faded edges used to indicate that more
3741 * content in this view is visible.
3742 *
3743 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3744 * faded edges are not enabled for this view.
3745 * @attr ref android.R.styleable#View_fadingEdgeLength
3746 */
3747 public int getHorizontalFadingEdgeLength() {
3748 if (isHorizontalFadingEdgeEnabled()) {
3749 ScrollabilityCache cache = mScrollCache;
3750 if (cache != null) {
3751 return cache.fadingEdgeLength;
3752 }
3753 }
3754 return 0;
3755 }
3756
3757 /**
3758 * Returns the width of the vertical scrollbar.
3759 *
3760 * @return The width in pixels of the vertical scrollbar or 0 if there
3761 * is no vertical scrollbar.
3762 */
3763 public int getVerticalScrollbarWidth() {
3764 ScrollabilityCache cache = mScrollCache;
3765 if (cache != null) {
3766 ScrollBarDrawable scrollBar = cache.scrollBar;
3767 if (scrollBar != null) {
3768 int size = scrollBar.getSize(true);
3769 if (size <= 0) {
3770 size = cache.scrollBarSize;
3771 }
3772 return size;
3773 }
3774 return 0;
3775 }
3776 return 0;
3777 }
3778
3779 /**
3780 * Returns the height of the horizontal scrollbar.
3781 *
3782 * @return The height in pixels of the horizontal scrollbar or 0 if
3783 * there is no horizontal scrollbar.
3784 */
3785 protected int getHorizontalScrollbarHeight() {
3786 ScrollabilityCache cache = mScrollCache;
3787 if (cache != null) {
3788 ScrollBarDrawable scrollBar = cache.scrollBar;
3789 if (scrollBar != null) {
3790 int size = scrollBar.getSize(false);
3791 if (size <= 0) {
3792 size = cache.scrollBarSize;
3793 }
3794 return size;
3795 }
3796 return 0;
3797 }
3798 return 0;
3799 }
3800
3801 /**
3802 * <p>
3803 * Initializes the scrollbars from a given set of styled attributes. This
3804 * method should be called by subclasses that need scrollbars and when an
3805 * instance of these subclasses is created programmatically rather than
3806 * being inflated from XML. This method is automatically called when the XML
3807 * is inflated.
3808 * </p>
3809 *
3810 * @param a the styled attributes set to initialize the scrollbars from
3811 */
3812 protected void initializeScrollbars(TypedArray a) {
3813 initScrollCache();
3814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003816
Mike Cleronf116bf82009-09-27 19:14:12 -07003817 if (scrollabilityCache.scrollBar == null) {
3818 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3819 }
Joe Malin32736f02011-01-19 16:14:20 -08003820
Romain Guy8bda2482010-03-02 11:42:11 -08003821 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822
Mike Cleronf116bf82009-09-27 19:14:12 -07003823 if (!fadeScrollbars) {
3824 scrollabilityCache.state = ScrollabilityCache.ON;
3825 }
3826 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003827
3828
Mike Cleronf116bf82009-09-27 19:14:12 -07003829 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3830 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3831 .getScrollBarFadeDuration());
3832 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3833 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3834 ViewConfiguration.getScrollDefaultDelay());
3835
Joe Malin32736f02011-01-19 16:14:20 -08003836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3838 com.android.internal.R.styleable.View_scrollbarSize,
3839 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3840
3841 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3842 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3843
3844 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3845 if (thumb != null) {
3846 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3847 }
3848
3849 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3850 false);
3851 if (alwaysDraw) {
3852 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3853 }
3854
3855 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3856 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3857
3858 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3859 if (thumb != null) {
3860 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3861 }
3862
3863 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3864 false);
3865 if (alwaysDraw) {
3866 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3867 }
3868
3869 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003870 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
3872
3873 /**
3874 * <p>
3875 * Initalizes the scrollability cache if necessary.
3876 * </p>
3877 */
3878 private void initScrollCache() {
3879 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003880 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 }
3882 }
3883
Philip Milne6c8ea062012-04-03 17:38:43 -07003884 private ScrollabilityCache getScrollCache() {
3885 initScrollCache();
3886 return mScrollCache;
3887 }
3888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 /**
Adam Powell20232d02010-12-08 21:08:53 -08003890 * Set the position of the vertical scroll bar. Should be one of
3891 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3892 * {@link #SCROLLBAR_POSITION_RIGHT}.
3893 *
3894 * @param position Where the vertical scroll bar should be positioned.
3895 */
3896 public void setVerticalScrollbarPosition(int position) {
3897 if (mVerticalScrollbarPosition != position) {
3898 mVerticalScrollbarPosition = position;
3899 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003900 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003901 }
3902 }
3903
3904 /**
3905 * @return The position where the vertical scroll bar will show, if applicable.
3906 * @see #setVerticalScrollbarPosition(int)
3907 */
3908 public int getVerticalScrollbarPosition() {
3909 return mVerticalScrollbarPosition;
3910 }
3911
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003912 ListenerInfo getListenerInfo() {
3913 if (mListenerInfo != null) {
3914 return mListenerInfo;
3915 }
3916 mListenerInfo = new ListenerInfo();
3917 return mListenerInfo;
3918 }
3919
Adam Powell20232d02010-12-08 21:08:53 -08003920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 * Register a callback to be invoked when focus of this view changed.
3922 *
3923 * @param l The callback that will run.
3924 */
3925 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003926 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 }
3928
3929 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003930 * Add a listener that will be called when the bounds of the view change due to
3931 * layout processing.
3932 *
3933 * @param listener The listener that will be called when layout bounds change.
3934 */
3935 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003936 ListenerInfo li = getListenerInfo();
3937 if (li.mOnLayoutChangeListeners == null) {
3938 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003939 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003940 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3941 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003942 }
Chet Haase21cd1382010-09-01 17:42:29 -07003943 }
3944
3945 /**
3946 * Remove a listener for layout changes.
3947 *
3948 * @param listener The listener for layout bounds change.
3949 */
3950 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 ListenerInfo li = mListenerInfo;
3952 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003953 return;
3954 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003956 }
3957
3958 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003959 * Add a listener for attach state changes.
3960 *
3961 * This listener will be called whenever this view is attached or detached
3962 * from a window. Remove the listener using
3963 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3964 *
3965 * @param listener Listener to attach
3966 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3967 */
3968 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003969 ListenerInfo li = getListenerInfo();
3970 if (li.mOnAttachStateChangeListeners == null) {
3971 li.mOnAttachStateChangeListeners
3972 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003973 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003974 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003975 }
3976
3977 /**
3978 * Remove a listener for attach state changes. The listener will receive no further
3979 * notification of window attach/detach events.
3980 *
3981 * @param listener Listener to remove
3982 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3983 */
3984 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003987 return;
3988 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003989 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003990 }
3991
3992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 * Returns the focus-change callback registered for this view.
3994 *
3995 * @return The callback, or null if one is not registered.
3996 */
3997 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003998 ListenerInfo li = mListenerInfo;
3999 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 }
4001
4002 /**
4003 * Register a callback to be invoked when this view is clicked. If this view is not
4004 * clickable, it becomes clickable.
4005 *
4006 * @param l The callback that will run
4007 *
4008 * @see #setClickable(boolean)
4009 */
4010 public void setOnClickListener(OnClickListener l) {
4011 if (!isClickable()) {
4012 setClickable(true);
4013 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004014 getListenerInfo().mOnClickListener = l;
4015 }
4016
4017 /**
4018 * Return whether this view has an attached OnClickListener. Returns
4019 * true if there is a listener, false if there is none.
4020 */
4021 public boolean hasOnClickListeners() {
4022 ListenerInfo li = mListenerInfo;
4023 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025
4026 /**
4027 * Register a callback to be invoked when this view is clicked and held. If this view is not
4028 * long clickable, it becomes long clickable.
4029 *
4030 * @param l The callback that will run
4031 *
4032 * @see #setLongClickable(boolean)
4033 */
4034 public void setOnLongClickListener(OnLongClickListener l) {
4035 if (!isLongClickable()) {
4036 setLongClickable(true);
4037 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 /**
4042 * Register a callback to be invoked when the context menu for this view is
4043 * being built. If this view is not long clickable, it becomes long clickable.
4044 *
4045 * @param l The callback that will run
4046 *
4047 */
4048 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4049 if (!isLongClickable()) {
4050 setLongClickable(true);
4051 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004052 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
4054
4055 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004056 * Call this view's OnClickListener, if it is defined. Performs all normal
4057 * actions associated with clicking: reporting accessibility event, playing
4058 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 *
4060 * @return True there was an assigned OnClickListener that was called, false
4061 * otherwise is returned.
4062 */
4063 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004064 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4065
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004066 ListenerInfo li = mListenerInfo;
4067 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004069 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 return true;
4071 }
4072
4073 return false;
4074 }
4075
4076 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004077 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4078 * this only calls the listener, and does not do any associated clicking
4079 * actions like reporting an accessibility event.
4080 *
4081 * @return True there was an assigned OnClickListener that was called, false
4082 * otherwise is returned.
4083 */
4084 public boolean callOnClick() {
4085 ListenerInfo li = mListenerInfo;
4086 if (li != null && li.mOnClickListener != null) {
4087 li.mOnClickListener.onClick(this);
4088 return true;
4089 }
4090 return false;
4091 }
4092
4093 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004094 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4095 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004097 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 */
4099 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004100 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004103 ListenerInfo li = mListenerInfo;
4104 if (li != null && li.mOnLongClickListener != null) {
4105 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 if (!handled) {
4108 handled = showContextMenu();
4109 }
4110 if (handled) {
4111 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4112 }
4113 return handled;
4114 }
4115
4116 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004117 * Performs button-related actions during a touch down event.
4118 *
4119 * @param event The event.
4120 * @return True if the down was consumed.
4121 *
4122 * @hide
4123 */
4124 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4125 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4126 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4127 return true;
4128 }
4129 }
4130 return false;
4131 }
4132
4133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * Bring up the context menu for this view.
4135 *
4136 * @return Whether a context menu was displayed.
4137 */
4138 public boolean showContextMenu() {
4139 return getParent().showContextMenuForChild(this);
4140 }
4141
4142 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004143 * Bring up the context menu for this view, referring to the item under the specified point.
4144 *
4145 * @param x The referenced x coordinate.
4146 * @param y The referenced y coordinate.
4147 * @param metaState The keyboard modifiers that were pressed.
4148 * @return Whether a context menu was displayed.
4149 *
4150 * @hide
4151 */
4152 public boolean showContextMenu(float x, float y, int metaState) {
4153 return showContextMenu();
4154 }
4155
4156 /**
Adam Powell6e346362010-07-23 10:18:23 -07004157 * Start an action mode.
4158 *
4159 * @param callback Callback that will control the lifecycle of the action mode
4160 * @return The new action mode if it is started, null otherwise
4161 *
4162 * @see ActionMode
4163 */
4164 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004165 ViewParent parent = getParent();
4166 if (parent == null) return null;
4167 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004168 }
4169
4170 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004171 * Register a callback to be invoked when a hardware key is pressed in this view.
4172 * Key presses in software input methods will generally not trigger the methods of
4173 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 * @param l the key listener to attach to this view
4175 */
4176 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004177 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 }
4179
4180 /**
4181 * Register a callback to be invoked when a touch event is sent to this view.
4182 * @param l the touch listener to attach to this view
4183 */
4184 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004185 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
4187
4188 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004189 * Register a callback to be invoked when a generic motion event is sent to this view.
4190 * @param l the generic motion listener to attach to this view
4191 */
4192 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004193 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004194 }
4195
4196 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004197 * Register a callback to be invoked when a hover event is sent to this view.
4198 * @param l the hover listener to attach to this view
4199 */
4200 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004201 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004202 }
4203
4204 /**
Joe Malin32736f02011-01-19 16:14:20 -08004205 * Register a drag event listener callback object for this View. The parameter is
4206 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4207 * View, the system calls the
4208 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4209 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004210 */
4211 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004212 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004213 }
4214
4215 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004216 * Give this view focus. This will cause
4217 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 *
4219 * Note: this does not check whether this {@link View} should get focus, it just
4220 * gives it focus no matter what. It should only be called internally by framework
4221 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4222 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004223 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4224 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 * focus moved when requestFocus() is called. It may not always
4226 * apply, in which case use the default View.FOCUS_DOWN.
4227 * @param previouslyFocusedRect The rectangle of the view that had focus
4228 * prior in this View's coordinate system.
4229 */
4230 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4231 if (DBG) {
4232 System.out.println(this + " requestFocus()");
4233 }
4234
4235 if ((mPrivateFlags & FOCUSED) == 0) {
4236 mPrivateFlags |= FOCUSED;
4237
4238 if (mParent != null) {
4239 mParent.requestChildFocus(this, this);
4240 }
4241
4242 onFocusChanged(true, direction, previouslyFocusedRect);
4243 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
4251 /**
4252 * Request that a rectangle of this view be visible on the screen,
4253 * scrolling if necessary just enough.
4254 *
4255 * <p>A View should call this if it maintains some notion of which part
4256 * of its content is interesting. For example, a text editing view
4257 * should call this when its cursor moves.
4258 *
4259 * @param rectangle The rectangle.
4260 * @return Whether any parent scrolled.
4261 */
4262 public boolean requestRectangleOnScreen(Rect rectangle) {
4263 return requestRectangleOnScreen(rectangle, false);
4264 }
4265
4266 /**
4267 * Request that a rectangle of this view be visible on the screen,
4268 * scrolling if necessary just enough.
4269 *
4270 * <p>A View should call this if it maintains some notion of which part
4271 * of its content is interesting. For example, a text editing view
4272 * should call this when its cursor moves.
4273 *
4274 * <p>When <code>immediate</code> is set to true, scrolling will not be
4275 * animated.
4276 *
4277 * @param rectangle The rectangle.
4278 * @param immediate True to forbid animated scrolling, false otherwise
4279 * @return Whether any parent scrolled.
4280 */
4281 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4282 View child = this;
4283 ViewParent parent = mParent;
4284 boolean scrolled = false;
4285 while (parent != null) {
4286 scrolled |= parent.requestChildRectangleOnScreen(child,
4287 rectangle, immediate);
4288
4289 // offset rect so next call has the rectangle in the
4290 // coordinate system of its direct child.
4291 rectangle.offset(child.getLeft(), child.getTop());
4292 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4293
4294 if (!(parent instanceof View)) {
4295 break;
4296 }
Romain Guy8506ab42009-06-11 17:35:47 -07004297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 child = (View) parent;
4299 parent = child.getParent();
4300 }
4301 return scrolled;
4302 }
4303
4304 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004305 * Called when this view wants to give up focus. If focus is cleared
4306 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4307 * <p>
4308 * <strong>Note:</strong> When a View clears focus the framework is trying
4309 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004310 * View is the first from the top that can take focus, then all callbacks
4311 * related to clearing focus will be invoked after wich the framework will
4312 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004313 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 */
4315 public void clearFocus() {
4316 if (DBG) {
4317 System.out.println(this + " clearFocus()");
4318 }
4319
4320 if ((mPrivateFlags & FOCUSED) != 0) {
4321 mPrivateFlags &= ~FOCUSED;
4322
4323 if (mParent != null) {
4324 mParent.clearChildFocus(this);
4325 }
4326
4327 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004330
4331 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004332
4333 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4334 notifyAccessibilityStateChanged();
4335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 }
4337 }
4338
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004339 void ensureInputFocusOnFirstFocusable() {
4340 View root = getRootView();
4341 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004342 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 }
4344 }
4345
4346 /**
4347 * Called internally by the view system when a new view is getting focus.
4348 * This is what clears the old focus.
4349 */
4350 void unFocus() {
4351 if (DBG) {
4352 System.out.println(this + " unFocus()");
4353 }
4354
4355 if ((mPrivateFlags & FOCUSED) != 0) {
4356 mPrivateFlags &= ~FOCUSED;
4357
4358 onFocusChanged(false, 0, null);
4359 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004360
4361 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4362 notifyAccessibilityStateChanged();
4363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365 }
4366
4367 /**
4368 * Returns true if this view has focus iteself, or is the ancestor of the
4369 * view that has focus.
4370 *
4371 * @return True if this view has or contains focus, false otherwise.
4372 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004373 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 public boolean hasFocus() {
4375 return (mPrivateFlags & FOCUSED) != 0;
4376 }
4377
4378 /**
4379 * Returns true if this view is focusable or if it contains a reachable View
4380 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4381 * is a View whose parents do not block descendants focus.
4382 *
4383 * Only {@link #VISIBLE} views are considered focusable.
4384 *
4385 * @return True if the view is focusable or if the view contains a focusable
4386 * View, false otherwise.
4387 *
4388 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4389 */
4390 public boolean hasFocusable() {
4391 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4392 }
4393
4394 /**
4395 * Called by the view system when the focus state of this view changes.
4396 * When the focus change event is caused by directional navigation, direction
4397 * and previouslyFocusedRect provide insight into where the focus is coming from.
4398 * When overriding, be sure to call up through to the super class so that
4399 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 * @param gainFocus True if the View has focus; false otherwise.
4402 * @param direction The direction focus has moved when requestFocus()
4403 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004404 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4405 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4406 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4408 * system, of the previously focused view. If applicable, this will be
4409 * passed in as finer grained information about where the focus is coming
4410 * from (in addition to direction). Will be <code>null</code> otherwise.
4411 */
4412 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004413 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004414 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4415 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004416 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004417 }
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 InputMethodManager imm = InputMethodManager.peekInstance();
4420 if (!gainFocus) {
4421 if (isPressed()) {
4422 setPressed(false);
4423 }
4424 if (imm != null && mAttachInfo != null
4425 && mAttachInfo.mHasWindowFocus) {
4426 imm.focusOut(this);
4427 }
Romain Guya2431d02009-04-30 16:30:00 -07004428 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 } else if (imm != null && mAttachInfo != null
4430 && mAttachInfo.mHasWindowFocus) {
4431 imm.focusIn(this);
4432 }
Romain Guy8506ab42009-06-11 17:35:47 -07004433
Romain Guy0fd89bf2011-01-26 15:41:30 -08004434 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004435 ListenerInfo li = mListenerInfo;
4436 if (li != null && li.mOnFocusChangeListener != null) {
4437 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
Joe Malin32736f02011-01-19 16:14:20 -08004439
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004440 if (mAttachInfo != null) {
4441 mAttachInfo.mKeyDispatchState.reset(this);
4442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 }
4444
4445 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004446 * Sends an accessibility event of the given type. If accessiiblity is
4447 * not enabled this method has no effect. The default implementation calls
4448 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4449 * to populate information about the event source (this View), then calls
4450 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4451 * populate the text content of the event source including its descendants,
4452 * and last calls
4453 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4454 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004455 * <p>
4456 * If an {@link AccessibilityDelegate} has been specified via calling
4457 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4458 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4459 * responsible for handling this call.
4460 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 *
Scott Mainb303d832011-10-12 16:45:18 -07004462 * @param eventType The type of the event to send, as defined by several types from
4463 * {@link android.view.accessibility.AccessibilityEvent}, such as
4464 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4465 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004466 *
4467 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4468 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4469 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004470 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004471 */
4472 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004473 if (mAccessibilityDelegate != null) {
4474 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4475 } else {
4476 sendAccessibilityEventInternal(eventType);
4477 }
4478 }
4479
4480 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004481 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4482 * {@link AccessibilityEvent} to make an announcement which is related to some
4483 * sort of a context change for which none of the events representing UI transitions
4484 * is a good fit. For example, announcing a new page in a book. If accessibility
4485 * is not enabled this method does nothing.
4486 *
4487 * @param text The announcement text.
4488 */
4489 public void announceForAccessibility(CharSequence text) {
4490 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4491 AccessibilityEvent event = AccessibilityEvent.obtain(
4492 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4493 event.getText().add(text);
4494 sendAccessibilityEventUnchecked(event);
4495 }
4496 }
4497
4498 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004499 * @see #sendAccessibilityEvent(int)
4500 *
4501 * Note: Called from the default {@link AccessibilityDelegate}.
4502 */
4503 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004504 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4505 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4506 }
4507 }
4508
4509 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004510 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4511 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004512 * perform a check whether accessibility is enabled.
4513 * <p>
4514 * If an {@link AccessibilityDelegate} has been specified via calling
4515 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4516 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4517 * is responsible for handling this call.
4518 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004519 *
4520 * @param event The event to send.
4521 *
4522 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004523 */
4524 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004525 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004526 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004527 } else {
4528 sendAccessibilityEventUncheckedInternal(event);
4529 }
4530 }
4531
4532 /**
4533 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4534 *
4535 * Note: Called from the default {@link AccessibilityDelegate}.
4536 */
4537 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004538 if (!isShown()) {
4539 return;
4540 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004541 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004542 // Only a subset of accessibility events populates text content.
4543 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4544 dispatchPopulateAccessibilityEvent(event);
4545 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004546 // Intercept accessibility focus events fired by virtual nodes to keep
4547 // track of accessibility focus position in such nodes.
4548 final int eventType = event.getEventType();
4549 switch (eventType) {
4550 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4551 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4552 event.getSourceNodeId());
4553 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4554 ViewRootImpl viewRootImpl = getViewRootImpl();
4555 if (viewRootImpl != null) {
4556 viewRootImpl.setAccessibilityFocusedHost(this);
4557 }
4558 }
4559 } break;
4560 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4561 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4562 event.getSourceNodeId());
4563 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4564 ViewRootImpl viewRootImpl = getViewRootImpl();
4565 if (viewRootImpl != null) {
4566 viewRootImpl.setAccessibilityFocusedHost(null);
4567 }
4568 }
4569 } break;
4570 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004571 // In the beginning we called #isShown(), so we know that getParent() is not null.
4572 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004573 }
4574
4575 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004576 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4577 * to its children for adding their text content to the event. Note that the
4578 * event text is populated in a separate dispatch path since we add to the
4579 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004580 * A typical implementation will call
4581 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4582 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4583 * on each child. Override this method if custom population of the event text
4584 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004585 * <p>
4586 * If an {@link AccessibilityDelegate} has been specified via calling
4587 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4588 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4589 * is responsible for handling this call.
4590 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004591 * <p>
4592 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4593 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4594 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004595 *
4596 * @param event The event.
4597 *
4598 * @return True if the event population was completed.
4599 */
4600 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004601 if (mAccessibilityDelegate != null) {
4602 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4603 } else {
4604 return dispatchPopulateAccessibilityEventInternal(event);
4605 }
4606 }
4607
4608 /**
4609 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4610 *
4611 * Note: Called from the default {@link AccessibilityDelegate}.
4612 */
4613 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004614 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004615 return false;
4616 }
4617
4618 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004619 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004620 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004621 * text content. While this method is free to modify event
4622 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004623 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4624 * <p>
4625 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004626 * to the text added by the super implementation:
4627 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004628 * super.onPopulateAccessibilityEvent(event);
4629 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4630 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4631 * mCurrentDate.getTimeInMillis(), flags);
4632 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004633 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004634 * <p>
4635 * If an {@link AccessibilityDelegate} has been specified via calling
4636 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4637 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4638 * is responsible for handling this call.
4639 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004640 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4641 * information to the event, in case the default implementation has basic information to add.
4642 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004643 *
4644 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004645 *
4646 * @see #sendAccessibilityEvent(int)
4647 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004648 */
4649 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004650 if (mAccessibilityDelegate != null) {
4651 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4652 } else {
4653 onPopulateAccessibilityEventInternal(event);
4654 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004655 }
4656
4657 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4659 *
4660 * Note: Called from the default {@link AccessibilityDelegate}.
4661 */
4662 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4663
4664 }
4665
4666 /**
4667 * Initializes an {@link AccessibilityEvent} with information about
4668 * this View which is the event source. In other words, the source of
4669 * an accessibility event is the view whose state change triggered firing
4670 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004671 * <p>
4672 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004673 * to properties set by the super implementation:
4674 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4675 * super.onInitializeAccessibilityEvent(event);
4676 * event.setPassword(true);
4677 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004678 * <p>
4679 * If an {@link AccessibilityDelegate} has been specified via calling
4680 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4681 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4682 * is responsible for handling this call.
4683 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004684 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4685 * information to the event, in case the default implementation has basic information to add.
4686 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004687 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004688 *
4689 * @see #sendAccessibilityEvent(int)
4690 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4691 */
4692 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004693 if (mAccessibilityDelegate != null) {
4694 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4695 } else {
4696 onInitializeAccessibilityEventInternal(event);
4697 }
4698 }
4699
4700 /**
4701 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4702 *
4703 * Note: Called from the default {@link AccessibilityDelegate}.
4704 */
4705 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004706 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004707 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004708 event.setPackageName(getContext().getPackageName());
4709 event.setEnabled(isEnabled());
4710 event.setContentDescription(mContentDescription);
4711
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004712 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004713 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004714 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4715 FOCUSABLES_ALL);
4716 event.setItemCount(focusablesTempList.size());
4717 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4718 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004719 }
4720 }
4721
4722 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004723 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4724 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4725 * This method is responsible for obtaining an accessibility node info from a
4726 * pool of reusable instances and calling
4727 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4728 * initialize the former.
4729 * <p>
4730 * Note: The client is responsible for recycling the obtained instance by calling
4731 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4732 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004733 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004734 * @return A populated {@link AccessibilityNodeInfo}.
4735 *
4736 * @see AccessibilityNodeInfo
4737 */
4738 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004739 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4740 if (provider != null) {
4741 return provider.createAccessibilityNodeInfo(View.NO_ID);
4742 } else {
4743 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4744 onInitializeAccessibilityNodeInfo(info);
4745 return info;
4746 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004747 }
4748
4749 /**
4750 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4751 * The base implementation sets:
4752 * <ul>
4753 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004754 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4755 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004756 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4757 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4758 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4759 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4760 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4761 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4762 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4763 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4764 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4765 * </ul>
4766 * <p>
4767 * Subclasses should override this method, call the super implementation,
4768 * and set additional attributes.
4769 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004770 * <p>
4771 * If an {@link AccessibilityDelegate} has been specified via calling
4772 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4773 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4774 * is responsible for handling this call.
4775 * </p>
4776 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004777 * @param info The instance to initialize.
4778 */
4779 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004780 if (mAccessibilityDelegate != null) {
4781 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4782 } else {
4783 onInitializeAccessibilityNodeInfoInternal(info);
4784 }
4785 }
4786
4787 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004788 * Gets the location of this view in screen coordintates.
4789 *
4790 * @param outRect The output location
4791 */
4792 private void getBoundsOnScreen(Rect outRect) {
4793 if (mAttachInfo == null) {
4794 return;
4795 }
4796
4797 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004798 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004799
4800 if (!hasIdentityMatrix()) {
4801 getMatrix().mapRect(position);
4802 }
4803
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004804 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004805
4806 ViewParent parent = mParent;
4807 while (parent instanceof View) {
4808 View parentView = (View) parent;
4809
4810 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4811
4812 if (!parentView.hasIdentityMatrix()) {
4813 parentView.getMatrix().mapRect(position);
4814 }
4815
4816 position.offset(parentView.mLeft, parentView.mTop);
4817
4818 parent = parentView.mParent;
4819 }
4820
4821 if (parent instanceof ViewRootImpl) {
4822 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4823 position.offset(0, -viewRootImpl.mCurScrollY);
4824 }
4825
4826 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4827
4828 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4829 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4830 }
4831
4832 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004833 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4834 *
4835 * Note: Called from the default {@link AccessibilityDelegate}.
4836 */
4837 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004838 Rect bounds = mAttachInfo.mTmpInvalRect;
4839 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004840 info.setBoundsInParent(bounds);
4841
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004842 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004843 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004844
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004845 ViewParent parent = getParentForAccessibility();
4846 if (parent instanceof View) {
4847 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004848 }
4849
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004850 info.setVisibleToUser(isVisibleToUser());
4851
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004852 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004853 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004854 info.setContentDescription(getContentDescription());
4855
4856 info.setEnabled(isEnabled());
4857 info.setClickable(isClickable());
4858 info.setFocusable(isFocusable());
4859 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004860 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004861 info.setSelected(isSelected());
4862 info.setLongClickable(isLongClickable());
4863
4864 // TODO: These make sense only if we are in an AdapterView but all
4865 // views can be selected. Maybe from accessiiblity perspective
4866 // we should report as selectable view in an AdapterView.
4867 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4868 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4869
4870 if (isFocusable()) {
4871 if (isFocused()) {
4872 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4873 } else {
4874 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4875 }
4876 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004877
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004878 if (!isAccessibilityFocused()) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07004879 final int mode = getAccessibilityFocusable();
4880 if (mode == ACCESSIBILITY_FOCUSABLE_YES || mode == ACCESSIBILITY_FOCUSABLE_AUTO) {
4881 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4882 }
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004883 } else {
4884 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4885 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004886
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004887 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004888 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4889 }
4890
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004891 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004892 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4893 }
4894
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004895 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004896 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4897 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4898 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004899 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4900 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004901 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004902 }
4903
4904 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004905 * Computes whether this view is visible to the user. Such a view is
4906 * attached, visible, all its predecessors are visible, it is not clipped
4907 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004908 *
4909 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004910 *
4911 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004912 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004913 protected boolean isVisibleToUser() {
4914 return isVisibleToUser(null);
4915 }
4916
4917 /**
4918 * Computes whether the given portion of this view is visible to the user. Such a view is
4919 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4920 * the specified portion is not clipped entirely by its predecessors.
4921 *
4922 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4923 * <code>null</code>, and the entire view will be tested in this case.
4924 * When <code>true</code> is returned by the function, the actual visible
4925 * region will be stored in this parameter; that is, if boundInView is fully
4926 * contained within the view, no modification will be made, otherwise regions
4927 * outside of the visible area of the view will be clipped.
4928 *
4929 * @return Whether the specified portion of the view is visible on the screen.
4930 *
4931 * @hide
4932 */
4933 protected boolean isVisibleToUser(Rect boundInView) {
4934 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4935 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004936 // The first two checks are made also made by isShown() which
4937 // however traverses the tree up to the parent to catch that.
4938 // Therefore, we do some fail fast check to minimize the up
4939 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004940 boolean isVisible = mAttachInfo != null
4941 && mAttachInfo.mWindowVisibility == View.VISIBLE
4942 && getAlpha() > 0
4943 && isShown()
4944 && getGlobalVisibleRect(visibleRect, offset);
4945 if (isVisible && boundInView != null) {
4946 visibleRect.offset(-offset.x, -offset.y);
4947 isVisible &= boundInView.intersect(visibleRect);
4948 }
4949 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004950 }
4951
4952 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004953 * Sets a delegate for implementing accessibility support via compositon as
4954 * opposed to inheritance. The delegate's primary use is for implementing
4955 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4956 *
4957 * @param delegate The delegate instance.
4958 *
4959 * @see AccessibilityDelegate
4960 */
4961 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4962 mAccessibilityDelegate = delegate;
4963 }
4964
4965 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004966 * Gets the provider for managing a virtual view hierarchy rooted at this View
4967 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4968 * that explore the window content.
4969 * <p>
4970 * If this method returns an instance, this instance is responsible for managing
4971 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4972 * View including the one representing the View itself. Similarly the returned
4973 * instance is responsible for performing accessibility actions on any virtual
4974 * view or the root view itself.
4975 * </p>
4976 * <p>
4977 * If an {@link AccessibilityDelegate} has been specified via calling
4978 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4979 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4980 * is responsible for handling this call.
4981 * </p>
4982 *
4983 * @return The provider.
4984 *
4985 * @see AccessibilityNodeProvider
4986 */
4987 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4988 if (mAccessibilityDelegate != null) {
4989 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4990 } else {
4991 return null;
4992 }
4993 }
4994
4995 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004996 * Gets the unique identifier of this view on the screen for accessibility purposes.
4997 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4998 *
4999 * @return The view accessibility id.
5000 *
5001 * @hide
5002 */
5003 public int getAccessibilityViewId() {
5004 if (mAccessibilityViewId == NO_ID) {
5005 mAccessibilityViewId = sNextAccessibilityViewId++;
5006 }
5007 return mAccessibilityViewId;
5008 }
5009
5010 /**
5011 * Gets the unique identifier of the window in which this View reseides.
5012 *
5013 * @return The window accessibility id.
5014 *
5015 * @hide
5016 */
5017 public int getAccessibilityWindowId() {
5018 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5019 }
5020
5021 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005022 * Gets the {@link View} description. It briefly describes the view and is
5023 * primarily used for accessibility support. Set this property to enable
5024 * better accessibility support for your application. This is especially
5025 * true for views that do not have textual representation (For example,
5026 * ImageButton).
5027 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005028 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005029 *
5030 * @attr ref android.R.styleable#View_contentDescription
5031 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005032 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005033 public CharSequence getContentDescription() {
5034 return mContentDescription;
5035 }
5036
5037 /**
5038 * Sets the {@link View} description. It briefly describes the view and is
5039 * primarily used for accessibility support. Set this property to enable
5040 * better accessibility support for your application. This is especially
5041 * true for views that do not have textual representation (For example,
5042 * ImageButton).
5043 *
5044 * @param contentDescription The content description.
5045 *
5046 * @attr ref android.R.styleable#View_contentDescription
5047 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005048 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005049 public void setContentDescription(CharSequence contentDescription) {
5050 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005051 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5052 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5053 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5054 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005055 }
5056
5057 /**
Romain Guya2431d02009-04-30 16:30:00 -07005058 * Invoked whenever this view loses focus, either by losing window focus or by losing
5059 * focus within its window. This method can be used to clear any state tied to the
5060 * focus. For instance, if a button is held pressed with the trackball and the window
5061 * loses focus, this method can be used to cancel the press.
5062 *
5063 * Subclasses of View overriding this method should always call super.onFocusLost().
5064 *
5065 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005066 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005067 *
5068 * @hide pending API council approval
5069 */
5070 protected void onFocusLost() {
5071 resetPressedState();
5072 }
5073
5074 private void resetPressedState() {
5075 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5076 return;
5077 }
5078
5079 if (isPressed()) {
5080 setPressed(false);
5081
5082 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005083 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005084 }
5085 }
5086 }
5087
5088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 * Returns true if this view has focus
5090 *
5091 * @return True if this view has focus, false otherwise.
5092 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005093 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 public boolean isFocused() {
5095 return (mPrivateFlags & FOCUSED) != 0;
5096 }
5097
5098 /**
5099 * Find the view in the hierarchy rooted at this view that currently has
5100 * focus.
5101 *
5102 * @return The view that currently has focus, or null if no focused view can
5103 * be found.
5104 */
5105 public View findFocus() {
5106 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5107 }
5108
5109 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005110 * Indicates whether this view is one of the set of scrollable containers in
5111 * its window.
5112 *
5113 * @return whether this view is one of the set of scrollable containers in
5114 * its window
5115 *
5116 * @attr ref android.R.styleable#View_isScrollContainer
5117 */
5118 public boolean isScrollContainer() {
5119 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5120 }
5121
5122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 * Change whether this view is one of the set of scrollable containers in
5124 * its window. This will be used to determine whether the window can
5125 * resize or must pan when a soft input area is open -- scrollable
5126 * containers allow the window to use resize mode since the container
5127 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005128 *
5129 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 */
5131 public void setScrollContainer(boolean isScrollContainer) {
5132 if (isScrollContainer) {
5133 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5134 mAttachInfo.mScrollContainers.add(this);
5135 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5136 }
5137 mPrivateFlags |= SCROLL_CONTAINER;
5138 } else {
5139 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5140 mAttachInfo.mScrollContainers.remove(this);
5141 }
5142 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5143 }
5144 }
5145
5146 /**
5147 * Returns the quality of the drawing cache.
5148 *
5149 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5150 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5151 *
5152 * @see #setDrawingCacheQuality(int)
5153 * @see #setDrawingCacheEnabled(boolean)
5154 * @see #isDrawingCacheEnabled()
5155 *
5156 * @attr ref android.R.styleable#View_drawingCacheQuality
5157 */
5158 public int getDrawingCacheQuality() {
5159 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5160 }
5161
5162 /**
5163 * Set the drawing cache quality of this view. This value is used only when the
5164 * drawing cache is enabled
5165 *
5166 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5167 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5168 *
5169 * @see #getDrawingCacheQuality()
5170 * @see #setDrawingCacheEnabled(boolean)
5171 * @see #isDrawingCacheEnabled()
5172 *
5173 * @attr ref android.R.styleable#View_drawingCacheQuality
5174 */
5175 public void setDrawingCacheQuality(int quality) {
5176 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5177 }
5178
5179 /**
5180 * Returns whether the screen should remain on, corresponding to the current
5181 * value of {@link #KEEP_SCREEN_ON}.
5182 *
5183 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5184 *
5185 * @see #setKeepScreenOn(boolean)
5186 *
5187 * @attr ref android.R.styleable#View_keepScreenOn
5188 */
5189 public boolean getKeepScreenOn() {
5190 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5191 }
5192
5193 /**
5194 * Controls whether the screen should remain on, modifying the
5195 * value of {@link #KEEP_SCREEN_ON}.
5196 *
5197 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5198 *
5199 * @see #getKeepScreenOn()
5200 *
5201 * @attr ref android.R.styleable#View_keepScreenOn
5202 */
5203 public void setKeepScreenOn(boolean keepScreenOn) {
5204 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5205 }
5206
5207 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005208 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5209 * @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 -08005210 *
5211 * @attr ref android.R.styleable#View_nextFocusLeft
5212 */
5213 public int getNextFocusLeftId() {
5214 return mNextFocusLeftId;
5215 }
5216
5217 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005218 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5219 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5220 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 *
5222 * @attr ref android.R.styleable#View_nextFocusLeft
5223 */
5224 public void setNextFocusLeftId(int nextFocusLeftId) {
5225 mNextFocusLeftId = nextFocusLeftId;
5226 }
5227
5228 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005229 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5230 * @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 -08005231 *
5232 * @attr ref android.R.styleable#View_nextFocusRight
5233 */
5234 public int getNextFocusRightId() {
5235 return mNextFocusRightId;
5236 }
5237
5238 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005239 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5240 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5241 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 *
5243 * @attr ref android.R.styleable#View_nextFocusRight
5244 */
5245 public void setNextFocusRightId(int nextFocusRightId) {
5246 mNextFocusRightId = nextFocusRightId;
5247 }
5248
5249 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005250 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5251 * @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 -08005252 *
5253 * @attr ref android.R.styleable#View_nextFocusUp
5254 */
5255 public int getNextFocusUpId() {
5256 return mNextFocusUpId;
5257 }
5258
5259 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005260 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5261 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5262 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 *
5264 * @attr ref android.R.styleable#View_nextFocusUp
5265 */
5266 public void setNextFocusUpId(int nextFocusUpId) {
5267 mNextFocusUpId = nextFocusUpId;
5268 }
5269
5270 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005271 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5272 * @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 -08005273 *
5274 * @attr ref android.R.styleable#View_nextFocusDown
5275 */
5276 public int getNextFocusDownId() {
5277 return mNextFocusDownId;
5278 }
5279
5280 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005281 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5282 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5283 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 *
5285 * @attr ref android.R.styleable#View_nextFocusDown
5286 */
5287 public void setNextFocusDownId(int nextFocusDownId) {
5288 mNextFocusDownId = nextFocusDownId;
5289 }
5290
5291 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005292 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5293 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5294 *
5295 * @attr ref android.R.styleable#View_nextFocusForward
5296 */
5297 public int getNextFocusForwardId() {
5298 return mNextFocusForwardId;
5299 }
5300
5301 /**
5302 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5303 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5304 * decide automatically.
5305 *
5306 * @attr ref android.R.styleable#View_nextFocusForward
5307 */
5308 public void setNextFocusForwardId(int nextFocusForwardId) {
5309 mNextFocusForwardId = nextFocusForwardId;
5310 }
5311
5312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 * Returns the visibility of this view and all of its ancestors
5314 *
5315 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5316 */
5317 public boolean isShown() {
5318 View current = this;
5319 //noinspection ConstantConditions
5320 do {
5321 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5322 return false;
5323 }
5324 ViewParent parent = current.mParent;
5325 if (parent == null) {
5326 return false; // We are not attached to the view root
5327 }
5328 if (!(parent instanceof View)) {
5329 return true;
5330 }
5331 current = (View) parent;
5332 } while (current != null);
5333
5334 return false;
5335 }
5336
5337 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005338 * Called by the view hierarchy when the content insets for a window have
5339 * changed, to allow it to adjust its content to fit within those windows.
5340 * The content insets tell you the space that the status bar, input method,
5341 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005343 * <p>You do not normally need to deal with this function, since the default
5344 * window decoration given to applications takes care of applying it to the
5345 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5346 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5347 * and your content can be placed under those system elements. You can then
5348 * use this method within your view hierarchy if you have parts of your UI
5349 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005351 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005352 * inset's to the view's padding, consuming that content (modifying the
5353 * insets to be 0), and returning true. This behavior is off by default, but can
5354 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5355 *
5356 * <p>This function's traversal down the hierarchy is depth-first. The same content
5357 * insets object is propagated down the hierarchy, so any changes made to it will
5358 * be seen by all following views (including potentially ones above in
5359 * the hierarchy since this is a depth-first traversal). The first view
5360 * that returns true will abort the entire traversal.
5361 *
5362 * <p>The default implementation works well for a situation where it is
5363 * used with a container that covers the entire window, allowing it to
5364 * apply the appropriate insets to its content on all edges. If you need
5365 * a more complicated layout (such as two different views fitting system
5366 * windows, one on the top of the window, and one on the bottom),
5367 * you can override the method and handle the insets however you would like.
5368 * Note that the insets provided by the framework are always relative to the
5369 * far edges of the window, not accounting for the location of the called view
5370 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005371 * where the layout will place the view, as it is done before layout happens.)
5372 *
5373 * <p>Note: unlike many View methods, there is no dispatch phase to this
5374 * call. If you are overriding it in a ViewGroup and want to allow the
5375 * call to continue to your children, you must be sure to call the super
5376 * implementation.
5377 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005378 * <p>Here is a sample layout that makes use of fitting system windows
5379 * to have controls for a video view placed inside of the window decorations
5380 * that it hides and shows. This can be used with code like the second
5381 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5382 *
5383 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5384 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005385 * @param insets Current content insets of the window. Prior to
5386 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5387 * the insets or else you and Android will be unhappy.
5388 *
5389 * @return Return true if this view applied the insets and it should not
5390 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005391 * @see #getFitsSystemWindows()
5392 * @see #setFitsSystemWindows()
5393 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 */
5395 protected boolean fitSystemWindows(Rect insets) {
5396 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005397 mUserPaddingStart = -1;
5398 mUserPaddingEnd = -1;
5399 mUserPaddingRelative = false;
5400 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5401 || mAttachInfo == null
5402 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5403 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5404 return true;
5405 } else {
5406 internalSetPadding(0, 0, 0, 0);
5407 return false;
5408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 }
5410 return false;
5411 }
5412
5413 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005414 * Sets whether or not this view should account for system screen decorations
5415 * such as the status bar and inset its content; that is, controlling whether
5416 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5417 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005418 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005419 * <p>Note that if you are providing your own implementation of
5420 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5421 * flag to true -- your implementation will be overriding the default
5422 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005423 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005424 * @param fitSystemWindows If true, then the default implementation of
5425 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005426 *
5427 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005428 * @see #getFitsSystemWindows()
5429 * @see #fitSystemWindows(Rect)
5430 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005431 */
5432 public void setFitsSystemWindows(boolean fitSystemWindows) {
5433 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5434 }
5435
5436 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005437 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005438 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5439 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005440 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005441 * @return Returns true if the default implementation of
5442 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005443 *
5444 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005445 * @see #setFitsSystemWindows()
5446 * @see #fitSystemWindows(Rect)
5447 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005448 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005449 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005450 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5451 }
5452
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005453 /** @hide */
5454 public boolean fitsSystemWindows() {
5455 return getFitsSystemWindows();
5456 }
5457
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005458 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005459 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5460 */
5461 public void requestFitSystemWindows() {
5462 if (mParent != null) {
5463 mParent.requestFitSystemWindows();
5464 }
5465 }
5466
5467 /**
5468 * For use by PhoneWindow to make its own system window fitting optional.
5469 * @hide
5470 */
5471 public void makeOptionalFitsSystemWindows() {
5472 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5473 }
5474
5475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 * Returns the visibility status for this view.
5477 *
5478 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5479 * @attr ref android.R.styleable#View_visibility
5480 */
5481 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005482 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5483 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5484 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 })
5486 public int getVisibility() {
5487 return mViewFlags & VISIBILITY_MASK;
5488 }
5489
5490 /**
5491 * Set the enabled state of this view.
5492 *
5493 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5494 * @attr ref android.R.styleable#View_visibility
5495 */
5496 @RemotableViewMethod
5497 public void setVisibility(int visibility) {
5498 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005499 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 }
5501
5502 /**
5503 * Returns the enabled status for this view. The interpretation of the
5504 * enabled state varies by subclass.
5505 *
5506 * @return True if this view is enabled, false otherwise.
5507 */
5508 @ViewDebug.ExportedProperty
5509 public boolean isEnabled() {
5510 return (mViewFlags & ENABLED_MASK) == ENABLED;
5511 }
5512
5513 /**
5514 * Set the enabled state of this view. The interpretation of the enabled
5515 * state varies by subclass.
5516 *
5517 * @param enabled True if this view is enabled, false otherwise.
5518 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005519 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005521 if (enabled == isEnabled()) return;
5522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5524
5525 /*
5526 * The View most likely has to change its appearance, so refresh
5527 * the drawable state.
5528 */
5529 refreshDrawableState();
5530
5531 // Invalidate too, since the default behavior for views is to be
5532 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005533 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 }
5535
5536 /**
5537 * Set whether this view can receive the focus.
5538 *
5539 * Setting this to false will also ensure that this view is not focusable
5540 * in touch mode.
5541 *
5542 * @param focusable If true, this view can receive the focus.
5543 *
5544 * @see #setFocusableInTouchMode(boolean)
5545 * @attr ref android.R.styleable#View_focusable
5546 */
5547 public void setFocusable(boolean focusable) {
5548 if (!focusable) {
5549 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5550 }
5551 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5552 }
5553
5554 /**
5555 * Set whether this view can receive focus while in touch mode.
5556 *
5557 * Setting this to true will also ensure that this view is focusable.
5558 *
5559 * @param focusableInTouchMode If true, this view can receive the focus while
5560 * in touch mode.
5561 *
5562 * @see #setFocusable(boolean)
5563 * @attr ref android.R.styleable#View_focusableInTouchMode
5564 */
5565 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5566 // Focusable in touch mode should always be set before the focusable flag
5567 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5568 // which, in touch mode, will not successfully request focus on this view
5569 // because the focusable in touch mode flag is not set
5570 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5571 if (focusableInTouchMode) {
5572 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5573 }
5574 }
5575
5576 /**
5577 * Set whether this view should have sound effects enabled for events such as
5578 * clicking and touching.
5579 *
5580 * <p>You may wish to disable sound effects for a view if you already play sounds,
5581 * for instance, a dial key that plays dtmf tones.
5582 *
5583 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5584 * @see #isSoundEffectsEnabled()
5585 * @see #playSoundEffect(int)
5586 * @attr ref android.R.styleable#View_soundEffectsEnabled
5587 */
5588 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5589 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5590 }
5591
5592 /**
5593 * @return whether this view should have sound effects enabled for events such as
5594 * clicking and touching.
5595 *
5596 * @see #setSoundEffectsEnabled(boolean)
5597 * @see #playSoundEffect(int)
5598 * @attr ref android.R.styleable#View_soundEffectsEnabled
5599 */
5600 @ViewDebug.ExportedProperty
5601 public boolean isSoundEffectsEnabled() {
5602 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5603 }
5604
5605 /**
5606 * Set whether this view should have haptic feedback for events such as
5607 * long presses.
5608 *
5609 * <p>You may wish to disable haptic feedback if your view already controls
5610 * its own haptic feedback.
5611 *
5612 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5613 * @see #isHapticFeedbackEnabled()
5614 * @see #performHapticFeedback(int)
5615 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5616 */
5617 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5618 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5619 }
5620
5621 /**
5622 * @return whether this view should have haptic feedback enabled for events
5623 * long presses.
5624 *
5625 * @see #setHapticFeedbackEnabled(boolean)
5626 * @see #performHapticFeedback(int)
5627 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5628 */
5629 @ViewDebug.ExportedProperty
5630 public boolean isHapticFeedbackEnabled() {
5631 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5632 }
5633
5634 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005635 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005636 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005637 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5638 * {@link #LAYOUT_DIRECTION_RTL},
5639 * {@link #LAYOUT_DIRECTION_INHERIT} or
5640 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005641 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005642 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005643 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005644 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005645 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005646 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5647 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5648 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5649 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005650 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005651 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005652 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005653 }
5654
5655 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005656 * Set the layout direction for this view. This will propagate a reset of layout direction
5657 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005658 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005659 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5660 * {@link #LAYOUT_DIRECTION_RTL},
5661 * {@link #LAYOUT_DIRECTION_INHERIT} or
5662 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005663 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005664 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005665 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005666 */
5667 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005668 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005669 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005670 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005671 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005672 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005673 // Set the new layout direction (filtered) and ask for a layout pass
5674 mPrivateFlags2 |=
5675 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5676 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005677 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005678 }
5679
5680 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005681 * Returns the resolved layout direction for this view.
5682 *
5683 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005684 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005685 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005686 */
5687 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005688 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5689 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005690 })
5691 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005692 // The layout diretion will be resolved only if needed
5693 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5694 resolveLayoutDirection();
5695 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005696 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005697 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5698 }
5699
5700 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005701 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5702 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005703 *
5704 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005705 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005706 */
5707 @ViewDebug.ExportedProperty(category = "layout")
5708 public boolean isLayoutRtl() {
5709 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5710 }
5711
5712 /**
Adam Powell539ee872012-02-03 19:00:49 -08005713 * Indicates whether the view is currently tracking transient state that the
5714 * app should not need to concern itself with saving and restoring, but that
5715 * the framework should take special note to preserve when possible.
5716 *
Adam Powell785c4472012-05-02 21:25:39 -07005717 * <p>A view with transient state cannot be trivially rebound from an external
5718 * data source, such as an adapter binding item views in a list. This may be
5719 * because the view is performing an animation, tracking user selection
5720 * of content, or similar.</p>
5721 *
Adam Powell539ee872012-02-03 19:00:49 -08005722 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005723 */
5724 @ViewDebug.ExportedProperty(category = "layout")
5725 public boolean hasTransientState() {
5726 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5727 }
5728
5729 /**
5730 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005731 * framework should attempt to preserve when possible. This flag is reference counted,
5732 * so every call to setHasTransientState(true) should be paired with a later call
5733 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005734 *
Adam Powell785c4472012-05-02 21:25:39 -07005735 * <p>A view with transient state cannot be trivially rebound from an external
5736 * data source, such as an adapter binding item views in a list. This may be
5737 * because the view is performing an animation, tracking user selection
5738 * of content, or similar.</p>
5739 *
Adam Powell539ee872012-02-03 19:00:49 -08005740 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005741 */
5742 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005743 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5744 mTransientStateCount - 1;
5745 if (mTransientStateCount < 0) {
5746 mTransientStateCount = 0;
5747 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5748 "unmatched pair of setHasTransientState calls");
5749 }
5750 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005751 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005752 // update flag if we've just incremented up from 0 or decremented down to 0
5753 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5754 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5755 if (mParent != null) {
5756 try {
5757 mParent.childHasTransientStateChanged(this, hasTransientState);
5758 } catch (AbstractMethodError e) {
5759 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5760 " does not fully implement ViewParent", e);
5761 }
Adam Powell539ee872012-02-03 19:00:49 -08005762 }
5763 }
5764 }
5765
5766 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 * If this view doesn't do any drawing on its own, set this flag to
5768 * allow further optimizations. By default, this flag is not set on
5769 * View, but could be set on some View subclasses such as ViewGroup.
5770 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005771 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5772 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 *
5774 * @param willNotDraw whether or not this View draw on its own
5775 */
5776 public void setWillNotDraw(boolean willNotDraw) {
5777 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5778 }
5779
5780 /**
5781 * Returns whether or not this View draws on its own.
5782 *
5783 * @return true if this view has nothing to draw, false otherwise
5784 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005785 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 public boolean willNotDraw() {
5787 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5788 }
5789
5790 /**
5791 * When a View's drawing cache is enabled, drawing is redirected to an
5792 * offscreen bitmap. Some views, like an ImageView, must be able to
5793 * bypass this mechanism if they already draw a single bitmap, to avoid
5794 * unnecessary usage of the memory.
5795 *
5796 * @param willNotCacheDrawing true if this view does not cache its
5797 * drawing, false otherwise
5798 */
5799 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5800 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5801 }
5802
5803 /**
5804 * Returns whether or not this View can cache its drawing or not.
5805 *
5806 * @return true if this view does not cache its drawing, false otherwise
5807 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005808 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 public boolean willNotCacheDrawing() {
5810 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5811 }
5812
5813 /**
5814 * Indicates whether this view reacts to click events or not.
5815 *
5816 * @return true if the view is clickable, false otherwise
5817 *
5818 * @see #setClickable(boolean)
5819 * @attr ref android.R.styleable#View_clickable
5820 */
5821 @ViewDebug.ExportedProperty
5822 public boolean isClickable() {
5823 return (mViewFlags & CLICKABLE) == CLICKABLE;
5824 }
5825
5826 /**
5827 * Enables or disables click events for this view. When a view
5828 * is clickable it will change its state to "pressed" on every click.
5829 * Subclasses should set the view clickable to visually react to
5830 * user's clicks.
5831 *
5832 * @param clickable true to make the view clickable, false otherwise
5833 *
5834 * @see #isClickable()
5835 * @attr ref android.R.styleable#View_clickable
5836 */
5837 public void setClickable(boolean clickable) {
5838 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5839 }
5840
5841 /**
5842 * Indicates whether this view reacts to long click events or not.
5843 *
5844 * @return true if the view is long clickable, false otherwise
5845 *
5846 * @see #setLongClickable(boolean)
5847 * @attr ref android.R.styleable#View_longClickable
5848 */
5849 public boolean isLongClickable() {
5850 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5851 }
5852
5853 /**
5854 * Enables or disables long click events for this view. When a view is long
5855 * clickable it reacts to the user holding down the button for a longer
5856 * duration than a tap. This event can either launch the listener or a
5857 * context menu.
5858 *
5859 * @param longClickable true to make the view long clickable, false otherwise
5860 * @see #isLongClickable()
5861 * @attr ref android.R.styleable#View_longClickable
5862 */
5863 public void setLongClickable(boolean longClickable) {
5864 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5865 }
5866
5867 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005868 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 *
5870 * @see #isClickable()
5871 * @see #setClickable(boolean)
5872 *
5873 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5874 * the View's internal state from a previously set "pressed" state.
5875 */
5876 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005877 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 if (pressed) {
5880 mPrivateFlags |= PRESSED;
5881 } else {
5882 mPrivateFlags &= ~PRESSED;
5883 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005884
5885 if (needsRefresh) {
5886 refreshDrawableState();
5887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 dispatchSetPressed(pressed);
5889 }
5890
5891 /**
5892 * Dispatch setPressed to all of this View's children.
5893 *
5894 * @see #setPressed(boolean)
5895 *
5896 * @param pressed The new pressed state
5897 */
5898 protected void dispatchSetPressed(boolean pressed) {
5899 }
5900
5901 /**
5902 * Indicates whether the view is currently in pressed state. Unless
5903 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5904 * the pressed state.
5905 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005906 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 * @see #isClickable()
5908 * @see #setClickable(boolean)
5909 *
5910 * @return true if the view is currently pressed, false otherwise
5911 */
5912 public boolean isPressed() {
5913 return (mPrivateFlags & PRESSED) == PRESSED;
5914 }
5915
5916 /**
5917 * Indicates whether this view will save its state (that is,
5918 * whether its {@link #onSaveInstanceState} method will be called).
5919 *
5920 * @return Returns true if the view state saving is enabled, else false.
5921 *
5922 * @see #setSaveEnabled(boolean)
5923 * @attr ref android.R.styleable#View_saveEnabled
5924 */
5925 public boolean isSaveEnabled() {
5926 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5927 }
5928
5929 /**
5930 * Controls whether the saving of this view's state is
5931 * enabled (that is, whether its {@link #onSaveInstanceState} method
5932 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005933 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 * for its state to be saved. This flag can only disable the
5935 * saving of this view; any child views may still have their state saved.
5936 *
5937 * @param enabled Set to false to <em>disable</em> state saving, or true
5938 * (the default) to allow it.
5939 *
5940 * @see #isSaveEnabled()
5941 * @see #setId(int)
5942 * @see #onSaveInstanceState()
5943 * @attr ref android.R.styleable#View_saveEnabled
5944 */
5945 public void setSaveEnabled(boolean enabled) {
5946 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5947 }
5948
Jeff Brown85a31762010-09-01 17:01:00 -07005949 /**
5950 * Gets whether the framework should discard touches when the view's
5951 * window is obscured by another visible window.
5952 * Refer to the {@link View} security documentation for more details.
5953 *
5954 * @return True if touch filtering is enabled.
5955 *
5956 * @see #setFilterTouchesWhenObscured(boolean)
5957 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5958 */
5959 @ViewDebug.ExportedProperty
5960 public boolean getFilterTouchesWhenObscured() {
5961 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5962 }
5963
5964 /**
5965 * Sets whether the framework should discard touches when the view's
5966 * window is obscured by another visible window.
5967 * Refer to the {@link View} security documentation for more details.
5968 *
5969 * @param enabled True if touch filtering should be enabled.
5970 *
5971 * @see #getFilterTouchesWhenObscured
5972 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5973 */
5974 public void setFilterTouchesWhenObscured(boolean enabled) {
5975 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5976 FILTER_TOUCHES_WHEN_OBSCURED);
5977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978
5979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005980 * Indicates whether the entire hierarchy under this view will save its
5981 * state when a state saving traversal occurs from its parent. The default
5982 * is true; if false, these views will not be saved unless
5983 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5984 *
5985 * @return Returns true if the view state saving from parent is enabled, else false.
5986 *
5987 * @see #setSaveFromParentEnabled(boolean)
5988 */
5989 public boolean isSaveFromParentEnabled() {
5990 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5991 }
5992
5993 /**
5994 * Controls whether the entire hierarchy under this view will save its
5995 * state when a state saving traversal occurs from its parent. The default
5996 * is true; if false, these views will not be saved unless
5997 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5998 *
5999 * @param enabled Set to false to <em>disable</em> state saving, or true
6000 * (the default) to allow it.
6001 *
6002 * @see #isSaveFromParentEnabled()
6003 * @see #setId(int)
6004 * @see #onSaveInstanceState()
6005 */
6006 public void setSaveFromParentEnabled(boolean enabled) {
6007 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6008 }
6009
6010
6011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 * Returns whether this View is able to take focus.
6013 *
6014 * @return True if this view can take focus, or false otherwise.
6015 * @attr ref android.R.styleable#View_focusable
6016 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006017 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 public final boolean isFocusable() {
6019 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6020 }
6021
6022 /**
6023 * When a view is focusable, it may not want to take focus when in touch mode.
6024 * For example, a button would like focus when the user is navigating via a D-pad
6025 * so that the user can click on it, but once the user starts touching the screen,
6026 * the button shouldn't take focus
6027 * @return Whether the view is focusable in touch mode.
6028 * @attr ref android.R.styleable#View_focusableInTouchMode
6029 */
6030 @ViewDebug.ExportedProperty
6031 public final boolean isFocusableInTouchMode() {
6032 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6033 }
6034
6035 /**
6036 * Find the nearest view in the specified direction that can take focus.
6037 * This does not actually give focus to that view.
6038 *
6039 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6040 *
6041 * @return The nearest focusable in the specified direction, or null if none
6042 * can be found.
6043 */
6044 public View focusSearch(int direction) {
6045 if (mParent != null) {
6046 return mParent.focusSearch(this, direction);
6047 } else {
6048 return null;
6049 }
6050 }
6051
6052 /**
6053 * This method is the last chance for the focused view and its ancestors to
6054 * respond to an arrow key. This is called when the focused view did not
6055 * consume the key internally, nor could the view system find a new view in
6056 * the requested direction to give focus to.
6057 *
6058 * @param focused The currently focused view.
6059 * @param direction The direction focus wants to move. One of FOCUS_UP,
6060 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6061 * @return True if the this view consumed this unhandled move.
6062 */
6063 public boolean dispatchUnhandledMove(View focused, int direction) {
6064 return false;
6065 }
6066
6067 /**
6068 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006069 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006071 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6072 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 * @return The user specified next view, or null if there is none.
6074 */
6075 View findUserSetNextFocus(View root, int direction) {
6076 switch (direction) {
6077 case FOCUS_LEFT:
6078 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006079 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 case FOCUS_RIGHT:
6081 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006082 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 case FOCUS_UP:
6084 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006085 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 case FOCUS_DOWN:
6087 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006088 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006089 case FOCUS_FORWARD:
6090 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006091 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006092 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006093 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006094 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006095 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006096 @Override
6097 public boolean apply(View t) {
6098 return t.mNextFocusForwardId == id;
6099 }
6100 });
6101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 }
6103 return null;
6104 }
6105
Jeff Brown4dfbec22011-08-15 14:55:37 -07006106 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6107 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6108 @Override
6109 public boolean apply(View t) {
6110 return t.mID == childViewId;
6111 }
6112 });
6113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 if (result == null) {
6115 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6116 + "by user for id " + childViewId);
6117 }
6118 return result;
6119 }
6120
6121 /**
6122 * Find and return all focusable views that are descendants of this view,
6123 * possibly including this view if it is focusable itself.
6124 *
6125 * @param direction The direction of the focus
6126 * @return A list of focusable views
6127 */
6128 public ArrayList<View> getFocusables(int direction) {
6129 ArrayList<View> result = new ArrayList<View>(24);
6130 addFocusables(result, direction);
6131 return result;
6132 }
6133
6134 /**
6135 * Add any focusable views that are descendants of this view (possibly
6136 * including this view if it is focusable itself) to views. If we are in touch mode,
6137 * only add views that are also focusable in touch mode.
6138 *
6139 * @param views Focusable views found so far
6140 * @param direction The direction of the focus
6141 */
6142 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006143 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145
svetoslavganov75986cf2009-05-14 22:28:01 -07006146 /**
6147 * Adds any focusable views that are descendants of this view (possibly
6148 * including this view if it is focusable itself) to views. This method
6149 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006150 * only views focusable in touch mode if we are in touch mode or
6151 * only views that can take accessibility focus if accessibility is enabeld
6152 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006153 *
6154 * @param views Focusable views found so far or null if all we are interested is
6155 * the number of focusables.
6156 * @param direction The direction of the focus.
6157 * @param focusableMode The type of focusables to be added.
6158 *
6159 * @see #FOCUSABLES_ALL
6160 * @see #FOCUSABLES_TOUCH_MODE
6161 */
6162 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006163 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006164 return;
6165 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006166 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006167 if (isAccessibilityFocusable()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006168 views.add(this);
6169 return;
6170 }
6171 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006172 if (!isFocusable()) {
6173 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006174 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006175 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6176 && isInTouchMode() && !isFocusableInTouchMode()) {
6177 return;
6178 }
6179 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 }
6181
6182 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006183 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006184 * The search is performed by either the text that the View renders or the content
6185 * description that describes the view for accessibility purposes and the view does
6186 * not render or both. Clients can specify how the search is to be performed via
6187 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6188 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006189 *
6190 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006191 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006192 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006193 * @see #FIND_VIEWS_WITH_TEXT
6194 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6195 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006196 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006197 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006198 if (getAccessibilityNodeProvider() != null) {
6199 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6200 outViews.add(this);
6201 }
6202 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006203 && (searched != null && searched.length() > 0)
6204 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006205 String searchedLowerCase = searched.toString().toLowerCase();
6206 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6207 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6208 outViews.add(this);
6209 }
6210 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006211 }
6212
6213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 * Find and return all touchable views that are descendants of this view,
6215 * possibly including this view if it is touchable itself.
6216 *
6217 * @return A list of touchable views
6218 */
6219 public ArrayList<View> getTouchables() {
6220 ArrayList<View> result = new ArrayList<View>();
6221 addTouchables(result);
6222 return result;
6223 }
6224
6225 /**
6226 * Add any touchable views that are descendants of this view (possibly
6227 * including this view if it is touchable itself) to views.
6228 *
6229 * @param views Touchable views found so far
6230 */
6231 public void addTouchables(ArrayList<View> views) {
6232 final int viewFlags = mViewFlags;
6233
6234 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6235 && (viewFlags & ENABLED_MASK) == ENABLED) {
6236 views.add(this);
6237 }
6238 }
6239
6240 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006241 * Returns whether this View is accessibility focused.
6242 *
6243 * @return True if this View is accessibility focused.
6244 */
6245 boolean isAccessibilityFocused() {
6246 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6247 }
6248
6249 /**
6250 * Call this to try to give accessibility focus to this view.
6251 *
6252 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6253 * returns false or the view is no visible or the view already has accessibility
6254 * focus.
6255 *
6256 * See also {@link #focusSearch(int)}, which is what you call to say that you
6257 * have focus, and you want your parent to look for the next one.
6258 *
6259 * @return Whether this view actually took accessibility focus.
6260 *
6261 * @hide
6262 */
6263 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006264 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6265 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006266 return false;
6267 }
6268 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6269 return false;
6270 }
6271 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6272 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6273 ViewRootImpl viewRootImpl = getViewRootImpl();
6274 if (viewRootImpl != null) {
6275 viewRootImpl.setAccessibilityFocusedHost(this);
6276 }
6277 invalidate();
6278 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6279 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006280 return true;
6281 }
6282 return false;
6283 }
6284
6285 /**
6286 * Call this to try to clear accessibility focus of this view.
6287 *
6288 * See also {@link #focusSearch(int)}, which is what you call to say that you
6289 * have focus, and you want your parent to look for the next one.
6290 *
6291 * @hide
6292 */
6293 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006294 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6295 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006296 invalidate();
6297 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6298 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006299 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006300 // Clear the global reference of accessibility focus if this
6301 // view or any of its descendants had accessibility focus.
6302 ViewRootImpl viewRootImpl = getViewRootImpl();
6303 if (viewRootImpl != null) {
6304 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6305 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6306 viewRootImpl.setAccessibilityFocusedHost(null);
6307 }
6308 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006309 }
6310
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006311 private void requestAccessibilityFocusFromHover() {
6312 if (includeForAccessibility() && isActionableForAccessibility()) {
6313 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006314 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006315 } else {
6316 if (mParent != null) {
6317 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6318 if (nextFocus != null) {
6319 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006320 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006321 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006322 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006323 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006324 }
6325
Svetoslav Ganova90e4512012-06-01 19:02:32 -07006326 private boolean canTakeAccessibilityFocusFromHover() {
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006327 if (includeForAccessibility() && isActionableForAccessibility()) {
6328 return true;
6329 }
6330 if (mParent != null) {
6331 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006332 }
6333 return false;
6334 }
6335
6336 /**
6337 * Clears accessibility focus without calling any callback methods
6338 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6339 * is used for clearing accessibility focus when giving this focus to
6340 * another view.
6341 */
6342 void clearAccessibilityFocusNoCallbacks() {
6343 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6344 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6345 invalidate();
6346 }
6347 }
6348
6349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 * Call this to try to give focus to a specific view or to one of its
6351 * descendants.
6352 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006353 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6354 * false), or if it is focusable and it is not focusable in touch mode
6355 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006357 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 * have focus, and you want your parent to look for the next one.
6359 *
6360 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6361 * {@link #FOCUS_DOWN} and <code>null</code>.
6362 *
6363 * @return Whether this view or one of its descendants actually took focus.
6364 */
6365 public final boolean requestFocus() {
6366 return requestFocus(View.FOCUS_DOWN);
6367 }
6368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 /**
6370 * Call this to try to give focus to a specific view or to one of its
6371 * descendants and give it a hint about what direction focus is heading.
6372 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006373 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6374 * false), or if it is focusable and it is not focusable in touch mode
6375 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006377 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 * have focus, and you want your parent to look for the next one.
6379 *
6380 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6381 * <code>null</code> set for the previously focused rectangle.
6382 *
6383 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6384 * @return Whether this view or one of its descendants actually took focus.
6385 */
6386 public final boolean requestFocus(int direction) {
6387 return requestFocus(direction, null);
6388 }
6389
6390 /**
6391 * Call this to try to give focus to a specific view or to one of its descendants
6392 * and give it hints about the direction and a specific rectangle that the focus
6393 * is coming from. The rectangle can help give larger views a finer grained hint
6394 * about where focus is coming from, and therefore, where to show selection, or
6395 * forward focus change internally.
6396 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006397 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6398 * false), or if it is focusable and it is not focusable in touch mode
6399 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 *
6401 * A View will not take focus if it is not visible.
6402 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006403 * A View will not take focus if one of its parents has
6404 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6405 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006407 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 * have focus, and you want your parent to look for the next one.
6409 *
6410 * You may wish to override this method if your custom {@link View} has an internal
6411 * {@link View} that it wishes to forward the request to.
6412 *
6413 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6414 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6415 * to give a finer grained hint about where focus is coming from. May be null
6416 * if there is no hint.
6417 * @return Whether this view or one of its descendants actually took focus.
6418 */
6419 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006420 return requestFocusNoSearch(direction, previouslyFocusedRect);
6421 }
6422
6423 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 // need to be focusable
6425 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6426 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6427 return false;
6428 }
6429
6430 // need to be focusable in touch mode if in touch mode
6431 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006432 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6433 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 }
6435
6436 // need to not have any parents blocking us
6437 if (hasAncestorThatBlocksDescendantFocus()) {
6438 return false;
6439 }
6440
6441 handleFocusGainInternal(direction, previouslyFocusedRect);
6442 return true;
6443 }
6444
6445 /**
6446 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6447 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6448 * touch mode to request focus when they are touched.
6449 *
6450 * @return Whether this view or one of its descendants actually took focus.
6451 *
6452 * @see #isInTouchMode()
6453 *
6454 */
6455 public final boolean requestFocusFromTouch() {
6456 // Leave touch mode if we need to
6457 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006458 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006459 if (viewRoot != null) {
6460 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 }
6462 }
6463 return requestFocus(View.FOCUS_DOWN);
6464 }
6465
6466 /**
6467 * @return Whether any ancestor of this view blocks descendant focus.
6468 */
6469 private boolean hasAncestorThatBlocksDescendantFocus() {
6470 ViewParent ancestor = mParent;
6471 while (ancestor instanceof ViewGroup) {
6472 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6473 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6474 return true;
6475 } else {
6476 ancestor = vgAncestor.getParent();
6477 }
6478 }
6479 return false;
6480 }
6481
6482 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006483 * Gets the mode for determining whether this View is important for accessibility
6484 * which is if it fires accessibility events and if it is reported to
6485 * accessibility services that query the screen.
6486 *
6487 * @return The mode for determining whether a View is important for accessibility.
6488 *
6489 * @attr ref android.R.styleable#View_importantForAccessibility
6490 *
6491 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6492 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6493 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6494 */
6495 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006496 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6497 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6498 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006499 })
6500 public int getImportantForAccessibility() {
6501 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6502 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6503 }
6504
6505 /**
6506 * Sets how to determine whether this view is important for accessibility
6507 * which is if it fires accessibility events and if it is reported to
6508 * accessibility services that query the screen.
6509 *
6510 * @param mode How to determine whether this view is important for accessibility.
6511 *
6512 * @attr ref android.R.styleable#View_importantForAccessibility
6513 *
6514 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6515 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6516 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6517 */
6518 public void setImportantForAccessibility(int mode) {
6519 if (mode != getImportantForAccessibility()) {
6520 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6521 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6522 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6523 notifyAccessibilityStateChanged();
6524 }
6525 }
6526
6527 /**
6528 * Gets whether this view should be exposed for accessibility.
6529 *
6530 * @return Whether the view is exposed for accessibility.
6531 *
6532 * @hide
6533 */
6534 public boolean isImportantForAccessibility() {
6535 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6536 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6537 switch (mode) {
6538 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6539 return true;
6540 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6541 return false;
6542 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6543 return isActionableForAccessibility() || hasListenersForAccessibility();
6544 default:
6545 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6546 + mode);
6547 }
6548 }
6549
6550 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006551 * Gets the mode for determining whether this View can take accessibility focus.
6552 *
6553 * @return The mode for determining whether a View can take accessibility focus.
6554 *
6555 * @attr ref android.R.styleable#View_accessibilityFocusable
6556 *
6557 * @see #ACCESSIBILITY_FOCUSABLE_YES
6558 * @see #ACCESSIBILITY_FOCUSABLE_NO
6559 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6560 *
6561 * @hide
6562 */
6563 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6564 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_AUTO, to = "auto"),
6565 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_YES, to = "yes"),
6566 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_NO, to = "no")
6567 })
6568 public int getAccessibilityFocusable() {
6569 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK) >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6570 }
6571
6572 /**
6573 * Sets how to determine whether this view can take accessibility focus.
6574 *
6575 * @param mode How to determine whether this view can take accessibility focus.
6576 *
6577 * @attr ref android.R.styleable#View_accessibilityFocusable
6578 *
6579 * @see #ACCESSIBILITY_FOCUSABLE_YES
6580 * @see #ACCESSIBILITY_FOCUSABLE_NO
6581 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6582 *
6583 * @hide
6584 */
6585 public void setAccessibilityFocusable(int mode) {
6586 if (mode != getAccessibilityFocusable()) {
6587 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSABLE_MASK;
6588 mPrivateFlags2 |= (mode << ACCESSIBILITY_FOCUSABLE_SHIFT)
6589 & ACCESSIBILITY_FOCUSABLE_MASK;
6590 notifyAccessibilityStateChanged();
6591 }
6592 }
6593
6594 /**
6595 * Gets whether this view can take accessibility focus.
6596 *
6597 * @return Whether the view can take accessibility focus.
6598 *
6599 * @hide
6600 */
6601 public boolean isAccessibilityFocusable() {
6602 final int mode = (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK)
6603 >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6604 switch (mode) {
6605 case ACCESSIBILITY_FOCUSABLE_YES:
6606 return true;
6607 case ACCESSIBILITY_FOCUSABLE_NO:
6608 return false;
6609 case ACCESSIBILITY_FOCUSABLE_AUTO:
6610 return canTakeAccessibilityFocusFromHover()
6611 || getAccessibilityNodeProvider() != null;
6612 default:
6613 throw new IllegalArgumentException("Unknow accessibility focusable mode: " + mode);
6614 }
6615 }
6616
6617 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006618 * Gets the parent for accessibility purposes. Note that the parent for
6619 * accessibility is not necessary the immediate parent. It is the first
6620 * predecessor that is important for accessibility.
6621 *
6622 * @return The parent for accessibility purposes.
6623 */
6624 public ViewParent getParentForAccessibility() {
6625 if (mParent instanceof View) {
6626 View parentView = (View) mParent;
6627 if (parentView.includeForAccessibility()) {
6628 return mParent;
6629 } else {
6630 return mParent.getParentForAccessibility();
6631 }
6632 }
6633 return null;
6634 }
6635
6636 /**
6637 * Adds the children of a given View for accessibility. Since some Views are
6638 * not important for accessibility the children for accessibility are not
6639 * necessarily direct children of the riew, rather they are the first level of
6640 * descendants important for accessibility.
6641 *
6642 * @param children The list of children for accessibility.
6643 */
6644 public void addChildrenForAccessibility(ArrayList<View> children) {
6645 if (includeForAccessibility()) {
6646 children.add(this);
6647 }
6648 }
6649
6650 /**
6651 * Whether to regard this view for accessibility. A view is regarded for
6652 * accessibility if it is important for accessibility or the querying
6653 * accessibility service has explicitly requested that view not
6654 * important for accessibility are regarded.
6655 *
6656 * @return Whether to regard the view 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 includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006661 if (mAttachInfo != null) {
6662 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006663 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006664 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006665 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006666 }
6667 return false;
6668 }
6669
6670 /**
6671 * Returns whether the View is considered actionable from
6672 * accessibility perspective. Such view are important for
6673 * accessiiblity.
6674 *
6675 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006676 *
6677 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006678 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006679 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006680 return (isClickable() || isLongClickable() || isFocusable());
6681 }
6682
6683 /**
6684 * Returns whether the View has registered callbacks wich makes it
6685 * important for accessiiblity.
6686 *
6687 * @return True if the view is actionable for accessibility.
6688 */
6689 private boolean hasListenersForAccessibility() {
6690 ListenerInfo info = getListenerInfo();
6691 return mTouchDelegate != null || info.mOnKeyListener != null
6692 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6693 || info.mOnHoverListener != null || info.mOnDragListener != null;
6694 }
6695
6696 /**
6697 * Notifies accessibility services that some view's important for
6698 * accessibility state has changed. Note that such notifications
6699 * are made at most once every
6700 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6701 * to avoid unnecessary load to the system. Also once a view has
6702 * made a notifucation this method is a NOP until the notification has
6703 * been sent to clients.
6704 *
6705 * @hide
6706 *
6707 * TODO: Makse sure this method is called for any view state change
6708 * that is interesting for accessilility purposes.
6709 */
6710 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006711 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6712 return;
6713 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006714 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6715 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6716 if (mParent != null) {
6717 mParent.childAccessibilityStateChanged(this);
6718 }
6719 }
6720 }
6721
6722 /**
6723 * Reset the state indicating the this view has requested clients
6724 * interested in its accessiblity state to be notified.
6725 *
6726 * @hide
6727 */
6728 public void resetAccessibilityStateChanged() {
6729 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6730 }
6731
6732 /**
6733 * Performs the specified accessibility action on the view. For
6734 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006735 * <p>
6736 * If an {@link AccessibilityDelegate} has been specified via calling
6737 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6738 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6739 * is responsible for handling this call.
6740 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006741 *
6742 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006743 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006744 * @return Whether the action was performed.
6745 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006746 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006747 if (mAccessibilityDelegate != null) {
6748 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6749 } else {
6750 return performAccessibilityActionInternal(action, arguments);
6751 }
6752 }
6753
6754 /**
6755 * @see #performAccessibilityAction(int, Bundle)
6756 *
6757 * Note: Called from the default {@link AccessibilityDelegate}.
6758 */
6759 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006760 switch (action) {
6761 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006762 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006763 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006764 }
6765 } break;
6766 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6767 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006768 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006769 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006770 } break;
6771 case AccessibilityNodeInfo.ACTION_FOCUS: {
6772 if (!hasFocus()) {
6773 // Get out of touch mode since accessibility
6774 // wants to move focus around.
6775 getViewRootImpl().ensureTouchMode(false);
6776 return requestFocus();
6777 }
6778 } break;
6779 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6780 if (hasFocus()) {
6781 clearFocus();
6782 return !isFocused();
6783 }
6784 } break;
6785 case AccessibilityNodeInfo.ACTION_SELECT: {
6786 if (!isSelected()) {
6787 setSelected(true);
6788 return isSelected();
6789 }
6790 } break;
6791 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6792 if (isSelected()) {
6793 setSelected(false);
6794 return !isSelected();
6795 }
6796 } break;
6797 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006798 final int mode = getAccessibilityFocusable();
6799 if (!isAccessibilityFocused()
6800 && (mode == ACCESSIBILITY_FOCUSABLE_YES
6801 || mode == ACCESSIBILITY_FOCUSABLE_AUTO)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006802 return requestAccessibilityFocus();
6803 }
6804 } break;
6805 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6806 if (isAccessibilityFocused()) {
6807 clearAccessibilityFocus();
6808 return true;
6809 }
6810 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006811 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6812 if (arguments != null) {
6813 final int granularity = arguments.getInt(
6814 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6815 return nextAtGranularity(granularity);
6816 }
6817 } break;
6818 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6819 if (arguments != null) {
6820 final int granularity = arguments.getInt(
6821 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6822 return previousAtGranularity(granularity);
6823 }
6824 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006825 }
6826 return false;
6827 }
6828
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006829 private boolean nextAtGranularity(int granularity) {
6830 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006831 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006832 return false;
6833 }
6834 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6835 if (iterator == null) {
6836 return false;
6837 }
6838 final int current = getAccessibilityCursorPosition();
6839 final int[] range = iterator.following(current);
6840 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006841 return false;
6842 }
6843 final int start = range[0];
6844 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006845 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006846 sendViewTextTraversedAtGranularityEvent(
6847 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6848 granularity, start, end);
6849 return true;
6850 }
6851
6852 private boolean previousAtGranularity(int granularity) {
6853 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006854 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006855 return false;
6856 }
6857 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6858 if (iterator == null) {
6859 return false;
6860 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006861 int current = getAccessibilityCursorPosition();
6862 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6863 current = text.length();
6864 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6865 // When traversing by character we always put the cursor after the character
6866 // to ease edit and have to compensate before asking the for previous segment.
6867 current--;
6868 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006869 final int[] range = iterator.preceding(current);
6870 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006871 return false;
6872 }
6873 final int start = range[0];
6874 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006875 // Always put the cursor after the character to ease edit.
6876 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6877 setAccessibilityCursorPosition(end);
6878 } else {
6879 setAccessibilityCursorPosition(start);
6880 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006881 sendViewTextTraversedAtGranularityEvent(
6882 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6883 granularity, start, end);
6884 return true;
6885 }
6886
6887 /**
6888 * Gets the text reported for accessibility purposes.
6889 *
6890 * @return The accessibility text.
6891 *
6892 * @hide
6893 */
6894 public CharSequence getIterableTextForAccessibility() {
6895 return mContentDescription;
6896 }
6897
6898 /**
6899 * @hide
6900 */
6901 public int getAccessibilityCursorPosition() {
6902 return mAccessibilityCursorPosition;
6903 }
6904
6905 /**
6906 * @hide
6907 */
6908 public void setAccessibilityCursorPosition(int position) {
6909 mAccessibilityCursorPosition = position;
6910 }
6911
6912 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6913 int fromIndex, int toIndex) {
6914 if (mParent == null) {
6915 return;
6916 }
6917 AccessibilityEvent event = AccessibilityEvent.obtain(
6918 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6919 onInitializeAccessibilityEvent(event);
6920 onPopulateAccessibilityEvent(event);
6921 event.setFromIndex(fromIndex);
6922 event.setToIndex(toIndex);
6923 event.setAction(action);
6924 event.setMovementGranularity(granularity);
6925 mParent.requestSendAccessibilityEvent(this, event);
6926 }
6927
6928 /**
6929 * @hide
6930 */
6931 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6932 switch (granularity) {
6933 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6934 CharSequence text = getIterableTextForAccessibility();
6935 if (text != null && text.length() > 0) {
6936 CharacterTextSegmentIterator iterator =
6937 CharacterTextSegmentIterator.getInstance(mContext);
6938 iterator.initialize(text.toString());
6939 return iterator;
6940 }
6941 } break;
6942 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6943 CharSequence text = getIterableTextForAccessibility();
6944 if (text != null && text.length() > 0) {
6945 WordTextSegmentIterator iterator =
6946 WordTextSegmentIterator.getInstance(mContext);
6947 iterator.initialize(text.toString());
6948 return iterator;
6949 }
6950 } break;
6951 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6952 CharSequence text = getIterableTextForAccessibility();
6953 if (text != null && text.length() > 0) {
6954 ParagraphTextSegmentIterator iterator =
6955 ParagraphTextSegmentIterator.getInstance();
6956 iterator.initialize(text.toString());
6957 return iterator;
6958 }
6959 } break;
6960 }
6961 return null;
6962 }
6963
Svetoslav Ganov42138042012-03-20 11:51:39 -07006964 /**
Romain Guya440b002010-02-24 15:57:54 -08006965 * @hide
6966 */
6967 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006968 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006969 clearDisplayList();
6970
Romain Guya440b002010-02-24 15:57:54 -08006971 onStartTemporaryDetach();
6972 }
6973
6974 /**
6975 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6977 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006978 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 */
6980 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006981 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006982 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006983 }
6984
6985 /**
6986 * @hide
6987 */
6988 public void dispatchFinishTemporaryDetach() {
6989 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 }
Romain Guy8506ab42009-06-11 17:35:47 -07006991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 /**
6993 * Called after {@link #onStartTemporaryDetach} when the container is done
6994 * changing the view.
6995 */
6996 public void onFinishTemporaryDetach() {
6997 }
Romain Guy8506ab42009-06-11 17:35:47 -07006998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007000 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7001 * for this view's window. Returns null if the view is not currently attached
7002 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007003 * just use the standard high-level event callbacks like
7004 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007005 */
7006 public KeyEvent.DispatcherState getKeyDispatcherState() {
7007 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7008 }
Joe Malin32736f02011-01-19 16:14:20 -08007009
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 * Dispatch a key event before it is processed by any input method
7012 * associated with the view hierarchy. This can be used to intercept
7013 * key events in special situations before the IME consumes them; a
7014 * typical example would be handling the BACK key to update the application's
7015 * UI instead of allowing the IME to see it and close itself.
7016 *
7017 * @param event The key event to be dispatched.
7018 * @return True if the event was handled, false otherwise.
7019 */
7020 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7021 return onKeyPreIme(event.getKeyCode(), event);
7022 }
7023
7024 /**
7025 * Dispatch a key event to the next view on the focus path. This path runs
7026 * from the top of the view tree down to the currently focused view. If this
7027 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7028 * the next node down the focus path. This method also fires any key
7029 * listeners.
7030 *
7031 * @param event The key event to be dispatched.
7032 * @return True if the event was handled, false otherwise.
7033 */
7034 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007035 if (mInputEventConsistencyVerifier != null) {
7036 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007038
Jeff Brown21bc5c92011-02-28 18:27:14 -08007039 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007040 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007041 ListenerInfo li = mListenerInfo;
7042 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7043 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 return true;
7045 }
7046
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007047 if (event.dispatch(this, mAttachInfo != null
7048 ? mAttachInfo.mKeyDispatchState : null, this)) {
7049 return true;
7050 }
7051
7052 if (mInputEventConsistencyVerifier != null) {
7053 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7054 }
7055 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 }
7057
7058 /**
7059 * Dispatches a key shortcut event.
7060 *
7061 * @param event The key event to be dispatched.
7062 * @return True if the event was handled by the view, false otherwise.
7063 */
7064 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7065 return onKeyShortcut(event.getKeyCode(), event);
7066 }
7067
7068 /**
7069 * Pass the touch screen motion event down to the target view, or this
7070 * view if it is the target.
7071 *
7072 * @param event The motion event to be dispatched.
7073 * @return True if the event was handled by the view, false otherwise.
7074 */
7075 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007076 if (mInputEventConsistencyVerifier != null) {
7077 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7078 }
7079
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007080 if (onFilterTouchEventForSecurity(event)) {
7081 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007082 ListenerInfo li = mListenerInfo;
7083 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7084 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007085 return true;
7086 }
7087
7088 if (onTouchEvent(event)) {
7089 return true;
7090 }
Jeff Brown85a31762010-09-01 17:01:00 -07007091 }
7092
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007093 if (mInputEventConsistencyVerifier != null) {
7094 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007095 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007096 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 }
7098
7099 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007100 * Filter the touch event to apply security policies.
7101 *
7102 * @param event The motion event to be filtered.
7103 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007104 *
Jeff Brown85a31762010-09-01 17:01:00 -07007105 * @see #getFilterTouchesWhenObscured
7106 */
7107 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007108 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007109 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7110 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7111 // Window is obscured, drop this touch.
7112 return false;
7113 }
7114 return true;
7115 }
7116
7117 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 * Pass a trackball motion event down to the focused view.
7119 *
7120 * @param event The motion event to be dispatched.
7121 * @return True if the event was handled by the view, false otherwise.
7122 */
7123 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007124 if (mInputEventConsistencyVerifier != null) {
7125 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7126 }
7127
Romain Guy02ccac62011-06-24 13:20:23 -07007128 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 }
7130
7131 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007132 * Dispatch a generic motion event.
7133 * <p>
7134 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7135 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007136 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007137 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007138 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007139 *
7140 * @param event The motion event to be dispatched.
7141 * @return True if the event was handled by the view, false otherwise.
7142 */
7143 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007144 if (mInputEventConsistencyVerifier != null) {
7145 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7146 }
7147
Jeff Browna032cc02011-03-07 16:56:21 -08007148 final int source = event.getSource();
7149 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7150 final int action = event.getAction();
7151 if (action == MotionEvent.ACTION_HOVER_ENTER
7152 || action == MotionEvent.ACTION_HOVER_MOVE
7153 || action == MotionEvent.ACTION_HOVER_EXIT) {
7154 if (dispatchHoverEvent(event)) {
7155 return true;
7156 }
7157 } else if (dispatchGenericPointerEvent(event)) {
7158 return true;
7159 }
7160 } else if (dispatchGenericFocusedEvent(event)) {
7161 return true;
7162 }
7163
Jeff Brown10b62902011-06-20 16:40:37 -07007164 if (dispatchGenericMotionEventInternal(event)) {
7165 return true;
7166 }
7167
7168 if (mInputEventConsistencyVerifier != null) {
7169 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7170 }
7171 return false;
7172 }
7173
7174 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007175 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007176 ListenerInfo li = mListenerInfo;
7177 if (li != null && li.mOnGenericMotionListener != null
7178 && (mViewFlags & ENABLED_MASK) == ENABLED
7179 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007180 return true;
7181 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007182
7183 if (onGenericMotionEvent(event)) {
7184 return true;
7185 }
7186
7187 if (mInputEventConsistencyVerifier != null) {
7188 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7189 }
7190 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007191 }
7192
7193 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007194 * Dispatch a hover event.
7195 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007196 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007197 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007198 * </p>
7199 *
7200 * @param event The motion event to be dispatched.
7201 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007202 */
7203 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007204 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007205 ListenerInfo li = mListenerInfo;
7206 if (li != null && li.mOnHoverListener != null
7207 && (mViewFlags & ENABLED_MASK) == ENABLED
7208 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007209 return true;
7210 }
7211
Jeff Browna032cc02011-03-07 16:56:21 -08007212 return onHoverEvent(event);
7213 }
7214
7215 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007216 * Returns true if the view has a child to which it has recently sent
7217 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7218 * it does not have a hovered child, then it must be the innermost hovered view.
7219 * @hide
7220 */
7221 protected boolean hasHoveredChild() {
7222 return false;
7223 }
7224
7225 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007226 * Dispatch a generic motion event to the view under the first pointer.
7227 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007228 * Do not call this method directly.
7229 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007230 * </p>
7231 *
7232 * @param event The motion event to be dispatched.
7233 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007234 */
7235 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7236 return false;
7237 }
7238
7239 /**
7240 * Dispatch a generic motion event to the currently focused view.
7241 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007242 * Do not call this method directly.
7243 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007244 * </p>
7245 *
7246 * @param event The motion event to be dispatched.
7247 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007248 */
7249 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7250 return false;
7251 }
7252
7253 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007254 * Dispatch a pointer event.
7255 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007256 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7257 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7258 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007259 * and should not be expected to handle other pointing device features.
7260 * </p>
7261 *
7262 * @param event The motion event to be dispatched.
7263 * @return True if the event was handled by the view, false otherwise.
7264 * @hide
7265 */
7266 public final boolean dispatchPointerEvent(MotionEvent event) {
7267 if (event.isTouchEvent()) {
7268 return dispatchTouchEvent(event);
7269 } else {
7270 return dispatchGenericMotionEvent(event);
7271 }
7272 }
7273
7274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 * Called when the window containing this view gains or loses window focus.
7276 * ViewGroups should override to route to their children.
7277 *
7278 * @param hasFocus True if the window containing this view now has focus,
7279 * false otherwise.
7280 */
7281 public void dispatchWindowFocusChanged(boolean hasFocus) {
7282 onWindowFocusChanged(hasFocus);
7283 }
7284
7285 /**
7286 * Called when the window containing this view gains or loses focus. Note
7287 * that this is separate from view focus: to receive key events, both
7288 * your view and its window must have focus. If a window is displayed
7289 * on top of yours that takes input focus, then your own window will lose
7290 * focus but the view focus will remain unchanged.
7291 *
7292 * @param hasWindowFocus True if the window containing this view now has
7293 * focus, false otherwise.
7294 */
7295 public void onWindowFocusChanged(boolean hasWindowFocus) {
7296 InputMethodManager imm = InputMethodManager.peekInstance();
7297 if (!hasWindowFocus) {
7298 if (isPressed()) {
7299 setPressed(false);
7300 }
7301 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7302 imm.focusOut(this);
7303 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007304 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007305 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007306 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7308 imm.focusIn(this);
7309 }
7310 refreshDrawableState();
7311 }
7312
7313 /**
7314 * Returns true if this view is in a window that currently has window focus.
7315 * Note that this is not the same as the view itself having focus.
7316 *
7317 * @return True if this view is in a window that currently has window focus.
7318 */
7319 public boolean hasWindowFocus() {
7320 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7321 }
7322
7323 /**
Adam Powell326d8082009-12-09 15:10:07 -08007324 * Dispatch a view visibility change down the view hierarchy.
7325 * ViewGroups should override to route to their children.
7326 * @param changedView The view whose visibility changed. Could be 'this' or
7327 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007328 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7329 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007330 */
7331 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7332 onVisibilityChanged(changedView, visibility);
7333 }
7334
7335 /**
7336 * Called when the visibility of the view or an ancestor of the view is changed.
7337 * @param changedView The view whose visibility changed. Could be 'this' or
7338 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007339 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7340 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007341 */
7342 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007343 if (visibility == VISIBLE) {
7344 if (mAttachInfo != null) {
7345 initialAwakenScrollBars();
7346 } else {
7347 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7348 }
7349 }
Adam Powell326d8082009-12-09 15:10:07 -08007350 }
7351
7352 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007353 * Dispatch a hint about whether this view is displayed. For instance, when
7354 * a View moves out of the screen, it might receives a display hint indicating
7355 * the view is not displayed. Applications should not <em>rely</em> on this hint
7356 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007357 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007358 * @param hint A hint about whether or not this view is displayed:
7359 * {@link #VISIBLE} or {@link #INVISIBLE}.
7360 */
7361 public void dispatchDisplayHint(int hint) {
7362 onDisplayHint(hint);
7363 }
7364
7365 /**
7366 * Gives this view a hint about whether is displayed or not. For instance, when
7367 * a View moves out of the screen, it might receives a display hint indicating
7368 * the view is not displayed. Applications should not <em>rely</em> on this hint
7369 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007370 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007371 * @param hint A hint about whether or not this view is displayed:
7372 * {@link #VISIBLE} or {@link #INVISIBLE}.
7373 */
7374 protected void onDisplayHint(int hint) {
7375 }
7376
7377 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007378 * Dispatch a window visibility change down the view hierarchy.
7379 * ViewGroups should override to route to their children.
7380 *
7381 * @param visibility The new visibility of the window.
7382 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007383 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384 */
7385 public void dispatchWindowVisibilityChanged(int visibility) {
7386 onWindowVisibilityChanged(visibility);
7387 }
7388
7389 /**
7390 * Called when the window containing has change its visibility
7391 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7392 * that this tells you whether or not your window is being made visible
7393 * to the window manager; this does <em>not</em> tell you whether or not
7394 * your window is obscured by other windows on the screen, even if it
7395 * is itself visible.
7396 *
7397 * @param visibility The new visibility of the window.
7398 */
7399 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007400 if (visibility == VISIBLE) {
7401 initialAwakenScrollBars();
7402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 }
7404
7405 /**
7406 * Returns the current visibility of the window this view is attached to
7407 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7408 *
7409 * @return Returns the current visibility of the view's window.
7410 */
7411 public int getWindowVisibility() {
7412 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7413 }
7414
7415 /**
7416 * Retrieve the overall visible display size in which the window this view is
7417 * attached to has been positioned in. This takes into account screen
7418 * decorations above the window, for both cases where the window itself
7419 * is being position inside of them or the window is being placed under
7420 * then and covered insets are used for the window to position its content
7421 * inside. In effect, this tells you the available area where content can
7422 * be placed and remain visible to users.
7423 *
7424 * <p>This function requires an IPC back to the window manager to retrieve
7425 * the requested information, so should not be used in performance critical
7426 * code like drawing.
7427 *
7428 * @param outRect Filled in with the visible display frame. If the view
7429 * is not attached to a window, this is simply the raw display size.
7430 */
7431 public void getWindowVisibleDisplayFrame(Rect outRect) {
7432 if (mAttachInfo != null) {
7433 try {
7434 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7435 } catch (RemoteException e) {
7436 return;
7437 }
7438 // XXX This is really broken, and probably all needs to be done
7439 // in the window manager, and we need to know more about whether
7440 // we want the area behind or in front of the IME.
7441 final Rect insets = mAttachInfo.mVisibleInsets;
7442 outRect.left += insets.left;
7443 outRect.top += insets.top;
7444 outRect.right -= insets.right;
7445 outRect.bottom -= insets.bottom;
7446 return;
7447 }
7448 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007449 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 }
7451
7452 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007453 * Dispatch a notification about a resource configuration change down
7454 * the view hierarchy.
7455 * ViewGroups should override to route to their children.
7456 *
7457 * @param newConfig The new resource configuration.
7458 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007459 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007460 */
7461 public void dispatchConfigurationChanged(Configuration newConfig) {
7462 onConfigurationChanged(newConfig);
7463 }
7464
7465 /**
7466 * Called when the current configuration of the resources being used
7467 * by the application have changed. You can use this to decide when
7468 * to reload resources that can changed based on orientation and other
7469 * configuration characterstics. You only need to use this if you are
7470 * not relying on the normal {@link android.app.Activity} mechanism of
7471 * recreating the activity instance upon a configuration change.
7472 *
7473 * @param newConfig The new resource configuration.
7474 */
7475 protected void onConfigurationChanged(Configuration newConfig) {
7476 }
7477
7478 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 * Private function to aggregate all per-view attributes in to the view
7480 * root.
7481 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007482 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7483 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 }
7485
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007486 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7487 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007488 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007489 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007490 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007491 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007492 ListenerInfo li = mListenerInfo;
7493 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007494 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 }
7497 }
7498
7499 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007500 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007502 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7503 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504 ai.mRecomputeGlobalAttributes = true;
7505 }
7506 }
7507 }
7508
7509 /**
7510 * Returns whether the device is currently in touch mode. Touch mode is entered
7511 * once the user begins interacting with the device by touch, and affects various
7512 * things like whether focus is always visible to the user.
7513 *
7514 * @return Whether the device is in touch mode.
7515 */
7516 @ViewDebug.ExportedProperty
7517 public boolean isInTouchMode() {
7518 if (mAttachInfo != null) {
7519 return mAttachInfo.mInTouchMode;
7520 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007521 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 }
7523 }
7524
7525 /**
7526 * Returns the context the view is running in, through which it can
7527 * access the current theme, resources, etc.
7528 *
7529 * @return The view's Context.
7530 */
7531 @ViewDebug.CapturedViewProperty
7532 public final Context getContext() {
7533 return mContext;
7534 }
7535
7536 /**
7537 * Handle a key event before it is processed by any input method
7538 * associated with the view hierarchy. This can be used to intercept
7539 * key events in special situations before the IME consumes them; a
7540 * typical example would be handling the BACK key to update the application's
7541 * UI instead of allowing the IME to see it and close itself.
7542 *
7543 * @param keyCode The value in event.getKeyCode().
7544 * @param event Description of the key event.
7545 * @return If you handled the event, return true. If you want to allow the
7546 * event to be handled by the next receiver, return false.
7547 */
7548 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7549 return false;
7550 }
7551
7552 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007553 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7554 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7556 * is released, if the view is enabled and clickable.
7557 *
Jean Chalard405bc512012-05-29 19:12:34 +09007558 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7559 * although some may elect to do so in some situations. Do not rely on this to
7560 * catch software key presses.
7561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 * @param keyCode A key code that represents the button pressed, from
7563 * {@link android.view.KeyEvent}.
7564 * @param event The KeyEvent object that defines the button action.
7565 */
7566 public boolean onKeyDown(int keyCode, KeyEvent event) {
7567 boolean result = false;
7568
7569 switch (keyCode) {
7570 case KeyEvent.KEYCODE_DPAD_CENTER:
7571 case KeyEvent.KEYCODE_ENTER: {
7572 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7573 return true;
7574 }
7575 // Long clickable items don't necessarily have to be clickable
7576 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7577 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7578 (event.getRepeatCount() == 0)) {
7579 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007580 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 return true;
7582 }
7583 break;
7584 }
7585 }
7586 return result;
7587 }
7588
7589 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007590 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7591 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7592 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007593 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7594 * although some may elect to do so in some situations. Do not rely on this to
7595 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007596 */
7597 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7598 return false;
7599 }
7600
7601 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007602 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7603 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7605 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007606 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7607 * although some may elect to do so in some situations. Do not rely on this to
7608 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 *
7610 * @param keyCode A key code that represents the button pressed, from
7611 * {@link android.view.KeyEvent}.
7612 * @param event The KeyEvent object that defines the button action.
7613 */
7614 public boolean onKeyUp(int keyCode, KeyEvent event) {
7615 boolean result = false;
7616
7617 switch (keyCode) {
7618 case KeyEvent.KEYCODE_DPAD_CENTER:
7619 case KeyEvent.KEYCODE_ENTER: {
7620 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7621 return true;
7622 }
7623 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7624 setPressed(false);
7625
7626 if (!mHasPerformedLongPress) {
7627 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007628 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629
7630 result = performClick();
7631 }
7632 }
7633 break;
7634 }
7635 }
7636 return result;
7637 }
7638
7639 /**
7640 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7641 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7642 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007643 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7644 * although some may elect to do so in some situations. Do not rely on this to
7645 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 *
7647 * @param keyCode A key code that represents the button pressed, from
7648 * {@link android.view.KeyEvent}.
7649 * @param repeatCount The number of times the action was made.
7650 * @param event The KeyEvent object that defines the button action.
7651 */
7652 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7653 return false;
7654 }
7655
7656 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007657 * Called on the focused view when a key shortcut event is not handled.
7658 * Override this method to implement local key shortcuts for the View.
7659 * Key shortcuts can also be implemented by setting the
7660 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 *
7662 * @param keyCode The value in event.getKeyCode().
7663 * @param event Description of the key event.
7664 * @return If you handled the event, return true. If you want to allow the
7665 * event to be handled by the next receiver, return false.
7666 */
7667 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7668 return false;
7669 }
7670
7671 /**
7672 * Check whether the called view is a text editor, in which case it
7673 * would make sense to automatically display a soft input window for
7674 * it. Subclasses should override this if they implement
7675 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007676 * a call on that method would return a non-null InputConnection, and
7677 * they are really a first-class editor that the user would normally
7678 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007679 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007680 * <p>The default implementation always returns false. This does
7681 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7682 * will not be called or the user can not otherwise perform edits on your
7683 * view; it is just a hint to the system that this is not the primary
7684 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007685 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 * @return Returns true if this view is a text editor, else false.
7687 */
7688 public boolean onCheckIsTextEditor() {
7689 return false;
7690 }
Romain Guy8506ab42009-06-11 17:35:47 -07007691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 /**
7693 * Create a new InputConnection for an InputMethod to interact
7694 * with the view. The default implementation returns null, since it doesn't
7695 * support input methods. You can override this to implement such support.
7696 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007697 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 * <p>When implementing this, you probably also want to implement
7699 * {@link #onCheckIsTextEditor()} to indicate you will return a
7700 * non-null InputConnection.
7701 *
7702 * @param outAttrs Fill in with attribute information about the connection.
7703 */
7704 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7705 return null;
7706 }
7707
7708 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007709 * Called by the {@link android.view.inputmethod.InputMethodManager}
7710 * when a view who is not the current
7711 * input connection target is trying to make a call on the manager. The
7712 * default implementation returns false; you can override this to return
7713 * true for certain views if you are performing InputConnection proxying
7714 * to them.
7715 * @param view The View that is making the InputMethodManager call.
7716 * @return Return true to allow the call, false to reject.
7717 */
7718 public boolean checkInputConnectionProxy(View view) {
7719 return false;
7720 }
Romain Guy8506ab42009-06-11 17:35:47 -07007721
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 * Show the context menu for this view. It is not safe to hold on to the
7724 * menu after returning from this method.
7725 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007726 * You should normally not overload this method. Overload
7727 * {@link #onCreateContextMenu(ContextMenu)} or define an
7728 * {@link OnCreateContextMenuListener} to add items to the context menu.
7729 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 * @param menu The context menu to populate
7731 */
7732 public void createContextMenu(ContextMenu menu) {
7733 ContextMenuInfo menuInfo = getContextMenuInfo();
7734
7735 // Sets the current menu info so all items added to menu will have
7736 // my extra info set.
7737 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7738
7739 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007740 ListenerInfo li = mListenerInfo;
7741 if (li != null && li.mOnCreateContextMenuListener != null) {
7742 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 }
7744
7745 // Clear the extra information so subsequent items that aren't mine don't
7746 // have my extra info.
7747 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7748
7749 if (mParent != null) {
7750 mParent.createContextMenu(menu);
7751 }
7752 }
7753
7754 /**
7755 * Views should implement this if they have extra information to associate
7756 * with the context menu. The return result is supplied as a parameter to
7757 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7758 * callback.
7759 *
7760 * @return Extra information about the item for which the context menu
7761 * should be shown. This information will vary across different
7762 * subclasses of View.
7763 */
7764 protected ContextMenuInfo getContextMenuInfo() {
7765 return null;
7766 }
7767
7768 /**
7769 * Views should implement this if the view itself is going to add items to
7770 * the context menu.
7771 *
7772 * @param menu the context menu to populate
7773 */
7774 protected void onCreateContextMenu(ContextMenu menu) {
7775 }
7776
7777 /**
7778 * Implement this method to handle trackball motion events. The
7779 * <em>relative</em> movement of the trackball since the last event
7780 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7781 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7782 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7783 * they will often be fractional values, representing the more fine-grained
7784 * movement information available from a trackball).
7785 *
7786 * @param event The motion event.
7787 * @return True if the event was handled, false otherwise.
7788 */
7789 public boolean onTrackballEvent(MotionEvent event) {
7790 return false;
7791 }
7792
7793 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007794 * Implement this method to handle generic motion events.
7795 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007796 * Generic motion events describe joystick movements, mouse hovers, track pad
7797 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007798 * {@link MotionEvent#getSource() source} of the motion event specifies
7799 * the class of input that was received. Implementations of this method
7800 * must examine the bits in the source before processing the event.
7801 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007802 * </p><p>
7803 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7804 * are delivered to the view under the pointer. All other generic motion events are
7805 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007806 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007807 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007808 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007809 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7810 * // process the joystick movement...
7811 * return true;
7812 * }
7813 * }
7814 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7815 * switch (event.getAction()) {
7816 * case MotionEvent.ACTION_HOVER_MOVE:
7817 * // process the mouse hover movement...
7818 * return true;
7819 * case MotionEvent.ACTION_SCROLL:
7820 * // process the scroll wheel movement...
7821 * return true;
7822 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007823 * }
7824 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007825 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007826 *
7827 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007828 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007829 */
7830 public boolean onGenericMotionEvent(MotionEvent event) {
7831 return false;
7832 }
7833
7834 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007835 * Implement this method to handle hover events.
7836 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007837 * This method is called whenever a pointer is hovering into, over, or out of the
7838 * bounds of a view and the view is not currently being touched.
7839 * Hover events are represented as pointer events with action
7840 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7841 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7842 * </p>
7843 * <ul>
7844 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7845 * when the pointer enters the bounds of the view.</li>
7846 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7847 * when the pointer has already entered the bounds of the view and has moved.</li>
7848 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7849 * when the pointer has exited the bounds of the view or when the pointer is
7850 * about to go down due to a button click, tap, or similar user action that
7851 * causes the view to be touched.</li>
7852 * </ul>
7853 * <p>
7854 * The view should implement this method to return true to indicate that it is
7855 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007856 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007857 * The default implementation calls {@link #setHovered} to update the hovered state
7858 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007859 * is enabled and is clickable. The default implementation also sends hover
7860 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007861 * </p>
7862 *
7863 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007864 * @return True if the view handled the hover event.
7865 *
7866 * @see #isHovered
7867 * @see #setHovered
7868 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007869 */
7870 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007871 // The root view may receive hover (or touch) events that are outside the bounds of
7872 // the window. This code ensures that we only send accessibility events for
7873 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007874 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007875 if (!mSendingHoverAccessibilityEvents) {
7876 if ((action == MotionEvent.ACTION_HOVER_ENTER
7877 || action == MotionEvent.ACTION_HOVER_MOVE)
7878 && !hasHoveredChild()
7879 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007880 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007881 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007882 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007883 }
7884 } else {
7885 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007886 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007887 && !pointInView(event.getX(), event.getY()))) {
7888 mSendingHoverAccessibilityEvents = false;
7889 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007890 // If the window does not have input focus we take away accessibility
7891 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007892 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007893 getViewRootImpl().setAccessibilityFocusedHost(null);
7894 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007895 }
Jeff Browna1b24182011-07-28 13:38:24 -07007896 }
7897
Jeff Brown87b7f802011-06-21 18:35:45 -07007898 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007899 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007900 case MotionEvent.ACTION_HOVER_ENTER:
7901 setHovered(true);
7902 break;
7903 case MotionEvent.ACTION_HOVER_EXIT:
7904 setHovered(false);
7905 break;
7906 }
Jeff Browna1b24182011-07-28 13:38:24 -07007907
7908 // Dispatch the event to onGenericMotionEvent before returning true.
7909 // This is to provide compatibility with existing applications that
7910 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7911 // break because of the new default handling for hoverable views
7912 // in onHoverEvent.
7913 // Note that onGenericMotionEvent will be called by default when
7914 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7915 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007916 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007917 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007918
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007919 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007920 }
7921
7922 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007923 * Returns true if the view should handle {@link #onHoverEvent}
7924 * by calling {@link #setHovered} to change its hovered state.
7925 *
7926 * @return True if the view is hoverable.
7927 */
7928 private boolean isHoverable() {
7929 final int viewFlags = mViewFlags;
7930 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7931 return false;
7932 }
7933
7934 return (viewFlags & CLICKABLE) == CLICKABLE
7935 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7936 }
7937
7938 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007939 * Returns true if the view is currently hovered.
7940 *
7941 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007942 *
7943 * @see #setHovered
7944 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007945 */
Jeff Brown10b62902011-06-20 16:40:37 -07007946 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007947 public boolean isHovered() {
7948 return (mPrivateFlags & HOVERED) != 0;
7949 }
7950
7951 /**
7952 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007953 * <p>
7954 * Calling this method also changes the drawable state of the view. This
7955 * enables the view to react to hover by using different drawable resources
7956 * to change its appearance.
7957 * </p><p>
7958 * The {@link #onHoverChanged} method is called when the hovered state changes.
7959 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007960 *
7961 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007962 *
7963 * @see #isHovered
7964 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007965 */
7966 public void setHovered(boolean hovered) {
7967 if (hovered) {
7968 if ((mPrivateFlags & HOVERED) == 0) {
7969 mPrivateFlags |= HOVERED;
7970 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007971 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007972 }
7973 } else {
7974 if ((mPrivateFlags & HOVERED) != 0) {
7975 mPrivateFlags &= ~HOVERED;
7976 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007977 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007978 }
7979 }
7980 }
7981
7982 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007983 * Implement this method to handle hover state changes.
7984 * <p>
7985 * This method is called whenever the hover state changes as a result of a
7986 * call to {@link #setHovered}.
7987 * </p>
7988 *
7989 * @param hovered The current hover state, as returned by {@link #isHovered}.
7990 *
7991 * @see #isHovered
7992 * @see #setHovered
7993 */
7994 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007995 }
7996
7997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 * Implement this method to handle touch screen motion events.
7999 *
8000 * @param event The motion event.
8001 * @return True if the event was handled, false otherwise.
8002 */
8003 public boolean onTouchEvent(MotionEvent event) {
8004 final int viewFlags = mViewFlags;
8005
8006 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008007 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008008 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 // A disabled view that is clickable still consumes the touch
8011 // events, it just doesn't respond to them.
8012 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8013 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8014 }
8015
8016 if (mTouchDelegate != null) {
8017 if (mTouchDelegate.onTouchEvent(event)) {
8018 return true;
8019 }
8020 }
8021
8022 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8023 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8024 switch (event.getAction()) {
8025 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08008026 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
8027 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 // take focus if we don't have it already and we should in
8029 // touch mode.
8030 boolean focusTaken = false;
8031 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8032 focusTaken = requestFocus();
8033 }
8034
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008035 if (prepressed) {
8036 // The button is being released before we actually
8037 // showed it as pressed. Make it show the pressed
8038 // state now (before scheduling the click) to ensure
8039 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008040 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008041 }
Joe Malin32736f02011-01-19 16:14:20 -08008042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043 if (!mHasPerformedLongPress) {
8044 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008045 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046
8047 // Only perform take click actions if we were in the pressed state
8048 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008049 // Use a Runnable and post this rather than calling
8050 // performClick directly. This lets other visual state
8051 // of the view update before click actions start.
8052 if (mPerformClick == null) {
8053 mPerformClick = new PerformClick();
8054 }
8055 if (!post(mPerformClick)) {
8056 performClick();
8057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 }
8059 }
8060
8061 if (mUnsetPressedState == null) {
8062 mUnsetPressedState = new UnsetPressedState();
8063 }
8064
Adam Powelle14579b2009-12-16 18:39:52 -08008065 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008066 postDelayed(mUnsetPressedState,
8067 ViewConfiguration.getPressedStateDuration());
8068 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 // If the post failed, unpress right now
8070 mUnsetPressedState.run();
8071 }
Adam Powelle14579b2009-12-16 18:39:52 -08008072 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
8074 break;
8075
8076 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008077 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008078
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008079 if (performButtonActionOnTouchDown(event)) {
8080 break;
8081 }
8082
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008083 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008084 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008085
8086 // For views inside a scrolling container, delay the pressed feedback for
8087 // a short period in case this is a scroll.
8088 if (isInScrollingContainer) {
8089 mPrivateFlags |= PREPRESSED;
8090 if (mPendingCheckForTap == null) {
8091 mPendingCheckForTap = new CheckForTap();
8092 }
8093 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8094 } else {
8095 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008096 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008097 checkForLongClick(0);
8098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 break;
8100
8101 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008102 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008103 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 break;
8105
8106 case MotionEvent.ACTION_MOVE:
8107 final int x = (int) event.getX();
8108 final int y = (int) event.getY();
8109
8110 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008111 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008112 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008113 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008115 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008116 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117
Adam Powell4d6f0662012-02-21 15:11:11 -08008118 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 }
8121 break;
8122 }
8123 return true;
8124 }
8125
8126 return false;
8127 }
8128
8129 /**
Adam Powell10298662011-08-14 18:26:30 -07008130 * @hide
8131 */
8132 public boolean isInScrollingContainer() {
8133 ViewParent p = getParent();
8134 while (p != null && p instanceof ViewGroup) {
8135 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8136 return true;
8137 }
8138 p = p.getParent();
8139 }
8140 return false;
8141 }
8142
8143 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008144 * Remove the longpress detection timer.
8145 */
8146 private void removeLongPressCallback() {
8147 if (mPendingCheckForLongPress != null) {
8148 removeCallbacks(mPendingCheckForLongPress);
8149 }
8150 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008151
8152 /**
8153 * Remove the pending click action
8154 */
8155 private void removePerformClickCallback() {
8156 if (mPerformClick != null) {
8157 removeCallbacks(mPerformClick);
8158 }
8159 }
8160
Adam Powelle14579b2009-12-16 18:39:52 -08008161 /**
Romain Guya440b002010-02-24 15:57:54 -08008162 * Remove the prepress detection timer.
8163 */
8164 private void removeUnsetPressCallback() {
8165 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8166 setPressed(false);
8167 removeCallbacks(mUnsetPressedState);
8168 }
8169 }
8170
8171 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008172 * Remove the tap detection timer.
8173 */
8174 private void removeTapCallback() {
8175 if (mPendingCheckForTap != null) {
8176 mPrivateFlags &= ~PREPRESSED;
8177 removeCallbacks(mPendingCheckForTap);
8178 }
8179 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008180
8181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 * Cancels a pending long press. Your subclass can use this if you
8183 * want the context menu to come up if the user presses and holds
8184 * at the same place, but you don't want it to come up if they press
8185 * and then move around enough to cause scrolling.
8186 */
8187 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008188 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008189
8190 /*
8191 * The prepressed state handled by the tap callback is a display
8192 * construct, but the tap callback will post a long press callback
8193 * less its own timeout. Remove it here.
8194 */
8195 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 }
8197
8198 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008199 * Remove the pending callback for sending a
8200 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8201 */
8202 private void removeSendViewScrolledAccessibilityEventCallback() {
8203 if (mSendViewScrolledAccessibilityEvent != null) {
8204 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008205 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008206 }
8207 }
8208
8209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 * Sets the TouchDelegate for this View.
8211 */
8212 public void setTouchDelegate(TouchDelegate delegate) {
8213 mTouchDelegate = delegate;
8214 }
8215
8216 /**
8217 * Gets the TouchDelegate for this View.
8218 */
8219 public TouchDelegate getTouchDelegate() {
8220 return mTouchDelegate;
8221 }
8222
8223 /**
8224 * Set flags controlling behavior of this view.
8225 *
8226 * @param flags Constant indicating the value which should be set
8227 * @param mask Constant indicating the bit range that should be changed
8228 */
8229 void setFlags(int flags, int mask) {
8230 int old = mViewFlags;
8231 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8232
8233 int changed = mViewFlags ^ old;
8234 if (changed == 0) {
8235 return;
8236 }
8237 int privateFlags = mPrivateFlags;
8238
8239 /* Check if the FOCUSABLE bit has changed */
8240 if (((changed & FOCUSABLE_MASK) != 0) &&
8241 ((privateFlags & HAS_BOUNDS) !=0)) {
8242 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8243 && ((privateFlags & FOCUSED) != 0)) {
8244 /* Give up focus if we are no longer focusable */
8245 clearFocus();
8246 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8247 && ((privateFlags & FOCUSED) == 0)) {
8248 /*
8249 * Tell the view system that we are now available to take focus
8250 * if no one else already has it.
8251 */
8252 if (mParent != null) mParent.focusableViewAvailable(this);
8253 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008254 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8255 notifyAccessibilityStateChanged();
8256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 }
8258
8259 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8260 if ((changed & VISIBILITY_MASK) != 0) {
8261 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008262 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008263 * it was not visible. Marking it drawn ensures that the invalidation will
8264 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008266 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008267 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268
8269 needGlobalAttributesUpdate(true);
8270
8271 // a view becoming visible is worth notifying the parent
8272 // about in case nothing has focus. even if this specific view
8273 // isn't focusable, it may contain something that is, so let
8274 // the root view try to give this focus if nothing else does.
8275 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8276 mParent.focusableViewAvailable(this);
8277 }
8278 }
8279 }
8280
8281 /* Check if the GONE bit has changed */
8282 if ((changed & GONE) != 0) {
8283 needGlobalAttributesUpdate(false);
8284 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285
Romain Guyecd80ee2009-12-03 17:13:02 -08008286 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8287 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008288 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008289 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008290 if (mParent instanceof View) {
8291 // GONE views noop invalidation, so invalidate the parent
8292 ((View) mParent).invalidate(true);
8293 }
8294 // Mark the view drawn to ensure that it gets invalidated properly the next
8295 // time it is visible and gets invalidated
8296 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 }
8298 if (mAttachInfo != null) {
8299 mAttachInfo.mViewVisibilityChanged = true;
8300 }
8301 }
8302
8303 /* Check if the VISIBLE bit has changed */
8304 if ((changed & INVISIBLE) != 0) {
8305 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008306 /*
8307 * If this view is becoming invisible, set the DRAWN flag so that
8308 * the next invalidate() will not be skipped.
8309 */
8310 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311
8312 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008313 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 if (getRootView() != this) {
8315 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008316 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 }
8318 }
8319 if (mAttachInfo != null) {
8320 mAttachInfo.mViewVisibilityChanged = true;
8321 }
8322 }
8323
Adam Powell326d8082009-12-09 15:10:07 -08008324 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008325 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008326 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8327 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008328 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008329 } else if (mParent != null) {
8330 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008331 }
Adam Powell326d8082009-12-09 15:10:07 -08008332 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8333 }
8334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8336 destroyDrawingCache();
8337 }
8338
8339 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8340 destroyDrawingCache();
8341 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008342 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008343 }
8344
8345 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8346 destroyDrawingCache();
8347 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8348 }
8349
8350 if ((changed & DRAW_MASK) != 0) {
8351 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008352 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 mPrivateFlags &= ~SKIP_DRAW;
8354 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8355 } else {
8356 mPrivateFlags |= SKIP_DRAW;
8357 }
8358 } else {
8359 mPrivateFlags &= ~SKIP_DRAW;
8360 }
8361 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008362 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 }
8364
8365 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008366 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 mParent.recomputeViewAttributes(this);
8368 }
8369 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008370
8371 if (AccessibilityManager.getInstance(mContext).isEnabled()
8372 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8373 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8374 notifyAccessibilityStateChanged();
8375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 }
8377
8378 /**
8379 * Change the view's z order in the tree, so it's on top of other sibling
8380 * views
8381 */
8382 public void bringToFront() {
8383 if (mParent != null) {
8384 mParent.bringChildToFront(this);
8385 }
8386 }
8387
8388 /**
8389 * This is called in response to an internal scroll in this view (i.e., the
8390 * view scrolled its own contents). This is typically as a result of
8391 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8392 * called.
8393 *
8394 * @param l Current horizontal scroll origin.
8395 * @param t Current vertical scroll origin.
8396 * @param oldl Previous horizontal scroll origin.
8397 * @param oldt Previous vertical scroll origin.
8398 */
8399 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008400 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8401 postSendViewScrolledAccessibilityEventCallback();
8402 }
8403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404 mBackgroundSizeChanged = true;
8405
8406 final AttachInfo ai = mAttachInfo;
8407 if (ai != null) {
8408 ai.mViewScrollChanged = true;
8409 }
8410 }
8411
8412 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008413 * Interface definition for a callback to be invoked when the layout bounds of a view
8414 * changes due to layout processing.
8415 */
8416 public interface OnLayoutChangeListener {
8417 /**
8418 * Called when the focus state of a view has changed.
8419 *
8420 * @param v The view whose state has changed.
8421 * @param left The new value of the view's left property.
8422 * @param top The new value of the view's top property.
8423 * @param right The new value of the view's right property.
8424 * @param bottom The new value of the view's bottom property.
8425 * @param oldLeft The previous value of the view's left property.
8426 * @param oldTop The previous value of the view's top property.
8427 * @param oldRight The previous value of the view's right property.
8428 * @param oldBottom The previous value of the view's bottom property.
8429 */
8430 void onLayoutChange(View v, int left, int top, int right, int bottom,
8431 int oldLeft, int oldTop, int oldRight, int oldBottom);
8432 }
8433
8434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 * This is called during layout when the size of this view has changed. If
8436 * you were just added to the view hierarchy, you're called with the old
8437 * values of 0.
8438 *
8439 * @param w Current width of this view.
8440 * @param h Current height of this view.
8441 * @param oldw Old width of this view.
8442 * @param oldh Old height of this view.
8443 */
8444 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8445 }
8446
8447 /**
8448 * Called by draw to draw the child views. This may be overridden
8449 * by derived classes to gain control just before its children are drawn
8450 * (but after its own view has been drawn).
8451 * @param canvas the canvas on which to draw the view
8452 */
8453 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008455 }
8456
8457 /**
8458 * Gets the parent of this view. Note that the parent is a
8459 * ViewParent and not necessarily a View.
8460 *
8461 * @return Parent of this view.
8462 */
8463 public final ViewParent getParent() {
8464 return mParent;
8465 }
8466
8467 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008468 * Set the horizontal scrolled position of your view. This will cause a call to
8469 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8470 * invalidated.
8471 * @param value the x position to scroll to
8472 */
8473 public void setScrollX(int value) {
8474 scrollTo(value, mScrollY);
8475 }
8476
8477 /**
8478 * Set the vertical scrolled position of your view. This will cause a call to
8479 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8480 * invalidated.
8481 * @param value the y position to scroll to
8482 */
8483 public void setScrollY(int value) {
8484 scrollTo(mScrollX, value);
8485 }
8486
8487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 * Return the scrolled left position of this view. This is the left edge of
8489 * the displayed part of your view. You do not need to draw any pixels
8490 * farther left, since those are outside of the frame of your view on
8491 * screen.
8492 *
8493 * @return The left edge of the displayed part of your view, in pixels.
8494 */
8495 public final int getScrollX() {
8496 return mScrollX;
8497 }
8498
8499 /**
8500 * Return the scrolled top position of this view. This is the top edge of
8501 * the displayed part of your view. You do not need to draw any pixels above
8502 * it, since those are outside of the frame of your view on screen.
8503 *
8504 * @return The top edge of the displayed part of your view, in pixels.
8505 */
8506 public final int getScrollY() {
8507 return mScrollY;
8508 }
8509
8510 /**
8511 * Return the width of the your view.
8512 *
8513 * @return The width of your view, in pixels.
8514 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008515 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 public final int getWidth() {
8517 return mRight - mLeft;
8518 }
8519
8520 /**
8521 * Return the height of your view.
8522 *
8523 * @return The height of your view, in pixels.
8524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008525 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 public final int getHeight() {
8527 return mBottom - mTop;
8528 }
8529
8530 /**
8531 * Return the visible drawing bounds of your view. Fills in the output
8532 * rectangle with the values from getScrollX(), getScrollY(),
8533 * getWidth(), and getHeight().
8534 *
8535 * @param outRect The (scrolled) drawing bounds of the view.
8536 */
8537 public void getDrawingRect(Rect outRect) {
8538 outRect.left = mScrollX;
8539 outRect.top = mScrollY;
8540 outRect.right = mScrollX + (mRight - mLeft);
8541 outRect.bottom = mScrollY + (mBottom - mTop);
8542 }
8543
8544 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008545 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8546 * raw width component (that is the result is masked by
8547 * {@link #MEASURED_SIZE_MASK}).
8548 *
8549 * @return The raw measured width of this view.
8550 */
8551 public final int getMeasuredWidth() {
8552 return mMeasuredWidth & MEASURED_SIZE_MASK;
8553 }
8554
8555 /**
8556 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008557 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008558 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 * This should be used during measurement and layout calculations only. Use
8560 * {@link #getWidth()} to see how wide a view is after layout.
8561 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008562 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008563 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008564 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 return mMeasuredWidth;
8566 }
8567
8568 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008569 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8570 * raw width component (that is the result is masked by
8571 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008573 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 */
8575 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008576 return mMeasuredHeight & MEASURED_SIZE_MASK;
8577 }
8578
8579 /**
8580 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008581 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008582 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8583 * This should be used during measurement and layout calculations only. Use
8584 * {@link #getHeight()} to see how wide a view is after layout.
8585 *
8586 * @return The measured width of this view as a bit mask.
8587 */
8588 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 return mMeasuredHeight;
8590 }
8591
8592 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008593 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8594 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8595 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8596 * and the height component is at the shifted bits
8597 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8598 */
8599 public final int getMeasuredState() {
8600 return (mMeasuredWidth&MEASURED_STATE_MASK)
8601 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8602 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8603 }
8604
8605 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008606 * The transform matrix of this view, which is calculated based on the current
8607 * roation, scale, and pivot properties.
8608 *
8609 * @see #getRotation()
8610 * @see #getScaleX()
8611 * @see #getScaleY()
8612 * @see #getPivotX()
8613 * @see #getPivotY()
8614 * @return The current transform matrix for the view
8615 */
8616 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008617 if (mTransformationInfo != null) {
8618 updateMatrix();
8619 return mTransformationInfo.mMatrix;
8620 }
8621 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008622 }
8623
8624 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008625 * Utility function to determine if the value is far enough away from zero to be
8626 * considered non-zero.
8627 * @param value A floating point value to check for zero-ness
8628 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8629 */
8630 private static boolean nonzero(float value) {
8631 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8632 }
8633
8634 /**
Jeff Brown86671742010-09-30 20:00:15 -07008635 * Returns true if the transform matrix is the identity matrix.
8636 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008637 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008638 * @return True if the transform matrix is the identity matrix, false otherwise.
8639 */
Jeff Brown86671742010-09-30 20:00:15 -07008640 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008641 if (mTransformationInfo != null) {
8642 updateMatrix();
8643 return mTransformationInfo.mMatrixIsIdentity;
8644 }
8645 return true;
8646 }
8647
8648 void ensureTransformationInfo() {
8649 if (mTransformationInfo == null) {
8650 mTransformationInfo = new TransformationInfo();
8651 }
Jeff Brown86671742010-09-30 20:00:15 -07008652 }
8653
8654 /**
8655 * Recomputes the transform matrix if necessary.
8656 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008657 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008658 final TransformationInfo info = mTransformationInfo;
8659 if (info == null) {
8660 return;
8661 }
8662 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008663 // transform-related properties have changed since the last time someone
8664 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008665
8666 // Figure out if we need to update the pivot point
8667 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008668 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8669 info.mPrevWidth = mRight - mLeft;
8670 info.mPrevHeight = mBottom - mTop;
8671 info.mPivotX = info.mPrevWidth / 2f;
8672 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008673 }
8674 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008675 info.mMatrix.reset();
8676 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8677 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8678 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8679 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008680 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008681 if (info.mCamera == null) {
8682 info.mCamera = new Camera();
8683 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008684 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008685 info.mCamera.save();
8686 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8687 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8688 info.mCamera.getMatrix(info.matrix3D);
8689 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8690 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8691 info.mPivotY + info.mTranslationY);
8692 info.mMatrix.postConcat(info.matrix3D);
8693 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008694 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008695 info.mMatrixDirty = false;
8696 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8697 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008698 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008699 }
8700
8701 /**
8702 * Utility method to retrieve the inverse of the current mMatrix property.
8703 * We cache the matrix to avoid recalculating it when transform properties
8704 * have not changed.
8705 *
8706 * @return The inverse of the current matrix of this view.
8707 */
Jeff Brown86671742010-09-30 20:00:15 -07008708 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008709 final TransformationInfo info = mTransformationInfo;
8710 if (info != null) {
8711 updateMatrix();
8712 if (info.mInverseMatrixDirty) {
8713 if (info.mInverseMatrix == null) {
8714 info.mInverseMatrix = new Matrix();
8715 }
8716 info.mMatrix.invert(info.mInverseMatrix);
8717 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008718 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008719 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008720 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008721 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008722 }
8723
8724 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008725 * Gets the distance along the Z axis from the camera to this view.
8726 *
8727 * @see #setCameraDistance(float)
8728 *
8729 * @return The distance along the Z axis.
8730 */
8731 public float getCameraDistance() {
8732 ensureTransformationInfo();
8733 final float dpi = mResources.getDisplayMetrics().densityDpi;
8734 final TransformationInfo info = mTransformationInfo;
8735 if (info.mCamera == null) {
8736 info.mCamera = new Camera();
8737 info.matrix3D = new Matrix();
8738 }
8739 return -(info.mCamera.getLocationZ() * dpi);
8740 }
8741
8742 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008743 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8744 * views are drawn) from the camera to this view. The camera's distance
8745 * affects 3D transformations, for instance rotations around the X and Y
8746 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008747 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008748 * use a camera distance that's greater than the height (X axis rotation) or
8749 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008750 *
Romain Guya5364ee2011-02-24 14:46:04 -08008751 * <p>The distance of the camera from the view plane can have an affect on the
8752 * perspective distortion of the view when it is rotated around the x or y axis.
8753 * For example, a large distance will result in a large viewing angle, and there
8754 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008755 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008756 * also result in some drawing artifacts if the rotated view ends up partially
8757 * behind the camera (which is why the recommendation is to use a distance at
8758 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008759 *
Romain Guya5364ee2011-02-24 14:46:04 -08008760 * <p>The distance is expressed in "depth pixels." The default distance depends
8761 * on the screen density. For instance, on a medium density display, the
8762 * default distance is 1280. On a high density display, the default distance
8763 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008764 *
Romain Guya5364ee2011-02-24 14:46:04 -08008765 * <p>If you want to specify a distance that leads to visually consistent
8766 * results across various densities, use the following formula:</p>
8767 * <pre>
8768 * float scale = context.getResources().getDisplayMetrics().density;
8769 * view.setCameraDistance(distance * scale);
8770 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008771 *
Romain Guya5364ee2011-02-24 14:46:04 -08008772 * <p>The density scale factor of a high density display is 1.5,
8773 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008774 *
Romain Guya5364ee2011-02-24 14:46:04 -08008775 * @param distance The distance in "depth pixels", if negative the opposite
8776 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008777 *
8778 * @see #setRotationX(float)
8779 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008780 */
8781 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008782 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008783
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008784 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008785 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008786 final TransformationInfo info = mTransformationInfo;
8787 if (info.mCamera == null) {
8788 info.mCamera = new Camera();
8789 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008790 }
8791
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008792 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8793 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008794
Chet Haase9d1992d2012-03-13 11:03:25 -07008795 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008796 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008797 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008798 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008799 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8800 // View was rejected last time it was drawn by its parent; this may have changed
8801 invalidateParentIfNeeded();
8802 }
Romain Guya5364ee2011-02-24 14:46:04 -08008803 }
8804
8805 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008806 * The degrees that the view is rotated around the pivot point.
8807 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008808 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008809 * @see #getPivotX()
8810 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008811 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008812 * @return The degrees of rotation.
8813 */
Chet Haasea5531132012-02-02 13:41:44 -08008814 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008815 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008816 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008817 }
8818
8819 /**
Chet Haase897247b2010-09-09 14:54:47 -07008820 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8821 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008822 *
8823 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008824 *
8825 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008826 * @see #getPivotX()
8827 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008828 * @see #setRotationX(float)
8829 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008830 *
8831 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008832 */
8833 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008834 ensureTransformationInfo();
8835 final TransformationInfo info = mTransformationInfo;
8836 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008837 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008838 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008839 info.mRotation = rotation;
8840 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008841 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008842 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008843 mDisplayList.setRotation(rotation);
8844 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008845 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8846 // View was rejected last time it was drawn by its parent; this may have changed
8847 invalidateParentIfNeeded();
8848 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008849 }
8850 }
8851
8852 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008853 * The degrees that the view is rotated around the vertical axis through the pivot point.
8854 *
8855 * @see #getPivotX()
8856 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008857 * @see #setRotationY(float)
8858 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008859 * @return The degrees of Y rotation.
8860 */
Chet Haasea5531132012-02-02 13:41:44 -08008861 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008862 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008863 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008864 }
8865
8866 /**
Chet Haase897247b2010-09-09 14:54:47 -07008867 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8868 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8869 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008870 *
Romain Guya5364ee2011-02-24 14:46:04 -08008871 * When rotating large views, it is recommended to adjust the camera distance
8872 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008873 *
8874 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008875 *
8876 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008877 * @see #getPivotX()
8878 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008879 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008880 * @see #setRotationX(float)
8881 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008882 *
8883 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008884 */
8885 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008886 ensureTransformationInfo();
8887 final TransformationInfo info = mTransformationInfo;
8888 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008889 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008890 info.mRotationY = rotationY;
8891 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008892 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008893 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008894 mDisplayList.setRotationY(rotationY);
8895 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008896 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8897 // View was rejected last time it was drawn by its parent; this may have changed
8898 invalidateParentIfNeeded();
8899 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008900 }
8901 }
8902
8903 /**
8904 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8905 *
8906 * @see #getPivotX()
8907 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008908 * @see #setRotationX(float)
8909 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008910 * @return The degrees of X rotation.
8911 */
Chet Haasea5531132012-02-02 13:41:44 -08008912 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008913 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008914 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008915 }
8916
8917 /**
Chet Haase897247b2010-09-09 14:54:47 -07008918 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8919 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8920 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008921 *
Romain Guya5364ee2011-02-24 14:46:04 -08008922 * When rotating large views, it is recommended to adjust the camera distance
8923 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008924 *
8925 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008926 *
8927 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008928 * @see #getPivotX()
8929 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008930 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008931 * @see #setRotationY(float)
8932 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008933 *
8934 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008935 */
8936 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008937 ensureTransformationInfo();
8938 final TransformationInfo info = mTransformationInfo;
8939 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008940 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008941 info.mRotationX = rotationX;
8942 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008943 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008944 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008945 mDisplayList.setRotationX(rotationX);
8946 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008947 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8948 // View was rejected last time it was drawn by its parent; this may have changed
8949 invalidateParentIfNeeded();
8950 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008951 }
8952 }
8953
8954 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008955 * The amount that the view is scaled in x around the pivot point, as a proportion of
8956 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8957 *
Joe Onorato93162322010-09-16 15:42:01 -04008958 * <p>By default, this is 1.0f.
8959 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008960 * @see #getPivotX()
8961 * @see #getPivotY()
8962 * @return The scaling factor.
8963 */
Chet Haasea5531132012-02-02 13:41:44 -08008964 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008965 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008966 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008967 }
8968
8969 /**
8970 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8971 * the view's unscaled width. A value of 1 means that no scaling is applied.
8972 *
8973 * @param scaleX The scaling factor.
8974 * @see #getPivotX()
8975 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008976 *
8977 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008978 */
8979 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008980 ensureTransformationInfo();
8981 final TransformationInfo info = mTransformationInfo;
8982 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008983 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008984 info.mScaleX = scaleX;
8985 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008986 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008987 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008988 mDisplayList.setScaleX(scaleX);
8989 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008990 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8991 // View was rejected last time it was drawn by its parent; this may have changed
8992 invalidateParentIfNeeded();
8993 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008994 }
8995 }
8996
8997 /**
8998 * The amount that the view is scaled in y around the pivot point, as a proportion of
8999 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9000 *
Joe Onorato93162322010-09-16 15:42:01 -04009001 * <p>By default, this is 1.0f.
9002 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009003 * @see #getPivotX()
9004 * @see #getPivotY()
9005 * @return The scaling factor.
9006 */
Chet Haasea5531132012-02-02 13:41:44 -08009007 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009009 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009010 }
9011
9012 /**
9013 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9014 * the view's unscaled width. A value of 1 means that no scaling is applied.
9015 *
9016 * @param scaleY The scaling factor.
9017 * @see #getPivotX()
9018 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009019 *
9020 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009021 */
9022 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 ensureTransformationInfo();
9024 final TransformationInfo info = mTransformationInfo;
9025 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009026 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009027 info.mScaleY = scaleY;
9028 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009029 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009030 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009031 mDisplayList.setScaleY(scaleY);
9032 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009033 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9034 // View was rejected last time it was drawn by its parent; this may have changed
9035 invalidateParentIfNeeded();
9036 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009037 }
9038 }
9039
9040 /**
9041 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9042 * and {@link #setScaleX(float) scaled}.
9043 *
9044 * @see #getRotation()
9045 * @see #getScaleX()
9046 * @see #getScaleY()
9047 * @see #getPivotY()
9048 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009049 *
9050 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009051 */
Chet Haasea5531132012-02-02 13:41:44 -08009052 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009053 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009054 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009055 }
9056
9057 /**
9058 * Sets the x location of the point around which the view is
9059 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009060 * By default, the pivot point is centered on the object.
9061 * Setting this property disables this behavior and causes the view to use only the
9062 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009063 *
9064 * @param pivotX The x location of the pivot point.
9065 * @see #getRotation()
9066 * @see #getScaleX()
9067 * @see #getScaleY()
9068 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009069 *
9070 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009071 */
9072 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009073 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009074 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009075 final TransformationInfo info = mTransformationInfo;
9076 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009077 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009078 info.mPivotX = pivotX;
9079 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009080 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009081 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009082 mDisplayList.setPivotX(pivotX);
9083 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009084 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9085 // View was rejected last time it was drawn by its parent; this may have changed
9086 invalidateParentIfNeeded();
9087 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009088 }
9089 }
9090
9091 /**
9092 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9093 * and {@link #setScaleY(float) scaled}.
9094 *
9095 * @see #getRotation()
9096 * @see #getScaleX()
9097 * @see #getScaleY()
9098 * @see #getPivotY()
9099 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009100 *
9101 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009102 */
Chet Haasea5531132012-02-02 13:41:44 -08009103 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009104 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009105 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009106 }
9107
9108 /**
9109 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009110 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9111 * Setting this property disables this behavior and causes the view to use only the
9112 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009113 *
9114 * @param pivotY The y location of the pivot point.
9115 * @see #getRotation()
9116 * @see #getScaleX()
9117 * @see #getScaleY()
9118 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009119 *
9120 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009121 */
9122 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009123 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009124 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009125 final TransformationInfo info = mTransformationInfo;
9126 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009127 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009128 info.mPivotY = pivotY;
9129 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009130 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009131 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009132 mDisplayList.setPivotY(pivotY);
9133 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009134 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9135 // View was rejected last time it was drawn by its parent; this may have changed
9136 invalidateParentIfNeeded();
9137 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009138 }
9139 }
9140
9141 /**
9142 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9143 * completely transparent and 1 means the view is completely opaque.
9144 *
Joe Onorato93162322010-09-16 15:42:01 -04009145 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009146 * @return The opacity of the view.
9147 */
Chet Haasea5531132012-02-02 13:41:44 -08009148 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009149 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009151 }
9152
9153 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009154 * Returns whether this View has content which overlaps. This function, intended to be
9155 * overridden by specific View types, is an optimization when alpha is set on a view. If
9156 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9157 * and then composited it into place, which can be expensive. If the view has no overlapping
9158 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9159 * An example of overlapping rendering is a TextView with a background image, such as a
9160 * Button. An example of non-overlapping rendering is a TextView with no background, or
9161 * an ImageView with only the foreground image. The default implementation returns true;
9162 * subclasses should override if they have cases which can be optimized.
9163 *
9164 * @return true if the content in this view might overlap, false otherwise.
9165 */
9166 public boolean hasOverlappingRendering() {
9167 return true;
9168 }
9169
9170 /**
Romain Guy171c5922011-01-06 10:04:23 -08009171 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9172 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009173 *
Romain Guy171c5922011-01-06 10:04:23 -08009174 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9175 * responsible for applying the opacity itself. Otherwise, calling this method is
9176 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009177 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009178 *
Chet Haasea5531132012-02-02 13:41:44 -08009179 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9180 * performance implications. It is generally best to use the alpha property sparingly and
9181 * transiently, as in the case of fading animations.</p>
9182 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009183 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009184 *
Joe Malin32736f02011-01-19 16:14:20 -08009185 * @see #setLayerType(int, android.graphics.Paint)
9186 *
Chet Haase73066682010-11-29 15:55:32 -08009187 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009188 */
9189 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009190 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009191 if (mTransformationInfo.mAlpha != alpha) {
9192 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009193 if (onSetAlpha((int) (alpha * 255))) {
9194 mPrivateFlags |= ALPHA_SET;
9195 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009196 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009197 invalidate(true);
9198 } else {
9199 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009200 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009201 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009202 mDisplayList.setAlpha(alpha);
9203 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009204 }
Chet Haaseed032702010-10-01 14:05:54 -07009205 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009206 }
9207
9208 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009209 * Faster version of setAlpha() which performs the same steps except there are
9210 * no calls to invalidate(). The caller of this function should perform proper invalidation
9211 * on the parent and this object. The return value indicates whether the subclass handles
9212 * alpha (the return value for onSetAlpha()).
9213 *
9214 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009215 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9216 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009217 */
9218 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009219 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009220 if (mTransformationInfo.mAlpha != alpha) {
9221 mTransformationInfo.mAlpha = alpha;
9222 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9223 if (subclassHandlesAlpha) {
9224 mPrivateFlags |= ALPHA_SET;
9225 return true;
9226 } else {
9227 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009228 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009229 mDisplayList.setAlpha(alpha);
9230 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009231 }
Chet Haasea00f3862011-02-22 06:34:40 -08009232 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009233 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009234 }
9235
9236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 * Top position of this view relative to its parent.
9238 *
9239 * @return The top of this view, in pixels.
9240 */
9241 @ViewDebug.CapturedViewProperty
9242 public final int getTop() {
9243 return mTop;
9244 }
9245
9246 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009247 * Sets the top position of this view relative to its parent. This method is meant to be called
9248 * by the layout system and should not generally be called otherwise, because the property
9249 * may be changed at any time by the layout.
9250 *
9251 * @param top The top of this view, in pixels.
9252 */
9253 public final void setTop(int top) {
9254 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009255 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009256 final boolean matrixIsIdentity = mTransformationInfo == null
9257 || mTransformationInfo.mMatrixIsIdentity;
9258 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009259 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009260 int minTop;
9261 int yLoc;
9262 if (top < mTop) {
9263 minTop = top;
9264 yLoc = top - mTop;
9265 } else {
9266 minTop = mTop;
9267 yLoc = 0;
9268 }
Chet Haasee9140a72011-02-16 16:23:29 -08009269 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009270 }
9271 } else {
9272 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009273 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009274 }
9275
Chet Haaseed032702010-10-01 14:05:54 -07009276 int width = mRight - mLeft;
9277 int oldHeight = mBottom - mTop;
9278
Chet Haase21cd1382010-09-01 17:42:29 -07009279 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009280 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009281 mDisplayList.setTop(mTop);
9282 }
Chet Haase21cd1382010-09-01 17:42:29 -07009283
Chet Haaseed032702010-10-01 14:05:54 -07009284 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9285
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009286 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009287 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9288 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009289 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009290 }
Chet Haase21cd1382010-09-01 17:42:29 -07009291 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009292 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009293 }
Chet Haase55dbb652010-12-21 20:15:08 -08009294 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009295 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009296 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9297 // View was rejected last time it was drawn by its parent; this may have changed
9298 invalidateParentIfNeeded();
9299 }
Chet Haase21cd1382010-09-01 17:42:29 -07009300 }
9301 }
9302
9303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009304 * Bottom position of this view relative to its parent.
9305 *
9306 * @return The bottom of this view, in pixels.
9307 */
9308 @ViewDebug.CapturedViewProperty
9309 public final int getBottom() {
9310 return mBottom;
9311 }
9312
9313 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009314 * True if this view has changed since the last time being drawn.
9315 *
9316 * @return The dirty state of this view.
9317 */
9318 public boolean isDirty() {
9319 return (mPrivateFlags & DIRTY_MASK) != 0;
9320 }
9321
9322 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009323 * Sets the bottom position of this view relative to its parent. This method is meant to be
9324 * called by the layout system and should not generally be called otherwise, because the
9325 * property may be changed at any time by the layout.
9326 *
9327 * @param bottom The bottom of this view, in pixels.
9328 */
9329 public final void setBottom(int bottom) {
9330 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009331 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009332 final boolean matrixIsIdentity = mTransformationInfo == null
9333 || mTransformationInfo.mMatrixIsIdentity;
9334 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009335 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009336 int maxBottom;
9337 if (bottom < mBottom) {
9338 maxBottom = mBottom;
9339 } else {
9340 maxBottom = bottom;
9341 }
Chet Haasee9140a72011-02-16 16:23:29 -08009342 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009343 }
9344 } else {
9345 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009346 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009347 }
9348
Chet Haaseed032702010-10-01 14:05:54 -07009349 int width = mRight - mLeft;
9350 int oldHeight = mBottom - mTop;
9351
Chet Haase21cd1382010-09-01 17:42:29 -07009352 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009353 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009354 mDisplayList.setBottom(mBottom);
9355 }
Chet Haase21cd1382010-09-01 17:42:29 -07009356
Chet Haaseed032702010-10-01 14:05:54 -07009357 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9358
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009359 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009360 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9361 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009362 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009363 }
Chet Haase21cd1382010-09-01 17:42:29 -07009364 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009365 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009366 }
Chet Haase55dbb652010-12-21 20:15:08 -08009367 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009368 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009369 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9370 // View was rejected last time it was drawn by its parent; this may have changed
9371 invalidateParentIfNeeded();
9372 }
Chet Haase21cd1382010-09-01 17:42:29 -07009373 }
9374 }
9375
9376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 * Left position of this view relative to its parent.
9378 *
9379 * @return The left edge of this view, in pixels.
9380 */
9381 @ViewDebug.CapturedViewProperty
9382 public final int getLeft() {
9383 return mLeft;
9384 }
9385
9386 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009387 * Sets the left position of this view relative to its parent. This method is meant to be called
9388 * by the layout system and should not generally be called otherwise, because the property
9389 * may be changed at any time by the layout.
9390 *
9391 * @param left The bottom of this view, in pixels.
9392 */
9393 public final void setLeft(int left) {
9394 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009395 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009396 final boolean matrixIsIdentity = mTransformationInfo == null
9397 || mTransformationInfo.mMatrixIsIdentity;
9398 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009399 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009400 int minLeft;
9401 int xLoc;
9402 if (left < mLeft) {
9403 minLeft = left;
9404 xLoc = left - mLeft;
9405 } else {
9406 minLeft = mLeft;
9407 xLoc = 0;
9408 }
Chet Haasee9140a72011-02-16 16:23:29 -08009409 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009410 }
9411 } else {
9412 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009413 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009414 }
9415
Chet Haaseed032702010-10-01 14:05:54 -07009416 int oldWidth = mRight - mLeft;
9417 int height = mBottom - mTop;
9418
Chet Haase21cd1382010-09-01 17:42:29 -07009419 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009420 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009421 mDisplayList.setLeft(left);
9422 }
Chet Haase21cd1382010-09-01 17:42:29 -07009423
Chet Haaseed032702010-10-01 14:05:54 -07009424 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9425
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009426 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009427 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9428 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009429 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009430 }
Chet Haase21cd1382010-09-01 17:42:29 -07009431 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009432 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009433 }
Chet Haase55dbb652010-12-21 20:15:08 -08009434 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009435 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009436 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9437 // View was rejected last time it was drawn by its parent; this may have changed
9438 invalidateParentIfNeeded();
9439 }
Chet Haase21cd1382010-09-01 17:42:29 -07009440 }
9441 }
9442
9443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009444 * Right position of this view relative to its parent.
9445 *
9446 * @return The right edge of this view, in pixels.
9447 */
9448 @ViewDebug.CapturedViewProperty
9449 public final int getRight() {
9450 return mRight;
9451 }
9452
9453 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009454 * Sets the right position of this view relative to its parent. This method is meant to be called
9455 * by the layout system and should not generally be called otherwise, because the property
9456 * may be changed at any time by the layout.
9457 *
9458 * @param right The bottom of this view, in pixels.
9459 */
9460 public final void setRight(int right) {
9461 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009462 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009463 final boolean matrixIsIdentity = mTransformationInfo == null
9464 || mTransformationInfo.mMatrixIsIdentity;
9465 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009466 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009467 int maxRight;
9468 if (right < mRight) {
9469 maxRight = mRight;
9470 } else {
9471 maxRight = right;
9472 }
Chet Haasee9140a72011-02-16 16:23:29 -08009473 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009474 }
9475 } else {
9476 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009477 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009478 }
9479
Chet Haaseed032702010-10-01 14:05:54 -07009480 int oldWidth = mRight - mLeft;
9481 int height = mBottom - mTop;
9482
Chet Haase21cd1382010-09-01 17:42:29 -07009483 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009484 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009485 mDisplayList.setRight(mRight);
9486 }
Chet Haase21cd1382010-09-01 17:42:29 -07009487
Chet Haaseed032702010-10-01 14:05:54 -07009488 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9489
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009490 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009491 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9492 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009493 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009494 }
Chet Haase21cd1382010-09-01 17:42:29 -07009495 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009496 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009497 }
Chet Haase55dbb652010-12-21 20:15:08 -08009498 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009499 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009500 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9501 // View was rejected last time it was drawn by its parent; this may have changed
9502 invalidateParentIfNeeded();
9503 }
Chet Haase21cd1382010-09-01 17:42:29 -07009504 }
9505 }
9506
9507 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009508 * The visual x position of this view, in pixels. This is equivalent to the
9509 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009510 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009511 *
Chet Haasedf030d22010-07-30 17:22:38 -07009512 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009513 */
Chet Haasea5531132012-02-02 13:41:44 -08009514 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009515 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009516 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009517 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009518
Chet Haasedf030d22010-07-30 17:22:38 -07009519 /**
9520 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9521 * {@link #setTranslationX(float) translationX} property to be the difference between
9522 * the x value passed in and the current {@link #getLeft() left} property.
9523 *
9524 * @param x The visual x position of this view, in pixels.
9525 */
9526 public void setX(float x) {
9527 setTranslationX(x - mLeft);
9528 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009529
Chet Haasedf030d22010-07-30 17:22:38 -07009530 /**
9531 * The visual y position of this view, in pixels. This is equivalent to the
9532 * {@link #setTranslationY(float) translationY} property plus the current
9533 * {@link #getTop() top} property.
9534 *
9535 * @return The visual y position of this view, in pixels.
9536 */
Chet Haasea5531132012-02-02 13:41:44 -08009537 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009538 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009539 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009540 }
9541
9542 /**
9543 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9544 * {@link #setTranslationY(float) translationY} property to be the difference between
9545 * the y value passed in and the current {@link #getTop() top} property.
9546 *
9547 * @param y The visual y position of this view, in pixels.
9548 */
9549 public void setY(float y) {
9550 setTranslationY(y - mTop);
9551 }
9552
9553
9554 /**
9555 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9556 * This position is post-layout, in addition to wherever the object's
9557 * layout placed it.
9558 *
9559 * @return The horizontal position of this view relative to its left position, in pixels.
9560 */
Chet Haasea5531132012-02-02 13:41:44 -08009561 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009562 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009563 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009564 }
9565
9566 /**
9567 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9568 * This effectively positions the object post-layout, in addition to wherever the object's
9569 * layout placed it.
9570 *
9571 * @param translationX The horizontal position of this view relative to its left position,
9572 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009573 *
9574 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009575 */
9576 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009577 ensureTransformationInfo();
9578 final TransformationInfo info = mTransformationInfo;
9579 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009580 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009581 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009582 info.mTranslationX = translationX;
9583 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009584 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009585 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009586 mDisplayList.setTranslationX(translationX);
9587 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009588 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9589 // View was rejected last time it was drawn by its parent; this may have changed
9590 invalidateParentIfNeeded();
9591 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009592 }
9593 }
9594
9595 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009596 * The horizontal location of this view relative to its {@link #getTop() top} position.
9597 * This position is post-layout, in addition to wherever the object's
9598 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009599 *
Chet Haasedf030d22010-07-30 17:22:38 -07009600 * @return The vertical position of this view relative to its top position,
9601 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009602 */
Chet Haasea5531132012-02-02 13:41:44 -08009603 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009604 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009605 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009606 }
9607
9608 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009609 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9610 * This effectively positions the object post-layout, in addition to wherever the object's
9611 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009612 *
Chet Haasedf030d22010-07-30 17:22:38 -07009613 * @param translationY The vertical position of this view relative to its top position,
9614 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009615 *
9616 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009617 */
Chet Haasedf030d22010-07-30 17:22:38 -07009618 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009619 ensureTransformationInfo();
9620 final TransformationInfo info = mTransformationInfo;
9621 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009622 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009623 info.mTranslationY = translationY;
9624 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009625 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009626 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009627 mDisplayList.setTranslationY(translationY);
9628 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009629 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9630 // View was rejected last time it was drawn by its parent; this may have changed
9631 invalidateParentIfNeeded();
9632 }
Chet Haasedf030d22010-07-30 17:22:38 -07009633 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009634 }
9635
9636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 * Hit rectangle in parent's coordinates
9638 *
9639 * @param outRect The hit rectangle of the view.
9640 */
9641 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009642 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009643 final TransformationInfo info = mTransformationInfo;
9644 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009645 outRect.set(mLeft, mTop, mRight, mBottom);
9646 } else {
9647 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009648 tmpRect.set(-info.mPivotX, -info.mPivotY,
9649 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9650 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009651 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9652 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009653 }
9654 }
9655
9656 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009657 * Determines whether the given point, in local coordinates is inside the view.
9658 */
9659 /*package*/ final boolean pointInView(float localX, float localY) {
9660 return localX >= 0 && localX < (mRight - mLeft)
9661 && localY >= 0 && localY < (mBottom - mTop);
9662 }
9663
9664 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009665 * Utility method to determine whether the given point, in local coordinates,
9666 * is inside the view, where the area of the view is expanded by the slop factor.
9667 * This method is called while processing touch-move events to determine if the event
9668 * is still within the view.
9669 */
9670 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009671 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009672 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009673 }
9674
9675 /**
9676 * When a view has focus and the user navigates away from it, the next view is searched for
9677 * starting from the rectangle filled in by this method.
9678 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009679 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9680 * of the view. However, if your view maintains some idea of internal selection,
9681 * such as a cursor, or a selected row or column, you should override this method and
9682 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 *
9684 * @param r The rectangle to fill in, in this view's coordinates.
9685 */
9686 public void getFocusedRect(Rect r) {
9687 getDrawingRect(r);
9688 }
9689
9690 /**
9691 * If some part of this view is not clipped by any of its parents, then
9692 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009693 * coordinates (without taking possible View rotations into account), offset
9694 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9695 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 *
9697 * @param r If true is returned, r holds the global coordinates of the
9698 * visible portion of this view.
9699 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9700 * between this view and its root. globalOffet may be null.
9701 * @return true if r is non-empty (i.e. part of the view is visible at the
9702 * root level.
9703 */
9704 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9705 int width = mRight - mLeft;
9706 int height = mBottom - mTop;
9707 if (width > 0 && height > 0) {
9708 r.set(0, 0, width, height);
9709 if (globalOffset != null) {
9710 globalOffset.set(-mScrollX, -mScrollY);
9711 }
9712 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9713 }
9714 return false;
9715 }
9716
9717 public final boolean getGlobalVisibleRect(Rect r) {
9718 return getGlobalVisibleRect(r, null);
9719 }
9720
9721 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009722 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 if (getGlobalVisibleRect(r, offset)) {
9724 r.offset(-offset.x, -offset.y); // make r local
9725 return true;
9726 }
9727 return false;
9728 }
9729
9730 /**
9731 * Offset this view's vertical location by the specified number of pixels.
9732 *
9733 * @param offset the number of pixels to offset the view by
9734 */
9735 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009736 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009737 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009738 final boolean matrixIsIdentity = mTransformationInfo == null
9739 || mTransformationInfo.mMatrixIsIdentity;
9740 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009741 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009742 invalidateViewProperty(false, false);
9743 } else {
9744 final ViewParent p = mParent;
9745 if (p != null && mAttachInfo != null) {
9746 final Rect r = mAttachInfo.mTmpInvalRect;
9747 int minTop;
9748 int maxBottom;
9749 int yLoc;
9750 if (offset < 0) {
9751 minTop = mTop + offset;
9752 maxBottom = mBottom;
9753 yLoc = offset;
9754 } else {
9755 minTop = mTop;
9756 maxBottom = mBottom + offset;
9757 yLoc = 0;
9758 }
9759 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9760 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009761 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009762 }
9763 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009764 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009765 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009766
Chet Haasec3aa3612010-06-17 08:50:37 -07009767 mTop += offset;
9768 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009769 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009770 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009771 invalidateViewProperty(false, false);
9772 } else {
9773 if (!matrixIsIdentity) {
9774 invalidateViewProperty(false, true);
9775 }
9776 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009777 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 }
9780
9781 /**
9782 * Offset this view's horizontal location by the specified amount of pixels.
9783 *
9784 * @param offset the numer of pixels to offset the view by
9785 */
9786 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009787 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009788 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009789 final boolean matrixIsIdentity = mTransformationInfo == null
9790 || mTransformationInfo.mMatrixIsIdentity;
9791 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009792 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009793 invalidateViewProperty(false, false);
9794 } else {
9795 final ViewParent p = mParent;
9796 if (p != null && mAttachInfo != null) {
9797 final Rect r = mAttachInfo.mTmpInvalRect;
9798 int minLeft;
9799 int maxRight;
9800 if (offset < 0) {
9801 minLeft = mLeft + offset;
9802 maxRight = mRight;
9803 } else {
9804 minLeft = mLeft;
9805 maxRight = mRight + offset;
9806 }
9807 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9808 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009809 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009810 }
9811 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009812 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009813 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009814
Chet Haasec3aa3612010-06-17 08:50:37 -07009815 mLeft += offset;
9816 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009817 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009818 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009819 invalidateViewProperty(false, false);
9820 } else {
9821 if (!matrixIsIdentity) {
9822 invalidateViewProperty(false, true);
9823 }
9824 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009825 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 }
9828
9829 /**
9830 * Get the LayoutParams associated with this view. All views should have
9831 * layout parameters. These supply parameters to the <i>parent</i> of this
9832 * view specifying how it should be arranged. There are many subclasses of
9833 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9834 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009835 *
9836 * This method may return null if this View is not attached to a parent
9837 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9838 * was not invoked successfully. When a View is attached to a parent
9839 * ViewGroup, this method must not return null.
9840 *
9841 * @return The LayoutParams associated with this view, or null if no
9842 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009843 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009844 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 public ViewGroup.LayoutParams getLayoutParams() {
9846 return mLayoutParams;
9847 }
9848
9849 /**
9850 * Set the layout parameters associated with this view. These supply
9851 * parameters to the <i>parent</i> of this view specifying how it should be
9852 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9853 * correspond to the different subclasses of ViewGroup that are responsible
9854 * for arranging their children.
9855 *
Romain Guy01c174b2011-02-22 11:51:06 -08009856 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009857 */
9858 public void setLayoutParams(ViewGroup.LayoutParams params) {
9859 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009860 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 }
9862 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009863 if (mParent instanceof ViewGroup) {
9864 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 requestLayout();
9867 }
9868
9869 /**
9870 * Set the scrolled position of your view. This will cause a call to
9871 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9872 * invalidated.
9873 * @param x the x position to scroll to
9874 * @param y the y position to scroll to
9875 */
9876 public void scrollTo(int x, int y) {
9877 if (mScrollX != x || mScrollY != y) {
9878 int oldX = mScrollX;
9879 int oldY = mScrollY;
9880 mScrollX = x;
9881 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009882 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009884 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009885 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 }
9888 }
9889
9890 /**
9891 * Move the scrolled position of your view. This will cause a call to
9892 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9893 * invalidated.
9894 * @param x the amount of pixels to scroll by horizontally
9895 * @param y the amount of pixels to scroll by vertically
9896 */
9897 public void scrollBy(int x, int y) {
9898 scrollTo(mScrollX + x, mScrollY + y);
9899 }
9900
9901 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009902 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9903 * animation to fade the scrollbars out after a default delay. If a subclass
9904 * provides animated scrolling, the start delay should equal the duration
9905 * of the scrolling animation.</p>
9906 *
9907 * <p>The animation starts only if at least one of the scrollbars is
9908 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9909 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9910 * this method returns true, and false otherwise. If the animation is
9911 * started, this method calls {@link #invalidate()}; in that case the
9912 * caller should not call {@link #invalidate()}.</p>
9913 *
9914 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009915 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009916 *
9917 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9918 * and {@link #scrollTo(int, int)}.</p>
9919 *
9920 * @return true if the animation is played, false otherwise
9921 *
9922 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009923 * @see #scrollBy(int, int)
9924 * @see #scrollTo(int, int)
9925 * @see #isHorizontalScrollBarEnabled()
9926 * @see #isVerticalScrollBarEnabled()
9927 * @see #setHorizontalScrollBarEnabled(boolean)
9928 * @see #setVerticalScrollBarEnabled(boolean)
9929 */
9930 protected boolean awakenScrollBars() {
9931 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009932 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009933 }
9934
9935 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009936 * Trigger the scrollbars to draw.
9937 * This method differs from awakenScrollBars() only in its default duration.
9938 * initialAwakenScrollBars() will show the scroll bars for longer than
9939 * usual to give the user more of a chance to notice them.
9940 *
9941 * @return true if the animation is played, false otherwise.
9942 */
9943 private boolean initialAwakenScrollBars() {
9944 return mScrollCache != null &&
9945 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9946 }
9947
9948 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009949 * <p>
9950 * Trigger the scrollbars to draw. When invoked this method starts an
9951 * animation to fade the scrollbars out after a fixed delay. If a subclass
9952 * provides animated scrolling, the start delay should equal the duration of
9953 * the scrolling animation.
9954 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009955 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009956 * <p>
9957 * The animation starts only if at least one of the scrollbars is enabled,
9958 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9959 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9960 * this method returns true, and false otherwise. If the animation is
9961 * started, this method calls {@link #invalidate()}; in that case the caller
9962 * should not call {@link #invalidate()}.
9963 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009964 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009965 * <p>
9966 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009967 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009968 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009969 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009970 * @param startDelay the delay, in milliseconds, after which the animation
9971 * should start; when the delay is 0, the animation starts
9972 * immediately
9973 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009974 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009975 * @see #scrollBy(int, int)
9976 * @see #scrollTo(int, int)
9977 * @see #isHorizontalScrollBarEnabled()
9978 * @see #isVerticalScrollBarEnabled()
9979 * @see #setHorizontalScrollBarEnabled(boolean)
9980 * @see #setVerticalScrollBarEnabled(boolean)
9981 */
9982 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009983 return awakenScrollBars(startDelay, true);
9984 }
Joe Malin32736f02011-01-19 16:14:20 -08009985
Mike Cleron290947b2009-09-29 18:34:32 -07009986 /**
9987 * <p>
9988 * Trigger the scrollbars to draw. When invoked this method starts an
9989 * animation to fade the scrollbars out after a fixed delay. If a subclass
9990 * provides animated scrolling, the start delay should equal the duration of
9991 * the scrolling animation.
9992 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009993 *
Mike Cleron290947b2009-09-29 18:34:32 -07009994 * <p>
9995 * The animation starts only if at least one of the scrollbars is enabled,
9996 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9997 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9998 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009999 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010000 * is set to true; in that case the caller
10001 * should not call {@link #invalidate()}.
10002 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010003 *
Mike Cleron290947b2009-09-29 18:34:32 -070010004 * <p>
10005 * This method should be invoked everytime a subclass directly updates the
10006 * scroll parameters.
10007 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010008 *
Mike Cleron290947b2009-09-29 18:34:32 -070010009 * @param startDelay the delay, in milliseconds, after which the animation
10010 * should start; when the delay is 0, the animation starts
10011 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010012 *
Mike Cleron290947b2009-09-29 18:34:32 -070010013 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010014 *
Mike Cleron290947b2009-09-29 18:34:32 -070010015 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010016 *
Mike Cleron290947b2009-09-29 18:34:32 -070010017 * @see #scrollBy(int, int)
10018 * @see #scrollTo(int, int)
10019 * @see #isHorizontalScrollBarEnabled()
10020 * @see #isVerticalScrollBarEnabled()
10021 * @see #setHorizontalScrollBarEnabled(boolean)
10022 * @see #setVerticalScrollBarEnabled(boolean)
10023 */
10024 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010025 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010026
Mike Cleronf116bf82009-09-27 19:14:12 -070010027 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10028 return false;
10029 }
10030
10031 if (scrollCache.scrollBar == null) {
10032 scrollCache.scrollBar = new ScrollBarDrawable();
10033 }
10034
10035 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10036
Mike Cleron290947b2009-09-29 18:34:32 -070010037 if (invalidate) {
10038 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010039 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010040 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010041
10042 if (scrollCache.state == ScrollabilityCache.OFF) {
10043 // FIXME: this is copied from WindowManagerService.
10044 // We should get this value from the system when it
10045 // is possible to do so.
10046 final int KEY_REPEAT_FIRST_DELAY = 750;
10047 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10048 }
10049
10050 // Tell mScrollCache when we should start fading. This may
10051 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010052 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010053 scrollCache.fadeStartTime = fadeStartTime;
10054 scrollCache.state = ScrollabilityCache.ON;
10055
10056 // Schedule our fader to run, unscheduling any old ones first
10057 if (mAttachInfo != null) {
10058 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10059 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10060 }
10061
10062 return true;
10063 }
10064
10065 return false;
10066 }
10067
10068 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010069 * Do not invalidate views which are not visible and which are not running an animation. They
10070 * will not get drawn and they should not set dirty flags as if they will be drawn
10071 */
10072 private boolean skipInvalidate() {
10073 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10074 (!(mParent instanceof ViewGroup) ||
10075 !((ViewGroup) mParent).isViewTransitioning(this));
10076 }
10077 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010078 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010079 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10080 * in the future. This must be called from a UI thread. To call from a non-UI
10081 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 *
10083 * WARNING: This method is destructive to dirty.
10084 * @param dirty the rectangle representing the bounds of the dirty region
10085 */
10086 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010087 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010088 return;
10089 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010090 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010091 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10092 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010093 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010094 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010095 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096 final ViewParent p = mParent;
10097 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010098 //noinspection PointlessBooleanExpression,ConstantConditions
10099 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10100 if (p != null && ai != null && ai.mHardwareAccelerated) {
10101 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010102 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010103 p.invalidateChild(this, null);
10104 return;
10105 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 if (p != null && ai != null) {
10108 final int scrollX = mScrollX;
10109 final int scrollY = mScrollY;
10110 final Rect r = ai.mTmpInvalRect;
10111 r.set(dirty.left - scrollX, dirty.top - scrollY,
10112 dirty.right - scrollX, dirty.bottom - scrollY);
10113 mParent.invalidateChild(this, r);
10114 }
10115 }
10116 }
10117
10118 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010119 * 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 -080010120 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010121 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10122 * will be called at some point in the future. This must be called from
10123 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010124 * @param l the left position of the dirty region
10125 * @param t the top position of the dirty region
10126 * @param r the right position of the dirty region
10127 * @param b the bottom position of the dirty region
10128 */
10129 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010130 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010131 return;
10132 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010133 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010134 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10135 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010137 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010138 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 final ViewParent p = mParent;
10140 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010141 //noinspection PointlessBooleanExpression,ConstantConditions
10142 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10143 if (p != null && ai != null && ai.mHardwareAccelerated) {
10144 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010145 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010146 p.invalidateChild(this, null);
10147 return;
10148 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 if (p != null && ai != null && l < r && t < b) {
10151 final int scrollX = mScrollX;
10152 final int scrollY = mScrollY;
10153 final Rect tmpr = ai.mTmpInvalRect;
10154 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10155 p.invalidateChild(this, tmpr);
10156 }
10157 }
10158 }
10159
10160 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010161 * Invalidate the whole view. If the view is visible,
10162 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10163 * the future. This must be called from a UI thread. To call from a non-UI thread,
10164 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165 */
10166 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010167 invalidate(true);
10168 }
Joe Malin32736f02011-01-19 16:14:20 -080010169
Chet Haaseed032702010-10-01 14:05:54 -070010170 /**
10171 * This is where the invalidate() work actually happens. A full invalidate()
10172 * causes the drawing cache to be invalidated, but this function can be called with
10173 * invalidateCache set to false to skip that invalidation step for cases that do not
10174 * need it (for example, a component that remains at the same dimensions with the same
10175 * content).
10176 *
10177 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10178 * well. This is usually true for a full invalidate, but may be set to false if the
10179 * View's contents or dimensions have not changed.
10180 */
Romain Guy849d0a32011-02-01 17:20:48 -080010181 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010182 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010183 return;
10184 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010185 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010186 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010187 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10188 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010189 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010190 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010191 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010192 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010193 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010196 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010197 //noinspection PointlessBooleanExpression,ConstantConditions
10198 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10199 if (p != null && ai != null && ai.mHardwareAccelerated) {
10200 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010201 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010202 p.invalidateChild(this, null);
10203 return;
10204 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010205 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 if (p != null && ai != null) {
10208 final Rect r = ai.mTmpInvalRect;
10209 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10210 // Don't call invalidate -- we don't want to internally scroll
10211 // our own bounds
10212 p.invalidateChild(this, r);
10213 }
10214 }
10215 }
10216
10217 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010218 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10219 * set any flags or handle all of the cases handled by the default invalidation methods.
10220 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10221 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10222 * walk up the hierarchy, transforming the dirty rect as necessary.
10223 *
10224 * The method also handles normal invalidation logic if display list properties are not
10225 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10226 * backup approach, to handle these cases used in the various property-setting methods.
10227 *
10228 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10229 * are not being used in this view
10230 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10231 * list properties are not being used in this view
10232 */
10233 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010234 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010235 if (invalidateParent) {
10236 invalidateParentCaches();
10237 }
10238 if (forceRedraw) {
10239 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10240 }
10241 invalidate(false);
10242 } else {
10243 final AttachInfo ai = mAttachInfo;
10244 final ViewParent p = mParent;
10245 if (p != null && ai != null) {
10246 final Rect r = ai.mTmpInvalRect;
10247 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10248 if (mParent instanceof ViewGroup) {
10249 ((ViewGroup) mParent).invalidateChildFast(this, r);
10250 } else {
10251 mParent.invalidateChild(this, r);
10252 }
10253 }
10254 }
10255 }
10256
10257 /**
10258 * Utility method to transform a given Rect by the current matrix of this view.
10259 */
10260 void transformRect(final Rect rect) {
10261 if (!getMatrix().isIdentity()) {
10262 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10263 boundingRect.set(rect);
10264 getMatrix().mapRect(boundingRect);
10265 rect.set((int) (boundingRect.left - 0.5f),
10266 (int) (boundingRect.top - 0.5f),
10267 (int) (boundingRect.right + 0.5f),
10268 (int) (boundingRect.bottom + 0.5f));
10269 }
10270 }
10271
10272 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010273 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010274 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10275 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010276 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10277 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010278 *
10279 * @hide
10280 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010281 protected void invalidateParentCaches() {
10282 if (mParent instanceof View) {
10283 ((View) mParent).mPrivateFlags |= INVALIDATED;
10284 }
10285 }
Joe Malin32736f02011-01-19 16:14:20 -080010286
Romain Guy0fd89bf2011-01-26 15:41:30 -080010287 /**
10288 * Used to indicate that the parent of this view should be invalidated. This functionality
10289 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10290 * which is necessary when various parent-managed properties of the view change, such as
10291 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10292 * an invalidation event to the parent.
10293 *
10294 * @hide
10295 */
10296 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010297 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010298 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010299 }
10300 }
10301
10302 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010303 * Indicates whether this View is opaque. An opaque View guarantees that it will
10304 * draw all the pixels overlapping its bounds using a fully opaque color.
10305 *
10306 * Subclasses of View should override this method whenever possible to indicate
10307 * whether an instance is opaque. Opaque Views are treated in a special way by
10308 * the View hierarchy, possibly allowing it to perform optimizations during
10309 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010310 *
Romain Guy24443ea2009-05-11 11:56:30 -070010311 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010312 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010313 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010314 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010315 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010316 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10317 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010318 }
10319
Adam Powell20232d02010-12-08 21:08:53 -080010320 /**
10321 * @hide
10322 */
10323 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010324 // Opaque if:
10325 // - Has a background
10326 // - Background is opaque
10327 // - Doesn't have scrollbars or scrollbars are inside overlay
10328
Philip Milne6c8ea062012-04-03 17:38:43 -070010329 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010330 mPrivateFlags |= OPAQUE_BACKGROUND;
10331 } else {
10332 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10333 }
10334
10335 final int flags = mViewFlags;
10336 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10337 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10338 mPrivateFlags |= OPAQUE_SCROLLBARS;
10339 } else {
10340 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10341 }
10342 }
10343
10344 /**
10345 * @hide
10346 */
10347 protected boolean hasOpaqueScrollbars() {
10348 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010349 }
10350
10351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 * @return A handler associated with the thread running the View. This
10353 * handler can be used to pump events in the UI events queue.
10354 */
10355 public Handler getHandler() {
10356 if (mAttachInfo != null) {
10357 return mAttachInfo.mHandler;
10358 }
10359 return null;
10360 }
10361
10362 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010363 * Gets the view root associated with the View.
10364 * @return The view root, or null if none.
10365 * @hide
10366 */
10367 public ViewRootImpl getViewRootImpl() {
10368 if (mAttachInfo != null) {
10369 return mAttachInfo.mViewRootImpl;
10370 }
10371 return null;
10372 }
10373
10374 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010375 * <p>Causes the Runnable to be added to the message queue.
10376 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010377 *
Romain Guye63a4f32011-08-11 11:33:31 -070010378 * <p>This method can be invoked from outside of the UI thread
10379 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010380 *
10381 * @param action The Runnable that will be executed.
10382 *
10383 * @return Returns true if the Runnable was successfully placed in to the
10384 * message queue. Returns false on failure, usually because the
10385 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010386 *
10387 * @see #postDelayed
10388 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 */
10390 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010391 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010392 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010393 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010395 // Assume that post will succeed later
10396 ViewRootImpl.getRunQueue().post(action);
10397 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 }
10399
10400 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010401 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010403 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010404 *
Romain Guye63a4f32011-08-11 11:33:31 -070010405 * <p>This method can be invoked from outside of the UI thread
10406 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 *
10408 * @param action The Runnable that will be executed.
10409 * @param delayMillis The delay (in milliseconds) until the Runnable
10410 * will be executed.
10411 *
10412 * @return true if the Runnable was successfully placed in to the
10413 * message queue. Returns false on failure, usually because the
10414 * looper processing the message queue is exiting. Note that a
10415 * result of true does not mean the Runnable will be processed --
10416 * if the looper is quit before the delivery time of the message
10417 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010418 *
10419 * @see #post
10420 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 */
10422 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010423 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010424 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010425 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010427 // Assume that post will succeed later
10428 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10429 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 }
10431
10432 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010433 * <p>Causes the Runnable to execute on the next animation time step.
10434 * The runnable will be run on the user interface thread.</p>
10435 *
10436 * <p>This method can be invoked from outside of the UI thread
10437 * only when this View is attached to a window.</p>
10438 *
10439 * @param action The Runnable that will be executed.
10440 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010441 * @see #postOnAnimationDelayed
10442 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010443 */
10444 public void postOnAnimation(Runnable action) {
10445 final AttachInfo attachInfo = mAttachInfo;
10446 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010447 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10448 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010449 } else {
10450 // Assume that post will succeed later
10451 ViewRootImpl.getRunQueue().post(action);
10452 }
10453 }
10454
10455 /**
10456 * <p>Causes the Runnable to execute on the next animation time step,
10457 * after the specified amount of time elapses.
10458 * The runnable will be run on the user interface thread.</p>
10459 *
10460 * <p>This method can be invoked from outside of the UI thread
10461 * only when this View is attached to a window.</p>
10462 *
10463 * @param action The Runnable that will be executed.
10464 * @param delayMillis The delay (in milliseconds) until the Runnable
10465 * will be executed.
10466 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010467 * @see #postOnAnimation
10468 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010469 */
10470 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10471 final AttachInfo attachInfo = mAttachInfo;
10472 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010473 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10474 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010475 } else {
10476 // Assume that post will succeed later
10477 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10478 }
10479 }
10480
10481 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010482 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010483 *
Romain Guye63a4f32011-08-11 11:33:31 -070010484 * <p>This method can be invoked from outside of the UI thread
10485 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486 *
10487 * @param action The Runnable to remove from the message handling queue
10488 *
10489 * @return true if this view could ask the Handler to remove the Runnable,
10490 * false otherwise. When the returned value is true, the Runnable
10491 * may or may not have been actually removed from the message queue
10492 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010493 *
10494 * @see #post
10495 * @see #postDelayed
10496 * @see #postOnAnimation
10497 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 */
10499 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010500 if (action != null) {
10501 final AttachInfo attachInfo = mAttachInfo;
10502 if (attachInfo != null) {
10503 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010504 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10505 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010506 } else {
10507 // Assume that post will succeed later
10508 ViewRootImpl.getRunQueue().removeCallbacks(action);
10509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 return true;
10512 }
10513
10514 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010515 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10516 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 *
Romain Guye63a4f32011-08-11 11:33:31 -070010518 * <p>This method can be invoked from outside of the UI thread
10519 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010520 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010522 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 */
10524 public void postInvalidate() {
10525 postInvalidateDelayed(0);
10526 }
10527
10528 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010529 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10530 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010531 *
Romain Guye63a4f32011-08-11 11:33:31 -070010532 * <p>This method can be invoked from outside of the UI thread
10533 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 *
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.
10539 *
10540 * @see #invalidate(int, int, int, int)
10541 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010542 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 */
10544 public void postInvalidate(int left, int top, int right, int bottom) {
10545 postInvalidateDelayed(0, left, top, right, bottom);
10546 }
10547
10548 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010549 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10550 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010551 *
Romain Guye63a4f32011-08-11 11:33:31 -070010552 * <p>This method can be invoked from outside of the UI thread
10553 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 *
10555 * @param delayMilliseconds the duration in milliseconds to delay the
10556 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010557 *
10558 * @see #invalidate()
10559 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 */
10561 public void postInvalidateDelayed(long delayMilliseconds) {
10562 // We try only with the AttachInfo because there's no point in invalidating
10563 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010564 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010565 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010566 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 }
10568 }
10569
10570 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010571 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10572 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010573 *
Romain Guye63a4f32011-08-11 11:33:31 -070010574 * <p>This method can be invoked from outside of the UI thread
10575 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576 *
10577 * @param delayMilliseconds the duration in milliseconds to delay the
10578 * invalidation by
10579 * @param left The left coordinate of the rectangle to invalidate.
10580 * @param top The top coordinate of the rectangle to invalidate.
10581 * @param right The right coordinate of the rectangle to invalidate.
10582 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010583 *
10584 * @see #invalidate(int, int, int, int)
10585 * @see #invalidate(Rect)
10586 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 */
10588 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10589 int right, int bottom) {
10590
10591 // We try only with the AttachInfo because there's no point in invalidating
10592 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010593 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010594 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10596 info.target = this;
10597 info.left = left;
10598 info.top = top;
10599 info.right = right;
10600 info.bottom = bottom;
10601
Jeff Browna175a5b2012-02-15 19:18:31 -080010602 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 }
10604 }
10605
10606 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010607 * <p>Cause an invalidate to happen on the next animation time step, typically the
10608 * next display frame.</p>
10609 *
10610 * <p>This method can be invoked from outside of the UI thread
10611 * only when this View is attached to a window.</p>
10612 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010613 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010614 */
10615 public void postInvalidateOnAnimation() {
10616 // We try only with the AttachInfo because there's no point in invalidating
10617 // if we are not attached to our window
10618 final AttachInfo attachInfo = mAttachInfo;
10619 if (attachInfo != null) {
10620 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10621 }
10622 }
10623
10624 /**
10625 * <p>Cause an invalidate of the specified area to happen on the next animation
10626 * time step, typically the next display frame.</p>
10627 *
10628 * <p>This method can be invoked from outside of the UI thread
10629 * only when this View is attached to a window.</p>
10630 *
10631 * @param left The left coordinate of the rectangle to invalidate.
10632 * @param top The top coordinate of the rectangle to invalidate.
10633 * @param right The right coordinate of the rectangle to invalidate.
10634 * @param bottom The bottom coordinate of the rectangle to invalidate.
10635 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010636 * @see #invalidate(int, int, int, int)
10637 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010638 */
10639 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10640 // We try only with the AttachInfo because there's no point in invalidating
10641 // if we are not attached to our window
10642 final AttachInfo attachInfo = mAttachInfo;
10643 if (attachInfo != null) {
10644 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10645 info.target = this;
10646 info.left = left;
10647 info.top = top;
10648 info.right = right;
10649 info.bottom = bottom;
10650
10651 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10652 }
10653 }
10654
10655 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010656 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10657 * This event is sent at most once every
10658 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10659 */
10660 private void postSendViewScrolledAccessibilityEventCallback() {
10661 if (mSendViewScrolledAccessibilityEvent == null) {
10662 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10663 }
10664 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10665 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10666 postDelayed(mSendViewScrolledAccessibilityEvent,
10667 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10668 }
10669 }
10670
10671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 * Called by a parent to request that a child update its values for mScrollX
10673 * and mScrollY if necessary. This will typically be done if the child is
10674 * animating a scroll using a {@link android.widget.Scroller Scroller}
10675 * object.
10676 */
10677 public void computeScroll() {
10678 }
10679
10680 /**
10681 * <p>Indicate whether the horizontal edges are faded when the view is
10682 * scrolled horizontally.</p>
10683 *
10684 * @return true if the horizontal edges should are faded on scroll, false
10685 * otherwise
10686 *
10687 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010688 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010689 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010690 */
10691 public boolean isHorizontalFadingEdgeEnabled() {
10692 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10693 }
10694
10695 /**
10696 * <p>Define whether the horizontal edges should be faded when this view
10697 * is scrolled horizontally.</p>
10698 *
10699 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10700 * be faded when the view is scrolled
10701 * horizontally
10702 *
10703 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010704 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010705 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010706 */
10707 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10708 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10709 if (horizontalFadingEdgeEnabled) {
10710 initScrollCache();
10711 }
10712
10713 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10714 }
10715 }
10716
10717 /**
10718 * <p>Indicate whether the vertical edges are faded when the view is
10719 * scrolled horizontally.</p>
10720 *
10721 * @return true if the vertical edges should are faded on scroll, false
10722 * otherwise
10723 *
10724 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010725 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010726 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010727 */
10728 public boolean isVerticalFadingEdgeEnabled() {
10729 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10730 }
10731
10732 /**
10733 * <p>Define whether the vertical edges should be faded when this view
10734 * is scrolled vertically.</p>
10735 *
10736 * @param verticalFadingEdgeEnabled true if the vertical edges should
10737 * be faded when the view is scrolled
10738 * vertically
10739 *
10740 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010741 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010742 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743 */
10744 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10745 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10746 if (verticalFadingEdgeEnabled) {
10747 initScrollCache();
10748 }
10749
10750 mViewFlags ^= FADING_EDGE_VERTICAL;
10751 }
10752 }
10753
10754 /**
10755 * Returns the strength, or intensity, of the top faded edge. The strength is
10756 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10757 * returns 0.0 or 1.0 but no value in between.
10758 *
10759 * Subclasses should override this method to provide a smoother fade transition
10760 * when scrolling occurs.
10761 *
10762 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10763 */
10764 protected float getTopFadingEdgeStrength() {
10765 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10766 }
10767
10768 /**
10769 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10770 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10771 * returns 0.0 or 1.0 but no value in between.
10772 *
10773 * Subclasses should override this method to provide a smoother fade transition
10774 * when scrolling occurs.
10775 *
10776 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10777 */
10778 protected float getBottomFadingEdgeStrength() {
10779 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10780 computeVerticalScrollRange() ? 1.0f : 0.0f;
10781 }
10782
10783 /**
10784 * Returns the strength, or intensity, of the left faded edge. The strength is
10785 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10786 * returns 0.0 or 1.0 but no value in between.
10787 *
10788 * Subclasses should override this method to provide a smoother fade transition
10789 * when scrolling occurs.
10790 *
10791 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10792 */
10793 protected float getLeftFadingEdgeStrength() {
10794 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10795 }
10796
10797 /**
10798 * Returns the strength, or intensity, of the right faded edge. The strength is
10799 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10800 * returns 0.0 or 1.0 but no value in between.
10801 *
10802 * Subclasses should override this method to provide a smoother fade transition
10803 * when scrolling occurs.
10804 *
10805 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10806 */
10807 protected float getRightFadingEdgeStrength() {
10808 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10809 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10810 }
10811
10812 /**
10813 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10814 * scrollbar is not drawn by default.</p>
10815 *
10816 * @return true if the horizontal scrollbar should be painted, false
10817 * otherwise
10818 *
10819 * @see #setHorizontalScrollBarEnabled(boolean)
10820 */
10821 public boolean isHorizontalScrollBarEnabled() {
10822 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10823 }
10824
10825 /**
10826 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10827 * scrollbar is not drawn by default.</p>
10828 *
10829 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10830 * be painted
10831 *
10832 * @see #isHorizontalScrollBarEnabled()
10833 */
10834 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10835 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10836 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010837 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010838 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 }
10840 }
10841
10842 /**
10843 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10844 * scrollbar is not drawn by default.</p>
10845 *
10846 * @return true if the vertical scrollbar should be painted, false
10847 * otherwise
10848 *
10849 * @see #setVerticalScrollBarEnabled(boolean)
10850 */
10851 public boolean isVerticalScrollBarEnabled() {
10852 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10853 }
10854
10855 /**
10856 * <p>Define whether the vertical scrollbar should be drawn or not. The
10857 * scrollbar is not drawn by default.</p>
10858 *
10859 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10860 * be painted
10861 *
10862 * @see #isVerticalScrollBarEnabled()
10863 */
10864 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10865 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10866 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010867 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010868 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 }
10870 }
10871
Adam Powell20232d02010-12-08 21:08:53 -080010872 /**
10873 * @hide
10874 */
10875 protected void recomputePadding() {
10876 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010877 }
Joe Malin32736f02011-01-19 16:14:20 -080010878
Mike Cleronfe81d382009-09-28 14:22:16 -070010879 /**
10880 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010881 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010882 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010883 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010884 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010885 */
10886 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10887 initScrollCache();
10888 final ScrollabilityCache scrollabilityCache = mScrollCache;
10889 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010890 if (fadeScrollbars) {
10891 scrollabilityCache.state = ScrollabilityCache.OFF;
10892 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010893 scrollabilityCache.state = ScrollabilityCache.ON;
10894 }
10895 }
Joe Malin32736f02011-01-19 16:14:20 -080010896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010897 /**
Joe Malin32736f02011-01-19 16:14:20 -080010898 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010899 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010900 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010901 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010902 *
10903 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010904 */
10905 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010906 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010907 }
Joe Malin32736f02011-01-19 16:14:20 -080010908
Mike Cleron52f0a642009-09-28 18:21:37 -070010909 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010910 *
10911 * Returns the delay before scrollbars fade.
10912 *
10913 * @return the delay before scrollbars fade
10914 *
10915 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10916 */
10917 public int getScrollBarDefaultDelayBeforeFade() {
10918 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10919 mScrollCache.scrollBarDefaultDelayBeforeFade;
10920 }
10921
10922 /**
10923 * Define the delay before scrollbars fade.
10924 *
10925 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10926 *
10927 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10928 */
10929 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10930 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10931 }
10932
10933 /**
10934 *
10935 * Returns the scrollbar fade duration.
10936 *
10937 * @return the scrollbar fade duration
10938 *
10939 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10940 */
10941 public int getScrollBarFadeDuration() {
10942 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10943 mScrollCache.scrollBarFadeDuration;
10944 }
10945
10946 /**
10947 * Define the scrollbar fade duration.
10948 *
10949 * @param scrollBarFadeDuration - the scrollbar fade duration
10950 *
10951 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10952 */
10953 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10954 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10955 }
10956
10957 /**
10958 *
10959 * Returns the scrollbar size.
10960 *
10961 * @return the scrollbar size
10962 *
10963 * @attr ref android.R.styleable#View_scrollbarSize
10964 */
10965 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010966 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010967 mScrollCache.scrollBarSize;
10968 }
10969
10970 /**
10971 * Define the scrollbar size.
10972 *
10973 * @param scrollBarSize - the scrollbar size
10974 *
10975 * @attr ref android.R.styleable#View_scrollbarSize
10976 */
10977 public void setScrollBarSize(int scrollBarSize) {
10978 getScrollCache().scrollBarSize = scrollBarSize;
10979 }
10980
10981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10983 * inset. When inset, they add to the padding of the view. And the scrollbars
10984 * can be drawn inside the padding area or on the edge of the view. For example,
10985 * if a view has a background drawable and you want to draw the scrollbars
10986 * inside the padding specified by the drawable, you can use
10987 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10988 * appear at the edge of the view, ignoring the padding, then you can use
10989 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10990 * @param style the style of the scrollbars. Should be one of
10991 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10992 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10993 * @see #SCROLLBARS_INSIDE_OVERLAY
10994 * @see #SCROLLBARS_INSIDE_INSET
10995 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10996 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010997 *
10998 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 */
11000 public void setScrollBarStyle(int style) {
11001 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11002 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011003 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011004 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 }
11006 }
11007
11008 /**
11009 * <p>Returns the current scrollbar style.</p>
11010 * @return the current scrollbar style
11011 * @see #SCROLLBARS_INSIDE_OVERLAY
11012 * @see #SCROLLBARS_INSIDE_INSET
11013 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11014 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011015 *
11016 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011017 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011018 @ViewDebug.ExportedProperty(mapping = {
11019 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11020 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11021 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11022 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11023 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 public int getScrollBarStyle() {
11025 return mViewFlags & SCROLLBARS_STYLE_MASK;
11026 }
11027
11028 /**
11029 * <p>Compute the horizontal range that the horizontal scrollbar
11030 * represents.</p>
11031 *
11032 * <p>The range is expressed in arbitrary units that must be the same as the
11033 * units used by {@link #computeHorizontalScrollExtent()} and
11034 * {@link #computeHorizontalScrollOffset()}.</p>
11035 *
11036 * <p>The default range is the drawing width of this view.</p>
11037 *
11038 * @return the total horizontal range represented by the horizontal
11039 * scrollbar
11040 *
11041 * @see #computeHorizontalScrollExtent()
11042 * @see #computeHorizontalScrollOffset()
11043 * @see android.widget.ScrollBarDrawable
11044 */
11045 protected int computeHorizontalScrollRange() {
11046 return getWidth();
11047 }
11048
11049 /**
11050 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11051 * within the horizontal range. This value is used to compute the position
11052 * of the thumb within the scrollbar's track.</p>
11053 *
11054 * <p>The range is expressed in arbitrary units that must be the same as the
11055 * units used by {@link #computeHorizontalScrollRange()} and
11056 * {@link #computeHorizontalScrollExtent()}.</p>
11057 *
11058 * <p>The default offset is the scroll offset of this view.</p>
11059 *
11060 * @return the horizontal offset of the scrollbar's thumb
11061 *
11062 * @see #computeHorizontalScrollRange()
11063 * @see #computeHorizontalScrollExtent()
11064 * @see android.widget.ScrollBarDrawable
11065 */
11066 protected int computeHorizontalScrollOffset() {
11067 return mScrollX;
11068 }
11069
11070 /**
11071 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11072 * within the horizontal range. This value is used to compute the length
11073 * of the thumb within the scrollbar's track.</p>
11074 *
11075 * <p>The range is expressed in arbitrary units that must be the same as the
11076 * units used by {@link #computeHorizontalScrollRange()} and
11077 * {@link #computeHorizontalScrollOffset()}.</p>
11078 *
11079 * <p>The default extent is the drawing width of this view.</p>
11080 *
11081 * @return the horizontal extent of the scrollbar's thumb
11082 *
11083 * @see #computeHorizontalScrollRange()
11084 * @see #computeHorizontalScrollOffset()
11085 * @see android.widget.ScrollBarDrawable
11086 */
11087 protected int computeHorizontalScrollExtent() {
11088 return getWidth();
11089 }
11090
11091 /**
11092 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11093 *
11094 * <p>The range is expressed in arbitrary units that must be the same as the
11095 * units used by {@link #computeVerticalScrollExtent()} and
11096 * {@link #computeVerticalScrollOffset()}.</p>
11097 *
11098 * @return the total vertical range represented by the vertical scrollbar
11099 *
11100 * <p>The default range is the drawing height of this view.</p>
11101 *
11102 * @see #computeVerticalScrollExtent()
11103 * @see #computeVerticalScrollOffset()
11104 * @see android.widget.ScrollBarDrawable
11105 */
11106 protected int computeVerticalScrollRange() {
11107 return getHeight();
11108 }
11109
11110 /**
11111 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11112 * within the horizontal range. This value is used to compute the position
11113 * of the thumb within the scrollbar's track.</p>
11114 *
11115 * <p>The range is expressed in arbitrary units that must be the same as the
11116 * units used by {@link #computeVerticalScrollRange()} and
11117 * {@link #computeVerticalScrollExtent()}.</p>
11118 *
11119 * <p>The default offset is the scroll offset of this view.</p>
11120 *
11121 * @return the vertical offset of the scrollbar's thumb
11122 *
11123 * @see #computeVerticalScrollRange()
11124 * @see #computeVerticalScrollExtent()
11125 * @see android.widget.ScrollBarDrawable
11126 */
11127 protected int computeVerticalScrollOffset() {
11128 return mScrollY;
11129 }
11130
11131 /**
11132 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11133 * within the vertical range. This value is used to compute the length
11134 * of the thumb within the scrollbar's track.</p>
11135 *
11136 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011137 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 * {@link #computeVerticalScrollOffset()}.</p>
11139 *
11140 * <p>The default extent is the drawing height of this view.</p>
11141 *
11142 * @return the vertical extent of the scrollbar's thumb
11143 *
11144 * @see #computeVerticalScrollRange()
11145 * @see #computeVerticalScrollOffset()
11146 * @see android.widget.ScrollBarDrawable
11147 */
11148 protected int computeVerticalScrollExtent() {
11149 return getHeight();
11150 }
11151
11152 /**
Adam Powell69159442011-06-13 17:53:06 -070011153 * Check if this view can be scrolled horizontally in a certain direction.
11154 *
11155 * @param direction Negative to check scrolling left, positive to check scrolling right.
11156 * @return true if this view can be scrolled in the specified direction, false otherwise.
11157 */
11158 public boolean canScrollHorizontally(int direction) {
11159 final int offset = computeHorizontalScrollOffset();
11160 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11161 if (range == 0) return false;
11162 if (direction < 0) {
11163 return offset > 0;
11164 } else {
11165 return offset < range - 1;
11166 }
11167 }
11168
11169 /**
11170 * Check if this view can be scrolled vertically in a certain direction.
11171 *
11172 * @param direction Negative to check scrolling up, positive to check scrolling down.
11173 * @return true if this view can be scrolled in the specified direction, false otherwise.
11174 */
11175 public boolean canScrollVertically(int direction) {
11176 final int offset = computeVerticalScrollOffset();
11177 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11178 if (range == 0) return false;
11179 if (direction < 0) {
11180 return offset > 0;
11181 } else {
11182 return offset < range - 1;
11183 }
11184 }
11185
11186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11188 * scrollbars are painted only if they have been awakened first.</p>
11189 *
11190 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011191 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011192 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011194 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011195 // scrollbars are drawn only when the animation is running
11196 final ScrollabilityCache cache = mScrollCache;
11197 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011198
Mike Cleronf116bf82009-09-27 19:14:12 -070011199 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011200
Mike Cleronf116bf82009-09-27 19:14:12 -070011201 if (state == ScrollabilityCache.OFF) {
11202 return;
11203 }
Joe Malin32736f02011-01-19 16:14:20 -080011204
Mike Cleronf116bf82009-09-27 19:14:12 -070011205 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011206
Mike Cleronf116bf82009-09-27 19:14:12 -070011207 if (state == ScrollabilityCache.FADING) {
11208 // We're fading -- get our fade interpolation
11209 if (cache.interpolatorValues == null) {
11210 cache.interpolatorValues = new float[1];
11211 }
Joe Malin32736f02011-01-19 16:14:20 -080011212
Mike Cleronf116bf82009-09-27 19:14:12 -070011213 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011214
Mike Cleronf116bf82009-09-27 19:14:12 -070011215 // Stops the animation if we're done
11216 if (cache.scrollBarInterpolator.timeToValues(values) ==
11217 Interpolator.Result.FREEZE_END) {
11218 cache.state = ScrollabilityCache.OFF;
11219 } else {
11220 cache.scrollBar.setAlpha(Math.round(values[0]));
11221 }
Joe Malin32736f02011-01-19 16:14:20 -080011222
11223 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011224 // drawing. We only want this when we're fading so that
11225 // we prevent excessive redraws
11226 invalidate = true;
11227 } else {
11228 // We're just on -- but we may have been fading before so
11229 // reset alpha
11230 cache.scrollBar.setAlpha(255);
11231 }
11232
Joe Malin32736f02011-01-19 16:14:20 -080011233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 final int viewFlags = mViewFlags;
11235
11236 final boolean drawHorizontalScrollBar =
11237 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11238 final boolean drawVerticalScrollBar =
11239 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11240 && !isVerticalScrollBarHidden();
11241
11242 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11243 final int width = mRight - mLeft;
11244 final int height = mBottom - mTop;
11245
11246 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011247
Mike Reede8853fc2009-09-04 14:01:48 -040011248 final int scrollX = mScrollX;
11249 final int scrollY = mScrollY;
11250 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11251
Mike Cleronf116bf82009-09-27 19:14:12 -070011252 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011255 int size = scrollBar.getSize(false);
11256 if (size <= 0) {
11257 size = cache.scrollBarSize;
11258 }
11259
Mike Cleronf116bf82009-09-27 19:14:12 -070011260 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011261 computeHorizontalScrollOffset(),
11262 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011263 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011264 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011265 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011266 left = scrollX + (mPaddingLeft & inside);
11267 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11268 bottom = top + size;
11269 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11270 if (invalidate) {
11271 invalidate(left, top, right, bottom);
11272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011273 }
11274
11275 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011276 int size = scrollBar.getSize(true);
11277 if (size <= 0) {
11278 size = cache.scrollBarSize;
11279 }
11280
Mike Reede8853fc2009-09-04 14:01:48 -040011281 scrollBar.setParameters(computeVerticalScrollRange(),
11282 computeVerticalScrollOffset(),
11283 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011284 switch (mVerticalScrollbarPosition) {
11285 default:
11286 case SCROLLBAR_POSITION_DEFAULT:
11287 case SCROLLBAR_POSITION_RIGHT:
11288 left = scrollX + width - size - (mUserPaddingRight & inside);
11289 break;
11290 case SCROLLBAR_POSITION_LEFT:
11291 left = scrollX + (mUserPaddingLeft & inside);
11292 break;
11293 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011294 top = scrollY + (mPaddingTop & inside);
11295 right = left + size;
11296 bottom = scrollY + height - (mUserPaddingBottom & inside);
11297 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11298 if (invalidate) {
11299 invalidate(left, top, right, bottom);
11300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011301 }
11302 }
11303 }
11304 }
Romain Guy8506ab42009-06-11 17:35:47 -070011305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011306 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011307 * 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 -080011308 * FastScroller is visible.
11309 * @return whether to temporarily hide the vertical scrollbar
11310 * @hide
11311 */
11312 protected boolean isVerticalScrollBarHidden() {
11313 return false;
11314 }
11315
11316 /**
11317 * <p>Draw the horizontal scrollbar if
11318 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 * @param canvas the canvas on which to draw the scrollbar
11321 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011322 *
11323 * @see #isHorizontalScrollBarEnabled()
11324 * @see #computeHorizontalScrollRange()
11325 * @see #computeHorizontalScrollExtent()
11326 * @see #computeHorizontalScrollOffset()
11327 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011328 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011329 */
Romain Guy8fb95422010-08-17 18:38:51 -070011330 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11331 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011332 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011333 scrollBar.draw(canvas);
11334 }
Mike Reede8853fc2009-09-04 14:01:48 -040011335
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11338 * returns true.</p>
11339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 * @param canvas the canvas on which to draw the scrollbar
11341 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011342 *
11343 * @see #isVerticalScrollBarEnabled()
11344 * @see #computeVerticalScrollRange()
11345 * @see #computeVerticalScrollExtent()
11346 * @see #computeVerticalScrollOffset()
11347 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011348 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349 */
Romain Guy8fb95422010-08-17 18:38:51 -070011350 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11351 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011352 scrollBar.setBounds(l, t, r, b);
11353 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011354 }
11355
11356 /**
11357 * Implement this to do your drawing.
11358 *
11359 * @param canvas the canvas on which the background will be drawn
11360 */
11361 protected void onDraw(Canvas canvas) {
11362 }
11363
11364 /*
11365 * Caller is responsible for calling requestLayout if necessary.
11366 * (This allows addViewInLayout to not request a new layout.)
11367 */
11368 void assignParent(ViewParent parent) {
11369 if (mParent == null) {
11370 mParent = parent;
11371 } else if (parent == null) {
11372 mParent = null;
11373 } else {
11374 throw new RuntimeException("view " + this + " being added, but"
11375 + " it already has a parent");
11376 }
11377 }
11378
11379 /**
11380 * This is called when the view is attached to a window. At this point it
11381 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011382 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11383 * however it may be called any time before the first onDraw -- including
11384 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 *
11386 * @see #onDetachedFromWindow()
11387 */
11388 protected void onAttachedToWindow() {
11389 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11390 mParent.requestTransparentRegion(this);
11391 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011392
Adam Powell8568c3a2010-04-19 14:26:11 -070011393 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11394 initialAwakenScrollBars();
11395 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11396 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011397
Chet Haasea9b61ac2010-12-20 07:40:25 -080011398 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011399
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011400 // Order is important here: LayoutDirection MUST be resolved before Padding
11401 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011402 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011403 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011404 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011405 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011406
Svetoslav Ganov42138042012-03-20 11:51:39 -070011407 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011408 if (isFocused()) {
11409 InputMethodManager imm = InputMethodManager.peekInstance();
11410 imm.focusIn(this);
11411 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011412
11413 if (mAttachInfo != null && mDisplayList != null) {
11414 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11415 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011416 }
Cibu Johny86666632010-02-22 13:01:02 -080011417
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011418 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011419 * @see #onScreenStateChanged(int)
11420 */
11421 void dispatchScreenStateChanged(int screenState) {
11422 onScreenStateChanged(screenState);
11423 }
11424
11425 /**
11426 * This method is called whenever the state of the screen this view is
11427 * attached to changes. A state change will usually occurs when the screen
11428 * turns on or off (whether it happens automatically or the user does it
11429 * manually.)
11430 *
11431 * @param screenState The new state of the screen. Can be either
11432 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11433 */
11434 public void onScreenStateChanged(int screenState) {
11435 }
11436
11437 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011438 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11439 */
11440 private boolean hasRtlSupport() {
11441 return mContext.getApplicationInfo().hasRtlSupport();
11442 }
11443
11444 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011445 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11446 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011447 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011448 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011449 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011450 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011451 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011452 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011453
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011454 if (hasRtlSupport()) {
11455 // Set resolved depending on layout direction
11456 switch (getLayoutDirection()) {
11457 case LAYOUT_DIRECTION_INHERIT:
11458 // If this is root view, no need to look at parent's layout dir.
11459 if (canResolveLayoutDirection()) {
11460 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011461
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011462 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11463 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11464 }
11465 } else {
11466 // Nothing to do, LTR by default
11467 }
11468 break;
11469 case LAYOUT_DIRECTION_RTL:
11470 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11471 break;
11472 case LAYOUT_DIRECTION_LOCALE:
11473 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011474 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11475 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011476 break;
11477 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011478 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011479 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011480 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011481
11482 // Set to resolved
11483 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011484 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011485 // Resolve padding
11486 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011487 }
11488
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011489 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011490 * Called when layout direction has been resolved.
11491 *
11492 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011493 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011494 */
11495 public void onResolvedLayoutDirectionChanged() {
11496 }
11497
11498 /**
11499 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011500 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011501 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011502 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011503 // If the user specified the absolute padding (either with android:padding or
11504 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11505 // use the default padding or the padding from the background drawable
11506 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011507 int resolvedLayoutDirection = getResolvedLayoutDirection();
11508 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011509 case LAYOUT_DIRECTION_RTL:
11510 // Start user padding override Right user padding. Otherwise, if Right user
11511 // padding is not defined, use the default Right padding. If Right user padding
11512 // is defined, just use it.
11513 if (mUserPaddingStart >= 0) {
11514 mUserPaddingRight = mUserPaddingStart;
11515 } else if (mUserPaddingRight < 0) {
11516 mUserPaddingRight = mPaddingRight;
11517 }
11518 if (mUserPaddingEnd >= 0) {
11519 mUserPaddingLeft = mUserPaddingEnd;
11520 } else if (mUserPaddingLeft < 0) {
11521 mUserPaddingLeft = mPaddingLeft;
11522 }
11523 break;
11524 case LAYOUT_DIRECTION_LTR:
11525 default:
11526 // Start user padding override Left user padding. Otherwise, if Left user
11527 // padding is not defined, use the default left padding. If Left user padding
11528 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011529 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011530 mUserPaddingLeft = mUserPaddingStart;
11531 } else if (mUserPaddingLeft < 0) {
11532 mUserPaddingLeft = mPaddingLeft;
11533 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011534 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011535 mUserPaddingRight = mUserPaddingEnd;
11536 } else if (mUserPaddingRight < 0) {
11537 mUserPaddingRight = mPaddingRight;
11538 }
11539 }
11540
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011541 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11542
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011543 if(isPaddingRelative()) {
11544 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11545 } else {
11546 recomputePadding();
11547 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011548 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011549 }
11550
11551 /**
11552 * Resolve padding depending on the layout direction. Subclasses that care about
11553 * padding resolution should override this method. The default implementation does
11554 * nothing.
11555 *
11556 * @param layoutDirection the direction of the layout
11557 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011558 * @see {@link #LAYOUT_DIRECTION_LTR}
11559 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011560 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011561 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011562 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011563 }
11564
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011565 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011566 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011567 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011568 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011569 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011570 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011571 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011572 switch (getLayoutDirection()) {
11573 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011574 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011575 default:
11576 return true;
11577 }
11578 }
11579
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011580 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011581 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11582 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011583 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011584 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011585 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011586 // Reset the current resolved bits
11587 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011588 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011589 // Reset also the text direction
11590 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011591 }
11592
11593 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011594 * Called during reset of resolved layout direction.
11595 *
11596 * Subclasses need to override this method to clear cached information that depends on the
11597 * resolved layout direction, or to inform child views that inherit their layout direction.
11598 *
11599 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011600 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011601 */
11602 public void onResolvedLayoutDirectionReset() {
11603 }
11604
11605 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011606 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011607 *
11608 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011609 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011610 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011611 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011612 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011613 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 }
11615
11616 /**
11617 * This is called when the view is detached from a window. At this point it
11618 * no longer has a surface for drawing.
11619 *
11620 * @see #onAttachedToWindow()
11621 */
11622 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011623 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011624
Romain Guya440b002010-02-24 15:57:54 -080011625 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011626 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011627 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011628 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011630 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011631
Romain Guya998dff2012-03-23 18:58:36 -070011632 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011633
11634 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011635 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011636 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011637 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011638 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011639 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011640 // Should never happen
11641 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011642 }
11643
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011644 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011645
11646 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011647 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011648 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 }
11650
11651 /**
11652 * @return The number of times this view has been attached to a window
11653 */
11654 protected int getWindowAttachCount() {
11655 return mWindowAttachCount;
11656 }
11657
11658 /**
11659 * Retrieve a unique token identifying the window this view is attached to.
11660 * @return Return the window's token for use in
11661 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11662 */
11663 public IBinder getWindowToken() {
11664 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11665 }
11666
11667 /**
11668 * Retrieve a unique token identifying the top-level "real" window of
11669 * the window that this view is attached to. That is, this is like
11670 * {@link #getWindowToken}, except if the window this view in is a panel
11671 * window (attached to another containing window), then the token of
11672 * the containing window is returned instead.
11673 *
11674 * @return Returns the associated window token, either
11675 * {@link #getWindowToken()} or the containing window's token.
11676 */
11677 public IBinder getApplicationWindowToken() {
11678 AttachInfo ai = mAttachInfo;
11679 if (ai != null) {
11680 IBinder appWindowToken = ai.mPanelParentWindowToken;
11681 if (appWindowToken == null) {
11682 appWindowToken = ai.mWindowToken;
11683 }
11684 return appWindowToken;
11685 }
11686 return null;
11687 }
11688
11689 /**
11690 * Retrieve private session object this view hierarchy is using to
11691 * communicate with the window manager.
11692 * @return the session object to communicate with the window manager
11693 */
11694 /*package*/ IWindowSession getWindowSession() {
11695 return mAttachInfo != null ? mAttachInfo.mSession : null;
11696 }
11697
11698 /**
11699 * @param info the {@link android.view.View.AttachInfo} to associated with
11700 * this view
11701 */
11702 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11703 //System.out.println("Attached! " + this);
11704 mAttachInfo = info;
11705 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011706 // We will need to evaluate the drawable state at least once.
11707 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011708 if (mFloatingTreeObserver != null) {
11709 info.mTreeObserver.merge(mFloatingTreeObserver);
11710 mFloatingTreeObserver = null;
11711 }
11712 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11713 mAttachInfo.mScrollContainers.add(this);
11714 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11715 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011716 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011717 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011718
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011719 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011720 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011721 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011722 if (listeners != null && listeners.size() > 0) {
11723 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11724 // perform the dispatching. The iterator is a safe guard against listeners that
11725 // could mutate the list by calling the various add/remove methods. This prevents
11726 // the array from being modified while we iterate it.
11727 for (OnAttachStateChangeListener listener : listeners) {
11728 listener.onViewAttachedToWindow(this);
11729 }
11730 }
11731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011732 int vis = info.mWindowVisibility;
11733 if (vis != GONE) {
11734 onWindowVisibilityChanged(vis);
11735 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011736 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11737 // If nobody has evaluated the drawable state yet, then do it now.
11738 refreshDrawableState();
11739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011740 }
11741
11742 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011743 AttachInfo info = mAttachInfo;
11744 if (info != null) {
11745 int vis = info.mWindowVisibility;
11746 if (vis != GONE) {
11747 onWindowVisibilityChanged(GONE);
11748 }
11749 }
11750
11751 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011752
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011753 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011754 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011755 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011756 if (listeners != null && listeners.size() > 0) {
11757 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11758 // perform the dispatching. The iterator is a safe guard against listeners that
11759 // could mutate the list by calling the various add/remove methods. This prevents
11760 // the array from being modified while we iterate it.
11761 for (OnAttachStateChangeListener listener : listeners) {
11762 listener.onViewDetachedFromWindow(this);
11763 }
11764 }
11765
Romain Guy01d5edc2011-01-28 11:28:53 -080011766 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011767 mAttachInfo.mScrollContainers.remove(this);
11768 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11769 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011771 mAttachInfo = null;
11772 }
11773
11774 /**
11775 * Store this view hierarchy's frozen state into the given container.
11776 *
11777 * @param container The SparseArray in which to save the view's state.
11778 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011779 * @see #restoreHierarchyState(android.util.SparseArray)
11780 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11781 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011782 */
11783 public void saveHierarchyState(SparseArray<Parcelable> container) {
11784 dispatchSaveInstanceState(container);
11785 }
11786
11787 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011788 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11789 * this view and its children. May be overridden to modify how freezing happens to a
11790 * 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 -080011791 *
11792 * @param container The SparseArray in which to save the view's state.
11793 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011794 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11795 * @see #saveHierarchyState(android.util.SparseArray)
11796 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011797 */
11798 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11799 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11800 mPrivateFlags &= ~SAVE_STATE_CALLED;
11801 Parcelable state = onSaveInstanceState();
11802 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11803 throw new IllegalStateException(
11804 "Derived class did not call super.onSaveInstanceState()");
11805 }
11806 if (state != null) {
11807 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11808 // + ": " + state);
11809 container.put(mID, state);
11810 }
11811 }
11812 }
11813
11814 /**
11815 * Hook allowing a view to generate a representation of its internal state
11816 * that can later be used to create a new instance with that same state.
11817 * This state should only contain information that is not persistent or can
11818 * not be reconstructed later. For example, you will never store your
11819 * current position on screen because that will be computed again when a
11820 * new instance of the view is placed in its view hierarchy.
11821 * <p>
11822 * Some examples of things you may store here: the current cursor position
11823 * in a text view (but usually not the text itself since that is stored in a
11824 * content provider or other persistent storage), the currently selected
11825 * item in a list view.
11826 *
11827 * @return Returns a Parcelable object containing the view's current dynamic
11828 * state, or null if there is nothing interesting to save. The
11829 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011830 * @see #onRestoreInstanceState(android.os.Parcelable)
11831 * @see #saveHierarchyState(android.util.SparseArray)
11832 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011833 * @see #setSaveEnabled(boolean)
11834 */
11835 protected Parcelable onSaveInstanceState() {
11836 mPrivateFlags |= SAVE_STATE_CALLED;
11837 return BaseSavedState.EMPTY_STATE;
11838 }
11839
11840 /**
11841 * Restore this view hierarchy's frozen state from the given container.
11842 *
11843 * @param container The SparseArray which holds previously frozen states.
11844 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011845 * @see #saveHierarchyState(android.util.SparseArray)
11846 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11847 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 */
11849 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11850 dispatchRestoreInstanceState(container);
11851 }
11852
11853 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011854 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11855 * state for this view and its children. May be overridden to modify how restoring
11856 * happens to a view's children; for example, some views may want to not store state
11857 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 *
11859 * @param container The SparseArray which holds previously saved state.
11860 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011861 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11862 * @see #restoreHierarchyState(android.util.SparseArray)
11863 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011864 */
11865 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11866 if (mID != NO_ID) {
11867 Parcelable state = container.get(mID);
11868 if (state != null) {
11869 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11870 // + ": " + state);
11871 mPrivateFlags &= ~SAVE_STATE_CALLED;
11872 onRestoreInstanceState(state);
11873 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11874 throw new IllegalStateException(
11875 "Derived class did not call super.onRestoreInstanceState()");
11876 }
11877 }
11878 }
11879 }
11880
11881 /**
11882 * Hook allowing a view to re-apply a representation of its internal state that had previously
11883 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11884 * null state.
11885 *
11886 * @param state The frozen state that had previously been returned by
11887 * {@link #onSaveInstanceState}.
11888 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011889 * @see #onSaveInstanceState()
11890 * @see #restoreHierarchyState(android.util.SparseArray)
11891 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 */
11893 protected void onRestoreInstanceState(Parcelable state) {
11894 mPrivateFlags |= SAVE_STATE_CALLED;
11895 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011896 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11897 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011898 + "when two views of different type have the same id in the same hierarchy. "
11899 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011900 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 }
11902 }
11903
11904 /**
11905 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11906 *
11907 * @return the drawing start time in milliseconds
11908 */
11909 public long getDrawingTime() {
11910 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11911 }
11912
11913 /**
11914 * <p>Enables or disables the duplication of the parent's state into this view. When
11915 * duplication is enabled, this view gets its drawable state from its parent rather
11916 * than from its own internal properties.</p>
11917 *
11918 * <p>Note: in the current implementation, setting this property to true after the
11919 * view was added to a ViewGroup might have no effect at all. This property should
11920 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11921 *
11922 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11923 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011924 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011925 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11926 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011927 *
11928 * @param enabled True to enable duplication of the parent's drawable state, false
11929 * to disable it.
11930 *
11931 * @see #getDrawableState()
11932 * @see #isDuplicateParentStateEnabled()
11933 */
11934 public void setDuplicateParentStateEnabled(boolean enabled) {
11935 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11936 }
11937
11938 /**
11939 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11940 *
11941 * @return True if this view's drawable state is duplicated from the parent,
11942 * false otherwise
11943 *
11944 * @see #getDrawableState()
11945 * @see #setDuplicateParentStateEnabled(boolean)
11946 */
11947 public boolean isDuplicateParentStateEnabled() {
11948 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11949 }
11950
11951 /**
Romain Guy171c5922011-01-06 10:04:23 -080011952 * <p>Specifies the type of layer backing this view. The layer can be
11953 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11954 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011955 *
Romain Guy171c5922011-01-06 10:04:23 -080011956 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11957 * instance that controls how the layer is composed on screen. The following
11958 * properties of the paint are taken into account when composing the layer:</p>
11959 * <ul>
11960 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11961 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11962 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11963 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011964 *
Romain Guy171c5922011-01-06 10:04:23 -080011965 * <p>If this view has an alpha value set to < 1.0 by calling
11966 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11967 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11968 * equivalent to setting a hardware layer on this view and providing a paint with
11969 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011970 *
Romain Guy171c5922011-01-06 10:04:23 -080011971 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11972 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11973 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011974 *
Romain Guy171c5922011-01-06 10:04:23 -080011975 * @param layerType The ype of layer to use with this view, must be one of
11976 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11977 * {@link #LAYER_TYPE_HARDWARE}
11978 * @param paint The paint used to compose the layer. This argument is optional
11979 * and can be null. It is ignored when the layer type is
11980 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011981 *
11982 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011983 * @see #LAYER_TYPE_NONE
11984 * @see #LAYER_TYPE_SOFTWARE
11985 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011986 * @see #setAlpha(float)
11987 *
Romain Guy171c5922011-01-06 10:04:23 -080011988 * @attr ref android.R.styleable#View_layerType
11989 */
11990 public void setLayerType(int layerType, Paint paint) {
11991 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011992 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011993 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11994 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011995
Romain Guyd6cd5722011-01-17 14:42:41 -080011996 if (layerType == mLayerType) {
11997 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11998 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011999 invalidateParentCaches();
12000 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012001 }
12002 return;
12003 }
Romain Guy171c5922011-01-06 10:04:23 -080012004
12005 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012006 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012007 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012008 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012009 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012010 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012011 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012012 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012013 default:
12014 break;
Romain Guy171c5922011-01-06 10:04:23 -080012015 }
12016
12017 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012018 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12019 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12020 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012021
Romain Guy0fd89bf2011-01-26 15:41:30 -080012022 invalidateParentCaches();
12023 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012024 }
12025
12026 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012027 * Indicates whether this view has a static layer. A view with layer type
12028 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12029 * dynamic.
12030 */
12031 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012032 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012033 }
12034
12035 /**
Romain Guy171c5922011-01-06 10:04:23 -080012036 * Indicates what type of layer is currently associated with this view. By default
12037 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12038 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12039 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012040 *
Romain Guy171c5922011-01-06 10:04:23 -080012041 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12042 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012043 *
12044 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012045 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012046 * @see #LAYER_TYPE_NONE
12047 * @see #LAYER_TYPE_SOFTWARE
12048 * @see #LAYER_TYPE_HARDWARE
12049 */
12050 public int getLayerType() {
12051 return mLayerType;
12052 }
Joe Malin32736f02011-01-19 16:14:20 -080012053
Romain Guy6c319ca2011-01-11 14:29:25 -080012054 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012055 * Forces this view's layer to be created and this view to be rendered
12056 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12057 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012058 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012059 * This method can for instance be used to render a view into its layer before
12060 * starting an animation. If this view is complex, rendering into the layer
12061 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012062 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012063 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012064 *
12065 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012066 */
12067 public void buildLayer() {
12068 if (mLayerType == LAYER_TYPE_NONE) return;
12069
12070 if (mAttachInfo == null) {
12071 throw new IllegalStateException("This view must be attached to a window first");
12072 }
12073
12074 switch (mLayerType) {
12075 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012076 if (mAttachInfo.mHardwareRenderer != null &&
12077 mAttachInfo.mHardwareRenderer.isEnabled() &&
12078 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012079 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012080 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012081 break;
12082 case LAYER_TYPE_SOFTWARE:
12083 buildDrawingCache(true);
12084 break;
12085 }
12086 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012087
Romain Guy9c4b79a2011-11-10 19:23:58 -080012088 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12089 void flushLayer() {
12090 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12091 mHardwareLayer.flush();
12092 }
12093 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012094
12095 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012096 * <p>Returns a hardware layer that can be used to draw this view again
12097 * without executing its draw method.</p>
12098 *
12099 * @return A HardwareLayer ready to render, or null if an error occurred.
12100 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012101 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012102 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12103 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012104 return null;
12105 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012106
Romain Guy9c4b79a2011-11-10 19:23:58 -080012107 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012108
12109 final int width = mRight - mLeft;
12110 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012111
Romain Guy6c319ca2011-01-11 14:29:25 -080012112 if (width == 0 || height == 0) {
12113 return null;
12114 }
12115
12116 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12117 if (mHardwareLayer == null) {
12118 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12119 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012120 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012121 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12122 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012123 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012124 }
12125
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012126 // The layer is not valid if the underlying GPU resources cannot be allocated
12127 if (!mHardwareLayer.isValid()) {
12128 return null;
12129 }
12130
Chet Haasea1cff502012-02-21 13:43:44 -080012131 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012132 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012133 }
12134
12135 return mHardwareLayer;
12136 }
Romain Guy171c5922011-01-06 10:04:23 -080012137
Romain Guy589b0bb2011-10-10 13:57:47 -070012138 /**
12139 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012140 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012141 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012142 *
12143 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012144 * @see #LAYER_TYPE_HARDWARE
12145 */
Romain Guya998dff2012-03-23 18:58:36 -070012146 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012147 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012148 AttachInfo info = mAttachInfo;
12149 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012150 info.mHardwareRenderer.isEnabled() &&
12151 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012152 mHardwareLayer.destroy();
12153 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012154
Romain Guy9c4b79a2011-11-10 19:23:58 -080012155 invalidate(true);
12156 invalidateParentCaches();
12157 }
Romain Guy65b345f2011-07-27 18:51:50 -070012158 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012159 }
Romain Guy65b345f2011-07-27 18:51:50 -070012160 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012161 }
12162
Romain Guy171c5922011-01-06 10:04:23 -080012163 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012164 * Destroys all hardware rendering resources. This method is invoked
12165 * when the system needs to reclaim resources. Upon execution of this
12166 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012167 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012168 * Note: you <strong>must</strong> call
12169 * <code>super.destroyHardwareResources()</code> when overriding
12170 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012171 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012172 * @hide
12173 */
12174 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012175 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012176 }
12177
12178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12180 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12181 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12182 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12183 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12184 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012185 *
Romain Guy171c5922011-01-06 10:04:23 -080012186 * <p>Enabling the drawing cache is similar to
12187 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012188 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12189 * drawing cache has no effect on rendering because the system uses a different mechanism
12190 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12191 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12192 * for information on how to enable software and hardware layers.</p>
12193 *
12194 * <p>This API can be used to manually generate
12195 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12196 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012197 *
12198 * @param enabled true to enable the drawing cache, false otherwise
12199 *
12200 * @see #isDrawingCacheEnabled()
12201 * @see #getDrawingCache()
12202 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012203 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012204 */
12205 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012206 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12208 }
12209
12210 /**
12211 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12212 *
12213 * @return true if the drawing cache is enabled
12214 *
12215 * @see #setDrawingCacheEnabled(boolean)
12216 * @see #getDrawingCache()
12217 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012218 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012219 public boolean isDrawingCacheEnabled() {
12220 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12221 }
12222
12223 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012224 * Debugging utility which recursively outputs the dirty state of a view and its
12225 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012226 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012227 * @hide
12228 */
Romain Guy676b1732011-02-14 14:45:33 -080012229 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012230 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12231 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12232 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12233 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12234 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12235 if (clear) {
12236 mPrivateFlags &= clearMask;
12237 }
12238 if (this instanceof ViewGroup) {
12239 ViewGroup parent = (ViewGroup) this;
12240 final int count = parent.getChildCount();
12241 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012242 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012243 child.outputDirtyFlags(indent + " ", clear, clearMask);
12244 }
12245 }
12246 }
12247
12248 /**
12249 * This method is used by ViewGroup to cause its children to restore or recreate their
12250 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12251 * to recreate its own display list, which would happen if it went through the normal
12252 * draw/dispatchDraw mechanisms.
12253 *
12254 * @hide
12255 */
12256 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012257
12258 /**
12259 * A view that is not attached or hardware accelerated cannot create a display list.
12260 * This method checks these conditions and returns the appropriate result.
12261 *
12262 * @return true if view has the ability to create a display list, false otherwise.
12263 *
12264 * @hide
12265 */
12266 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012267 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012268 }
Joe Malin32736f02011-01-19 16:14:20 -080012269
Chet Haasedaf98e92011-01-10 14:10:36 -080012270 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012271 * @return The HardwareRenderer associated with that view or null if hardware rendering
12272 * is not supported or this this has not been attached to a window.
12273 *
12274 * @hide
12275 */
12276 public HardwareRenderer getHardwareRenderer() {
12277 if (mAttachInfo != null) {
12278 return mAttachInfo.mHardwareRenderer;
12279 }
12280 return null;
12281 }
12282
12283 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012284 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12285 * Otherwise, the same display list will be returned (after having been rendered into
12286 * along the way, depending on the invalidation state of the view).
12287 *
12288 * @param displayList The previous version of this displayList, could be null.
12289 * @param isLayer Whether the requester of the display list is a layer. If so,
12290 * the view will avoid creating a layer inside the resulting display list.
12291 * @return A new or reused DisplayList object.
12292 */
12293 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12294 if (!canHaveDisplayList()) {
12295 return null;
12296 }
12297
12298 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12299 displayList == null || !displayList.isValid() ||
12300 (!isLayer && mRecreateDisplayList))) {
12301 // Don't need to recreate the display list, just need to tell our
12302 // children to restore/recreate theirs
12303 if (displayList != null && displayList.isValid() &&
12304 !isLayer && !mRecreateDisplayList) {
12305 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12306 mPrivateFlags &= ~DIRTY_MASK;
12307 dispatchGetDisplayList();
12308
12309 return displayList;
12310 }
12311
12312 if (!isLayer) {
12313 // If we got here, we're recreating it. Mark it as such to ensure that
12314 // we copy in child display lists into ours in drawChild()
12315 mRecreateDisplayList = true;
12316 }
12317 if (displayList == null) {
12318 final String name = getClass().getSimpleName();
12319 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12320 // If we're creating a new display list, make sure our parent gets invalidated
12321 // since they will need to recreate their display list to account for this
12322 // new child display list.
12323 invalidateParentCaches();
12324 }
12325
12326 boolean caching = false;
12327 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012328 int width = mRight - mLeft;
12329 int height = mBottom - mTop;
12330
12331 try {
12332 canvas.setViewport(width, height);
12333 // The dirty rect should always be null for a display list
12334 canvas.onPreDraw(null);
12335 int layerType = (
12336 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12337 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012338 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012339 if (layerType == LAYER_TYPE_HARDWARE) {
12340 final HardwareLayer layer = getHardwareLayer();
12341 if (layer != null && layer.isValid()) {
12342 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12343 } else {
12344 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12345 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12346 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12347 }
12348 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012349 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012350 buildDrawingCache(true);
12351 Bitmap cache = getDrawingCache(true);
12352 if (cache != null) {
12353 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12354 caching = true;
12355 }
Chet Haasea1cff502012-02-21 13:43:44 -080012356 }
Chet Haasea1cff502012-02-21 13:43:44 -080012357 } else {
12358
12359 computeScroll();
12360
Chet Haasea1cff502012-02-21 13:43:44 -080012361 canvas.translate(-mScrollX, -mScrollY);
12362 if (!isLayer) {
12363 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12364 mPrivateFlags &= ~DIRTY_MASK;
12365 }
12366
12367 // Fast path for layouts with no backgrounds
12368 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12369 dispatchDraw(canvas);
12370 } else {
12371 draw(canvas);
12372 }
12373 }
12374 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012375 canvas.onPostDraw();
12376
12377 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012378 displayList.setCaching(caching);
12379 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012380 displayList.setLeftTopRightBottom(0, 0, width, height);
12381 } else {
12382 setDisplayListProperties(displayList);
12383 }
12384 }
12385 } else if (!isLayer) {
12386 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12387 mPrivateFlags &= ~DIRTY_MASK;
12388 }
12389
12390 return displayList;
12391 }
12392
12393 /**
12394 * Get the DisplayList for the HardwareLayer
12395 *
12396 * @param layer The HardwareLayer whose DisplayList we want
12397 * @return A DisplayList fopr the specified HardwareLayer
12398 */
12399 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12400 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12401 layer.setDisplayList(displayList);
12402 return displayList;
12403 }
12404
12405
12406 /**
Romain Guyb051e892010-09-28 19:09:36 -070012407 * <p>Returns a display list that can be used to draw this view again
12408 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012409 *
Romain Guyb051e892010-09-28 19:09:36 -070012410 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012411 *
12412 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012413 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012414 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012415 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012416 return mDisplayList;
12417 }
12418
Romain Guy38c2ece2012-05-24 14:20:56 -070012419 private void clearDisplayList() {
12420 if (mDisplayList != null) {
12421 mDisplayList.invalidate();
12422 mDisplayList.clear();
12423 }
12424 }
12425
Romain Guyb051e892010-09-28 19:09:36 -070012426 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012427 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012428 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012429 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012430 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012431 * @see #getDrawingCache(boolean)
12432 */
12433 public Bitmap getDrawingCache() {
12434 return getDrawingCache(false);
12435 }
12436
12437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012438 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12439 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12440 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12441 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12442 * request the drawing cache by calling this method and draw it on screen if the
12443 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012444 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012445 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12446 * this method will create a bitmap of the same size as this view. Because this bitmap
12447 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12448 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12449 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12450 * size than the view. This implies that your application must be able to handle this
12451 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012452 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012453 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12454 * the current density of the screen when the application is in compatibility
12455 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012456 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012457 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459 * @see #setDrawingCacheEnabled(boolean)
12460 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012461 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012462 * @see #destroyDrawingCache()
12463 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012464 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12466 return null;
12467 }
12468 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012469 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012470 }
Romain Guy02890fd2010-08-06 17:58:44 -070012471 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012472 }
12473
12474 /**
12475 * <p>Frees the resources used by the drawing cache. If you call
12476 * {@link #buildDrawingCache()} manually without calling
12477 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12478 * should cleanup the cache with this method afterwards.</p>
12479 *
12480 * @see #setDrawingCacheEnabled(boolean)
12481 * @see #buildDrawingCache()
12482 * @see #getDrawingCache()
12483 */
12484 public void destroyDrawingCache() {
12485 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012486 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012487 mDrawingCache = null;
12488 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012489 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012490 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012491 mUnscaledDrawingCache = null;
12492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012493 }
12494
12495 /**
12496 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012497 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012498 * view will always be drawn on top of a solid color.
12499 *
12500 * @param color The background color to use for the drawing cache's bitmap
12501 *
12502 * @see #setDrawingCacheEnabled(boolean)
12503 * @see #buildDrawingCache()
12504 * @see #getDrawingCache()
12505 */
12506 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012507 if (color != mDrawingCacheBackgroundColor) {
12508 mDrawingCacheBackgroundColor = color;
12509 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012511 }
12512
12513 /**
12514 * @see #setDrawingCacheBackgroundColor(int)
12515 *
12516 * @return The background color to used for the drawing cache's bitmap
12517 */
12518 public int getDrawingCacheBackgroundColor() {
12519 return mDrawingCacheBackgroundColor;
12520 }
12521
12522 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012523 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012524 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012525 * @see #buildDrawingCache(boolean)
12526 */
12527 public void buildDrawingCache() {
12528 buildDrawingCache(false);
12529 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012530
Romain Guyfbd8f692009-06-26 14:51:58 -070012531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12533 *
12534 * <p>If you call {@link #buildDrawingCache()} manually without calling
12535 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12536 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012537 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012538 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12539 * this method will create a bitmap of the same size as this view. Because this bitmap
12540 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12541 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12542 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12543 * size than the view. This implies that your application must be able to handle this
12544 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012545 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012546 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12547 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012548 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012549 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012550 *
12551 * @see #getDrawingCache()
12552 * @see #destroyDrawingCache()
12553 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012554 public void buildDrawingCache(boolean autoScale) {
12555 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012556 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012557 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012558
Romain Guy8506ab42009-06-11 17:35:47 -070012559 int width = mRight - mLeft;
12560 int height = mBottom - mTop;
12561
12562 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012563 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012564
Romain Guye1123222009-06-29 14:24:56 -070012565 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012566 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12567 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012569
12570 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012571 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012572 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573
12574 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012575 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012576 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012577 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12578 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012579 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580 return;
12581 }
12582
12583 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012584 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012585
12586 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587 Bitmap.Config quality;
12588 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012589 // Never pick ARGB_4444 because it looks awful
12590 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012591 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12592 case DRAWING_CACHE_QUALITY_AUTO:
12593 quality = Bitmap.Config.ARGB_8888;
12594 break;
12595 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012596 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012597 break;
12598 case DRAWING_CACHE_QUALITY_HIGH:
12599 quality = Bitmap.Config.ARGB_8888;
12600 break;
12601 default:
12602 quality = Bitmap.Config.ARGB_8888;
12603 break;
12604 }
12605 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012606 // Optimization for translucent windows
12607 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012608 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012609 }
12610
12611 // Try to cleanup memory
12612 if (bitmap != null) bitmap.recycle();
12613
12614 try {
12615 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012616 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012617 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012618 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012619 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012620 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012621 }
Adam Powell26153a32010-11-08 15:22:27 -080012622 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012623 } catch (OutOfMemoryError e) {
12624 // If there is not enough memory to create the bitmap cache, just
12625 // ignore the issue as bitmap caches are not required to draw the
12626 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012627 if (autoScale) {
12628 mDrawingCache = null;
12629 } else {
12630 mUnscaledDrawingCache = null;
12631 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012632 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012633 return;
12634 }
12635
12636 clear = drawingCacheBackgroundColor != 0;
12637 }
12638
12639 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 if (attachInfo != null) {
12641 canvas = attachInfo.mCanvas;
12642 if (canvas == null) {
12643 canvas = new Canvas();
12644 }
12645 canvas.setBitmap(bitmap);
12646 // Temporarily clobber the cached Canvas in case one of our children
12647 // is also using a drawing cache. Without this, the children would
12648 // steal the canvas by attaching their own bitmap to it and bad, bad
12649 // thing would happen (invisible views, corrupted drawings, etc.)
12650 attachInfo.mCanvas = null;
12651 } else {
12652 // This case should hopefully never or seldom happen
12653 canvas = new Canvas(bitmap);
12654 }
12655
12656 if (clear) {
12657 bitmap.eraseColor(drawingCacheBackgroundColor);
12658 }
12659
12660 computeScroll();
12661 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012662
Romain Guye1123222009-06-29 14:24:56 -070012663 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012664 final float scale = attachInfo.mApplicationScale;
12665 canvas.scale(scale, scale);
12666 }
Joe Malin32736f02011-01-19 16:14:20 -080012667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012668 canvas.translate(-mScrollX, -mScrollY);
12669
Romain Guy5bcdff42009-05-14 21:27:18 -070012670 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012671 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12672 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012673 mPrivateFlags |= DRAWING_CACHE_VALID;
12674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675
12676 // Fast path for layouts with no backgrounds
12677 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012678 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012679 dispatchDraw(canvas);
12680 } else {
12681 draw(canvas);
12682 }
12683
12684 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012685 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012686
12687 if (attachInfo != null) {
12688 // Restore the cached Canvas for our siblings
12689 attachInfo.mCanvas = canvas;
12690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 }
12692 }
12693
12694 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012695 * Create a snapshot of the view into a bitmap. We should probably make
12696 * some form of this public, but should think about the API.
12697 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012698 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012699 int width = mRight - mLeft;
12700 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012701
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012702 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012703 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012704 width = (int) ((width * scale) + 0.5f);
12705 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012706
Romain Guy8c11e312009-09-14 15:15:30 -070012707 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012708 if (bitmap == null) {
12709 throw new OutOfMemoryError();
12710 }
12711
Romain Guyc529d8d2011-09-06 15:01:39 -070012712 Resources resources = getResources();
12713 if (resources != null) {
12714 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12715 }
Joe Malin32736f02011-01-19 16:14:20 -080012716
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012717 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012718 if (attachInfo != null) {
12719 canvas = attachInfo.mCanvas;
12720 if (canvas == null) {
12721 canvas = new Canvas();
12722 }
12723 canvas.setBitmap(bitmap);
12724 // Temporarily clobber the cached Canvas in case one of our children
12725 // is also using a drawing cache. Without this, the children would
12726 // steal the canvas by attaching their own bitmap to it and bad, bad
12727 // things would happen (invisible views, corrupted drawings, etc.)
12728 attachInfo.mCanvas = null;
12729 } else {
12730 // This case should hopefully never or seldom happen
12731 canvas = new Canvas(bitmap);
12732 }
12733
Romain Guy5bcdff42009-05-14 21:27:18 -070012734 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012735 bitmap.eraseColor(backgroundColor);
12736 }
12737
12738 computeScroll();
12739 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012740 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012741 canvas.translate(-mScrollX, -mScrollY);
12742
Romain Guy5bcdff42009-05-14 21:27:18 -070012743 // Temporarily remove the dirty mask
12744 int flags = mPrivateFlags;
12745 mPrivateFlags &= ~DIRTY_MASK;
12746
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012747 // Fast path for layouts with no backgrounds
12748 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12749 dispatchDraw(canvas);
12750 } else {
12751 draw(canvas);
12752 }
12753
Romain Guy5bcdff42009-05-14 21:27:18 -070012754 mPrivateFlags = flags;
12755
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012756 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012757 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012758
12759 if (attachInfo != null) {
12760 // Restore the cached Canvas for our siblings
12761 attachInfo.mCanvas = canvas;
12762 }
Romain Guy8506ab42009-06-11 17:35:47 -070012763
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012764 return bitmap;
12765 }
12766
12767 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012768 * Indicates whether this View is currently in edit mode. A View is usually
12769 * in edit mode when displayed within a developer tool. For instance, if
12770 * this View is being drawn by a visual user interface builder, this method
12771 * should return true.
12772 *
12773 * Subclasses should check the return value of this method to provide
12774 * different behaviors if their normal behavior might interfere with the
12775 * host environment. For instance: the class spawns a thread in its
12776 * constructor, the drawing code relies on device-specific features, etc.
12777 *
12778 * This method is usually checked in the drawing code of custom widgets.
12779 *
12780 * @return True if this View is in edit mode, false otherwise.
12781 */
12782 public boolean isInEditMode() {
12783 return false;
12784 }
12785
12786 /**
12787 * If the View draws content inside its padding and enables fading edges,
12788 * it needs to support padding offsets. Padding offsets are added to the
12789 * fading edges to extend the length of the fade so that it covers pixels
12790 * drawn inside the padding.
12791 *
12792 * Subclasses of this class should override this method if they need
12793 * to draw content inside the padding.
12794 *
12795 * @return True if padding offset must be applied, false otherwise.
12796 *
12797 * @see #getLeftPaddingOffset()
12798 * @see #getRightPaddingOffset()
12799 * @see #getTopPaddingOffset()
12800 * @see #getBottomPaddingOffset()
12801 *
12802 * @since CURRENT
12803 */
12804 protected boolean isPaddingOffsetRequired() {
12805 return false;
12806 }
12807
12808 /**
12809 * Amount by which to extend the left fading region. Called only when
12810 * {@link #isPaddingOffsetRequired()} returns true.
12811 *
12812 * @return The left padding offset in pixels.
12813 *
12814 * @see #isPaddingOffsetRequired()
12815 *
12816 * @since CURRENT
12817 */
12818 protected int getLeftPaddingOffset() {
12819 return 0;
12820 }
12821
12822 /**
12823 * Amount by which to extend the right fading region. Called only when
12824 * {@link #isPaddingOffsetRequired()} returns true.
12825 *
12826 * @return The right padding offset in pixels.
12827 *
12828 * @see #isPaddingOffsetRequired()
12829 *
12830 * @since CURRENT
12831 */
12832 protected int getRightPaddingOffset() {
12833 return 0;
12834 }
12835
12836 /**
12837 * Amount by which to extend the top fading region. Called only when
12838 * {@link #isPaddingOffsetRequired()} returns true.
12839 *
12840 * @return The top padding offset in pixels.
12841 *
12842 * @see #isPaddingOffsetRequired()
12843 *
12844 * @since CURRENT
12845 */
12846 protected int getTopPaddingOffset() {
12847 return 0;
12848 }
12849
12850 /**
12851 * Amount by which to extend the bottom fading region. Called only when
12852 * {@link #isPaddingOffsetRequired()} returns true.
12853 *
12854 * @return The bottom padding offset in pixels.
12855 *
12856 * @see #isPaddingOffsetRequired()
12857 *
12858 * @since CURRENT
12859 */
12860 protected int getBottomPaddingOffset() {
12861 return 0;
12862 }
12863
12864 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012865 * @hide
12866 * @param offsetRequired
12867 */
12868 protected int getFadeTop(boolean offsetRequired) {
12869 int top = mPaddingTop;
12870 if (offsetRequired) top += getTopPaddingOffset();
12871 return top;
12872 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012873
Romain Guyf2fc4602011-07-19 15:20:03 -070012874 /**
12875 * @hide
12876 * @param offsetRequired
12877 */
12878 protected int getFadeHeight(boolean offsetRequired) {
12879 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012880 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012881 return mBottom - mTop - mPaddingBottom - padding;
12882 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012883
Romain Guyf2fc4602011-07-19 15:20:03 -070012884 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012885 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012886 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012887 *
Romain Guy2bffd262010-09-12 17:40:02 -070012888 * <p>Even if this method returns true, it does not mean that every call
12889 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12890 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012891 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012892 * window is hardware accelerated,
12893 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12894 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012895 *
Romain Guy2bffd262010-09-12 17:40:02 -070012896 * @return True if the view is attached to a window and the window is
12897 * hardware accelerated; false in any other case.
12898 */
12899 public boolean isHardwareAccelerated() {
12900 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12901 }
Joe Malin32736f02011-01-19 16:14:20 -080012902
Romain Guy2bffd262010-09-12 17:40:02 -070012903 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012904 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12905 * case of an active Animation being run on the view.
12906 */
12907 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12908 Animation a, boolean scalingRequired) {
12909 Transformation invalidationTransform;
12910 final int flags = parent.mGroupFlags;
12911 final boolean initialized = a.isInitialized();
12912 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012913 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012914 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012915 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012916 onAnimationStart();
12917 }
12918
12919 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12920 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12921 if (parent.mInvalidationTransformation == null) {
12922 parent.mInvalidationTransformation = new Transformation();
12923 }
12924 invalidationTransform = parent.mInvalidationTransformation;
12925 a.getTransformation(drawingTime, invalidationTransform, 1f);
12926 } else {
12927 invalidationTransform = parent.mChildTransformation;
12928 }
Romain Guy393a52c2012-05-22 20:21:08 -070012929
Chet Haasebcca79a2012-02-14 08:45:14 -080012930 if (more) {
12931 if (!a.willChangeBounds()) {
12932 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12933 parent.FLAG_OPTIMIZE_INVALIDATE) {
12934 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12935 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12936 // The child need to draw an animation, potentially offscreen, so
12937 // make sure we do not cancel invalidate requests
12938 parent.mPrivateFlags |= DRAW_ANIMATION;
12939 parent.invalidate(mLeft, mTop, mRight, mBottom);
12940 }
12941 } else {
12942 if (parent.mInvalidateRegion == null) {
12943 parent.mInvalidateRegion = new RectF();
12944 }
12945 final RectF region = parent.mInvalidateRegion;
12946 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12947 invalidationTransform);
12948
12949 // The child need to draw an animation, potentially offscreen, so
12950 // make sure we do not cancel invalidate requests
12951 parent.mPrivateFlags |= DRAW_ANIMATION;
12952
12953 final int left = mLeft + (int) region.left;
12954 final int top = mTop + (int) region.top;
12955 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12956 top + (int) (region.height() + .5f));
12957 }
12958 }
12959 return more;
12960 }
12961
Chet Haasea1cff502012-02-21 13:43:44 -080012962 /**
12963 * This method is called by getDisplayList() when a display list is created or re-rendered.
12964 * It sets or resets the current value of all properties on that display list (resetting is
12965 * necessary when a display list is being re-created, because we need to make sure that
12966 * previously-set transform values
12967 */
12968 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012969 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012970 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012971 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012972 if (mParent instanceof ViewGroup) {
12973 displayList.setClipChildren(
12974 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12975 }
Chet Haase9420abd2012-03-29 16:28:32 -070012976 float alpha = 1;
12977 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12978 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12979 ViewGroup parentVG = (ViewGroup) mParent;
12980 final boolean hasTransform =
12981 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12982 if (hasTransform) {
12983 Transformation transform = parentVG.mChildTransformation;
12984 final int transformType = parentVG.mChildTransformation.getTransformationType();
12985 if (transformType != Transformation.TYPE_IDENTITY) {
12986 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12987 alpha = transform.getAlpha();
12988 }
12989 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12990 displayList.setStaticMatrix(transform.getMatrix());
12991 }
12992 }
12993 }
Chet Haasea1cff502012-02-21 13:43:44 -080012994 }
12995 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012996 alpha *= mTransformationInfo.mAlpha;
12997 if (alpha < 1) {
12998 final int multipliedAlpha = (int) (255 * alpha);
12999 if (onSetAlpha(multipliedAlpha)) {
13000 alpha = 1;
13001 }
13002 }
13003 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013004 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13005 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13006 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13007 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013008 if (mTransformationInfo.mCamera == null) {
13009 mTransformationInfo.mCamera = new Camera();
13010 mTransformationInfo.matrix3D = new Matrix();
13011 }
13012 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080013013 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
13014 displayList.setPivotX(getPivotX());
13015 displayList.setPivotY(getPivotY());
13016 }
Chet Haase9420abd2012-03-29 16:28:32 -070013017 } else if (alpha < 1) {
13018 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013019 }
13020 }
13021 }
13022
Chet Haasebcca79a2012-02-14 08:45:14 -080013023 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013024 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13025 * This draw() method is an implementation detail and is not intended to be overridden or
13026 * to be called from anywhere else other than ViewGroup.drawChild().
13027 */
13028 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013029 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013030 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013031 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013032 final int flags = parent.mGroupFlags;
13033
Chet Haasea1cff502012-02-21 13:43:44 -080013034 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013035 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013036 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013037 }
13038
13039 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013040 boolean concatMatrix = false;
13041
13042 boolean scalingRequired = false;
13043 boolean caching;
13044 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
13045
13046 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013047 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13048 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013049 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013050 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013051 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13052 } else {
13053 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13054 }
13055
Chet Haasebcca79a2012-02-14 08:45:14 -080013056 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013057 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013058 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013059 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013060 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070013061 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013062 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013063 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013064 } else {
Chet Haase21433372012-06-05 07:54:09 -070013065 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013066 mDisplayList != null) {
13067 // No longer animating: clear out old animation matrix
13068 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070013069 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013070 }
13071 if (!useDisplayListProperties &&
13072 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13073 final boolean hasTransform =
13074 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13075 if (hasTransform) {
13076 final int transformType = parent.mChildTransformation.getTransformationType();
13077 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13078 parent.mChildTransformation : null;
13079 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13080 }
Chet Haase64a48c12012-02-13 16:33:29 -080013081 }
13082 }
13083
13084 concatMatrix |= !childHasIdentityMatrix;
13085
13086 // Sets the flag as early as possible to allow draw() implementations
13087 // to call invalidate() successfully when doing animations
13088 mPrivateFlags |= DRAWN;
13089
Chet Haasebcca79a2012-02-14 08:45:14 -080013090 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013091 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013092 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013093 return more;
13094 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013095 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013096
13097 if (hardwareAccelerated) {
13098 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13099 // retain the flag's value temporarily in the mRecreateDisplayList flag
13100 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13101 mPrivateFlags &= ~INVALIDATED;
13102 }
13103
13104 computeScroll();
13105
13106 final int sx = mScrollX;
13107 final int sy = mScrollY;
13108
13109 DisplayList displayList = null;
13110 Bitmap cache = null;
13111 boolean hasDisplayList = false;
13112 if (caching) {
13113 if (!hardwareAccelerated) {
13114 if (layerType != LAYER_TYPE_NONE) {
13115 layerType = LAYER_TYPE_SOFTWARE;
13116 buildDrawingCache(true);
13117 }
13118 cache = getDrawingCache(true);
13119 } else {
13120 switch (layerType) {
13121 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013122 if (useDisplayListProperties) {
13123 hasDisplayList = canHaveDisplayList();
13124 } else {
13125 buildDrawingCache(true);
13126 cache = getDrawingCache(true);
13127 }
Chet Haase64a48c12012-02-13 16:33:29 -080013128 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013129 case LAYER_TYPE_HARDWARE:
13130 if (useDisplayListProperties) {
13131 hasDisplayList = canHaveDisplayList();
13132 }
13133 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013134 case LAYER_TYPE_NONE:
13135 // Delay getting the display list until animation-driven alpha values are
13136 // set up and possibly passed on to the view
13137 hasDisplayList = canHaveDisplayList();
13138 break;
13139 }
13140 }
13141 }
Chet Haasea1cff502012-02-21 13:43:44 -080013142 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013143 if (useDisplayListProperties) {
13144 displayList = getDisplayList();
13145 if (!displayList.isValid()) {
13146 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13147 // to getDisplayList(), the display list will be marked invalid and we should not
13148 // try to use it again.
13149 displayList = null;
13150 hasDisplayList = false;
13151 useDisplayListProperties = false;
13152 }
13153 }
Chet Haase64a48c12012-02-13 16:33:29 -080013154
13155 final boolean hasNoCache = cache == null || hasDisplayList;
13156 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13157 layerType != LAYER_TYPE_HARDWARE;
13158
Chet Haasea1cff502012-02-21 13:43:44 -080013159 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013160 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013161 restoreTo = canvas.save();
13162 }
Chet Haase64a48c12012-02-13 16:33:29 -080013163 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013164 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013165 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013166 if (!useDisplayListProperties) {
13167 canvas.translate(mLeft, mTop);
13168 }
Chet Haase64a48c12012-02-13 16:33:29 -080013169 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013170 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013171 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013172 restoreTo = canvas.save();
13173 }
Chet Haase64a48c12012-02-13 16:33:29 -080013174 // mAttachInfo cannot be null, otherwise scalingRequired == false
13175 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13176 canvas.scale(scale, scale);
13177 }
13178 }
13179
Chet Haasea1cff502012-02-21 13:43:44 -080013180 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013181 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
13182 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013183 if (transformToApply != null || !childHasIdentityMatrix) {
13184 int transX = 0;
13185 int transY = 0;
13186
13187 if (offsetForScroll) {
13188 transX = -sx;
13189 transY = -sy;
13190 }
13191
13192 if (transformToApply != null) {
13193 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013194 if (useDisplayListProperties) {
13195 displayList.setAnimationMatrix(transformToApply.getMatrix());
13196 } else {
13197 // Undo the scroll translation, apply the transformation matrix,
13198 // then redo the scroll translate to get the correct result.
13199 canvas.translate(-transX, -transY);
13200 canvas.concat(transformToApply.getMatrix());
13201 canvas.translate(transX, transY);
13202 }
Chet Haasea1cff502012-02-21 13:43:44 -080013203 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013204 }
13205
13206 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013207 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013208 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013209 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013210 }
13211 }
13212
Chet Haasea1cff502012-02-21 13:43:44 -080013213 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013214 canvas.translate(-transX, -transY);
13215 canvas.concat(getMatrix());
13216 canvas.translate(transX, transY);
13217 }
13218 }
13219
Chet Haase21433372012-06-05 07:54:09 -070013220 // Deal with alpha if it is or used to be <1
13221 if (alpha < 1 ||
13222 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13223 if (alpha < 1) {
13224 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13225 } else {
13226 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13227 }
Chet Haasea1cff502012-02-21 13:43:44 -080013228 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013229 if (hasNoCache) {
13230 final int multipliedAlpha = (int) (255 * alpha);
13231 if (!onSetAlpha(multipliedAlpha)) {
13232 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013233 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013234 layerType != LAYER_TYPE_NONE) {
13235 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13236 }
Chet Haase9420abd2012-03-29 16:28:32 -070013237 if (useDisplayListProperties) {
13238 displayList.setAlpha(alpha * getAlpha());
13239 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013240 final int scrollX = hasDisplayList ? 0 : sx;
13241 final int scrollY = hasDisplayList ? 0 : sy;
13242 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13243 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013244 }
13245 } else {
13246 // Alpha is handled by the child directly, clobber the layer's alpha
13247 mPrivateFlags |= ALPHA_SET;
13248 }
13249 }
13250 }
13251 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13252 onSetAlpha(255);
13253 mPrivateFlags &= ~ALPHA_SET;
13254 }
13255
Chet Haasea1cff502012-02-21 13:43:44 -080013256 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13257 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013258 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013259 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013260 } else {
13261 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013262 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013263 } else {
13264 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13265 }
13266 }
13267 }
13268
Chet Haase9420abd2012-03-29 16:28:32 -070013269 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013270 displayList = getDisplayList();
13271 if (!displayList.isValid()) {
13272 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13273 // to getDisplayList(), the display list will be marked invalid and we should not
13274 // try to use it again.
13275 displayList = null;
13276 hasDisplayList = false;
13277 }
13278 }
13279
13280 if (hasNoCache) {
13281 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013282 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013283 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013284 if (layer != null && layer.isValid()) {
13285 mLayerPaint.setAlpha((int) (alpha * 255));
13286 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13287 layerRendered = true;
13288 } else {
13289 final int scrollX = hasDisplayList ? 0 : sx;
13290 final int scrollY = hasDisplayList ? 0 : sy;
13291 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013292 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013293 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13294 }
13295 }
13296
13297 if (!layerRendered) {
13298 if (!hasDisplayList) {
13299 // Fast path for layouts with no backgrounds
13300 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013301 mPrivateFlags &= ~DIRTY_MASK;
13302 dispatchDraw(canvas);
13303 } else {
13304 draw(canvas);
13305 }
13306 } else {
13307 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013308 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013309 }
13310 }
13311 } else if (cache != null) {
13312 mPrivateFlags &= ~DIRTY_MASK;
13313 Paint cachePaint;
13314
13315 if (layerType == LAYER_TYPE_NONE) {
13316 cachePaint = parent.mCachePaint;
13317 if (cachePaint == null) {
13318 cachePaint = new Paint();
13319 cachePaint.setDither(false);
13320 parent.mCachePaint = cachePaint;
13321 }
Chet Haase9420abd2012-03-29 16:28:32 -070013322 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013323 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013324 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13325 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013326 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013327 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013328 }
13329 } else {
13330 cachePaint = mLayerPaint;
13331 cachePaint.setAlpha((int) (alpha * 255));
13332 }
13333 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13334 }
13335
Chet Haasea1cff502012-02-21 13:43:44 -080013336 if (restoreTo >= 0) {
13337 canvas.restoreToCount(restoreTo);
13338 }
Chet Haase64a48c12012-02-13 16:33:29 -080013339
13340 if (a != null && !more) {
13341 if (!hardwareAccelerated && !a.getFillAfter()) {
13342 onSetAlpha(255);
13343 }
13344 parent.finishAnimatingView(this, a);
13345 }
13346
13347 if (more && hardwareAccelerated) {
13348 // invalidation is the trigger to recreate display lists, so if we're using
13349 // display lists to render, force an invalidate to allow the animation to
13350 // continue drawing another frame
13351 parent.invalidate(true);
13352 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13353 // alpha animations should cause the child to recreate its display list
13354 invalidate(true);
13355 }
13356 }
13357
13358 mRecreateDisplayList = false;
13359
13360 return more;
13361 }
13362
13363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013364 * Manually render this view (and all of its children) to the given Canvas.
13365 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013366 * called. When implementing a view, implement
13367 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13368 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013369 *
13370 * @param canvas The Canvas to which the View is rendered.
13371 */
13372 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013373 final int privateFlags = mPrivateFlags;
13374 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13375 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13376 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013378 /*
13379 * Draw traversal performs several drawing steps which must be executed
13380 * in the appropriate order:
13381 *
13382 * 1. Draw the background
13383 * 2. If necessary, save the canvas' layers to prepare for fading
13384 * 3. Draw view's content
13385 * 4. Draw children
13386 * 5. If necessary, draw the fading edges and restore layers
13387 * 6. Draw decorations (scrollbars for instance)
13388 */
13389
13390 // Step 1, draw the background, if needed
13391 int saveCount;
13392
Romain Guy24443ea2009-05-11 11:56:30 -070013393 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013394 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013395 if (background != null) {
13396 final int scrollX = mScrollX;
13397 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013398
Romain Guy24443ea2009-05-11 11:56:30 -070013399 if (mBackgroundSizeChanged) {
13400 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13401 mBackgroundSizeChanged = false;
13402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013403
Romain Guy24443ea2009-05-11 11:56:30 -070013404 if ((scrollX | scrollY) == 0) {
13405 background.draw(canvas);
13406 } else {
13407 canvas.translate(scrollX, scrollY);
13408 background.draw(canvas);
13409 canvas.translate(-scrollX, -scrollY);
13410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 }
13412 }
13413
13414 // skip step 2 & 5 if possible (common case)
13415 final int viewFlags = mViewFlags;
13416 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13417 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13418 if (!verticalEdges && !horizontalEdges) {
13419 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013420 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013421
13422 // Step 4, draw the children
13423 dispatchDraw(canvas);
13424
13425 // Step 6, draw decorations (scrollbars)
13426 onDrawScrollBars(canvas);
13427
13428 // we're done...
13429 return;
13430 }
13431
13432 /*
13433 * Here we do the full fledged routine...
13434 * (this is an uncommon case where speed matters less,
13435 * this is why we repeat some of the tests that have been
13436 * done above)
13437 */
13438
13439 boolean drawTop = false;
13440 boolean drawBottom = false;
13441 boolean drawLeft = false;
13442 boolean drawRight = false;
13443
13444 float topFadeStrength = 0.0f;
13445 float bottomFadeStrength = 0.0f;
13446 float leftFadeStrength = 0.0f;
13447 float rightFadeStrength = 0.0f;
13448
13449 // Step 2, save the canvas' layers
13450 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013451
13452 final boolean offsetRequired = isPaddingOffsetRequired();
13453 if (offsetRequired) {
13454 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013455 }
13456
13457 int left = mScrollX + paddingLeft;
13458 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013459 int top = mScrollY + getFadeTop(offsetRequired);
13460 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013461
13462 if (offsetRequired) {
13463 right += getRightPaddingOffset();
13464 bottom += getBottomPaddingOffset();
13465 }
13466
13467 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013468 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013469 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013470
13471 // clip the fade length if top and bottom fades overlap
13472 // overlapping fades produce odd-looking artifacts
13473 if (verticalEdges && (top + length > bottom - length)) {
13474 length = (bottom - top) / 2;
13475 }
13476
13477 // also clip horizontal fades if necessary
13478 if (horizontalEdges && (left + length > right - length)) {
13479 length = (right - left) / 2;
13480 }
13481
13482 if (verticalEdges) {
13483 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013484 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013486 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013487 }
13488
13489 if (horizontalEdges) {
13490 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013491 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013492 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013493 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494 }
13495
13496 saveCount = canvas.getSaveCount();
13497
13498 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013499 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13501
13502 if (drawTop) {
13503 canvas.saveLayer(left, top, right, top + length, null, flags);
13504 }
13505
13506 if (drawBottom) {
13507 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13508 }
13509
13510 if (drawLeft) {
13511 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13512 }
13513
13514 if (drawRight) {
13515 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13516 }
13517 } else {
13518 scrollabilityCache.setFadeColor(solidColor);
13519 }
13520
13521 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013522 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013523
13524 // Step 4, draw the children
13525 dispatchDraw(canvas);
13526
13527 // Step 5, draw the fade effect and restore layers
13528 final Paint p = scrollabilityCache.paint;
13529 final Matrix matrix = scrollabilityCache.matrix;
13530 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013531
13532 if (drawTop) {
13533 matrix.setScale(1, fadeHeight * topFadeStrength);
13534 matrix.postTranslate(left, top);
13535 fade.setLocalMatrix(matrix);
13536 canvas.drawRect(left, top, right, top + length, p);
13537 }
13538
13539 if (drawBottom) {
13540 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13541 matrix.postRotate(180);
13542 matrix.postTranslate(left, bottom);
13543 fade.setLocalMatrix(matrix);
13544 canvas.drawRect(left, bottom - length, right, bottom, p);
13545 }
13546
13547 if (drawLeft) {
13548 matrix.setScale(1, fadeHeight * leftFadeStrength);
13549 matrix.postRotate(-90);
13550 matrix.postTranslate(left, top);
13551 fade.setLocalMatrix(matrix);
13552 canvas.drawRect(left, top, left + length, bottom, p);
13553 }
13554
13555 if (drawRight) {
13556 matrix.setScale(1, fadeHeight * rightFadeStrength);
13557 matrix.postRotate(90);
13558 matrix.postTranslate(right, top);
13559 fade.setLocalMatrix(matrix);
13560 canvas.drawRect(right - length, top, right, bottom, p);
13561 }
13562
13563 canvas.restoreToCount(saveCount);
13564
13565 // Step 6, draw decorations (scrollbars)
13566 onDrawScrollBars(canvas);
13567 }
13568
13569 /**
13570 * Override this if your view is known to always be drawn on top of a solid color background,
13571 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13572 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13573 * should be set to 0xFF.
13574 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013575 * @see #setVerticalFadingEdgeEnabled(boolean)
13576 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013577 *
13578 * @return The known solid color background for this view, or 0 if the color may vary
13579 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013580 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013581 public int getSolidColor() {
13582 return 0;
13583 }
13584
13585 /**
13586 * Build a human readable string representation of the specified view flags.
13587 *
13588 * @param flags the view flags to convert to a string
13589 * @return a String representing the supplied flags
13590 */
13591 private static String printFlags(int flags) {
13592 String output = "";
13593 int numFlags = 0;
13594 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13595 output += "TAKES_FOCUS";
13596 numFlags++;
13597 }
13598
13599 switch (flags & VISIBILITY_MASK) {
13600 case INVISIBLE:
13601 if (numFlags > 0) {
13602 output += " ";
13603 }
13604 output += "INVISIBLE";
13605 // USELESS HERE numFlags++;
13606 break;
13607 case GONE:
13608 if (numFlags > 0) {
13609 output += " ";
13610 }
13611 output += "GONE";
13612 // USELESS HERE numFlags++;
13613 break;
13614 default:
13615 break;
13616 }
13617 return output;
13618 }
13619
13620 /**
13621 * Build a human readable string representation of the specified private
13622 * view flags.
13623 *
13624 * @param privateFlags the private view flags to convert to a string
13625 * @return a String representing the supplied flags
13626 */
13627 private static String printPrivateFlags(int privateFlags) {
13628 String output = "";
13629 int numFlags = 0;
13630
13631 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13632 output += "WANTS_FOCUS";
13633 numFlags++;
13634 }
13635
13636 if ((privateFlags & FOCUSED) == FOCUSED) {
13637 if (numFlags > 0) {
13638 output += " ";
13639 }
13640 output += "FOCUSED";
13641 numFlags++;
13642 }
13643
13644 if ((privateFlags & SELECTED) == SELECTED) {
13645 if (numFlags > 0) {
13646 output += " ";
13647 }
13648 output += "SELECTED";
13649 numFlags++;
13650 }
13651
13652 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13653 if (numFlags > 0) {
13654 output += " ";
13655 }
13656 output += "IS_ROOT_NAMESPACE";
13657 numFlags++;
13658 }
13659
13660 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13661 if (numFlags > 0) {
13662 output += " ";
13663 }
13664 output += "HAS_BOUNDS";
13665 numFlags++;
13666 }
13667
13668 if ((privateFlags & DRAWN) == DRAWN) {
13669 if (numFlags > 0) {
13670 output += " ";
13671 }
13672 output += "DRAWN";
13673 // USELESS HERE numFlags++;
13674 }
13675 return output;
13676 }
13677
13678 /**
13679 * <p>Indicates whether or not this view's layout will be requested during
13680 * the next hierarchy layout pass.</p>
13681 *
13682 * @return true if the layout will be forced during next layout pass
13683 */
13684 public boolean isLayoutRequested() {
13685 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13686 }
13687
13688 /**
13689 * Assign a size and position to a view and all of its
13690 * descendants
13691 *
13692 * <p>This is the second phase of the layout mechanism.
13693 * (The first is measuring). In this phase, each parent calls
13694 * layout on all of its children to position them.
13695 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013696 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013697 *
Chet Haase9c087442011-01-12 16:20:16 -080013698 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013699 * Derived classes with children should override
13700 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013701 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013702 *
13703 * @param l Left position, relative to parent
13704 * @param t Top position, relative to parent
13705 * @param r Right position, relative to parent
13706 * @param b Bottom position, relative to parent
13707 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013708 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013709 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013710 int oldL = mLeft;
13711 int oldT = mTop;
13712 int oldB = mBottom;
13713 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013714 boolean changed = setFrame(l, t, r, b);
13715 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013716 onLayout(changed, l, t, r, b);
13717 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013718
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013719 ListenerInfo li = mListenerInfo;
13720 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013721 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013722 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013723 int numListeners = listenersCopy.size();
13724 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013725 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013726 }
13727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013728 }
13729 mPrivateFlags &= ~FORCE_LAYOUT;
13730 }
13731
13732 /**
13733 * Called from layout when this view should
13734 * assign a size and position to each of its children.
13735 *
13736 * Derived classes with children should override
13737 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013738 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 * @param changed This is a new size or position for this view
13740 * @param left Left position, relative to parent
13741 * @param top Top position, relative to parent
13742 * @param right Right position, relative to parent
13743 * @param bottom Bottom position, relative to parent
13744 */
13745 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13746 }
13747
13748 /**
13749 * Assign a size and position to this view.
13750 *
13751 * This is called from layout.
13752 *
13753 * @param left Left position, relative to parent
13754 * @param top Top position, relative to parent
13755 * @param right Right position, relative to parent
13756 * @param bottom Bottom position, relative to parent
13757 * @return true if the new size and position are different than the
13758 * previous ones
13759 * {@hide}
13760 */
13761 protected boolean setFrame(int left, int top, int right, int bottom) {
13762 boolean changed = false;
13763
13764 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013765 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766 + right + "," + bottom + ")");
13767 }
13768
13769 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13770 changed = true;
13771
13772 // Remember our drawn bit
13773 int drawn = mPrivateFlags & DRAWN;
13774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013775 int oldWidth = mRight - mLeft;
13776 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013777 int newWidth = right - left;
13778 int newHeight = bottom - top;
13779 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13780
13781 // Invalidate our old position
13782 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783
13784 mLeft = left;
13785 mTop = top;
13786 mRight = right;
13787 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013788 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013789 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791
13792 mPrivateFlags |= HAS_BOUNDS;
13793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013794
Chet Haase75755e22011-07-18 17:48:25 -070013795 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013796 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13797 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013798 if (mTransformationInfo != null) {
13799 mTransformationInfo.mMatrixDirty = true;
13800 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013802 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13803 }
13804
13805 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13806 // If we are visible, force the DRAWN bit to on so that
13807 // this invalidate will go through (at least to our parent).
13808 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013809 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013810 // the DRAWN bit.
13811 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013812 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013813 // parent display list may need to be recreated based on a change in the bounds
13814 // of any child
13815 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013816 }
13817
13818 // Reset drawn bit to original value (invalidate turns it off)
13819 mPrivateFlags |= drawn;
13820
13821 mBackgroundSizeChanged = true;
13822 }
13823 return changed;
13824 }
13825
13826 /**
13827 * Finalize inflating a view from XML. This is called as the last phase
13828 * of inflation, after all child views have been added.
13829 *
13830 * <p>Even if the subclass overrides onFinishInflate, they should always be
13831 * sure to call the super method, so that we get called.
13832 */
13833 protected void onFinishInflate() {
13834 }
13835
13836 /**
13837 * Returns the resources associated with this view.
13838 *
13839 * @return Resources object.
13840 */
13841 public Resources getResources() {
13842 return mResources;
13843 }
13844
13845 /**
13846 * Invalidates the specified Drawable.
13847 *
13848 * @param drawable the drawable to invalidate
13849 */
13850 public void invalidateDrawable(Drawable drawable) {
13851 if (verifyDrawable(drawable)) {
13852 final Rect dirty = drawable.getBounds();
13853 final int scrollX = mScrollX;
13854 final int scrollY = mScrollY;
13855
13856 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13857 dirty.right + scrollX, dirty.bottom + scrollY);
13858 }
13859 }
13860
13861 /**
13862 * Schedules an action on a drawable to occur at a specified time.
13863 *
13864 * @param who the recipient of the action
13865 * @param what the action to run on the drawable
13866 * @param when the time at which the action must occur. Uses the
13867 * {@link SystemClock#uptimeMillis} timebase.
13868 */
13869 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013870 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013871 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013872 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013873 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13874 Choreographer.CALLBACK_ANIMATION, what, who,
13875 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013876 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013877 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013879 }
13880 }
13881
13882 /**
13883 * Cancels a scheduled action on a drawable.
13884 *
13885 * @param who the recipient of the action
13886 * @param what the action to cancel
13887 */
13888 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013889 if (verifyDrawable(who) && what != null) {
13890 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013891 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13892 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013893 } else {
13894 ViewRootImpl.getRunQueue().removeCallbacks(what);
13895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013896 }
13897 }
13898
13899 /**
13900 * Unschedule any events associated with the given Drawable. This can be
13901 * used when selecting a new Drawable into a view, so that the previous
13902 * one is completely unscheduled.
13903 *
13904 * @param who The Drawable to unschedule.
13905 *
13906 * @see #drawableStateChanged
13907 */
13908 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013909 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013910 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13911 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 }
13913 }
13914
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013915 /**
13916 * Return the layout direction of a given Drawable.
13917 *
13918 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013919 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013920 */
13921 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013922 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013923 }
13924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013925 /**
13926 * If your view subclass is displaying its own Drawable objects, it should
13927 * override this function and return true for any Drawable it is
13928 * displaying. This allows animations for those drawables to be
13929 * scheduled.
13930 *
13931 * <p>Be sure to call through to the super class when overriding this
13932 * function.
13933 *
13934 * @param who The Drawable to verify. Return true if it is one you are
13935 * displaying, else return the result of calling through to the
13936 * super class.
13937 *
13938 * @return boolean If true than the Drawable is being displayed in the
13939 * view; else false and it is not allowed to animate.
13940 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013941 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13942 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013943 */
13944 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013945 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013946 }
13947
13948 /**
13949 * This function is called whenever the state of the view changes in such
13950 * a way that it impacts the state of drawables being shown.
13951 *
13952 * <p>Be sure to call through to the superclass when overriding this
13953 * function.
13954 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013955 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956 */
13957 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013958 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013959 if (d != null && d.isStateful()) {
13960 d.setState(getDrawableState());
13961 }
13962 }
13963
13964 /**
13965 * Call this to force a view to update its drawable state. This will cause
13966 * drawableStateChanged to be called on this view. Views that are interested
13967 * in the new state should call getDrawableState.
13968 *
13969 * @see #drawableStateChanged
13970 * @see #getDrawableState
13971 */
13972 public void refreshDrawableState() {
13973 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13974 drawableStateChanged();
13975
13976 ViewParent parent = mParent;
13977 if (parent != null) {
13978 parent.childDrawableStateChanged(this);
13979 }
13980 }
13981
13982 /**
13983 * Return an array of resource IDs of the drawable states representing the
13984 * current state of the view.
13985 *
13986 * @return The current drawable state
13987 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013988 * @see Drawable#setState(int[])
13989 * @see #drawableStateChanged()
13990 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013991 */
13992 public final int[] getDrawableState() {
13993 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13994 return mDrawableState;
13995 } else {
13996 mDrawableState = onCreateDrawableState(0);
13997 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13998 return mDrawableState;
13999 }
14000 }
14001
14002 /**
14003 * Generate the new {@link android.graphics.drawable.Drawable} state for
14004 * this view. This is called by the view
14005 * system when the cached Drawable state is determined to be invalid. To
14006 * retrieve the current state, you should use {@link #getDrawableState}.
14007 *
14008 * @param extraSpace if non-zero, this is the number of extra entries you
14009 * would like in the returned array in which you can place your own
14010 * states.
14011 *
14012 * @return Returns an array holding the current {@link Drawable} state of
14013 * the view.
14014 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014015 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014016 */
14017 protected int[] onCreateDrawableState(int extraSpace) {
14018 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14019 mParent instanceof View) {
14020 return ((View) mParent).onCreateDrawableState(extraSpace);
14021 }
14022
14023 int[] drawableState;
14024
14025 int privateFlags = mPrivateFlags;
14026
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014027 int viewStateIndex = 0;
14028 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
14029 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14030 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070014031 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014032 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
14033 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014034 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14035 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014036 // This is set if HW acceleration is requested, even if the current
14037 // process doesn't allow it. This is just to allow app preview
14038 // windows to better match their app.
14039 viewStateIndex |= VIEW_STATE_ACCELERATED;
14040 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070014041 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042
Christopher Tate3d4bf172011-03-28 16:16:46 -070014043 final int privateFlags2 = mPrivateFlags2;
14044 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14045 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
14046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014047 drawableState = VIEW_STATE_SETS[viewStateIndex];
14048
14049 //noinspection ConstantIfStatement
14050 if (false) {
14051 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14052 Log.i("View", toString()
14053 + " pressed=" + ((privateFlags & PRESSED) != 0)
14054 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14055 + " fo=" + hasFocus()
14056 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014057 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 + ": " + Arrays.toString(drawableState));
14059 }
14060
14061 if (extraSpace == 0) {
14062 return drawableState;
14063 }
14064
14065 final int[] fullState;
14066 if (drawableState != null) {
14067 fullState = new int[drawableState.length + extraSpace];
14068 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14069 } else {
14070 fullState = new int[extraSpace];
14071 }
14072
14073 return fullState;
14074 }
14075
14076 /**
14077 * Merge your own state values in <var>additionalState</var> into the base
14078 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014079 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 *
14081 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014082 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 * own additional state values.
14084 *
14085 * @param additionalState The additional state values you would like
14086 * added to <var>baseState</var>; this array is not modified.
14087 *
14088 * @return As a convenience, the <var>baseState</var> array you originally
14089 * passed into the function is returned.
14090 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014091 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092 */
14093 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14094 final int N = baseState.length;
14095 int i = N - 1;
14096 while (i >= 0 && baseState[i] == 0) {
14097 i--;
14098 }
14099 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14100 return baseState;
14101 }
14102
14103 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014104 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14105 * on all Drawable objects associated with this view.
14106 */
14107 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014108 if (mBackground != null) {
14109 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014110 }
14111 }
14112
14113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 * Sets the background color for this view.
14115 * @param color the color of the background
14116 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014117 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014118 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014119 if (mBackground instanceof ColorDrawable) {
14120 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014121 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014122 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014124 }
14125
14126 /**
14127 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014128 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014129 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 * @attr ref android.R.styleable#View_background
14132 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014133 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014134 public void setBackgroundResource(int resid) {
14135 if (resid != 0 && resid == mBackgroundResource) {
14136 return;
14137 }
14138
14139 Drawable d= null;
14140 if (resid != 0) {
14141 d = mResources.getDrawable(resid);
14142 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014143 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014144
14145 mBackgroundResource = resid;
14146 }
14147
14148 /**
14149 * Set the background to a given Drawable, or remove the background. If the
14150 * background has padding, this View's padding is set to the background's
14151 * padding. However, when a background is removed, this View's padding isn't
14152 * touched. If setting the padding is desired, please use
14153 * {@link #setPadding(int, int, int, int)}.
14154 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014155 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014156 * background
14157 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014158 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014159 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014160 setBackgroundDrawable(background);
14161 }
14162
14163 /**
14164 * @deprecated use {@link #setBackground(Drawable)} instead
14165 */
14166 @Deprecated
14167 public void setBackgroundDrawable(Drawable background) {
14168 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014169 return;
14170 }
14171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 boolean requestLayout = false;
14173
14174 mBackgroundResource = 0;
14175
14176 /*
14177 * Regardless of whether we're setting a new background or not, we want
14178 * to clear the previous drawable.
14179 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014180 if (mBackground != null) {
14181 mBackground.setCallback(null);
14182 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183 }
14184
Philip Milne6c8ea062012-04-03 17:38:43 -070014185 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014186 Rect padding = sThreadLocal.get();
14187 if (padding == null) {
14188 padding = new Rect();
14189 sThreadLocal.set(padding);
14190 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014191 if (background.getPadding(padding)) {
14192 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014193 case LAYOUT_DIRECTION_RTL:
14194 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14195 break;
14196 case LAYOUT_DIRECTION_LTR:
14197 default:
14198 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014200 }
14201
14202 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14203 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014204 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14205 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206 requestLayout = true;
14207 }
14208
Philip Milne6c8ea062012-04-03 17:38:43 -070014209 background.setCallback(this);
14210 if (background.isStateful()) {
14211 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014213 background.setVisible(getVisibility() == VISIBLE, false);
14214 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215
14216 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14217 mPrivateFlags &= ~SKIP_DRAW;
14218 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14219 requestLayout = true;
14220 }
14221 } else {
14222 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014223 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224
14225 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14226 /*
14227 * This view ONLY drew the background before and we're removing
14228 * the background, so now it won't draw anything
14229 * (hence we SKIP_DRAW)
14230 */
14231 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14232 mPrivateFlags |= SKIP_DRAW;
14233 }
14234
14235 /*
14236 * When the background is set, we try to apply its padding to this
14237 * View. When the background is removed, we don't touch this View's
14238 * padding. This is noted in the Javadocs. Hence, we don't need to
14239 * requestLayout(), the invalidate() below is sufficient.
14240 */
14241
14242 // The old background's minimum size could have affected this
14243 // View's layout, so let's requestLayout
14244 requestLayout = true;
14245 }
14246
Romain Guy8f1344f52009-05-15 16:03:59 -070014247 computeOpaqueFlags();
14248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014249 if (requestLayout) {
14250 requestLayout();
14251 }
14252
14253 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014254 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 }
14256
14257 /**
14258 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014260 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014261 *
14262 * @see #setBackground(Drawable)
14263 *
14264 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014265 */
14266 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014267 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 }
14269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014270 /**
14271 * Sets the padding. The view may add on the space required to display
14272 * the scrollbars, depending on the style and visibility of the scrollbars.
14273 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14274 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14275 * from the values set in this call.
14276 *
14277 * @attr ref android.R.styleable#View_padding
14278 * @attr ref android.R.styleable#View_paddingBottom
14279 * @attr ref android.R.styleable#View_paddingLeft
14280 * @attr ref android.R.styleable#View_paddingRight
14281 * @attr ref android.R.styleable#View_paddingTop
14282 * @param left the left padding in pixels
14283 * @param top the top padding in pixels
14284 * @param right the right padding in pixels
14285 * @param bottom the bottom padding in pixels
14286 */
14287 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014288 mUserPaddingStart = -1;
14289 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014290 mUserPaddingRelative = false;
14291
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014292 internalSetPadding(left, top, right, bottom);
14293 }
14294
14295 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014296 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014297 mUserPaddingRight = right;
14298 mUserPaddingBottom = bottom;
14299
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014300 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014301 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014302
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014303 // Common case is there are no scroll bars.
14304 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014305 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014306 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014307 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014308 switch (mVerticalScrollbarPosition) {
14309 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014310 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14311 left += offset;
14312 } else {
14313 right += offset;
14314 }
14315 break;
Adam Powell20232d02010-12-08 21:08:53 -080014316 case SCROLLBAR_POSITION_RIGHT:
14317 right += offset;
14318 break;
14319 case SCROLLBAR_POSITION_LEFT:
14320 left += offset;
14321 break;
14322 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014323 }
Adam Powell20232d02010-12-08 21:08:53 -080014324 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014325 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14326 ? 0 : getHorizontalScrollbarHeight();
14327 }
14328 }
Romain Guy8506ab42009-06-11 17:35:47 -070014329
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014330 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014331 changed = true;
14332 mPaddingLeft = left;
14333 }
14334 if (mPaddingTop != top) {
14335 changed = true;
14336 mPaddingTop = top;
14337 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014338 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014339 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014340 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014342 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014344 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014345 }
14346
14347 if (changed) {
14348 requestLayout();
14349 }
14350 }
14351
14352 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014353 * Sets the relative padding. The view may add on the space required to display
14354 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014355 * from the values set in this call.
14356 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014357 * @param start the start padding in pixels
14358 * @param top the top padding in pixels
14359 * @param end the end padding in pixels
14360 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014361 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014362 */
14363 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014364 mUserPaddingStart = start;
14365 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014366 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014367
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014368 switch(getResolvedLayoutDirection()) {
14369 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014370 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014371 break;
14372 case LAYOUT_DIRECTION_LTR:
14373 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014374 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014375 }
14376 }
14377
14378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 * Returns the top padding of this view.
14380 *
14381 * @return the top padding in pixels
14382 */
14383 public int getPaddingTop() {
14384 return mPaddingTop;
14385 }
14386
14387 /**
14388 * Returns the bottom padding of this view. If there are inset and enabled
14389 * scrollbars, this value may include the space required to display the
14390 * scrollbars as well.
14391 *
14392 * @return the bottom padding in pixels
14393 */
14394 public int getPaddingBottom() {
14395 return mPaddingBottom;
14396 }
14397
14398 /**
14399 * Returns the left padding of this view. If there are inset and enabled
14400 * scrollbars, this value may include the space required to display the
14401 * scrollbars as well.
14402 *
14403 * @return the left padding in pixels
14404 */
14405 public int getPaddingLeft() {
14406 return mPaddingLeft;
14407 }
14408
14409 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014410 * Returns the start padding of this view depending on its resolved layout direction.
14411 * If there are inset and enabled scrollbars, this value may include the space
14412 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014413 *
14414 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014415 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014416 */
14417 public int getPaddingStart() {
14418 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14419 mPaddingRight : mPaddingLeft;
14420 }
14421
14422 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014423 * Returns the right padding of this view. If there are inset and enabled
14424 * scrollbars, this value may include the space required to display the
14425 * scrollbars as well.
14426 *
14427 * @return the right padding in pixels
14428 */
14429 public int getPaddingRight() {
14430 return mPaddingRight;
14431 }
14432
14433 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014434 * Returns the end padding of this view depending on its resolved layout direction.
14435 * If there are inset and enabled scrollbars, this value may include the space
14436 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014437 *
14438 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014439 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014440 */
14441 public int getPaddingEnd() {
14442 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14443 mPaddingLeft : mPaddingRight;
14444 }
14445
14446 /**
14447 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014448 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014449 *
14450 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014451 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014452 */
14453 public boolean isPaddingRelative() {
14454 return mUserPaddingRelative;
14455 }
14456
14457 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014458 * @hide
14459 */
Philip Milne7a23b492012-04-24 22:12:36 -070014460 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014461 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014462 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014463 }
14464 return mLayoutInsets;
14465 }
14466
14467 /**
14468 * @hide
14469 */
14470 public void setLayoutInsets(Insets layoutInsets) {
14471 mLayoutInsets = layoutInsets;
14472 }
14473
14474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014475 * Changes the selection state of this view. A view can be selected or not.
14476 * Note that selection is not the same as focus. Views are typically
14477 * selected in the context of an AdapterView like ListView or GridView;
14478 * the selected view is the view that is highlighted.
14479 *
14480 * @param selected true if the view must be selected, false otherwise
14481 */
14482 public void setSelected(boolean selected) {
14483 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14484 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014485 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014486 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 refreshDrawableState();
14488 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014489 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14490 notifyAccessibilityStateChanged();
14491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014492 }
14493 }
14494
14495 /**
14496 * Dispatch setSelected to all of this View's children.
14497 *
14498 * @see #setSelected(boolean)
14499 *
14500 * @param selected The new selected state
14501 */
14502 protected void dispatchSetSelected(boolean selected) {
14503 }
14504
14505 /**
14506 * Indicates the selection state of this view.
14507 *
14508 * @return true if the view is selected, false otherwise
14509 */
14510 @ViewDebug.ExportedProperty
14511 public boolean isSelected() {
14512 return (mPrivateFlags & SELECTED) != 0;
14513 }
14514
14515 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014516 * Changes the activated state of this view. A view can be activated or not.
14517 * Note that activation is not the same as selection. Selection is
14518 * a transient property, representing the view (hierarchy) the user is
14519 * currently interacting with. Activation is a longer-term state that the
14520 * user can move views in and out of. For example, in a list view with
14521 * single or multiple selection enabled, the views in the current selection
14522 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14523 * here.) The activated state is propagated down to children of the view it
14524 * is set on.
14525 *
14526 * @param activated true if the view must be activated, false otherwise
14527 */
14528 public void setActivated(boolean activated) {
14529 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14530 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014531 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014532 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014533 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014534 }
14535 }
14536
14537 /**
14538 * Dispatch setActivated to all of this View's children.
14539 *
14540 * @see #setActivated(boolean)
14541 *
14542 * @param activated The new activated state
14543 */
14544 protected void dispatchSetActivated(boolean activated) {
14545 }
14546
14547 /**
14548 * Indicates the activation state of this view.
14549 *
14550 * @return true if the view is activated, false otherwise
14551 */
14552 @ViewDebug.ExportedProperty
14553 public boolean isActivated() {
14554 return (mPrivateFlags & ACTIVATED) != 0;
14555 }
14556
14557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014558 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14559 * observer can be used to get notifications when global events, like
14560 * layout, happen.
14561 *
14562 * The returned ViewTreeObserver observer is not guaranteed to remain
14563 * valid for the lifetime of this View. If the caller of this method keeps
14564 * a long-lived reference to ViewTreeObserver, it should always check for
14565 * the return value of {@link ViewTreeObserver#isAlive()}.
14566 *
14567 * @return The ViewTreeObserver for this view's hierarchy.
14568 */
14569 public ViewTreeObserver getViewTreeObserver() {
14570 if (mAttachInfo != null) {
14571 return mAttachInfo.mTreeObserver;
14572 }
14573 if (mFloatingTreeObserver == null) {
14574 mFloatingTreeObserver = new ViewTreeObserver();
14575 }
14576 return mFloatingTreeObserver;
14577 }
14578
14579 /**
14580 * <p>Finds the topmost view in the current view hierarchy.</p>
14581 *
14582 * @return the topmost view containing this view
14583 */
14584 public View getRootView() {
14585 if (mAttachInfo != null) {
14586 final View v = mAttachInfo.mRootView;
14587 if (v != null) {
14588 return v;
14589 }
14590 }
Romain Guy8506ab42009-06-11 17:35:47 -070014591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014592 View parent = this;
14593
14594 while (parent.mParent != null && parent.mParent instanceof View) {
14595 parent = (View) parent.mParent;
14596 }
14597
14598 return parent;
14599 }
14600
14601 /**
14602 * <p>Computes the coordinates of this view on the screen. The argument
14603 * must be an array of two integers. After the method returns, the array
14604 * contains the x and y location in that order.</p>
14605 *
14606 * @param location an array of two integers in which to hold the coordinates
14607 */
14608 public void getLocationOnScreen(int[] location) {
14609 getLocationInWindow(location);
14610
14611 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014612 if (info != null) {
14613 location[0] += info.mWindowLeft;
14614 location[1] += info.mWindowTop;
14615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014616 }
14617
14618 /**
14619 * <p>Computes the coordinates of this view in its window. The argument
14620 * must be an array of two integers. After the method returns, the array
14621 * contains the x and y location in that order.</p>
14622 *
14623 * @param location an array of two integers in which to hold the coordinates
14624 */
14625 public void getLocationInWindow(int[] location) {
14626 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014627 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014628 }
14629
Gilles Debunne6583ce52011-12-06 18:09:02 -080014630 if (mAttachInfo == null) {
14631 // When the view is not attached to a window, this method does not make sense
14632 location[0] = location[1] = 0;
14633 return;
14634 }
14635
Gilles Debunnecea45132011-11-24 02:19:27 +010014636 float[] position = mAttachInfo.mTmpTransformLocation;
14637 position[0] = position[1] = 0.0f;
14638
14639 if (!hasIdentityMatrix()) {
14640 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014642
Gilles Debunnecea45132011-11-24 02:19:27 +010014643 position[0] += mLeft;
14644 position[1] += mTop;
14645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014646 ViewParent viewParent = mParent;
14647 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014648 final View view = (View) viewParent;
14649
14650 position[0] -= view.mScrollX;
14651 position[1] -= view.mScrollY;
14652
14653 if (!view.hasIdentityMatrix()) {
14654 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014655 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014656
14657 position[0] += view.mLeft;
14658 position[1] += view.mTop;
14659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014660 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014661 }
Romain Guy8506ab42009-06-11 17:35:47 -070014662
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014663 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014664 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014665 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14666 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014667 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014668
14669 location[0] = (int) (position[0] + 0.5f);
14670 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014671 }
14672
14673 /**
14674 * {@hide}
14675 * @param id the id of the view to be found
14676 * @return the view of the specified id, null if cannot be found
14677 */
14678 protected View findViewTraversal(int id) {
14679 if (id == mID) {
14680 return this;
14681 }
14682 return null;
14683 }
14684
14685 /**
14686 * {@hide}
14687 * @param tag the tag of the view to be found
14688 * @return the view of specified tag, null if cannot be found
14689 */
14690 protected View findViewWithTagTraversal(Object tag) {
14691 if (tag != null && tag.equals(mTag)) {
14692 return this;
14693 }
14694 return null;
14695 }
14696
14697 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014698 * {@hide}
14699 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014700 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014701 * @return The first view that matches the predicate or null.
14702 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014703 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014704 if (predicate.apply(this)) {
14705 return this;
14706 }
14707 return null;
14708 }
14709
14710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014711 * Look for a child view with the given id. If this view has the given
14712 * id, return this view.
14713 *
14714 * @param id The id to search for.
14715 * @return The view that has the given id in the hierarchy or null
14716 */
14717 public final View findViewById(int id) {
14718 if (id < 0) {
14719 return null;
14720 }
14721 return findViewTraversal(id);
14722 }
14723
14724 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014725 * Finds a view by its unuque and stable accessibility id.
14726 *
14727 * @param accessibilityId The searched accessibility id.
14728 * @return The found view.
14729 */
14730 final View findViewByAccessibilityId(int accessibilityId) {
14731 if (accessibilityId < 0) {
14732 return null;
14733 }
14734 return findViewByAccessibilityIdTraversal(accessibilityId);
14735 }
14736
14737 /**
14738 * Performs the traversal to find a view by its unuque and stable accessibility id.
14739 *
14740 * <strong>Note:</strong>This method does not stop at the root namespace
14741 * boundary since the user can touch the screen at an arbitrary location
14742 * potentially crossing the root namespace bounday which will send an
14743 * accessibility event to accessibility services and they should be able
14744 * to obtain the event source. Also accessibility ids are guaranteed to be
14745 * unique in the window.
14746 *
14747 * @param accessibilityId The accessibility id.
14748 * @return The found view.
14749 */
14750 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14751 if (getAccessibilityViewId() == accessibilityId) {
14752 return this;
14753 }
14754 return null;
14755 }
14756
14757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014758 * Look for a child view with the given tag. If this view has the given
14759 * tag, return this view.
14760 *
14761 * @param tag The tag to search for, using "tag.equals(getTag())".
14762 * @return The View that has the given tag in the hierarchy or null
14763 */
14764 public final View findViewWithTag(Object tag) {
14765 if (tag == null) {
14766 return null;
14767 }
14768 return findViewWithTagTraversal(tag);
14769 }
14770
14771 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014772 * {@hide}
14773 * Look for a child view that matches the specified predicate.
14774 * If this view matches the predicate, return this view.
14775 *
14776 * @param predicate The predicate to evaluate.
14777 * @return The first view that matches the predicate or null.
14778 */
14779 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014780 return findViewByPredicateTraversal(predicate, null);
14781 }
14782
14783 /**
14784 * {@hide}
14785 * Look for a child view that matches the specified predicate,
14786 * starting with the specified view and its descendents and then
14787 * recusively searching the ancestors and siblings of that view
14788 * until this view is reached.
14789 *
14790 * This method is useful in cases where the predicate does not match
14791 * a single unique view (perhaps multiple views use the same id)
14792 * and we are trying to find the view that is "closest" in scope to the
14793 * starting view.
14794 *
14795 * @param start The view to start from.
14796 * @param predicate The predicate to evaluate.
14797 * @return The first view that matches the predicate or null.
14798 */
14799 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14800 View childToSkip = null;
14801 for (;;) {
14802 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14803 if (view != null || start == this) {
14804 return view;
14805 }
14806
14807 ViewParent parent = start.getParent();
14808 if (parent == null || !(parent instanceof View)) {
14809 return null;
14810 }
14811
14812 childToSkip = start;
14813 start = (View) parent;
14814 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014815 }
14816
14817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014818 * Sets the identifier for this view. The identifier does not have to be
14819 * unique in this view's hierarchy. The identifier should be a positive
14820 * number.
14821 *
14822 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014823 * @see #getId()
14824 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014825 *
14826 * @param id a number used to identify the view
14827 *
14828 * @attr ref android.R.styleable#View_id
14829 */
14830 public void setId(int id) {
14831 mID = id;
14832 }
14833
14834 /**
14835 * {@hide}
14836 *
14837 * @param isRoot true if the view belongs to the root namespace, false
14838 * otherwise
14839 */
14840 public void setIsRootNamespace(boolean isRoot) {
14841 if (isRoot) {
14842 mPrivateFlags |= IS_ROOT_NAMESPACE;
14843 } else {
14844 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14845 }
14846 }
14847
14848 /**
14849 * {@hide}
14850 *
14851 * @return true if the view belongs to the root namespace, false otherwise
14852 */
14853 public boolean isRootNamespace() {
14854 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14855 }
14856
14857 /**
14858 * Returns this view's identifier.
14859 *
14860 * @return a positive integer used to identify the view or {@link #NO_ID}
14861 * if the view has no ID
14862 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014863 * @see #setId(int)
14864 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014865 * @attr ref android.R.styleable#View_id
14866 */
14867 @ViewDebug.CapturedViewProperty
14868 public int getId() {
14869 return mID;
14870 }
14871
14872 /**
14873 * Returns this view's tag.
14874 *
14875 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014876 *
14877 * @see #setTag(Object)
14878 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014879 */
14880 @ViewDebug.ExportedProperty
14881 public Object getTag() {
14882 return mTag;
14883 }
14884
14885 /**
14886 * Sets the tag associated with this view. A tag can be used to mark
14887 * a view in its hierarchy and does not have to be unique within the
14888 * hierarchy. Tags can also be used to store data within a view without
14889 * resorting to another data structure.
14890 *
14891 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014892 *
14893 * @see #getTag()
14894 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014895 */
14896 public void setTag(final Object tag) {
14897 mTag = tag;
14898 }
14899
14900 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014901 * Returns the tag associated with this view and the specified key.
14902 *
14903 * @param key The key identifying the tag
14904 *
14905 * @return the Object stored in this view as a tag
14906 *
14907 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014908 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014909 */
14910 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014911 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014912 return null;
14913 }
14914
14915 /**
14916 * Sets a tag associated with this view and a key. A tag can be used
14917 * to mark a view in its hierarchy and does not have to be unique within
14918 * the hierarchy. Tags can also be used to store data within a view
14919 * without resorting to another data structure.
14920 *
14921 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014922 * application to ensure it is unique (see the <a
14923 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14924 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014925 * the Android framework or not associated with any package will cause
14926 * an {@link IllegalArgumentException} to be thrown.
14927 *
14928 * @param key The key identifying the tag
14929 * @param tag An Object to tag the view with
14930 *
14931 * @throws IllegalArgumentException If they specified key is not valid
14932 *
14933 * @see #setTag(Object)
14934 * @see #getTag(int)
14935 */
14936 public void setTag(int key, final Object tag) {
14937 // If the package id is 0x00 or 0x01, it's either an undefined package
14938 // or a framework id
14939 if ((key >>> 24) < 2) {
14940 throw new IllegalArgumentException("The key must be an application-specific "
14941 + "resource id.");
14942 }
14943
Adam Powell2b2f6d62011-09-23 11:15:39 -070014944 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014945 }
14946
14947 /**
14948 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14949 * framework id.
14950 *
14951 * @hide
14952 */
14953 public void setTagInternal(int key, Object tag) {
14954 if ((key >>> 24) != 0x1) {
14955 throw new IllegalArgumentException("The key must be a framework-specific "
14956 + "resource id.");
14957 }
14958
Adam Powell2b2f6d62011-09-23 11:15:39 -070014959 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014960 }
14961
Adam Powell2b2f6d62011-09-23 11:15:39 -070014962 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014963 if (mKeyedTags == null) {
14964 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014965 }
14966
Adam Powell7db82ac2011-09-22 19:44:04 -070014967 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014968 }
14969
14970 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014971 * Prints information about this view in the log output, with the tag
14972 * {@link #VIEW_LOG_TAG}.
14973 *
14974 * @hide
14975 */
14976 public void debug() {
14977 debug(0);
14978 }
14979
14980 /**
14981 * Prints information about this view in the log output, with the tag
14982 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14983 * indentation defined by the <code>depth</code>.
14984 *
14985 * @param depth the indentation level
14986 *
14987 * @hide
14988 */
14989 protected void debug(int depth) {
14990 String output = debugIndent(depth - 1);
14991
14992 output += "+ " + this;
14993 int id = getId();
14994 if (id != -1) {
14995 output += " (id=" + id + ")";
14996 }
14997 Object tag = getTag();
14998 if (tag != null) {
14999 output += " (tag=" + tag + ")";
15000 }
15001 Log.d(VIEW_LOG_TAG, output);
15002
15003 if ((mPrivateFlags & FOCUSED) != 0) {
15004 output = debugIndent(depth) + " FOCUSED";
15005 Log.d(VIEW_LOG_TAG, output);
15006 }
15007
15008 output = debugIndent(depth);
15009 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15010 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15011 + "} ";
15012 Log.d(VIEW_LOG_TAG, output);
15013
15014 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15015 || mPaddingBottom != 0) {
15016 output = debugIndent(depth);
15017 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15018 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15019 Log.d(VIEW_LOG_TAG, output);
15020 }
15021
15022 output = debugIndent(depth);
15023 output += "mMeasureWidth=" + mMeasuredWidth +
15024 " mMeasureHeight=" + mMeasuredHeight;
15025 Log.d(VIEW_LOG_TAG, output);
15026
15027 output = debugIndent(depth);
15028 if (mLayoutParams == null) {
15029 output += "BAD! no layout params";
15030 } else {
15031 output = mLayoutParams.debug(output);
15032 }
15033 Log.d(VIEW_LOG_TAG, output);
15034
15035 output = debugIndent(depth);
15036 output += "flags={";
15037 output += View.printFlags(mViewFlags);
15038 output += "}";
15039 Log.d(VIEW_LOG_TAG, output);
15040
15041 output = debugIndent(depth);
15042 output += "privateFlags={";
15043 output += View.printPrivateFlags(mPrivateFlags);
15044 output += "}";
15045 Log.d(VIEW_LOG_TAG, output);
15046 }
15047
15048 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015049 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015050 *
15051 * @param depth the indentation level
15052 * @return a String containing (depth * 2 + 3) * 2 white spaces
15053 *
15054 * @hide
15055 */
15056 protected static String debugIndent(int depth) {
15057 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15058 for (int i = 0; i < (depth * 2) + 3; i++) {
15059 spaces.append(' ').append(' ');
15060 }
15061 return spaces.toString();
15062 }
15063
15064 /**
15065 * <p>Return the offset of the widget's text baseline from the widget's top
15066 * boundary. If this widget does not support baseline alignment, this
15067 * method returns -1. </p>
15068 *
15069 * @return the offset of the baseline within the widget's bounds or -1
15070 * if baseline alignment is not supported
15071 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015072 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015073 public int getBaseline() {
15074 return -1;
15075 }
15076
15077 /**
15078 * Call this when something has changed which has invalidated the
15079 * layout of this view. This will schedule a layout pass of the view
15080 * tree.
15081 */
15082 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015083 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015084 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015085
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015086 if (mLayoutParams != null) {
15087 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15088 }
15089
15090 if (mParent != null && !mParent.isLayoutRequested()) {
15091 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015092 }
15093 }
15094
15095 /**
15096 * Forces this view to be laid out during the next layout pass.
15097 * This method does not call requestLayout() or forceLayout()
15098 * on the parent.
15099 */
15100 public void forceLayout() {
15101 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015102 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015103 }
15104
15105 /**
15106 * <p>
15107 * This is called to find out how big a view should be. The parent
15108 * supplies constraint information in the width and height parameters.
15109 * </p>
15110 *
15111 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015112 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015113 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015114 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015115 * </p>
15116 *
15117 *
15118 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15119 * parent
15120 * @param heightMeasureSpec Vertical space requirements as imposed by the
15121 * parent
15122 *
15123 * @see #onMeasure(int, int)
15124 */
15125 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15126 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15127 widthMeasureSpec != mOldWidthMeasureSpec ||
15128 heightMeasureSpec != mOldHeightMeasureSpec) {
15129
15130 // first clears the measured dimension flag
15131 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015133 // measure ourselves, this should set the measured dimension flag back
15134 onMeasure(widthMeasureSpec, heightMeasureSpec);
15135
15136 // flag not set, setMeasuredDimension() was not invoked, we raise
15137 // an exception to warn the developer
15138 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15139 throw new IllegalStateException("onMeasure() did not set the"
15140 + " measured dimension by calling"
15141 + " setMeasuredDimension()");
15142 }
15143
15144 mPrivateFlags |= LAYOUT_REQUIRED;
15145 }
15146
15147 mOldWidthMeasureSpec = widthMeasureSpec;
15148 mOldHeightMeasureSpec = heightMeasureSpec;
15149 }
15150
15151 /**
15152 * <p>
15153 * Measure the view and its content to determine the measured width and the
15154 * measured height. This method is invoked by {@link #measure(int, int)} and
15155 * should be overriden by subclasses to provide accurate and efficient
15156 * measurement of their contents.
15157 * </p>
15158 *
15159 * <p>
15160 * <strong>CONTRACT:</strong> When overriding this method, you
15161 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15162 * measured width and height of this view. Failure to do so will trigger an
15163 * <code>IllegalStateException</code>, thrown by
15164 * {@link #measure(int, int)}. Calling the superclass'
15165 * {@link #onMeasure(int, int)} is a valid use.
15166 * </p>
15167 *
15168 * <p>
15169 * The base class implementation of measure defaults to the background size,
15170 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15171 * override {@link #onMeasure(int, int)} to provide better measurements of
15172 * their content.
15173 * </p>
15174 *
15175 * <p>
15176 * If this method is overridden, it is the subclass's responsibility to make
15177 * sure the measured height and width are at least the view's minimum height
15178 * and width ({@link #getSuggestedMinimumHeight()} and
15179 * {@link #getSuggestedMinimumWidth()}).
15180 * </p>
15181 *
15182 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15183 * The requirements are encoded with
15184 * {@link android.view.View.MeasureSpec}.
15185 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15186 * The requirements are encoded with
15187 * {@link android.view.View.MeasureSpec}.
15188 *
15189 * @see #getMeasuredWidth()
15190 * @see #getMeasuredHeight()
15191 * @see #setMeasuredDimension(int, int)
15192 * @see #getSuggestedMinimumHeight()
15193 * @see #getSuggestedMinimumWidth()
15194 * @see android.view.View.MeasureSpec#getMode(int)
15195 * @see android.view.View.MeasureSpec#getSize(int)
15196 */
15197 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15198 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15199 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15200 }
15201
15202 /**
15203 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15204 * measured width and measured height. Failing to do so will trigger an
15205 * exception at measurement time.</p>
15206 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015207 * @param measuredWidth The measured width of this view. May be a complex
15208 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15209 * {@link #MEASURED_STATE_TOO_SMALL}.
15210 * @param measuredHeight The measured height of this view. May be a complex
15211 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15212 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015213 */
15214 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15215 mMeasuredWidth = measuredWidth;
15216 mMeasuredHeight = measuredHeight;
15217
15218 mPrivateFlags |= MEASURED_DIMENSION_SET;
15219 }
15220
15221 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015222 * Merge two states as returned by {@link #getMeasuredState()}.
15223 * @param curState The current state as returned from a view or the result
15224 * of combining multiple views.
15225 * @param newState The new view state to combine.
15226 * @return Returns a new integer reflecting the combination of the two
15227 * states.
15228 */
15229 public static int combineMeasuredStates(int curState, int newState) {
15230 return curState | newState;
15231 }
15232
15233 /**
15234 * Version of {@link #resolveSizeAndState(int, int, int)}
15235 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15236 */
15237 public static int resolveSize(int size, int measureSpec) {
15238 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15239 }
15240
15241 /**
15242 * Utility to reconcile a desired size and state, with constraints imposed
15243 * by a MeasureSpec. Will take the desired size, unless a different size
15244 * is imposed by the constraints. The returned value is a compound integer,
15245 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15246 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15247 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015248 *
15249 * @param size How big the view wants to be
15250 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015251 * @return Size information bit mask as defined by
15252 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015253 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015254 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015255 int result = size;
15256 int specMode = MeasureSpec.getMode(measureSpec);
15257 int specSize = MeasureSpec.getSize(measureSpec);
15258 switch (specMode) {
15259 case MeasureSpec.UNSPECIFIED:
15260 result = size;
15261 break;
15262 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015263 if (specSize < size) {
15264 result = specSize | MEASURED_STATE_TOO_SMALL;
15265 } else {
15266 result = size;
15267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015268 break;
15269 case MeasureSpec.EXACTLY:
15270 result = specSize;
15271 break;
15272 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015273 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015274 }
15275
15276 /**
15277 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015278 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015279 * by the MeasureSpec.
15280 *
15281 * @param size Default size for this view
15282 * @param measureSpec Constraints imposed by the parent
15283 * @return The size this view should be.
15284 */
15285 public static int getDefaultSize(int size, int measureSpec) {
15286 int result = size;
15287 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015288 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015289
15290 switch (specMode) {
15291 case MeasureSpec.UNSPECIFIED:
15292 result = size;
15293 break;
15294 case MeasureSpec.AT_MOST:
15295 case MeasureSpec.EXACTLY:
15296 result = specSize;
15297 break;
15298 }
15299 return result;
15300 }
15301
15302 /**
15303 * Returns the suggested minimum height that the view should use. This
15304 * returns the maximum of the view's minimum height
15305 * and the background's minimum height
15306 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15307 * <p>
15308 * When being used in {@link #onMeasure(int, int)}, the caller should still
15309 * ensure the returned height is within the requirements of the parent.
15310 *
15311 * @return The suggested minimum height of the view.
15312 */
15313 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015314 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015316 }
15317
15318 /**
15319 * Returns the suggested minimum width that the view should use. This
15320 * returns the maximum of the view's minimum width)
15321 * and the background's minimum width
15322 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15323 * <p>
15324 * When being used in {@link #onMeasure(int, int)}, the caller should still
15325 * ensure the returned width is within the requirements of the parent.
15326 *
15327 * @return The suggested minimum width of the view.
15328 */
15329 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015330 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015332
Philip Milne6c8ea062012-04-03 17:38:43 -070015333 /**
15334 * Returns the minimum height of the view.
15335 *
15336 * @return the minimum height the view will try to be.
15337 *
15338 * @see #setMinimumHeight(int)
15339 *
15340 * @attr ref android.R.styleable#View_minHeight
15341 */
15342 public int getMinimumHeight() {
15343 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015344 }
15345
15346 /**
15347 * Sets the minimum height of the view. It is not guaranteed the view will
15348 * be able to achieve this minimum height (for example, if its parent layout
15349 * constrains it with less available height).
15350 *
15351 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015352 *
15353 * @see #getMinimumHeight()
15354 *
15355 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015356 */
15357 public void setMinimumHeight(int minHeight) {
15358 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015359 requestLayout();
15360 }
15361
15362 /**
15363 * Returns the minimum width of the view.
15364 *
15365 * @return the minimum width the view will try to be.
15366 *
15367 * @see #setMinimumWidth(int)
15368 *
15369 * @attr ref android.R.styleable#View_minWidth
15370 */
15371 public int getMinimumWidth() {
15372 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015373 }
15374
15375 /**
15376 * Sets the minimum width of the view. It is not guaranteed the view will
15377 * be able to achieve this minimum width (for example, if its parent layout
15378 * constrains it with less available width).
15379 *
15380 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015381 *
15382 * @see #getMinimumWidth()
15383 *
15384 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015385 */
15386 public void setMinimumWidth(int minWidth) {
15387 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015388 requestLayout();
15389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015390 }
15391
15392 /**
15393 * Get the animation currently associated with this view.
15394 *
15395 * @return The animation that is currently playing or
15396 * scheduled to play for this view.
15397 */
15398 public Animation getAnimation() {
15399 return mCurrentAnimation;
15400 }
15401
15402 /**
15403 * Start the specified animation now.
15404 *
15405 * @param animation the animation to start now
15406 */
15407 public void startAnimation(Animation animation) {
15408 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15409 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015410 invalidateParentCaches();
15411 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015412 }
15413
15414 /**
15415 * Cancels any animations for this view.
15416 */
15417 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015418 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015419 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015421 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015422 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015423 }
15424
15425 /**
15426 * Sets the next animation to play for this view.
15427 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015428 * {@link #startAnimation(android.view.animation.Animation)} instead.
15429 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015430 * control over the start time and invalidation, but you
15431 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015432 * 2) the view's parent (which controls animations on its children)
15433 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015434 * start.
15435 *
15436 * @param animation The next animation, or null.
15437 */
15438 public void setAnimation(Animation animation) {
15439 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015441 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015442 // If the screen is off assume the animation start time is now instead of
15443 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15444 // would cause the animation to start when the screen turns back on
15445 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15446 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15447 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015449 animation.reset();
15450 }
15451 }
15452
15453 /**
15454 * Invoked by a parent ViewGroup to notify the start of the animation
15455 * currently associated with this view. If you override this method,
15456 * always call super.onAnimationStart();
15457 *
15458 * @see #setAnimation(android.view.animation.Animation)
15459 * @see #getAnimation()
15460 */
15461 protected void onAnimationStart() {
15462 mPrivateFlags |= ANIMATION_STARTED;
15463 }
15464
15465 /**
15466 * Invoked by a parent ViewGroup to notify the end of the animation
15467 * currently associated with this view. If you override this method,
15468 * always call super.onAnimationEnd();
15469 *
15470 * @see #setAnimation(android.view.animation.Animation)
15471 * @see #getAnimation()
15472 */
15473 protected void onAnimationEnd() {
15474 mPrivateFlags &= ~ANIMATION_STARTED;
15475 }
15476
15477 /**
15478 * Invoked if there is a Transform that involves alpha. Subclass that can
15479 * draw themselves with the specified alpha should return true, and then
15480 * respect that alpha when their onDraw() is called. If this returns false
15481 * then the view may be redirected to draw into an offscreen buffer to
15482 * fulfill the request, which will look fine, but may be slower than if the
15483 * subclass handles it internally. The default implementation returns false.
15484 *
15485 * @param alpha The alpha (0..255) to apply to the view's drawing
15486 * @return true if the view can draw with the specified alpha.
15487 */
15488 protected boolean onSetAlpha(int alpha) {
15489 return false;
15490 }
15491
15492 /**
15493 * This is used by the RootView to perform an optimization when
15494 * the view hierarchy contains one or several SurfaceView.
15495 * SurfaceView is always considered transparent, but its children are not,
15496 * therefore all View objects remove themselves from the global transparent
15497 * region (passed as a parameter to this function).
15498 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015499 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015500 *
15501 * @return Returns true if the effective visibility of the view at this
15502 * point is opaque, regardless of the transparent region; returns false
15503 * if it is possible for underlying windows to be seen behind the view.
15504 *
15505 * {@hide}
15506 */
15507 public boolean gatherTransparentRegion(Region region) {
15508 final AttachInfo attachInfo = mAttachInfo;
15509 if (region != null && attachInfo != null) {
15510 final int pflags = mPrivateFlags;
15511 if ((pflags & SKIP_DRAW) == 0) {
15512 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15513 // remove it from the transparent region.
15514 final int[] location = attachInfo.mTransparentLocation;
15515 getLocationInWindow(location);
15516 region.op(location[0], location[1], location[0] + mRight - mLeft,
15517 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015518 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015519 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15520 // exists, so we remove the background drawable's non-transparent
15521 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015522 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015523 }
15524 }
15525 return true;
15526 }
15527
15528 /**
15529 * Play a sound effect for this view.
15530 *
15531 * <p>The framework will play sound effects for some built in actions, such as
15532 * clicking, but you may wish to play these effects in your widget,
15533 * for instance, for internal navigation.
15534 *
15535 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15536 * {@link #isSoundEffectsEnabled()} is true.
15537 *
15538 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15539 */
15540 public void playSoundEffect(int soundConstant) {
15541 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15542 return;
15543 }
15544 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15545 }
15546
15547 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015548 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015549 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015550 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015551 *
15552 * <p>The framework will provide haptic feedback for some built in actions,
15553 * such as long presses, but you may wish to provide feedback for your
15554 * own widget.
15555 *
15556 * <p>The feedback will only be performed if
15557 * {@link #isHapticFeedbackEnabled()} is true.
15558 *
15559 * @param feedbackConstant One of the constants defined in
15560 * {@link HapticFeedbackConstants}
15561 */
15562 public boolean performHapticFeedback(int feedbackConstant) {
15563 return performHapticFeedback(feedbackConstant, 0);
15564 }
15565
15566 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015567 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015568 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015569 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015570 *
15571 * @param feedbackConstant One of the constants defined in
15572 * {@link HapticFeedbackConstants}
15573 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15574 */
15575 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15576 if (mAttachInfo == null) {
15577 return false;
15578 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015579 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015580 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015581 && !isHapticFeedbackEnabled()) {
15582 return false;
15583 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015584 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15585 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015586 }
15587
15588 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015589 * Request that the visibility of the status bar or other screen/window
15590 * decorations be changed.
15591 *
15592 * <p>This method is used to put the over device UI into temporary modes
15593 * where the user's attention is focused more on the application content,
15594 * by dimming or hiding surrounding system affordances. This is typically
15595 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15596 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15597 * to be placed behind the action bar (and with these flags other system
15598 * affordances) so that smooth transitions between hiding and showing them
15599 * can be done.
15600 *
15601 * <p>Two representative examples of the use of system UI visibility is
15602 * implementing a content browsing application (like a magazine reader)
15603 * and a video playing application.
15604 *
15605 * <p>The first code shows a typical implementation of a View in a content
15606 * browsing application. In this implementation, the application goes
15607 * into a content-oriented mode by hiding the status bar and action bar,
15608 * and putting the navigation elements into lights out mode. The user can
15609 * then interact with content while in this mode. Such an application should
15610 * provide an easy way for the user to toggle out of the mode (such as to
15611 * check information in the status bar or access notifications). In the
15612 * implementation here, this is done simply by tapping on the content.
15613 *
15614 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15615 * content}
15616 *
15617 * <p>This second code sample shows a typical implementation of a View
15618 * in a video playing application. In this situation, while the video is
15619 * playing the application would like to go into a complete full-screen mode,
15620 * to use as much of the display as possible for the video. When in this state
15621 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015622 * touching on the screen to pop the UI out of full screen mode. See
15623 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015624 *
15625 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15626 * content}
15627 *
15628 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15629 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15630 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15631 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015632 */
15633 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015634 if (visibility != mSystemUiVisibility) {
15635 mSystemUiVisibility = visibility;
15636 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15637 mParent.recomputeViewAttributes(this);
15638 }
Joe Onorato664644d2011-01-23 17:53:23 -080015639 }
15640 }
15641
15642 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015643 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15644 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15645 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15646 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15647 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015648 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015649 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015650 return mSystemUiVisibility;
15651 }
15652
Scott Mainec6331b2011-05-24 16:55:56 -070015653 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015654 * Returns the current system UI visibility that is currently set for
15655 * the entire window. This is the combination of the
15656 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15657 * views in the window.
15658 */
15659 public int getWindowSystemUiVisibility() {
15660 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15661 }
15662
15663 /**
15664 * Override to find out when the window's requested system UI visibility
15665 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15666 * This is different from the callbacks recieved through
15667 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15668 * in that this is only telling you about the local request of the window,
15669 * not the actual values applied by the system.
15670 */
15671 public void onWindowSystemUiVisibilityChanged(int visible) {
15672 }
15673
15674 /**
15675 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15676 * the view hierarchy.
15677 */
15678 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15679 onWindowSystemUiVisibilityChanged(visible);
15680 }
15681
15682 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015683 * Set a listener to receive callbacks when the visibility of the system bar changes.
15684 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15685 */
Joe Onorato664644d2011-01-23 17:53:23 -080015686 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015687 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015688 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15689 mParent.recomputeViewAttributes(this);
15690 }
15691 }
15692
15693 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015694 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15695 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015696 */
15697 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015698 ListenerInfo li = mListenerInfo;
15699 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15700 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015701 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015702 }
15703 }
15704
Dianne Hackborncf675782012-05-10 15:07:24 -070015705 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015706 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15707 if (val != mSystemUiVisibility) {
15708 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015709 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015710 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015711 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015712 }
15713
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015714 /** @hide */
15715 public void setDisabledSystemUiVisibility(int flags) {
15716 if (mAttachInfo != null) {
15717 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15718 mAttachInfo.mDisabledSystemUiVisibility = flags;
15719 if (mParent != null) {
15720 mParent.recomputeViewAttributes(this);
15721 }
15722 }
15723 }
15724 }
15725
Joe Onorato664644d2011-01-23 17:53:23 -080015726 /**
Joe Malin32736f02011-01-19 16:14:20 -080015727 * Creates an image that the system displays during the drag and drop
15728 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15729 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15730 * appearance as the given View. The default also positions the center of the drag shadow
15731 * directly under the touch point. If no View is provided (the constructor with no parameters
15732 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15733 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15734 * default is an invisible drag shadow.
15735 * <p>
15736 * You are not required to use the View you provide to the constructor as the basis of the
15737 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15738 * anything you want as the drag shadow.
15739 * </p>
15740 * <p>
15741 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15742 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15743 * size and position of the drag shadow. It uses this data to construct a
15744 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15745 * so that your application can draw the shadow image in the Canvas.
15746 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015747 *
15748 * <div class="special reference">
15749 * <h3>Developer Guides</h3>
15750 * <p>For a guide to implementing drag and drop features, read the
15751 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15752 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015753 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015754 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015755 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015756
15757 /**
Joe Malin32736f02011-01-19 16:14:20 -080015758 * Constructs a shadow image builder based on a View. By default, the resulting drag
15759 * shadow will have the same appearance and dimensions as the View, with the touch point
15760 * over the center of the View.
15761 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015762 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015763 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015764 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015765 }
15766
Christopher Tate17ed60c2011-01-18 12:50:26 -080015767 /**
15768 * Construct a shadow builder object with no associated View. This
15769 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15770 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15771 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015772 * reference to any View object. If they are not overridden, then the result is an
15773 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015774 */
15775 public DragShadowBuilder() {
15776 mView = new WeakReference<View>(null);
15777 }
15778
15779 /**
15780 * Returns the View object that had been passed to the
15781 * {@link #View.DragShadowBuilder(View)}
15782 * constructor. If that View parameter was {@code null} or if the
15783 * {@link #View.DragShadowBuilder()}
15784 * constructor was used to instantiate the builder object, this method will return
15785 * null.
15786 *
15787 * @return The View object associate with this builder object.
15788 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015789 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015790 final public View getView() {
15791 return mView.get();
15792 }
15793
Christopher Tate2c095f32010-10-04 14:13:40 -070015794 /**
Joe Malin32736f02011-01-19 16:14:20 -080015795 * Provides the metrics for the shadow image. These include the dimensions of
15796 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015797 * be centered under the touch location while dragging.
15798 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015799 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015800 * same as the dimensions of the View itself and centers the shadow under
15801 * the touch point.
15802 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015803 *
Joe Malin32736f02011-01-19 16:14:20 -080015804 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15805 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15806 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15807 * image.
15808 *
15809 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15810 * shadow image that should be underneath the touch point during the drag and drop
15811 * operation. Your application must set {@link android.graphics.Point#x} to the
15812 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015813 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015814 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015815 final View view = mView.get();
15816 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015817 shadowSize.set(view.getWidth(), view.getHeight());
15818 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015819 } else {
15820 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15821 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015822 }
15823
15824 /**
Joe Malin32736f02011-01-19 16:14:20 -080015825 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15826 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015827 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015828 *
Joe Malin32736f02011-01-19 16:14:20 -080015829 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015830 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015831 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015832 final View view = mView.get();
15833 if (view != null) {
15834 view.draw(canvas);
15835 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015836 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015837 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015838 }
15839 }
15840
15841 /**
Joe Malin32736f02011-01-19 16:14:20 -080015842 * Starts a drag and drop operation. When your application calls this method, it passes a
15843 * {@link android.view.View.DragShadowBuilder} object to the system. The
15844 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15845 * to get metrics for the drag shadow, and then calls the object's
15846 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15847 * <p>
15848 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15849 * drag events to all the View objects in your application that are currently visible. It does
15850 * this either by calling the View object's drag listener (an implementation of
15851 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15852 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15853 * Both are passed a {@link android.view.DragEvent} object that has a
15854 * {@link android.view.DragEvent#getAction()} value of
15855 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15856 * </p>
15857 * <p>
15858 * Your application can invoke startDrag() on any attached View object. The View object does not
15859 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15860 * be related to the View the user selected for dragging.
15861 * </p>
15862 * @param data A {@link android.content.ClipData} object pointing to the data to be
15863 * transferred by the drag and drop operation.
15864 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15865 * drag shadow.
15866 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15867 * drop operation. This Object is put into every DragEvent object sent by the system during the
15868 * current drag.
15869 * <p>
15870 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15871 * to the target Views. For example, it can contain flags that differentiate between a
15872 * a copy operation and a move operation.
15873 * </p>
15874 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15875 * so the parameter should be set to 0.
15876 * @return {@code true} if the method completes successfully, or
15877 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15878 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015879 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015880 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015881 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015882 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015883 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015884 }
15885 boolean okay = false;
15886
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015887 Point shadowSize = new Point();
15888 Point shadowTouchPoint = new Point();
15889 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015890
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015891 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15892 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15893 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015894 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015895
Chris Tatea32dcf72010-10-14 12:13:50 -070015896 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015897 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15898 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015899 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015900 Surface surface = new Surface();
15901 try {
15902 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015903 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015904 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015905 + " surface=" + surface);
15906 if (token != null) {
15907 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015908 try {
Chris Tate6b391282010-10-14 15:48:59 -070015909 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015910 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015911 } finally {
15912 surface.unlockCanvasAndPost(canvas);
15913 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015914
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015915 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015916
15917 // Cache the local state object for delivery with DragEvents
15918 root.setLocalDragState(myLocalState);
15919
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015920 // repurpose 'shadowSize' for the last touch point
15921 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015922
Christopher Tatea53146c2010-09-07 11:57:52 -070015923 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015924 shadowSize.x, shadowSize.y,
15925 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015926 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015927
15928 // Off and running! Release our local surface instance; the drag
15929 // shadow surface is now managed by the system process.
15930 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015931 }
15932 } catch (Exception e) {
15933 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15934 surface.destroy();
15935 }
15936
15937 return okay;
15938 }
15939
Christopher Tatea53146c2010-09-07 11:57:52 -070015940 /**
Joe Malin32736f02011-01-19 16:14:20 -080015941 * Handles drag events sent by the system following a call to
15942 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15943 *<p>
15944 * When the system calls this method, it passes a
15945 * {@link android.view.DragEvent} object. A call to
15946 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15947 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15948 * operation.
15949 * @param event The {@link android.view.DragEvent} sent by the system.
15950 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15951 * in DragEvent, indicating the type of drag event represented by this object.
15952 * @return {@code true} if the method was successful, otherwise {@code false}.
15953 * <p>
15954 * The method should return {@code true} in response to an action type of
15955 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15956 * operation.
15957 * </p>
15958 * <p>
15959 * The method should also return {@code true} in response to an action type of
15960 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15961 * {@code false} if it didn't.
15962 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015963 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015964 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015965 return false;
15966 }
15967
15968 /**
Joe Malin32736f02011-01-19 16:14:20 -080015969 * Detects if this View is enabled and has a drag event listener.
15970 * If both are true, then it calls the drag event listener with the
15971 * {@link android.view.DragEvent} it received. If the drag event listener returns
15972 * {@code true}, then dispatchDragEvent() returns {@code true}.
15973 * <p>
15974 * For all other cases, the method calls the
15975 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15976 * method and returns its result.
15977 * </p>
15978 * <p>
15979 * This ensures that a drag event is always consumed, even if the View does not have a drag
15980 * event listener. However, if the View has a listener and the listener returns true, then
15981 * onDragEvent() is not called.
15982 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015983 */
15984 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015985 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015986 ListenerInfo li = mListenerInfo;
15987 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15988 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015989 return true;
15990 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015991 return onDragEvent(event);
15992 }
15993
Christopher Tate3d4bf172011-03-28 16:16:46 -070015994 boolean canAcceptDrag() {
15995 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15996 }
15997
Christopher Tatea53146c2010-09-07 11:57:52 -070015998 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015999 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16000 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016001 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016002 */
16003 public void onCloseSystemDialogs(String reason) {
16004 }
Joe Malin32736f02011-01-19 16:14:20 -080016005
Dianne Hackbornffa42482009-09-23 22:20:11 -070016006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016007 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016008 * update a Region being computed for
16009 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016010 * that any non-transparent parts of the Drawable are removed from the
16011 * given transparent region.
16012 *
16013 * @param dr The Drawable whose transparency is to be applied to the region.
16014 * @param region A Region holding the current transparency information,
16015 * where any parts of the region that are set are considered to be
16016 * transparent. On return, this region will be modified to have the
16017 * transparency information reduced by the corresponding parts of the
16018 * Drawable that are not transparent.
16019 * {@hide}
16020 */
16021 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16022 if (DBG) {
16023 Log.i("View", "Getting transparent region for: " + this);
16024 }
16025 final Region r = dr.getTransparentRegion();
16026 final Rect db = dr.getBounds();
16027 final AttachInfo attachInfo = mAttachInfo;
16028 if (r != null && attachInfo != null) {
16029 final int w = getRight()-getLeft();
16030 final int h = getBottom()-getTop();
16031 if (db.left > 0) {
16032 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16033 r.op(0, 0, db.left, h, Region.Op.UNION);
16034 }
16035 if (db.right < w) {
16036 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16037 r.op(db.right, 0, w, h, Region.Op.UNION);
16038 }
16039 if (db.top > 0) {
16040 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16041 r.op(0, 0, w, db.top, Region.Op.UNION);
16042 }
16043 if (db.bottom < h) {
16044 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16045 r.op(0, db.bottom, w, h, Region.Op.UNION);
16046 }
16047 final int[] location = attachInfo.mTransparentLocation;
16048 getLocationInWindow(location);
16049 r.translate(location[0], location[1]);
16050 region.op(r, Region.Op.INTERSECT);
16051 } else {
16052 region.op(db, Region.Op.DIFFERENCE);
16053 }
16054 }
16055
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016056 private void checkForLongClick(int delayOffset) {
16057 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16058 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016059
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016060 if (mPendingCheckForLongPress == null) {
16061 mPendingCheckForLongPress = new CheckForLongPress();
16062 }
16063 mPendingCheckForLongPress.rememberWindowAttachCount();
16064 postDelayed(mPendingCheckForLongPress,
16065 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016067 }
16068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016069 /**
16070 * Inflate a view from an XML resource. This convenience method wraps the {@link
16071 * LayoutInflater} class, which provides a full range of options for view inflation.
16072 *
16073 * @param context The Context object for your activity or application.
16074 * @param resource The resource ID to inflate
16075 * @param root A view group that will be the parent. Used to properly inflate the
16076 * layout_* parameters.
16077 * @see LayoutInflater
16078 */
16079 public static View inflate(Context context, int resource, ViewGroup root) {
16080 LayoutInflater factory = LayoutInflater.from(context);
16081 return factory.inflate(resource, root);
16082 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016084 /**
Adam Powell637d3372010-08-25 14:37:03 -070016085 * Scroll the view with standard behavior for scrolling beyond the normal
16086 * content boundaries. Views that call this method should override
16087 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16088 * results of an over-scroll operation.
16089 *
16090 * Views can use this method to handle any touch or fling-based scrolling.
16091 *
16092 * @param deltaX Change in X in pixels
16093 * @param deltaY Change in Y in pixels
16094 * @param scrollX Current X scroll value in pixels before applying deltaX
16095 * @param scrollY Current Y scroll value in pixels before applying deltaY
16096 * @param scrollRangeX Maximum content scroll range along the X axis
16097 * @param scrollRangeY Maximum content scroll range along the Y axis
16098 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16099 * along the X axis.
16100 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16101 * along the Y axis.
16102 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16103 * @return true if scrolling was clamped to an over-scroll boundary along either
16104 * axis, false otherwise.
16105 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016106 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016107 protected boolean overScrollBy(int deltaX, int deltaY,
16108 int scrollX, int scrollY,
16109 int scrollRangeX, int scrollRangeY,
16110 int maxOverScrollX, int maxOverScrollY,
16111 boolean isTouchEvent) {
16112 final int overScrollMode = mOverScrollMode;
16113 final boolean canScrollHorizontal =
16114 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16115 final boolean canScrollVertical =
16116 computeVerticalScrollRange() > computeVerticalScrollExtent();
16117 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16118 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16119 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16120 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16121
16122 int newScrollX = scrollX + deltaX;
16123 if (!overScrollHorizontal) {
16124 maxOverScrollX = 0;
16125 }
16126
16127 int newScrollY = scrollY + deltaY;
16128 if (!overScrollVertical) {
16129 maxOverScrollY = 0;
16130 }
16131
16132 // Clamp values if at the limits and record
16133 final int left = -maxOverScrollX;
16134 final int right = maxOverScrollX + scrollRangeX;
16135 final int top = -maxOverScrollY;
16136 final int bottom = maxOverScrollY + scrollRangeY;
16137
16138 boolean clampedX = false;
16139 if (newScrollX > right) {
16140 newScrollX = right;
16141 clampedX = true;
16142 } else if (newScrollX < left) {
16143 newScrollX = left;
16144 clampedX = true;
16145 }
16146
16147 boolean clampedY = false;
16148 if (newScrollY > bottom) {
16149 newScrollY = bottom;
16150 clampedY = true;
16151 } else if (newScrollY < top) {
16152 newScrollY = top;
16153 clampedY = true;
16154 }
16155
16156 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16157
16158 return clampedX || clampedY;
16159 }
16160
16161 /**
16162 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16163 * respond to the results of an over-scroll operation.
16164 *
16165 * @param scrollX New X scroll value in pixels
16166 * @param scrollY New Y scroll value in pixels
16167 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16168 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16169 */
16170 protected void onOverScrolled(int scrollX, int scrollY,
16171 boolean clampedX, boolean clampedY) {
16172 // Intentionally empty.
16173 }
16174
16175 /**
16176 * Returns the over-scroll mode for this view. The result will be
16177 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16178 * (allow over-scrolling only if the view content is larger than the container),
16179 * or {@link #OVER_SCROLL_NEVER}.
16180 *
16181 * @return This view's over-scroll mode.
16182 */
16183 public int getOverScrollMode() {
16184 return mOverScrollMode;
16185 }
16186
16187 /**
16188 * Set the over-scroll mode for this view. Valid over-scroll modes are
16189 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16190 * (allow over-scrolling only if the view content is larger than the container),
16191 * or {@link #OVER_SCROLL_NEVER}.
16192 *
16193 * Setting the over-scroll mode of a view will have an effect only if the
16194 * view is capable of scrolling.
16195 *
16196 * @param overScrollMode The new over-scroll mode for this view.
16197 */
16198 public void setOverScrollMode(int overScrollMode) {
16199 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16200 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16201 overScrollMode != OVER_SCROLL_NEVER) {
16202 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16203 }
16204 mOverScrollMode = overScrollMode;
16205 }
16206
16207 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016208 * Gets a scale factor that determines the distance the view should scroll
16209 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16210 * @return The vertical scroll scale factor.
16211 * @hide
16212 */
16213 protected float getVerticalScrollFactor() {
16214 if (mVerticalScrollFactor == 0) {
16215 TypedValue outValue = new TypedValue();
16216 if (!mContext.getTheme().resolveAttribute(
16217 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16218 throw new IllegalStateException(
16219 "Expected theme to define listPreferredItemHeight.");
16220 }
16221 mVerticalScrollFactor = outValue.getDimension(
16222 mContext.getResources().getDisplayMetrics());
16223 }
16224 return mVerticalScrollFactor;
16225 }
16226
16227 /**
16228 * Gets a scale factor that determines the distance the view should scroll
16229 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16230 * @return The horizontal scroll scale factor.
16231 * @hide
16232 */
16233 protected float getHorizontalScrollFactor() {
16234 // TODO: Should use something else.
16235 return getVerticalScrollFactor();
16236 }
16237
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016238 /**
16239 * Return the value specifying the text direction or policy that was set with
16240 * {@link #setTextDirection(int)}.
16241 *
16242 * @return the defined text direction. It can be one of:
16243 *
16244 * {@link #TEXT_DIRECTION_INHERIT},
16245 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16246 * {@link #TEXT_DIRECTION_ANY_RTL},
16247 * {@link #TEXT_DIRECTION_LTR},
16248 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016249 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016250 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016251 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016252 @ViewDebug.ExportedProperty(category = "text", mapping = {
16253 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16254 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16255 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16256 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16257 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16258 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16259 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016260 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016261 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016262 }
16263
16264 /**
16265 * Set the text direction.
16266 *
16267 * @param textDirection the direction to set. Should be one of:
16268 *
16269 * {@link #TEXT_DIRECTION_INHERIT},
16270 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16271 * {@link #TEXT_DIRECTION_ANY_RTL},
16272 * {@link #TEXT_DIRECTION_LTR},
16273 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016274 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016275 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016276 */
16277 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016278 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016279 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016280 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016281 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016282 // Set the new text direction
16283 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016284 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016285 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016286 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016287 }
16288 }
16289
16290 /**
16291 * Return the resolved text direction.
16292 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016293 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16294 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16295 * up the parent chain of the view. if there is no parent, then it will return the default
16296 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16297 *
16298 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016299 *
Doug Feltcb3791202011-07-07 11:57:48 -070016300 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16301 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016302 * {@link #TEXT_DIRECTION_LTR},
16303 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016304 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016305 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016306 */
16307 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016308 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016309 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016310 resolveTextDirection();
16311 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016312 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016313 }
16314
16315 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016316 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16317 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016318 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016319 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016320 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016321 // Reset any previous text direction resolution
16322 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16323
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016324 if (hasRtlSupport()) {
16325 // Set resolved text direction flag depending on text direction flag
16326 final int textDirection = getTextDirection();
16327 switch(textDirection) {
16328 case TEXT_DIRECTION_INHERIT:
16329 if (canResolveTextDirection()) {
16330 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016331
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016332 // Set current resolved direction to the same value as the parent's one
16333 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16334 switch (parentResolvedDirection) {
16335 case TEXT_DIRECTION_FIRST_STRONG:
16336 case TEXT_DIRECTION_ANY_RTL:
16337 case TEXT_DIRECTION_LTR:
16338 case TEXT_DIRECTION_RTL:
16339 case TEXT_DIRECTION_LOCALE:
16340 mPrivateFlags2 |=
16341 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16342 break;
16343 default:
16344 // Default resolved direction is "first strong" heuristic
16345 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16346 }
16347 } else {
16348 // We cannot do the resolution if there is no parent, so use the default one
16349 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016350 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016351 break;
16352 case TEXT_DIRECTION_FIRST_STRONG:
16353 case TEXT_DIRECTION_ANY_RTL:
16354 case TEXT_DIRECTION_LTR:
16355 case TEXT_DIRECTION_RTL:
16356 case TEXT_DIRECTION_LOCALE:
16357 // Resolved direction is the same as text direction
16358 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16359 break;
16360 default:
16361 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016362 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016363 }
16364 } else {
16365 // Default resolved direction is "first strong" heuristic
16366 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016367 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016368
16369 // Set to resolved
16370 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016371 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016372 }
16373
16374 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016375 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016376 * resolution should override this method.
16377 *
16378 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016379 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016380 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016381 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016382 }
16383
16384 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016385 * Check if text direction resolution can be done.
16386 *
16387 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016388 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016389 */
16390 public boolean canResolveTextDirection() {
16391 switch (getTextDirection()) {
16392 case TEXT_DIRECTION_INHERIT:
16393 return (mParent != null) && (mParent instanceof ViewGroup);
16394 default:
16395 return true;
16396 }
16397 }
16398
16399 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016400 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016401 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016402 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016403 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016404 */
16405 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016406 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016407 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016408 }
16409
16410 /**
16411 * Called when text direction is reset. Subclasses that care about text direction reset should
16412 * override this method and do a reset of the text direction of their children. The default
16413 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016414 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016415 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016416 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016417 }
16418
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016419 /**
16420 * Return the value specifying the text alignment or policy that was set with
16421 * {@link #setTextAlignment(int)}.
16422 *
16423 * @return the defined text alignment. It can be one of:
16424 *
16425 * {@link #TEXT_ALIGNMENT_INHERIT},
16426 * {@link #TEXT_ALIGNMENT_GRAVITY},
16427 * {@link #TEXT_ALIGNMENT_CENTER},
16428 * {@link #TEXT_ALIGNMENT_TEXT_START},
16429 * {@link #TEXT_ALIGNMENT_TEXT_END},
16430 * {@link #TEXT_ALIGNMENT_VIEW_START},
16431 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016432 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016433 */
16434 @ViewDebug.ExportedProperty(category = "text", mapping = {
16435 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16436 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16437 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16438 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16439 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16440 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16441 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16442 })
16443 public int getTextAlignment() {
16444 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16445 }
16446
16447 /**
16448 * Set the text alignment.
16449 *
16450 * @param textAlignment The text alignment to set. Should be one of
16451 *
16452 * {@link #TEXT_ALIGNMENT_INHERIT},
16453 * {@link #TEXT_ALIGNMENT_GRAVITY},
16454 * {@link #TEXT_ALIGNMENT_CENTER},
16455 * {@link #TEXT_ALIGNMENT_TEXT_START},
16456 * {@link #TEXT_ALIGNMENT_TEXT_END},
16457 * {@link #TEXT_ALIGNMENT_VIEW_START},
16458 * {@link #TEXT_ALIGNMENT_VIEW_END}
16459 *
16460 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016461 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016462 */
16463 public void setTextAlignment(int textAlignment) {
16464 if (textAlignment != getTextAlignment()) {
16465 // Reset the current and resolved text alignment
16466 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16467 resetResolvedTextAlignment();
16468 // Set the new text alignment
16469 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16470 // Refresh
16471 requestLayout();
16472 invalidate(true);
16473 }
16474 }
16475
16476 /**
16477 * Return the resolved text alignment.
16478 *
16479 * The resolved text alignment. This needs resolution if the value is
16480 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16481 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16482 *
16483 * @return the resolved text alignment. Returns one of:
16484 *
16485 * {@link #TEXT_ALIGNMENT_GRAVITY},
16486 * {@link #TEXT_ALIGNMENT_CENTER},
16487 * {@link #TEXT_ALIGNMENT_TEXT_START},
16488 * {@link #TEXT_ALIGNMENT_TEXT_END},
16489 * {@link #TEXT_ALIGNMENT_VIEW_START},
16490 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016491 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016492 */
16493 @ViewDebug.ExportedProperty(category = "text", mapping = {
16494 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16495 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16496 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16497 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16498 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16499 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16500 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16501 })
16502 public int getResolvedTextAlignment() {
16503 // If text alignment is not resolved, then resolve it
16504 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16505 resolveTextAlignment();
16506 }
16507 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16508 }
16509
16510 /**
16511 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16512 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016513 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016514 */
16515 public void resolveTextAlignment() {
16516 // Reset any previous text alignment resolution
16517 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16518
16519 if (hasRtlSupport()) {
16520 // Set resolved text alignment flag depending on text alignment flag
16521 final int textAlignment = getTextAlignment();
16522 switch (textAlignment) {
16523 case TEXT_ALIGNMENT_INHERIT:
16524 // Check if we can resolve the text alignment
16525 if (canResolveLayoutDirection() && mParent instanceof View) {
16526 View view = (View) mParent;
16527
16528 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16529 switch (parentResolvedTextAlignment) {
16530 case TEXT_ALIGNMENT_GRAVITY:
16531 case TEXT_ALIGNMENT_TEXT_START:
16532 case TEXT_ALIGNMENT_TEXT_END:
16533 case TEXT_ALIGNMENT_CENTER:
16534 case TEXT_ALIGNMENT_VIEW_START:
16535 case TEXT_ALIGNMENT_VIEW_END:
16536 // Resolved text alignment is the same as the parent resolved
16537 // text alignment
16538 mPrivateFlags2 |=
16539 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16540 break;
16541 default:
16542 // Use default resolved text alignment
16543 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16544 }
16545 }
16546 else {
16547 // We cannot do the resolution if there is no parent so use the default
16548 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16549 }
16550 break;
16551 case TEXT_ALIGNMENT_GRAVITY:
16552 case TEXT_ALIGNMENT_TEXT_START:
16553 case TEXT_ALIGNMENT_TEXT_END:
16554 case TEXT_ALIGNMENT_CENTER:
16555 case TEXT_ALIGNMENT_VIEW_START:
16556 case TEXT_ALIGNMENT_VIEW_END:
16557 // Resolved text alignment is the same as text alignment
16558 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16559 break;
16560 default:
16561 // Use default resolved text alignment
16562 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16563 }
16564 } else {
16565 // Use default resolved text alignment
16566 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16567 }
16568
16569 // Set the resolved
16570 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16571 onResolvedTextAlignmentChanged();
16572 }
16573
16574 /**
16575 * Check if text alignment resolution can be done.
16576 *
16577 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016578 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016579 */
16580 public boolean canResolveTextAlignment() {
16581 switch (getTextAlignment()) {
16582 case TEXT_DIRECTION_INHERIT:
16583 return (mParent != null);
16584 default:
16585 return true;
16586 }
16587 }
16588
16589 /**
16590 * Called when text alignment has been resolved. Subclasses that care about text alignment
16591 * resolution should override this method.
16592 *
16593 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016594 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016595 */
16596 public void onResolvedTextAlignmentChanged() {
16597 }
16598
16599 /**
16600 * Reset resolved text alignment. Text alignment can be resolved with a call to
16601 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16602 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016603 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016604 */
16605 public void resetResolvedTextAlignment() {
16606 // Reset any previous text alignment resolution
16607 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16608 onResolvedTextAlignmentReset();
16609 }
16610
16611 /**
16612 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16613 * override this method and do a reset of the text alignment of their children. The default
16614 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016615 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016616 */
16617 public void onResolvedTextAlignmentReset() {
16618 }
16619
Chet Haaseb39f0512011-05-24 14:36:40 -070016620 //
16621 // Properties
16622 //
16623 /**
16624 * A Property wrapper around the <code>alpha</code> functionality handled by the
16625 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16626 */
Chet Haased47f1532011-12-16 11:18:52 -080016627 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016628 @Override
16629 public void setValue(View object, float value) {
16630 object.setAlpha(value);
16631 }
16632
16633 @Override
16634 public Float get(View object) {
16635 return object.getAlpha();
16636 }
16637 };
16638
16639 /**
16640 * A Property wrapper around the <code>translationX</code> functionality handled by the
16641 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16642 */
Chet Haased47f1532011-12-16 11:18:52 -080016643 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016644 @Override
16645 public void setValue(View object, float value) {
16646 object.setTranslationX(value);
16647 }
16648
16649 @Override
16650 public Float get(View object) {
16651 return object.getTranslationX();
16652 }
16653 };
16654
16655 /**
16656 * A Property wrapper around the <code>translationY</code> functionality handled by the
16657 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16658 */
Chet Haased47f1532011-12-16 11:18:52 -080016659 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016660 @Override
16661 public void setValue(View object, float value) {
16662 object.setTranslationY(value);
16663 }
16664
16665 @Override
16666 public Float get(View object) {
16667 return object.getTranslationY();
16668 }
16669 };
16670
16671 /**
16672 * A Property wrapper around the <code>x</code> functionality handled by the
16673 * {@link View#setX(float)} and {@link View#getX()} methods.
16674 */
Chet Haased47f1532011-12-16 11:18:52 -080016675 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016676 @Override
16677 public void setValue(View object, float value) {
16678 object.setX(value);
16679 }
16680
16681 @Override
16682 public Float get(View object) {
16683 return object.getX();
16684 }
16685 };
16686
16687 /**
16688 * A Property wrapper around the <code>y</code> functionality handled by the
16689 * {@link View#setY(float)} and {@link View#getY()} methods.
16690 */
Chet Haased47f1532011-12-16 11:18:52 -080016691 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016692 @Override
16693 public void setValue(View object, float value) {
16694 object.setY(value);
16695 }
16696
16697 @Override
16698 public Float get(View object) {
16699 return object.getY();
16700 }
16701 };
16702
16703 /**
16704 * A Property wrapper around the <code>rotation</code> functionality handled by the
16705 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16706 */
Chet Haased47f1532011-12-16 11:18:52 -080016707 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016708 @Override
16709 public void setValue(View object, float value) {
16710 object.setRotation(value);
16711 }
16712
16713 @Override
16714 public Float get(View object) {
16715 return object.getRotation();
16716 }
16717 };
16718
16719 /**
16720 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16721 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16722 */
Chet Haased47f1532011-12-16 11:18:52 -080016723 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016724 @Override
16725 public void setValue(View object, float value) {
16726 object.setRotationX(value);
16727 }
16728
16729 @Override
16730 public Float get(View object) {
16731 return object.getRotationX();
16732 }
16733 };
16734
16735 /**
16736 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16737 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16738 */
Chet Haased47f1532011-12-16 11:18:52 -080016739 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016740 @Override
16741 public void setValue(View object, float value) {
16742 object.setRotationY(value);
16743 }
16744
16745 @Override
16746 public Float get(View object) {
16747 return object.getRotationY();
16748 }
16749 };
16750
16751 /**
16752 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16753 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16754 */
Chet Haased47f1532011-12-16 11:18:52 -080016755 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016756 @Override
16757 public void setValue(View object, float value) {
16758 object.setScaleX(value);
16759 }
16760
16761 @Override
16762 public Float get(View object) {
16763 return object.getScaleX();
16764 }
16765 };
16766
16767 /**
16768 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16769 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16770 */
Chet Haased47f1532011-12-16 11:18:52 -080016771 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016772 @Override
16773 public void setValue(View object, float value) {
16774 object.setScaleY(value);
16775 }
16776
16777 @Override
16778 public Float get(View object) {
16779 return object.getScaleY();
16780 }
16781 };
16782
Jeff Brown33bbfd22011-02-24 20:55:35 -080016783 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016784 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16785 * Each MeasureSpec represents a requirement for either the width or the height.
16786 * A MeasureSpec is comprised of a size and a mode. There are three possible
16787 * modes:
16788 * <dl>
16789 * <dt>UNSPECIFIED</dt>
16790 * <dd>
16791 * The parent has not imposed any constraint on the child. It can be whatever size
16792 * it wants.
16793 * </dd>
16794 *
16795 * <dt>EXACTLY</dt>
16796 * <dd>
16797 * The parent has determined an exact size for the child. The child is going to be
16798 * given those bounds regardless of how big it wants to be.
16799 * </dd>
16800 *
16801 * <dt>AT_MOST</dt>
16802 * <dd>
16803 * The child can be as large as it wants up to the specified size.
16804 * </dd>
16805 * </dl>
16806 *
16807 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16808 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16809 */
16810 public static class MeasureSpec {
16811 private static final int MODE_SHIFT = 30;
16812 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16813
16814 /**
16815 * Measure specification mode: The parent has not imposed any constraint
16816 * on the child. It can be whatever size it wants.
16817 */
16818 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16819
16820 /**
16821 * Measure specification mode: The parent has determined an exact size
16822 * for the child. The child is going to be given those bounds regardless
16823 * of how big it wants to be.
16824 */
16825 public static final int EXACTLY = 1 << MODE_SHIFT;
16826
16827 /**
16828 * Measure specification mode: The child can be as large as it wants up
16829 * to the specified size.
16830 */
16831 public static final int AT_MOST = 2 << MODE_SHIFT;
16832
16833 /**
16834 * Creates a measure specification based on the supplied size and mode.
16835 *
16836 * The mode must always be one of the following:
16837 * <ul>
16838 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16839 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16840 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16841 * </ul>
16842 *
16843 * @param size the size of the measure specification
16844 * @param mode the mode of the measure specification
16845 * @return the measure specification based on size and mode
16846 */
16847 public static int makeMeasureSpec(int size, int mode) {
16848 return size + mode;
16849 }
16850
16851 /**
16852 * Extracts the mode from the supplied measure specification.
16853 *
16854 * @param measureSpec the measure specification to extract the mode from
16855 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16856 * {@link android.view.View.MeasureSpec#AT_MOST} or
16857 * {@link android.view.View.MeasureSpec#EXACTLY}
16858 */
16859 public static int getMode(int measureSpec) {
16860 return (measureSpec & MODE_MASK);
16861 }
16862
16863 /**
16864 * Extracts the size from the supplied measure specification.
16865 *
16866 * @param measureSpec the measure specification to extract the size from
16867 * @return the size in pixels defined in the supplied measure specification
16868 */
16869 public static int getSize(int measureSpec) {
16870 return (measureSpec & ~MODE_MASK);
16871 }
16872
16873 /**
16874 * Returns a String representation of the specified measure
16875 * specification.
16876 *
16877 * @param measureSpec the measure specification to convert to a String
16878 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16879 */
16880 public static String toString(int measureSpec) {
16881 int mode = getMode(measureSpec);
16882 int size = getSize(measureSpec);
16883
16884 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16885
16886 if (mode == UNSPECIFIED)
16887 sb.append("UNSPECIFIED ");
16888 else if (mode == EXACTLY)
16889 sb.append("EXACTLY ");
16890 else if (mode == AT_MOST)
16891 sb.append("AT_MOST ");
16892 else
16893 sb.append(mode).append(" ");
16894
16895 sb.append(size);
16896 return sb.toString();
16897 }
16898 }
16899
16900 class CheckForLongPress implements Runnable {
16901
16902 private int mOriginalWindowAttachCount;
16903
16904 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016905 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016906 && mOriginalWindowAttachCount == mWindowAttachCount) {
16907 if (performLongClick()) {
16908 mHasPerformedLongPress = true;
16909 }
16910 }
16911 }
16912
16913 public void rememberWindowAttachCount() {
16914 mOriginalWindowAttachCount = mWindowAttachCount;
16915 }
16916 }
Joe Malin32736f02011-01-19 16:14:20 -080016917
Adam Powelle14579b2009-12-16 18:39:52 -080016918 private final class CheckForTap implements Runnable {
16919 public void run() {
16920 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016921 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016922 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016923 }
16924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016925
Adam Powella35d7682010-03-12 14:48:13 -080016926 private final class PerformClick implements Runnable {
16927 public void run() {
16928 performClick();
16929 }
16930 }
16931
Dianne Hackborn63042d62011-01-26 18:56:29 -080016932 /** @hide */
16933 public void hackTurnOffWindowResizeAnim(boolean off) {
16934 mAttachInfo.mTurnOffWindowResizeAnim = off;
16935 }
Joe Malin32736f02011-01-19 16:14:20 -080016936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016937 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016938 * This method returns a ViewPropertyAnimator object, which can be used to animate
16939 * specific properties on this View.
16940 *
16941 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16942 */
16943 public ViewPropertyAnimator animate() {
16944 if (mAnimator == null) {
16945 mAnimator = new ViewPropertyAnimator(this);
16946 }
16947 return mAnimator;
16948 }
16949
16950 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016951 * Interface definition for a callback to be invoked when a hardware key event is
16952 * dispatched to this view. The callback will be invoked before the key event is
16953 * given to the view. This is only useful for hardware keyboards; a software input
16954 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016955 */
16956 public interface OnKeyListener {
16957 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016958 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016959 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016960 * <p>Key presses in software keyboards will generally NOT trigger this method,
16961 * although some may elect to do so in some situations. Do not assume a
16962 * software input method has to be key-based; even if it is, it may use key presses
16963 * in a different way than you expect, so there is no way to reliably catch soft
16964 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016965 *
16966 * @param v The view the key has been dispatched to.
16967 * @param keyCode The code for the physical key that was pressed
16968 * @param event The KeyEvent object containing full information about
16969 * the event.
16970 * @return True if the listener has consumed the event, false otherwise.
16971 */
16972 boolean onKey(View v, int keyCode, KeyEvent event);
16973 }
16974
16975 /**
16976 * Interface definition for a callback to be invoked when a touch event is
16977 * dispatched to this view. The callback will be invoked before the touch
16978 * event is given to the view.
16979 */
16980 public interface OnTouchListener {
16981 /**
16982 * Called when a touch event is dispatched to a view. This allows listeners to
16983 * get a chance to respond before the target view.
16984 *
16985 * @param v The view the touch event has been dispatched to.
16986 * @param event The MotionEvent object containing full information about
16987 * the event.
16988 * @return True if the listener has consumed the event, false otherwise.
16989 */
16990 boolean onTouch(View v, MotionEvent event);
16991 }
16992
16993 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016994 * Interface definition for a callback to be invoked when a hover event is
16995 * dispatched to this view. The callback will be invoked before the hover
16996 * event is given to the view.
16997 */
16998 public interface OnHoverListener {
16999 /**
17000 * Called when a hover event is dispatched to a view. This allows listeners to
17001 * get a chance to respond before the target view.
17002 *
17003 * @param v The view the hover event has been dispatched to.
17004 * @param event The MotionEvent object containing full information about
17005 * the event.
17006 * @return True if the listener has consumed the event, false otherwise.
17007 */
17008 boolean onHover(View v, MotionEvent event);
17009 }
17010
17011 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017012 * Interface definition for a callback to be invoked when a generic motion event is
17013 * dispatched to this view. The callback will be invoked before the generic motion
17014 * event is given to the view.
17015 */
17016 public interface OnGenericMotionListener {
17017 /**
17018 * Called when a generic motion event is dispatched to a view. This allows listeners to
17019 * get a chance to respond before the target view.
17020 *
17021 * @param v The view the generic motion event has been dispatched to.
17022 * @param event The MotionEvent object containing full information about
17023 * the event.
17024 * @return True if the listener has consumed the event, false otherwise.
17025 */
17026 boolean onGenericMotion(View v, MotionEvent event);
17027 }
17028
17029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017030 * Interface definition for a callback to be invoked when a view has been clicked and held.
17031 */
17032 public interface OnLongClickListener {
17033 /**
17034 * Called when a view has been clicked and held.
17035 *
17036 * @param v The view that was clicked and held.
17037 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017038 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017039 */
17040 boolean onLongClick(View v);
17041 }
17042
17043 /**
Chris Tate32affef2010-10-18 15:29:21 -070017044 * Interface definition for a callback to be invoked when a drag is being dispatched
17045 * to this view. The callback will be invoked before the hosting view's own
17046 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17047 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017048 *
17049 * <div class="special reference">
17050 * <h3>Developer Guides</h3>
17051 * <p>For a guide to implementing drag and drop features, read the
17052 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17053 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017054 */
17055 public interface OnDragListener {
17056 /**
17057 * Called when a drag event is dispatched to a view. This allows listeners
17058 * to get a chance to override base View behavior.
17059 *
Joe Malin32736f02011-01-19 16:14:20 -080017060 * @param v The View that received the drag event.
17061 * @param event The {@link android.view.DragEvent} object for the drag event.
17062 * @return {@code true} if the drag event was handled successfully, or {@code false}
17063 * if the drag event was not handled. Note that {@code false} will trigger the View
17064 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017065 */
17066 boolean onDrag(View v, DragEvent event);
17067 }
17068
17069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017070 * Interface definition for a callback to be invoked when the focus state of
17071 * a view changed.
17072 */
17073 public interface OnFocusChangeListener {
17074 /**
17075 * Called when the focus state of a view has changed.
17076 *
17077 * @param v The view whose state has changed.
17078 * @param hasFocus The new focus state of v.
17079 */
17080 void onFocusChange(View v, boolean hasFocus);
17081 }
17082
17083 /**
17084 * Interface definition for a callback to be invoked when a view is clicked.
17085 */
17086 public interface OnClickListener {
17087 /**
17088 * Called when a view has been clicked.
17089 *
17090 * @param v The view that was clicked.
17091 */
17092 void onClick(View v);
17093 }
17094
17095 /**
17096 * Interface definition for a callback to be invoked when the context menu
17097 * for this view is being built.
17098 */
17099 public interface OnCreateContextMenuListener {
17100 /**
17101 * Called when the context menu for this view is being built. It is not
17102 * safe to hold onto the menu after this method returns.
17103 *
17104 * @param menu The context menu that is being built
17105 * @param v The view for which the context menu is being built
17106 * @param menuInfo Extra information about the item for which the
17107 * context menu should be shown. This information will vary
17108 * depending on the class of v.
17109 */
17110 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17111 }
17112
Joe Onorato664644d2011-01-23 17:53:23 -080017113 /**
17114 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017115 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017116 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017117 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017118 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017119 */
17120 public interface OnSystemUiVisibilityChangeListener {
17121 /**
17122 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017123 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017124 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017125 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17126 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17127 * This tells you the <strong>global</strong> state of these UI visibility
17128 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017129 */
17130 public void onSystemUiVisibilityChange(int visibility);
17131 }
17132
Adam Powell4afd62b2011-02-18 15:02:18 -080017133 /**
17134 * Interface definition for a callback to be invoked when this view is attached
17135 * or detached from its window.
17136 */
17137 public interface OnAttachStateChangeListener {
17138 /**
17139 * Called when the view is attached to a window.
17140 * @param v The view that was attached
17141 */
17142 public void onViewAttachedToWindow(View v);
17143 /**
17144 * Called when the view is detached from a window.
17145 * @param v The view that was detached
17146 */
17147 public void onViewDetachedFromWindow(View v);
17148 }
17149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017150 private final class UnsetPressedState implements Runnable {
17151 public void run() {
17152 setPressed(false);
17153 }
17154 }
17155
17156 /**
17157 * Base class for derived classes that want to save and restore their own
17158 * state in {@link android.view.View#onSaveInstanceState()}.
17159 */
17160 public static class BaseSavedState extends AbsSavedState {
17161 /**
17162 * Constructor used when reading from a parcel. Reads the state of the superclass.
17163 *
17164 * @param source
17165 */
17166 public BaseSavedState(Parcel source) {
17167 super(source);
17168 }
17169
17170 /**
17171 * Constructor called by derived classes when creating their SavedState objects
17172 *
17173 * @param superState The state of the superclass of this view
17174 */
17175 public BaseSavedState(Parcelable superState) {
17176 super(superState);
17177 }
17178
17179 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17180 new Parcelable.Creator<BaseSavedState>() {
17181 public BaseSavedState createFromParcel(Parcel in) {
17182 return new BaseSavedState(in);
17183 }
17184
17185 public BaseSavedState[] newArray(int size) {
17186 return new BaseSavedState[size];
17187 }
17188 };
17189 }
17190
17191 /**
17192 * A set of information given to a view when it is attached to its parent
17193 * window.
17194 */
17195 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017196 interface Callbacks {
17197 void playSoundEffect(int effectId);
17198 boolean performHapticFeedback(int effectId, boolean always);
17199 }
17200
17201 /**
17202 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17203 * to a Handler. This class contains the target (View) to invalidate and
17204 * the coordinates of the dirty rectangle.
17205 *
17206 * For performance purposes, this class also implements a pool of up to
17207 * POOL_LIMIT objects that get reused. This reduces memory allocations
17208 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017209 */
Romain Guyd928d682009-03-31 17:52:16 -070017210 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017211 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017212 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17213 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017214 public InvalidateInfo newInstance() {
17215 return new InvalidateInfo();
17216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017217
Romain Guyd928d682009-03-31 17:52:16 -070017218 public void onAcquired(InvalidateInfo element) {
17219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017220
Romain Guyd928d682009-03-31 17:52:16 -070017221 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017222 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017223 }
17224 }, POOL_LIMIT)
17225 );
17226
17227 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017228 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017229
17230 View target;
17231
17232 int left;
17233 int top;
17234 int right;
17235 int bottom;
17236
Romain Guyd928d682009-03-31 17:52:16 -070017237 public void setNextPoolable(InvalidateInfo element) {
17238 mNext = element;
17239 }
17240
17241 public InvalidateInfo getNextPoolable() {
17242 return mNext;
17243 }
17244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017245 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017246 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017247 }
17248
17249 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017250 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017251 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017252
17253 public boolean isPooled() {
17254 return mIsPooled;
17255 }
17256
17257 public void setPooled(boolean isPooled) {
17258 mIsPooled = isPooled;
17259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017260 }
17261
17262 final IWindowSession mSession;
17263
17264 final IWindow mWindow;
17265
17266 final IBinder mWindowToken;
17267
17268 final Callbacks mRootCallbacks;
17269
Romain Guy59a12ca2011-06-09 17:48:21 -070017270 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017272 /**
17273 * The top view of the hierarchy.
17274 */
17275 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017277 IBinder mPanelParentWindowToken;
17278 Surface mSurface;
17279
Romain Guyb051e892010-09-28 19:09:36 -070017280 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017281 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017282 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017283
Romain Guy7e4e5612012-03-05 14:37:29 -080017284 boolean mScreenOn;
17285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017286 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017287 * Scale factor used by the compatibility mode
17288 */
17289 float mApplicationScale;
17290
17291 /**
17292 * Indicates whether the application is in compatibility mode
17293 */
17294 boolean mScalingRequired;
17295
17296 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017297 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017298 */
17299 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017300
Dianne Hackborn63042d62011-01-26 18:56:29 -080017301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017302 * Left position of this view's window
17303 */
17304 int mWindowLeft;
17305
17306 /**
17307 * Top position of this view's window
17308 */
17309 int mWindowTop;
17310
17311 /**
Adam Powell26153a32010-11-08 15:22:27 -080017312 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017313 */
Adam Powell26153a32010-11-08 15:22:27 -080017314 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017315
17316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017317 * For windows that are full-screen but using insets to layout inside
17318 * of the screen decorations, these are the current insets for the
17319 * content of the window.
17320 */
17321 final Rect mContentInsets = new Rect();
17322
17323 /**
17324 * For windows that are full-screen but using insets to layout inside
17325 * of the screen decorations, these are the current insets for the
17326 * actual visible parts of the window.
17327 */
17328 final Rect mVisibleInsets = new Rect();
17329
17330 /**
17331 * The internal insets given by this window. This value is
17332 * supplied by the client (through
17333 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17334 * be given to the window manager when changed to be used in laying
17335 * out windows behind it.
17336 */
17337 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17338 = new ViewTreeObserver.InternalInsetsInfo();
17339
17340 /**
17341 * All views in the window's hierarchy that serve as scroll containers,
17342 * used to determine if the window can be resized or must be panned
17343 * to adjust for a soft input area.
17344 */
17345 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17346
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017347 final KeyEvent.DispatcherState mKeyDispatchState
17348 = new KeyEvent.DispatcherState();
17349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017350 /**
17351 * Indicates whether the view's window currently has the focus.
17352 */
17353 boolean mHasWindowFocus;
17354
17355 /**
17356 * The current visibility of the window.
17357 */
17358 int mWindowVisibility;
17359
17360 /**
17361 * Indicates the time at which drawing started to occur.
17362 */
17363 long mDrawingTime;
17364
17365 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017366 * Indicates whether or not ignoring the DIRTY_MASK flags.
17367 */
17368 boolean mIgnoreDirtyState;
17369
17370 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017371 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17372 * to avoid clearing that flag prematurely.
17373 */
17374 boolean mSetIgnoreDirtyState = false;
17375
17376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017377 * Indicates whether the view's window is currently in touch mode.
17378 */
17379 boolean mInTouchMode;
17380
17381 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017382 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017383 * the next time it performs a traversal
17384 */
17385 boolean mRecomputeGlobalAttributes;
17386
17387 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017388 * Always report new attributes at next traversal.
17389 */
17390 boolean mForceReportNewAttributes;
17391
17392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017393 * Set during a traveral if any views want to keep the screen on.
17394 */
17395 boolean mKeepScreenOn;
17396
17397 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017398 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17399 */
17400 int mSystemUiVisibility;
17401
17402 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017403 * Hack to force certain system UI visibility flags to be cleared.
17404 */
17405 int mDisabledSystemUiVisibility;
17406
17407 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017408 * Last global system UI visibility reported by the window manager.
17409 */
17410 int mGlobalSystemUiVisibility;
17411
17412 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017413 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17414 * attached.
17415 */
17416 boolean mHasSystemUiListeners;
17417
17418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017419 * Set if the visibility of any views has changed.
17420 */
17421 boolean mViewVisibilityChanged;
17422
17423 /**
17424 * Set to true if a view has been scrolled.
17425 */
17426 boolean mViewScrollChanged;
17427
17428 /**
17429 * Global to the view hierarchy used as a temporary for dealing with
17430 * x/y points in the transparent region computations.
17431 */
17432 final int[] mTransparentLocation = new int[2];
17433
17434 /**
17435 * Global to the view hierarchy used as a temporary for dealing with
17436 * x/y points in the ViewGroup.invalidateChild implementation.
17437 */
17438 final int[] mInvalidateChildLocation = new int[2];
17439
Chet Haasec3aa3612010-06-17 08:50:37 -070017440
17441 /**
17442 * Global to the view hierarchy used as a temporary for dealing with
17443 * x/y location when view is transformed.
17444 */
17445 final float[] mTmpTransformLocation = new float[2];
17446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017447 /**
17448 * The view tree observer used to dispatch global events like
17449 * layout, pre-draw, touch mode change, etc.
17450 */
17451 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17452
17453 /**
17454 * A Canvas used by the view hierarchy to perform bitmap caching.
17455 */
17456 Canvas mCanvas;
17457
17458 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017459 * The view root impl.
17460 */
17461 final ViewRootImpl mViewRootImpl;
17462
17463 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017464 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017465 * handler can be used to pump events in the UI events queue.
17466 */
17467 final Handler mHandler;
17468
17469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017470 * Temporary for use in computing invalidate rectangles while
17471 * calling up the hierarchy.
17472 */
17473 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017474
17475 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017476 * Temporary for use in computing hit areas with transformed views
17477 */
17478 final RectF mTmpTransformRect = new RectF();
17479
17480 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017481 * Temporary list for use in collecting focusable descendents of a view.
17482 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017483 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017485 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017486 * The id of the window for accessibility purposes.
17487 */
17488 int mAccessibilityWindowId = View.NO_ID;
17489
17490 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017491 * Whether to ingore not exposed for accessibility Views when
17492 * reporting the view tree to accessibility services.
17493 */
17494 boolean mIncludeNotImportantViews;
17495
17496 /**
17497 * The drawable for highlighting accessibility focus.
17498 */
17499 Drawable mAccessibilityFocusDrawable;
17500
17501 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017502 * Show where the margins, bounds and layout bounds are for each view.
17503 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017504 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017505
17506 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017507 * Point used to compute visible regions.
17508 */
17509 final Point mPoint = new Point();
17510
17511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017512 * Creates a new set of attachment information with the specified
17513 * events handler and thread.
17514 *
17515 * @param handler the events handler the view must use
17516 */
17517 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017518 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017519 mSession = session;
17520 mWindow = window;
17521 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017522 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017523 mHandler = handler;
17524 mRootCallbacks = effectPlayer;
17525 }
17526 }
17527
17528 /**
17529 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17530 * is supported. This avoids keeping too many unused fields in most
17531 * instances of View.</p>
17532 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017533 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017534
Mike Cleronf116bf82009-09-27 19:14:12 -070017535 /**
17536 * Scrollbars are not visible
17537 */
17538 public static final int OFF = 0;
17539
17540 /**
17541 * Scrollbars are visible
17542 */
17543 public static final int ON = 1;
17544
17545 /**
17546 * Scrollbars are fading away
17547 */
17548 public static final int FADING = 2;
17549
17550 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017552 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017553 public int scrollBarDefaultDelayBeforeFade;
17554 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017555
17556 public int scrollBarSize;
17557 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017558 public float[] interpolatorValues;
17559 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017560
17561 public final Paint paint;
17562 public final Matrix matrix;
17563 public Shader shader;
17564
Mike Cleronf116bf82009-09-27 19:14:12 -070017565 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17566
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017567 private static final float[] OPAQUE = { 255 };
17568 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017569
Mike Cleronf116bf82009-09-27 19:14:12 -070017570 /**
17571 * When fading should start. This time moves into the future every time
17572 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17573 */
17574 public long fadeStartTime;
17575
17576
17577 /**
17578 * The current state of the scrollbars: ON, OFF, or FADING
17579 */
17580 public int state = OFF;
17581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017582 private int mLastColor;
17583
Mike Cleronf116bf82009-09-27 19:14:12 -070017584 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017585 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17586 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017587 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17588 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017589
17590 paint = new Paint();
17591 matrix = new Matrix();
17592 // use use a height of 1, and then wack the matrix each time we
17593 // actually use it.
17594 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017596 paint.setShader(shader);
17597 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017598 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017599 }
Romain Guy8506ab42009-06-11 17:35:47 -070017600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017601 public void setFadeColor(int color) {
17602 if (color != 0 && color != mLastColor) {
17603 mLastColor = color;
17604 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017605
Romain Guye55e1a72009-08-27 10:42:26 -070017606 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17607 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017609 paint.setShader(shader);
17610 // Restore the default transfer mode (src_over)
17611 paint.setXfermode(null);
17612 }
17613 }
Joe Malin32736f02011-01-19 16:14:20 -080017614
Mike Cleronf116bf82009-09-27 19:14:12 -070017615 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017616 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017617 if (now >= fadeStartTime) {
17618
17619 // the animation fades the scrollbars out by changing
17620 // the opacity (alpha) from fully opaque to fully
17621 // transparent
17622 int nextFrame = (int) now;
17623 int framesCount = 0;
17624
17625 Interpolator interpolator = scrollBarInterpolator;
17626
17627 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017628 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017629
17630 // End transparent
17631 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017632 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017633
17634 state = FADING;
17635
17636 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017637 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017638 }
17639 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017640 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017641
Svetoslav Ganova0156172011-06-26 17:55:44 -070017642 /**
17643 * Resuable callback for sending
17644 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17645 */
17646 private class SendViewScrolledAccessibilityEvent implements Runnable {
17647 public volatile boolean mIsPending;
17648
17649 public void run() {
17650 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17651 mIsPending = false;
17652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017653 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017654
17655 /**
17656 * <p>
17657 * This class represents a delegate that can be registered in a {@link View}
17658 * to enhance accessibility support via composition rather via inheritance.
17659 * It is specifically targeted to widget developers that extend basic View
17660 * classes i.e. classes in package android.view, that would like their
17661 * applications to be backwards compatible.
17662 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017663 * <div class="special reference">
17664 * <h3>Developer Guides</h3>
17665 * <p>For more information about making applications accessible, read the
17666 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17667 * developer guide.</p>
17668 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017669 * <p>
17670 * A scenario in which a developer would like to use an accessibility delegate
17671 * is overriding a method introduced in a later API version then the minimal API
17672 * version supported by the application. For example, the method
17673 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17674 * in API version 4 when the accessibility APIs were first introduced. If a
17675 * developer would like his application to run on API version 4 devices (assuming
17676 * all other APIs used by the application are version 4 or lower) and take advantage
17677 * of this method, instead of overriding the method which would break the application's
17678 * backwards compatibility, he can override the corresponding method in this
17679 * delegate and register the delegate in the target View if the API version of
17680 * the system is high enough i.e. the API version is same or higher to the API
17681 * version that introduced
17682 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17683 * </p>
17684 * <p>
17685 * Here is an example implementation:
17686 * </p>
17687 * <code><pre><p>
17688 * if (Build.VERSION.SDK_INT >= 14) {
17689 * // If the API version is equal of higher than the version in
17690 * // which onInitializeAccessibilityNodeInfo was introduced we
17691 * // register a delegate with a customized implementation.
17692 * View view = findViewById(R.id.view_id);
17693 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17694 * public void onInitializeAccessibilityNodeInfo(View host,
17695 * AccessibilityNodeInfo info) {
17696 * // Let the default implementation populate the info.
17697 * super.onInitializeAccessibilityNodeInfo(host, info);
17698 * // Set some other information.
17699 * info.setEnabled(host.isEnabled());
17700 * }
17701 * });
17702 * }
17703 * </code></pre></p>
17704 * <p>
17705 * This delegate contains methods that correspond to the accessibility methods
17706 * in View. If a delegate has been specified the implementation in View hands
17707 * off handling to the corresponding method in this delegate. The default
17708 * implementation the delegate methods behaves exactly as the corresponding
17709 * method in View for the case of no accessibility delegate been set. Hence,
17710 * to customize the behavior of a View method, clients can override only the
17711 * corresponding delegate method without altering the behavior of the rest
17712 * accessibility related methods of the host view.
17713 * </p>
17714 */
17715 public static class AccessibilityDelegate {
17716
17717 /**
17718 * Sends an accessibility event of the given type. If accessibility is not
17719 * enabled this method has no effect.
17720 * <p>
17721 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17722 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17723 * been set.
17724 * </p>
17725 *
17726 * @param host The View hosting the delegate.
17727 * @param eventType The type of the event to send.
17728 *
17729 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17730 */
17731 public void sendAccessibilityEvent(View host, int eventType) {
17732 host.sendAccessibilityEventInternal(eventType);
17733 }
17734
17735 /**
alanv8eeefef2012-05-07 16:57:53 -070017736 * Performs the specified accessibility action on the view. For
17737 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17738 * <p>
17739 * The default implementation behaves as
17740 * {@link View#performAccessibilityAction(int, Bundle)
17741 * View#performAccessibilityAction(int, Bundle)} for the case of
17742 * no accessibility delegate been set.
17743 * </p>
17744 *
17745 * @param action The action to perform.
17746 * @return Whether the action was performed.
17747 *
17748 * @see View#performAccessibilityAction(int, Bundle)
17749 * View#performAccessibilityAction(int, Bundle)
17750 */
17751 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17752 return host.performAccessibilityActionInternal(action, args);
17753 }
17754
17755 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017756 * Sends an accessibility event. This method behaves exactly as
17757 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17758 * empty {@link AccessibilityEvent} and does not perform a check whether
17759 * accessibility is enabled.
17760 * <p>
17761 * The default implementation behaves as
17762 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17763 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17764 * the case of no accessibility delegate been set.
17765 * </p>
17766 *
17767 * @param host The View hosting the delegate.
17768 * @param event The event to send.
17769 *
17770 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17771 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17772 */
17773 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17774 host.sendAccessibilityEventUncheckedInternal(event);
17775 }
17776
17777 /**
17778 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17779 * to its children for adding their text content to the event.
17780 * <p>
17781 * The default implementation behaves as
17782 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17783 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17784 * the case of no accessibility delegate been set.
17785 * </p>
17786 *
17787 * @param host The View hosting the delegate.
17788 * @param event The event.
17789 * @return True if the event population was completed.
17790 *
17791 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17792 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17793 */
17794 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17795 return host.dispatchPopulateAccessibilityEventInternal(event);
17796 }
17797
17798 /**
17799 * Gives a chance to the host View to populate the accessibility event with its
17800 * text content.
17801 * <p>
17802 * The default implementation behaves as
17803 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17804 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17805 * the case of no accessibility delegate been set.
17806 * </p>
17807 *
17808 * @param host The View hosting the delegate.
17809 * @param event The accessibility event which to populate.
17810 *
17811 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17812 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17813 */
17814 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17815 host.onPopulateAccessibilityEventInternal(event);
17816 }
17817
17818 /**
17819 * Initializes an {@link AccessibilityEvent} with information about the
17820 * the host View which is the event source.
17821 * <p>
17822 * The default implementation behaves as
17823 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17824 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17825 * the case of no accessibility delegate been set.
17826 * </p>
17827 *
17828 * @param host The View hosting the delegate.
17829 * @param event The event to initialize.
17830 *
17831 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17832 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17833 */
17834 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17835 host.onInitializeAccessibilityEventInternal(event);
17836 }
17837
17838 /**
17839 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17840 * <p>
17841 * The default implementation behaves as
17842 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17843 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17844 * the case of no accessibility delegate been set.
17845 * </p>
17846 *
17847 * @param host The View hosting the delegate.
17848 * @param info The instance to initialize.
17849 *
17850 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17851 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17852 */
17853 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17854 host.onInitializeAccessibilityNodeInfoInternal(info);
17855 }
17856
17857 /**
17858 * Called when a child of the host View has requested sending an
17859 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17860 * to augment the event.
17861 * <p>
17862 * The default implementation behaves as
17863 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17864 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17865 * the case of no accessibility delegate been set.
17866 * </p>
17867 *
17868 * @param host The View hosting the delegate.
17869 * @param child The child which requests sending the event.
17870 * @param event The event to be sent.
17871 * @return True if the event should be sent
17872 *
17873 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17874 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17875 */
17876 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17877 AccessibilityEvent event) {
17878 return host.onRequestSendAccessibilityEventInternal(child, event);
17879 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017880
17881 /**
17882 * Gets the provider for managing a virtual view hierarchy rooted at this View
17883 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17884 * that explore the window content.
17885 * <p>
17886 * The default implementation behaves as
17887 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17888 * the case of no accessibility delegate been set.
17889 * </p>
17890 *
17891 * @return The provider.
17892 *
17893 * @see AccessibilityNodeProvider
17894 */
17895 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17896 return null;
17897 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017899}