blob: f005eeba151c68b193d0ccde23992bfc20c69741 [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));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003612 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614 }
3615
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003616 a.recycle();
3617
Adam Powell637d3372010-08-25 14:37:03 -07003618 setOverScrollMode(overScrollMode);
3619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003621 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 }
3623
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003624 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3625 // layout direction). Those cached values will be used later during padding resolution.
3626 mUserPaddingStart = startPadding;
3627 mUserPaddingEnd = endPadding;
3628
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003629 updateUserPaddingRelative();
3630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 if (padding >= 0) {
3632 leftPadding = padding;
3633 topPadding = padding;
3634 rightPadding = padding;
3635 bottomPadding = padding;
3636 }
3637
3638 // If the user specified the padding (either with android:padding or
3639 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3640 // use the default padding or the padding from the background drawable
3641 // (stored at this point in mPadding*)
3642 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3643 topPadding >= 0 ? topPadding : mPaddingTop,
3644 rightPadding >= 0 ? rightPadding : mPaddingRight,
3645 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3646
3647 if (viewFlagMasks != 0) {
3648 setFlags(viewFlagValues, viewFlagMasks);
3649 }
3650
3651 // Needs to be called after mViewFlags is set
3652 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3653 recomputePadding();
3654 }
3655
3656 if (x != 0 || y != 0) {
3657 scrollTo(x, y);
3658 }
3659
Chet Haase73066682010-11-29 15:55:32 -08003660 if (transformSet) {
3661 setTranslationX(tx);
3662 setTranslationY(ty);
3663 setRotation(rotation);
3664 setRotationX(rotationX);
3665 setRotationY(rotationY);
3666 setScaleX(sx);
3667 setScaleY(sy);
3668 }
3669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3671 setScrollContainer(true);
3672 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003673
3674 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 }
3676
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003677 private void updateUserPaddingRelative() {
3678 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3679 }
3680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 /**
3682 * Non-public constructor for use in testing
3683 */
3684 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003685 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 }
3687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 /**
3689 * <p>
3690 * Initializes the fading edges from a given set of styled attributes. This
3691 * method should be called by subclasses that need fading edges and when an
3692 * instance of these subclasses is created programmatically rather than
3693 * being inflated from XML. This method is automatically called when the XML
3694 * is inflated.
3695 * </p>
3696 *
3697 * @param a the styled attributes set to initialize the fading edges from
3698 */
3699 protected void initializeFadingEdge(TypedArray a) {
3700 initScrollCache();
3701
3702 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3703 R.styleable.View_fadingEdgeLength,
3704 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3705 }
3706
3707 /**
3708 * Returns the size of the vertical faded edges used to indicate that more
3709 * content in this view is visible.
3710 *
3711 * @return The size in pixels of the vertical faded edge or 0 if vertical
3712 * faded edges are not enabled for this view.
3713 * @attr ref android.R.styleable#View_fadingEdgeLength
3714 */
3715 public int getVerticalFadingEdgeLength() {
3716 if (isVerticalFadingEdgeEnabled()) {
3717 ScrollabilityCache cache = mScrollCache;
3718 if (cache != null) {
3719 return cache.fadingEdgeLength;
3720 }
3721 }
3722 return 0;
3723 }
3724
3725 /**
3726 * Set the size of the faded edge used to indicate that more content in this
3727 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003728 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3729 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3730 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 *
3732 * @param length The size in pixels of the faded edge used to indicate that more
3733 * content in this view is visible.
3734 */
3735 public void setFadingEdgeLength(int length) {
3736 initScrollCache();
3737 mScrollCache.fadingEdgeLength = length;
3738 }
3739
3740 /**
3741 * Returns the size of the horizontal faded edges used to indicate that more
3742 * content in this view is visible.
3743 *
3744 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3745 * faded edges are not enabled for this view.
3746 * @attr ref android.R.styleable#View_fadingEdgeLength
3747 */
3748 public int getHorizontalFadingEdgeLength() {
3749 if (isHorizontalFadingEdgeEnabled()) {
3750 ScrollabilityCache cache = mScrollCache;
3751 if (cache != null) {
3752 return cache.fadingEdgeLength;
3753 }
3754 }
3755 return 0;
3756 }
3757
3758 /**
3759 * Returns the width of the vertical scrollbar.
3760 *
3761 * @return The width in pixels of the vertical scrollbar or 0 if there
3762 * is no vertical scrollbar.
3763 */
3764 public int getVerticalScrollbarWidth() {
3765 ScrollabilityCache cache = mScrollCache;
3766 if (cache != null) {
3767 ScrollBarDrawable scrollBar = cache.scrollBar;
3768 if (scrollBar != null) {
3769 int size = scrollBar.getSize(true);
3770 if (size <= 0) {
3771 size = cache.scrollBarSize;
3772 }
3773 return size;
3774 }
3775 return 0;
3776 }
3777 return 0;
3778 }
3779
3780 /**
3781 * Returns the height of the horizontal scrollbar.
3782 *
3783 * @return The height in pixels of the horizontal scrollbar or 0 if
3784 * there is no horizontal scrollbar.
3785 */
3786 protected int getHorizontalScrollbarHeight() {
3787 ScrollabilityCache cache = mScrollCache;
3788 if (cache != null) {
3789 ScrollBarDrawable scrollBar = cache.scrollBar;
3790 if (scrollBar != null) {
3791 int size = scrollBar.getSize(false);
3792 if (size <= 0) {
3793 size = cache.scrollBarSize;
3794 }
3795 return size;
3796 }
3797 return 0;
3798 }
3799 return 0;
3800 }
3801
3802 /**
3803 * <p>
3804 * Initializes the scrollbars from a given set of styled attributes. This
3805 * method should be called by subclasses that need scrollbars and when an
3806 * instance of these subclasses is created programmatically rather than
3807 * being inflated from XML. This method is automatically called when the XML
3808 * is inflated.
3809 * </p>
3810 *
3811 * @param a the styled attributes set to initialize the scrollbars from
3812 */
3813 protected void initializeScrollbars(TypedArray a) {
3814 initScrollCache();
3815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003817
Mike Cleronf116bf82009-09-27 19:14:12 -07003818 if (scrollabilityCache.scrollBar == null) {
3819 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3820 }
Joe Malin32736f02011-01-19 16:14:20 -08003821
Romain Guy8bda2482010-03-02 11:42:11 -08003822 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823
Mike Cleronf116bf82009-09-27 19:14:12 -07003824 if (!fadeScrollbars) {
3825 scrollabilityCache.state = ScrollabilityCache.ON;
3826 }
3827 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003828
3829
Mike Cleronf116bf82009-09-27 19:14:12 -07003830 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3831 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3832 .getScrollBarFadeDuration());
3833 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3834 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3835 ViewConfiguration.getScrollDefaultDelay());
3836
Joe Malin32736f02011-01-19 16:14:20 -08003837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3839 com.android.internal.R.styleable.View_scrollbarSize,
3840 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3841
3842 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3843 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3844
3845 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3846 if (thumb != null) {
3847 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3848 }
3849
3850 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3851 false);
3852 if (alwaysDraw) {
3853 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3854 }
3855
3856 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3857 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3858
3859 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3860 if (thumb != null) {
3861 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3862 }
3863
3864 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3865 false);
3866 if (alwaysDraw) {
3867 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3868 }
3869
3870 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003871 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 }
3873
3874 /**
3875 * <p>
3876 * Initalizes the scrollability cache if necessary.
3877 * </p>
3878 */
3879 private void initScrollCache() {
3880 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003881 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
3883 }
3884
Philip Milne6c8ea062012-04-03 17:38:43 -07003885 private ScrollabilityCache getScrollCache() {
3886 initScrollCache();
3887 return mScrollCache;
3888 }
3889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 /**
Adam Powell20232d02010-12-08 21:08:53 -08003891 * Set the position of the vertical scroll bar. Should be one of
3892 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3893 * {@link #SCROLLBAR_POSITION_RIGHT}.
3894 *
3895 * @param position Where the vertical scroll bar should be positioned.
3896 */
3897 public void setVerticalScrollbarPosition(int position) {
3898 if (mVerticalScrollbarPosition != position) {
3899 mVerticalScrollbarPosition = position;
3900 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003901 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003902 }
3903 }
3904
3905 /**
3906 * @return The position where the vertical scroll bar will show, if applicable.
3907 * @see #setVerticalScrollbarPosition(int)
3908 */
3909 public int getVerticalScrollbarPosition() {
3910 return mVerticalScrollbarPosition;
3911 }
3912
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003913 ListenerInfo getListenerInfo() {
3914 if (mListenerInfo != null) {
3915 return mListenerInfo;
3916 }
3917 mListenerInfo = new ListenerInfo();
3918 return mListenerInfo;
3919 }
3920
Adam Powell20232d02010-12-08 21:08:53 -08003921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 * Register a callback to be invoked when focus of this view changed.
3923 *
3924 * @param l The callback that will run.
3925 */
3926 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003927 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929
3930 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003931 * Add a listener that will be called when the bounds of the view change due to
3932 * layout processing.
3933 *
3934 * @param listener The listener that will be called when layout bounds change.
3935 */
3936 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003937 ListenerInfo li = getListenerInfo();
3938 if (li.mOnLayoutChangeListeners == null) {
3939 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003940 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003941 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3942 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003943 }
Chet Haase21cd1382010-09-01 17:42:29 -07003944 }
3945
3946 /**
3947 * Remove a listener for layout changes.
3948 *
3949 * @param listener The listener for layout bounds change.
3950 */
3951 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 ListenerInfo li = mListenerInfo;
3953 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003954 return;
3955 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003956 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003957 }
3958
3959 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003960 * Add a listener for attach state changes.
3961 *
3962 * This listener will be called whenever this view is attached or detached
3963 * from a window. Remove the listener using
3964 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3965 *
3966 * @param listener Listener to attach
3967 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3968 */
3969 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003970 ListenerInfo li = getListenerInfo();
3971 if (li.mOnAttachStateChangeListeners == null) {
3972 li.mOnAttachStateChangeListeners
3973 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003974 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003975 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003976 }
3977
3978 /**
3979 * Remove a listener for attach state changes. The listener will receive no further
3980 * notification of window attach/detach events.
3981 *
3982 * @param listener Listener to remove
3983 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3984 */
3985 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003986 ListenerInfo li = mListenerInfo;
3987 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003988 return;
3989 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003990 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003991 }
3992
3993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 * Returns the focus-change callback registered for this view.
3995 *
3996 * @return The callback, or null if one is not registered.
3997 */
3998 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003999 ListenerInfo li = mListenerInfo;
4000 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 }
4002
4003 /**
4004 * Register a callback to be invoked when this view is clicked. If this view is not
4005 * clickable, it becomes clickable.
4006 *
4007 * @param l The callback that will run
4008 *
4009 * @see #setClickable(boolean)
4010 */
4011 public void setOnClickListener(OnClickListener l) {
4012 if (!isClickable()) {
4013 setClickable(true);
4014 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004015 getListenerInfo().mOnClickListener = l;
4016 }
4017
4018 /**
4019 * Return whether this view has an attached OnClickListener. Returns
4020 * true if there is a listener, false if there is none.
4021 */
4022 public boolean hasOnClickListeners() {
4023 ListenerInfo li = mListenerInfo;
4024 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
4026
4027 /**
4028 * Register a callback to be invoked when this view is clicked and held. If this view is not
4029 * long clickable, it becomes long clickable.
4030 *
4031 * @param l The callback that will run
4032 *
4033 * @see #setLongClickable(boolean)
4034 */
4035 public void setOnLongClickListener(OnLongClickListener l) {
4036 if (!isLongClickable()) {
4037 setLongClickable(true);
4038 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004039 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
4041
4042 /**
4043 * Register a callback to be invoked when the context menu for this view is
4044 * being built. If this view is not long clickable, it becomes long clickable.
4045 *
4046 * @param l The callback that will run
4047 *
4048 */
4049 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4050 if (!isLongClickable()) {
4051 setLongClickable(true);
4052 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004053 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
4055
4056 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004057 * Call this view's OnClickListener, if it is defined. Performs all normal
4058 * actions associated with clicking: reporting accessibility event, playing
4059 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 *
4061 * @return True there was an assigned OnClickListener that was called, false
4062 * otherwise is returned.
4063 */
4064 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004065 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4066
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004067 ListenerInfo li = mListenerInfo;
4068 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004070 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 return true;
4072 }
4073
4074 return false;
4075 }
4076
4077 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004078 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4079 * this only calls the listener, and does not do any associated clicking
4080 * actions like reporting an accessibility event.
4081 *
4082 * @return True there was an assigned OnClickListener that was called, false
4083 * otherwise is returned.
4084 */
4085 public boolean callOnClick() {
4086 ListenerInfo li = mListenerInfo;
4087 if (li != null && li.mOnClickListener != null) {
4088 li.mOnClickListener.onClick(this);
4089 return true;
4090 }
4091 return false;
4092 }
4093
4094 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004095 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4096 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004098 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 */
4100 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004101 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004104 ListenerInfo li = mListenerInfo;
4105 if (li != null && li.mOnLongClickListener != null) {
4106 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 }
4108 if (!handled) {
4109 handled = showContextMenu();
4110 }
4111 if (handled) {
4112 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4113 }
4114 return handled;
4115 }
4116
4117 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004118 * Performs button-related actions during a touch down event.
4119 *
4120 * @param event The event.
4121 * @return True if the down was consumed.
4122 *
4123 * @hide
4124 */
4125 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4126 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4127 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4128 return true;
4129 }
4130 }
4131 return false;
4132 }
4133
4134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 * Bring up the context menu for this view.
4136 *
4137 * @return Whether a context menu was displayed.
4138 */
4139 public boolean showContextMenu() {
4140 return getParent().showContextMenuForChild(this);
4141 }
4142
4143 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004144 * Bring up the context menu for this view, referring to the item under the specified point.
4145 *
4146 * @param x The referenced x coordinate.
4147 * @param y The referenced y coordinate.
4148 * @param metaState The keyboard modifiers that were pressed.
4149 * @return Whether a context menu was displayed.
4150 *
4151 * @hide
4152 */
4153 public boolean showContextMenu(float x, float y, int metaState) {
4154 return showContextMenu();
4155 }
4156
4157 /**
Adam Powell6e346362010-07-23 10:18:23 -07004158 * Start an action mode.
4159 *
4160 * @param callback Callback that will control the lifecycle of the action mode
4161 * @return The new action mode if it is started, null otherwise
4162 *
4163 * @see ActionMode
4164 */
4165 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004166 ViewParent parent = getParent();
4167 if (parent == null) return null;
4168 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004169 }
4170
4171 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004172 * Register a callback to be invoked when a hardware key is pressed in this view.
4173 * Key presses in software input methods will generally not trigger the methods of
4174 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 * @param l the key listener to attach to this view
4176 */
4177 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004178 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 }
4180
4181 /**
4182 * Register a callback to be invoked when a touch event is sent to this view.
4183 * @param l the touch listener to attach to this view
4184 */
4185 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004186 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188
4189 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004190 * Register a callback to be invoked when a generic motion event is sent to this view.
4191 * @param l the generic motion listener to attach to this view
4192 */
4193 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004194 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004195 }
4196
4197 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004198 * Register a callback to be invoked when a hover event is sent to this view.
4199 * @param l the hover listener to attach to this view
4200 */
4201 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004202 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004203 }
4204
4205 /**
Joe Malin32736f02011-01-19 16:14:20 -08004206 * Register a drag event listener callback object for this View. The parameter is
4207 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4208 * View, the system calls the
4209 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4210 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004211 */
4212 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004213 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004214 }
4215
4216 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004217 * Give this view focus. This will cause
4218 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 *
4220 * Note: this does not check whether this {@link View} should get focus, it just
4221 * gives it focus no matter what. It should only be called internally by framework
4222 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4223 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004224 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4225 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 * focus moved when requestFocus() is called. It may not always
4227 * apply, in which case use the default View.FOCUS_DOWN.
4228 * @param previouslyFocusedRect The rectangle of the view that had focus
4229 * prior in this View's coordinate system.
4230 */
4231 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4232 if (DBG) {
4233 System.out.println(this + " requestFocus()");
4234 }
4235
4236 if ((mPrivateFlags & FOCUSED) == 0) {
4237 mPrivateFlags |= FOCUSED;
4238
4239 if (mParent != null) {
4240 mParent.requestChildFocus(this, this);
4241 }
4242
4243 onFocusChanged(true, direction, previouslyFocusedRect);
4244 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004245
4246 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4247 notifyAccessibilityStateChanged();
4248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 }
4250 }
4251
4252 /**
4253 * Request that a rectangle of this view be visible on the screen,
4254 * scrolling if necessary just enough.
4255 *
4256 * <p>A View should call this if it maintains some notion of which part
4257 * of its content is interesting. For example, a text editing view
4258 * should call this when its cursor moves.
4259 *
4260 * @param rectangle The rectangle.
4261 * @return Whether any parent scrolled.
4262 */
4263 public boolean requestRectangleOnScreen(Rect rectangle) {
4264 return requestRectangleOnScreen(rectangle, false);
4265 }
4266
4267 /**
4268 * Request that a rectangle of this view be visible on the screen,
4269 * scrolling if necessary just enough.
4270 *
4271 * <p>A View should call this if it maintains some notion of which part
4272 * of its content is interesting. For example, a text editing view
4273 * should call this when its cursor moves.
4274 *
4275 * <p>When <code>immediate</code> is set to true, scrolling will not be
4276 * animated.
4277 *
4278 * @param rectangle The rectangle.
4279 * @param immediate True to forbid animated scrolling, false otherwise
4280 * @return Whether any parent scrolled.
4281 */
4282 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4283 View child = this;
4284 ViewParent parent = mParent;
4285 boolean scrolled = false;
4286 while (parent != null) {
4287 scrolled |= parent.requestChildRectangleOnScreen(child,
4288 rectangle, immediate);
4289
4290 // offset rect so next call has the rectangle in the
4291 // coordinate system of its direct child.
4292 rectangle.offset(child.getLeft(), child.getTop());
4293 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4294
4295 if (!(parent instanceof View)) {
4296 break;
4297 }
Romain Guy8506ab42009-06-11 17:35:47 -07004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 child = (View) parent;
4300 parent = child.getParent();
4301 }
4302 return scrolled;
4303 }
4304
4305 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004306 * Called when this view wants to give up focus. If focus is cleared
4307 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4308 * <p>
4309 * <strong>Note:</strong> When a View clears focus the framework is trying
4310 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004311 * View is the first from the top that can take focus, then all callbacks
4312 * related to clearing focus will be invoked after wich the framework will
4313 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004314 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 */
4316 public void clearFocus() {
4317 if (DBG) {
4318 System.out.println(this + " clearFocus()");
4319 }
4320
4321 if ((mPrivateFlags & FOCUSED) != 0) {
4322 mPrivateFlags &= ~FOCUSED;
4323
4324 if (mParent != null) {
4325 mParent.clearChildFocus(this);
4326 }
4327
4328 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004331
4332 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004333
4334 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4335 notifyAccessibilityStateChanged();
4336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 }
4338 }
4339
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004340 void ensureInputFocusOnFirstFocusable() {
4341 View root = getRootView();
4342 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004343 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 }
4345 }
4346
4347 /**
4348 * Called internally by the view system when a new view is getting focus.
4349 * This is what clears the old focus.
4350 */
4351 void unFocus() {
4352 if (DBG) {
4353 System.out.println(this + " unFocus()");
4354 }
4355
4356 if ((mPrivateFlags & FOCUSED) != 0) {
4357 mPrivateFlags &= ~FOCUSED;
4358
4359 onFocusChanged(false, 0, null);
4360 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004361
4362 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4363 notifyAccessibilityStateChanged();
4364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
4366 }
4367
4368 /**
4369 * Returns true if this view has focus iteself, or is the ancestor of the
4370 * view that has focus.
4371 *
4372 * @return True if this view has or contains focus, false otherwise.
4373 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004374 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 public boolean hasFocus() {
4376 return (mPrivateFlags & FOCUSED) != 0;
4377 }
4378
4379 /**
4380 * Returns true if this view is focusable or if it contains a reachable View
4381 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4382 * is a View whose parents do not block descendants focus.
4383 *
4384 * Only {@link #VISIBLE} views are considered focusable.
4385 *
4386 * @return True if the view is focusable or if the view contains a focusable
4387 * View, false otherwise.
4388 *
4389 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4390 */
4391 public boolean hasFocusable() {
4392 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4393 }
4394
4395 /**
4396 * Called by the view system when the focus state of this view changes.
4397 * When the focus change event is caused by directional navigation, direction
4398 * and previouslyFocusedRect provide insight into where the focus is coming from.
4399 * When overriding, be sure to call up through to the super class so that
4400 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 * @param gainFocus True if the View has focus; false otherwise.
4403 * @param direction The direction focus has moved when requestFocus()
4404 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004405 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4406 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4407 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4409 * system, of the previously focused view. If applicable, this will be
4410 * passed in as finer grained information about where the focus is coming
4411 * from (in addition to direction). Will be <code>null</code> otherwise.
4412 */
4413 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004414 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004415 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4416 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004417 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004418 }
4419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 InputMethodManager imm = InputMethodManager.peekInstance();
4421 if (!gainFocus) {
4422 if (isPressed()) {
4423 setPressed(false);
4424 }
4425 if (imm != null && mAttachInfo != null
4426 && mAttachInfo.mHasWindowFocus) {
4427 imm.focusOut(this);
4428 }
Romain Guya2431d02009-04-30 16:30:00 -07004429 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 } else if (imm != null && mAttachInfo != null
4431 && mAttachInfo.mHasWindowFocus) {
4432 imm.focusIn(this);
4433 }
Romain Guy8506ab42009-06-11 17:35:47 -07004434
Romain Guy0fd89bf2011-01-26 15:41:30 -08004435 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004436 ListenerInfo li = mListenerInfo;
4437 if (li != null && li.mOnFocusChangeListener != null) {
4438 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 }
Joe Malin32736f02011-01-19 16:14:20 -08004440
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004441 if (mAttachInfo != null) {
4442 mAttachInfo.mKeyDispatchState.reset(this);
4443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 }
4445
4446 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004447 * Sends an accessibility event of the given type. If accessiiblity is
4448 * not enabled this method has no effect. The default implementation calls
4449 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4450 * to populate information about the event source (this View), then calls
4451 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4452 * populate the text content of the event source including its descendants,
4453 * and last calls
4454 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4455 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004456 * <p>
4457 * If an {@link AccessibilityDelegate} has been specified via calling
4458 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4459 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4460 * responsible for handling this call.
4461 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004462 *
Scott Mainb303d832011-10-12 16:45:18 -07004463 * @param eventType The type of the event to send, as defined by several types from
4464 * {@link android.view.accessibility.AccessibilityEvent}, such as
4465 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4466 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004467 *
4468 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4469 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4470 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004471 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004472 */
4473 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004474 if (mAccessibilityDelegate != null) {
4475 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4476 } else {
4477 sendAccessibilityEventInternal(eventType);
4478 }
4479 }
4480
4481 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004482 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4483 * {@link AccessibilityEvent} to make an announcement which is related to some
4484 * sort of a context change for which none of the events representing UI transitions
4485 * is a good fit. For example, announcing a new page in a book. If accessibility
4486 * is not enabled this method does nothing.
4487 *
4488 * @param text The announcement text.
4489 */
4490 public void announceForAccessibility(CharSequence text) {
4491 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4492 AccessibilityEvent event = AccessibilityEvent.obtain(
4493 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4494 event.getText().add(text);
4495 sendAccessibilityEventUnchecked(event);
4496 }
4497 }
4498
4499 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004500 * @see #sendAccessibilityEvent(int)
4501 *
4502 * Note: Called from the default {@link AccessibilityDelegate}.
4503 */
4504 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004505 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4506 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4507 }
4508 }
4509
4510 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004511 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4512 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004513 * perform a check whether accessibility is enabled.
4514 * <p>
4515 * If an {@link AccessibilityDelegate} has been specified via calling
4516 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4517 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4518 * is responsible for handling this call.
4519 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004520 *
4521 * @param event The event to send.
4522 *
4523 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004524 */
4525 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004526 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004527 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004528 } else {
4529 sendAccessibilityEventUncheckedInternal(event);
4530 }
4531 }
4532
4533 /**
4534 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4535 *
4536 * Note: Called from the default {@link AccessibilityDelegate}.
4537 */
4538 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004539 if (!isShown()) {
4540 return;
4541 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004542 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004543 // Only a subset of accessibility events populates text content.
4544 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4545 dispatchPopulateAccessibilityEvent(event);
4546 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004547 // Intercept accessibility focus events fired by virtual nodes to keep
4548 // track of accessibility focus position in such nodes.
4549 final int eventType = event.getEventType();
4550 switch (eventType) {
4551 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4552 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4553 event.getSourceNodeId());
4554 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4555 ViewRootImpl viewRootImpl = getViewRootImpl();
4556 if (viewRootImpl != null) {
4557 viewRootImpl.setAccessibilityFocusedHost(this);
4558 }
4559 }
4560 } break;
4561 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4562 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4563 event.getSourceNodeId());
4564 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4565 ViewRootImpl viewRootImpl = getViewRootImpl();
4566 if (viewRootImpl != null) {
4567 viewRootImpl.setAccessibilityFocusedHost(null);
4568 }
4569 }
4570 } break;
4571 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004572 // In the beginning we called #isShown(), so we know that getParent() is not null.
4573 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004574 }
4575
4576 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004577 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4578 * to its children for adding their text content to the event. Note that the
4579 * event text is populated in a separate dispatch path since we add to the
4580 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004581 * A typical implementation will call
4582 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4583 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4584 * on each child. Override this method if custom population of the event text
4585 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004586 * <p>
4587 * If an {@link AccessibilityDelegate} has been specified via calling
4588 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4589 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4590 * is responsible for handling this call.
4591 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004592 * <p>
4593 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4594 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4595 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004596 *
4597 * @param event The event.
4598 *
4599 * @return True if the event population was completed.
4600 */
4601 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004602 if (mAccessibilityDelegate != null) {
4603 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4604 } else {
4605 return dispatchPopulateAccessibilityEventInternal(event);
4606 }
4607 }
4608
4609 /**
4610 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4611 *
4612 * Note: Called from the default {@link AccessibilityDelegate}.
4613 */
4614 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004615 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004616 return false;
4617 }
4618
4619 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004620 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004621 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004622 * text content. While this method is free to modify event
4623 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004624 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4625 * <p>
4626 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004627 * to the text added by the super implementation:
4628 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004629 * super.onPopulateAccessibilityEvent(event);
4630 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4631 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4632 * mCurrentDate.getTimeInMillis(), flags);
4633 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004634 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004635 * <p>
4636 * If an {@link AccessibilityDelegate} has been specified via calling
4637 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4638 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4639 * is responsible for handling this call.
4640 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004641 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4642 * information to the event, in case the default implementation has basic information to add.
4643 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004644 *
4645 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004646 *
4647 * @see #sendAccessibilityEvent(int)
4648 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004649 */
4650 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004651 if (mAccessibilityDelegate != null) {
4652 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4653 } else {
4654 onPopulateAccessibilityEventInternal(event);
4655 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004656 }
4657
4658 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004659 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4660 *
4661 * Note: Called from the default {@link AccessibilityDelegate}.
4662 */
4663 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4664
4665 }
4666
4667 /**
4668 * Initializes an {@link AccessibilityEvent} with information about
4669 * this View which is the event source. In other words, the source of
4670 * an accessibility event is the view whose state change triggered firing
4671 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004672 * <p>
4673 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004674 * to properties set by the super implementation:
4675 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4676 * super.onInitializeAccessibilityEvent(event);
4677 * event.setPassword(true);
4678 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004679 * <p>
4680 * If an {@link AccessibilityDelegate} has been specified via calling
4681 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4682 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4683 * is responsible for handling this call.
4684 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004685 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4686 * information to the event, in case the default implementation has basic information to add.
4687 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004688 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004689 *
4690 * @see #sendAccessibilityEvent(int)
4691 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4692 */
4693 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004694 if (mAccessibilityDelegate != null) {
4695 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4696 } else {
4697 onInitializeAccessibilityEventInternal(event);
4698 }
4699 }
4700
4701 /**
4702 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4703 *
4704 * Note: Called from the default {@link AccessibilityDelegate}.
4705 */
4706 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004707 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004708 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004709 event.setPackageName(getContext().getPackageName());
4710 event.setEnabled(isEnabled());
4711 event.setContentDescription(mContentDescription);
4712
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004713 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004714 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004715 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4716 FOCUSABLES_ALL);
4717 event.setItemCount(focusablesTempList.size());
4718 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4719 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004720 }
4721 }
4722
4723 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004724 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4725 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4726 * This method is responsible for obtaining an accessibility node info from a
4727 * pool of reusable instances and calling
4728 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4729 * initialize the former.
4730 * <p>
4731 * Note: The client is responsible for recycling the obtained instance by calling
4732 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4733 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004734 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004735 * @return A populated {@link AccessibilityNodeInfo}.
4736 *
4737 * @see AccessibilityNodeInfo
4738 */
4739 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004740 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4741 if (provider != null) {
4742 return provider.createAccessibilityNodeInfo(View.NO_ID);
4743 } else {
4744 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4745 onInitializeAccessibilityNodeInfo(info);
4746 return info;
4747 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004748 }
4749
4750 /**
4751 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4752 * The base implementation sets:
4753 * <ul>
4754 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004755 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4756 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004757 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4758 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4759 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4760 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4761 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4762 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4763 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4764 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4765 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4766 * </ul>
4767 * <p>
4768 * Subclasses should override this method, call the super implementation,
4769 * and set additional attributes.
4770 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004771 * <p>
4772 * If an {@link AccessibilityDelegate} has been specified via calling
4773 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4774 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4775 * is responsible for handling this call.
4776 * </p>
4777 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004778 * @param info The instance to initialize.
4779 */
4780 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004781 if (mAccessibilityDelegate != null) {
4782 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4783 } else {
4784 onInitializeAccessibilityNodeInfoInternal(info);
4785 }
4786 }
4787
4788 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004789 * Gets the location of this view in screen coordintates.
4790 *
4791 * @param outRect The output location
4792 */
4793 private void getBoundsOnScreen(Rect outRect) {
4794 if (mAttachInfo == null) {
4795 return;
4796 }
4797
4798 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004799 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004800
4801 if (!hasIdentityMatrix()) {
4802 getMatrix().mapRect(position);
4803 }
4804
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004805 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004806
4807 ViewParent parent = mParent;
4808 while (parent instanceof View) {
4809 View parentView = (View) parent;
4810
4811 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4812
4813 if (!parentView.hasIdentityMatrix()) {
4814 parentView.getMatrix().mapRect(position);
4815 }
4816
4817 position.offset(parentView.mLeft, parentView.mTop);
4818
4819 parent = parentView.mParent;
4820 }
4821
4822 if (parent instanceof ViewRootImpl) {
4823 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4824 position.offset(0, -viewRootImpl.mCurScrollY);
4825 }
4826
4827 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4828
4829 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4830 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4831 }
4832
4833 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004834 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4835 *
4836 * Note: Called from the default {@link AccessibilityDelegate}.
4837 */
4838 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004839 Rect bounds = mAttachInfo.mTmpInvalRect;
4840 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004841 info.setBoundsInParent(bounds);
4842
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004843 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004844 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004845
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004846 ViewParent parent = getParentForAccessibility();
4847 if (parent instanceof View) {
4848 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004849 }
4850
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004851 info.setVisibleToUser(isVisibleToUser());
4852
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004853 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004854 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004855 info.setContentDescription(getContentDescription());
4856
4857 info.setEnabled(isEnabled());
4858 info.setClickable(isClickable());
4859 info.setFocusable(isFocusable());
4860 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004861 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004862 info.setSelected(isSelected());
4863 info.setLongClickable(isLongClickable());
4864
4865 // TODO: These make sense only if we are in an AdapterView but all
4866 // views can be selected. Maybe from accessiiblity perspective
4867 // we should report as selectable view in an AdapterView.
4868 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4869 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4870
4871 if (isFocusable()) {
4872 if (isFocused()) {
4873 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4874 } else {
4875 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4876 }
4877 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004878
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004879 if (!isAccessibilityFocused()) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07004880 final int mode = getAccessibilityFocusable();
4881 if (mode == ACCESSIBILITY_FOCUSABLE_YES || mode == ACCESSIBILITY_FOCUSABLE_AUTO) {
4882 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4883 }
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004884 } else {
4885 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4886 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004887
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004888 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004889 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4890 }
4891
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004892 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004893 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4894 }
4895
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004896 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004897 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4898 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4899 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004900 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4901 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004902 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004903 }
4904
4905 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -07004906 * Returns the delta between the actual and last reported window left.
4907 *
4908 * @hide
4909 */
4910 public int getActualAndReportedWindowLeftDelta() {
4911 if (mAttachInfo != null) {
4912 return mAttachInfo.mActualWindowLeft - mAttachInfo.mWindowLeft;
4913 }
4914 return 0;
4915 }
4916
4917 /**
4918 * Returns the delta between the actual and last reported window top.
4919 *
4920 * @hide
4921 */
4922 public int getActualAndReportedWindowTopDelta() {
4923 if (mAttachInfo != null) {
4924 return mAttachInfo.mActualWindowTop - mAttachInfo.mWindowTop;
4925 }
4926 return 0;
4927 }
4928
4929 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004930 * Computes whether this view is visible to the user. Such a view is
4931 * attached, visible, all its predecessors are visible, it is not clipped
4932 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004933 *
4934 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004935 *
4936 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004937 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004938 protected boolean isVisibleToUser() {
4939 return isVisibleToUser(null);
4940 }
4941
4942 /**
4943 * Computes whether the given portion of this view is visible to the user. Such a view is
4944 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4945 * the specified portion is not clipped entirely by its predecessors.
4946 *
4947 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4948 * <code>null</code>, and the entire view will be tested in this case.
4949 * When <code>true</code> is returned by the function, the actual visible
4950 * region will be stored in this parameter; that is, if boundInView is fully
4951 * contained within the view, no modification will be made, otherwise regions
4952 * outside of the visible area of the view will be clipped.
4953 *
4954 * @return Whether the specified portion of the view is visible on the screen.
4955 *
4956 * @hide
4957 */
4958 protected boolean isVisibleToUser(Rect boundInView) {
4959 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4960 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004961 // The first two checks are made also made by isShown() which
4962 // however traverses the tree up to the parent to catch that.
4963 // Therefore, we do some fail fast check to minimize the up
4964 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004965 boolean isVisible = mAttachInfo != null
4966 && mAttachInfo.mWindowVisibility == View.VISIBLE
4967 && getAlpha() > 0
4968 && isShown()
4969 && getGlobalVisibleRect(visibleRect, offset);
4970 if (isVisible && boundInView != null) {
4971 visibleRect.offset(-offset.x, -offset.y);
4972 isVisible &= boundInView.intersect(visibleRect);
4973 }
4974 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004975 }
4976
4977 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004978 * Sets a delegate for implementing accessibility support via compositon as
4979 * opposed to inheritance. The delegate's primary use is for implementing
4980 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4981 *
4982 * @param delegate The delegate instance.
4983 *
4984 * @see AccessibilityDelegate
4985 */
4986 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4987 mAccessibilityDelegate = delegate;
4988 }
4989
4990 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004991 * Gets the provider for managing a virtual view hierarchy rooted at this View
4992 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4993 * that explore the window content.
4994 * <p>
4995 * If this method returns an instance, this instance is responsible for managing
4996 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4997 * View including the one representing the View itself. Similarly the returned
4998 * instance is responsible for performing accessibility actions on any virtual
4999 * view or the root view itself.
5000 * </p>
5001 * <p>
5002 * If an {@link AccessibilityDelegate} has been specified via calling
5003 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5004 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5005 * is responsible for handling this call.
5006 * </p>
5007 *
5008 * @return The provider.
5009 *
5010 * @see AccessibilityNodeProvider
5011 */
5012 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5013 if (mAccessibilityDelegate != null) {
5014 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5015 } else {
5016 return null;
5017 }
5018 }
5019
5020 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005021 * Gets the unique identifier of this view on the screen for accessibility purposes.
5022 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5023 *
5024 * @return The view accessibility id.
5025 *
5026 * @hide
5027 */
5028 public int getAccessibilityViewId() {
5029 if (mAccessibilityViewId == NO_ID) {
5030 mAccessibilityViewId = sNextAccessibilityViewId++;
5031 }
5032 return mAccessibilityViewId;
5033 }
5034
5035 /**
5036 * Gets the unique identifier of the window in which this View reseides.
5037 *
5038 * @return The window accessibility id.
5039 *
5040 * @hide
5041 */
5042 public int getAccessibilityWindowId() {
5043 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5044 }
5045
5046 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005047 * Gets the {@link View} description. It briefly describes the view and is
5048 * primarily used for accessibility support. Set this property to enable
5049 * better accessibility support for your application. This is especially
5050 * true for views that do not have textual representation (For example,
5051 * ImageButton).
5052 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005053 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005054 *
5055 * @attr ref android.R.styleable#View_contentDescription
5056 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005057 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005058 public CharSequence getContentDescription() {
5059 return mContentDescription;
5060 }
5061
5062 /**
5063 * Sets the {@link View} description. It briefly describes the view and is
5064 * primarily used for accessibility support. Set this property to enable
5065 * better accessibility support for your application. This is especially
5066 * true for views that do not have textual representation (For example,
5067 * ImageButton).
5068 *
5069 * @param contentDescription The content description.
5070 *
5071 * @attr ref android.R.styleable#View_contentDescription
5072 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005073 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005074 public void setContentDescription(CharSequence contentDescription) {
5075 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005076 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5077 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5078 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5079 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005080 }
5081
5082 /**
Romain Guya2431d02009-04-30 16:30:00 -07005083 * Invoked whenever this view loses focus, either by losing window focus or by losing
5084 * focus within its window. This method can be used to clear any state tied to the
5085 * focus. For instance, if a button is held pressed with the trackball and the window
5086 * loses focus, this method can be used to cancel the press.
5087 *
5088 * Subclasses of View overriding this method should always call super.onFocusLost().
5089 *
5090 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005091 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005092 *
5093 * @hide pending API council approval
5094 */
5095 protected void onFocusLost() {
5096 resetPressedState();
5097 }
5098
5099 private void resetPressedState() {
5100 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5101 return;
5102 }
5103
5104 if (isPressed()) {
5105 setPressed(false);
5106
5107 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005108 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005109 }
5110 }
5111 }
5112
5113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 * Returns true if this view has focus
5115 *
5116 * @return True if this view has focus, false otherwise.
5117 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005118 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 public boolean isFocused() {
5120 return (mPrivateFlags & FOCUSED) != 0;
5121 }
5122
5123 /**
5124 * Find the view in the hierarchy rooted at this view that currently has
5125 * focus.
5126 *
5127 * @return The view that currently has focus, or null if no focused view can
5128 * be found.
5129 */
5130 public View findFocus() {
5131 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5132 }
5133
5134 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005135 * Indicates whether this view is one of the set of scrollable containers in
5136 * its window.
5137 *
5138 * @return whether this view is one of the set of scrollable containers in
5139 * its window
5140 *
5141 * @attr ref android.R.styleable#View_isScrollContainer
5142 */
5143 public boolean isScrollContainer() {
5144 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5145 }
5146
5147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 * Change whether this view is one of the set of scrollable containers in
5149 * its window. This will be used to determine whether the window can
5150 * resize or must pan when a soft input area is open -- scrollable
5151 * containers allow the window to use resize mode since the container
5152 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005153 *
5154 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 */
5156 public void setScrollContainer(boolean isScrollContainer) {
5157 if (isScrollContainer) {
5158 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5159 mAttachInfo.mScrollContainers.add(this);
5160 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5161 }
5162 mPrivateFlags |= SCROLL_CONTAINER;
5163 } else {
5164 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5165 mAttachInfo.mScrollContainers.remove(this);
5166 }
5167 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5168 }
5169 }
5170
5171 /**
5172 * Returns the quality of the drawing cache.
5173 *
5174 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5175 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5176 *
5177 * @see #setDrawingCacheQuality(int)
5178 * @see #setDrawingCacheEnabled(boolean)
5179 * @see #isDrawingCacheEnabled()
5180 *
5181 * @attr ref android.R.styleable#View_drawingCacheQuality
5182 */
5183 public int getDrawingCacheQuality() {
5184 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5185 }
5186
5187 /**
5188 * Set the drawing cache quality of this view. This value is used only when the
5189 * drawing cache is enabled
5190 *
5191 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5192 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5193 *
5194 * @see #getDrawingCacheQuality()
5195 * @see #setDrawingCacheEnabled(boolean)
5196 * @see #isDrawingCacheEnabled()
5197 *
5198 * @attr ref android.R.styleable#View_drawingCacheQuality
5199 */
5200 public void setDrawingCacheQuality(int quality) {
5201 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5202 }
5203
5204 /**
5205 * Returns whether the screen should remain on, corresponding to the current
5206 * value of {@link #KEEP_SCREEN_ON}.
5207 *
5208 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5209 *
5210 * @see #setKeepScreenOn(boolean)
5211 *
5212 * @attr ref android.R.styleable#View_keepScreenOn
5213 */
5214 public boolean getKeepScreenOn() {
5215 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5216 }
5217
5218 /**
5219 * Controls whether the screen should remain on, modifying the
5220 * value of {@link #KEEP_SCREEN_ON}.
5221 *
5222 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5223 *
5224 * @see #getKeepScreenOn()
5225 *
5226 * @attr ref android.R.styleable#View_keepScreenOn
5227 */
5228 public void setKeepScreenOn(boolean keepScreenOn) {
5229 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5230 }
5231
5232 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005233 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5234 * @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 -08005235 *
5236 * @attr ref android.R.styleable#View_nextFocusLeft
5237 */
5238 public int getNextFocusLeftId() {
5239 return mNextFocusLeftId;
5240 }
5241
5242 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005243 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5244 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5245 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 *
5247 * @attr ref android.R.styleable#View_nextFocusLeft
5248 */
5249 public void setNextFocusLeftId(int nextFocusLeftId) {
5250 mNextFocusLeftId = nextFocusLeftId;
5251 }
5252
5253 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005254 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5255 * @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 -08005256 *
5257 * @attr ref android.R.styleable#View_nextFocusRight
5258 */
5259 public int getNextFocusRightId() {
5260 return mNextFocusRightId;
5261 }
5262
5263 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005264 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5265 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5266 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 *
5268 * @attr ref android.R.styleable#View_nextFocusRight
5269 */
5270 public void setNextFocusRightId(int nextFocusRightId) {
5271 mNextFocusRightId = nextFocusRightId;
5272 }
5273
5274 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005275 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5276 * @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 -08005277 *
5278 * @attr ref android.R.styleable#View_nextFocusUp
5279 */
5280 public int getNextFocusUpId() {
5281 return mNextFocusUpId;
5282 }
5283
5284 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005285 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5286 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5287 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 *
5289 * @attr ref android.R.styleable#View_nextFocusUp
5290 */
5291 public void setNextFocusUpId(int nextFocusUpId) {
5292 mNextFocusUpId = nextFocusUpId;
5293 }
5294
5295 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005296 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5297 * @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 -08005298 *
5299 * @attr ref android.R.styleable#View_nextFocusDown
5300 */
5301 public int getNextFocusDownId() {
5302 return mNextFocusDownId;
5303 }
5304
5305 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005306 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5307 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5308 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 *
5310 * @attr ref android.R.styleable#View_nextFocusDown
5311 */
5312 public void setNextFocusDownId(int nextFocusDownId) {
5313 mNextFocusDownId = nextFocusDownId;
5314 }
5315
5316 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005317 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5318 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5319 *
5320 * @attr ref android.R.styleable#View_nextFocusForward
5321 */
5322 public int getNextFocusForwardId() {
5323 return mNextFocusForwardId;
5324 }
5325
5326 /**
5327 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5328 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5329 * decide automatically.
5330 *
5331 * @attr ref android.R.styleable#View_nextFocusForward
5332 */
5333 public void setNextFocusForwardId(int nextFocusForwardId) {
5334 mNextFocusForwardId = nextFocusForwardId;
5335 }
5336
5337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 * Returns the visibility of this view and all of its ancestors
5339 *
5340 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5341 */
5342 public boolean isShown() {
5343 View current = this;
5344 //noinspection ConstantConditions
5345 do {
5346 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5347 return false;
5348 }
5349 ViewParent parent = current.mParent;
5350 if (parent == null) {
5351 return false; // We are not attached to the view root
5352 }
5353 if (!(parent instanceof View)) {
5354 return true;
5355 }
5356 current = (View) parent;
5357 } while (current != null);
5358
5359 return false;
5360 }
5361
5362 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005363 * Called by the view hierarchy when the content insets for a window have
5364 * changed, to allow it to adjust its content to fit within those windows.
5365 * The content insets tell you the space that the status bar, input method,
5366 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005368 * <p>You do not normally need to deal with this function, since the default
5369 * window decoration given to applications takes care of applying it to the
5370 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5371 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5372 * and your content can be placed under those system elements. You can then
5373 * use this method within your view hierarchy if you have parts of your UI
5374 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005376 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005377 * inset's to the view's padding, consuming that content (modifying the
5378 * insets to be 0), and returning true. This behavior is off by default, but can
5379 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5380 *
5381 * <p>This function's traversal down the hierarchy is depth-first. The same content
5382 * insets object is propagated down the hierarchy, so any changes made to it will
5383 * be seen by all following views (including potentially ones above in
5384 * the hierarchy since this is a depth-first traversal). The first view
5385 * that returns true will abort the entire traversal.
5386 *
5387 * <p>The default implementation works well for a situation where it is
5388 * used with a container that covers the entire window, allowing it to
5389 * apply the appropriate insets to its content on all edges. If you need
5390 * a more complicated layout (such as two different views fitting system
5391 * windows, one on the top of the window, and one on the bottom),
5392 * you can override the method and handle the insets however you would like.
5393 * Note that the insets provided by the framework are always relative to the
5394 * far edges of the window, not accounting for the location of the called view
5395 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005396 * where the layout will place the view, as it is done before layout happens.)
5397 *
5398 * <p>Note: unlike many View methods, there is no dispatch phase to this
5399 * call. If you are overriding it in a ViewGroup and want to allow the
5400 * call to continue to your children, you must be sure to call the super
5401 * implementation.
5402 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005403 * <p>Here is a sample layout that makes use of fitting system windows
5404 * to have controls for a video view placed inside of the window decorations
5405 * that it hides and shows. This can be used with code like the second
5406 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5407 *
5408 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5409 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005410 * @param insets Current content insets of the window. Prior to
5411 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5412 * the insets or else you and Android will be unhappy.
5413 *
5414 * @return Return true if this view applied the insets and it should not
5415 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005416 * @see #getFitsSystemWindows()
5417 * @see #setFitsSystemWindows()
5418 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 */
5420 protected boolean fitSystemWindows(Rect insets) {
5421 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005422 mUserPaddingStart = -1;
5423 mUserPaddingEnd = -1;
5424 mUserPaddingRelative = false;
5425 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5426 || mAttachInfo == null
5427 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5428 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5429 return true;
5430 } else {
5431 internalSetPadding(0, 0, 0, 0);
5432 return false;
5433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 }
5435 return false;
5436 }
5437
5438 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005439 * Sets whether or not this view should account for system screen decorations
5440 * such as the status bar and inset its content; that is, controlling whether
5441 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5442 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005443 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005444 * <p>Note that if you are providing your own implementation of
5445 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5446 * flag to true -- your implementation will be overriding the default
5447 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005448 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005449 * @param fitSystemWindows If true, then the default implementation of
5450 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005451 *
5452 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005453 * @see #getFitsSystemWindows()
5454 * @see #fitSystemWindows(Rect)
5455 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005456 */
5457 public void setFitsSystemWindows(boolean fitSystemWindows) {
5458 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5459 }
5460
5461 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005462 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005463 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5464 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005465 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005466 * @return Returns true if the default implementation of
5467 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005468 *
5469 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005470 * @see #setFitsSystemWindows()
5471 * @see #fitSystemWindows(Rect)
5472 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005473 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005474 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005475 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5476 }
5477
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005478 /** @hide */
5479 public boolean fitsSystemWindows() {
5480 return getFitsSystemWindows();
5481 }
5482
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005483 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005484 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5485 */
5486 public void requestFitSystemWindows() {
5487 if (mParent != null) {
5488 mParent.requestFitSystemWindows();
5489 }
5490 }
5491
5492 /**
5493 * For use by PhoneWindow to make its own system window fitting optional.
5494 * @hide
5495 */
5496 public void makeOptionalFitsSystemWindows() {
5497 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5498 }
5499
5500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 * Returns the visibility status for this view.
5502 *
5503 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5504 * @attr ref android.R.styleable#View_visibility
5505 */
5506 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005507 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5508 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5509 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 })
5511 public int getVisibility() {
5512 return mViewFlags & VISIBILITY_MASK;
5513 }
5514
5515 /**
5516 * Set the enabled state of this view.
5517 *
5518 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5519 * @attr ref android.R.styleable#View_visibility
5520 */
5521 @RemotableViewMethod
5522 public void setVisibility(int visibility) {
5523 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005524 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525 }
5526
5527 /**
5528 * Returns the enabled status for this view. The interpretation of the
5529 * enabled state varies by subclass.
5530 *
5531 * @return True if this view is enabled, false otherwise.
5532 */
5533 @ViewDebug.ExportedProperty
5534 public boolean isEnabled() {
5535 return (mViewFlags & ENABLED_MASK) == ENABLED;
5536 }
5537
5538 /**
5539 * Set the enabled state of this view. The interpretation of the enabled
5540 * state varies by subclass.
5541 *
5542 * @param enabled True if this view is enabled, false otherwise.
5543 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005544 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005546 if (enabled == isEnabled()) return;
5547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5549
5550 /*
5551 * The View most likely has to change its appearance, so refresh
5552 * the drawable state.
5553 */
5554 refreshDrawableState();
5555
5556 // Invalidate too, since the default behavior for views is to be
5557 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005558 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 }
5560
5561 /**
5562 * Set whether this view can receive the focus.
5563 *
5564 * Setting this to false will also ensure that this view is not focusable
5565 * in touch mode.
5566 *
5567 * @param focusable If true, this view can receive the focus.
5568 *
5569 * @see #setFocusableInTouchMode(boolean)
5570 * @attr ref android.R.styleable#View_focusable
5571 */
5572 public void setFocusable(boolean focusable) {
5573 if (!focusable) {
5574 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5575 }
5576 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5577 }
5578
5579 /**
5580 * Set whether this view can receive focus while in touch mode.
5581 *
5582 * Setting this to true will also ensure that this view is focusable.
5583 *
5584 * @param focusableInTouchMode If true, this view can receive the focus while
5585 * in touch mode.
5586 *
5587 * @see #setFocusable(boolean)
5588 * @attr ref android.R.styleable#View_focusableInTouchMode
5589 */
5590 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5591 // Focusable in touch mode should always be set before the focusable flag
5592 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5593 // which, in touch mode, will not successfully request focus on this view
5594 // because the focusable in touch mode flag is not set
5595 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5596 if (focusableInTouchMode) {
5597 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5598 }
5599 }
5600
5601 /**
5602 * Set whether this view should have sound effects enabled for events such as
5603 * clicking and touching.
5604 *
5605 * <p>You may wish to disable sound effects for a view if you already play sounds,
5606 * for instance, a dial key that plays dtmf tones.
5607 *
5608 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5609 * @see #isSoundEffectsEnabled()
5610 * @see #playSoundEffect(int)
5611 * @attr ref android.R.styleable#View_soundEffectsEnabled
5612 */
5613 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5614 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5615 }
5616
5617 /**
5618 * @return whether this view should have sound effects enabled for events such as
5619 * clicking and touching.
5620 *
5621 * @see #setSoundEffectsEnabled(boolean)
5622 * @see #playSoundEffect(int)
5623 * @attr ref android.R.styleable#View_soundEffectsEnabled
5624 */
5625 @ViewDebug.ExportedProperty
5626 public boolean isSoundEffectsEnabled() {
5627 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5628 }
5629
5630 /**
5631 * Set whether this view should have haptic feedback for events such as
5632 * long presses.
5633 *
5634 * <p>You may wish to disable haptic feedback if your view already controls
5635 * its own haptic feedback.
5636 *
5637 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5638 * @see #isHapticFeedbackEnabled()
5639 * @see #performHapticFeedback(int)
5640 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5641 */
5642 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5643 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5644 }
5645
5646 /**
5647 * @return whether this view should have haptic feedback enabled for events
5648 * long presses.
5649 *
5650 * @see #setHapticFeedbackEnabled(boolean)
5651 * @see #performHapticFeedback(int)
5652 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5653 */
5654 @ViewDebug.ExportedProperty
5655 public boolean isHapticFeedbackEnabled() {
5656 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5657 }
5658
5659 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005660 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005661 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005662 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5663 * {@link #LAYOUT_DIRECTION_RTL},
5664 * {@link #LAYOUT_DIRECTION_INHERIT} or
5665 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005666 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005667 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005668 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005669 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005670 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005671 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5672 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5673 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5674 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005675 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005676 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005677 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005678 }
5679
5680 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005681 * Set the layout direction for this view. This will propagate a reset of layout direction
5682 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005683 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005684 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5685 * {@link #LAYOUT_DIRECTION_RTL},
5686 * {@link #LAYOUT_DIRECTION_INHERIT} or
5687 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005688 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005689 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005690 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005691 */
5692 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005693 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005694 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005695 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005696 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005697 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005698 // Set the new layout direction (filtered) and ask for a layout pass
5699 mPrivateFlags2 |=
5700 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5701 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005702 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005703 }
5704
5705 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005706 * Returns the resolved layout direction for this view.
5707 *
5708 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005709 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005710 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005711 */
5712 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005713 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5714 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005715 })
5716 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005717 // The layout diretion will be resolved only if needed
5718 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5719 resolveLayoutDirection();
5720 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005721 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005722 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5723 }
5724
5725 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005726 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5727 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005728 *
5729 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005730 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005731 */
5732 @ViewDebug.ExportedProperty(category = "layout")
5733 public boolean isLayoutRtl() {
5734 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5735 }
5736
5737 /**
Adam Powell539ee872012-02-03 19:00:49 -08005738 * Indicates whether the view is currently tracking transient state that the
5739 * app should not need to concern itself with saving and restoring, but that
5740 * the framework should take special note to preserve when possible.
5741 *
Adam Powell785c4472012-05-02 21:25:39 -07005742 * <p>A view with transient state cannot be trivially rebound from an external
5743 * data source, such as an adapter binding item views in a list. This may be
5744 * because the view is performing an animation, tracking user selection
5745 * of content, or similar.</p>
5746 *
Adam Powell539ee872012-02-03 19:00:49 -08005747 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005748 */
5749 @ViewDebug.ExportedProperty(category = "layout")
5750 public boolean hasTransientState() {
5751 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5752 }
5753
5754 /**
5755 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005756 * framework should attempt to preserve when possible. This flag is reference counted,
5757 * so every call to setHasTransientState(true) should be paired with a later call
5758 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005759 *
Adam Powell785c4472012-05-02 21:25:39 -07005760 * <p>A view with transient state cannot be trivially rebound from an external
5761 * data source, such as an adapter binding item views in a list. This may be
5762 * because the view is performing an animation, tracking user selection
5763 * of content, or similar.</p>
5764 *
Adam Powell539ee872012-02-03 19:00:49 -08005765 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005766 */
5767 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005768 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5769 mTransientStateCount - 1;
5770 if (mTransientStateCount < 0) {
5771 mTransientStateCount = 0;
5772 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5773 "unmatched pair of setHasTransientState calls");
5774 }
5775 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005776 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005777 // update flag if we've just incremented up from 0 or decremented down to 0
5778 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5779 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5780 if (mParent != null) {
5781 try {
5782 mParent.childHasTransientStateChanged(this, hasTransientState);
5783 } catch (AbstractMethodError e) {
5784 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5785 " does not fully implement ViewParent", e);
5786 }
Adam Powell539ee872012-02-03 19:00:49 -08005787 }
5788 }
5789 }
5790
5791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 * If this view doesn't do any drawing on its own, set this flag to
5793 * allow further optimizations. By default, this flag is not set on
5794 * View, but could be set on some View subclasses such as ViewGroup.
5795 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005796 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5797 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 *
5799 * @param willNotDraw whether or not this View draw on its own
5800 */
5801 public void setWillNotDraw(boolean willNotDraw) {
5802 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5803 }
5804
5805 /**
5806 * Returns whether or not this View draws on its own.
5807 *
5808 * @return true if this view has nothing to draw, false otherwise
5809 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005810 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 public boolean willNotDraw() {
5812 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5813 }
5814
5815 /**
5816 * When a View's drawing cache is enabled, drawing is redirected to an
5817 * offscreen bitmap. Some views, like an ImageView, must be able to
5818 * bypass this mechanism if they already draw a single bitmap, to avoid
5819 * unnecessary usage of the memory.
5820 *
5821 * @param willNotCacheDrawing true if this view does not cache its
5822 * drawing, false otherwise
5823 */
5824 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5825 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5826 }
5827
5828 /**
5829 * Returns whether or not this View can cache its drawing or not.
5830 *
5831 * @return true if this view does not cache its drawing, false otherwise
5832 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005833 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 public boolean willNotCacheDrawing() {
5835 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5836 }
5837
5838 /**
5839 * Indicates whether this view reacts to click events or not.
5840 *
5841 * @return true if the view is clickable, false otherwise
5842 *
5843 * @see #setClickable(boolean)
5844 * @attr ref android.R.styleable#View_clickable
5845 */
5846 @ViewDebug.ExportedProperty
5847 public boolean isClickable() {
5848 return (mViewFlags & CLICKABLE) == CLICKABLE;
5849 }
5850
5851 /**
5852 * Enables or disables click events for this view. When a view
5853 * is clickable it will change its state to "pressed" on every click.
5854 * Subclasses should set the view clickable to visually react to
5855 * user's clicks.
5856 *
5857 * @param clickable true to make the view clickable, false otherwise
5858 *
5859 * @see #isClickable()
5860 * @attr ref android.R.styleable#View_clickable
5861 */
5862 public void setClickable(boolean clickable) {
5863 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5864 }
5865
5866 /**
5867 * Indicates whether this view reacts to long click events or not.
5868 *
5869 * @return true if the view is long clickable, false otherwise
5870 *
5871 * @see #setLongClickable(boolean)
5872 * @attr ref android.R.styleable#View_longClickable
5873 */
5874 public boolean isLongClickable() {
5875 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5876 }
5877
5878 /**
5879 * Enables or disables long click events for this view. When a view is long
5880 * clickable it reacts to the user holding down the button for a longer
5881 * duration than a tap. This event can either launch the listener or a
5882 * context menu.
5883 *
5884 * @param longClickable true to make the view long clickable, false otherwise
5885 * @see #isLongClickable()
5886 * @attr ref android.R.styleable#View_longClickable
5887 */
5888 public void setLongClickable(boolean longClickable) {
5889 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5890 }
5891
5892 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005893 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 *
5895 * @see #isClickable()
5896 * @see #setClickable(boolean)
5897 *
5898 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5899 * the View's internal state from a previously set "pressed" state.
5900 */
5901 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005902 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 if (pressed) {
5905 mPrivateFlags |= PRESSED;
5906 } else {
5907 mPrivateFlags &= ~PRESSED;
5908 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005909
5910 if (needsRefresh) {
5911 refreshDrawableState();
5912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 dispatchSetPressed(pressed);
5914 }
5915
5916 /**
5917 * Dispatch setPressed to all of this View's children.
5918 *
5919 * @see #setPressed(boolean)
5920 *
5921 * @param pressed The new pressed state
5922 */
5923 protected void dispatchSetPressed(boolean pressed) {
5924 }
5925
5926 /**
5927 * Indicates whether the view is currently in pressed state. Unless
5928 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5929 * the pressed state.
5930 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005931 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 * @see #isClickable()
5933 * @see #setClickable(boolean)
5934 *
5935 * @return true if the view is currently pressed, false otherwise
5936 */
5937 public boolean isPressed() {
5938 return (mPrivateFlags & PRESSED) == PRESSED;
5939 }
5940
5941 /**
5942 * Indicates whether this view will save its state (that is,
5943 * whether its {@link #onSaveInstanceState} method will be called).
5944 *
5945 * @return Returns true if the view state saving is enabled, else false.
5946 *
5947 * @see #setSaveEnabled(boolean)
5948 * @attr ref android.R.styleable#View_saveEnabled
5949 */
5950 public boolean isSaveEnabled() {
5951 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5952 }
5953
5954 /**
5955 * Controls whether the saving of this view's state is
5956 * enabled (that is, whether its {@link #onSaveInstanceState} method
5957 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005958 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 * for its state to be saved. This flag can only disable the
5960 * saving of this view; any child views may still have their state saved.
5961 *
5962 * @param enabled Set to false to <em>disable</em> state saving, or true
5963 * (the default) to allow it.
5964 *
5965 * @see #isSaveEnabled()
5966 * @see #setId(int)
5967 * @see #onSaveInstanceState()
5968 * @attr ref android.R.styleable#View_saveEnabled
5969 */
5970 public void setSaveEnabled(boolean enabled) {
5971 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5972 }
5973
Jeff Brown85a31762010-09-01 17:01:00 -07005974 /**
5975 * Gets whether the framework should discard touches when the view's
5976 * window is obscured by another visible window.
5977 * Refer to the {@link View} security documentation for more details.
5978 *
5979 * @return True if touch filtering is enabled.
5980 *
5981 * @see #setFilterTouchesWhenObscured(boolean)
5982 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5983 */
5984 @ViewDebug.ExportedProperty
5985 public boolean getFilterTouchesWhenObscured() {
5986 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5987 }
5988
5989 /**
5990 * Sets whether the framework should discard touches when the view's
5991 * window is obscured by another visible window.
5992 * Refer to the {@link View} security documentation for more details.
5993 *
5994 * @param enabled True if touch filtering should be enabled.
5995 *
5996 * @see #getFilterTouchesWhenObscured
5997 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5998 */
5999 public void setFilterTouchesWhenObscured(boolean enabled) {
6000 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6001 FILTER_TOUCHES_WHEN_OBSCURED);
6002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003
6004 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006005 * Indicates whether the entire hierarchy under this view will save its
6006 * state when a state saving traversal occurs from its parent. The default
6007 * is true; if false, these views will not be saved unless
6008 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6009 *
6010 * @return Returns true if the view state saving from parent is enabled, else false.
6011 *
6012 * @see #setSaveFromParentEnabled(boolean)
6013 */
6014 public boolean isSaveFromParentEnabled() {
6015 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6016 }
6017
6018 /**
6019 * Controls whether the entire hierarchy under this view will save its
6020 * state when a state saving traversal occurs from its parent. The default
6021 * is true; if false, these views will not be saved unless
6022 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6023 *
6024 * @param enabled Set to false to <em>disable</em> state saving, or true
6025 * (the default) to allow it.
6026 *
6027 * @see #isSaveFromParentEnabled()
6028 * @see #setId(int)
6029 * @see #onSaveInstanceState()
6030 */
6031 public void setSaveFromParentEnabled(boolean enabled) {
6032 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6033 }
6034
6035
6036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 * Returns whether this View is able to take focus.
6038 *
6039 * @return True if this view can take focus, or false otherwise.
6040 * @attr ref android.R.styleable#View_focusable
6041 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006042 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 public final boolean isFocusable() {
6044 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6045 }
6046
6047 /**
6048 * When a view is focusable, it may not want to take focus when in touch mode.
6049 * For example, a button would like focus when the user is navigating via a D-pad
6050 * so that the user can click on it, but once the user starts touching the screen,
6051 * the button shouldn't take focus
6052 * @return Whether the view is focusable in touch mode.
6053 * @attr ref android.R.styleable#View_focusableInTouchMode
6054 */
6055 @ViewDebug.ExportedProperty
6056 public final boolean isFocusableInTouchMode() {
6057 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6058 }
6059
6060 /**
6061 * Find the nearest view in the specified direction that can take focus.
6062 * This does not actually give focus to that view.
6063 *
6064 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6065 *
6066 * @return The nearest focusable in the specified direction, or null if none
6067 * can be found.
6068 */
6069 public View focusSearch(int direction) {
6070 if (mParent != null) {
6071 return mParent.focusSearch(this, direction);
6072 } else {
6073 return null;
6074 }
6075 }
6076
6077 /**
6078 * This method is the last chance for the focused view and its ancestors to
6079 * respond to an arrow key. This is called when the focused view did not
6080 * consume the key internally, nor could the view system find a new view in
6081 * the requested direction to give focus to.
6082 *
6083 * @param focused The currently focused view.
6084 * @param direction The direction focus wants to move. One of FOCUS_UP,
6085 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6086 * @return True if the this view consumed this unhandled move.
6087 */
6088 public boolean dispatchUnhandledMove(View focused, int direction) {
6089 return false;
6090 }
6091
6092 /**
6093 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006094 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006096 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6097 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 * @return The user specified next view, or null if there is none.
6099 */
6100 View findUserSetNextFocus(View root, int direction) {
6101 switch (direction) {
6102 case FOCUS_LEFT:
6103 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006104 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 case FOCUS_RIGHT:
6106 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006107 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 case FOCUS_UP:
6109 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006110 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 case FOCUS_DOWN:
6112 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006113 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006114 case FOCUS_FORWARD:
6115 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006116 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006117 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006118 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006119 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006120 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006121 @Override
6122 public boolean apply(View t) {
6123 return t.mNextFocusForwardId == id;
6124 }
6125 });
6126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 }
6128 return null;
6129 }
6130
Jeff Brown4dfbec22011-08-15 14:55:37 -07006131 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6132 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6133 @Override
6134 public boolean apply(View t) {
6135 return t.mID == childViewId;
6136 }
6137 });
6138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 if (result == null) {
6140 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6141 + "by user for id " + childViewId);
6142 }
6143 return result;
6144 }
6145
6146 /**
6147 * Find and return all focusable views that are descendants of this view,
6148 * possibly including this view if it is focusable itself.
6149 *
6150 * @param direction The direction of the focus
6151 * @return A list of focusable views
6152 */
6153 public ArrayList<View> getFocusables(int direction) {
6154 ArrayList<View> result = new ArrayList<View>(24);
6155 addFocusables(result, direction);
6156 return result;
6157 }
6158
6159 /**
6160 * Add any focusable views that are descendants of this view (possibly
6161 * including this view if it is focusable itself) to views. If we are in touch mode,
6162 * only add views that are also focusable in touch mode.
6163 *
6164 * @param views Focusable views found so far
6165 * @param direction The direction of the focus
6166 */
6167 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006168 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170
svetoslavganov75986cf2009-05-14 22:28:01 -07006171 /**
6172 * Adds any focusable views that are descendants of this view (possibly
6173 * including this view if it is focusable itself) to views. This method
6174 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006175 * only views focusable in touch mode if we are in touch mode or
6176 * only views that can take accessibility focus if accessibility is enabeld
6177 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006178 *
6179 * @param views Focusable views found so far or null if all we are interested is
6180 * the number of focusables.
6181 * @param direction The direction of the focus.
6182 * @param focusableMode The type of focusables to be added.
6183 *
6184 * @see #FOCUSABLES_ALL
6185 * @see #FOCUSABLES_TOUCH_MODE
6186 */
6187 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006188 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006189 return;
6190 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006191 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006192 if (isAccessibilityFocusable()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006193 views.add(this);
6194 return;
6195 }
6196 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006197 if (!isFocusable()) {
6198 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006199 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006200 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6201 && isInTouchMode() && !isFocusableInTouchMode()) {
6202 return;
6203 }
6204 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 }
6206
6207 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006208 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006209 * The search is performed by either the text that the View renders or the content
6210 * description that describes the view for accessibility purposes and the view does
6211 * not render or both. Clients can specify how the search is to be performed via
6212 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6213 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006214 *
6215 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006216 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006217 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006218 * @see #FIND_VIEWS_WITH_TEXT
6219 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6220 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006221 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006222 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006223 if (getAccessibilityNodeProvider() != null) {
6224 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6225 outViews.add(this);
6226 }
6227 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006228 && (searched != null && searched.length() > 0)
6229 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006230 String searchedLowerCase = searched.toString().toLowerCase();
6231 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6232 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6233 outViews.add(this);
6234 }
6235 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006236 }
6237
6238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 * Find and return all touchable views that are descendants of this view,
6240 * possibly including this view if it is touchable itself.
6241 *
6242 * @return A list of touchable views
6243 */
6244 public ArrayList<View> getTouchables() {
6245 ArrayList<View> result = new ArrayList<View>();
6246 addTouchables(result);
6247 return result;
6248 }
6249
6250 /**
6251 * Add any touchable views that are descendants of this view (possibly
6252 * including this view if it is touchable itself) to views.
6253 *
6254 * @param views Touchable views found so far
6255 */
6256 public void addTouchables(ArrayList<View> views) {
6257 final int viewFlags = mViewFlags;
6258
6259 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6260 && (viewFlags & ENABLED_MASK) == ENABLED) {
6261 views.add(this);
6262 }
6263 }
6264
6265 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006266 * Returns whether this View is accessibility focused.
6267 *
6268 * @return True if this View is accessibility focused.
6269 */
6270 boolean isAccessibilityFocused() {
6271 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6272 }
6273
6274 /**
6275 * Call this to try to give accessibility focus to this view.
6276 *
6277 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6278 * returns false or the view is no visible or the view already has accessibility
6279 * focus.
6280 *
6281 * See also {@link #focusSearch(int)}, which is what you call to say that you
6282 * have focus, and you want your parent to look for the next one.
6283 *
6284 * @return Whether this view actually took accessibility focus.
6285 *
6286 * @hide
6287 */
6288 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006289 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6290 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006291 return false;
6292 }
6293 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6294 return false;
6295 }
6296 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6297 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6298 ViewRootImpl viewRootImpl = getViewRootImpl();
6299 if (viewRootImpl != null) {
6300 viewRootImpl.setAccessibilityFocusedHost(this);
6301 }
6302 invalidate();
6303 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6304 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006305 return true;
6306 }
6307 return false;
6308 }
6309
6310 /**
6311 * Call this to try to clear accessibility focus of this view.
6312 *
6313 * See also {@link #focusSearch(int)}, which is what you call to say that you
6314 * have focus, and you want your parent to look for the next one.
6315 *
6316 * @hide
6317 */
6318 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006319 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6320 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006321 invalidate();
6322 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6323 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006324 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006325 // Clear the global reference of accessibility focus if this
6326 // view or any of its descendants had accessibility focus.
6327 ViewRootImpl viewRootImpl = getViewRootImpl();
6328 if (viewRootImpl != null) {
6329 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6330 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6331 viewRootImpl.setAccessibilityFocusedHost(null);
6332 }
6333 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006334 }
6335
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006336 private void requestAccessibilityFocusFromHover() {
6337 if (includeForAccessibility() && isActionableForAccessibility()) {
6338 requestAccessibilityFocus();
6339 } else {
6340 if (mParent != null) {
6341 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6342 if (nextFocus != null) {
6343 nextFocus.requestAccessibilityFocus();
6344 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006345 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006346 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006347 }
6348
Svetoslav Ganova90e4512012-06-01 19:02:32 -07006349 private boolean canTakeAccessibilityFocusFromHover() {
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006350 if (includeForAccessibility() && isActionableForAccessibility()) {
6351 return true;
6352 }
6353 if (mParent != null) {
6354 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006355 }
6356 return false;
6357 }
6358
6359 /**
6360 * Clears accessibility focus without calling any callback methods
6361 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6362 * is used for clearing accessibility focus when giving this focus to
6363 * another view.
6364 */
6365 void clearAccessibilityFocusNoCallbacks() {
6366 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6367 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6368 invalidate();
6369 }
6370 }
6371
6372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 * Call this to try to give focus to a specific view or to one of its
6374 * descendants.
6375 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006376 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6377 * false), or if it is focusable and it is not focusable in touch mode
6378 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006380 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 * have focus, and you want your parent to look for the next one.
6382 *
6383 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6384 * {@link #FOCUS_DOWN} and <code>null</code>.
6385 *
6386 * @return Whether this view or one of its descendants actually took focus.
6387 */
6388 public final boolean requestFocus() {
6389 return requestFocus(View.FOCUS_DOWN);
6390 }
6391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 /**
6393 * Call this to try to give focus to a specific view or to one of its
6394 * descendants and give it a hint about what direction focus is heading.
6395 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006396 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6397 * false), or if it is focusable and it is not focusable in touch mode
6398 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006400 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 * have focus, and you want your parent to look for the next one.
6402 *
6403 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6404 * <code>null</code> set for the previously focused rectangle.
6405 *
6406 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6407 * @return Whether this view or one of its descendants actually took focus.
6408 */
6409 public final boolean requestFocus(int direction) {
6410 return requestFocus(direction, null);
6411 }
6412
6413 /**
6414 * Call this to try to give focus to a specific view or to one of its descendants
6415 * and give it hints about the direction and a specific rectangle that the focus
6416 * is coming from. The rectangle can help give larger views a finer grained hint
6417 * about where focus is coming from, and therefore, where to show selection, or
6418 * forward focus change internally.
6419 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006420 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6421 * false), or if it is focusable and it is not focusable in touch mode
6422 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 *
6424 * A View will not take focus if it is not visible.
6425 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006426 * A View will not take focus if one of its parents has
6427 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6428 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006430 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 * have focus, and you want your parent to look for the next one.
6432 *
6433 * You may wish to override this method if your custom {@link View} has an internal
6434 * {@link View} that it wishes to forward the request to.
6435 *
6436 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6437 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6438 * to give a finer grained hint about where focus is coming from. May be null
6439 * if there is no hint.
6440 * @return Whether this view or one of its descendants actually took focus.
6441 */
6442 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006443 return requestFocusNoSearch(direction, previouslyFocusedRect);
6444 }
6445
6446 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 // need to be focusable
6448 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6449 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6450 return false;
6451 }
6452
6453 // need to be focusable in touch mode if in touch mode
6454 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006455 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6456 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 }
6458
6459 // need to not have any parents blocking us
6460 if (hasAncestorThatBlocksDescendantFocus()) {
6461 return false;
6462 }
6463
6464 handleFocusGainInternal(direction, previouslyFocusedRect);
6465 return true;
6466 }
6467
6468 /**
6469 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6470 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6471 * touch mode to request focus when they are touched.
6472 *
6473 * @return Whether this view or one of its descendants actually took focus.
6474 *
6475 * @see #isInTouchMode()
6476 *
6477 */
6478 public final boolean requestFocusFromTouch() {
6479 // Leave touch mode if we need to
6480 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006481 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006482 if (viewRoot != null) {
6483 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006484 }
6485 }
6486 return requestFocus(View.FOCUS_DOWN);
6487 }
6488
6489 /**
6490 * @return Whether any ancestor of this view blocks descendant focus.
6491 */
6492 private boolean hasAncestorThatBlocksDescendantFocus() {
6493 ViewParent ancestor = mParent;
6494 while (ancestor instanceof ViewGroup) {
6495 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6496 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6497 return true;
6498 } else {
6499 ancestor = vgAncestor.getParent();
6500 }
6501 }
6502 return false;
6503 }
6504
6505 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006506 * Gets the mode for determining 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 * @return The mode for determining whether a 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 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006519 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6520 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6521 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006522 })
6523 public int getImportantForAccessibility() {
6524 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6525 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6526 }
6527
6528 /**
6529 * Sets how to determine whether this view is important for accessibility
6530 * which is if it fires accessibility events and if it is reported to
6531 * accessibility services that query the screen.
6532 *
6533 * @param mode How to determine whether this view is important for accessibility.
6534 *
6535 * @attr ref android.R.styleable#View_importantForAccessibility
6536 *
6537 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6538 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6539 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6540 */
6541 public void setImportantForAccessibility(int mode) {
6542 if (mode != getImportantForAccessibility()) {
6543 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6544 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6545 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6546 notifyAccessibilityStateChanged();
6547 }
6548 }
6549
6550 /**
6551 * Gets whether this view should be exposed for accessibility.
6552 *
6553 * @return Whether the view is exposed for accessibility.
6554 *
6555 * @hide
6556 */
6557 public boolean isImportantForAccessibility() {
6558 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6559 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6560 switch (mode) {
6561 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6562 return true;
6563 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6564 return false;
6565 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6566 return isActionableForAccessibility() || hasListenersForAccessibility();
6567 default:
6568 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6569 + mode);
6570 }
6571 }
6572
6573 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006574 * Gets the mode for determining whether this View can take accessibility focus.
6575 *
6576 * @return The mode for determining whether a View can take accessibility focus.
6577 *
6578 * @attr ref android.R.styleable#View_accessibilityFocusable
6579 *
6580 * @see #ACCESSIBILITY_FOCUSABLE_YES
6581 * @see #ACCESSIBILITY_FOCUSABLE_NO
6582 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6583 *
6584 * @hide
6585 */
6586 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6587 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_AUTO, to = "auto"),
6588 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_YES, to = "yes"),
6589 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_NO, to = "no")
6590 })
6591 public int getAccessibilityFocusable() {
6592 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK) >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6593 }
6594
6595 /**
6596 * Sets how to determine whether this view can take accessibility focus.
6597 *
6598 * @param mode How to determine whether this view can take accessibility focus.
6599 *
6600 * @attr ref android.R.styleable#View_accessibilityFocusable
6601 *
6602 * @see #ACCESSIBILITY_FOCUSABLE_YES
6603 * @see #ACCESSIBILITY_FOCUSABLE_NO
6604 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6605 *
6606 * @hide
6607 */
6608 public void setAccessibilityFocusable(int mode) {
6609 if (mode != getAccessibilityFocusable()) {
6610 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSABLE_MASK;
6611 mPrivateFlags2 |= (mode << ACCESSIBILITY_FOCUSABLE_SHIFT)
6612 & ACCESSIBILITY_FOCUSABLE_MASK;
6613 notifyAccessibilityStateChanged();
6614 }
6615 }
6616
6617 /**
6618 * Gets whether this view can take accessibility focus.
6619 *
6620 * @return Whether the view can take accessibility focus.
6621 *
6622 * @hide
6623 */
6624 public boolean isAccessibilityFocusable() {
6625 final int mode = (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK)
6626 >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6627 switch (mode) {
6628 case ACCESSIBILITY_FOCUSABLE_YES:
6629 return true;
6630 case ACCESSIBILITY_FOCUSABLE_NO:
6631 return false;
6632 case ACCESSIBILITY_FOCUSABLE_AUTO:
6633 return canTakeAccessibilityFocusFromHover()
6634 || getAccessibilityNodeProvider() != null;
6635 default:
6636 throw new IllegalArgumentException("Unknow accessibility focusable mode: " + mode);
6637 }
6638 }
6639
6640 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006641 * Gets the parent for accessibility purposes. Note that the parent for
6642 * accessibility is not necessary the immediate parent. It is the first
6643 * predecessor that is important for accessibility.
6644 *
6645 * @return The parent for accessibility purposes.
6646 */
6647 public ViewParent getParentForAccessibility() {
6648 if (mParent instanceof View) {
6649 View parentView = (View) mParent;
6650 if (parentView.includeForAccessibility()) {
6651 return mParent;
6652 } else {
6653 return mParent.getParentForAccessibility();
6654 }
6655 }
6656 return null;
6657 }
6658
6659 /**
6660 * Adds the children of a given View for accessibility. Since some Views are
6661 * not important for accessibility the children for accessibility are not
6662 * necessarily direct children of the riew, rather they are the first level of
6663 * descendants important for accessibility.
6664 *
6665 * @param children The list of children for accessibility.
6666 */
6667 public void addChildrenForAccessibility(ArrayList<View> children) {
6668 if (includeForAccessibility()) {
6669 children.add(this);
6670 }
6671 }
6672
6673 /**
6674 * Whether to regard this view for accessibility. A view is regarded for
6675 * accessibility if it is important for accessibility or the querying
6676 * accessibility service has explicitly requested that view not
6677 * important for accessibility are regarded.
6678 *
6679 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006680 *
6681 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006682 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006683 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006684 if (mAttachInfo != null) {
6685 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006686 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006687 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006688 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006689 }
6690 return false;
6691 }
6692
6693 /**
6694 * Returns whether the View is considered actionable from
6695 * accessibility perspective. Such view are important for
6696 * accessiiblity.
6697 *
6698 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006699 *
6700 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006701 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006702 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006703 return (isClickable() || isLongClickable() || isFocusable());
6704 }
6705
6706 /**
6707 * Returns whether the View has registered callbacks wich makes it
6708 * important for accessiiblity.
6709 *
6710 * @return True if the view is actionable for accessibility.
6711 */
6712 private boolean hasListenersForAccessibility() {
6713 ListenerInfo info = getListenerInfo();
6714 return mTouchDelegate != null || info.mOnKeyListener != null
6715 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6716 || info.mOnHoverListener != null || info.mOnDragListener != null;
6717 }
6718
6719 /**
6720 * Notifies accessibility services that some view's important for
6721 * accessibility state has changed. Note that such notifications
6722 * are made at most once every
6723 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6724 * to avoid unnecessary load to the system. Also once a view has
6725 * made a notifucation this method is a NOP until the notification has
6726 * been sent to clients.
6727 *
6728 * @hide
6729 *
6730 * TODO: Makse sure this method is called for any view state change
6731 * that is interesting for accessilility purposes.
6732 */
6733 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006734 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6735 return;
6736 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006737 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6738 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6739 if (mParent != null) {
6740 mParent.childAccessibilityStateChanged(this);
6741 }
6742 }
6743 }
6744
6745 /**
6746 * Reset the state indicating the this view has requested clients
6747 * interested in its accessiblity state to be notified.
6748 *
6749 * @hide
6750 */
6751 public void resetAccessibilityStateChanged() {
6752 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6753 }
6754
6755 /**
6756 * Performs the specified accessibility action on the view. For
6757 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006758 * <p>
6759 * If an {@link AccessibilityDelegate} has been specified via calling
6760 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6761 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6762 * is responsible for handling this call.
6763 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006764 *
6765 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006766 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006767 * @return Whether the action was performed.
6768 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006769 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006770 if (mAccessibilityDelegate != null) {
6771 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6772 } else {
6773 return performAccessibilityActionInternal(action, arguments);
6774 }
6775 }
6776
6777 /**
6778 * @see #performAccessibilityAction(int, Bundle)
6779 *
6780 * Note: Called from the default {@link AccessibilityDelegate}.
6781 */
6782 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006783 switch (action) {
6784 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006785 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006786 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006787 }
6788 } break;
6789 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6790 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006791 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006792 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006793 } break;
6794 case AccessibilityNodeInfo.ACTION_FOCUS: {
6795 if (!hasFocus()) {
6796 // Get out of touch mode since accessibility
6797 // wants to move focus around.
6798 getViewRootImpl().ensureTouchMode(false);
6799 return requestFocus();
6800 }
6801 } break;
6802 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6803 if (hasFocus()) {
6804 clearFocus();
6805 return !isFocused();
6806 }
6807 } break;
6808 case AccessibilityNodeInfo.ACTION_SELECT: {
6809 if (!isSelected()) {
6810 setSelected(true);
6811 return isSelected();
6812 }
6813 } break;
6814 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6815 if (isSelected()) {
6816 setSelected(false);
6817 return !isSelected();
6818 }
6819 } break;
6820 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006821 final int mode = getAccessibilityFocusable();
6822 if (!isAccessibilityFocused()
6823 && (mode == ACCESSIBILITY_FOCUSABLE_YES
6824 || mode == ACCESSIBILITY_FOCUSABLE_AUTO)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006825 return requestAccessibilityFocus();
6826 }
6827 } break;
6828 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6829 if (isAccessibilityFocused()) {
6830 clearAccessibilityFocus();
6831 return true;
6832 }
6833 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006834 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6835 if (arguments != null) {
6836 final int granularity = arguments.getInt(
6837 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6838 return nextAtGranularity(granularity);
6839 }
6840 } break;
6841 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6842 if (arguments != null) {
6843 final int granularity = arguments.getInt(
6844 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6845 return previousAtGranularity(granularity);
6846 }
6847 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006848 }
6849 return false;
6850 }
6851
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006852 private boolean nextAtGranularity(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 }
6861 final int current = getAccessibilityCursorPosition();
6862 final int[] range = iterator.following(current);
6863 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006864 return false;
6865 }
6866 final int start = range[0];
6867 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006868 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006869 sendViewTextTraversedAtGranularityEvent(
6870 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6871 granularity, start, end);
6872 return true;
6873 }
6874
6875 private boolean previousAtGranularity(int granularity) {
6876 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006877 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006878 return false;
6879 }
6880 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6881 if (iterator == null) {
6882 return false;
6883 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006884 int current = getAccessibilityCursorPosition();
6885 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6886 current = text.length();
6887 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6888 // When traversing by character we always put the cursor after the character
6889 // to ease edit and have to compensate before asking the for previous segment.
6890 current--;
6891 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006892 final int[] range = iterator.preceding(current);
6893 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006894 return false;
6895 }
6896 final int start = range[0];
6897 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006898 // Always put the cursor after the character to ease edit.
6899 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6900 setAccessibilityCursorPosition(end);
6901 } else {
6902 setAccessibilityCursorPosition(start);
6903 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006904 sendViewTextTraversedAtGranularityEvent(
6905 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6906 granularity, start, end);
6907 return true;
6908 }
6909
6910 /**
6911 * Gets the text reported for accessibility purposes.
6912 *
6913 * @return The accessibility text.
6914 *
6915 * @hide
6916 */
6917 public CharSequence getIterableTextForAccessibility() {
6918 return mContentDescription;
6919 }
6920
6921 /**
6922 * @hide
6923 */
6924 public int getAccessibilityCursorPosition() {
6925 return mAccessibilityCursorPosition;
6926 }
6927
6928 /**
6929 * @hide
6930 */
6931 public void setAccessibilityCursorPosition(int position) {
6932 mAccessibilityCursorPosition = position;
6933 }
6934
6935 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6936 int fromIndex, int toIndex) {
6937 if (mParent == null) {
6938 return;
6939 }
6940 AccessibilityEvent event = AccessibilityEvent.obtain(
6941 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6942 onInitializeAccessibilityEvent(event);
6943 onPopulateAccessibilityEvent(event);
6944 event.setFromIndex(fromIndex);
6945 event.setToIndex(toIndex);
6946 event.setAction(action);
6947 event.setMovementGranularity(granularity);
6948 mParent.requestSendAccessibilityEvent(this, event);
6949 }
6950
6951 /**
6952 * @hide
6953 */
6954 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6955 switch (granularity) {
6956 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6957 CharSequence text = getIterableTextForAccessibility();
6958 if (text != null && text.length() > 0) {
6959 CharacterTextSegmentIterator iterator =
6960 CharacterTextSegmentIterator.getInstance(mContext);
6961 iterator.initialize(text.toString());
6962 return iterator;
6963 }
6964 } break;
6965 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6966 CharSequence text = getIterableTextForAccessibility();
6967 if (text != null && text.length() > 0) {
6968 WordTextSegmentIterator iterator =
6969 WordTextSegmentIterator.getInstance(mContext);
6970 iterator.initialize(text.toString());
6971 return iterator;
6972 }
6973 } break;
6974 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6975 CharSequence text = getIterableTextForAccessibility();
6976 if (text != null && text.length() > 0) {
6977 ParagraphTextSegmentIterator iterator =
6978 ParagraphTextSegmentIterator.getInstance();
6979 iterator.initialize(text.toString());
6980 return iterator;
6981 }
6982 } break;
6983 }
6984 return null;
6985 }
6986
Svetoslav Ganov42138042012-03-20 11:51:39 -07006987 /**
Romain Guya440b002010-02-24 15:57:54 -08006988 * @hide
6989 */
6990 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006991 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006992 clearDisplayList();
6993
Romain Guya440b002010-02-24 15:57:54 -08006994 onStartTemporaryDetach();
6995 }
6996
6997 /**
6998 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7000 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007001 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 */
7003 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007004 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08007005 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007006 }
7007
7008 /**
7009 * @hide
7010 */
7011 public void dispatchFinishTemporaryDetach() {
7012 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 }
Romain Guy8506ab42009-06-11 17:35:47 -07007014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 /**
7016 * Called after {@link #onStartTemporaryDetach} when the container is done
7017 * changing the view.
7018 */
7019 public void onFinishTemporaryDetach() {
7020 }
Romain Guy8506ab42009-06-11 17:35:47 -07007021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007023 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7024 * for this view's window. Returns null if the view is not currently attached
7025 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007026 * just use the standard high-level event callbacks like
7027 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007028 */
7029 public KeyEvent.DispatcherState getKeyDispatcherState() {
7030 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7031 }
Joe Malin32736f02011-01-19 16:14:20 -08007032
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034 * Dispatch a key event before it is processed by any input method
7035 * associated with the view hierarchy. This can be used to intercept
7036 * key events in special situations before the IME consumes them; a
7037 * typical example would be handling the BACK key to update the application's
7038 * UI instead of allowing the IME to see it and close itself.
7039 *
7040 * @param event The key event to be dispatched.
7041 * @return True if the event was handled, false otherwise.
7042 */
7043 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7044 return onKeyPreIme(event.getKeyCode(), event);
7045 }
7046
7047 /**
7048 * Dispatch a key event to the next view on the focus path. This path runs
7049 * from the top of the view tree down to the currently focused view. If this
7050 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7051 * the next node down the focus path. This method also fires any key
7052 * listeners.
7053 *
7054 * @param event The key event to be dispatched.
7055 * @return True if the event was handled, false otherwise.
7056 */
7057 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007058 if (mInputEventConsistencyVerifier != null) {
7059 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007061
Jeff Brown21bc5c92011-02-28 18:27:14 -08007062 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007063 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007064 ListenerInfo li = mListenerInfo;
7065 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7066 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 return true;
7068 }
7069
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007070 if (event.dispatch(this, mAttachInfo != null
7071 ? mAttachInfo.mKeyDispatchState : null, this)) {
7072 return true;
7073 }
7074
7075 if (mInputEventConsistencyVerifier != null) {
7076 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7077 }
7078 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 }
7080
7081 /**
7082 * Dispatches a key shortcut event.
7083 *
7084 * @param event The key event to be dispatched.
7085 * @return True if the event was handled by the view, false otherwise.
7086 */
7087 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7088 return onKeyShortcut(event.getKeyCode(), event);
7089 }
7090
7091 /**
7092 * Pass the touch screen motion event down to the target view, or this
7093 * view if it is the target.
7094 *
7095 * @param event The motion event to be dispatched.
7096 * @return True if the event was handled by the view, false otherwise.
7097 */
7098 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007099 if (mInputEventConsistencyVerifier != null) {
7100 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7101 }
7102
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007103 if (onFilterTouchEventForSecurity(event)) {
7104 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007105 ListenerInfo li = mListenerInfo;
7106 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7107 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007108 return true;
7109 }
7110
7111 if (onTouchEvent(event)) {
7112 return true;
7113 }
Jeff Brown85a31762010-09-01 17:01:00 -07007114 }
7115
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007116 if (mInputEventConsistencyVerifier != null) {
7117 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007119 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 }
7121
7122 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007123 * Filter the touch event to apply security policies.
7124 *
7125 * @param event The motion event to be filtered.
7126 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007127 *
Jeff Brown85a31762010-09-01 17:01:00 -07007128 * @see #getFilterTouchesWhenObscured
7129 */
7130 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007131 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007132 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7133 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7134 // Window is obscured, drop this touch.
7135 return false;
7136 }
7137 return true;
7138 }
7139
7140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 * Pass a trackball motion event down to the focused view.
7142 *
7143 * @param event The motion event to be dispatched.
7144 * @return True if the event was handled by the view, false otherwise.
7145 */
7146 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007147 if (mInputEventConsistencyVerifier != null) {
7148 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7149 }
7150
Romain Guy02ccac62011-06-24 13:20:23 -07007151 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 }
7153
7154 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007155 * Dispatch a generic motion event.
7156 * <p>
7157 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7158 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007159 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007160 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007161 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007162 *
7163 * @param event The motion event to be dispatched.
7164 * @return True if the event was handled by the view, false otherwise.
7165 */
7166 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007167 if (mInputEventConsistencyVerifier != null) {
7168 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7169 }
7170
Jeff Browna032cc02011-03-07 16:56:21 -08007171 final int source = event.getSource();
7172 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7173 final int action = event.getAction();
7174 if (action == MotionEvent.ACTION_HOVER_ENTER
7175 || action == MotionEvent.ACTION_HOVER_MOVE
7176 || action == MotionEvent.ACTION_HOVER_EXIT) {
7177 if (dispatchHoverEvent(event)) {
7178 return true;
7179 }
7180 } else if (dispatchGenericPointerEvent(event)) {
7181 return true;
7182 }
7183 } else if (dispatchGenericFocusedEvent(event)) {
7184 return true;
7185 }
7186
Jeff Brown10b62902011-06-20 16:40:37 -07007187 if (dispatchGenericMotionEventInternal(event)) {
7188 return true;
7189 }
7190
7191 if (mInputEventConsistencyVerifier != null) {
7192 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7193 }
7194 return false;
7195 }
7196
7197 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007198 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007199 ListenerInfo li = mListenerInfo;
7200 if (li != null && li.mOnGenericMotionListener != null
7201 && (mViewFlags & ENABLED_MASK) == ENABLED
7202 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007203 return true;
7204 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007205
7206 if (onGenericMotionEvent(event)) {
7207 return true;
7208 }
7209
7210 if (mInputEventConsistencyVerifier != null) {
7211 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7212 }
7213 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007214 }
7215
7216 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007217 * Dispatch a hover event.
7218 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007219 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007220 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007221 * </p>
7222 *
7223 * @param event The motion event to be dispatched.
7224 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007225 */
7226 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007227 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007228 ListenerInfo li = mListenerInfo;
7229 if (li != null && li.mOnHoverListener != null
7230 && (mViewFlags & ENABLED_MASK) == ENABLED
7231 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007232 return true;
7233 }
7234
Jeff Browna032cc02011-03-07 16:56:21 -08007235 return onHoverEvent(event);
7236 }
7237
7238 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007239 * Returns true if the view has a child to which it has recently sent
7240 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7241 * it does not have a hovered child, then it must be the innermost hovered view.
7242 * @hide
7243 */
7244 protected boolean hasHoveredChild() {
7245 return false;
7246 }
7247
7248 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007249 * Dispatch a generic motion event to the view under the first pointer.
7250 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007251 * Do not call this method directly.
7252 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007253 * </p>
7254 *
7255 * @param event The motion event to be dispatched.
7256 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007257 */
7258 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7259 return false;
7260 }
7261
7262 /**
7263 * Dispatch a generic motion event to the currently focused view.
7264 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007265 * Do not call this method directly.
7266 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007267 * </p>
7268 *
7269 * @param event The motion event to be dispatched.
7270 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007271 */
7272 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7273 return false;
7274 }
7275
7276 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007277 * Dispatch a pointer event.
7278 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007279 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7280 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7281 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007282 * and should not be expected to handle other pointing device features.
7283 * </p>
7284 *
7285 * @param event The motion event to be dispatched.
7286 * @return True if the event was handled by the view, false otherwise.
7287 * @hide
7288 */
7289 public final boolean dispatchPointerEvent(MotionEvent event) {
7290 if (event.isTouchEvent()) {
7291 return dispatchTouchEvent(event);
7292 } else {
7293 return dispatchGenericMotionEvent(event);
7294 }
7295 }
7296
7297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 * Called when the window containing this view gains or loses window focus.
7299 * ViewGroups should override to route to their children.
7300 *
7301 * @param hasFocus True if the window containing this view now has focus,
7302 * false otherwise.
7303 */
7304 public void dispatchWindowFocusChanged(boolean hasFocus) {
7305 onWindowFocusChanged(hasFocus);
7306 }
7307
7308 /**
7309 * Called when the window containing this view gains or loses focus. Note
7310 * that this is separate from view focus: to receive key events, both
7311 * your view and its window must have focus. If a window is displayed
7312 * on top of yours that takes input focus, then your own window will lose
7313 * focus but the view focus will remain unchanged.
7314 *
7315 * @param hasWindowFocus True if the window containing this view now has
7316 * focus, false otherwise.
7317 */
7318 public void onWindowFocusChanged(boolean hasWindowFocus) {
7319 InputMethodManager imm = InputMethodManager.peekInstance();
7320 if (!hasWindowFocus) {
7321 if (isPressed()) {
7322 setPressed(false);
7323 }
7324 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7325 imm.focusOut(this);
7326 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007327 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007328 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007329 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7331 imm.focusIn(this);
7332 }
7333 refreshDrawableState();
7334 }
7335
7336 /**
7337 * Returns true if this view is in a window that currently has window focus.
7338 * Note that this is not the same as the view itself having focus.
7339 *
7340 * @return True if this view is in a window that currently has window focus.
7341 */
7342 public boolean hasWindowFocus() {
7343 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7344 }
7345
7346 /**
Adam Powell326d8082009-12-09 15:10:07 -08007347 * Dispatch a view visibility change down the view hierarchy.
7348 * ViewGroups should override to route to their children.
7349 * @param changedView The view whose visibility changed. Could be 'this' or
7350 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007351 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7352 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007353 */
7354 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7355 onVisibilityChanged(changedView, visibility);
7356 }
7357
7358 /**
7359 * Called when the visibility of the view or an ancestor of the view is changed.
7360 * @param changedView The view whose visibility changed. Could be 'this' or
7361 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007362 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7363 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007364 */
7365 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007366 if (visibility == VISIBLE) {
7367 if (mAttachInfo != null) {
7368 initialAwakenScrollBars();
7369 } else {
7370 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7371 }
7372 }
Adam Powell326d8082009-12-09 15:10:07 -08007373 }
7374
7375 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007376 * Dispatch a hint about whether this view is displayed. For instance, when
7377 * a View moves out of the screen, it might receives a display hint indicating
7378 * the view is not displayed. Applications should not <em>rely</em> on this hint
7379 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007380 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007381 * @param hint A hint about whether or not this view is displayed:
7382 * {@link #VISIBLE} or {@link #INVISIBLE}.
7383 */
7384 public void dispatchDisplayHint(int hint) {
7385 onDisplayHint(hint);
7386 }
7387
7388 /**
7389 * Gives this view a hint about whether is displayed or not. For instance, when
7390 * a View moves out of the screen, it might receives a display hint indicating
7391 * the view is not displayed. Applications should not <em>rely</em> on this hint
7392 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007393 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007394 * @param hint A hint about whether or not this view is displayed:
7395 * {@link #VISIBLE} or {@link #INVISIBLE}.
7396 */
7397 protected void onDisplayHint(int hint) {
7398 }
7399
7400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007401 * Dispatch a window visibility change down the view hierarchy.
7402 * ViewGroups should override to route to their children.
7403 *
7404 * @param visibility The new visibility of the window.
7405 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007406 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407 */
7408 public void dispatchWindowVisibilityChanged(int visibility) {
7409 onWindowVisibilityChanged(visibility);
7410 }
7411
7412 /**
7413 * Called when the window containing has change its visibility
7414 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7415 * that this tells you whether or not your window is being made visible
7416 * to the window manager; this does <em>not</em> tell you whether or not
7417 * your window is obscured by other windows on the screen, even if it
7418 * is itself visible.
7419 *
7420 * @param visibility The new visibility of the window.
7421 */
7422 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007423 if (visibility == VISIBLE) {
7424 initialAwakenScrollBars();
7425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 }
7427
7428 /**
7429 * Returns the current visibility of the window this view is attached to
7430 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7431 *
7432 * @return Returns the current visibility of the view's window.
7433 */
7434 public int getWindowVisibility() {
7435 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7436 }
7437
7438 /**
7439 * Retrieve the overall visible display size in which the window this view is
7440 * attached to has been positioned in. This takes into account screen
7441 * decorations above the window, for both cases where the window itself
7442 * is being position inside of them or the window is being placed under
7443 * then and covered insets are used for the window to position its content
7444 * inside. In effect, this tells you the available area where content can
7445 * be placed and remain visible to users.
7446 *
7447 * <p>This function requires an IPC back to the window manager to retrieve
7448 * the requested information, so should not be used in performance critical
7449 * code like drawing.
7450 *
7451 * @param outRect Filled in with the visible display frame. If the view
7452 * is not attached to a window, this is simply the raw display size.
7453 */
7454 public void getWindowVisibleDisplayFrame(Rect outRect) {
7455 if (mAttachInfo != null) {
7456 try {
7457 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7458 } catch (RemoteException e) {
7459 return;
7460 }
7461 // XXX This is really broken, and probably all needs to be done
7462 // in the window manager, and we need to know more about whether
7463 // we want the area behind or in front of the IME.
7464 final Rect insets = mAttachInfo.mVisibleInsets;
7465 outRect.left += insets.left;
7466 outRect.top += insets.top;
7467 outRect.right -= insets.right;
7468 outRect.bottom -= insets.bottom;
7469 return;
7470 }
7471 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007472 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 }
7474
7475 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007476 * Dispatch a notification about a resource configuration change down
7477 * the view hierarchy.
7478 * ViewGroups should override to route to their children.
7479 *
7480 * @param newConfig The new resource configuration.
7481 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007482 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007483 */
7484 public void dispatchConfigurationChanged(Configuration newConfig) {
7485 onConfigurationChanged(newConfig);
7486 }
7487
7488 /**
7489 * Called when the current configuration of the resources being used
7490 * by the application have changed. You can use this to decide when
7491 * to reload resources that can changed based on orientation and other
7492 * configuration characterstics. You only need to use this if you are
7493 * not relying on the normal {@link android.app.Activity} mechanism of
7494 * recreating the activity instance upon a configuration change.
7495 *
7496 * @param newConfig The new resource configuration.
7497 */
7498 protected void onConfigurationChanged(Configuration newConfig) {
7499 }
7500
7501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 * Private function to aggregate all per-view attributes in to the view
7503 * root.
7504 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007505 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7506 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 }
7508
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007509 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7510 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007511 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007512 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007513 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007514 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007515 ListenerInfo li = mListenerInfo;
7516 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007517 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 }
7520 }
7521
7522 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007523 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007525 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7526 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 ai.mRecomputeGlobalAttributes = true;
7528 }
7529 }
7530 }
7531
7532 /**
7533 * Returns whether the device is currently in touch mode. Touch mode is entered
7534 * once the user begins interacting with the device by touch, and affects various
7535 * things like whether focus is always visible to the user.
7536 *
7537 * @return Whether the device is in touch mode.
7538 */
7539 @ViewDebug.ExportedProperty
7540 public boolean isInTouchMode() {
7541 if (mAttachInfo != null) {
7542 return mAttachInfo.mInTouchMode;
7543 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007544 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007545 }
7546 }
7547
7548 /**
7549 * Returns the context the view is running in, through which it can
7550 * access the current theme, resources, etc.
7551 *
7552 * @return The view's Context.
7553 */
7554 @ViewDebug.CapturedViewProperty
7555 public final Context getContext() {
7556 return mContext;
7557 }
7558
7559 /**
7560 * Handle a key event before it is processed by any input method
7561 * associated with the view hierarchy. This can be used to intercept
7562 * key events in special situations before the IME consumes them; a
7563 * typical example would be handling the BACK key to update the application's
7564 * UI instead of allowing the IME to see it and close itself.
7565 *
7566 * @param keyCode The value in event.getKeyCode().
7567 * @param event Description of the key event.
7568 * @return If you handled the event, return true. If you want to allow the
7569 * event to be handled by the next receiver, return false.
7570 */
7571 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7572 return false;
7573 }
7574
7575 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007576 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7577 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7579 * is released, if the view is enabled and clickable.
7580 *
Jean Chalard405bc512012-05-29 19:12:34 +09007581 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7582 * although some may elect to do so in some situations. Do not rely on this to
7583 * catch software key presses.
7584 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 * @param keyCode A key code that represents the button pressed, from
7586 * {@link android.view.KeyEvent}.
7587 * @param event The KeyEvent object that defines the button action.
7588 */
7589 public boolean onKeyDown(int keyCode, KeyEvent event) {
7590 boolean result = false;
7591
7592 switch (keyCode) {
7593 case KeyEvent.KEYCODE_DPAD_CENTER:
7594 case KeyEvent.KEYCODE_ENTER: {
7595 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7596 return true;
7597 }
7598 // Long clickable items don't necessarily have to be clickable
7599 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7600 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7601 (event.getRepeatCount() == 0)) {
7602 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007603 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 return true;
7605 }
7606 break;
7607 }
7608 }
7609 return result;
7610 }
7611
7612 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007613 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7614 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7615 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007616 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7617 * although some may elect to do so in some situations. Do not rely on this to
7618 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007619 */
7620 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7621 return false;
7622 }
7623
7624 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007625 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7626 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7628 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007629 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7630 * although some may elect to do so in some situations. Do not rely on this to
7631 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 *
7633 * @param keyCode A key code that represents the button pressed, from
7634 * {@link android.view.KeyEvent}.
7635 * @param event The KeyEvent object that defines the button action.
7636 */
7637 public boolean onKeyUp(int keyCode, KeyEvent event) {
7638 boolean result = false;
7639
7640 switch (keyCode) {
7641 case KeyEvent.KEYCODE_DPAD_CENTER:
7642 case KeyEvent.KEYCODE_ENTER: {
7643 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7644 return true;
7645 }
7646 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7647 setPressed(false);
7648
7649 if (!mHasPerformedLongPress) {
7650 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007651 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007652
7653 result = performClick();
7654 }
7655 }
7656 break;
7657 }
7658 }
7659 return result;
7660 }
7661
7662 /**
7663 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7664 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7665 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007666 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7667 * although some may elect to do so in some situations. Do not rely on this to
7668 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 *
7670 * @param keyCode A key code that represents the button pressed, from
7671 * {@link android.view.KeyEvent}.
7672 * @param repeatCount The number of times the action was made.
7673 * @param event The KeyEvent object that defines the button action.
7674 */
7675 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7676 return false;
7677 }
7678
7679 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007680 * Called on the focused view when a key shortcut event is not handled.
7681 * Override this method to implement local key shortcuts for the View.
7682 * Key shortcuts can also be implemented by setting the
7683 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 *
7685 * @param keyCode The value in event.getKeyCode().
7686 * @param event Description of the key event.
7687 * @return If you handled the event, return true. If you want to allow the
7688 * event to be handled by the next receiver, return false.
7689 */
7690 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7691 return false;
7692 }
7693
7694 /**
7695 * Check whether the called view is a text editor, in which case it
7696 * would make sense to automatically display a soft input window for
7697 * it. Subclasses should override this if they implement
7698 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007699 * a call on that method would return a non-null InputConnection, and
7700 * they are really a first-class editor that the user would normally
7701 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007702 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007703 * <p>The default implementation always returns false. This does
7704 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7705 * will not be called or the user can not otherwise perform edits on your
7706 * view; it is just a hint to the system that this is not the primary
7707 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007708 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 * @return Returns true if this view is a text editor, else false.
7710 */
7711 public boolean onCheckIsTextEditor() {
7712 return false;
7713 }
Romain Guy8506ab42009-06-11 17:35:47 -07007714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 /**
7716 * Create a new InputConnection for an InputMethod to interact
7717 * with the view. The default implementation returns null, since it doesn't
7718 * support input methods. You can override this to implement such support.
7719 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007720 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 * <p>When implementing this, you probably also want to implement
7722 * {@link #onCheckIsTextEditor()} to indicate you will return a
7723 * non-null InputConnection.
7724 *
7725 * @param outAttrs Fill in with attribute information about the connection.
7726 */
7727 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7728 return null;
7729 }
7730
7731 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007732 * Called by the {@link android.view.inputmethod.InputMethodManager}
7733 * when a view who is not the current
7734 * input connection target is trying to make a call on the manager. The
7735 * default implementation returns false; you can override this to return
7736 * true for certain views if you are performing InputConnection proxying
7737 * to them.
7738 * @param view The View that is making the InputMethodManager call.
7739 * @return Return true to allow the call, false to reject.
7740 */
7741 public boolean checkInputConnectionProxy(View view) {
7742 return false;
7743 }
Romain Guy8506ab42009-06-11 17:35:47 -07007744
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 * Show the context menu for this view. It is not safe to hold on to the
7747 * menu after returning from this method.
7748 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007749 * You should normally not overload this method. Overload
7750 * {@link #onCreateContextMenu(ContextMenu)} or define an
7751 * {@link OnCreateContextMenuListener} to add items to the context menu.
7752 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 * @param menu The context menu to populate
7754 */
7755 public void createContextMenu(ContextMenu menu) {
7756 ContextMenuInfo menuInfo = getContextMenuInfo();
7757
7758 // Sets the current menu info so all items added to menu will have
7759 // my extra info set.
7760 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7761
7762 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007763 ListenerInfo li = mListenerInfo;
7764 if (li != null && li.mOnCreateContextMenuListener != null) {
7765 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 }
7767
7768 // Clear the extra information so subsequent items that aren't mine don't
7769 // have my extra info.
7770 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7771
7772 if (mParent != null) {
7773 mParent.createContextMenu(menu);
7774 }
7775 }
7776
7777 /**
7778 * Views should implement this if they have extra information to associate
7779 * with the context menu. The return result is supplied as a parameter to
7780 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7781 * callback.
7782 *
7783 * @return Extra information about the item for which the context menu
7784 * should be shown. This information will vary across different
7785 * subclasses of View.
7786 */
7787 protected ContextMenuInfo getContextMenuInfo() {
7788 return null;
7789 }
7790
7791 /**
7792 * Views should implement this if the view itself is going to add items to
7793 * the context menu.
7794 *
7795 * @param menu the context menu to populate
7796 */
7797 protected void onCreateContextMenu(ContextMenu menu) {
7798 }
7799
7800 /**
7801 * Implement this method to handle trackball motion events. The
7802 * <em>relative</em> movement of the trackball since the last event
7803 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7804 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7805 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7806 * they will often be fractional values, representing the more fine-grained
7807 * movement information available from a trackball).
7808 *
7809 * @param event The motion event.
7810 * @return True if the event was handled, false otherwise.
7811 */
7812 public boolean onTrackballEvent(MotionEvent event) {
7813 return false;
7814 }
7815
7816 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007817 * Implement this method to handle generic motion events.
7818 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007819 * Generic motion events describe joystick movements, mouse hovers, track pad
7820 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007821 * {@link MotionEvent#getSource() source} of the motion event specifies
7822 * the class of input that was received. Implementations of this method
7823 * must examine the bits in the source before processing the event.
7824 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007825 * </p><p>
7826 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7827 * are delivered to the view under the pointer. All other generic motion events are
7828 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007829 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007830 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007831 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007832 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7833 * // process the joystick movement...
7834 * return true;
7835 * }
7836 * }
7837 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7838 * switch (event.getAction()) {
7839 * case MotionEvent.ACTION_HOVER_MOVE:
7840 * // process the mouse hover movement...
7841 * return true;
7842 * case MotionEvent.ACTION_SCROLL:
7843 * // process the scroll wheel movement...
7844 * return true;
7845 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007846 * }
7847 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007848 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007849 *
7850 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007851 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007852 */
7853 public boolean onGenericMotionEvent(MotionEvent event) {
7854 return false;
7855 }
7856
7857 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007858 * Implement this method to handle hover events.
7859 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007860 * This method is called whenever a pointer is hovering into, over, or out of the
7861 * bounds of a view and the view is not currently being touched.
7862 * Hover events are represented as pointer events with action
7863 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7864 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7865 * </p>
7866 * <ul>
7867 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7868 * when the pointer enters the bounds of the view.</li>
7869 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7870 * when the pointer has already entered the bounds of the view and has moved.</li>
7871 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7872 * when the pointer has exited the bounds of the view or when the pointer is
7873 * about to go down due to a button click, tap, or similar user action that
7874 * causes the view to be touched.</li>
7875 * </ul>
7876 * <p>
7877 * The view should implement this method to return true to indicate that it is
7878 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007879 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007880 * The default implementation calls {@link #setHovered} to update the hovered state
7881 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007882 * is enabled and is clickable. The default implementation also sends hover
7883 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007884 * </p>
7885 *
7886 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007887 * @return True if the view handled the hover event.
7888 *
7889 * @see #isHovered
7890 * @see #setHovered
7891 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007892 */
7893 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007894 // The root view may receive hover (or touch) events that are outside the bounds of
7895 // the window. This code ensures that we only send accessibility events for
7896 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007897 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007898 if (!mSendingHoverAccessibilityEvents) {
7899 if ((action == MotionEvent.ACTION_HOVER_ENTER
7900 || action == MotionEvent.ACTION_HOVER_MOVE)
7901 && !hasHoveredChild()
7902 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007903 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007904 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007905 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007906 }
7907 } else {
7908 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007909 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007910 && !pointInView(event.getX(), event.getY()))) {
7911 mSendingHoverAccessibilityEvents = false;
7912 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007913 // If the window does not have input focus we take away accessibility
7914 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007915 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007916 getViewRootImpl().setAccessibilityFocusedHost(null);
7917 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007918 }
Jeff Browna1b24182011-07-28 13:38:24 -07007919 }
7920
Jeff Brown87b7f802011-06-21 18:35:45 -07007921 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007922 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007923 case MotionEvent.ACTION_HOVER_ENTER:
7924 setHovered(true);
7925 break;
7926 case MotionEvent.ACTION_HOVER_EXIT:
7927 setHovered(false);
7928 break;
7929 }
Jeff Browna1b24182011-07-28 13:38:24 -07007930
7931 // Dispatch the event to onGenericMotionEvent before returning true.
7932 // This is to provide compatibility with existing applications that
7933 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7934 // break because of the new default handling for hoverable views
7935 // in onHoverEvent.
7936 // Note that onGenericMotionEvent will be called by default when
7937 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7938 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007939 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007940 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007941
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007942 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007943 }
7944
7945 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007946 * Returns true if the view should handle {@link #onHoverEvent}
7947 * by calling {@link #setHovered} to change its hovered state.
7948 *
7949 * @return True if the view is hoverable.
7950 */
7951 private boolean isHoverable() {
7952 final int viewFlags = mViewFlags;
7953 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7954 return false;
7955 }
7956
7957 return (viewFlags & CLICKABLE) == CLICKABLE
7958 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7959 }
7960
7961 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007962 * Returns true if the view is currently hovered.
7963 *
7964 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007965 *
7966 * @see #setHovered
7967 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007968 */
Jeff Brown10b62902011-06-20 16:40:37 -07007969 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007970 public boolean isHovered() {
7971 return (mPrivateFlags & HOVERED) != 0;
7972 }
7973
7974 /**
7975 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007976 * <p>
7977 * Calling this method also changes the drawable state of the view. This
7978 * enables the view to react to hover by using different drawable resources
7979 * to change its appearance.
7980 * </p><p>
7981 * The {@link #onHoverChanged} method is called when the hovered state changes.
7982 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007983 *
7984 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007985 *
7986 * @see #isHovered
7987 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007988 */
7989 public void setHovered(boolean hovered) {
7990 if (hovered) {
7991 if ((mPrivateFlags & HOVERED) == 0) {
7992 mPrivateFlags |= HOVERED;
7993 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007994 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007995 }
7996 } else {
7997 if ((mPrivateFlags & HOVERED) != 0) {
7998 mPrivateFlags &= ~HOVERED;
7999 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008000 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008001 }
8002 }
8003 }
8004
8005 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008006 * Implement this method to handle hover state changes.
8007 * <p>
8008 * This method is called whenever the hover state changes as a result of a
8009 * call to {@link #setHovered}.
8010 * </p>
8011 *
8012 * @param hovered The current hover state, as returned by {@link #isHovered}.
8013 *
8014 * @see #isHovered
8015 * @see #setHovered
8016 */
8017 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008018 }
8019
8020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 * Implement this method to handle touch screen motion events.
8022 *
8023 * @param event The motion event.
8024 * @return True if the event was handled, false otherwise.
8025 */
8026 public boolean onTouchEvent(MotionEvent event) {
8027 final int viewFlags = mViewFlags;
8028
8029 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008030 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008031 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 // A disabled view that is clickable still consumes the touch
8034 // events, it just doesn't respond to them.
8035 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8036 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8037 }
8038
8039 if (mTouchDelegate != null) {
8040 if (mTouchDelegate.onTouchEvent(event)) {
8041 return true;
8042 }
8043 }
8044
8045 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8046 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8047 switch (event.getAction()) {
8048 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08008049 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
8050 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 // take focus if we don't have it already and we should in
8052 // touch mode.
8053 boolean focusTaken = false;
8054 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8055 focusTaken = requestFocus();
8056 }
8057
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008058 if (prepressed) {
8059 // The button is being released before we actually
8060 // showed it as pressed. Make it show the pressed
8061 // state now (before scheduling the click) to ensure
8062 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008063 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008064 }
Joe Malin32736f02011-01-19 16:14:20 -08008065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 if (!mHasPerformedLongPress) {
8067 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008068 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069
8070 // Only perform take click actions if we were in the pressed state
8071 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008072 // Use a Runnable and post this rather than calling
8073 // performClick directly. This lets other visual state
8074 // of the view update before click actions start.
8075 if (mPerformClick == null) {
8076 mPerformClick = new PerformClick();
8077 }
8078 if (!post(mPerformClick)) {
8079 performClick();
8080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 }
8082 }
8083
8084 if (mUnsetPressedState == null) {
8085 mUnsetPressedState = new UnsetPressedState();
8086 }
8087
Adam Powelle14579b2009-12-16 18:39:52 -08008088 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008089 postDelayed(mUnsetPressedState,
8090 ViewConfiguration.getPressedStateDuration());
8091 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 // If the post failed, unpress right now
8093 mUnsetPressedState.run();
8094 }
Adam Powelle14579b2009-12-16 18:39:52 -08008095 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 }
8097 break;
8098
8099 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008100 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008101
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008102 if (performButtonActionOnTouchDown(event)) {
8103 break;
8104 }
8105
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008106 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008107 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008108
8109 // For views inside a scrolling container, delay the pressed feedback for
8110 // a short period in case this is a scroll.
8111 if (isInScrollingContainer) {
8112 mPrivateFlags |= PREPRESSED;
8113 if (mPendingCheckForTap == null) {
8114 mPendingCheckForTap = new CheckForTap();
8115 }
8116 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8117 } else {
8118 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008119 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008120 checkForLongClick(0);
8121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 break;
8123
8124 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008125 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008126 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 break;
8128
8129 case MotionEvent.ACTION_MOVE:
8130 final int x = (int) event.getX();
8131 final int y = (int) event.getY();
8132
8133 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008134 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008136 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008138 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008139 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140
Adam Powell4d6f0662012-02-21 15:11:11 -08008141 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 }
8144 break;
8145 }
8146 return true;
8147 }
8148
8149 return false;
8150 }
8151
8152 /**
Adam Powell10298662011-08-14 18:26:30 -07008153 * @hide
8154 */
8155 public boolean isInScrollingContainer() {
8156 ViewParent p = getParent();
8157 while (p != null && p instanceof ViewGroup) {
8158 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8159 return true;
8160 }
8161 p = p.getParent();
8162 }
8163 return false;
8164 }
8165
8166 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008167 * Remove the longpress detection timer.
8168 */
8169 private void removeLongPressCallback() {
8170 if (mPendingCheckForLongPress != null) {
8171 removeCallbacks(mPendingCheckForLongPress);
8172 }
8173 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008174
8175 /**
8176 * Remove the pending click action
8177 */
8178 private void removePerformClickCallback() {
8179 if (mPerformClick != null) {
8180 removeCallbacks(mPerformClick);
8181 }
8182 }
8183
Adam Powelle14579b2009-12-16 18:39:52 -08008184 /**
Romain Guya440b002010-02-24 15:57:54 -08008185 * Remove the prepress detection timer.
8186 */
8187 private void removeUnsetPressCallback() {
8188 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8189 setPressed(false);
8190 removeCallbacks(mUnsetPressedState);
8191 }
8192 }
8193
8194 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008195 * Remove the tap detection timer.
8196 */
8197 private void removeTapCallback() {
8198 if (mPendingCheckForTap != null) {
8199 mPrivateFlags &= ~PREPRESSED;
8200 removeCallbacks(mPendingCheckForTap);
8201 }
8202 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008203
8204 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008205 * Cancels a pending long press. Your subclass can use this if you
8206 * want the context menu to come up if the user presses and holds
8207 * at the same place, but you don't want it to come up if they press
8208 * and then move around enough to cause scrolling.
8209 */
8210 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008211 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008212
8213 /*
8214 * The prepressed state handled by the tap callback is a display
8215 * construct, but the tap callback will post a long press callback
8216 * less its own timeout. Remove it here.
8217 */
8218 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 }
8220
8221 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008222 * Remove the pending callback for sending a
8223 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8224 */
8225 private void removeSendViewScrolledAccessibilityEventCallback() {
8226 if (mSendViewScrolledAccessibilityEvent != null) {
8227 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008228 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008229 }
8230 }
8231
8232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 * Sets the TouchDelegate for this View.
8234 */
8235 public void setTouchDelegate(TouchDelegate delegate) {
8236 mTouchDelegate = delegate;
8237 }
8238
8239 /**
8240 * Gets the TouchDelegate for this View.
8241 */
8242 public TouchDelegate getTouchDelegate() {
8243 return mTouchDelegate;
8244 }
8245
8246 /**
8247 * Set flags controlling behavior of this view.
8248 *
8249 * @param flags Constant indicating the value which should be set
8250 * @param mask Constant indicating the bit range that should be changed
8251 */
8252 void setFlags(int flags, int mask) {
8253 int old = mViewFlags;
8254 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8255
8256 int changed = mViewFlags ^ old;
8257 if (changed == 0) {
8258 return;
8259 }
8260 int privateFlags = mPrivateFlags;
8261
8262 /* Check if the FOCUSABLE bit has changed */
8263 if (((changed & FOCUSABLE_MASK) != 0) &&
8264 ((privateFlags & HAS_BOUNDS) !=0)) {
8265 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8266 && ((privateFlags & FOCUSED) != 0)) {
8267 /* Give up focus if we are no longer focusable */
8268 clearFocus();
8269 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8270 && ((privateFlags & FOCUSED) == 0)) {
8271 /*
8272 * Tell the view system that we are now available to take focus
8273 * if no one else already has it.
8274 */
8275 if (mParent != null) mParent.focusableViewAvailable(this);
8276 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008277 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8278 notifyAccessibilityStateChanged();
8279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 }
8281
8282 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8283 if ((changed & VISIBILITY_MASK) != 0) {
8284 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008285 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008286 * it was not visible. Marking it drawn ensures that the invalidation will
8287 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008289 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008290 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291
8292 needGlobalAttributesUpdate(true);
8293
8294 // a view becoming visible is worth notifying the parent
8295 // about in case nothing has focus. even if this specific view
8296 // isn't focusable, it may contain something that is, so let
8297 // the root view try to give this focus if nothing else does.
8298 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8299 mParent.focusableViewAvailable(this);
8300 }
8301 }
8302 }
8303
8304 /* Check if the GONE bit has changed */
8305 if ((changed & GONE) != 0) {
8306 needGlobalAttributesUpdate(false);
8307 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308
Romain Guyecd80ee2009-12-03 17:13:02 -08008309 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8310 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008311 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008312 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008313 if (mParent instanceof View) {
8314 // GONE views noop invalidation, so invalidate the parent
8315 ((View) mParent).invalidate(true);
8316 }
8317 // Mark the view drawn to ensure that it gets invalidated properly the next
8318 // time it is visible and gets invalidated
8319 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 }
8321 if (mAttachInfo != null) {
8322 mAttachInfo.mViewVisibilityChanged = true;
8323 }
8324 }
8325
8326 /* Check if the VISIBLE bit has changed */
8327 if ((changed & INVISIBLE) != 0) {
8328 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008329 /*
8330 * If this view is becoming invisible, set the DRAWN flag so that
8331 * the next invalidate() will not be skipped.
8332 */
8333 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334
8335 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008336 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 if (getRootView() != this) {
8338 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008339 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 }
8341 }
8342 if (mAttachInfo != null) {
8343 mAttachInfo.mViewVisibilityChanged = true;
8344 }
8345 }
8346
Adam Powell326d8082009-12-09 15:10:07 -08008347 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008348 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008349 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8350 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008351 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008352 } else if (mParent != null) {
8353 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008354 }
Adam Powell326d8082009-12-09 15:10:07 -08008355 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8356 }
8357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8359 destroyDrawingCache();
8360 }
8361
8362 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8363 destroyDrawingCache();
8364 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008365 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 }
8367
8368 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8369 destroyDrawingCache();
8370 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8371 }
8372
8373 if ((changed & DRAW_MASK) != 0) {
8374 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008375 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 mPrivateFlags &= ~SKIP_DRAW;
8377 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8378 } else {
8379 mPrivateFlags |= SKIP_DRAW;
8380 }
8381 } else {
8382 mPrivateFlags &= ~SKIP_DRAW;
8383 }
8384 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008385 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 }
8387
8388 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008389 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 mParent.recomputeViewAttributes(this);
8391 }
8392 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008393
8394 if (AccessibilityManager.getInstance(mContext).isEnabled()
8395 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8396 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8397 notifyAccessibilityStateChanged();
8398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 }
8400
8401 /**
8402 * Change the view's z order in the tree, so it's on top of other sibling
8403 * views
8404 */
8405 public void bringToFront() {
8406 if (mParent != null) {
8407 mParent.bringChildToFront(this);
8408 }
8409 }
8410
8411 /**
8412 * This is called in response to an internal scroll in this view (i.e., the
8413 * view scrolled its own contents). This is typically as a result of
8414 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8415 * called.
8416 *
8417 * @param l Current horizontal scroll origin.
8418 * @param t Current vertical scroll origin.
8419 * @param oldl Previous horizontal scroll origin.
8420 * @param oldt Previous vertical scroll origin.
8421 */
8422 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008423 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8424 postSendViewScrolledAccessibilityEventCallback();
8425 }
8426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 mBackgroundSizeChanged = true;
8428
8429 final AttachInfo ai = mAttachInfo;
8430 if (ai != null) {
8431 ai.mViewScrollChanged = true;
8432 }
8433 }
8434
8435 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008436 * Interface definition for a callback to be invoked when the layout bounds of a view
8437 * changes due to layout processing.
8438 */
8439 public interface OnLayoutChangeListener {
8440 /**
8441 * Called when the focus state of a view has changed.
8442 *
8443 * @param v The view whose state has changed.
8444 * @param left The new value of the view's left property.
8445 * @param top The new value of the view's top property.
8446 * @param right The new value of the view's right property.
8447 * @param bottom The new value of the view's bottom property.
8448 * @param oldLeft The previous value of the view's left property.
8449 * @param oldTop The previous value of the view's top property.
8450 * @param oldRight The previous value of the view's right property.
8451 * @param oldBottom The previous value of the view's bottom property.
8452 */
8453 void onLayoutChange(View v, int left, int top, int right, int bottom,
8454 int oldLeft, int oldTop, int oldRight, int oldBottom);
8455 }
8456
8457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 * This is called during layout when the size of this view has changed. If
8459 * you were just added to the view hierarchy, you're called with the old
8460 * values of 0.
8461 *
8462 * @param w Current width of this view.
8463 * @param h Current height of this view.
8464 * @param oldw Old width of this view.
8465 * @param oldh Old height of this view.
8466 */
8467 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8468 }
8469
8470 /**
8471 * Called by draw to draw the child views. This may be overridden
8472 * by derived classes to gain control just before its children are drawn
8473 * (but after its own view has been drawn).
8474 * @param canvas the canvas on which to draw the view
8475 */
8476 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 }
8479
8480 /**
8481 * Gets the parent of this view. Note that the parent is a
8482 * ViewParent and not necessarily a View.
8483 *
8484 * @return Parent of this view.
8485 */
8486 public final ViewParent getParent() {
8487 return mParent;
8488 }
8489
8490 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008491 * Set the horizontal scrolled position of your view. This will cause a call to
8492 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8493 * invalidated.
8494 * @param value the x position to scroll to
8495 */
8496 public void setScrollX(int value) {
8497 scrollTo(value, mScrollY);
8498 }
8499
8500 /**
8501 * Set the vertical scrolled position of your view. This will cause a call to
8502 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8503 * invalidated.
8504 * @param value the y position to scroll to
8505 */
8506 public void setScrollY(int value) {
8507 scrollTo(mScrollX, value);
8508 }
8509
8510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 * Return the scrolled left position of this view. This is the left edge of
8512 * the displayed part of your view. You do not need to draw any pixels
8513 * farther left, since those are outside of the frame of your view on
8514 * screen.
8515 *
8516 * @return The left edge of the displayed part of your view, in pixels.
8517 */
8518 public final int getScrollX() {
8519 return mScrollX;
8520 }
8521
8522 /**
8523 * Return the scrolled top position of this view. This is the top edge of
8524 * the displayed part of your view. You do not need to draw any pixels above
8525 * it, since those are outside of the frame of your view on screen.
8526 *
8527 * @return The top edge of the displayed part of your view, in pixels.
8528 */
8529 public final int getScrollY() {
8530 return mScrollY;
8531 }
8532
8533 /**
8534 * Return the width of the your view.
8535 *
8536 * @return The width of your view, in pixels.
8537 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008538 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 public final int getWidth() {
8540 return mRight - mLeft;
8541 }
8542
8543 /**
8544 * Return the height of your view.
8545 *
8546 * @return The height of your view, in pixels.
8547 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008548 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 public final int getHeight() {
8550 return mBottom - mTop;
8551 }
8552
8553 /**
8554 * Return the visible drawing bounds of your view. Fills in the output
8555 * rectangle with the values from getScrollX(), getScrollY(),
8556 * getWidth(), and getHeight().
8557 *
8558 * @param outRect The (scrolled) drawing bounds of the view.
8559 */
8560 public void getDrawingRect(Rect outRect) {
8561 outRect.left = mScrollX;
8562 outRect.top = mScrollY;
8563 outRect.right = mScrollX + (mRight - mLeft);
8564 outRect.bottom = mScrollY + (mBottom - mTop);
8565 }
8566
8567 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008568 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8569 * raw width component (that is the result is masked by
8570 * {@link #MEASURED_SIZE_MASK}).
8571 *
8572 * @return The raw measured width of this view.
8573 */
8574 public final int getMeasuredWidth() {
8575 return mMeasuredWidth & MEASURED_SIZE_MASK;
8576 }
8577
8578 /**
8579 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008580 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008581 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 * This should be used during measurement and layout calculations only. Use
8583 * {@link #getWidth()} to see how wide a view is after layout.
8584 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008585 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008587 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 return mMeasuredWidth;
8589 }
8590
8591 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008592 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8593 * raw width component (that is the result is masked by
8594 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008596 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 */
8598 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008599 return mMeasuredHeight & MEASURED_SIZE_MASK;
8600 }
8601
8602 /**
8603 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008604 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008605 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8606 * This should be used during measurement and layout calculations only. Use
8607 * {@link #getHeight()} to see how wide a view is after layout.
8608 *
8609 * @return The measured width of this view as a bit mask.
8610 */
8611 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 return mMeasuredHeight;
8613 }
8614
8615 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008616 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8617 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8618 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8619 * and the height component is at the shifted bits
8620 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8621 */
8622 public final int getMeasuredState() {
8623 return (mMeasuredWidth&MEASURED_STATE_MASK)
8624 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8625 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8626 }
8627
8628 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008629 * The transform matrix of this view, which is calculated based on the current
8630 * roation, scale, and pivot properties.
8631 *
8632 * @see #getRotation()
8633 * @see #getScaleX()
8634 * @see #getScaleY()
8635 * @see #getPivotX()
8636 * @see #getPivotY()
8637 * @return The current transform matrix for the view
8638 */
8639 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008640 if (mTransformationInfo != null) {
8641 updateMatrix();
8642 return mTransformationInfo.mMatrix;
8643 }
8644 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008645 }
8646
8647 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008648 * Utility function to determine if the value is far enough away from zero to be
8649 * considered non-zero.
8650 * @param value A floating point value to check for zero-ness
8651 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8652 */
8653 private static boolean nonzero(float value) {
8654 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8655 }
8656
8657 /**
Jeff Brown86671742010-09-30 20:00:15 -07008658 * Returns true if the transform matrix is the identity matrix.
8659 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008660 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008661 * @return True if the transform matrix is the identity matrix, false otherwise.
8662 */
Jeff Brown86671742010-09-30 20:00:15 -07008663 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008664 if (mTransformationInfo != null) {
8665 updateMatrix();
8666 return mTransformationInfo.mMatrixIsIdentity;
8667 }
8668 return true;
8669 }
8670
8671 void ensureTransformationInfo() {
8672 if (mTransformationInfo == null) {
8673 mTransformationInfo = new TransformationInfo();
8674 }
Jeff Brown86671742010-09-30 20:00:15 -07008675 }
8676
8677 /**
8678 * Recomputes the transform matrix if necessary.
8679 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008680 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008681 final TransformationInfo info = mTransformationInfo;
8682 if (info == null) {
8683 return;
8684 }
8685 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008686 // transform-related properties have changed since the last time someone
8687 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008688
8689 // Figure out if we need to update the pivot point
8690 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008691 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8692 info.mPrevWidth = mRight - mLeft;
8693 info.mPrevHeight = mBottom - mTop;
8694 info.mPivotX = info.mPrevWidth / 2f;
8695 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008696 }
8697 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008698 info.mMatrix.reset();
8699 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8700 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8701 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8702 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008703 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008704 if (info.mCamera == null) {
8705 info.mCamera = new Camera();
8706 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008707 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008708 info.mCamera.save();
8709 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8710 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8711 info.mCamera.getMatrix(info.matrix3D);
8712 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8713 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8714 info.mPivotY + info.mTranslationY);
8715 info.mMatrix.postConcat(info.matrix3D);
8716 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008717 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008718 info.mMatrixDirty = false;
8719 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8720 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008721 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008722 }
8723
8724 /**
8725 * Utility method to retrieve the inverse of the current mMatrix property.
8726 * We cache the matrix to avoid recalculating it when transform properties
8727 * have not changed.
8728 *
8729 * @return The inverse of the current matrix of this view.
8730 */
Jeff Brown86671742010-09-30 20:00:15 -07008731 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008732 final TransformationInfo info = mTransformationInfo;
8733 if (info != null) {
8734 updateMatrix();
8735 if (info.mInverseMatrixDirty) {
8736 if (info.mInverseMatrix == null) {
8737 info.mInverseMatrix = new Matrix();
8738 }
8739 info.mMatrix.invert(info.mInverseMatrix);
8740 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008741 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008742 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008743 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008744 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008745 }
8746
8747 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008748 * Gets the distance along the Z axis from the camera to this view.
8749 *
8750 * @see #setCameraDistance(float)
8751 *
8752 * @return The distance along the Z axis.
8753 */
8754 public float getCameraDistance() {
8755 ensureTransformationInfo();
8756 final float dpi = mResources.getDisplayMetrics().densityDpi;
8757 final TransformationInfo info = mTransformationInfo;
8758 if (info.mCamera == null) {
8759 info.mCamera = new Camera();
8760 info.matrix3D = new Matrix();
8761 }
8762 return -(info.mCamera.getLocationZ() * dpi);
8763 }
8764
8765 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008766 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8767 * views are drawn) from the camera to this view. The camera's distance
8768 * affects 3D transformations, for instance rotations around the X and Y
8769 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008770 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008771 * use a camera distance that's greater than the height (X axis rotation) or
8772 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008773 *
Romain Guya5364ee2011-02-24 14:46:04 -08008774 * <p>The distance of the camera from the view plane can have an affect on the
8775 * perspective distortion of the view when it is rotated around the x or y axis.
8776 * For example, a large distance will result in a large viewing angle, and there
8777 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008778 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008779 * also result in some drawing artifacts if the rotated view ends up partially
8780 * behind the camera (which is why the recommendation is to use a distance at
8781 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008782 *
Romain Guya5364ee2011-02-24 14:46:04 -08008783 * <p>The distance is expressed in "depth pixels." The default distance depends
8784 * on the screen density. For instance, on a medium density display, the
8785 * default distance is 1280. On a high density display, the default distance
8786 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008787 *
Romain Guya5364ee2011-02-24 14:46:04 -08008788 * <p>If you want to specify a distance that leads to visually consistent
8789 * results across various densities, use the following formula:</p>
8790 * <pre>
8791 * float scale = context.getResources().getDisplayMetrics().density;
8792 * view.setCameraDistance(distance * scale);
8793 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008794 *
Romain Guya5364ee2011-02-24 14:46:04 -08008795 * <p>The density scale factor of a high density display is 1.5,
8796 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008797 *
Romain Guya5364ee2011-02-24 14:46:04 -08008798 * @param distance The distance in "depth pixels", if negative the opposite
8799 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008800 *
8801 * @see #setRotationX(float)
8802 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008803 */
8804 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008805 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008806
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008807 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008808 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008809 final TransformationInfo info = mTransformationInfo;
8810 if (info.mCamera == null) {
8811 info.mCamera = new Camera();
8812 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008813 }
8814
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8816 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008817
Chet Haase9d1992d2012-03-13 11:03:25 -07008818 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008819 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008820 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008821 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008822 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8823 // View was rejected last time it was drawn by its parent; this may have changed
8824 invalidateParentIfNeeded();
8825 }
Romain Guya5364ee2011-02-24 14:46:04 -08008826 }
8827
8828 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008829 * The degrees that the view is rotated around the pivot point.
8830 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008831 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008832 * @see #getPivotX()
8833 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008834 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008835 * @return The degrees of rotation.
8836 */
Chet Haasea5531132012-02-02 13:41:44 -08008837 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008838 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008839 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 }
8841
8842 /**
Chet Haase897247b2010-09-09 14:54:47 -07008843 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8844 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008845 *
8846 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008847 *
8848 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008849 * @see #getPivotX()
8850 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008851 * @see #setRotationX(float)
8852 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008853 *
8854 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008855 */
8856 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008857 ensureTransformationInfo();
8858 final TransformationInfo info = mTransformationInfo;
8859 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008860 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008861 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008862 info.mRotation = rotation;
8863 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008864 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008865 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008866 mDisplayList.setRotation(rotation);
8867 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008868 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8869 // View was rejected last time it was drawn by its parent; this may have changed
8870 invalidateParentIfNeeded();
8871 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008872 }
8873 }
8874
8875 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008876 * The degrees that the view is rotated around the vertical axis through the pivot point.
8877 *
8878 * @see #getPivotX()
8879 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008880 * @see #setRotationY(float)
8881 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008882 * @return The degrees of Y rotation.
8883 */
Chet Haasea5531132012-02-02 13:41:44 -08008884 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008885 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008886 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008887 }
8888
8889 /**
Chet Haase897247b2010-09-09 14:54:47 -07008890 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8891 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8892 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008893 *
Romain Guya5364ee2011-02-24 14:46:04 -08008894 * When rotating large views, it is recommended to adjust the camera distance
8895 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008896 *
8897 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008898 *
8899 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008900 * @see #getPivotX()
8901 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008902 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008903 * @see #setRotationX(float)
8904 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008905 *
8906 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008907 */
8908 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008909 ensureTransformationInfo();
8910 final TransformationInfo info = mTransformationInfo;
8911 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008912 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008913 info.mRotationY = rotationY;
8914 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008915 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008916 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008917 mDisplayList.setRotationY(rotationY);
8918 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008919 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8920 // View was rejected last time it was drawn by its parent; this may have changed
8921 invalidateParentIfNeeded();
8922 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008923 }
8924 }
8925
8926 /**
8927 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8928 *
8929 * @see #getPivotX()
8930 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008931 * @see #setRotationX(float)
8932 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008933 * @return The degrees of X rotation.
8934 */
Chet Haasea5531132012-02-02 13:41:44 -08008935 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008936 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008937 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008938 }
8939
8940 /**
Chet Haase897247b2010-09-09 14:54:47 -07008941 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8942 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8943 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008944 *
Romain Guya5364ee2011-02-24 14:46:04 -08008945 * When rotating large views, it is recommended to adjust the camera distance
8946 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008947 *
8948 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008949 *
8950 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008951 * @see #getPivotX()
8952 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008953 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008954 * @see #setRotationY(float)
8955 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008956 *
8957 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008958 */
8959 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008960 ensureTransformationInfo();
8961 final TransformationInfo info = mTransformationInfo;
8962 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008963 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008964 info.mRotationX = rotationX;
8965 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008966 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008967 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008968 mDisplayList.setRotationX(rotationX);
8969 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008970 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8971 // View was rejected last time it was drawn by its parent; this may have changed
8972 invalidateParentIfNeeded();
8973 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008974 }
8975 }
8976
8977 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008978 * The amount that the view is scaled in x around the pivot point, as a proportion of
8979 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8980 *
Joe Onorato93162322010-09-16 15:42:01 -04008981 * <p>By default, this is 1.0f.
8982 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008983 * @see #getPivotX()
8984 * @see #getPivotY()
8985 * @return The scaling factor.
8986 */
Chet Haasea5531132012-02-02 13:41:44 -08008987 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008988 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008989 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008990 }
8991
8992 /**
8993 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8994 * the view's unscaled width. A value of 1 means that no scaling is applied.
8995 *
8996 * @param scaleX The scaling factor.
8997 * @see #getPivotX()
8998 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008999 *
9000 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009001 */
9002 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009003 ensureTransformationInfo();
9004 final TransformationInfo info = mTransformationInfo;
9005 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009006 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009007 info.mScaleX = scaleX;
9008 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009009 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009010 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009011 mDisplayList.setScaleX(scaleX);
9012 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009013 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9014 // View was rejected last time it was drawn by its parent; this may have changed
9015 invalidateParentIfNeeded();
9016 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 }
9018 }
9019
9020 /**
9021 * The amount that the view is scaled in y around the pivot point, as a proportion of
9022 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9023 *
Joe Onorato93162322010-09-16 15:42:01 -04009024 * <p>By default, this is 1.0f.
9025 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009026 * @see #getPivotX()
9027 * @see #getPivotY()
9028 * @return The scaling factor.
9029 */
Chet Haasea5531132012-02-02 13:41:44 -08009030 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009031 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009032 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009033 }
9034
9035 /**
9036 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9037 * the view's unscaled width. A value of 1 means that no scaling is applied.
9038 *
9039 * @param scaleY The scaling factor.
9040 * @see #getPivotX()
9041 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009042 *
9043 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009044 */
9045 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009046 ensureTransformationInfo();
9047 final TransformationInfo info = mTransformationInfo;
9048 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009049 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009050 info.mScaleY = scaleY;
9051 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009052 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009053 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009054 mDisplayList.setScaleY(scaleY);
9055 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009056 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9057 // View was rejected last time it was drawn by its parent; this may have changed
9058 invalidateParentIfNeeded();
9059 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009060 }
9061 }
9062
9063 /**
9064 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9065 * and {@link #setScaleX(float) scaled}.
9066 *
9067 * @see #getRotation()
9068 * @see #getScaleX()
9069 * @see #getScaleY()
9070 * @see #getPivotY()
9071 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009072 *
9073 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009074 */
Chet Haasea5531132012-02-02 13:41:44 -08009075 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009076 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009077 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009078 }
9079
9080 /**
9081 * Sets the x location of the point around which the view is
9082 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009083 * By default, the pivot point is centered on the object.
9084 * Setting this property disables this behavior and causes the view to use only the
9085 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009086 *
9087 * @param pivotX The x location of the pivot point.
9088 * @see #getRotation()
9089 * @see #getScaleX()
9090 * @see #getScaleY()
9091 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009092 *
9093 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009094 */
9095 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009096 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009097 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009098 final TransformationInfo info = mTransformationInfo;
9099 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009100 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009101 info.mPivotX = pivotX;
9102 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009103 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009104 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009105 mDisplayList.setPivotX(pivotX);
9106 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009107 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9108 // View was rejected last time it was drawn by its parent; this may have changed
9109 invalidateParentIfNeeded();
9110 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009111 }
9112 }
9113
9114 /**
9115 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9116 * and {@link #setScaleY(float) scaled}.
9117 *
9118 * @see #getRotation()
9119 * @see #getScaleX()
9120 * @see #getScaleY()
9121 * @see #getPivotY()
9122 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009123 *
9124 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009125 */
Chet Haasea5531132012-02-02 13:41:44 -08009126 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009127 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009128 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009129 }
9130
9131 /**
9132 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009133 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9134 * Setting this property disables this behavior and causes the view to use only the
9135 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009136 *
9137 * @param pivotY The y location of the pivot point.
9138 * @see #getRotation()
9139 * @see #getScaleX()
9140 * @see #getScaleY()
9141 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009142 *
9143 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009144 */
9145 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009146 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009147 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009148 final TransformationInfo info = mTransformationInfo;
9149 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009150 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009151 info.mPivotY = pivotY;
9152 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009153 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009154 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009155 mDisplayList.setPivotY(pivotY);
9156 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009157 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9158 // View was rejected last time it was drawn by its parent; this may have changed
9159 invalidateParentIfNeeded();
9160 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009161 }
9162 }
9163
9164 /**
9165 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9166 * completely transparent and 1 means the view is completely opaque.
9167 *
Joe Onorato93162322010-09-16 15:42:01 -04009168 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009169 * @return The opacity of the view.
9170 */
Chet Haasea5531132012-02-02 13:41:44 -08009171 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009172 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009173 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 }
9175
9176 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009177 * Returns whether this View has content which overlaps. This function, intended to be
9178 * overridden by specific View types, is an optimization when alpha is set on a view. If
9179 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9180 * and then composited it into place, which can be expensive. If the view has no overlapping
9181 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9182 * An example of overlapping rendering is a TextView with a background image, such as a
9183 * Button. An example of non-overlapping rendering is a TextView with no background, or
9184 * an ImageView with only the foreground image. The default implementation returns true;
9185 * subclasses should override if they have cases which can be optimized.
9186 *
9187 * @return true if the content in this view might overlap, false otherwise.
9188 */
9189 public boolean hasOverlappingRendering() {
9190 return true;
9191 }
9192
9193 /**
Romain Guy171c5922011-01-06 10:04:23 -08009194 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9195 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009196 *
Romain Guy171c5922011-01-06 10:04:23 -08009197 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9198 * responsible for applying the opacity itself. Otherwise, calling this method is
9199 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009200 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009201 *
Chet Haasea5531132012-02-02 13:41:44 -08009202 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9203 * performance implications. It is generally best to use the alpha property sparingly and
9204 * transiently, as in the case of fading animations.</p>
9205 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009206 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009207 *
Joe Malin32736f02011-01-19 16:14:20 -08009208 * @see #setLayerType(int, android.graphics.Paint)
9209 *
Chet Haase73066682010-11-29 15:55:32 -08009210 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009211 */
9212 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009213 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009214 if (mTransformationInfo.mAlpha != alpha) {
9215 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009216 if (onSetAlpha((int) (alpha * 255))) {
9217 mPrivateFlags |= ALPHA_SET;
9218 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009219 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009220 invalidate(true);
9221 } else {
9222 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009223 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009224 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009225 mDisplayList.setAlpha(alpha);
9226 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009227 }
Chet Haaseed032702010-10-01 14:05:54 -07009228 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009229 }
9230
9231 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009232 * Faster version of setAlpha() which performs the same steps except there are
9233 * no calls to invalidate(). The caller of this function should perform proper invalidation
9234 * on the parent and this object. The return value indicates whether the subclass handles
9235 * alpha (the return value for onSetAlpha()).
9236 *
9237 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009238 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9239 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009240 */
9241 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009242 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009243 if (mTransformationInfo.mAlpha != alpha) {
9244 mTransformationInfo.mAlpha = alpha;
9245 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9246 if (subclassHandlesAlpha) {
9247 mPrivateFlags |= ALPHA_SET;
9248 return true;
9249 } else {
9250 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009251 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009252 mDisplayList.setAlpha(alpha);
9253 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009254 }
Chet Haasea00f3862011-02-22 06:34:40 -08009255 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009256 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009257 }
9258
9259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 * Top position of this view relative to its parent.
9261 *
9262 * @return The top of this view, in pixels.
9263 */
9264 @ViewDebug.CapturedViewProperty
9265 public final int getTop() {
9266 return mTop;
9267 }
9268
9269 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009270 * Sets the top position of this view relative to its parent. This method is meant to be called
9271 * by the layout system and should not generally be called otherwise, because the property
9272 * may be changed at any time by the layout.
9273 *
9274 * @param top The top of this view, in pixels.
9275 */
9276 public final void setTop(int top) {
9277 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009278 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009279 final boolean matrixIsIdentity = mTransformationInfo == null
9280 || mTransformationInfo.mMatrixIsIdentity;
9281 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009282 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009283 int minTop;
9284 int yLoc;
9285 if (top < mTop) {
9286 minTop = top;
9287 yLoc = top - mTop;
9288 } else {
9289 minTop = mTop;
9290 yLoc = 0;
9291 }
Chet Haasee9140a72011-02-16 16:23:29 -08009292 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009293 }
9294 } else {
9295 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009296 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009297 }
9298
Chet Haaseed032702010-10-01 14:05:54 -07009299 int width = mRight - mLeft;
9300 int oldHeight = mBottom - mTop;
9301
Chet Haase21cd1382010-09-01 17:42:29 -07009302 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009303 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009304 mDisplayList.setTop(mTop);
9305 }
Chet Haase21cd1382010-09-01 17:42:29 -07009306
Chet Haaseed032702010-10-01 14:05:54 -07009307 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9308
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009309 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009310 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9311 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009312 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009313 }
Chet Haase21cd1382010-09-01 17:42:29 -07009314 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009315 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009316 }
Chet Haase55dbb652010-12-21 20:15:08 -08009317 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009318 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009319 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9320 // View was rejected last time it was drawn by its parent; this may have changed
9321 invalidateParentIfNeeded();
9322 }
Chet Haase21cd1382010-09-01 17:42:29 -07009323 }
9324 }
9325
9326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 * Bottom position of this view relative to its parent.
9328 *
9329 * @return The bottom of this view, in pixels.
9330 */
9331 @ViewDebug.CapturedViewProperty
9332 public final int getBottom() {
9333 return mBottom;
9334 }
9335
9336 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009337 * True if this view has changed since the last time being drawn.
9338 *
9339 * @return The dirty state of this view.
9340 */
9341 public boolean isDirty() {
9342 return (mPrivateFlags & DIRTY_MASK) != 0;
9343 }
9344
9345 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009346 * Sets the bottom position of this view relative to its parent. This method is meant to be
9347 * called by the layout system and should not generally be called otherwise, because the
9348 * property may be changed at any time by the layout.
9349 *
9350 * @param bottom The bottom of this view, in pixels.
9351 */
9352 public final void setBottom(int bottom) {
9353 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009354 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009355 final boolean matrixIsIdentity = mTransformationInfo == null
9356 || mTransformationInfo.mMatrixIsIdentity;
9357 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009358 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009359 int maxBottom;
9360 if (bottom < mBottom) {
9361 maxBottom = mBottom;
9362 } else {
9363 maxBottom = bottom;
9364 }
Chet Haasee9140a72011-02-16 16:23:29 -08009365 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009366 }
9367 } else {
9368 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009369 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009370 }
9371
Chet Haaseed032702010-10-01 14:05:54 -07009372 int width = mRight - mLeft;
9373 int oldHeight = mBottom - mTop;
9374
Chet Haase21cd1382010-09-01 17:42:29 -07009375 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009376 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009377 mDisplayList.setBottom(mBottom);
9378 }
Chet Haase21cd1382010-09-01 17:42:29 -07009379
Chet Haaseed032702010-10-01 14:05:54 -07009380 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9381
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009382 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009383 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9384 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009385 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009386 }
Chet Haase21cd1382010-09-01 17:42:29 -07009387 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009388 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009389 }
Chet Haase55dbb652010-12-21 20:15:08 -08009390 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009391 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009392 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9393 // View was rejected last time it was drawn by its parent; this may have changed
9394 invalidateParentIfNeeded();
9395 }
Chet Haase21cd1382010-09-01 17:42:29 -07009396 }
9397 }
9398
9399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009400 * Left position of this view relative to its parent.
9401 *
9402 * @return The left edge of this view, in pixels.
9403 */
9404 @ViewDebug.CapturedViewProperty
9405 public final int getLeft() {
9406 return mLeft;
9407 }
9408
9409 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009410 * Sets the left position of this view relative to its parent. This method is meant to be called
9411 * by the layout system and should not generally be called otherwise, because the property
9412 * may be changed at any time by the layout.
9413 *
9414 * @param left The bottom of this view, in pixels.
9415 */
9416 public final void setLeft(int left) {
9417 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009418 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009419 final boolean matrixIsIdentity = mTransformationInfo == null
9420 || mTransformationInfo.mMatrixIsIdentity;
9421 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009422 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009423 int minLeft;
9424 int xLoc;
9425 if (left < mLeft) {
9426 minLeft = left;
9427 xLoc = left - mLeft;
9428 } else {
9429 minLeft = mLeft;
9430 xLoc = 0;
9431 }
Chet Haasee9140a72011-02-16 16:23:29 -08009432 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009433 }
9434 } else {
9435 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009436 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009437 }
9438
Chet Haaseed032702010-10-01 14:05:54 -07009439 int oldWidth = mRight - mLeft;
9440 int height = mBottom - mTop;
9441
Chet Haase21cd1382010-09-01 17:42:29 -07009442 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009443 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009444 mDisplayList.setLeft(left);
9445 }
Chet Haase21cd1382010-09-01 17:42:29 -07009446
Chet Haaseed032702010-10-01 14:05:54 -07009447 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9448
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009449 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009450 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9451 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009452 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009453 }
Chet Haase21cd1382010-09-01 17:42:29 -07009454 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009455 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009456 }
Chet Haase55dbb652010-12-21 20:15:08 -08009457 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009458 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009459 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9460 // View was rejected last time it was drawn by its parent; this may have changed
9461 invalidateParentIfNeeded();
9462 }
Chet Haase21cd1382010-09-01 17:42:29 -07009463 }
9464 }
9465
9466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 * Right position of this view relative to its parent.
9468 *
9469 * @return The right edge of this view, in pixels.
9470 */
9471 @ViewDebug.CapturedViewProperty
9472 public final int getRight() {
9473 return mRight;
9474 }
9475
9476 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009477 * Sets the right position of this view relative to its parent. This method is meant to be called
9478 * by the layout system and should not generally be called otherwise, because the property
9479 * may be changed at any time by the layout.
9480 *
9481 * @param right The bottom of this view, in pixels.
9482 */
9483 public final void setRight(int right) {
9484 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009485 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009486 final boolean matrixIsIdentity = mTransformationInfo == null
9487 || mTransformationInfo.mMatrixIsIdentity;
9488 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009489 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009490 int maxRight;
9491 if (right < mRight) {
9492 maxRight = mRight;
9493 } else {
9494 maxRight = right;
9495 }
Chet Haasee9140a72011-02-16 16:23:29 -08009496 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009497 }
9498 } else {
9499 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009500 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009501 }
9502
Chet Haaseed032702010-10-01 14:05:54 -07009503 int oldWidth = mRight - mLeft;
9504 int height = mBottom - mTop;
9505
Chet Haase21cd1382010-09-01 17:42:29 -07009506 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009507 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009508 mDisplayList.setRight(mRight);
9509 }
Chet Haase21cd1382010-09-01 17:42:29 -07009510
Chet Haaseed032702010-10-01 14:05:54 -07009511 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9512
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009513 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009514 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9515 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009516 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009517 }
Chet Haase21cd1382010-09-01 17:42:29 -07009518 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009519 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009520 }
Chet Haase55dbb652010-12-21 20:15:08 -08009521 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009522 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009523 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9524 // View was rejected last time it was drawn by its parent; this may have changed
9525 invalidateParentIfNeeded();
9526 }
Chet Haase21cd1382010-09-01 17:42:29 -07009527 }
9528 }
9529
9530 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009531 * The visual x position of this view, in pixels. This is equivalent to the
9532 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009533 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009534 *
Chet Haasedf030d22010-07-30 17:22:38 -07009535 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009536 */
Chet Haasea5531132012-02-02 13:41:44 -08009537 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009538 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009539 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009540 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009541
Chet Haasedf030d22010-07-30 17:22:38 -07009542 /**
9543 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9544 * {@link #setTranslationX(float) translationX} property to be the difference between
9545 * the x value passed in and the current {@link #getLeft() left} property.
9546 *
9547 * @param x The visual x position of this view, in pixels.
9548 */
9549 public void setX(float x) {
9550 setTranslationX(x - mLeft);
9551 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009552
Chet Haasedf030d22010-07-30 17:22:38 -07009553 /**
9554 * The visual y position of this view, in pixels. This is equivalent to the
9555 * {@link #setTranslationY(float) translationY} property plus the current
9556 * {@link #getTop() top} property.
9557 *
9558 * @return The visual y position of this view, in pixels.
9559 */
Chet Haasea5531132012-02-02 13:41:44 -08009560 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009561 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009562 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009563 }
9564
9565 /**
9566 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9567 * {@link #setTranslationY(float) translationY} property to be the difference between
9568 * the y value passed in and the current {@link #getTop() top} property.
9569 *
9570 * @param y The visual y position of this view, in pixels.
9571 */
9572 public void setY(float y) {
9573 setTranslationY(y - mTop);
9574 }
9575
9576
9577 /**
9578 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9579 * This position is post-layout, in addition to wherever the object's
9580 * layout placed it.
9581 *
9582 * @return The horizontal position of this view relative to its left position, in pixels.
9583 */
Chet Haasea5531132012-02-02 13:41:44 -08009584 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009585 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009586 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009587 }
9588
9589 /**
9590 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9591 * This effectively positions the object post-layout, in addition to wherever the object's
9592 * layout placed it.
9593 *
9594 * @param translationX The horizontal position of this view relative to its left position,
9595 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009596 *
9597 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009598 */
9599 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009600 ensureTransformationInfo();
9601 final TransformationInfo info = mTransformationInfo;
9602 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009603 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009604 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009605 info.mTranslationX = translationX;
9606 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009607 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009608 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009609 mDisplayList.setTranslationX(translationX);
9610 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009611 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9612 // View was rejected last time it was drawn by its parent; this may have changed
9613 invalidateParentIfNeeded();
9614 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009615 }
9616 }
9617
9618 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009619 * The horizontal location of this view relative to its {@link #getTop() top} position.
9620 * This position is post-layout, in addition to wherever the object's
9621 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009622 *
Chet Haasedf030d22010-07-30 17:22:38 -07009623 * @return The vertical position of this view relative to its top position,
9624 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009625 */
Chet Haasea5531132012-02-02 13:41:44 -08009626 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009627 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009628 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009629 }
9630
9631 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009632 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9633 * This effectively positions the object post-layout, in addition to wherever the object's
9634 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009635 *
Chet Haasedf030d22010-07-30 17:22:38 -07009636 * @param translationY The vertical position of this view relative to its top position,
9637 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009638 *
9639 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009640 */
Chet Haasedf030d22010-07-30 17:22:38 -07009641 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009642 ensureTransformationInfo();
9643 final TransformationInfo info = mTransformationInfo;
9644 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009645 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009646 info.mTranslationY = translationY;
9647 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009648 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009649 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009650 mDisplayList.setTranslationY(translationY);
9651 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009652 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9653 // View was rejected last time it was drawn by its parent; this may have changed
9654 invalidateParentIfNeeded();
9655 }
Chet Haasedf030d22010-07-30 17:22:38 -07009656 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009657 }
9658
9659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 * Hit rectangle in parent's coordinates
9661 *
9662 * @param outRect The hit rectangle of the view.
9663 */
9664 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009665 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009666 final TransformationInfo info = mTransformationInfo;
9667 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009668 outRect.set(mLeft, mTop, mRight, mBottom);
9669 } else {
9670 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009671 tmpRect.set(-info.mPivotX, -info.mPivotY,
9672 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9673 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009674 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9675 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009676 }
9677 }
9678
9679 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009680 * Determines whether the given point, in local coordinates is inside the view.
9681 */
9682 /*package*/ final boolean pointInView(float localX, float localY) {
9683 return localX >= 0 && localX < (mRight - mLeft)
9684 && localY >= 0 && localY < (mBottom - mTop);
9685 }
9686
9687 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009688 * Utility method to determine whether the given point, in local coordinates,
9689 * is inside the view, where the area of the view is expanded by the slop factor.
9690 * This method is called while processing touch-move events to determine if the event
9691 * is still within the view.
9692 */
9693 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009694 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009695 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 }
9697
9698 /**
9699 * When a view has focus and the user navigates away from it, the next view is searched for
9700 * starting from the rectangle filled in by this method.
9701 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009702 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9703 * of the view. However, if your view maintains some idea of internal selection,
9704 * such as a cursor, or a selected row or column, you should override this method and
9705 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 *
9707 * @param r The rectangle to fill in, in this view's coordinates.
9708 */
9709 public void getFocusedRect(Rect r) {
9710 getDrawingRect(r);
9711 }
9712
9713 /**
9714 * If some part of this view is not clipped by any of its parents, then
9715 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009716 * coordinates (without taking possible View rotations into account), offset
9717 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9718 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009719 *
9720 * @param r If true is returned, r holds the global coordinates of the
9721 * visible portion of this view.
9722 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9723 * between this view and its root. globalOffet may be null.
9724 * @return true if r is non-empty (i.e. part of the view is visible at the
9725 * root level.
9726 */
9727 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9728 int width = mRight - mLeft;
9729 int height = mBottom - mTop;
9730 if (width > 0 && height > 0) {
9731 r.set(0, 0, width, height);
9732 if (globalOffset != null) {
9733 globalOffset.set(-mScrollX, -mScrollY);
9734 }
9735 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9736 }
9737 return false;
9738 }
9739
9740 public final boolean getGlobalVisibleRect(Rect r) {
9741 return getGlobalVisibleRect(r, null);
9742 }
9743
9744 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009745 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009746 if (getGlobalVisibleRect(r, offset)) {
9747 r.offset(-offset.x, -offset.y); // make r local
9748 return true;
9749 }
9750 return false;
9751 }
9752
9753 /**
9754 * Offset this view's vertical location by the specified number of pixels.
9755 *
9756 * @param offset the number of pixels to offset the view by
9757 */
9758 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009759 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009760 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009761 final boolean matrixIsIdentity = mTransformationInfo == null
9762 || mTransformationInfo.mMatrixIsIdentity;
9763 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009764 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009765 invalidateViewProperty(false, false);
9766 } else {
9767 final ViewParent p = mParent;
9768 if (p != null && mAttachInfo != null) {
9769 final Rect r = mAttachInfo.mTmpInvalRect;
9770 int minTop;
9771 int maxBottom;
9772 int yLoc;
9773 if (offset < 0) {
9774 minTop = mTop + offset;
9775 maxBottom = mBottom;
9776 yLoc = offset;
9777 } else {
9778 minTop = mTop;
9779 maxBottom = mBottom + offset;
9780 yLoc = 0;
9781 }
9782 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9783 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009784 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009785 }
9786 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009787 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009788 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009789
Chet Haasec3aa3612010-06-17 08:50:37 -07009790 mTop += offset;
9791 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009792 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009793 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009794 invalidateViewProperty(false, false);
9795 } else {
9796 if (!matrixIsIdentity) {
9797 invalidateViewProperty(false, true);
9798 }
9799 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009800 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 }
9803
9804 /**
9805 * Offset this view's horizontal location by the specified amount of pixels.
9806 *
9807 * @param offset the numer of pixels to offset the view by
9808 */
9809 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009810 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009811 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009812 final boolean matrixIsIdentity = mTransformationInfo == null
9813 || mTransformationInfo.mMatrixIsIdentity;
9814 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009815 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009816 invalidateViewProperty(false, false);
9817 } else {
9818 final ViewParent p = mParent;
9819 if (p != null && mAttachInfo != null) {
9820 final Rect r = mAttachInfo.mTmpInvalRect;
9821 int minLeft;
9822 int maxRight;
9823 if (offset < 0) {
9824 minLeft = mLeft + offset;
9825 maxRight = mRight;
9826 } else {
9827 minLeft = mLeft;
9828 maxRight = mRight + offset;
9829 }
9830 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9831 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009832 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009833 }
9834 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009835 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009836 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009837
Chet Haasec3aa3612010-06-17 08:50:37 -07009838 mLeft += offset;
9839 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009840 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009841 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009842 invalidateViewProperty(false, false);
9843 } else {
9844 if (!matrixIsIdentity) {
9845 invalidateViewProperty(false, true);
9846 }
9847 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009848 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 }
9851
9852 /**
9853 * Get the LayoutParams associated with this view. All views should have
9854 * layout parameters. These supply parameters to the <i>parent</i> of this
9855 * view specifying how it should be arranged. There are many subclasses of
9856 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9857 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009858 *
9859 * This method may return null if this View is not attached to a parent
9860 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9861 * was not invoked successfully. When a View is attached to a parent
9862 * ViewGroup, this method must not return null.
9863 *
9864 * @return The LayoutParams associated with this view, or null if no
9865 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009867 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 public ViewGroup.LayoutParams getLayoutParams() {
9869 return mLayoutParams;
9870 }
9871
9872 /**
9873 * Set the layout parameters associated with this view. These supply
9874 * parameters to the <i>parent</i> of this view specifying how it should be
9875 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9876 * correspond to the different subclasses of ViewGroup that are responsible
9877 * for arranging their children.
9878 *
Romain Guy01c174b2011-02-22 11:51:06 -08009879 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 */
9881 public void setLayoutParams(ViewGroup.LayoutParams params) {
9882 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009883 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 }
9885 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009886 if (mParent instanceof ViewGroup) {
9887 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889 requestLayout();
9890 }
9891
9892 /**
9893 * Set the scrolled position of your view. This will cause a call to
9894 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9895 * invalidated.
9896 * @param x the x position to scroll to
9897 * @param y the y position to scroll to
9898 */
9899 public void scrollTo(int x, int y) {
9900 if (mScrollX != x || mScrollY != y) {
9901 int oldX = mScrollX;
9902 int oldY = mScrollY;
9903 mScrollX = x;
9904 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009905 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009906 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009907 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009908 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 }
9911 }
9912
9913 /**
9914 * Move the scrolled position of your view. This will cause a call to
9915 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9916 * invalidated.
9917 * @param x the amount of pixels to scroll by horizontally
9918 * @param y the amount of pixels to scroll by vertically
9919 */
9920 public void scrollBy(int x, int y) {
9921 scrollTo(mScrollX + x, mScrollY + y);
9922 }
9923
9924 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009925 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9926 * animation to fade the scrollbars out after a default delay. If a subclass
9927 * provides animated scrolling, the start delay should equal the duration
9928 * of the scrolling animation.</p>
9929 *
9930 * <p>The animation starts only if at least one of the scrollbars is
9931 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9932 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9933 * this method returns true, and false otherwise. If the animation is
9934 * started, this method calls {@link #invalidate()}; in that case the
9935 * caller should not call {@link #invalidate()}.</p>
9936 *
9937 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009938 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009939 *
9940 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9941 * and {@link #scrollTo(int, int)}.</p>
9942 *
9943 * @return true if the animation is played, false otherwise
9944 *
9945 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009946 * @see #scrollBy(int, int)
9947 * @see #scrollTo(int, int)
9948 * @see #isHorizontalScrollBarEnabled()
9949 * @see #isVerticalScrollBarEnabled()
9950 * @see #setHorizontalScrollBarEnabled(boolean)
9951 * @see #setVerticalScrollBarEnabled(boolean)
9952 */
9953 protected boolean awakenScrollBars() {
9954 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009955 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009956 }
9957
9958 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009959 * Trigger the scrollbars to draw.
9960 * This method differs from awakenScrollBars() only in its default duration.
9961 * initialAwakenScrollBars() will show the scroll bars for longer than
9962 * usual to give the user more of a chance to notice them.
9963 *
9964 * @return true if the animation is played, false otherwise.
9965 */
9966 private boolean initialAwakenScrollBars() {
9967 return mScrollCache != null &&
9968 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9969 }
9970
9971 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009972 * <p>
9973 * Trigger the scrollbars to draw. When invoked this method starts an
9974 * animation to fade the scrollbars out after a fixed delay. If a subclass
9975 * provides animated scrolling, the start delay should equal the duration of
9976 * the scrolling animation.
9977 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009978 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009979 * <p>
9980 * The animation starts only if at least one of the scrollbars is enabled,
9981 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9982 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9983 * this method returns true, and false otherwise. If the animation is
9984 * started, this method calls {@link #invalidate()}; in that case the caller
9985 * should not call {@link #invalidate()}.
9986 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009987 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009988 * <p>
9989 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009990 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009991 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009992 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009993 * @param startDelay the delay, in milliseconds, after which the animation
9994 * should start; when the delay is 0, the animation starts
9995 * immediately
9996 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009997 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009998 * @see #scrollBy(int, int)
9999 * @see #scrollTo(int, int)
10000 * @see #isHorizontalScrollBarEnabled()
10001 * @see #isVerticalScrollBarEnabled()
10002 * @see #setHorizontalScrollBarEnabled(boolean)
10003 * @see #setVerticalScrollBarEnabled(boolean)
10004 */
10005 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010006 return awakenScrollBars(startDelay, true);
10007 }
Joe Malin32736f02011-01-19 16:14:20 -080010008
Mike Cleron290947b2009-09-29 18:34:32 -070010009 /**
10010 * <p>
10011 * Trigger the scrollbars to draw. When invoked this method starts an
10012 * animation to fade the scrollbars out after a fixed delay. If a subclass
10013 * provides animated scrolling, the start delay should equal the duration of
10014 * the scrolling animation.
10015 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010016 *
Mike Cleron290947b2009-09-29 18:34:32 -070010017 * <p>
10018 * The animation starts only if at least one of the scrollbars is enabled,
10019 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10020 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10021 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010022 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010023 * is set to true; in that case the caller
10024 * should not call {@link #invalidate()}.
10025 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010026 *
Mike Cleron290947b2009-09-29 18:34:32 -070010027 * <p>
10028 * This method should be invoked everytime a subclass directly updates the
10029 * scroll parameters.
10030 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010031 *
Mike Cleron290947b2009-09-29 18:34:32 -070010032 * @param startDelay the delay, in milliseconds, after which the animation
10033 * should start; when the delay is 0, the animation starts
10034 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010035 *
Mike Cleron290947b2009-09-29 18:34:32 -070010036 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010037 *
Mike Cleron290947b2009-09-29 18:34:32 -070010038 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010039 *
Mike Cleron290947b2009-09-29 18:34:32 -070010040 * @see #scrollBy(int, int)
10041 * @see #scrollTo(int, int)
10042 * @see #isHorizontalScrollBarEnabled()
10043 * @see #isVerticalScrollBarEnabled()
10044 * @see #setHorizontalScrollBarEnabled(boolean)
10045 * @see #setVerticalScrollBarEnabled(boolean)
10046 */
10047 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010048 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010049
Mike Cleronf116bf82009-09-27 19:14:12 -070010050 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10051 return false;
10052 }
10053
10054 if (scrollCache.scrollBar == null) {
10055 scrollCache.scrollBar = new ScrollBarDrawable();
10056 }
10057
10058 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10059
Mike Cleron290947b2009-09-29 18:34:32 -070010060 if (invalidate) {
10061 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010062 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010063 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010064
10065 if (scrollCache.state == ScrollabilityCache.OFF) {
10066 // FIXME: this is copied from WindowManagerService.
10067 // We should get this value from the system when it
10068 // is possible to do so.
10069 final int KEY_REPEAT_FIRST_DELAY = 750;
10070 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10071 }
10072
10073 // Tell mScrollCache when we should start fading. This may
10074 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010075 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010076 scrollCache.fadeStartTime = fadeStartTime;
10077 scrollCache.state = ScrollabilityCache.ON;
10078
10079 // Schedule our fader to run, unscheduling any old ones first
10080 if (mAttachInfo != null) {
10081 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10082 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10083 }
10084
10085 return true;
10086 }
10087
10088 return false;
10089 }
10090
10091 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010092 * Do not invalidate views which are not visible and which are not running an animation. They
10093 * will not get drawn and they should not set dirty flags as if they will be drawn
10094 */
10095 private boolean skipInvalidate() {
10096 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10097 (!(mParent instanceof ViewGroup) ||
10098 !((ViewGroup) mParent).isViewTransitioning(this));
10099 }
10100 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010101 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010102 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10103 * in the future. This must be called from a UI thread. To call from a non-UI
10104 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010105 *
10106 * WARNING: This method is destructive to dirty.
10107 * @param dirty the rectangle representing the bounds of the dirty region
10108 */
10109 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010110 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010111 return;
10112 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010113 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010114 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10115 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010116 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010117 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010118 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 final ViewParent p = mParent;
10120 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010121 //noinspection PointlessBooleanExpression,ConstantConditions
10122 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10123 if (p != null && ai != null && ai.mHardwareAccelerated) {
10124 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010125 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010126 p.invalidateChild(this, null);
10127 return;
10128 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 if (p != null && ai != null) {
10131 final int scrollX = mScrollX;
10132 final int scrollY = mScrollY;
10133 final Rect r = ai.mTmpInvalRect;
10134 r.set(dirty.left - scrollX, dirty.top - scrollY,
10135 dirty.right - scrollX, dirty.bottom - scrollY);
10136 mParent.invalidateChild(this, r);
10137 }
10138 }
10139 }
10140
10141 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010142 * 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 -080010143 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010144 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10145 * will be called at some point in the future. This must be called from
10146 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010147 * @param l the left position of the dirty region
10148 * @param t the top position of the dirty region
10149 * @param r the right position of the dirty region
10150 * @param b the bottom position of the dirty region
10151 */
10152 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010153 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010154 return;
10155 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010156 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010157 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10158 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010159 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010160 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010161 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 final ViewParent p = mParent;
10163 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010164 //noinspection PointlessBooleanExpression,ConstantConditions
10165 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10166 if (p != null && ai != null && ai.mHardwareAccelerated) {
10167 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010168 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010169 p.invalidateChild(this, null);
10170 return;
10171 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010173 if (p != null && ai != null && l < r && t < b) {
10174 final int scrollX = mScrollX;
10175 final int scrollY = mScrollY;
10176 final Rect tmpr = ai.mTmpInvalRect;
10177 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10178 p.invalidateChild(this, tmpr);
10179 }
10180 }
10181 }
10182
10183 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010184 * Invalidate the whole view. If the view is visible,
10185 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10186 * the future. This must be called from a UI thread. To call from a non-UI thread,
10187 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 */
10189 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010190 invalidate(true);
10191 }
Joe Malin32736f02011-01-19 16:14:20 -080010192
Chet Haaseed032702010-10-01 14:05:54 -070010193 /**
10194 * This is where the invalidate() work actually happens. A full invalidate()
10195 * causes the drawing cache to be invalidated, but this function can be called with
10196 * invalidateCache set to false to skip that invalidation step for cases that do not
10197 * need it (for example, a component that remains at the same dimensions with the same
10198 * content).
10199 *
10200 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10201 * well. This is usually true for a full invalidate, but may be set to false if the
10202 * View's contents or dimensions have not changed.
10203 */
Romain Guy849d0a32011-02-01 17:20:48 -080010204 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010205 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010206 return;
10207 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010208 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010209 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010210 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10211 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010212 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010213 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010214 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010215 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010216 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010219 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010220 //noinspection PointlessBooleanExpression,ConstantConditions
10221 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10222 if (p != null && ai != null && ai.mHardwareAccelerated) {
10223 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010224 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010225 p.invalidateChild(this, null);
10226 return;
10227 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010228 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010230 if (p != null && ai != null) {
10231 final Rect r = ai.mTmpInvalRect;
10232 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10233 // Don't call invalidate -- we don't want to internally scroll
10234 // our own bounds
10235 p.invalidateChild(this, r);
10236 }
10237 }
10238 }
10239
10240 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010241 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10242 * set any flags or handle all of the cases handled by the default invalidation methods.
10243 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10244 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10245 * walk up the hierarchy, transforming the dirty rect as necessary.
10246 *
10247 * The method also handles normal invalidation logic if display list properties are not
10248 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10249 * backup approach, to handle these cases used in the various property-setting methods.
10250 *
10251 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10252 * are not being used in this view
10253 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10254 * list properties are not being used in this view
10255 */
10256 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010257 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010258 if (invalidateParent) {
10259 invalidateParentCaches();
10260 }
10261 if (forceRedraw) {
10262 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10263 }
10264 invalidate(false);
10265 } else {
10266 final AttachInfo ai = mAttachInfo;
10267 final ViewParent p = mParent;
10268 if (p != null && ai != null) {
10269 final Rect r = ai.mTmpInvalRect;
10270 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10271 if (mParent instanceof ViewGroup) {
10272 ((ViewGroup) mParent).invalidateChildFast(this, r);
10273 } else {
10274 mParent.invalidateChild(this, r);
10275 }
10276 }
10277 }
10278 }
10279
10280 /**
10281 * Utility method to transform a given Rect by the current matrix of this view.
10282 */
10283 void transformRect(final Rect rect) {
10284 if (!getMatrix().isIdentity()) {
10285 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10286 boundingRect.set(rect);
10287 getMatrix().mapRect(boundingRect);
10288 rect.set((int) (boundingRect.left - 0.5f),
10289 (int) (boundingRect.top - 0.5f),
10290 (int) (boundingRect.right + 0.5f),
10291 (int) (boundingRect.bottom + 0.5f));
10292 }
10293 }
10294
10295 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010296 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010297 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10298 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010299 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10300 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010301 *
10302 * @hide
10303 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010304 protected void invalidateParentCaches() {
10305 if (mParent instanceof View) {
10306 ((View) mParent).mPrivateFlags |= INVALIDATED;
10307 }
10308 }
Joe Malin32736f02011-01-19 16:14:20 -080010309
Romain Guy0fd89bf2011-01-26 15:41:30 -080010310 /**
10311 * Used to indicate that the parent of this view should be invalidated. This functionality
10312 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10313 * which is necessary when various parent-managed properties of the view change, such as
10314 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10315 * an invalidation event to the parent.
10316 *
10317 * @hide
10318 */
10319 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010320 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010321 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010322 }
10323 }
10324
10325 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010326 * Indicates whether this View is opaque. An opaque View guarantees that it will
10327 * draw all the pixels overlapping its bounds using a fully opaque color.
10328 *
10329 * Subclasses of View should override this method whenever possible to indicate
10330 * whether an instance is opaque. Opaque Views are treated in a special way by
10331 * the View hierarchy, possibly allowing it to perform optimizations during
10332 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010333 *
Romain Guy24443ea2009-05-11 11:56:30 -070010334 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010335 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010336 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010337 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010338 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010339 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10340 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010341 }
10342
Adam Powell20232d02010-12-08 21:08:53 -080010343 /**
10344 * @hide
10345 */
10346 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010347 // Opaque if:
10348 // - Has a background
10349 // - Background is opaque
10350 // - Doesn't have scrollbars or scrollbars are inside overlay
10351
Philip Milne6c8ea062012-04-03 17:38:43 -070010352 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010353 mPrivateFlags |= OPAQUE_BACKGROUND;
10354 } else {
10355 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10356 }
10357
10358 final int flags = mViewFlags;
10359 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10360 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10361 mPrivateFlags |= OPAQUE_SCROLLBARS;
10362 } else {
10363 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10364 }
10365 }
10366
10367 /**
10368 * @hide
10369 */
10370 protected boolean hasOpaqueScrollbars() {
10371 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010372 }
10373
10374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 * @return A handler associated with the thread running the View. This
10376 * handler can be used to pump events in the UI events queue.
10377 */
10378 public Handler getHandler() {
10379 if (mAttachInfo != null) {
10380 return mAttachInfo.mHandler;
10381 }
10382 return null;
10383 }
10384
10385 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010386 * Gets the view root associated with the View.
10387 * @return The view root, or null if none.
10388 * @hide
10389 */
10390 public ViewRootImpl getViewRootImpl() {
10391 if (mAttachInfo != null) {
10392 return mAttachInfo.mViewRootImpl;
10393 }
10394 return null;
10395 }
10396
10397 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010398 * <p>Causes the Runnable to be added to the message queue.
10399 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010400 *
Romain Guye63a4f32011-08-11 11:33:31 -070010401 * <p>This method can be invoked from outside of the UI thread
10402 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 *
10404 * @param action The Runnable that will be executed.
10405 *
10406 * @return Returns true if the Runnable was successfully placed in to the
10407 * message queue. Returns false on failure, usually because the
10408 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010409 *
10410 * @see #postDelayed
10411 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010412 */
10413 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010414 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010415 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010416 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010418 // Assume that post will succeed later
10419 ViewRootImpl.getRunQueue().post(action);
10420 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 }
10422
10423 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010424 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010426 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010427 *
Romain Guye63a4f32011-08-11 11:33:31 -070010428 * <p>This method can be invoked from outside of the UI thread
10429 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 *
10431 * @param action The Runnable that will be executed.
10432 * @param delayMillis The delay (in milliseconds) until the Runnable
10433 * will be executed.
10434 *
10435 * @return true if the Runnable was successfully placed in to the
10436 * message queue. Returns false on failure, usually because the
10437 * looper processing the message queue is exiting. Note that a
10438 * result of true does not mean the Runnable will be processed --
10439 * if the looper is quit before the delivery time of the message
10440 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010441 *
10442 * @see #post
10443 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 */
10445 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010446 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010447 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010448 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010450 // Assume that post will succeed later
10451 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10452 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 }
10454
10455 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010456 * <p>Causes the Runnable to execute on the next animation time step.
10457 * The runnable will be run on the user interface thread.</p>
10458 *
10459 * <p>This method can be invoked from outside of the UI thread
10460 * only when this View is attached to a window.</p>
10461 *
10462 * @param action The Runnable that will be executed.
10463 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010464 * @see #postOnAnimationDelayed
10465 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010466 */
10467 public void postOnAnimation(Runnable action) {
10468 final AttachInfo attachInfo = mAttachInfo;
10469 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010470 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10471 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010472 } else {
10473 // Assume that post will succeed later
10474 ViewRootImpl.getRunQueue().post(action);
10475 }
10476 }
10477
10478 /**
10479 * <p>Causes the Runnable to execute on the next animation time step,
10480 * after the specified amount of time elapses.
10481 * The runnable will be run on the user interface thread.</p>
10482 *
10483 * <p>This method can be invoked from outside of the UI thread
10484 * only when this View is attached to a window.</p>
10485 *
10486 * @param action The Runnable that will be executed.
10487 * @param delayMillis The delay (in milliseconds) until the Runnable
10488 * will be executed.
10489 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010490 * @see #postOnAnimation
10491 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010492 */
10493 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10494 final AttachInfo attachInfo = mAttachInfo;
10495 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010496 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10497 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010498 } else {
10499 // Assume that post will succeed later
10500 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10501 }
10502 }
10503
10504 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010505 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010506 *
Romain Guye63a4f32011-08-11 11:33:31 -070010507 * <p>This method can be invoked from outside of the UI thread
10508 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 *
10510 * @param action The Runnable to remove from the message handling queue
10511 *
10512 * @return true if this view could ask the Handler to remove the Runnable,
10513 * false otherwise. When the returned value is true, the Runnable
10514 * may or may not have been actually removed from the message queue
10515 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010516 *
10517 * @see #post
10518 * @see #postDelayed
10519 * @see #postOnAnimation
10520 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 */
10522 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010523 if (action != null) {
10524 final AttachInfo attachInfo = mAttachInfo;
10525 if (attachInfo != null) {
10526 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010527 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10528 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010529 } else {
10530 // Assume that post will succeed later
10531 ViewRootImpl.getRunQueue().removeCallbacks(action);
10532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 return true;
10535 }
10536
10537 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010538 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10539 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 *
Romain Guye63a4f32011-08-11 11:33:31 -070010541 * <p>This method can be invoked from outside of the UI thread
10542 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010543 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010545 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 */
10547 public void postInvalidate() {
10548 postInvalidateDelayed(0);
10549 }
10550
10551 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010552 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10553 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010554 *
Romain Guye63a4f32011-08-11 11:33:31 -070010555 * <p>This method can be invoked from outside of the UI thread
10556 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 *
10558 * @param left The left coordinate of the rectangle to invalidate.
10559 * @param top The top coordinate of the rectangle to invalidate.
10560 * @param right The right coordinate of the rectangle to invalidate.
10561 * @param bottom The bottom coordinate of the rectangle to invalidate.
10562 *
10563 * @see #invalidate(int, int, int, int)
10564 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010565 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 */
10567 public void postInvalidate(int left, int top, int right, int bottom) {
10568 postInvalidateDelayed(0, left, top, right, bottom);
10569 }
10570
10571 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010572 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10573 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010574 *
Romain Guye63a4f32011-08-11 11:33:31 -070010575 * <p>This method can be invoked from outside of the UI thread
10576 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577 *
10578 * @param delayMilliseconds the duration in milliseconds to delay the
10579 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010580 *
10581 * @see #invalidate()
10582 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 */
10584 public void postInvalidateDelayed(long delayMilliseconds) {
10585 // We try only with the AttachInfo because there's no point in invalidating
10586 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010587 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010588 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010589 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
10591 }
10592
10593 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010594 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10595 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010596 *
Romain Guye63a4f32011-08-11 11:33:31 -070010597 * <p>This method can be invoked from outside of the UI thread
10598 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 *
10600 * @param delayMilliseconds the duration in milliseconds to delay the
10601 * invalidation by
10602 * @param left The left coordinate of the rectangle to invalidate.
10603 * @param top The top coordinate of the rectangle to invalidate.
10604 * @param right The right coordinate of the rectangle to invalidate.
10605 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010606 *
10607 * @see #invalidate(int, int, int, int)
10608 * @see #invalidate(Rect)
10609 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 */
10611 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10612 int right, int bottom) {
10613
10614 // We try only with the AttachInfo because there's no point in invalidating
10615 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010616 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010617 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10619 info.target = this;
10620 info.left = left;
10621 info.top = top;
10622 info.right = right;
10623 info.bottom = bottom;
10624
Jeff Browna175a5b2012-02-15 19:18:31 -080010625 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 }
10627 }
10628
10629 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010630 * <p>Cause an invalidate to happen on the next animation time step, typically the
10631 * next display frame.</p>
10632 *
10633 * <p>This method can be invoked from outside of the UI thread
10634 * only when this View is attached to a window.</p>
10635 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010636 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010637 */
10638 public void postInvalidateOnAnimation() {
10639 // We try only with the AttachInfo because there's no point in invalidating
10640 // if we are not attached to our window
10641 final AttachInfo attachInfo = mAttachInfo;
10642 if (attachInfo != null) {
10643 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10644 }
10645 }
10646
10647 /**
10648 * <p>Cause an invalidate of the specified area to happen on the next animation
10649 * time step, typically the next display frame.</p>
10650 *
10651 * <p>This method can be invoked from outside of the UI thread
10652 * only when this View is attached to a window.</p>
10653 *
10654 * @param left The left coordinate of the rectangle to invalidate.
10655 * @param top The top coordinate of the rectangle to invalidate.
10656 * @param right The right coordinate of the rectangle to invalidate.
10657 * @param bottom The bottom coordinate of the rectangle to invalidate.
10658 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010659 * @see #invalidate(int, int, int, int)
10660 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010661 */
10662 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10663 // We try only with the AttachInfo because there's no point in invalidating
10664 // if we are not attached to our window
10665 final AttachInfo attachInfo = mAttachInfo;
10666 if (attachInfo != null) {
10667 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10668 info.target = this;
10669 info.left = left;
10670 info.top = top;
10671 info.right = right;
10672 info.bottom = bottom;
10673
10674 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10675 }
10676 }
10677
10678 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010679 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10680 * This event is sent at most once every
10681 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10682 */
10683 private void postSendViewScrolledAccessibilityEventCallback() {
10684 if (mSendViewScrolledAccessibilityEvent == null) {
10685 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10686 }
10687 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10688 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10689 postDelayed(mSendViewScrolledAccessibilityEvent,
10690 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10691 }
10692 }
10693
10694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 * Called by a parent to request that a child update its values for mScrollX
10696 * and mScrollY if necessary. This will typically be done if the child is
10697 * animating a scroll using a {@link android.widget.Scroller Scroller}
10698 * object.
10699 */
10700 public void computeScroll() {
10701 }
10702
10703 /**
10704 * <p>Indicate whether the horizontal edges are faded when the view is
10705 * scrolled horizontally.</p>
10706 *
10707 * @return true if the horizontal edges should are faded on scroll, false
10708 * otherwise
10709 *
10710 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010711 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010712 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 */
10714 public boolean isHorizontalFadingEdgeEnabled() {
10715 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10716 }
10717
10718 /**
10719 * <p>Define whether the horizontal edges should be faded when this view
10720 * is scrolled horizontally.</p>
10721 *
10722 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10723 * be faded when the view is scrolled
10724 * horizontally
10725 *
10726 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010727 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010728 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 */
10730 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10731 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10732 if (horizontalFadingEdgeEnabled) {
10733 initScrollCache();
10734 }
10735
10736 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10737 }
10738 }
10739
10740 /**
10741 * <p>Indicate whether the vertical edges are faded when the view is
10742 * scrolled horizontally.</p>
10743 *
10744 * @return true if the vertical edges should are faded on scroll, false
10745 * otherwise
10746 *
10747 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010748 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010749 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 */
10751 public boolean isVerticalFadingEdgeEnabled() {
10752 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10753 }
10754
10755 /**
10756 * <p>Define whether the vertical edges should be faded when this view
10757 * is scrolled vertically.</p>
10758 *
10759 * @param verticalFadingEdgeEnabled true if the vertical edges should
10760 * be faded when the view is scrolled
10761 * vertically
10762 *
10763 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010764 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010765 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 */
10767 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10768 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10769 if (verticalFadingEdgeEnabled) {
10770 initScrollCache();
10771 }
10772
10773 mViewFlags ^= FADING_EDGE_VERTICAL;
10774 }
10775 }
10776
10777 /**
10778 * Returns the strength, or intensity, of the top faded edge. The strength is
10779 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10780 * returns 0.0 or 1.0 but no value in between.
10781 *
10782 * Subclasses should override this method to provide a smoother fade transition
10783 * when scrolling occurs.
10784 *
10785 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10786 */
10787 protected float getTopFadingEdgeStrength() {
10788 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10789 }
10790
10791 /**
10792 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10793 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10794 * returns 0.0 or 1.0 but no value in between.
10795 *
10796 * Subclasses should override this method to provide a smoother fade transition
10797 * when scrolling occurs.
10798 *
10799 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10800 */
10801 protected float getBottomFadingEdgeStrength() {
10802 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10803 computeVerticalScrollRange() ? 1.0f : 0.0f;
10804 }
10805
10806 /**
10807 * Returns the strength, or intensity, of the left faded edge. The strength is
10808 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10809 * returns 0.0 or 1.0 but no value in between.
10810 *
10811 * Subclasses should override this method to provide a smoother fade transition
10812 * when scrolling occurs.
10813 *
10814 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10815 */
10816 protected float getLeftFadingEdgeStrength() {
10817 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10818 }
10819
10820 /**
10821 * Returns the strength, or intensity, of the right faded edge. The strength is
10822 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10823 * returns 0.0 or 1.0 but no value in between.
10824 *
10825 * Subclasses should override this method to provide a smoother fade transition
10826 * when scrolling occurs.
10827 *
10828 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10829 */
10830 protected float getRightFadingEdgeStrength() {
10831 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10832 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10833 }
10834
10835 /**
10836 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10837 * scrollbar is not drawn by default.</p>
10838 *
10839 * @return true if the horizontal scrollbar should be painted, false
10840 * otherwise
10841 *
10842 * @see #setHorizontalScrollBarEnabled(boolean)
10843 */
10844 public boolean isHorizontalScrollBarEnabled() {
10845 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10846 }
10847
10848 /**
10849 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10850 * scrollbar is not drawn by default.</p>
10851 *
10852 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10853 * be painted
10854 *
10855 * @see #isHorizontalScrollBarEnabled()
10856 */
10857 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10858 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10859 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010860 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010861 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 }
10863 }
10864
10865 /**
10866 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10867 * scrollbar is not drawn by default.</p>
10868 *
10869 * @return true if the vertical scrollbar should be painted, false
10870 * otherwise
10871 *
10872 * @see #setVerticalScrollBarEnabled(boolean)
10873 */
10874 public boolean isVerticalScrollBarEnabled() {
10875 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10876 }
10877
10878 /**
10879 * <p>Define whether the vertical scrollbar should be drawn or not. The
10880 * scrollbar is not drawn by default.</p>
10881 *
10882 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10883 * be painted
10884 *
10885 * @see #isVerticalScrollBarEnabled()
10886 */
10887 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10888 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10889 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010890 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010891 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 }
10893 }
10894
Adam Powell20232d02010-12-08 21:08:53 -080010895 /**
10896 * @hide
10897 */
10898 protected void recomputePadding() {
10899 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 }
Joe Malin32736f02011-01-19 16:14:20 -080010901
Mike Cleronfe81d382009-09-28 14:22:16 -070010902 /**
10903 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010904 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010905 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010906 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010907 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010908 */
10909 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10910 initScrollCache();
10911 final ScrollabilityCache scrollabilityCache = mScrollCache;
10912 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010913 if (fadeScrollbars) {
10914 scrollabilityCache.state = ScrollabilityCache.OFF;
10915 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010916 scrollabilityCache.state = ScrollabilityCache.ON;
10917 }
10918 }
Joe Malin32736f02011-01-19 16:14:20 -080010919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 /**
Joe Malin32736f02011-01-19 16:14:20 -080010921 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010922 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010923 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010924 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010925 *
10926 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010927 */
10928 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010929 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010930 }
Joe Malin32736f02011-01-19 16:14:20 -080010931
Mike Cleron52f0a642009-09-28 18:21:37 -070010932 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010933 *
10934 * Returns the delay before scrollbars fade.
10935 *
10936 * @return the delay before scrollbars fade
10937 *
10938 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10939 */
10940 public int getScrollBarDefaultDelayBeforeFade() {
10941 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10942 mScrollCache.scrollBarDefaultDelayBeforeFade;
10943 }
10944
10945 /**
10946 * Define the delay before scrollbars fade.
10947 *
10948 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10949 *
10950 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10951 */
10952 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10953 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10954 }
10955
10956 /**
10957 *
10958 * Returns the scrollbar fade duration.
10959 *
10960 * @return the scrollbar fade duration
10961 *
10962 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10963 */
10964 public int getScrollBarFadeDuration() {
10965 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10966 mScrollCache.scrollBarFadeDuration;
10967 }
10968
10969 /**
10970 * Define the scrollbar fade duration.
10971 *
10972 * @param scrollBarFadeDuration - the scrollbar fade duration
10973 *
10974 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10975 */
10976 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10977 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10978 }
10979
10980 /**
10981 *
10982 * Returns the scrollbar size.
10983 *
10984 * @return the scrollbar size
10985 *
10986 * @attr ref android.R.styleable#View_scrollbarSize
10987 */
10988 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010989 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010990 mScrollCache.scrollBarSize;
10991 }
10992
10993 /**
10994 * Define the scrollbar size.
10995 *
10996 * @param scrollBarSize - the scrollbar size
10997 *
10998 * @attr ref android.R.styleable#View_scrollbarSize
10999 */
11000 public void setScrollBarSize(int scrollBarSize) {
11001 getScrollCache().scrollBarSize = scrollBarSize;
11002 }
11003
11004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11006 * inset. When inset, they add to the padding of the view. And the scrollbars
11007 * can be drawn inside the padding area or on the edge of the view. For example,
11008 * if a view has a background drawable and you want to draw the scrollbars
11009 * inside the padding specified by the drawable, you can use
11010 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11011 * appear at the edge of the view, ignoring the padding, then you can use
11012 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11013 * @param style the style of the scrollbars. Should be one of
11014 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11015 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11016 * @see #SCROLLBARS_INSIDE_OVERLAY
11017 * @see #SCROLLBARS_INSIDE_INSET
11018 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11019 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011020 *
11021 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 */
11023 public void setScrollBarStyle(int style) {
11024 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11025 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011026 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011027 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 }
11029 }
11030
11031 /**
11032 * <p>Returns the current scrollbar style.</p>
11033 * @return the current scrollbar style
11034 * @see #SCROLLBARS_INSIDE_OVERLAY
11035 * @see #SCROLLBARS_INSIDE_INSET
11036 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11037 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011038 *
11039 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011041 @ViewDebug.ExportedProperty(mapping = {
11042 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11043 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11044 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11045 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11046 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011047 public int getScrollBarStyle() {
11048 return mViewFlags & SCROLLBARS_STYLE_MASK;
11049 }
11050
11051 /**
11052 * <p>Compute the horizontal range that the horizontal scrollbar
11053 * represents.</p>
11054 *
11055 * <p>The range is expressed in arbitrary units that must be the same as the
11056 * units used by {@link #computeHorizontalScrollExtent()} and
11057 * {@link #computeHorizontalScrollOffset()}.</p>
11058 *
11059 * <p>The default range is the drawing width of this view.</p>
11060 *
11061 * @return the total horizontal range represented by the horizontal
11062 * scrollbar
11063 *
11064 * @see #computeHorizontalScrollExtent()
11065 * @see #computeHorizontalScrollOffset()
11066 * @see android.widget.ScrollBarDrawable
11067 */
11068 protected int computeHorizontalScrollRange() {
11069 return getWidth();
11070 }
11071
11072 /**
11073 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11074 * within the horizontal range. This value is used to compute the position
11075 * of the thumb within the scrollbar's track.</p>
11076 *
11077 * <p>The range is expressed in arbitrary units that must be the same as the
11078 * units used by {@link #computeHorizontalScrollRange()} and
11079 * {@link #computeHorizontalScrollExtent()}.</p>
11080 *
11081 * <p>The default offset is the scroll offset of this view.</p>
11082 *
11083 * @return the horizontal offset of the scrollbar's thumb
11084 *
11085 * @see #computeHorizontalScrollRange()
11086 * @see #computeHorizontalScrollExtent()
11087 * @see android.widget.ScrollBarDrawable
11088 */
11089 protected int computeHorizontalScrollOffset() {
11090 return mScrollX;
11091 }
11092
11093 /**
11094 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11095 * within the horizontal range. This value is used to compute the length
11096 * of the thumb within the scrollbar's track.</p>
11097 *
11098 * <p>The range is expressed in arbitrary units that must be the same as the
11099 * units used by {@link #computeHorizontalScrollRange()} and
11100 * {@link #computeHorizontalScrollOffset()}.</p>
11101 *
11102 * <p>The default extent is the drawing width of this view.</p>
11103 *
11104 * @return the horizontal extent of the scrollbar's thumb
11105 *
11106 * @see #computeHorizontalScrollRange()
11107 * @see #computeHorizontalScrollOffset()
11108 * @see android.widget.ScrollBarDrawable
11109 */
11110 protected int computeHorizontalScrollExtent() {
11111 return getWidth();
11112 }
11113
11114 /**
11115 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11116 *
11117 * <p>The range is expressed in arbitrary units that must be the same as the
11118 * units used by {@link #computeVerticalScrollExtent()} and
11119 * {@link #computeVerticalScrollOffset()}.</p>
11120 *
11121 * @return the total vertical range represented by the vertical scrollbar
11122 *
11123 * <p>The default range is the drawing height of this view.</p>
11124 *
11125 * @see #computeVerticalScrollExtent()
11126 * @see #computeVerticalScrollOffset()
11127 * @see android.widget.ScrollBarDrawable
11128 */
11129 protected int computeVerticalScrollRange() {
11130 return getHeight();
11131 }
11132
11133 /**
11134 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11135 * within the horizontal range. This value is used to compute the position
11136 * of the thumb within the scrollbar's track.</p>
11137 *
11138 * <p>The range is expressed in arbitrary units that must be the same as the
11139 * units used by {@link #computeVerticalScrollRange()} and
11140 * {@link #computeVerticalScrollExtent()}.</p>
11141 *
11142 * <p>The default offset is the scroll offset of this view.</p>
11143 *
11144 * @return the vertical offset of the scrollbar's thumb
11145 *
11146 * @see #computeVerticalScrollRange()
11147 * @see #computeVerticalScrollExtent()
11148 * @see android.widget.ScrollBarDrawable
11149 */
11150 protected int computeVerticalScrollOffset() {
11151 return mScrollY;
11152 }
11153
11154 /**
11155 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11156 * within the vertical range. This value is used to compute the length
11157 * of the thumb within the scrollbar's track.</p>
11158 *
11159 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011160 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 * {@link #computeVerticalScrollOffset()}.</p>
11162 *
11163 * <p>The default extent is the drawing height of this view.</p>
11164 *
11165 * @return the vertical extent of the scrollbar's thumb
11166 *
11167 * @see #computeVerticalScrollRange()
11168 * @see #computeVerticalScrollOffset()
11169 * @see android.widget.ScrollBarDrawable
11170 */
11171 protected int computeVerticalScrollExtent() {
11172 return getHeight();
11173 }
11174
11175 /**
Adam Powell69159442011-06-13 17:53:06 -070011176 * Check if this view can be scrolled horizontally in a certain direction.
11177 *
11178 * @param direction Negative to check scrolling left, positive to check scrolling right.
11179 * @return true if this view can be scrolled in the specified direction, false otherwise.
11180 */
11181 public boolean canScrollHorizontally(int direction) {
11182 final int offset = computeHorizontalScrollOffset();
11183 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11184 if (range == 0) return false;
11185 if (direction < 0) {
11186 return offset > 0;
11187 } else {
11188 return offset < range - 1;
11189 }
11190 }
11191
11192 /**
11193 * Check if this view can be scrolled vertically in a certain direction.
11194 *
11195 * @param direction Negative to check scrolling up, positive to check scrolling down.
11196 * @return true if this view can be scrolled in the specified direction, false otherwise.
11197 */
11198 public boolean canScrollVertically(int direction) {
11199 final int offset = computeVerticalScrollOffset();
11200 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11201 if (range == 0) return false;
11202 if (direction < 0) {
11203 return offset > 0;
11204 } else {
11205 return offset < range - 1;
11206 }
11207 }
11208
11209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11211 * scrollbars are painted only if they have been awakened first.</p>
11212 *
11213 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011214 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011215 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011217 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 // scrollbars are drawn only when the animation is running
11219 final ScrollabilityCache cache = mScrollCache;
11220 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011221
Mike Cleronf116bf82009-09-27 19:14:12 -070011222 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011223
Mike Cleronf116bf82009-09-27 19:14:12 -070011224 if (state == ScrollabilityCache.OFF) {
11225 return;
11226 }
Joe Malin32736f02011-01-19 16:14:20 -080011227
Mike Cleronf116bf82009-09-27 19:14:12 -070011228 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011229
Mike Cleronf116bf82009-09-27 19:14:12 -070011230 if (state == ScrollabilityCache.FADING) {
11231 // We're fading -- get our fade interpolation
11232 if (cache.interpolatorValues == null) {
11233 cache.interpolatorValues = new float[1];
11234 }
Joe Malin32736f02011-01-19 16:14:20 -080011235
Mike Cleronf116bf82009-09-27 19:14:12 -070011236 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011237
Mike Cleronf116bf82009-09-27 19:14:12 -070011238 // Stops the animation if we're done
11239 if (cache.scrollBarInterpolator.timeToValues(values) ==
11240 Interpolator.Result.FREEZE_END) {
11241 cache.state = ScrollabilityCache.OFF;
11242 } else {
11243 cache.scrollBar.setAlpha(Math.round(values[0]));
11244 }
Joe Malin32736f02011-01-19 16:14:20 -080011245
11246 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011247 // drawing. We only want this when we're fading so that
11248 // we prevent excessive redraws
11249 invalidate = true;
11250 } else {
11251 // We're just on -- but we may have been fading before so
11252 // reset alpha
11253 cache.scrollBar.setAlpha(255);
11254 }
11255
Joe Malin32736f02011-01-19 16:14:20 -080011256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 final int viewFlags = mViewFlags;
11258
11259 final boolean drawHorizontalScrollBar =
11260 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11261 final boolean drawVerticalScrollBar =
11262 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11263 && !isVerticalScrollBarHidden();
11264
11265 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11266 final int width = mRight - mLeft;
11267 final int height = mBottom - mTop;
11268
11269 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011270
Mike Reede8853fc2009-09-04 14:01:48 -040011271 final int scrollX = mScrollX;
11272 final int scrollY = mScrollY;
11273 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11274
Mike Cleronf116bf82009-09-27 19:14:12 -070011275 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011278 int size = scrollBar.getSize(false);
11279 if (size <= 0) {
11280 size = cache.scrollBarSize;
11281 }
11282
Mike Cleronf116bf82009-09-27 19:14:12 -070011283 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011284 computeHorizontalScrollOffset(),
11285 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011286 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011287 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011288 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011289 left = scrollX + (mPaddingLeft & inside);
11290 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11291 bottom = top + size;
11292 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11293 if (invalidate) {
11294 invalidate(left, top, right, bottom);
11295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 }
11297
11298 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011299 int size = scrollBar.getSize(true);
11300 if (size <= 0) {
11301 size = cache.scrollBarSize;
11302 }
11303
Mike Reede8853fc2009-09-04 14:01:48 -040011304 scrollBar.setParameters(computeVerticalScrollRange(),
11305 computeVerticalScrollOffset(),
11306 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011307 switch (mVerticalScrollbarPosition) {
11308 default:
11309 case SCROLLBAR_POSITION_DEFAULT:
11310 case SCROLLBAR_POSITION_RIGHT:
11311 left = scrollX + width - size - (mUserPaddingRight & inside);
11312 break;
11313 case SCROLLBAR_POSITION_LEFT:
11314 left = scrollX + (mUserPaddingLeft & inside);
11315 break;
11316 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011317 top = scrollY + (mPaddingTop & inside);
11318 right = left + size;
11319 bottom = scrollY + height - (mUserPaddingBottom & inside);
11320 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11321 if (invalidate) {
11322 invalidate(left, top, right, bottom);
11323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 }
11325 }
11326 }
11327 }
Romain Guy8506ab42009-06-11 17:35:47 -070011328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011329 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011330 * 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 -080011331 * FastScroller is visible.
11332 * @return whether to temporarily hide the vertical scrollbar
11333 * @hide
11334 */
11335 protected boolean isVerticalScrollBarHidden() {
11336 return false;
11337 }
11338
11339 /**
11340 * <p>Draw the horizontal scrollbar if
11341 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11342 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011343 * @param canvas the canvas on which to draw the scrollbar
11344 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 *
11346 * @see #isHorizontalScrollBarEnabled()
11347 * @see #computeHorizontalScrollRange()
11348 * @see #computeHorizontalScrollExtent()
11349 * @see #computeHorizontalScrollOffset()
11350 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011351 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011352 */
Romain Guy8fb95422010-08-17 18:38:51 -070011353 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11354 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011355 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011356 scrollBar.draw(canvas);
11357 }
Mike Reede8853fc2009-09-04 14:01:48 -040011358
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011360 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11361 * returns true.</p>
11362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 * @param canvas the canvas on which to draw the scrollbar
11364 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 *
11366 * @see #isVerticalScrollBarEnabled()
11367 * @see #computeVerticalScrollRange()
11368 * @see #computeVerticalScrollExtent()
11369 * @see #computeVerticalScrollOffset()
11370 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011371 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 */
Romain Guy8fb95422010-08-17 18:38:51 -070011373 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11374 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011375 scrollBar.setBounds(l, t, r, b);
11376 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 }
11378
11379 /**
11380 * Implement this to do your drawing.
11381 *
11382 * @param canvas the canvas on which the background will be drawn
11383 */
11384 protected void onDraw(Canvas canvas) {
11385 }
11386
11387 /*
11388 * Caller is responsible for calling requestLayout if necessary.
11389 * (This allows addViewInLayout to not request a new layout.)
11390 */
11391 void assignParent(ViewParent parent) {
11392 if (mParent == null) {
11393 mParent = parent;
11394 } else if (parent == null) {
11395 mParent = null;
11396 } else {
11397 throw new RuntimeException("view " + this + " being added, but"
11398 + " it already has a parent");
11399 }
11400 }
11401
11402 /**
11403 * This is called when the view is attached to a window. At this point it
11404 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011405 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11406 * however it may be called any time before the first onDraw -- including
11407 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011408 *
11409 * @see #onDetachedFromWindow()
11410 */
11411 protected void onAttachedToWindow() {
11412 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11413 mParent.requestTransparentRegion(this);
11414 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011415
Adam Powell8568c3a2010-04-19 14:26:11 -070011416 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11417 initialAwakenScrollBars();
11418 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11419 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011420
Chet Haasea9b61ac2010-12-20 07:40:25 -080011421 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011422
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011423 // Order is important here: LayoutDirection MUST be resolved before Padding
11424 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011425 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011426 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011427 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011428 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011429
Svetoslav Ganov42138042012-03-20 11:51:39 -070011430 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011431 if (isFocused()) {
11432 InputMethodManager imm = InputMethodManager.peekInstance();
11433 imm.focusIn(this);
11434 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011435
11436 if (mAttachInfo != null && mDisplayList != null) {
11437 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11438 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011439 }
Cibu Johny86666632010-02-22 13:01:02 -080011440
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011441 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011442 * @see #onScreenStateChanged(int)
11443 */
11444 void dispatchScreenStateChanged(int screenState) {
11445 onScreenStateChanged(screenState);
11446 }
11447
11448 /**
11449 * This method is called whenever the state of the screen this view is
11450 * attached to changes. A state change will usually occurs when the screen
11451 * turns on or off (whether it happens automatically or the user does it
11452 * manually.)
11453 *
11454 * @param screenState The new state of the screen. Can be either
11455 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11456 */
11457 public void onScreenStateChanged(int screenState) {
11458 }
11459
11460 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011461 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11462 */
11463 private boolean hasRtlSupport() {
11464 return mContext.getApplicationInfo().hasRtlSupport();
11465 }
11466
11467 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011468 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11469 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011470 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011471 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011472 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011473 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011474 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011475 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011476
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011477 if (hasRtlSupport()) {
11478 // Set resolved depending on layout direction
11479 switch (getLayoutDirection()) {
11480 case LAYOUT_DIRECTION_INHERIT:
11481 // If this is root view, no need to look at parent's layout dir.
11482 if (canResolveLayoutDirection()) {
11483 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011484
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011485 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11486 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11487 }
11488 } else {
11489 // Nothing to do, LTR by default
11490 }
11491 break;
11492 case LAYOUT_DIRECTION_RTL:
11493 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11494 break;
11495 case LAYOUT_DIRECTION_LOCALE:
11496 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011497 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11498 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011499 break;
11500 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011501 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011502 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011503 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011504
11505 // Set to resolved
11506 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011507 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011508 // Resolve padding
11509 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011510 }
11511
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011512 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011513 * Called when layout direction has been resolved.
11514 *
11515 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011516 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011517 */
11518 public void onResolvedLayoutDirectionChanged() {
11519 }
11520
11521 /**
11522 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011523 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011524 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011525 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011526 // If the user specified the absolute padding (either with android:padding or
11527 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11528 // use the default padding or the padding from the background drawable
11529 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011530 int resolvedLayoutDirection = getResolvedLayoutDirection();
11531 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011532 case LAYOUT_DIRECTION_RTL:
11533 // Start user padding override Right user padding. Otherwise, if Right user
11534 // padding is not defined, use the default Right padding. If Right user padding
11535 // is defined, just use it.
11536 if (mUserPaddingStart >= 0) {
11537 mUserPaddingRight = mUserPaddingStart;
11538 } else if (mUserPaddingRight < 0) {
11539 mUserPaddingRight = mPaddingRight;
11540 }
11541 if (mUserPaddingEnd >= 0) {
11542 mUserPaddingLeft = mUserPaddingEnd;
11543 } else if (mUserPaddingLeft < 0) {
11544 mUserPaddingLeft = mPaddingLeft;
11545 }
11546 break;
11547 case LAYOUT_DIRECTION_LTR:
11548 default:
11549 // Start user padding override Left user padding. Otherwise, if Left user
11550 // padding is not defined, use the default left padding. If Left user padding
11551 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011552 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011553 mUserPaddingLeft = mUserPaddingStart;
11554 } else if (mUserPaddingLeft < 0) {
11555 mUserPaddingLeft = mPaddingLeft;
11556 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011557 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011558 mUserPaddingRight = mUserPaddingEnd;
11559 } else if (mUserPaddingRight < 0) {
11560 mUserPaddingRight = mPaddingRight;
11561 }
11562 }
11563
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011564 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11565
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011566 if(isPaddingRelative()) {
11567 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11568 } else {
11569 recomputePadding();
11570 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011571 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011572 }
11573
11574 /**
11575 * Resolve padding depending on the layout direction. Subclasses that care about
11576 * padding resolution should override this method. The default implementation does
11577 * nothing.
11578 *
11579 * @param layoutDirection the direction of the layout
11580 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011581 * @see {@link #LAYOUT_DIRECTION_LTR}
11582 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011583 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011584 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011585 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011586 }
11587
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011588 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011589 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011590 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011591 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011592 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011593 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011594 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011595 switch (getLayoutDirection()) {
11596 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011597 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011598 default:
11599 return true;
11600 }
11601 }
11602
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011603 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011604 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11605 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011606 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011607 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011608 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011609 // Reset the current resolved bits
11610 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011611 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011612 // Reset also the text direction
11613 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011614 }
11615
11616 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011617 * Called during reset of resolved layout direction.
11618 *
11619 * Subclasses need to override this method to clear cached information that depends on the
11620 * resolved layout direction, or to inform child views that inherit their layout direction.
11621 *
11622 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011623 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011624 */
11625 public void onResolvedLayoutDirectionReset() {
11626 }
11627
11628 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011629 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011630 *
11631 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011632 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011633 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011634 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011635 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011636 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011637 }
11638
11639 /**
11640 * This is called when the view is detached from a window. At this point it
11641 * no longer has a surface for drawing.
11642 *
11643 * @see #onAttachedToWindow()
11644 */
11645 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011646 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011647
Romain Guya440b002010-02-24 15:57:54 -080011648 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011649 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011650 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011651 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011653 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011654
Romain Guya998dff2012-03-23 18:58:36 -070011655 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011656
11657 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011658 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011659 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011660 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011661 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011662 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011663 // Should never happen
11664 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011665 }
11666
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011667 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011668
11669 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011670 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011671 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011672 }
11673
11674 /**
11675 * @return The number of times this view has been attached to a window
11676 */
11677 protected int getWindowAttachCount() {
11678 return mWindowAttachCount;
11679 }
11680
11681 /**
11682 * Retrieve a unique token identifying the window this view is attached to.
11683 * @return Return the window's token for use in
11684 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11685 */
11686 public IBinder getWindowToken() {
11687 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11688 }
11689
11690 /**
11691 * Retrieve a unique token identifying the top-level "real" window of
11692 * the window that this view is attached to. That is, this is like
11693 * {@link #getWindowToken}, except if the window this view in is a panel
11694 * window (attached to another containing window), then the token of
11695 * the containing window is returned instead.
11696 *
11697 * @return Returns the associated window token, either
11698 * {@link #getWindowToken()} or the containing window's token.
11699 */
11700 public IBinder getApplicationWindowToken() {
11701 AttachInfo ai = mAttachInfo;
11702 if (ai != null) {
11703 IBinder appWindowToken = ai.mPanelParentWindowToken;
11704 if (appWindowToken == null) {
11705 appWindowToken = ai.mWindowToken;
11706 }
11707 return appWindowToken;
11708 }
11709 return null;
11710 }
11711
11712 /**
11713 * Retrieve private session object this view hierarchy is using to
11714 * communicate with the window manager.
11715 * @return the session object to communicate with the window manager
11716 */
11717 /*package*/ IWindowSession getWindowSession() {
11718 return mAttachInfo != null ? mAttachInfo.mSession : null;
11719 }
11720
11721 /**
11722 * @param info the {@link android.view.View.AttachInfo} to associated with
11723 * this view
11724 */
11725 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11726 //System.out.println("Attached! " + this);
11727 mAttachInfo = info;
11728 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011729 // We will need to evaluate the drawable state at least once.
11730 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011731 if (mFloatingTreeObserver != null) {
11732 info.mTreeObserver.merge(mFloatingTreeObserver);
11733 mFloatingTreeObserver = null;
11734 }
11735 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11736 mAttachInfo.mScrollContainers.add(this);
11737 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11738 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011739 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011740 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011741
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011742 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011743 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011744 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011745 if (listeners != null && listeners.size() > 0) {
11746 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11747 // perform the dispatching. The iterator is a safe guard against listeners that
11748 // could mutate the list by calling the various add/remove methods. This prevents
11749 // the array from being modified while we iterate it.
11750 for (OnAttachStateChangeListener listener : listeners) {
11751 listener.onViewAttachedToWindow(this);
11752 }
11753 }
11754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 int vis = info.mWindowVisibility;
11756 if (vis != GONE) {
11757 onWindowVisibilityChanged(vis);
11758 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011759 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11760 // If nobody has evaluated the drawable state yet, then do it now.
11761 refreshDrawableState();
11762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011763 }
11764
11765 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011766 AttachInfo info = mAttachInfo;
11767 if (info != null) {
11768 int vis = info.mWindowVisibility;
11769 if (vis != GONE) {
11770 onWindowVisibilityChanged(GONE);
11771 }
11772 }
11773
11774 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011775
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011776 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011777 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011778 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011779 if (listeners != null && listeners.size() > 0) {
11780 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11781 // perform the dispatching. The iterator is a safe guard against listeners that
11782 // could mutate the list by calling the various add/remove methods. This prevents
11783 // the array from being modified while we iterate it.
11784 for (OnAttachStateChangeListener listener : listeners) {
11785 listener.onViewDetachedFromWindow(this);
11786 }
11787 }
11788
Romain Guy01d5edc2011-01-28 11:28:53 -080011789 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 mAttachInfo.mScrollContainers.remove(this);
11791 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11792 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011794 mAttachInfo = null;
11795 }
11796
11797 /**
11798 * Store this view hierarchy's frozen state into the given container.
11799 *
11800 * @param container The SparseArray in which to save the view's state.
11801 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011802 * @see #restoreHierarchyState(android.util.SparseArray)
11803 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11804 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011805 */
11806 public void saveHierarchyState(SparseArray<Parcelable> container) {
11807 dispatchSaveInstanceState(container);
11808 }
11809
11810 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011811 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11812 * this view and its children. May be overridden to modify how freezing happens to a
11813 * 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 -080011814 *
11815 * @param container The SparseArray in which to save the view's state.
11816 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011817 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11818 * @see #saveHierarchyState(android.util.SparseArray)
11819 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011820 */
11821 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11822 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11823 mPrivateFlags &= ~SAVE_STATE_CALLED;
11824 Parcelable state = onSaveInstanceState();
11825 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11826 throw new IllegalStateException(
11827 "Derived class did not call super.onSaveInstanceState()");
11828 }
11829 if (state != null) {
11830 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11831 // + ": " + state);
11832 container.put(mID, state);
11833 }
11834 }
11835 }
11836
11837 /**
11838 * Hook allowing a view to generate a representation of its internal state
11839 * that can later be used to create a new instance with that same state.
11840 * This state should only contain information that is not persistent or can
11841 * not be reconstructed later. For example, you will never store your
11842 * current position on screen because that will be computed again when a
11843 * new instance of the view is placed in its view hierarchy.
11844 * <p>
11845 * Some examples of things you may store here: the current cursor position
11846 * in a text view (but usually not the text itself since that is stored in a
11847 * content provider or other persistent storage), the currently selected
11848 * item in a list view.
11849 *
11850 * @return Returns a Parcelable object containing the view's current dynamic
11851 * state, or null if there is nothing interesting to save. The
11852 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011853 * @see #onRestoreInstanceState(android.os.Parcelable)
11854 * @see #saveHierarchyState(android.util.SparseArray)
11855 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 * @see #setSaveEnabled(boolean)
11857 */
11858 protected Parcelable onSaveInstanceState() {
11859 mPrivateFlags |= SAVE_STATE_CALLED;
11860 return BaseSavedState.EMPTY_STATE;
11861 }
11862
11863 /**
11864 * Restore this view hierarchy's frozen state from the given container.
11865 *
11866 * @param container The SparseArray which holds previously frozen states.
11867 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011868 * @see #saveHierarchyState(android.util.SparseArray)
11869 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11870 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011871 */
11872 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11873 dispatchRestoreInstanceState(container);
11874 }
11875
11876 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011877 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11878 * state for this view and its children. May be overridden to modify how restoring
11879 * happens to a view's children; for example, some views may want to not store state
11880 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011881 *
11882 * @param container The SparseArray which holds previously saved state.
11883 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011884 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11885 * @see #restoreHierarchyState(android.util.SparseArray)
11886 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 */
11888 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11889 if (mID != NO_ID) {
11890 Parcelable state = container.get(mID);
11891 if (state != null) {
11892 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11893 // + ": " + state);
11894 mPrivateFlags &= ~SAVE_STATE_CALLED;
11895 onRestoreInstanceState(state);
11896 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11897 throw new IllegalStateException(
11898 "Derived class did not call super.onRestoreInstanceState()");
11899 }
11900 }
11901 }
11902 }
11903
11904 /**
11905 * Hook allowing a view to re-apply a representation of its internal state that had previously
11906 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11907 * null state.
11908 *
11909 * @param state The frozen state that had previously been returned by
11910 * {@link #onSaveInstanceState}.
11911 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011912 * @see #onSaveInstanceState()
11913 * @see #restoreHierarchyState(android.util.SparseArray)
11914 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011915 */
11916 protected void onRestoreInstanceState(Parcelable state) {
11917 mPrivateFlags |= SAVE_STATE_CALLED;
11918 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011919 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11920 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011921 + "when two views of different type have the same id in the same hierarchy. "
11922 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011923 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011924 }
11925 }
11926
11927 /**
11928 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11929 *
11930 * @return the drawing start time in milliseconds
11931 */
11932 public long getDrawingTime() {
11933 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11934 }
11935
11936 /**
11937 * <p>Enables or disables the duplication of the parent's state into this view. When
11938 * duplication is enabled, this view gets its drawable state from its parent rather
11939 * than from its own internal properties.</p>
11940 *
11941 * <p>Note: in the current implementation, setting this property to true after the
11942 * view was added to a ViewGroup might have no effect at all. This property should
11943 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11944 *
11945 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11946 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011947 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011948 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11949 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011950 *
11951 * @param enabled True to enable duplication of the parent's drawable state, false
11952 * to disable it.
11953 *
11954 * @see #getDrawableState()
11955 * @see #isDuplicateParentStateEnabled()
11956 */
11957 public void setDuplicateParentStateEnabled(boolean enabled) {
11958 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11959 }
11960
11961 /**
11962 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11963 *
11964 * @return True if this view's drawable state is duplicated from the parent,
11965 * false otherwise
11966 *
11967 * @see #getDrawableState()
11968 * @see #setDuplicateParentStateEnabled(boolean)
11969 */
11970 public boolean isDuplicateParentStateEnabled() {
11971 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11972 }
11973
11974 /**
Romain Guy171c5922011-01-06 10:04:23 -080011975 * <p>Specifies the type of layer backing this view. The layer can be
11976 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11977 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011978 *
Romain Guy171c5922011-01-06 10:04:23 -080011979 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11980 * instance that controls how the layer is composed on screen. The following
11981 * properties of the paint are taken into account when composing the layer:</p>
11982 * <ul>
11983 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11984 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11985 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11986 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011987 *
Romain Guy171c5922011-01-06 10:04:23 -080011988 * <p>If this view has an alpha value set to < 1.0 by calling
11989 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11990 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11991 * equivalent to setting a hardware layer on this view and providing a paint with
11992 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011993 *
Romain Guy171c5922011-01-06 10:04:23 -080011994 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11995 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11996 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011997 *
Romain Guy171c5922011-01-06 10:04:23 -080011998 * @param layerType The ype of layer to use with this view, must be one of
11999 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12000 * {@link #LAYER_TYPE_HARDWARE}
12001 * @param paint The paint used to compose the layer. This argument is optional
12002 * and can be null. It is ignored when the layer type is
12003 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012004 *
12005 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012006 * @see #LAYER_TYPE_NONE
12007 * @see #LAYER_TYPE_SOFTWARE
12008 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012009 * @see #setAlpha(float)
12010 *
Romain Guy171c5922011-01-06 10:04:23 -080012011 * @attr ref android.R.styleable#View_layerType
12012 */
12013 public void setLayerType(int layerType, Paint paint) {
12014 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012015 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012016 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12017 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012018
Romain Guyd6cd5722011-01-17 14:42:41 -080012019 if (layerType == mLayerType) {
12020 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12021 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012022 invalidateParentCaches();
12023 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012024 }
12025 return;
12026 }
Romain Guy171c5922011-01-06 10:04:23 -080012027
12028 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012029 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012030 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012031 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012032 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012033 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012034 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012035 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012036 default:
12037 break;
Romain Guy171c5922011-01-06 10:04:23 -080012038 }
12039
12040 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012041 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12042 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12043 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012044
Romain Guy0fd89bf2011-01-26 15:41:30 -080012045 invalidateParentCaches();
12046 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012047 }
12048
12049 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012050 * Indicates whether this view has a static layer. A view with layer type
12051 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12052 * dynamic.
12053 */
12054 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012055 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012056 }
12057
12058 /**
Romain Guy171c5922011-01-06 10:04:23 -080012059 * Indicates what type of layer is currently associated with this view. By default
12060 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12061 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12062 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012063 *
Romain Guy171c5922011-01-06 10:04:23 -080012064 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12065 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012066 *
12067 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012068 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012069 * @see #LAYER_TYPE_NONE
12070 * @see #LAYER_TYPE_SOFTWARE
12071 * @see #LAYER_TYPE_HARDWARE
12072 */
12073 public int getLayerType() {
12074 return mLayerType;
12075 }
Joe Malin32736f02011-01-19 16:14:20 -080012076
Romain Guy6c319ca2011-01-11 14:29:25 -080012077 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012078 * Forces this view's layer to be created and this view to be rendered
12079 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12080 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012081 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012082 * This method can for instance be used to render a view into its layer before
12083 * starting an animation. If this view is complex, rendering into the layer
12084 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012085 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012086 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012087 *
12088 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012089 */
12090 public void buildLayer() {
12091 if (mLayerType == LAYER_TYPE_NONE) return;
12092
12093 if (mAttachInfo == null) {
12094 throw new IllegalStateException("This view must be attached to a window first");
12095 }
12096
12097 switch (mLayerType) {
12098 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012099 if (mAttachInfo.mHardwareRenderer != null &&
12100 mAttachInfo.mHardwareRenderer.isEnabled() &&
12101 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012102 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012103 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012104 break;
12105 case LAYER_TYPE_SOFTWARE:
12106 buildDrawingCache(true);
12107 break;
12108 }
12109 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012110
Romain Guy9c4b79a2011-11-10 19:23:58 -080012111 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12112 void flushLayer() {
12113 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12114 mHardwareLayer.flush();
12115 }
12116 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012117
12118 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012119 * <p>Returns a hardware layer that can be used to draw this view again
12120 * without executing its draw method.</p>
12121 *
12122 * @return A HardwareLayer ready to render, or null if an error occurred.
12123 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012124 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012125 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12126 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012127 return null;
12128 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012129
Romain Guy9c4b79a2011-11-10 19:23:58 -080012130 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012131
12132 final int width = mRight - mLeft;
12133 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012134
Romain Guy6c319ca2011-01-11 14:29:25 -080012135 if (width == 0 || height == 0) {
12136 return null;
12137 }
12138
12139 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12140 if (mHardwareLayer == null) {
12141 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12142 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012143 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012144 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12145 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012146 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012147 }
12148
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012149 // The layer is not valid if the underlying GPU resources cannot be allocated
12150 if (!mHardwareLayer.isValid()) {
12151 return null;
12152 }
12153
Chet Haasea1cff502012-02-21 13:43:44 -080012154 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012155 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012156 }
12157
12158 return mHardwareLayer;
12159 }
Romain Guy171c5922011-01-06 10:04:23 -080012160
Romain Guy589b0bb2011-10-10 13:57:47 -070012161 /**
12162 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012163 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012164 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012165 *
12166 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012167 * @see #LAYER_TYPE_HARDWARE
12168 */
Romain Guya998dff2012-03-23 18:58:36 -070012169 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012170 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012171 AttachInfo info = mAttachInfo;
12172 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012173 info.mHardwareRenderer.isEnabled() &&
12174 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012175 mHardwareLayer.destroy();
12176 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012177
Romain Guy9c4b79a2011-11-10 19:23:58 -080012178 invalidate(true);
12179 invalidateParentCaches();
12180 }
Romain Guy65b345f2011-07-27 18:51:50 -070012181 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012182 }
Romain Guy65b345f2011-07-27 18:51:50 -070012183 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012184 }
12185
Romain Guy171c5922011-01-06 10:04:23 -080012186 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012187 * Destroys all hardware rendering resources. This method is invoked
12188 * when the system needs to reclaim resources. Upon execution of this
12189 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012190 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012191 * Note: you <strong>must</strong> call
12192 * <code>super.destroyHardwareResources()</code> when overriding
12193 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012194 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012195 * @hide
12196 */
12197 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012198 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012199 }
12200
12201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12203 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12204 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12205 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12206 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12207 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012208 *
Romain Guy171c5922011-01-06 10:04:23 -080012209 * <p>Enabling the drawing cache is similar to
12210 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012211 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12212 * drawing cache has no effect on rendering because the system uses a different mechanism
12213 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12214 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12215 * for information on how to enable software and hardware layers.</p>
12216 *
12217 * <p>This API can be used to manually generate
12218 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12219 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012220 *
12221 * @param enabled true to enable the drawing cache, false otherwise
12222 *
12223 * @see #isDrawingCacheEnabled()
12224 * @see #getDrawingCache()
12225 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012226 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012227 */
12228 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012229 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012230 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12231 }
12232
12233 /**
12234 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12235 *
12236 * @return true if the drawing cache is enabled
12237 *
12238 * @see #setDrawingCacheEnabled(boolean)
12239 * @see #getDrawingCache()
12240 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012241 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012242 public boolean isDrawingCacheEnabled() {
12243 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12244 }
12245
12246 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012247 * Debugging utility which recursively outputs the dirty state of a view and its
12248 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012249 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012250 * @hide
12251 */
Romain Guy676b1732011-02-14 14:45:33 -080012252 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012253 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12254 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12255 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12256 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12257 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12258 if (clear) {
12259 mPrivateFlags &= clearMask;
12260 }
12261 if (this instanceof ViewGroup) {
12262 ViewGroup parent = (ViewGroup) this;
12263 final int count = parent.getChildCount();
12264 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012265 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012266 child.outputDirtyFlags(indent + " ", clear, clearMask);
12267 }
12268 }
12269 }
12270
12271 /**
12272 * This method is used by ViewGroup to cause its children to restore or recreate their
12273 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12274 * to recreate its own display list, which would happen if it went through the normal
12275 * draw/dispatchDraw mechanisms.
12276 *
12277 * @hide
12278 */
12279 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012280
12281 /**
12282 * A view that is not attached or hardware accelerated cannot create a display list.
12283 * This method checks these conditions and returns the appropriate result.
12284 *
12285 * @return true if view has the ability to create a display list, false otherwise.
12286 *
12287 * @hide
12288 */
12289 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012290 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012291 }
Joe Malin32736f02011-01-19 16:14:20 -080012292
Chet Haasedaf98e92011-01-10 14:10:36 -080012293 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012294 * @return The HardwareRenderer associated with that view or null if hardware rendering
12295 * is not supported or this this has not been attached to a window.
12296 *
12297 * @hide
12298 */
12299 public HardwareRenderer getHardwareRenderer() {
12300 if (mAttachInfo != null) {
12301 return mAttachInfo.mHardwareRenderer;
12302 }
12303 return null;
12304 }
12305
12306 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012307 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12308 * Otherwise, the same display list will be returned (after having been rendered into
12309 * along the way, depending on the invalidation state of the view).
12310 *
12311 * @param displayList The previous version of this displayList, could be null.
12312 * @param isLayer Whether the requester of the display list is a layer. If so,
12313 * the view will avoid creating a layer inside the resulting display list.
12314 * @return A new or reused DisplayList object.
12315 */
12316 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12317 if (!canHaveDisplayList()) {
12318 return null;
12319 }
12320
12321 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12322 displayList == null || !displayList.isValid() ||
12323 (!isLayer && mRecreateDisplayList))) {
12324 // Don't need to recreate the display list, just need to tell our
12325 // children to restore/recreate theirs
12326 if (displayList != null && displayList.isValid() &&
12327 !isLayer && !mRecreateDisplayList) {
12328 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12329 mPrivateFlags &= ~DIRTY_MASK;
12330 dispatchGetDisplayList();
12331
12332 return displayList;
12333 }
12334
12335 if (!isLayer) {
12336 // If we got here, we're recreating it. Mark it as such to ensure that
12337 // we copy in child display lists into ours in drawChild()
12338 mRecreateDisplayList = true;
12339 }
12340 if (displayList == null) {
12341 final String name = getClass().getSimpleName();
12342 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12343 // If we're creating a new display list, make sure our parent gets invalidated
12344 // since they will need to recreate their display list to account for this
12345 // new child display list.
12346 invalidateParentCaches();
12347 }
12348
12349 boolean caching = false;
12350 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012351 int width = mRight - mLeft;
12352 int height = mBottom - mTop;
12353
12354 try {
12355 canvas.setViewport(width, height);
12356 // The dirty rect should always be null for a display list
12357 canvas.onPreDraw(null);
12358 int layerType = (
12359 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12360 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012361 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012362 if (layerType == LAYER_TYPE_HARDWARE) {
12363 final HardwareLayer layer = getHardwareLayer();
12364 if (layer != null && layer.isValid()) {
12365 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12366 } else {
12367 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12368 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12369 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12370 }
12371 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012372 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012373 buildDrawingCache(true);
12374 Bitmap cache = getDrawingCache(true);
12375 if (cache != null) {
12376 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12377 caching = true;
12378 }
Chet Haasea1cff502012-02-21 13:43:44 -080012379 }
Chet Haasea1cff502012-02-21 13:43:44 -080012380 } else {
12381
12382 computeScroll();
12383
Chet Haasea1cff502012-02-21 13:43:44 -080012384 canvas.translate(-mScrollX, -mScrollY);
12385 if (!isLayer) {
12386 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12387 mPrivateFlags &= ~DIRTY_MASK;
12388 }
12389
12390 // Fast path for layouts with no backgrounds
12391 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12392 dispatchDraw(canvas);
12393 } else {
12394 draw(canvas);
12395 }
12396 }
12397 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012398 canvas.onPostDraw();
12399
12400 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012401 displayList.setCaching(caching);
12402 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012403 displayList.setLeftTopRightBottom(0, 0, width, height);
12404 } else {
12405 setDisplayListProperties(displayList);
12406 }
12407 }
12408 } else if (!isLayer) {
12409 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12410 mPrivateFlags &= ~DIRTY_MASK;
12411 }
12412
12413 return displayList;
12414 }
12415
12416 /**
12417 * Get the DisplayList for the HardwareLayer
12418 *
12419 * @param layer The HardwareLayer whose DisplayList we want
12420 * @return A DisplayList fopr the specified HardwareLayer
12421 */
12422 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12423 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12424 layer.setDisplayList(displayList);
12425 return displayList;
12426 }
12427
12428
12429 /**
Romain Guyb051e892010-09-28 19:09:36 -070012430 * <p>Returns a display list that can be used to draw this view again
12431 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012432 *
Romain Guyb051e892010-09-28 19:09:36 -070012433 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012434 *
12435 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012436 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012437 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012438 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012439 return mDisplayList;
12440 }
12441
Romain Guy38c2ece2012-05-24 14:20:56 -070012442 private void clearDisplayList() {
12443 if (mDisplayList != null) {
12444 mDisplayList.invalidate();
12445 mDisplayList.clear();
12446 }
12447 }
12448
Romain Guyb051e892010-09-28 19:09:36 -070012449 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012450 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012451 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012452 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012453 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012454 * @see #getDrawingCache(boolean)
12455 */
12456 public Bitmap getDrawingCache() {
12457 return getDrawingCache(false);
12458 }
12459
12460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12462 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12463 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12464 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12465 * request the drawing cache by calling this method and draw it on screen if the
12466 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012467 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012468 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12469 * this method will create a bitmap of the same size as this view. Because this bitmap
12470 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12471 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12472 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12473 * size than the view. This implies that your application must be able to handle this
12474 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012475 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012476 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12477 * the current density of the screen when the application is in compatibility
12478 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012479 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012480 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012482 * @see #setDrawingCacheEnabled(boolean)
12483 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012484 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012485 * @see #destroyDrawingCache()
12486 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012487 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12489 return null;
12490 }
12491 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012492 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012493 }
Romain Guy02890fd2010-08-06 17:58:44 -070012494 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 }
12496
12497 /**
12498 * <p>Frees the resources used by the drawing cache. If you call
12499 * {@link #buildDrawingCache()} manually without calling
12500 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12501 * should cleanup the cache with this method afterwards.</p>
12502 *
12503 * @see #setDrawingCacheEnabled(boolean)
12504 * @see #buildDrawingCache()
12505 * @see #getDrawingCache()
12506 */
12507 public void destroyDrawingCache() {
12508 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012509 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012510 mDrawingCache = null;
12511 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012512 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012513 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012514 mUnscaledDrawingCache = null;
12515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012516 }
12517
12518 /**
12519 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012520 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012521 * view will always be drawn on top of a solid color.
12522 *
12523 * @param color The background color to use for the drawing cache's bitmap
12524 *
12525 * @see #setDrawingCacheEnabled(boolean)
12526 * @see #buildDrawingCache()
12527 * @see #getDrawingCache()
12528 */
12529 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012530 if (color != mDrawingCacheBackgroundColor) {
12531 mDrawingCacheBackgroundColor = color;
12532 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012534 }
12535
12536 /**
12537 * @see #setDrawingCacheBackgroundColor(int)
12538 *
12539 * @return The background color to used for the drawing cache's bitmap
12540 */
12541 public int getDrawingCacheBackgroundColor() {
12542 return mDrawingCacheBackgroundColor;
12543 }
12544
12545 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012546 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012547 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012548 * @see #buildDrawingCache(boolean)
12549 */
12550 public void buildDrawingCache() {
12551 buildDrawingCache(false);
12552 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012553
Romain Guyfbd8f692009-06-26 14:51:58 -070012554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012555 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12556 *
12557 * <p>If you call {@link #buildDrawingCache()} manually without calling
12558 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12559 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012560 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012561 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12562 * this method will create a bitmap of the same size as this view. Because this bitmap
12563 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12564 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12565 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12566 * size than the view. This implies that your application must be able to handle this
12567 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012568 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012569 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12570 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012571 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012572 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573 *
12574 * @see #getDrawingCache()
12575 * @see #destroyDrawingCache()
12576 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012577 public void buildDrawingCache(boolean autoScale) {
12578 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012579 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012580 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012581
Romain Guy8506ab42009-06-11 17:35:47 -070012582 int width = mRight - mLeft;
12583 int height = mBottom - mTop;
12584
12585 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012586 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012587
Romain Guye1123222009-06-29 14:24:56 -070012588 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012589 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12590 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592
12593 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012594 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012595 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012596
12597 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012598 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012599 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012600 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12601 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012602 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012603 return;
12604 }
12605
12606 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012607 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012608
12609 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610 Bitmap.Config quality;
12611 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012612 // Never pick ARGB_4444 because it looks awful
12613 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012614 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12615 case DRAWING_CACHE_QUALITY_AUTO:
12616 quality = Bitmap.Config.ARGB_8888;
12617 break;
12618 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012619 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012620 break;
12621 case DRAWING_CACHE_QUALITY_HIGH:
12622 quality = Bitmap.Config.ARGB_8888;
12623 break;
12624 default:
12625 quality = Bitmap.Config.ARGB_8888;
12626 break;
12627 }
12628 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012629 // Optimization for translucent windows
12630 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012631 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012632 }
12633
12634 // Try to cleanup memory
12635 if (bitmap != null) bitmap.recycle();
12636
12637 try {
12638 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012639 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012640 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012641 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012642 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012643 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012644 }
Adam Powell26153a32010-11-08 15:22:27 -080012645 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012646 } catch (OutOfMemoryError e) {
12647 // If there is not enough memory to create the bitmap cache, just
12648 // ignore the issue as bitmap caches are not required to draw the
12649 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012650 if (autoScale) {
12651 mDrawingCache = null;
12652 } else {
12653 mUnscaledDrawingCache = null;
12654 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012655 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012656 return;
12657 }
12658
12659 clear = drawingCacheBackgroundColor != 0;
12660 }
12661
12662 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012663 if (attachInfo != null) {
12664 canvas = attachInfo.mCanvas;
12665 if (canvas == null) {
12666 canvas = new Canvas();
12667 }
12668 canvas.setBitmap(bitmap);
12669 // Temporarily clobber the cached Canvas in case one of our children
12670 // is also using a drawing cache. Without this, the children would
12671 // steal the canvas by attaching their own bitmap to it and bad, bad
12672 // thing would happen (invisible views, corrupted drawings, etc.)
12673 attachInfo.mCanvas = null;
12674 } else {
12675 // This case should hopefully never or seldom happen
12676 canvas = new Canvas(bitmap);
12677 }
12678
12679 if (clear) {
12680 bitmap.eraseColor(drawingCacheBackgroundColor);
12681 }
12682
12683 computeScroll();
12684 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012685
Romain Guye1123222009-06-29 14:24:56 -070012686 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012687 final float scale = attachInfo.mApplicationScale;
12688 canvas.scale(scale, scale);
12689 }
Joe Malin32736f02011-01-19 16:14:20 -080012690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 canvas.translate(-mScrollX, -mScrollY);
12692
Romain Guy5bcdff42009-05-14 21:27:18 -070012693 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012694 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12695 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012696 mPrivateFlags |= DRAWING_CACHE_VALID;
12697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012698
12699 // Fast path for layouts with no backgrounds
12700 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012701 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012702 dispatchDraw(canvas);
12703 } else {
12704 draw(canvas);
12705 }
12706
12707 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012708 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012709
12710 if (attachInfo != null) {
12711 // Restore the cached Canvas for our siblings
12712 attachInfo.mCanvas = canvas;
12713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012714 }
12715 }
12716
12717 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012718 * Create a snapshot of the view into a bitmap. We should probably make
12719 * some form of this public, but should think about the API.
12720 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012721 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012722 int width = mRight - mLeft;
12723 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012724
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012725 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012726 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012727 width = (int) ((width * scale) + 0.5f);
12728 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012729
Romain Guy8c11e312009-09-14 15:15:30 -070012730 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012731 if (bitmap == null) {
12732 throw new OutOfMemoryError();
12733 }
12734
Romain Guyc529d8d2011-09-06 15:01:39 -070012735 Resources resources = getResources();
12736 if (resources != null) {
12737 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12738 }
Joe Malin32736f02011-01-19 16:14:20 -080012739
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012740 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012741 if (attachInfo != null) {
12742 canvas = attachInfo.mCanvas;
12743 if (canvas == null) {
12744 canvas = new Canvas();
12745 }
12746 canvas.setBitmap(bitmap);
12747 // Temporarily clobber the cached Canvas in case one of our children
12748 // is also using a drawing cache. Without this, the children would
12749 // steal the canvas by attaching their own bitmap to it and bad, bad
12750 // things would happen (invisible views, corrupted drawings, etc.)
12751 attachInfo.mCanvas = null;
12752 } else {
12753 // This case should hopefully never or seldom happen
12754 canvas = new Canvas(bitmap);
12755 }
12756
Romain Guy5bcdff42009-05-14 21:27:18 -070012757 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012758 bitmap.eraseColor(backgroundColor);
12759 }
12760
12761 computeScroll();
12762 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012763 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012764 canvas.translate(-mScrollX, -mScrollY);
12765
Romain Guy5bcdff42009-05-14 21:27:18 -070012766 // Temporarily remove the dirty mask
12767 int flags = mPrivateFlags;
12768 mPrivateFlags &= ~DIRTY_MASK;
12769
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012770 // Fast path for layouts with no backgrounds
12771 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12772 dispatchDraw(canvas);
12773 } else {
12774 draw(canvas);
12775 }
12776
Romain Guy5bcdff42009-05-14 21:27:18 -070012777 mPrivateFlags = flags;
12778
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012779 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012780 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012781
12782 if (attachInfo != null) {
12783 // Restore the cached Canvas for our siblings
12784 attachInfo.mCanvas = canvas;
12785 }
Romain Guy8506ab42009-06-11 17:35:47 -070012786
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012787 return bitmap;
12788 }
12789
12790 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012791 * Indicates whether this View is currently in edit mode. A View is usually
12792 * in edit mode when displayed within a developer tool. For instance, if
12793 * this View is being drawn by a visual user interface builder, this method
12794 * should return true.
12795 *
12796 * Subclasses should check the return value of this method to provide
12797 * different behaviors if their normal behavior might interfere with the
12798 * host environment. For instance: the class spawns a thread in its
12799 * constructor, the drawing code relies on device-specific features, etc.
12800 *
12801 * This method is usually checked in the drawing code of custom widgets.
12802 *
12803 * @return True if this View is in edit mode, false otherwise.
12804 */
12805 public boolean isInEditMode() {
12806 return false;
12807 }
12808
12809 /**
12810 * If the View draws content inside its padding and enables fading edges,
12811 * it needs to support padding offsets. Padding offsets are added to the
12812 * fading edges to extend the length of the fade so that it covers pixels
12813 * drawn inside the padding.
12814 *
12815 * Subclasses of this class should override this method if they need
12816 * to draw content inside the padding.
12817 *
12818 * @return True if padding offset must be applied, false otherwise.
12819 *
12820 * @see #getLeftPaddingOffset()
12821 * @see #getRightPaddingOffset()
12822 * @see #getTopPaddingOffset()
12823 * @see #getBottomPaddingOffset()
12824 *
12825 * @since CURRENT
12826 */
12827 protected boolean isPaddingOffsetRequired() {
12828 return false;
12829 }
12830
12831 /**
12832 * Amount by which to extend the left fading region. Called only when
12833 * {@link #isPaddingOffsetRequired()} returns true.
12834 *
12835 * @return The left padding offset in pixels.
12836 *
12837 * @see #isPaddingOffsetRequired()
12838 *
12839 * @since CURRENT
12840 */
12841 protected int getLeftPaddingOffset() {
12842 return 0;
12843 }
12844
12845 /**
12846 * Amount by which to extend the right fading region. Called only when
12847 * {@link #isPaddingOffsetRequired()} returns true.
12848 *
12849 * @return The right padding offset in pixels.
12850 *
12851 * @see #isPaddingOffsetRequired()
12852 *
12853 * @since CURRENT
12854 */
12855 protected int getRightPaddingOffset() {
12856 return 0;
12857 }
12858
12859 /**
12860 * Amount by which to extend the top fading region. Called only when
12861 * {@link #isPaddingOffsetRequired()} returns true.
12862 *
12863 * @return The top padding offset in pixels.
12864 *
12865 * @see #isPaddingOffsetRequired()
12866 *
12867 * @since CURRENT
12868 */
12869 protected int getTopPaddingOffset() {
12870 return 0;
12871 }
12872
12873 /**
12874 * Amount by which to extend the bottom fading region. Called only when
12875 * {@link #isPaddingOffsetRequired()} returns true.
12876 *
12877 * @return The bottom padding offset in pixels.
12878 *
12879 * @see #isPaddingOffsetRequired()
12880 *
12881 * @since CURRENT
12882 */
12883 protected int getBottomPaddingOffset() {
12884 return 0;
12885 }
12886
12887 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012888 * @hide
12889 * @param offsetRequired
12890 */
12891 protected int getFadeTop(boolean offsetRequired) {
12892 int top = mPaddingTop;
12893 if (offsetRequired) top += getTopPaddingOffset();
12894 return top;
12895 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012896
Romain Guyf2fc4602011-07-19 15:20:03 -070012897 /**
12898 * @hide
12899 * @param offsetRequired
12900 */
12901 protected int getFadeHeight(boolean offsetRequired) {
12902 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012903 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012904 return mBottom - mTop - mPaddingBottom - padding;
12905 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012906
Romain Guyf2fc4602011-07-19 15:20:03 -070012907 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012908 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012909 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012910 *
Romain Guy2bffd262010-09-12 17:40:02 -070012911 * <p>Even if this method returns true, it does not mean that every call
12912 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12913 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012914 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012915 * window is hardware accelerated,
12916 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12917 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012918 *
Romain Guy2bffd262010-09-12 17:40:02 -070012919 * @return True if the view is attached to a window and the window is
12920 * hardware accelerated; false in any other case.
12921 */
12922 public boolean isHardwareAccelerated() {
12923 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12924 }
Joe Malin32736f02011-01-19 16:14:20 -080012925
Romain Guy2bffd262010-09-12 17:40:02 -070012926 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012927 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12928 * case of an active Animation being run on the view.
12929 */
12930 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12931 Animation a, boolean scalingRequired) {
12932 Transformation invalidationTransform;
12933 final int flags = parent.mGroupFlags;
12934 final boolean initialized = a.isInitialized();
12935 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012936 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012937 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012938 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012939 onAnimationStart();
12940 }
12941
12942 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12943 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12944 if (parent.mInvalidationTransformation == null) {
12945 parent.mInvalidationTransformation = new Transformation();
12946 }
12947 invalidationTransform = parent.mInvalidationTransformation;
12948 a.getTransformation(drawingTime, invalidationTransform, 1f);
12949 } else {
12950 invalidationTransform = parent.mChildTransformation;
12951 }
Romain Guy393a52c2012-05-22 20:21:08 -070012952
Chet Haasebcca79a2012-02-14 08:45:14 -080012953 if (more) {
12954 if (!a.willChangeBounds()) {
12955 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12956 parent.FLAG_OPTIMIZE_INVALIDATE) {
12957 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12958 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12959 // The child need to draw an animation, potentially offscreen, so
12960 // make sure we do not cancel invalidate requests
12961 parent.mPrivateFlags |= DRAW_ANIMATION;
12962 parent.invalidate(mLeft, mTop, mRight, mBottom);
12963 }
12964 } else {
12965 if (parent.mInvalidateRegion == null) {
12966 parent.mInvalidateRegion = new RectF();
12967 }
12968 final RectF region = parent.mInvalidateRegion;
12969 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12970 invalidationTransform);
12971
12972 // The child need to draw an animation, potentially offscreen, so
12973 // make sure we do not cancel invalidate requests
12974 parent.mPrivateFlags |= DRAW_ANIMATION;
12975
12976 final int left = mLeft + (int) region.left;
12977 final int top = mTop + (int) region.top;
12978 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12979 top + (int) (region.height() + .5f));
12980 }
12981 }
12982 return more;
12983 }
12984
Chet Haasea1cff502012-02-21 13:43:44 -080012985 /**
12986 * This method is called by getDisplayList() when a display list is created or re-rendered.
12987 * It sets or resets the current value of all properties on that display list (resetting is
12988 * necessary when a display list is being re-created, because we need to make sure that
12989 * previously-set transform values
12990 */
12991 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012992 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012993 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012994 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012995 if (mParent instanceof ViewGroup) {
12996 displayList.setClipChildren(
12997 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12998 }
Chet Haase9420abd2012-03-29 16:28:32 -070012999 float alpha = 1;
13000 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13001 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13002 ViewGroup parentVG = (ViewGroup) mParent;
13003 final boolean hasTransform =
13004 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13005 if (hasTransform) {
13006 Transformation transform = parentVG.mChildTransformation;
13007 final int transformType = parentVG.mChildTransformation.getTransformationType();
13008 if (transformType != Transformation.TYPE_IDENTITY) {
13009 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13010 alpha = transform.getAlpha();
13011 }
13012 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13013 displayList.setStaticMatrix(transform.getMatrix());
13014 }
13015 }
13016 }
Chet Haasea1cff502012-02-21 13:43:44 -080013017 }
13018 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013019 alpha *= mTransformationInfo.mAlpha;
13020 if (alpha < 1) {
13021 final int multipliedAlpha = (int) (255 * alpha);
13022 if (onSetAlpha(multipliedAlpha)) {
13023 alpha = 1;
13024 }
13025 }
13026 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013027 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13028 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13029 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13030 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013031 if (mTransformationInfo.mCamera == null) {
13032 mTransformationInfo.mCamera = new Camera();
13033 mTransformationInfo.matrix3D = new Matrix();
13034 }
13035 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080013036 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
13037 displayList.setPivotX(getPivotX());
13038 displayList.setPivotY(getPivotY());
13039 }
Chet Haase9420abd2012-03-29 16:28:32 -070013040 } else if (alpha < 1) {
13041 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013042 }
13043 }
13044 }
13045
Chet Haasebcca79a2012-02-14 08:45:14 -080013046 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013047 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13048 * This draw() method is an implementation detail and is not intended to be overridden or
13049 * to be called from anywhere else other than ViewGroup.drawChild().
13050 */
13051 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013052 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013053 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013054 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013055 final int flags = parent.mGroupFlags;
13056
Chet Haasea1cff502012-02-21 13:43:44 -080013057 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013058 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013059 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013060 }
13061
13062 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013063 boolean concatMatrix = false;
13064
13065 boolean scalingRequired = false;
13066 boolean caching;
13067 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
13068
13069 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013070 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13071 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013072 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013073 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013074 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13075 } else {
13076 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13077 }
13078
Chet Haasebcca79a2012-02-14 08:45:14 -080013079 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013080 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013081 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013082 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013083 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070013084 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013085 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013086 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013087 } else {
Chet Haase21433372012-06-05 07:54:09 -070013088 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013089 mDisplayList != null) {
13090 // No longer animating: clear out old animation matrix
13091 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070013092 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013093 }
13094 if (!useDisplayListProperties &&
13095 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13096 final boolean hasTransform =
13097 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13098 if (hasTransform) {
13099 final int transformType = parent.mChildTransformation.getTransformationType();
13100 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13101 parent.mChildTransformation : null;
13102 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13103 }
Chet Haase64a48c12012-02-13 16:33:29 -080013104 }
13105 }
13106
13107 concatMatrix |= !childHasIdentityMatrix;
13108
13109 // Sets the flag as early as possible to allow draw() implementations
13110 // to call invalidate() successfully when doing animations
13111 mPrivateFlags |= DRAWN;
13112
Chet Haasebcca79a2012-02-14 08:45:14 -080013113 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013114 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013115 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013116 return more;
13117 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013118 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013119
13120 if (hardwareAccelerated) {
13121 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13122 // retain the flag's value temporarily in the mRecreateDisplayList flag
13123 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13124 mPrivateFlags &= ~INVALIDATED;
13125 }
13126
13127 computeScroll();
13128
13129 final int sx = mScrollX;
13130 final int sy = mScrollY;
13131
13132 DisplayList displayList = null;
13133 Bitmap cache = null;
13134 boolean hasDisplayList = false;
13135 if (caching) {
13136 if (!hardwareAccelerated) {
13137 if (layerType != LAYER_TYPE_NONE) {
13138 layerType = LAYER_TYPE_SOFTWARE;
13139 buildDrawingCache(true);
13140 }
13141 cache = getDrawingCache(true);
13142 } else {
13143 switch (layerType) {
13144 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013145 if (useDisplayListProperties) {
13146 hasDisplayList = canHaveDisplayList();
13147 } else {
13148 buildDrawingCache(true);
13149 cache = getDrawingCache(true);
13150 }
Chet Haase64a48c12012-02-13 16:33:29 -080013151 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013152 case LAYER_TYPE_HARDWARE:
13153 if (useDisplayListProperties) {
13154 hasDisplayList = canHaveDisplayList();
13155 }
13156 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013157 case LAYER_TYPE_NONE:
13158 // Delay getting the display list until animation-driven alpha values are
13159 // set up and possibly passed on to the view
13160 hasDisplayList = canHaveDisplayList();
13161 break;
13162 }
13163 }
13164 }
Chet Haasea1cff502012-02-21 13:43:44 -080013165 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013166 if (useDisplayListProperties) {
13167 displayList = getDisplayList();
13168 if (!displayList.isValid()) {
13169 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13170 // to getDisplayList(), the display list will be marked invalid and we should not
13171 // try to use it again.
13172 displayList = null;
13173 hasDisplayList = false;
13174 useDisplayListProperties = false;
13175 }
13176 }
Chet Haase64a48c12012-02-13 16:33:29 -080013177
13178 final boolean hasNoCache = cache == null || hasDisplayList;
13179 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13180 layerType != LAYER_TYPE_HARDWARE;
13181
Chet Haasea1cff502012-02-21 13:43:44 -080013182 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013183 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013184 restoreTo = canvas.save();
13185 }
Chet Haase64a48c12012-02-13 16:33:29 -080013186 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013187 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013188 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013189 if (!useDisplayListProperties) {
13190 canvas.translate(mLeft, mTop);
13191 }
Chet Haase64a48c12012-02-13 16:33:29 -080013192 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013193 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013194 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013195 restoreTo = canvas.save();
13196 }
Chet Haase64a48c12012-02-13 16:33:29 -080013197 // mAttachInfo cannot be null, otherwise scalingRequired == false
13198 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13199 canvas.scale(scale, scale);
13200 }
13201 }
13202
Chet Haasea1cff502012-02-21 13:43:44 -080013203 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013204 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
13205 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013206 if (transformToApply != null || !childHasIdentityMatrix) {
13207 int transX = 0;
13208 int transY = 0;
13209
13210 if (offsetForScroll) {
13211 transX = -sx;
13212 transY = -sy;
13213 }
13214
13215 if (transformToApply != null) {
13216 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013217 if (useDisplayListProperties) {
13218 displayList.setAnimationMatrix(transformToApply.getMatrix());
13219 } else {
13220 // Undo the scroll translation, apply the transformation matrix,
13221 // then redo the scroll translate to get the correct result.
13222 canvas.translate(-transX, -transY);
13223 canvas.concat(transformToApply.getMatrix());
13224 canvas.translate(transX, transY);
13225 }
Chet Haasea1cff502012-02-21 13:43:44 -080013226 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013227 }
13228
13229 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013230 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013231 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013232 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013233 }
13234 }
13235
Chet Haasea1cff502012-02-21 13:43:44 -080013236 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013237 canvas.translate(-transX, -transY);
13238 canvas.concat(getMatrix());
13239 canvas.translate(transX, transY);
13240 }
13241 }
13242
Chet Haase21433372012-06-05 07:54:09 -070013243 // Deal with alpha if it is or used to be <1
13244 if (alpha < 1 ||
13245 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13246 if (alpha < 1) {
13247 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13248 } else {
13249 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13250 }
Chet Haasea1cff502012-02-21 13:43:44 -080013251 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013252 if (hasNoCache) {
13253 final int multipliedAlpha = (int) (255 * alpha);
13254 if (!onSetAlpha(multipliedAlpha)) {
13255 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013256 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013257 layerType != LAYER_TYPE_NONE) {
13258 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13259 }
Chet Haase9420abd2012-03-29 16:28:32 -070013260 if (useDisplayListProperties) {
13261 displayList.setAlpha(alpha * getAlpha());
13262 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013263 final int scrollX = hasDisplayList ? 0 : sx;
13264 final int scrollY = hasDisplayList ? 0 : sy;
13265 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13266 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013267 }
13268 } else {
13269 // Alpha is handled by the child directly, clobber the layer's alpha
13270 mPrivateFlags |= ALPHA_SET;
13271 }
13272 }
13273 }
13274 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13275 onSetAlpha(255);
13276 mPrivateFlags &= ~ALPHA_SET;
13277 }
13278
Chet Haasea1cff502012-02-21 13:43:44 -080013279 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13280 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013281 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013282 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013283 } else {
13284 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013285 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013286 } else {
13287 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13288 }
13289 }
13290 }
13291
Chet Haase9420abd2012-03-29 16:28:32 -070013292 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013293 displayList = getDisplayList();
13294 if (!displayList.isValid()) {
13295 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13296 // to getDisplayList(), the display list will be marked invalid and we should not
13297 // try to use it again.
13298 displayList = null;
13299 hasDisplayList = false;
13300 }
13301 }
13302
13303 if (hasNoCache) {
13304 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013305 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013306 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013307 if (layer != null && layer.isValid()) {
13308 mLayerPaint.setAlpha((int) (alpha * 255));
13309 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13310 layerRendered = true;
13311 } else {
13312 final int scrollX = hasDisplayList ? 0 : sx;
13313 final int scrollY = hasDisplayList ? 0 : sy;
13314 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013315 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013316 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13317 }
13318 }
13319
13320 if (!layerRendered) {
13321 if (!hasDisplayList) {
13322 // Fast path for layouts with no backgrounds
13323 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013324 mPrivateFlags &= ~DIRTY_MASK;
13325 dispatchDraw(canvas);
13326 } else {
13327 draw(canvas);
13328 }
13329 } else {
13330 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013331 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013332 }
13333 }
13334 } else if (cache != null) {
13335 mPrivateFlags &= ~DIRTY_MASK;
13336 Paint cachePaint;
13337
13338 if (layerType == LAYER_TYPE_NONE) {
13339 cachePaint = parent.mCachePaint;
13340 if (cachePaint == null) {
13341 cachePaint = new Paint();
13342 cachePaint.setDither(false);
13343 parent.mCachePaint = cachePaint;
13344 }
Chet Haase9420abd2012-03-29 16:28:32 -070013345 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013346 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013347 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13348 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013349 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013350 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013351 }
13352 } else {
13353 cachePaint = mLayerPaint;
13354 cachePaint.setAlpha((int) (alpha * 255));
13355 }
13356 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13357 }
13358
Chet Haasea1cff502012-02-21 13:43:44 -080013359 if (restoreTo >= 0) {
13360 canvas.restoreToCount(restoreTo);
13361 }
Chet Haase64a48c12012-02-13 16:33:29 -080013362
13363 if (a != null && !more) {
13364 if (!hardwareAccelerated && !a.getFillAfter()) {
13365 onSetAlpha(255);
13366 }
13367 parent.finishAnimatingView(this, a);
13368 }
13369
13370 if (more && hardwareAccelerated) {
13371 // invalidation is the trigger to recreate display lists, so if we're using
13372 // display lists to render, force an invalidate to allow the animation to
13373 // continue drawing another frame
13374 parent.invalidate(true);
13375 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13376 // alpha animations should cause the child to recreate its display list
13377 invalidate(true);
13378 }
13379 }
13380
13381 mRecreateDisplayList = false;
13382
13383 return more;
13384 }
13385
13386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013387 * Manually render this view (and all of its children) to the given Canvas.
13388 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013389 * called. When implementing a view, implement
13390 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13391 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013392 *
13393 * @param canvas The Canvas to which the View is rendered.
13394 */
13395 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013396 final int privateFlags = mPrivateFlags;
13397 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13398 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13399 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013401 /*
13402 * Draw traversal performs several drawing steps which must be executed
13403 * in the appropriate order:
13404 *
13405 * 1. Draw the background
13406 * 2. If necessary, save the canvas' layers to prepare for fading
13407 * 3. Draw view's content
13408 * 4. Draw children
13409 * 5. If necessary, draw the fading edges and restore layers
13410 * 6. Draw decorations (scrollbars for instance)
13411 */
13412
13413 // Step 1, draw the background, if needed
13414 int saveCount;
13415
Romain Guy24443ea2009-05-11 11:56:30 -070013416 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013417 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013418 if (background != null) {
13419 final int scrollX = mScrollX;
13420 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013421
Romain Guy24443ea2009-05-11 11:56:30 -070013422 if (mBackgroundSizeChanged) {
13423 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13424 mBackgroundSizeChanged = false;
13425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013426
Romain Guy24443ea2009-05-11 11:56:30 -070013427 if ((scrollX | scrollY) == 0) {
13428 background.draw(canvas);
13429 } else {
13430 canvas.translate(scrollX, scrollY);
13431 background.draw(canvas);
13432 canvas.translate(-scrollX, -scrollY);
13433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013434 }
13435 }
13436
13437 // skip step 2 & 5 if possible (common case)
13438 final int viewFlags = mViewFlags;
13439 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13440 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13441 if (!verticalEdges && !horizontalEdges) {
13442 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013443 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444
13445 // Step 4, draw the children
13446 dispatchDraw(canvas);
13447
13448 // Step 6, draw decorations (scrollbars)
13449 onDrawScrollBars(canvas);
13450
13451 // we're done...
13452 return;
13453 }
13454
13455 /*
13456 * Here we do the full fledged routine...
13457 * (this is an uncommon case where speed matters less,
13458 * this is why we repeat some of the tests that have been
13459 * done above)
13460 */
13461
13462 boolean drawTop = false;
13463 boolean drawBottom = false;
13464 boolean drawLeft = false;
13465 boolean drawRight = false;
13466
13467 float topFadeStrength = 0.0f;
13468 float bottomFadeStrength = 0.0f;
13469 float leftFadeStrength = 0.0f;
13470 float rightFadeStrength = 0.0f;
13471
13472 // Step 2, save the canvas' layers
13473 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013474
13475 final boolean offsetRequired = isPaddingOffsetRequired();
13476 if (offsetRequired) {
13477 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478 }
13479
13480 int left = mScrollX + paddingLeft;
13481 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013482 int top = mScrollY + getFadeTop(offsetRequired);
13483 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013484
13485 if (offsetRequired) {
13486 right += getRightPaddingOffset();
13487 bottom += getBottomPaddingOffset();
13488 }
13489
13490 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013491 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013492 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493
13494 // clip the fade length if top and bottom fades overlap
13495 // overlapping fades produce odd-looking artifacts
13496 if (verticalEdges && (top + length > bottom - length)) {
13497 length = (bottom - top) / 2;
13498 }
13499
13500 // also clip horizontal fades if necessary
13501 if (horizontalEdges && (left + length > right - length)) {
13502 length = (right - left) / 2;
13503 }
13504
13505 if (verticalEdges) {
13506 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013507 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013508 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013509 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 }
13511
13512 if (horizontalEdges) {
13513 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013514 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013515 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013516 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013517 }
13518
13519 saveCount = canvas.getSaveCount();
13520
13521 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013522 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013523 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13524
13525 if (drawTop) {
13526 canvas.saveLayer(left, top, right, top + length, null, flags);
13527 }
13528
13529 if (drawBottom) {
13530 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13531 }
13532
13533 if (drawLeft) {
13534 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13535 }
13536
13537 if (drawRight) {
13538 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13539 }
13540 } else {
13541 scrollabilityCache.setFadeColor(solidColor);
13542 }
13543
13544 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013545 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013546
13547 // Step 4, draw the children
13548 dispatchDraw(canvas);
13549
13550 // Step 5, draw the fade effect and restore layers
13551 final Paint p = scrollabilityCache.paint;
13552 final Matrix matrix = scrollabilityCache.matrix;
13553 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013554
13555 if (drawTop) {
13556 matrix.setScale(1, fadeHeight * topFadeStrength);
13557 matrix.postTranslate(left, top);
13558 fade.setLocalMatrix(matrix);
13559 canvas.drawRect(left, top, right, top + length, p);
13560 }
13561
13562 if (drawBottom) {
13563 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13564 matrix.postRotate(180);
13565 matrix.postTranslate(left, bottom);
13566 fade.setLocalMatrix(matrix);
13567 canvas.drawRect(left, bottom - length, right, bottom, p);
13568 }
13569
13570 if (drawLeft) {
13571 matrix.setScale(1, fadeHeight * leftFadeStrength);
13572 matrix.postRotate(-90);
13573 matrix.postTranslate(left, top);
13574 fade.setLocalMatrix(matrix);
13575 canvas.drawRect(left, top, left + length, bottom, p);
13576 }
13577
13578 if (drawRight) {
13579 matrix.setScale(1, fadeHeight * rightFadeStrength);
13580 matrix.postRotate(90);
13581 matrix.postTranslate(right, top);
13582 fade.setLocalMatrix(matrix);
13583 canvas.drawRect(right - length, top, right, bottom, p);
13584 }
13585
13586 canvas.restoreToCount(saveCount);
13587
13588 // Step 6, draw decorations (scrollbars)
13589 onDrawScrollBars(canvas);
13590 }
13591
13592 /**
13593 * Override this if your view is known to always be drawn on top of a solid color background,
13594 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13595 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13596 * should be set to 0xFF.
13597 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013598 * @see #setVerticalFadingEdgeEnabled(boolean)
13599 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013600 *
13601 * @return The known solid color background for this view, or 0 if the color may vary
13602 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013603 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013604 public int getSolidColor() {
13605 return 0;
13606 }
13607
13608 /**
13609 * Build a human readable string representation of the specified view flags.
13610 *
13611 * @param flags the view flags to convert to a string
13612 * @return a String representing the supplied flags
13613 */
13614 private static String printFlags(int flags) {
13615 String output = "";
13616 int numFlags = 0;
13617 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13618 output += "TAKES_FOCUS";
13619 numFlags++;
13620 }
13621
13622 switch (flags & VISIBILITY_MASK) {
13623 case INVISIBLE:
13624 if (numFlags > 0) {
13625 output += " ";
13626 }
13627 output += "INVISIBLE";
13628 // USELESS HERE numFlags++;
13629 break;
13630 case GONE:
13631 if (numFlags > 0) {
13632 output += " ";
13633 }
13634 output += "GONE";
13635 // USELESS HERE numFlags++;
13636 break;
13637 default:
13638 break;
13639 }
13640 return output;
13641 }
13642
13643 /**
13644 * Build a human readable string representation of the specified private
13645 * view flags.
13646 *
13647 * @param privateFlags the private view flags to convert to a string
13648 * @return a String representing the supplied flags
13649 */
13650 private static String printPrivateFlags(int privateFlags) {
13651 String output = "";
13652 int numFlags = 0;
13653
13654 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13655 output += "WANTS_FOCUS";
13656 numFlags++;
13657 }
13658
13659 if ((privateFlags & FOCUSED) == FOCUSED) {
13660 if (numFlags > 0) {
13661 output += " ";
13662 }
13663 output += "FOCUSED";
13664 numFlags++;
13665 }
13666
13667 if ((privateFlags & SELECTED) == SELECTED) {
13668 if (numFlags > 0) {
13669 output += " ";
13670 }
13671 output += "SELECTED";
13672 numFlags++;
13673 }
13674
13675 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13676 if (numFlags > 0) {
13677 output += " ";
13678 }
13679 output += "IS_ROOT_NAMESPACE";
13680 numFlags++;
13681 }
13682
13683 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13684 if (numFlags > 0) {
13685 output += " ";
13686 }
13687 output += "HAS_BOUNDS";
13688 numFlags++;
13689 }
13690
13691 if ((privateFlags & DRAWN) == DRAWN) {
13692 if (numFlags > 0) {
13693 output += " ";
13694 }
13695 output += "DRAWN";
13696 // USELESS HERE numFlags++;
13697 }
13698 return output;
13699 }
13700
13701 /**
13702 * <p>Indicates whether or not this view's layout will be requested during
13703 * the next hierarchy layout pass.</p>
13704 *
13705 * @return true if the layout will be forced during next layout pass
13706 */
13707 public boolean isLayoutRequested() {
13708 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13709 }
13710
13711 /**
13712 * Assign a size and position to a view and all of its
13713 * descendants
13714 *
13715 * <p>This is the second phase of the layout mechanism.
13716 * (The first is measuring). In this phase, each parent calls
13717 * layout on all of its children to position them.
13718 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013719 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013720 *
Chet Haase9c087442011-01-12 16:20:16 -080013721 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013722 * Derived classes with children should override
13723 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013724 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013725 *
13726 * @param l Left position, relative to parent
13727 * @param t Top position, relative to parent
13728 * @param r Right position, relative to parent
13729 * @param b Bottom position, relative to parent
13730 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013731 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013732 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013733 int oldL = mLeft;
13734 int oldT = mTop;
13735 int oldB = mBottom;
13736 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737 boolean changed = setFrame(l, t, r, b);
13738 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 onLayout(changed, l, t, r, b);
13740 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013741
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013742 ListenerInfo li = mListenerInfo;
13743 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013744 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013745 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013746 int numListeners = listenersCopy.size();
13747 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013748 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013749 }
13750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 }
13752 mPrivateFlags &= ~FORCE_LAYOUT;
13753 }
13754
13755 /**
13756 * Called from layout when this view should
13757 * assign a size and position to each of its children.
13758 *
13759 * Derived classes with children should override
13760 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013761 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762 * @param changed This is a new size or position for this view
13763 * @param left Left position, relative to parent
13764 * @param top Top position, relative to parent
13765 * @param right Right position, relative to parent
13766 * @param bottom Bottom position, relative to parent
13767 */
13768 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13769 }
13770
13771 /**
13772 * Assign a size and position to this view.
13773 *
13774 * This is called from layout.
13775 *
13776 * @param left Left position, relative to parent
13777 * @param top Top position, relative to parent
13778 * @param right Right position, relative to parent
13779 * @param bottom Bottom position, relative to parent
13780 * @return true if the new size and position are different than the
13781 * previous ones
13782 * {@hide}
13783 */
13784 protected boolean setFrame(int left, int top, int right, int bottom) {
13785 boolean changed = false;
13786
13787 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013788 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013789 + right + "," + bottom + ")");
13790 }
13791
13792 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13793 changed = true;
13794
13795 // Remember our drawn bit
13796 int drawn = mPrivateFlags & DRAWN;
13797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013798 int oldWidth = mRight - mLeft;
13799 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013800 int newWidth = right - left;
13801 int newHeight = bottom - top;
13802 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13803
13804 // Invalidate our old position
13805 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013806
13807 mLeft = left;
13808 mTop = top;
13809 mRight = right;
13810 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013811 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013812 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013814
13815 mPrivateFlags |= HAS_BOUNDS;
13816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013817
Chet Haase75755e22011-07-18 17:48:25 -070013818 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013819 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13820 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013821 if (mTransformationInfo != null) {
13822 mTransformationInfo.mMatrixDirty = true;
13823 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013825 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13826 }
13827
13828 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13829 // If we are visible, force the DRAWN bit to on so that
13830 // this invalidate will go through (at least to our parent).
13831 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013832 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833 // the DRAWN bit.
13834 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013835 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013836 // parent display list may need to be recreated based on a change in the bounds
13837 // of any child
13838 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839 }
13840
13841 // Reset drawn bit to original value (invalidate turns it off)
13842 mPrivateFlags |= drawn;
13843
13844 mBackgroundSizeChanged = true;
13845 }
13846 return changed;
13847 }
13848
13849 /**
13850 * Finalize inflating a view from XML. This is called as the last phase
13851 * of inflation, after all child views have been added.
13852 *
13853 * <p>Even if the subclass overrides onFinishInflate, they should always be
13854 * sure to call the super method, so that we get called.
13855 */
13856 protected void onFinishInflate() {
13857 }
13858
13859 /**
13860 * Returns the resources associated with this view.
13861 *
13862 * @return Resources object.
13863 */
13864 public Resources getResources() {
13865 return mResources;
13866 }
13867
13868 /**
13869 * Invalidates the specified Drawable.
13870 *
13871 * @param drawable the drawable to invalidate
13872 */
13873 public void invalidateDrawable(Drawable drawable) {
13874 if (verifyDrawable(drawable)) {
13875 final Rect dirty = drawable.getBounds();
13876 final int scrollX = mScrollX;
13877 final int scrollY = mScrollY;
13878
13879 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13880 dirty.right + scrollX, dirty.bottom + scrollY);
13881 }
13882 }
13883
13884 /**
13885 * Schedules an action on a drawable to occur at a specified time.
13886 *
13887 * @param who the recipient of the action
13888 * @param what the action to run on the drawable
13889 * @param when the time at which the action must occur. Uses the
13890 * {@link SystemClock#uptimeMillis} timebase.
13891 */
13892 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013893 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013894 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013895 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013896 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13897 Choreographer.CALLBACK_ANIMATION, what, who,
13898 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013899 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013900 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013902 }
13903 }
13904
13905 /**
13906 * Cancels a scheduled action on a drawable.
13907 *
13908 * @param who the recipient of the action
13909 * @param what the action to cancel
13910 */
13911 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013912 if (verifyDrawable(who) && what != null) {
13913 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013914 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13915 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013916 } else {
13917 ViewRootImpl.getRunQueue().removeCallbacks(what);
13918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013919 }
13920 }
13921
13922 /**
13923 * Unschedule any events associated with the given Drawable. This can be
13924 * used when selecting a new Drawable into a view, so that the previous
13925 * one is completely unscheduled.
13926 *
13927 * @param who The Drawable to unschedule.
13928 *
13929 * @see #drawableStateChanged
13930 */
13931 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013932 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013933 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13934 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 }
13936 }
13937
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013938 /**
13939 * Return the layout direction of a given Drawable.
13940 *
13941 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013942 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013943 */
13944 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013945 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013946 }
13947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013948 /**
13949 * If your view subclass is displaying its own Drawable objects, it should
13950 * override this function and return true for any Drawable it is
13951 * displaying. This allows animations for those drawables to be
13952 * scheduled.
13953 *
13954 * <p>Be sure to call through to the super class when overriding this
13955 * function.
13956 *
13957 * @param who The Drawable to verify. Return true if it is one you are
13958 * displaying, else return the result of calling through to the
13959 * super class.
13960 *
13961 * @return boolean If true than the Drawable is being displayed in the
13962 * view; else false and it is not allowed to animate.
13963 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013964 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13965 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013966 */
13967 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013968 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013969 }
13970
13971 /**
13972 * This function is called whenever the state of the view changes in such
13973 * a way that it impacts the state of drawables being shown.
13974 *
13975 * <p>Be sure to call through to the superclass when overriding this
13976 * function.
13977 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013978 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979 */
13980 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013981 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013982 if (d != null && d.isStateful()) {
13983 d.setState(getDrawableState());
13984 }
13985 }
13986
13987 /**
13988 * Call this to force a view to update its drawable state. This will cause
13989 * drawableStateChanged to be called on this view. Views that are interested
13990 * in the new state should call getDrawableState.
13991 *
13992 * @see #drawableStateChanged
13993 * @see #getDrawableState
13994 */
13995 public void refreshDrawableState() {
13996 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13997 drawableStateChanged();
13998
13999 ViewParent parent = mParent;
14000 if (parent != null) {
14001 parent.childDrawableStateChanged(this);
14002 }
14003 }
14004
14005 /**
14006 * Return an array of resource IDs of the drawable states representing the
14007 * current state of the view.
14008 *
14009 * @return The current drawable state
14010 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014011 * @see Drawable#setState(int[])
14012 * @see #drawableStateChanged()
14013 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 */
14015 public final int[] getDrawableState() {
14016 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
14017 return mDrawableState;
14018 } else {
14019 mDrawableState = onCreateDrawableState(0);
14020 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
14021 return mDrawableState;
14022 }
14023 }
14024
14025 /**
14026 * Generate the new {@link android.graphics.drawable.Drawable} state for
14027 * this view. This is called by the view
14028 * system when the cached Drawable state is determined to be invalid. To
14029 * retrieve the current state, you should use {@link #getDrawableState}.
14030 *
14031 * @param extraSpace if non-zero, this is the number of extra entries you
14032 * would like in the returned array in which you can place your own
14033 * states.
14034 *
14035 * @return Returns an array holding the current {@link Drawable} state of
14036 * the view.
14037 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014038 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039 */
14040 protected int[] onCreateDrawableState(int extraSpace) {
14041 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14042 mParent instanceof View) {
14043 return ((View) mParent).onCreateDrawableState(extraSpace);
14044 }
14045
14046 int[] drawableState;
14047
14048 int privateFlags = mPrivateFlags;
14049
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014050 int viewStateIndex = 0;
14051 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
14052 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14053 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070014054 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014055 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
14056 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014057 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14058 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014059 // This is set if HW acceleration is requested, even if the current
14060 // process doesn't allow it. This is just to allow app preview
14061 // windows to better match their app.
14062 viewStateIndex |= VIEW_STATE_ACCELERATED;
14063 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070014064 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014065
Christopher Tate3d4bf172011-03-28 16:16:46 -070014066 final int privateFlags2 = mPrivateFlags2;
14067 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14068 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
14069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014070 drawableState = VIEW_STATE_SETS[viewStateIndex];
14071
14072 //noinspection ConstantIfStatement
14073 if (false) {
14074 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14075 Log.i("View", toString()
14076 + " pressed=" + ((privateFlags & PRESSED) != 0)
14077 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14078 + " fo=" + hasFocus()
14079 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014080 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014081 + ": " + Arrays.toString(drawableState));
14082 }
14083
14084 if (extraSpace == 0) {
14085 return drawableState;
14086 }
14087
14088 final int[] fullState;
14089 if (drawableState != null) {
14090 fullState = new int[drawableState.length + extraSpace];
14091 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14092 } else {
14093 fullState = new int[extraSpace];
14094 }
14095
14096 return fullState;
14097 }
14098
14099 /**
14100 * Merge your own state values in <var>additionalState</var> into the base
14101 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014102 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103 *
14104 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014105 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014106 * own additional state values.
14107 *
14108 * @param additionalState The additional state values you would like
14109 * added to <var>baseState</var>; this array is not modified.
14110 *
14111 * @return As a convenience, the <var>baseState</var> array you originally
14112 * passed into the function is returned.
14113 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014114 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014115 */
14116 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14117 final int N = baseState.length;
14118 int i = N - 1;
14119 while (i >= 0 && baseState[i] == 0) {
14120 i--;
14121 }
14122 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14123 return baseState;
14124 }
14125
14126 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014127 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14128 * on all Drawable objects associated with this view.
14129 */
14130 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014131 if (mBackground != null) {
14132 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014133 }
14134 }
14135
14136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014137 * Sets the background color for this view.
14138 * @param color the color of the background
14139 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014140 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014141 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014142 if (mBackground instanceof ColorDrawable) {
14143 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014144 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014145 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014147 }
14148
14149 /**
14150 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014151 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 * @attr ref android.R.styleable#View_background
14155 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014156 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014157 public void setBackgroundResource(int resid) {
14158 if (resid != 0 && resid == mBackgroundResource) {
14159 return;
14160 }
14161
14162 Drawable d= null;
14163 if (resid != 0) {
14164 d = mResources.getDrawable(resid);
14165 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014166 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014167
14168 mBackgroundResource = resid;
14169 }
14170
14171 /**
14172 * Set the background to a given Drawable, or remove the background. If the
14173 * background has padding, this View's padding is set to the background's
14174 * padding. However, when a background is removed, this View's padding isn't
14175 * touched. If setting the padding is desired, please use
14176 * {@link #setPadding(int, int, int, int)}.
14177 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014178 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179 * background
14180 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014181 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014182 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014183 setBackgroundDrawable(background);
14184 }
14185
14186 /**
14187 * @deprecated use {@link #setBackground(Drawable)} instead
14188 */
14189 @Deprecated
14190 public void setBackgroundDrawable(Drawable background) {
14191 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014192 return;
14193 }
14194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014195 boolean requestLayout = false;
14196
14197 mBackgroundResource = 0;
14198
14199 /*
14200 * Regardless of whether we're setting a new background or not, we want
14201 * to clear the previous drawable.
14202 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014203 if (mBackground != null) {
14204 mBackground.setCallback(null);
14205 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206 }
14207
Philip Milne6c8ea062012-04-03 17:38:43 -070014208 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014209 Rect padding = sThreadLocal.get();
14210 if (padding == null) {
14211 padding = new Rect();
14212 sThreadLocal.set(padding);
14213 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014214 if (background.getPadding(padding)) {
14215 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014216 case LAYOUT_DIRECTION_RTL:
14217 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14218 break;
14219 case LAYOUT_DIRECTION_LTR:
14220 default:
14221 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014223 }
14224
14225 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14226 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014227 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14228 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 requestLayout = true;
14230 }
14231
Philip Milne6c8ea062012-04-03 17:38:43 -070014232 background.setCallback(this);
14233 if (background.isStateful()) {
14234 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014236 background.setVisible(getVisibility() == VISIBLE, false);
14237 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014238
14239 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14240 mPrivateFlags &= ~SKIP_DRAW;
14241 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14242 requestLayout = true;
14243 }
14244 } else {
14245 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014246 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247
14248 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14249 /*
14250 * This view ONLY drew the background before and we're removing
14251 * the background, so now it won't draw anything
14252 * (hence we SKIP_DRAW)
14253 */
14254 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14255 mPrivateFlags |= SKIP_DRAW;
14256 }
14257
14258 /*
14259 * When the background is set, we try to apply its padding to this
14260 * View. When the background is removed, we don't touch this View's
14261 * padding. This is noted in the Javadocs. Hence, we don't need to
14262 * requestLayout(), the invalidate() below is sufficient.
14263 */
14264
14265 // The old background's minimum size could have affected this
14266 // View's layout, so let's requestLayout
14267 requestLayout = true;
14268 }
14269
Romain Guy8f1344f52009-05-15 16:03:59 -070014270 computeOpaqueFlags();
14271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 if (requestLayout) {
14273 requestLayout();
14274 }
14275
14276 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014277 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014278 }
14279
14280 /**
14281 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014283 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014284 *
14285 * @see #setBackground(Drawable)
14286 *
14287 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014288 */
14289 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014290 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014291 }
14292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014293 /**
14294 * Sets the padding. The view may add on the space required to display
14295 * the scrollbars, depending on the style and visibility of the scrollbars.
14296 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14297 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14298 * from the values set in this call.
14299 *
14300 * @attr ref android.R.styleable#View_padding
14301 * @attr ref android.R.styleable#View_paddingBottom
14302 * @attr ref android.R.styleable#View_paddingLeft
14303 * @attr ref android.R.styleable#View_paddingRight
14304 * @attr ref android.R.styleable#View_paddingTop
14305 * @param left the left padding in pixels
14306 * @param top the top padding in pixels
14307 * @param right the right padding in pixels
14308 * @param bottom the bottom padding in pixels
14309 */
14310 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014311 mUserPaddingStart = -1;
14312 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014313 mUserPaddingRelative = false;
14314
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014315 internalSetPadding(left, top, right, bottom);
14316 }
14317
14318 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014319 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 mUserPaddingRight = right;
14321 mUserPaddingBottom = bottom;
14322
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014323 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014324 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014325
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014326 // Common case is there are no scroll bars.
14327 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014328 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014329 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014330 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014331 switch (mVerticalScrollbarPosition) {
14332 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014333 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14334 left += offset;
14335 } else {
14336 right += offset;
14337 }
14338 break;
Adam Powell20232d02010-12-08 21:08:53 -080014339 case SCROLLBAR_POSITION_RIGHT:
14340 right += offset;
14341 break;
14342 case SCROLLBAR_POSITION_LEFT:
14343 left += offset;
14344 break;
14345 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014346 }
Adam Powell20232d02010-12-08 21:08:53 -080014347 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014348 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14349 ? 0 : getHorizontalScrollbarHeight();
14350 }
14351 }
Romain Guy8506ab42009-06-11 17:35:47 -070014352
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014353 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014354 changed = true;
14355 mPaddingLeft = left;
14356 }
14357 if (mPaddingTop != top) {
14358 changed = true;
14359 mPaddingTop = top;
14360 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014361 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014363 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014364 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014365 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014366 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014367 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014368 }
14369
14370 if (changed) {
14371 requestLayout();
14372 }
14373 }
14374
14375 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014376 * Sets the relative padding. The view may add on the space required to display
14377 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014378 * from the values set in this call.
14379 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014380 * @param start the start padding in pixels
14381 * @param top the top padding in pixels
14382 * @param end the end padding in pixels
14383 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014384 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014385 */
14386 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014387 mUserPaddingStart = start;
14388 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014389 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014390
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014391 switch(getResolvedLayoutDirection()) {
14392 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014393 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014394 break;
14395 case LAYOUT_DIRECTION_LTR:
14396 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014397 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014398 }
14399 }
14400
14401 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014402 * Returns the top padding of this view.
14403 *
14404 * @return the top padding in pixels
14405 */
14406 public int getPaddingTop() {
14407 return mPaddingTop;
14408 }
14409
14410 /**
14411 * Returns the bottom padding of this view. If there are inset and enabled
14412 * scrollbars, this value may include the space required to display the
14413 * scrollbars as well.
14414 *
14415 * @return the bottom padding in pixels
14416 */
14417 public int getPaddingBottom() {
14418 return mPaddingBottom;
14419 }
14420
14421 /**
14422 * Returns the left padding of this view. If there are inset and enabled
14423 * scrollbars, this value may include the space required to display the
14424 * scrollbars as well.
14425 *
14426 * @return the left padding in pixels
14427 */
14428 public int getPaddingLeft() {
14429 return mPaddingLeft;
14430 }
14431
14432 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014433 * Returns the start padding of this view depending on its resolved layout direction.
14434 * If there are inset and enabled scrollbars, this value may include the space
14435 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014436 *
14437 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014438 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014439 */
14440 public int getPaddingStart() {
14441 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14442 mPaddingRight : mPaddingLeft;
14443 }
14444
14445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014446 * Returns the right padding of this view. If there are inset and enabled
14447 * scrollbars, this value may include the space required to display the
14448 * scrollbars as well.
14449 *
14450 * @return the right padding in pixels
14451 */
14452 public int getPaddingRight() {
14453 return mPaddingRight;
14454 }
14455
14456 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014457 * Returns the end padding of this view depending on its resolved layout direction.
14458 * If there are inset and enabled scrollbars, this value may include the space
14459 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014460 *
14461 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014462 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014463 */
14464 public int getPaddingEnd() {
14465 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14466 mPaddingLeft : mPaddingRight;
14467 }
14468
14469 /**
14470 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014471 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014472 *
14473 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014474 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014475 */
14476 public boolean isPaddingRelative() {
14477 return mUserPaddingRelative;
14478 }
14479
14480 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014481 * @hide
14482 */
Philip Milne7a23b492012-04-24 22:12:36 -070014483 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014484 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014485 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014486 }
14487 return mLayoutInsets;
14488 }
14489
14490 /**
14491 * @hide
14492 */
14493 public void setLayoutInsets(Insets layoutInsets) {
14494 mLayoutInsets = layoutInsets;
14495 }
14496
14497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014498 * Changes the selection state of this view. A view can be selected or not.
14499 * Note that selection is not the same as focus. Views are typically
14500 * selected in the context of an AdapterView like ListView or GridView;
14501 * the selected view is the view that is highlighted.
14502 *
14503 * @param selected true if the view must be selected, false otherwise
14504 */
14505 public void setSelected(boolean selected) {
14506 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14507 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014508 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014509 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014510 refreshDrawableState();
14511 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014512 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14513 notifyAccessibilityStateChanged();
14514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014515 }
14516 }
14517
14518 /**
14519 * Dispatch setSelected to all of this View's children.
14520 *
14521 * @see #setSelected(boolean)
14522 *
14523 * @param selected The new selected state
14524 */
14525 protected void dispatchSetSelected(boolean selected) {
14526 }
14527
14528 /**
14529 * Indicates the selection state of this view.
14530 *
14531 * @return true if the view is selected, false otherwise
14532 */
14533 @ViewDebug.ExportedProperty
14534 public boolean isSelected() {
14535 return (mPrivateFlags & SELECTED) != 0;
14536 }
14537
14538 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014539 * Changes the activated state of this view. A view can be activated or not.
14540 * Note that activation is not the same as selection. Selection is
14541 * a transient property, representing the view (hierarchy) the user is
14542 * currently interacting with. Activation is a longer-term state that the
14543 * user can move views in and out of. For example, in a list view with
14544 * single or multiple selection enabled, the views in the current selection
14545 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14546 * here.) The activated state is propagated down to children of the view it
14547 * is set on.
14548 *
14549 * @param activated true if the view must be activated, false otherwise
14550 */
14551 public void setActivated(boolean activated) {
14552 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14553 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014554 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014555 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014556 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014557 }
14558 }
14559
14560 /**
14561 * Dispatch setActivated to all of this View's children.
14562 *
14563 * @see #setActivated(boolean)
14564 *
14565 * @param activated The new activated state
14566 */
14567 protected void dispatchSetActivated(boolean activated) {
14568 }
14569
14570 /**
14571 * Indicates the activation state of this view.
14572 *
14573 * @return true if the view is activated, false otherwise
14574 */
14575 @ViewDebug.ExportedProperty
14576 public boolean isActivated() {
14577 return (mPrivateFlags & ACTIVATED) != 0;
14578 }
14579
14580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014581 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14582 * observer can be used to get notifications when global events, like
14583 * layout, happen.
14584 *
14585 * The returned ViewTreeObserver observer is not guaranteed to remain
14586 * valid for the lifetime of this View. If the caller of this method keeps
14587 * a long-lived reference to ViewTreeObserver, it should always check for
14588 * the return value of {@link ViewTreeObserver#isAlive()}.
14589 *
14590 * @return The ViewTreeObserver for this view's hierarchy.
14591 */
14592 public ViewTreeObserver getViewTreeObserver() {
14593 if (mAttachInfo != null) {
14594 return mAttachInfo.mTreeObserver;
14595 }
14596 if (mFloatingTreeObserver == null) {
14597 mFloatingTreeObserver = new ViewTreeObserver();
14598 }
14599 return mFloatingTreeObserver;
14600 }
14601
14602 /**
14603 * <p>Finds the topmost view in the current view hierarchy.</p>
14604 *
14605 * @return the topmost view containing this view
14606 */
14607 public View getRootView() {
14608 if (mAttachInfo != null) {
14609 final View v = mAttachInfo.mRootView;
14610 if (v != null) {
14611 return v;
14612 }
14613 }
Romain Guy8506ab42009-06-11 17:35:47 -070014614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014615 View parent = this;
14616
14617 while (parent.mParent != null && parent.mParent instanceof View) {
14618 parent = (View) parent.mParent;
14619 }
14620
14621 return parent;
14622 }
14623
14624 /**
14625 * <p>Computes the coordinates of this view on the screen. The argument
14626 * must be an array of two integers. After the method returns, the array
14627 * contains the x and y location in that order.</p>
14628 *
14629 * @param location an array of two integers in which to hold the coordinates
14630 */
14631 public void getLocationOnScreen(int[] location) {
14632 getLocationInWindow(location);
14633
14634 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014635 if (info != null) {
14636 location[0] += info.mWindowLeft;
14637 location[1] += info.mWindowTop;
14638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014639 }
14640
14641 /**
14642 * <p>Computes the coordinates of this view in its window. The argument
14643 * must be an array of two integers. After the method returns, the array
14644 * contains the x and y location in that order.</p>
14645 *
14646 * @param location an array of two integers in which to hold the coordinates
14647 */
14648 public void getLocationInWindow(int[] location) {
14649 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014650 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014651 }
14652
Gilles Debunne6583ce52011-12-06 18:09:02 -080014653 if (mAttachInfo == null) {
14654 // When the view is not attached to a window, this method does not make sense
14655 location[0] = location[1] = 0;
14656 return;
14657 }
14658
Gilles Debunnecea45132011-11-24 02:19:27 +010014659 float[] position = mAttachInfo.mTmpTransformLocation;
14660 position[0] = position[1] = 0.0f;
14661
14662 if (!hasIdentityMatrix()) {
14663 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014665
Gilles Debunnecea45132011-11-24 02:19:27 +010014666 position[0] += mLeft;
14667 position[1] += mTop;
14668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014669 ViewParent viewParent = mParent;
14670 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014671 final View view = (View) viewParent;
14672
14673 position[0] -= view.mScrollX;
14674 position[1] -= view.mScrollY;
14675
14676 if (!view.hasIdentityMatrix()) {
14677 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014678 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014679
14680 position[0] += view.mLeft;
14681 position[1] += view.mTop;
14682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014683 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014684 }
Romain Guy8506ab42009-06-11 17:35:47 -070014685
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014686 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014687 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014688 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14689 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014690 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014691
14692 location[0] = (int) (position[0] + 0.5f);
14693 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014694 }
14695
14696 /**
14697 * {@hide}
14698 * @param id the id of the view to be found
14699 * @return the view of the specified id, null if cannot be found
14700 */
14701 protected View findViewTraversal(int id) {
14702 if (id == mID) {
14703 return this;
14704 }
14705 return null;
14706 }
14707
14708 /**
14709 * {@hide}
14710 * @param tag the tag of the view to be found
14711 * @return the view of specified tag, null if cannot be found
14712 */
14713 protected View findViewWithTagTraversal(Object tag) {
14714 if (tag != null && tag.equals(mTag)) {
14715 return this;
14716 }
14717 return null;
14718 }
14719
14720 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014721 * {@hide}
14722 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014723 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014724 * @return The first view that matches the predicate or null.
14725 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014726 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014727 if (predicate.apply(this)) {
14728 return this;
14729 }
14730 return null;
14731 }
14732
14733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014734 * Look for a child view with the given id. If this view has the given
14735 * id, return this view.
14736 *
14737 * @param id The id to search for.
14738 * @return The view that has the given id in the hierarchy or null
14739 */
14740 public final View findViewById(int id) {
14741 if (id < 0) {
14742 return null;
14743 }
14744 return findViewTraversal(id);
14745 }
14746
14747 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014748 * Finds a view by its unuque and stable accessibility id.
14749 *
14750 * @param accessibilityId The searched accessibility id.
14751 * @return The found view.
14752 */
14753 final View findViewByAccessibilityId(int accessibilityId) {
14754 if (accessibilityId < 0) {
14755 return null;
14756 }
14757 return findViewByAccessibilityIdTraversal(accessibilityId);
14758 }
14759
14760 /**
14761 * Performs the traversal to find a view by its unuque and stable accessibility id.
14762 *
14763 * <strong>Note:</strong>This method does not stop at the root namespace
14764 * boundary since the user can touch the screen at an arbitrary location
14765 * potentially crossing the root namespace bounday which will send an
14766 * accessibility event to accessibility services and they should be able
14767 * to obtain the event source. Also accessibility ids are guaranteed to be
14768 * unique in the window.
14769 *
14770 * @param accessibilityId The accessibility id.
14771 * @return The found view.
14772 */
14773 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14774 if (getAccessibilityViewId() == accessibilityId) {
14775 return this;
14776 }
14777 return null;
14778 }
14779
14780 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014781 * Look for a child view with the given tag. If this view has the given
14782 * tag, return this view.
14783 *
14784 * @param tag The tag to search for, using "tag.equals(getTag())".
14785 * @return The View that has the given tag in the hierarchy or null
14786 */
14787 public final View findViewWithTag(Object tag) {
14788 if (tag == null) {
14789 return null;
14790 }
14791 return findViewWithTagTraversal(tag);
14792 }
14793
14794 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014795 * {@hide}
14796 * Look for a child view that matches the specified predicate.
14797 * If this view matches the predicate, return this view.
14798 *
14799 * @param predicate The predicate to evaluate.
14800 * @return The first view that matches the predicate or null.
14801 */
14802 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014803 return findViewByPredicateTraversal(predicate, null);
14804 }
14805
14806 /**
14807 * {@hide}
14808 * Look for a child view that matches the specified predicate,
14809 * starting with the specified view and its descendents and then
14810 * recusively searching the ancestors and siblings of that view
14811 * until this view is reached.
14812 *
14813 * This method is useful in cases where the predicate does not match
14814 * a single unique view (perhaps multiple views use the same id)
14815 * and we are trying to find the view that is "closest" in scope to the
14816 * starting view.
14817 *
14818 * @param start The view to start from.
14819 * @param predicate The predicate to evaluate.
14820 * @return The first view that matches the predicate or null.
14821 */
14822 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14823 View childToSkip = null;
14824 for (;;) {
14825 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14826 if (view != null || start == this) {
14827 return view;
14828 }
14829
14830 ViewParent parent = start.getParent();
14831 if (parent == null || !(parent instanceof View)) {
14832 return null;
14833 }
14834
14835 childToSkip = start;
14836 start = (View) parent;
14837 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014838 }
14839
14840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014841 * Sets the identifier for this view. The identifier does not have to be
14842 * unique in this view's hierarchy. The identifier should be a positive
14843 * number.
14844 *
14845 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014846 * @see #getId()
14847 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 *
14849 * @param id a number used to identify the view
14850 *
14851 * @attr ref android.R.styleable#View_id
14852 */
14853 public void setId(int id) {
14854 mID = id;
14855 }
14856
14857 /**
14858 * {@hide}
14859 *
14860 * @param isRoot true if the view belongs to the root namespace, false
14861 * otherwise
14862 */
14863 public void setIsRootNamespace(boolean isRoot) {
14864 if (isRoot) {
14865 mPrivateFlags |= IS_ROOT_NAMESPACE;
14866 } else {
14867 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14868 }
14869 }
14870
14871 /**
14872 * {@hide}
14873 *
14874 * @return true if the view belongs to the root namespace, false otherwise
14875 */
14876 public boolean isRootNamespace() {
14877 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14878 }
14879
14880 /**
14881 * Returns this view's identifier.
14882 *
14883 * @return a positive integer used to identify the view or {@link #NO_ID}
14884 * if the view has no ID
14885 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014886 * @see #setId(int)
14887 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 * @attr ref android.R.styleable#View_id
14889 */
14890 @ViewDebug.CapturedViewProperty
14891 public int getId() {
14892 return mID;
14893 }
14894
14895 /**
14896 * Returns this view's tag.
14897 *
14898 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014899 *
14900 * @see #setTag(Object)
14901 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014902 */
14903 @ViewDebug.ExportedProperty
14904 public Object getTag() {
14905 return mTag;
14906 }
14907
14908 /**
14909 * Sets the tag associated with this view. A tag can be used to mark
14910 * a view in its hierarchy and does not have to be unique within the
14911 * hierarchy. Tags can also be used to store data within a view without
14912 * resorting to another data structure.
14913 *
14914 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014915 *
14916 * @see #getTag()
14917 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014918 */
14919 public void setTag(final Object tag) {
14920 mTag = tag;
14921 }
14922
14923 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014924 * Returns the tag associated with this view and the specified key.
14925 *
14926 * @param key The key identifying the tag
14927 *
14928 * @return the Object stored in this view as a tag
14929 *
14930 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014931 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014932 */
14933 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014934 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014935 return null;
14936 }
14937
14938 /**
14939 * Sets a tag associated with this view and a key. A tag can be used
14940 * to mark a view in its hierarchy and does not have to be unique within
14941 * the hierarchy. Tags can also be used to store data within a view
14942 * without resorting to another data structure.
14943 *
14944 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014945 * application to ensure it is unique (see the <a
14946 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14947 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014948 * the Android framework or not associated with any package will cause
14949 * an {@link IllegalArgumentException} to be thrown.
14950 *
14951 * @param key The key identifying the tag
14952 * @param tag An Object to tag the view with
14953 *
14954 * @throws IllegalArgumentException If they specified key is not valid
14955 *
14956 * @see #setTag(Object)
14957 * @see #getTag(int)
14958 */
14959 public void setTag(int key, final Object tag) {
14960 // If the package id is 0x00 or 0x01, it's either an undefined package
14961 // or a framework id
14962 if ((key >>> 24) < 2) {
14963 throw new IllegalArgumentException("The key must be an application-specific "
14964 + "resource id.");
14965 }
14966
Adam Powell2b2f6d62011-09-23 11:15:39 -070014967 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014968 }
14969
14970 /**
14971 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14972 * framework id.
14973 *
14974 * @hide
14975 */
14976 public void setTagInternal(int key, Object tag) {
14977 if ((key >>> 24) != 0x1) {
14978 throw new IllegalArgumentException("The key must be a framework-specific "
14979 + "resource id.");
14980 }
14981
Adam Powell2b2f6d62011-09-23 11:15:39 -070014982 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014983 }
14984
Adam Powell2b2f6d62011-09-23 11:15:39 -070014985 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014986 if (mKeyedTags == null) {
14987 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014988 }
14989
Adam Powell7db82ac2011-09-22 19:44:04 -070014990 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014991 }
14992
14993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014994 * Prints information about this view in the log output, with the tag
14995 * {@link #VIEW_LOG_TAG}.
14996 *
14997 * @hide
14998 */
14999 public void debug() {
15000 debug(0);
15001 }
15002
15003 /**
15004 * Prints information about this view in the log output, with the tag
15005 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15006 * indentation defined by the <code>depth</code>.
15007 *
15008 * @param depth the indentation level
15009 *
15010 * @hide
15011 */
15012 protected void debug(int depth) {
15013 String output = debugIndent(depth - 1);
15014
15015 output += "+ " + this;
15016 int id = getId();
15017 if (id != -1) {
15018 output += " (id=" + id + ")";
15019 }
15020 Object tag = getTag();
15021 if (tag != null) {
15022 output += " (tag=" + tag + ")";
15023 }
15024 Log.d(VIEW_LOG_TAG, output);
15025
15026 if ((mPrivateFlags & FOCUSED) != 0) {
15027 output = debugIndent(depth) + " FOCUSED";
15028 Log.d(VIEW_LOG_TAG, output);
15029 }
15030
15031 output = debugIndent(depth);
15032 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15033 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15034 + "} ";
15035 Log.d(VIEW_LOG_TAG, output);
15036
15037 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15038 || mPaddingBottom != 0) {
15039 output = debugIndent(depth);
15040 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15041 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15042 Log.d(VIEW_LOG_TAG, output);
15043 }
15044
15045 output = debugIndent(depth);
15046 output += "mMeasureWidth=" + mMeasuredWidth +
15047 " mMeasureHeight=" + mMeasuredHeight;
15048 Log.d(VIEW_LOG_TAG, output);
15049
15050 output = debugIndent(depth);
15051 if (mLayoutParams == null) {
15052 output += "BAD! no layout params";
15053 } else {
15054 output = mLayoutParams.debug(output);
15055 }
15056 Log.d(VIEW_LOG_TAG, output);
15057
15058 output = debugIndent(depth);
15059 output += "flags={";
15060 output += View.printFlags(mViewFlags);
15061 output += "}";
15062 Log.d(VIEW_LOG_TAG, output);
15063
15064 output = debugIndent(depth);
15065 output += "privateFlags={";
15066 output += View.printPrivateFlags(mPrivateFlags);
15067 output += "}";
15068 Log.d(VIEW_LOG_TAG, output);
15069 }
15070
15071 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015072 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015073 *
15074 * @param depth the indentation level
15075 * @return a String containing (depth * 2 + 3) * 2 white spaces
15076 *
15077 * @hide
15078 */
15079 protected static String debugIndent(int depth) {
15080 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15081 for (int i = 0; i < (depth * 2) + 3; i++) {
15082 spaces.append(' ').append(' ');
15083 }
15084 return spaces.toString();
15085 }
15086
15087 /**
15088 * <p>Return the offset of the widget's text baseline from the widget's top
15089 * boundary. If this widget does not support baseline alignment, this
15090 * method returns -1. </p>
15091 *
15092 * @return the offset of the baseline within the widget's bounds or -1
15093 * if baseline alignment is not supported
15094 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015095 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015096 public int getBaseline() {
15097 return -1;
15098 }
15099
15100 /**
15101 * Call this when something has changed which has invalidated the
15102 * layout of this view. This will schedule a layout pass of the view
15103 * tree.
15104 */
15105 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015106 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015107 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015108
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015109 if (mLayoutParams != null) {
15110 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15111 }
15112
15113 if (mParent != null && !mParent.isLayoutRequested()) {
15114 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015115 }
15116 }
15117
15118 /**
15119 * Forces this view to be laid out during the next layout pass.
15120 * This method does not call requestLayout() or forceLayout()
15121 * on the parent.
15122 */
15123 public void forceLayout() {
15124 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015125 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015126 }
15127
15128 /**
15129 * <p>
15130 * This is called to find out how big a view should be. The parent
15131 * supplies constraint information in the width and height parameters.
15132 * </p>
15133 *
15134 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015135 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015136 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015137 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 * </p>
15139 *
15140 *
15141 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15142 * parent
15143 * @param heightMeasureSpec Vertical space requirements as imposed by the
15144 * parent
15145 *
15146 * @see #onMeasure(int, int)
15147 */
15148 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15149 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15150 widthMeasureSpec != mOldWidthMeasureSpec ||
15151 heightMeasureSpec != mOldHeightMeasureSpec) {
15152
15153 // first clears the measured dimension flag
15154 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015156 // measure ourselves, this should set the measured dimension flag back
15157 onMeasure(widthMeasureSpec, heightMeasureSpec);
15158
15159 // flag not set, setMeasuredDimension() was not invoked, we raise
15160 // an exception to warn the developer
15161 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15162 throw new IllegalStateException("onMeasure() did not set the"
15163 + " measured dimension by calling"
15164 + " setMeasuredDimension()");
15165 }
15166
15167 mPrivateFlags |= LAYOUT_REQUIRED;
15168 }
15169
15170 mOldWidthMeasureSpec = widthMeasureSpec;
15171 mOldHeightMeasureSpec = heightMeasureSpec;
15172 }
15173
15174 /**
15175 * <p>
15176 * Measure the view and its content to determine the measured width and the
15177 * measured height. This method is invoked by {@link #measure(int, int)} and
15178 * should be overriden by subclasses to provide accurate and efficient
15179 * measurement of their contents.
15180 * </p>
15181 *
15182 * <p>
15183 * <strong>CONTRACT:</strong> When overriding this method, you
15184 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15185 * measured width and height of this view. Failure to do so will trigger an
15186 * <code>IllegalStateException</code>, thrown by
15187 * {@link #measure(int, int)}. Calling the superclass'
15188 * {@link #onMeasure(int, int)} is a valid use.
15189 * </p>
15190 *
15191 * <p>
15192 * The base class implementation of measure defaults to the background size,
15193 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15194 * override {@link #onMeasure(int, int)} to provide better measurements of
15195 * their content.
15196 * </p>
15197 *
15198 * <p>
15199 * If this method is overridden, it is the subclass's responsibility to make
15200 * sure the measured height and width are at least the view's minimum height
15201 * and width ({@link #getSuggestedMinimumHeight()} and
15202 * {@link #getSuggestedMinimumWidth()}).
15203 * </p>
15204 *
15205 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15206 * The requirements are encoded with
15207 * {@link android.view.View.MeasureSpec}.
15208 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15209 * The requirements are encoded with
15210 * {@link android.view.View.MeasureSpec}.
15211 *
15212 * @see #getMeasuredWidth()
15213 * @see #getMeasuredHeight()
15214 * @see #setMeasuredDimension(int, int)
15215 * @see #getSuggestedMinimumHeight()
15216 * @see #getSuggestedMinimumWidth()
15217 * @see android.view.View.MeasureSpec#getMode(int)
15218 * @see android.view.View.MeasureSpec#getSize(int)
15219 */
15220 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15221 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15222 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15223 }
15224
15225 /**
15226 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15227 * measured width and measured height. Failing to do so will trigger an
15228 * exception at measurement time.</p>
15229 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015230 * @param measuredWidth The measured width of this view. May be a complex
15231 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15232 * {@link #MEASURED_STATE_TOO_SMALL}.
15233 * @param measuredHeight The measured height of this view. May be a complex
15234 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15235 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 */
15237 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15238 mMeasuredWidth = measuredWidth;
15239 mMeasuredHeight = measuredHeight;
15240
15241 mPrivateFlags |= MEASURED_DIMENSION_SET;
15242 }
15243
15244 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015245 * Merge two states as returned by {@link #getMeasuredState()}.
15246 * @param curState The current state as returned from a view or the result
15247 * of combining multiple views.
15248 * @param newState The new view state to combine.
15249 * @return Returns a new integer reflecting the combination of the two
15250 * states.
15251 */
15252 public static int combineMeasuredStates(int curState, int newState) {
15253 return curState | newState;
15254 }
15255
15256 /**
15257 * Version of {@link #resolveSizeAndState(int, int, int)}
15258 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15259 */
15260 public static int resolveSize(int size, int measureSpec) {
15261 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15262 }
15263
15264 /**
15265 * Utility to reconcile a desired size and state, with constraints imposed
15266 * by a MeasureSpec. Will take the desired size, unless a different size
15267 * is imposed by the constraints. The returned value is a compound integer,
15268 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15269 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15270 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015271 *
15272 * @param size How big the view wants to be
15273 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015274 * @return Size information bit mask as defined by
15275 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015276 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015277 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015278 int result = size;
15279 int specMode = MeasureSpec.getMode(measureSpec);
15280 int specSize = MeasureSpec.getSize(measureSpec);
15281 switch (specMode) {
15282 case MeasureSpec.UNSPECIFIED:
15283 result = size;
15284 break;
15285 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015286 if (specSize < size) {
15287 result = specSize | MEASURED_STATE_TOO_SMALL;
15288 } else {
15289 result = size;
15290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015291 break;
15292 case MeasureSpec.EXACTLY:
15293 result = specSize;
15294 break;
15295 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015296 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015297 }
15298
15299 /**
15300 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015301 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015302 * by the MeasureSpec.
15303 *
15304 * @param size Default size for this view
15305 * @param measureSpec Constraints imposed by the parent
15306 * @return The size this view should be.
15307 */
15308 public static int getDefaultSize(int size, int measureSpec) {
15309 int result = size;
15310 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015311 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015312
15313 switch (specMode) {
15314 case MeasureSpec.UNSPECIFIED:
15315 result = size;
15316 break;
15317 case MeasureSpec.AT_MOST:
15318 case MeasureSpec.EXACTLY:
15319 result = specSize;
15320 break;
15321 }
15322 return result;
15323 }
15324
15325 /**
15326 * Returns the suggested minimum height that the view should use. This
15327 * returns the maximum of the view's minimum height
15328 * and the background's minimum height
15329 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15330 * <p>
15331 * When being used in {@link #onMeasure(int, int)}, the caller should still
15332 * ensure the returned height is within the requirements of the parent.
15333 *
15334 * @return The suggested minimum height of the view.
15335 */
15336 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015337 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015339 }
15340
15341 /**
15342 * Returns the suggested minimum width that the view should use. This
15343 * returns the maximum of the view's minimum width)
15344 * and the background's minimum width
15345 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15346 * <p>
15347 * When being used in {@link #onMeasure(int, int)}, the caller should still
15348 * ensure the returned width is within the requirements of the parent.
15349 *
15350 * @return The suggested minimum width of the view.
15351 */
15352 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015353 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015355
Philip Milne6c8ea062012-04-03 17:38:43 -070015356 /**
15357 * Returns the minimum height of the view.
15358 *
15359 * @return the minimum height the view will try to be.
15360 *
15361 * @see #setMinimumHeight(int)
15362 *
15363 * @attr ref android.R.styleable#View_minHeight
15364 */
15365 public int getMinimumHeight() {
15366 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015367 }
15368
15369 /**
15370 * Sets the minimum height of the view. It is not guaranteed the view will
15371 * be able to achieve this minimum height (for example, if its parent layout
15372 * constrains it with less available height).
15373 *
15374 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015375 *
15376 * @see #getMinimumHeight()
15377 *
15378 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015379 */
15380 public void setMinimumHeight(int minHeight) {
15381 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015382 requestLayout();
15383 }
15384
15385 /**
15386 * Returns the minimum width of the view.
15387 *
15388 * @return the minimum width the view will try to be.
15389 *
15390 * @see #setMinimumWidth(int)
15391 *
15392 * @attr ref android.R.styleable#View_minWidth
15393 */
15394 public int getMinimumWidth() {
15395 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015396 }
15397
15398 /**
15399 * Sets the minimum width of the view. It is not guaranteed the view will
15400 * be able to achieve this minimum width (for example, if its parent layout
15401 * constrains it with less available width).
15402 *
15403 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015404 *
15405 * @see #getMinimumWidth()
15406 *
15407 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015408 */
15409 public void setMinimumWidth(int minWidth) {
15410 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015411 requestLayout();
15412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015413 }
15414
15415 /**
15416 * Get the animation currently associated with this view.
15417 *
15418 * @return The animation that is currently playing or
15419 * scheduled to play for this view.
15420 */
15421 public Animation getAnimation() {
15422 return mCurrentAnimation;
15423 }
15424
15425 /**
15426 * Start the specified animation now.
15427 *
15428 * @param animation the animation to start now
15429 */
15430 public void startAnimation(Animation animation) {
15431 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15432 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015433 invalidateParentCaches();
15434 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015435 }
15436
15437 /**
15438 * Cancels any animations for this view.
15439 */
15440 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015441 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015442 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015444 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015445 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015446 }
15447
15448 /**
15449 * Sets the next animation to play for this view.
15450 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015451 * {@link #startAnimation(android.view.animation.Animation)} instead.
15452 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015453 * control over the start time and invalidation, but you
15454 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015455 * 2) the view's parent (which controls animations on its children)
15456 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015457 * start.
15458 *
15459 * @param animation The next animation, or null.
15460 */
15461 public void setAnimation(Animation animation) {
15462 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015464 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015465 // If the screen is off assume the animation start time is now instead of
15466 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15467 // would cause the animation to start when the screen turns back on
15468 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15469 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15470 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015472 animation.reset();
15473 }
15474 }
15475
15476 /**
15477 * Invoked by a parent ViewGroup to notify the start of the animation
15478 * currently associated with this view. If you override this method,
15479 * always call super.onAnimationStart();
15480 *
15481 * @see #setAnimation(android.view.animation.Animation)
15482 * @see #getAnimation()
15483 */
15484 protected void onAnimationStart() {
15485 mPrivateFlags |= ANIMATION_STARTED;
15486 }
15487
15488 /**
15489 * Invoked by a parent ViewGroup to notify the end of the animation
15490 * currently associated with this view. If you override this method,
15491 * always call super.onAnimationEnd();
15492 *
15493 * @see #setAnimation(android.view.animation.Animation)
15494 * @see #getAnimation()
15495 */
15496 protected void onAnimationEnd() {
15497 mPrivateFlags &= ~ANIMATION_STARTED;
15498 }
15499
15500 /**
15501 * Invoked if there is a Transform that involves alpha. Subclass that can
15502 * draw themselves with the specified alpha should return true, and then
15503 * respect that alpha when their onDraw() is called. If this returns false
15504 * then the view may be redirected to draw into an offscreen buffer to
15505 * fulfill the request, which will look fine, but may be slower than if the
15506 * subclass handles it internally. The default implementation returns false.
15507 *
15508 * @param alpha The alpha (0..255) to apply to the view's drawing
15509 * @return true if the view can draw with the specified alpha.
15510 */
15511 protected boolean onSetAlpha(int alpha) {
15512 return false;
15513 }
15514
15515 /**
15516 * This is used by the RootView to perform an optimization when
15517 * the view hierarchy contains one or several SurfaceView.
15518 * SurfaceView is always considered transparent, but its children are not,
15519 * therefore all View objects remove themselves from the global transparent
15520 * region (passed as a parameter to this function).
15521 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015522 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015523 *
15524 * @return Returns true if the effective visibility of the view at this
15525 * point is opaque, regardless of the transparent region; returns false
15526 * if it is possible for underlying windows to be seen behind the view.
15527 *
15528 * {@hide}
15529 */
15530 public boolean gatherTransparentRegion(Region region) {
15531 final AttachInfo attachInfo = mAttachInfo;
15532 if (region != null && attachInfo != null) {
15533 final int pflags = mPrivateFlags;
15534 if ((pflags & SKIP_DRAW) == 0) {
15535 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15536 // remove it from the transparent region.
15537 final int[] location = attachInfo.mTransparentLocation;
15538 getLocationInWindow(location);
15539 region.op(location[0], location[1], location[0] + mRight - mLeft,
15540 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015541 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015542 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15543 // exists, so we remove the background drawable's non-transparent
15544 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015545 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015546 }
15547 }
15548 return true;
15549 }
15550
15551 /**
15552 * Play a sound effect for this view.
15553 *
15554 * <p>The framework will play sound effects for some built in actions, such as
15555 * clicking, but you may wish to play these effects in your widget,
15556 * for instance, for internal navigation.
15557 *
15558 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15559 * {@link #isSoundEffectsEnabled()} is true.
15560 *
15561 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15562 */
15563 public void playSoundEffect(int soundConstant) {
15564 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15565 return;
15566 }
15567 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15568 }
15569
15570 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015571 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015572 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015573 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015574 *
15575 * <p>The framework will provide haptic feedback for some built in actions,
15576 * such as long presses, but you may wish to provide feedback for your
15577 * own widget.
15578 *
15579 * <p>The feedback will only be performed if
15580 * {@link #isHapticFeedbackEnabled()} is true.
15581 *
15582 * @param feedbackConstant One of the constants defined in
15583 * {@link HapticFeedbackConstants}
15584 */
15585 public boolean performHapticFeedback(int feedbackConstant) {
15586 return performHapticFeedback(feedbackConstant, 0);
15587 }
15588
15589 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015590 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015591 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015592 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015593 *
15594 * @param feedbackConstant One of the constants defined in
15595 * {@link HapticFeedbackConstants}
15596 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15597 */
15598 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15599 if (mAttachInfo == null) {
15600 return false;
15601 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015602 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015603 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015604 && !isHapticFeedbackEnabled()) {
15605 return false;
15606 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015607 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15608 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015609 }
15610
15611 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015612 * Request that the visibility of the status bar or other screen/window
15613 * decorations be changed.
15614 *
15615 * <p>This method is used to put the over device UI into temporary modes
15616 * where the user's attention is focused more on the application content,
15617 * by dimming or hiding surrounding system affordances. This is typically
15618 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15619 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15620 * to be placed behind the action bar (and with these flags other system
15621 * affordances) so that smooth transitions between hiding and showing them
15622 * can be done.
15623 *
15624 * <p>Two representative examples of the use of system UI visibility is
15625 * implementing a content browsing application (like a magazine reader)
15626 * and a video playing application.
15627 *
15628 * <p>The first code shows a typical implementation of a View in a content
15629 * browsing application. In this implementation, the application goes
15630 * into a content-oriented mode by hiding the status bar and action bar,
15631 * and putting the navigation elements into lights out mode. The user can
15632 * then interact with content while in this mode. Such an application should
15633 * provide an easy way for the user to toggle out of the mode (such as to
15634 * check information in the status bar or access notifications). In the
15635 * implementation here, this is done simply by tapping on the content.
15636 *
15637 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15638 * content}
15639 *
15640 * <p>This second code sample shows a typical implementation of a View
15641 * in a video playing application. In this situation, while the video is
15642 * playing the application would like to go into a complete full-screen mode,
15643 * to use as much of the display as possible for the video. When in this state
15644 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015645 * touching on the screen to pop the UI out of full screen mode. See
15646 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015647 *
15648 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15649 * content}
15650 *
15651 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15652 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15653 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15654 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015655 */
15656 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015657 if (visibility != mSystemUiVisibility) {
15658 mSystemUiVisibility = visibility;
15659 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15660 mParent.recomputeViewAttributes(this);
15661 }
Joe Onorato664644d2011-01-23 17:53:23 -080015662 }
15663 }
15664
15665 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015666 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15667 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15668 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15669 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15670 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015671 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015672 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015673 return mSystemUiVisibility;
15674 }
15675
Scott Mainec6331b2011-05-24 16:55:56 -070015676 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015677 * Returns the current system UI visibility that is currently set for
15678 * the entire window. This is the combination of the
15679 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15680 * views in the window.
15681 */
15682 public int getWindowSystemUiVisibility() {
15683 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15684 }
15685
15686 /**
15687 * Override to find out when the window's requested system UI visibility
15688 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15689 * This is different from the callbacks recieved through
15690 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15691 * in that this is only telling you about the local request of the window,
15692 * not the actual values applied by the system.
15693 */
15694 public void onWindowSystemUiVisibilityChanged(int visible) {
15695 }
15696
15697 /**
15698 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15699 * the view hierarchy.
15700 */
15701 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15702 onWindowSystemUiVisibilityChanged(visible);
15703 }
15704
15705 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015706 * Set a listener to receive callbacks when the visibility of the system bar changes.
15707 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15708 */
Joe Onorato664644d2011-01-23 17:53:23 -080015709 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015710 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015711 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15712 mParent.recomputeViewAttributes(this);
15713 }
15714 }
15715
15716 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015717 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15718 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015719 */
15720 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015721 ListenerInfo li = mListenerInfo;
15722 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15723 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015724 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015725 }
15726 }
15727
Dianne Hackborncf675782012-05-10 15:07:24 -070015728 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015729 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15730 if (val != mSystemUiVisibility) {
15731 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015732 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015733 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015734 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015735 }
15736
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015737 /** @hide */
15738 public void setDisabledSystemUiVisibility(int flags) {
15739 if (mAttachInfo != null) {
15740 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15741 mAttachInfo.mDisabledSystemUiVisibility = flags;
15742 if (mParent != null) {
15743 mParent.recomputeViewAttributes(this);
15744 }
15745 }
15746 }
15747 }
15748
Joe Onorato664644d2011-01-23 17:53:23 -080015749 /**
Joe Malin32736f02011-01-19 16:14:20 -080015750 * Creates an image that the system displays during the drag and drop
15751 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15752 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15753 * appearance as the given View. The default also positions the center of the drag shadow
15754 * directly under the touch point. If no View is provided (the constructor with no parameters
15755 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15756 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15757 * default is an invisible drag shadow.
15758 * <p>
15759 * You are not required to use the View you provide to the constructor as the basis of the
15760 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15761 * anything you want as the drag shadow.
15762 * </p>
15763 * <p>
15764 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15765 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15766 * size and position of the drag shadow. It uses this data to construct a
15767 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15768 * so that your application can draw the shadow image in the Canvas.
15769 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015770 *
15771 * <div class="special reference">
15772 * <h3>Developer Guides</h3>
15773 * <p>For a guide to implementing drag and drop features, read the
15774 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15775 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015776 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015777 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015778 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015779
15780 /**
Joe Malin32736f02011-01-19 16:14:20 -080015781 * Constructs a shadow image builder based on a View. By default, the resulting drag
15782 * shadow will have the same appearance and dimensions as the View, with the touch point
15783 * over the center of the View.
15784 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015785 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015786 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015787 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015788 }
15789
Christopher Tate17ed60c2011-01-18 12:50:26 -080015790 /**
15791 * Construct a shadow builder object with no associated View. This
15792 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15793 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15794 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015795 * reference to any View object. If they are not overridden, then the result is an
15796 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015797 */
15798 public DragShadowBuilder() {
15799 mView = new WeakReference<View>(null);
15800 }
15801
15802 /**
15803 * Returns the View object that had been passed to the
15804 * {@link #View.DragShadowBuilder(View)}
15805 * constructor. If that View parameter was {@code null} or if the
15806 * {@link #View.DragShadowBuilder()}
15807 * constructor was used to instantiate the builder object, this method will return
15808 * null.
15809 *
15810 * @return The View object associate with this builder object.
15811 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015812 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015813 final public View getView() {
15814 return mView.get();
15815 }
15816
Christopher Tate2c095f32010-10-04 14:13:40 -070015817 /**
Joe Malin32736f02011-01-19 16:14:20 -080015818 * Provides the metrics for the shadow image. These include the dimensions of
15819 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015820 * be centered under the touch location while dragging.
15821 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015822 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015823 * same as the dimensions of the View itself and centers the shadow under
15824 * the touch point.
15825 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015826 *
Joe Malin32736f02011-01-19 16:14:20 -080015827 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15828 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15829 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15830 * image.
15831 *
15832 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15833 * shadow image that should be underneath the touch point during the drag and drop
15834 * operation. Your application must set {@link android.graphics.Point#x} to the
15835 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015836 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015837 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015838 final View view = mView.get();
15839 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015840 shadowSize.set(view.getWidth(), view.getHeight());
15841 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015842 } else {
15843 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15844 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015845 }
15846
15847 /**
Joe Malin32736f02011-01-19 16:14:20 -080015848 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15849 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015850 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015851 *
Joe Malin32736f02011-01-19 16:14:20 -080015852 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015853 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015854 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015855 final View view = mView.get();
15856 if (view != null) {
15857 view.draw(canvas);
15858 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015859 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015860 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015861 }
15862 }
15863
15864 /**
Joe Malin32736f02011-01-19 16:14:20 -080015865 * Starts a drag and drop operation. When your application calls this method, it passes a
15866 * {@link android.view.View.DragShadowBuilder} object to the system. The
15867 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15868 * to get metrics for the drag shadow, and then calls the object's
15869 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15870 * <p>
15871 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15872 * drag events to all the View objects in your application that are currently visible. It does
15873 * this either by calling the View object's drag listener (an implementation of
15874 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15875 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15876 * Both are passed a {@link android.view.DragEvent} object that has a
15877 * {@link android.view.DragEvent#getAction()} value of
15878 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15879 * </p>
15880 * <p>
15881 * Your application can invoke startDrag() on any attached View object. The View object does not
15882 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15883 * be related to the View the user selected for dragging.
15884 * </p>
15885 * @param data A {@link android.content.ClipData} object pointing to the data to be
15886 * transferred by the drag and drop operation.
15887 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15888 * drag shadow.
15889 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15890 * drop operation. This Object is put into every DragEvent object sent by the system during the
15891 * current drag.
15892 * <p>
15893 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15894 * to the target Views. For example, it can contain flags that differentiate between a
15895 * a copy operation and a move operation.
15896 * </p>
15897 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15898 * so the parameter should be set to 0.
15899 * @return {@code true} if the method completes successfully, or
15900 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15901 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015902 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015903 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015904 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015905 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015906 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015907 }
15908 boolean okay = false;
15909
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015910 Point shadowSize = new Point();
15911 Point shadowTouchPoint = new Point();
15912 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015913
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015914 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15915 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15916 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015917 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015918
Chris Tatea32dcf72010-10-14 12:13:50 -070015919 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015920 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15921 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015922 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015923 Surface surface = new Surface();
15924 try {
15925 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015926 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015927 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015928 + " surface=" + surface);
15929 if (token != null) {
15930 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015931 try {
Chris Tate6b391282010-10-14 15:48:59 -070015932 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015933 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015934 } finally {
15935 surface.unlockCanvasAndPost(canvas);
15936 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015937
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015938 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015939
15940 // Cache the local state object for delivery with DragEvents
15941 root.setLocalDragState(myLocalState);
15942
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015943 // repurpose 'shadowSize' for the last touch point
15944 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015945
Christopher Tatea53146c2010-09-07 11:57:52 -070015946 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015947 shadowSize.x, shadowSize.y,
15948 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015949 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015950
15951 // Off and running! Release our local surface instance; the drag
15952 // shadow surface is now managed by the system process.
15953 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015954 }
15955 } catch (Exception e) {
15956 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15957 surface.destroy();
15958 }
15959
15960 return okay;
15961 }
15962
Christopher Tatea53146c2010-09-07 11:57:52 -070015963 /**
Joe Malin32736f02011-01-19 16:14:20 -080015964 * Handles drag events sent by the system following a call to
15965 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15966 *<p>
15967 * When the system calls this method, it passes a
15968 * {@link android.view.DragEvent} object. A call to
15969 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15970 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15971 * operation.
15972 * @param event The {@link android.view.DragEvent} sent by the system.
15973 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15974 * in DragEvent, indicating the type of drag event represented by this object.
15975 * @return {@code true} if the method was successful, otherwise {@code false}.
15976 * <p>
15977 * The method should return {@code true} in response to an action type of
15978 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15979 * operation.
15980 * </p>
15981 * <p>
15982 * The method should also return {@code true} in response to an action type of
15983 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15984 * {@code false} if it didn't.
15985 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015986 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015987 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015988 return false;
15989 }
15990
15991 /**
Joe Malin32736f02011-01-19 16:14:20 -080015992 * Detects if this View is enabled and has a drag event listener.
15993 * If both are true, then it calls the drag event listener with the
15994 * {@link android.view.DragEvent} it received. If the drag event listener returns
15995 * {@code true}, then dispatchDragEvent() returns {@code true}.
15996 * <p>
15997 * For all other cases, the method calls the
15998 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15999 * method and returns its result.
16000 * </p>
16001 * <p>
16002 * This ensures that a drag event is always consumed, even if the View does not have a drag
16003 * event listener. However, if the View has a listener and the listener returns true, then
16004 * onDragEvent() is not called.
16005 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016006 */
16007 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016008 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016009 ListenerInfo li = mListenerInfo;
16010 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16011 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016012 return true;
16013 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016014 return onDragEvent(event);
16015 }
16016
Christopher Tate3d4bf172011-03-28 16:16:46 -070016017 boolean canAcceptDrag() {
16018 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
16019 }
16020
Christopher Tatea53146c2010-09-07 11:57:52 -070016021 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016022 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16023 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016024 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016025 */
16026 public void onCloseSystemDialogs(String reason) {
16027 }
Joe Malin32736f02011-01-19 16:14:20 -080016028
Dianne Hackbornffa42482009-09-23 22:20:11 -070016029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016030 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016031 * update a Region being computed for
16032 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016033 * that any non-transparent parts of the Drawable are removed from the
16034 * given transparent region.
16035 *
16036 * @param dr The Drawable whose transparency is to be applied to the region.
16037 * @param region A Region holding the current transparency information,
16038 * where any parts of the region that are set are considered to be
16039 * transparent. On return, this region will be modified to have the
16040 * transparency information reduced by the corresponding parts of the
16041 * Drawable that are not transparent.
16042 * {@hide}
16043 */
16044 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16045 if (DBG) {
16046 Log.i("View", "Getting transparent region for: " + this);
16047 }
16048 final Region r = dr.getTransparentRegion();
16049 final Rect db = dr.getBounds();
16050 final AttachInfo attachInfo = mAttachInfo;
16051 if (r != null && attachInfo != null) {
16052 final int w = getRight()-getLeft();
16053 final int h = getBottom()-getTop();
16054 if (db.left > 0) {
16055 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16056 r.op(0, 0, db.left, h, Region.Op.UNION);
16057 }
16058 if (db.right < w) {
16059 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16060 r.op(db.right, 0, w, h, Region.Op.UNION);
16061 }
16062 if (db.top > 0) {
16063 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16064 r.op(0, 0, w, db.top, Region.Op.UNION);
16065 }
16066 if (db.bottom < h) {
16067 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16068 r.op(0, db.bottom, w, h, Region.Op.UNION);
16069 }
16070 final int[] location = attachInfo.mTransparentLocation;
16071 getLocationInWindow(location);
16072 r.translate(location[0], location[1]);
16073 region.op(r, Region.Op.INTERSECT);
16074 } else {
16075 region.op(db, Region.Op.DIFFERENCE);
16076 }
16077 }
16078
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016079 private void checkForLongClick(int delayOffset) {
16080 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16081 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016082
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016083 if (mPendingCheckForLongPress == null) {
16084 mPendingCheckForLongPress = new CheckForLongPress();
16085 }
16086 mPendingCheckForLongPress.rememberWindowAttachCount();
16087 postDelayed(mPendingCheckForLongPress,
16088 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016090 }
16091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016092 /**
16093 * Inflate a view from an XML resource. This convenience method wraps the {@link
16094 * LayoutInflater} class, which provides a full range of options for view inflation.
16095 *
16096 * @param context The Context object for your activity or application.
16097 * @param resource The resource ID to inflate
16098 * @param root A view group that will be the parent. Used to properly inflate the
16099 * layout_* parameters.
16100 * @see LayoutInflater
16101 */
16102 public static View inflate(Context context, int resource, ViewGroup root) {
16103 LayoutInflater factory = LayoutInflater.from(context);
16104 return factory.inflate(resource, root);
16105 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016107 /**
Adam Powell637d3372010-08-25 14:37:03 -070016108 * Scroll the view with standard behavior for scrolling beyond the normal
16109 * content boundaries. Views that call this method should override
16110 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16111 * results of an over-scroll operation.
16112 *
16113 * Views can use this method to handle any touch or fling-based scrolling.
16114 *
16115 * @param deltaX Change in X in pixels
16116 * @param deltaY Change in Y in pixels
16117 * @param scrollX Current X scroll value in pixels before applying deltaX
16118 * @param scrollY Current Y scroll value in pixels before applying deltaY
16119 * @param scrollRangeX Maximum content scroll range along the X axis
16120 * @param scrollRangeY Maximum content scroll range along the Y axis
16121 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16122 * along the X axis.
16123 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16124 * along the Y axis.
16125 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16126 * @return true if scrolling was clamped to an over-scroll boundary along either
16127 * axis, false otherwise.
16128 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016129 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016130 protected boolean overScrollBy(int deltaX, int deltaY,
16131 int scrollX, int scrollY,
16132 int scrollRangeX, int scrollRangeY,
16133 int maxOverScrollX, int maxOverScrollY,
16134 boolean isTouchEvent) {
16135 final int overScrollMode = mOverScrollMode;
16136 final boolean canScrollHorizontal =
16137 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16138 final boolean canScrollVertical =
16139 computeVerticalScrollRange() > computeVerticalScrollExtent();
16140 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16141 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16142 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16143 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16144
16145 int newScrollX = scrollX + deltaX;
16146 if (!overScrollHorizontal) {
16147 maxOverScrollX = 0;
16148 }
16149
16150 int newScrollY = scrollY + deltaY;
16151 if (!overScrollVertical) {
16152 maxOverScrollY = 0;
16153 }
16154
16155 // Clamp values if at the limits and record
16156 final int left = -maxOverScrollX;
16157 final int right = maxOverScrollX + scrollRangeX;
16158 final int top = -maxOverScrollY;
16159 final int bottom = maxOverScrollY + scrollRangeY;
16160
16161 boolean clampedX = false;
16162 if (newScrollX > right) {
16163 newScrollX = right;
16164 clampedX = true;
16165 } else if (newScrollX < left) {
16166 newScrollX = left;
16167 clampedX = true;
16168 }
16169
16170 boolean clampedY = false;
16171 if (newScrollY > bottom) {
16172 newScrollY = bottom;
16173 clampedY = true;
16174 } else if (newScrollY < top) {
16175 newScrollY = top;
16176 clampedY = true;
16177 }
16178
16179 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16180
16181 return clampedX || clampedY;
16182 }
16183
16184 /**
16185 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16186 * respond to the results of an over-scroll operation.
16187 *
16188 * @param scrollX New X scroll value in pixels
16189 * @param scrollY New Y scroll value in pixels
16190 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16191 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16192 */
16193 protected void onOverScrolled(int scrollX, int scrollY,
16194 boolean clampedX, boolean clampedY) {
16195 // Intentionally empty.
16196 }
16197
16198 /**
16199 * Returns the over-scroll mode for this view. The result will be
16200 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16201 * (allow over-scrolling only if the view content is larger than the container),
16202 * or {@link #OVER_SCROLL_NEVER}.
16203 *
16204 * @return This view's over-scroll mode.
16205 */
16206 public int getOverScrollMode() {
16207 return mOverScrollMode;
16208 }
16209
16210 /**
16211 * Set the over-scroll mode for this view. Valid over-scroll modes are
16212 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16213 * (allow over-scrolling only if the view content is larger than the container),
16214 * or {@link #OVER_SCROLL_NEVER}.
16215 *
16216 * Setting the over-scroll mode of a view will have an effect only if the
16217 * view is capable of scrolling.
16218 *
16219 * @param overScrollMode The new over-scroll mode for this view.
16220 */
16221 public void setOverScrollMode(int overScrollMode) {
16222 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16223 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16224 overScrollMode != OVER_SCROLL_NEVER) {
16225 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16226 }
16227 mOverScrollMode = overScrollMode;
16228 }
16229
16230 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016231 * Gets a scale factor that determines the distance the view should scroll
16232 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16233 * @return The vertical scroll scale factor.
16234 * @hide
16235 */
16236 protected float getVerticalScrollFactor() {
16237 if (mVerticalScrollFactor == 0) {
16238 TypedValue outValue = new TypedValue();
16239 if (!mContext.getTheme().resolveAttribute(
16240 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16241 throw new IllegalStateException(
16242 "Expected theme to define listPreferredItemHeight.");
16243 }
16244 mVerticalScrollFactor = outValue.getDimension(
16245 mContext.getResources().getDisplayMetrics());
16246 }
16247 return mVerticalScrollFactor;
16248 }
16249
16250 /**
16251 * Gets a scale factor that determines the distance the view should scroll
16252 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16253 * @return The horizontal scroll scale factor.
16254 * @hide
16255 */
16256 protected float getHorizontalScrollFactor() {
16257 // TODO: Should use something else.
16258 return getVerticalScrollFactor();
16259 }
16260
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016261 /**
16262 * Return the value specifying the text direction or policy that was set with
16263 * {@link #setTextDirection(int)}.
16264 *
16265 * @return the defined text direction. It can be one of:
16266 *
16267 * {@link #TEXT_DIRECTION_INHERIT},
16268 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16269 * {@link #TEXT_DIRECTION_ANY_RTL},
16270 * {@link #TEXT_DIRECTION_LTR},
16271 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016272 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016273 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016274 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016275 @ViewDebug.ExportedProperty(category = "text", mapping = {
16276 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16277 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16278 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16279 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16280 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16281 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16282 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016283 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016284 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016285 }
16286
16287 /**
16288 * Set the text direction.
16289 *
16290 * @param textDirection the direction to set. Should be one of:
16291 *
16292 * {@link #TEXT_DIRECTION_INHERIT},
16293 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16294 * {@link #TEXT_DIRECTION_ANY_RTL},
16295 * {@link #TEXT_DIRECTION_LTR},
16296 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016297 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016298 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016299 */
16300 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016301 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016302 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016303 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016304 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016305 // Set the new text direction
16306 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016307 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016308 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016309 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016310 }
16311 }
16312
16313 /**
16314 * Return the resolved text direction.
16315 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016316 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16317 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16318 * up the parent chain of the view. if there is no parent, then it will return the default
16319 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16320 *
16321 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016322 *
Doug Feltcb3791202011-07-07 11:57:48 -070016323 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16324 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016325 * {@link #TEXT_DIRECTION_LTR},
16326 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016327 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016328 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016329 */
16330 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016331 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016332 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016333 resolveTextDirection();
16334 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016335 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016336 }
16337
16338 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016339 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16340 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016341 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016342 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016343 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016344 // Reset any previous text direction resolution
16345 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16346
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016347 if (hasRtlSupport()) {
16348 // Set resolved text direction flag depending on text direction flag
16349 final int textDirection = getTextDirection();
16350 switch(textDirection) {
16351 case TEXT_DIRECTION_INHERIT:
16352 if (canResolveTextDirection()) {
16353 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016354
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016355 // Set current resolved direction to the same value as the parent's one
16356 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16357 switch (parentResolvedDirection) {
16358 case TEXT_DIRECTION_FIRST_STRONG:
16359 case TEXT_DIRECTION_ANY_RTL:
16360 case TEXT_DIRECTION_LTR:
16361 case TEXT_DIRECTION_RTL:
16362 case TEXT_DIRECTION_LOCALE:
16363 mPrivateFlags2 |=
16364 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16365 break;
16366 default:
16367 // Default resolved direction is "first strong" heuristic
16368 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16369 }
16370 } else {
16371 // We cannot do the resolution if there is no parent, so use the default one
16372 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016373 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016374 break;
16375 case TEXT_DIRECTION_FIRST_STRONG:
16376 case TEXT_DIRECTION_ANY_RTL:
16377 case TEXT_DIRECTION_LTR:
16378 case TEXT_DIRECTION_RTL:
16379 case TEXT_DIRECTION_LOCALE:
16380 // Resolved direction is the same as text direction
16381 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16382 break;
16383 default:
16384 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016385 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016386 }
16387 } else {
16388 // Default resolved direction is "first strong" heuristic
16389 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016390 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016391
16392 // Set to resolved
16393 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016394 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016395 }
16396
16397 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016398 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016399 * resolution should override this method.
16400 *
16401 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016402 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016403 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016404 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016405 }
16406
16407 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016408 * Check if text direction resolution can be done.
16409 *
16410 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016411 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016412 */
16413 public boolean canResolveTextDirection() {
16414 switch (getTextDirection()) {
16415 case TEXT_DIRECTION_INHERIT:
16416 return (mParent != null) && (mParent instanceof ViewGroup);
16417 default:
16418 return true;
16419 }
16420 }
16421
16422 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016423 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016424 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016425 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016426 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016427 */
16428 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016429 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016430 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016431 }
16432
16433 /**
16434 * Called when text direction is reset. Subclasses that care about text direction reset should
16435 * override this method and do a reset of the text direction of their children. The default
16436 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016437 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016438 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016439 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016440 }
16441
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016442 /**
16443 * Return the value specifying the text alignment or policy that was set with
16444 * {@link #setTextAlignment(int)}.
16445 *
16446 * @return the defined text alignment. It can be one of:
16447 *
16448 * {@link #TEXT_ALIGNMENT_INHERIT},
16449 * {@link #TEXT_ALIGNMENT_GRAVITY},
16450 * {@link #TEXT_ALIGNMENT_CENTER},
16451 * {@link #TEXT_ALIGNMENT_TEXT_START},
16452 * {@link #TEXT_ALIGNMENT_TEXT_END},
16453 * {@link #TEXT_ALIGNMENT_VIEW_START},
16454 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016455 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016456 */
16457 @ViewDebug.ExportedProperty(category = "text", mapping = {
16458 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16459 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16460 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16461 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16462 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16463 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16464 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16465 })
16466 public int getTextAlignment() {
16467 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16468 }
16469
16470 /**
16471 * Set the text alignment.
16472 *
16473 * @param textAlignment The text alignment to set. Should be one of
16474 *
16475 * {@link #TEXT_ALIGNMENT_INHERIT},
16476 * {@link #TEXT_ALIGNMENT_GRAVITY},
16477 * {@link #TEXT_ALIGNMENT_CENTER},
16478 * {@link #TEXT_ALIGNMENT_TEXT_START},
16479 * {@link #TEXT_ALIGNMENT_TEXT_END},
16480 * {@link #TEXT_ALIGNMENT_VIEW_START},
16481 * {@link #TEXT_ALIGNMENT_VIEW_END}
16482 *
16483 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016484 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016485 */
16486 public void setTextAlignment(int textAlignment) {
16487 if (textAlignment != getTextAlignment()) {
16488 // Reset the current and resolved text alignment
16489 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16490 resetResolvedTextAlignment();
16491 // Set the new text alignment
16492 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16493 // Refresh
16494 requestLayout();
16495 invalidate(true);
16496 }
16497 }
16498
16499 /**
16500 * Return the resolved text alignment.
16501 *
16502 * The resolved text alignment. This needs resolution if the value is
16503 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16504 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16505 *
16506 * @return the resolved text alignment. Returns one of:
16507 *
16508 * {@link #TEXT_ALIGNMENT_GRAVITY},
16509 * {@link #TEXT_ALIGNMENT_CENTER},
16510 * {@link #TEXT_ALIGNMENT_TEXT_START},
16511 * {@link #TEXT_ALIGNMENT_TEXT_END},
16512 * {@link #TEXT_ALIGNMENT_VIEW_START},
16513 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016514 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016515 */
16516 @ViewDebug.ExportedProperty(category = "text", mapping = {
16517 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16518 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16519 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16520 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16521 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16522 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16523 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16524 })
16525 public int getResolvedTextAlignment() {
16526 // If text alignment is not resolved, then resolve it
16527 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16528 resolveTextAlignment();
16529 }
16530 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16531 }
16532
16533 /**
16534 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16535 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016536 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016537 */
16538 public void resolveTextAlignment() {
16539 // Reset any previous text alignment resolution
16540 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16541
16542 if (hasRtlSupport()) {
16543 // Set resolved text alignment flag depending on text alignment flag
16544 final int textAlignment = getTextAlignment();
16545 switch (textAlignment) {
16546 case TEXT_ALIGNMENT_INHERIT:
16547 // Check if we can resolve the text alignment
16548 if (canResolveLayoutDirection() && mParent instanceof View) {
16549 View view = (View) mParent;
16550
16551 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16552 switch (parentResolvedTextAlignment) {
16553 case TEXT_ALIGNMENT_GRAVITY:
16554 case TEXT_ALIGNMENT_TEXT_START:
16555 case TEXT_ALIGNMENT_TEXT_END:
16556 case TEXT_ALIGNMENT_CENTER:
16557 case TEXT_ALIGNMENT_VIEW_START:
16558 case TEXT_ALIGNMENT_VIEW_END:
16559 // Resolved text alignment is the same as the parent resolved
16560 // text alignment
16561 mPrivateFlags2 |=
16562 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16563 break;
16564 default:
16565 // Use default resolved text alignment
16566 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16567 }
16568 }
16569 else {
16570 // We cannot do the resolution if there is no parent so use the default
16571 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16572 }
16573 break;
16574 case TEXT_ALIGNMENT_GRAVITY:
16575 case TEXT_ALIGNMENT_TEXT_START:
16576 case TEXT_ALIGNMENT_TEXT_END:
16577 case TEXT_ALIGNMENT_CENTER:
16578 case TEXT_ALIGNMENT_VIEW_START:
16579 case TEXT_ALIGNMENT_VIEW_END:
16580 // Resolved text alignment is the same as text alignment
16581 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16582 break;
16583 default:
16584 // Use default resolved text alignment
16585 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16586 }
16587 } else {
16588 // Use default resolved text alignment
16589 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16590 }
16591
16592 // Set the resolved
16593 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16594 onResolvedTextAlignmentChanged();
16595 }
16596
16597 /**
16598 * Check if text alignment resolution can be done.
16599 *
16600 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016601 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016602 */
16603 public boolean canResolveTextAlignment() {
16604 switch (getTextAlignment()) {
16605 case TEXT_DIRECTION_INHERIT:
16606 return (mParent != null);
16607 default:
16608 return true;
16609 }
16610 }
16611
16612 /**
16613 * Called when text alignment has been resolved. Subclasses that care about text alignment
16614 * resolution should override this method.
16615 *
16616 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016617 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016618 */
16619 public void onResolvedTextAlignmentChanged() {
16620 }
16621
16622 /**
16623 * Reset resolved text alignment. Text alignment can be resolved with a call to
16624 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16625 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016626 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016627 */
16628 public void resetResolvedTextAlignment() {
16629 // Reset any previous text alignment resolution
16630 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16631 onResolvedTextAlignmentReset();
16632 }
16633
16634 /**
16635 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16636 * override this method and do a reset of the text alignment of their children. The default
16637 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016638 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016639 */
16640 public void onResolvedTextAlignmentReset() {
16641 }
16642
Chet Haaseb39f0512011-05-24 14:36:40 -070016643 //
16644 // Properties
16645 //
16646 /**
16647 * A Property wrapper around the <code>alpha</code> functionality handled by the
16648 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16649 */
Chet Haased47f1532011-12-16 11:18:52 -080016650 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016651 @Override
16652 public void setValue(View object, float value) {
16653 object.setAlpha(value);
16654 }
16655
16656 @Override
16657 public Float get(View object) {
16658 return object.getAlpha();
16659 }
16660 };
16661
16662 /**
16663 * A Property wrapper around the <code>translationX</code> functionality handled by the
16664 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16665 */
Chet Haased47f1532011-12-16 11:18:52 -080016666 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016667 @Override
16668 public void setValue(View object, float value) {
16669 object.setTranslationX(value);
16670 }
16671
16672 @Override
16673 public Float get(View object) {
16674 return object.getTranslationX();
16675 }
16676 };
16677
16678 /**
16679 * A Property wrapper around the <code>translationY</code> functionality handled by the
16680 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16681 */
Chet Haased47f1532011-12-16 11:18:52 -080016682 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016683 @Override
16684 public void setValue(View object, float value) {
16685 object.setTranslationY(value);
16686 }
16687
16688 @Override
16689 public Float get(View object) {
16690 return object.getTranslationY();
16691 }
16692 };
16693
16694 /**
16695 * A Property wrapper around the <code>x</code> functionality handled by the
16696 * {@link View#setX(float)} and {@link View#getX()} methods.
16697 */
Chet Haased47f1532011-12-16 11:18:52 -080016698 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016699 @Override
16700 public void setValue(View object, float value) {
16701 object.setX(value);
16702 }
16703
16704 @Override
16705 public Float get(View object) {
16706 return object.getX();
16707 }
16708 };
16709
16710 /**
16711 * A Property wrapper around the <code>y</code> functionality handled by the
16712 * {@link View#setY(float)} and {@link View#getY()} methods.
16713 */
Chet Haased47f1532011-12-16 11:18:52 -080016714 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016715 @Override
16716 public void setValue(View object, float value) {
16717 object.setY(value);
16718 }
16719
16720 @Override
16721 public Float get(View object) {
16722 return object.getY();
16723 }
16724 };
16725
16726 /**
16727 * A Property wrapper around the <code>rotation</code> functionality handled by the
16728 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16729 */
Chet Haased47f1532011-12-16 11:18:52 -080016730 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016731 @Override
16732 public void setValue(View object, float value) {
16733 object.setRotation(value);
16734 }
16735
16736 @Override
16737 public Float get(View object) {
16738 return object.getRotation();
16739 }
16740 };
16741
16742 /**
16743 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16744 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16745 */
Chet Haased47f1532011-12-16 11:18:52 -080016746 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016747 @Override
16748 public void setValue(View object, float value) {
16749 object.setRotationX(value);
16750 }
16751
16752 @Override
16753 public Float get(View object) {
16754 return object.getRotationX();
16755 }
16756 };
16757
16758 /**
16759 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16760 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16761 */
Chet Haased47f1532011-12-16 11:18:52 -080016762 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016763 @Override
16764 public void setValue(View object, float value) {
16765 object.setRotationY(value);
16766 }
16767
16768 @Override
16769 public Float get(View object) {
16770 return object.getRotationY();
16771 }
16772 };
16773
16774 /**
16775 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16776 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16777 */
Chet Haased47f1532011-12-16 11:18:52 -080016778 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016779 @Override
16780 public void setValue(View object, float value) {
16781 object.setScaleX(value);
16782 }
16783
16784 @Override
16785 public Float get(View object) {
16786 return object.getScaleX();
16787 }
16788 };
16789
16790 /**
16791 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16792 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16793 */
Chet Haased47f1532011-12-16 11:18:52 -080016794 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016795 @Override
16796 public void setValue(View object, float value) {
16797 object.setScaleY(value);
16798 }
16799
16800 @Override
16801 public Float get(View object) {
16802 return object.getScaleY();
16803 }
16804 };
16805
Jeff Brown33bbfd22011-02-24 20:55:35 -080016806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016807 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16808 * Each MeasureSpec represents a requirement for either the width or the height.
16809 * A MeasureSpec is comprised of a size and a mode. There are three possible
16810 * modes:
16811 * <dl>
16812 * <dt>UNSPECIFIED</dt>
16813 * <dd>
16814 * The parent has not imposed any constraint on the child. It can be whatever size
16815 * it wants.
16816 * </dd>
16817 *
16818 * <dt>EXACTLY</dt>
16819 * <dd>
16820 * The parent has determined an exact size for the child. The child is going to be
16821 * given those bounds regardless of how big it wants to be.
16822 * </dd>
16823 *
16824 * <dt>AT_MOST</dt>
16825 * <dd>
16826 * The child can be as large as it wants up to the specified size.
16827 * </dd>
16828 * </dl>
16829 *
16830 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16831 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16832 */
16833 public static class MeasureSpec {
16834 private static final int MODE_SHIFT = 30;
16835 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16836
16837 /**
16838 * Measure specification mode: The parent has not imposed any constraint
16839 * on the child. It can be whatever size it wants.
16840 */
16841 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16842
16843 /**
16844 * Measure specification mode: The parent has determined an exact size
16845 * for the child. The child is going to be given those bounds regardless
16846 * of how big it wants to be.
16847 */
16848 public static final int EXACTLY = 1 << MODE_SHIFT;
16849
16850 /**
16851 * Measure specification mode: The child can be as large as it wants up
16852 * to the specified size.
16853 */
16854 public static final int AT_MOST = 2 << MODE_SHIFT;
16855
16856 /**
16857 * Creates a measure specification based on the supplied size and mode.
16858 *
16859 * The mode must always be one of the following:
16860 * <ul>
16861 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16862 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16863 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16864 * </ul>
16865 *
16866 * @param size the size of the measure specification
16867 * @param mode the mode of the measure specification
16868 * @return the measure specification based on size and mode
16869 */
16870 public static int makeMeasureSpec(int size, int mode) {
16871 return size + mode;
16872 }
16873
16874 /**
16875 * Extracts the mode from the supplied measure specification.
16876 *
16877 * @param measureSpec the measure specification to extract the mode from
16878 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16879 * {@link android.view.View.MeasureSpec#AT_MOST} or
16880 * {@link android.view.View.MeasureSpec#EXACTLY}
16881 */
16882 public static int getMode(int measureSpec) {
16883 return (measureSpec & MODE_MASK);
16884 }
16885
16886 /**
16887 * Extracts the size from the supplied measure specification.
16888 *
16889 * @param measureSpec the measure specification to extract the size from
16890 * @return the size in pixels defined in the supplied measure specification
16891 */
16892 public static int getSize(int measureSpec) {
16893 return (measureSpec & ~MODE_MASK);
16894 }
16895
16896 /**
16897 * Returns a String representation of the specified measure
16898 * specification.
16899 *
16900 * @param measureSpec the measure specification to convert to a String
16901 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16902 */
16903 public static String toString(int measureSpec) {
16904 int mode = getMode(measureSpec);
16905 int size = getSize(measureSpec);
16906
16907 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16908
16909 if (mode == UNSPECIFIED)
16910 sb.append("UNSPECIFIED ");
16911 else if (mode == EXACTLY)
16912 sb.append("EXACTLY ");
16913 else if (mode == AT_MOST)
16914 sb.append("AT_MOST ");
16915 else
16916 sb.append(mode).append(" ");
16917
16918 sb.append(size);
16919 return sb.toString();
16920 }
16921 }
16922
16923 class CheckForLongPress implements Runnable {
16924
16925 private int mOriginalWindowAttachCount;
16926
16927 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016928 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016929 && mOriginalWindowAttachCount == mWindowAttachCount) {
16930 if (performLongClick()) {
16931 mHasPerformedLongPress = true;
16932 }
16933 }
16934 }
16935
16936 public void rememberWindowAttachCount() {
16937 mOriginalWindowAttachCount = mWindowAttachCount;
16938 }
16939 }
Joe Malin32736f02011-01-19 16:14:20 -080016940
Adam Powelle14579b2009-12-16 18:39:52 -080016941 private final class CheckForTap implements Runnable {
16942 public void run() {
16943 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016944 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016945 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016946 }
16947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016948
Adam Powella35d7682010-03-12 14:48:13 -080016949 private final class PerformClick implements Runnable {
16950 public void run() {
16951 performClick();
16952 }
16953 }
16954
Dianne Hackborn63042d62011-01-26 18:56:29 -080016955 /** @hide */
16956 public void hackTurnOffWindowResizeAnim(boolean off) {
16957 mAttachInfo.mTurnOffWindowResizeAnim = off;
16958 }
Joe Malin32736f02011-01-19 16:14:20 -080016959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016960 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016961 * This method returns a ViewPropertyAnimator object, which can be used to animate
16962 * specific properties on this View.
16963 *
16964 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16965 */
16966 public ViewPropertyAnimator animate() {
16967 if (mAnimator == null) {
16968 mAnimator = new ViewPropertyAnimator(this);
16969 }
16970 return mAnimator;
16971 }
16972
16973 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016974 * Interface definition for a callback to be invoked when a hardware key event is
16975 * dispatched to this view. The callback will be invoked before the key event is
16976 * given to the view. This is only useful for hardware keyboards; a software input
16977 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016978 */
16979 public interface OnKeyListener {
16980 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016981 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016982 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016983 * <p>Key presses in software keyboards will generally NOT trigger this method,
16984 * although some may elect to do so in some situations. Do not assume a
16985 * software input method has to be key-based; even if it is, it may use key presses
16986 * in a different way than you expect, so there is no way to reliably catch soft
16987 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016988 *
16989 * @param v The view the key has been dispatched to.
16990 * @param keyCode The code for the physical key that was pressed
16991 * @param event The KeyEvent object containing full information about
16992 * the event.
16993 * @return True if the listener has consumed the event, false otherwise.
16994 */
16995 boolean onKey(View v, int keyCode, KeyEvent event);
16996 }
16997
16998 /**
16999 * Interface definition for a callback to be invoked when a touch event is
17000 * dispatched to this view. The callback will be invoked before the touch
17001 * event is given to the view.
17002 */
17003 public interface OnTouchListener {
17004 /**
17005 * Called when a touch event is dispatched to a view. This allows listeners to
17006 * get a chance to respond before the target view.
17007 *
17008 * @param v The view the touch event has been dispatched to.
17009 * @param event The MotionEvent object containing full information about
17010 * the event.
17011 * @return True if the listener has consumed the event, false otherwise.
17012 */
17013 boolean onTouch(View v, MotionEvent event);
17014 }
17015
17016 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017017 * Interface definition for a callback to be invoked when a hover event is
17018 * dispatched to this view. The callback will be invoked before the hover
17019 * event is given to the view.
17020 */
17021 public interface OnHoverListener {
17022 /**
17023 * Called when a hover event is dispatched to a view. This allows listeners to
17024 * get a chance to respond before the target view.
17025 *
17026 * @param v The view the hover event has been dispatched to.
17027 * @param event The MotionEvent object containing full information about
17028 * the event.
17029 * @return True if the listener has consumed the event, false otherwise.
17030 */
17031 boolean onHover(View v, MotionEvent event);
17032 }
17033
17034 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017035 * Interface definition for a callback to be invoked when a generic motion event is
17036 * dispatched to this view. The callback will be invoked before the generic motion
17037 * event is given to the view.
17038 */
17039 public interface OnGenericMotionListener {
17040 /**
17041 * Called when a generic motion event is dispatched to a view. This allows listeners to
17042 * get a chance to respond before the target view.
17043 *
17044 * @param v The view the generic motion event has been dispatched to.
17045 * @param event The MotionEvent object containing full information about
17046 * the event.
17047 * @return True if the listener has consumed the event, false otherwise.
17048 */
17049 boolean onGenericMotion(View v, MotionEvent event);
17050 }
17051
17052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017053 * Interface definition for a callback to be invoked when a view has been clicked and held.
17054 */
17055 public interface OnLongClickListener {
17056 /**
17057 * Called when a view has been clicked and held.
17058 *
17059 * @param v The view that was clicked and held.
17060 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017061 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017062 */
17063 boolean onLongClick(View v);
17064 }
17065
17066 /**
Chris Tate32affef2010-10-18 15:29:21 -070017067 * Interface definition for a callback to be invoked when a drag is being dispatched
17068 * to this view. The callback will be invoked before the hosting view's own
17069 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17070 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017071 *
17072 * <div class="special reference">
17073 * <h3>Developer Guides</h3>
17074 * <p>For a guide to implementing drag and drop features, read the
17075 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17076 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017077 */
17078 public interface OnDragListener {
17079 /**
17080 * Called when a drag event is dispatched to a view. This allows listeners
17081 * to get a chance to override base View behavior.
17082 *
Joe Malin32736f02011-01-19 16:14:20 -080017083 * @param v The View that received the drag event.
17084 * @param event The {@link android.view.DragEvent} object for the drag event.
17085 * @return {@code true} if the drag event was handled successfully, or {@code false}
17086 * if the drag event was not handled. Note that {@code false} will trigger the View
17087 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017088 */
17089 boolean onDrag(View v, DragEvent event);
17090 }
17091
17092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017093 * Interface definition for a callback to be invoked when the focus state of
17094 * a view changed.
17095 */
17096 public interface OnFocusChangeListener {
17097 /**
17098 * Called when the focus state of a view has changed.
17099 *
17100 * @param v The view whose state has changed.
17101 * @param hasFocus The new focus state of v.
17102 */
17103 void onFocusChange(View v, boolean hasFocus);
17104 }
17105
17106 /**
17107 * Interface definition for a callback to be invoked when a view is clicked.
17108 */
17109 public interface OnClickListener {
17110 /**
17111 * Called when a view has been clicked.
17112 *
17113 * @param v The view that was clicked.
17114 */
17115 void onClick(View v);
17116 }
17117
17118 /**
17119 * Interface definition for a callback to be invoked when the context menu
17120 * for this view is being built.
17121 */
17122 public interface OnCreateContextMenuListener {
17123 /**
17124 * Called when the context menu for this view is being built. It is not
17125 * safe to hold onto the menu after this method returns.
17126 *
17127 * @param menu The context menu that is being built
17128 * @param v The view for which the context menu is being built
17129 * @param menuInfo Extra information about the item for which the
17130 * context menu should be shown. This information will vary
17131 * depending on the class of v.
17132 */
17133 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17134 }
17135
Joe Onorato664644d2011-01-23 17:53:23 -080017136 /**
17137 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017138 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017139 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017140 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017141 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017142 */
17143 public interface OnSystemUiVisibilityChangeListener {
17144 /**
17145 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017146 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017147 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017148 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17149 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17150 * This tells you the <strong>global</strong> state of these UI visibility
17151 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017152 */
17153 public void onSystemUiVisibilityChange(int visibility);
17154 }
17155
Adam Powell4afd62b2011-02-18 15:02:18 -080017156 /**
17157 * Interface definition for a callback to be invoked when this view is attached
17158 * or detached from its window.
17159 */
17160 public interface OnAttachStateChangeListener {
17161 /**
17162 * Called when the view is attached to a window.
17163 * @param v The view that was attached
17164 */
17165 public void onViewAttachedToWindow(View v);
17166 /**
17167 * Called when the view is detached from a window.
17168 * @param v The view that was detached
17169 */
17170 public void onViewDetachedFromWindow(View v);
17171 }
17172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017173 private final class UnsetPressedState implements Runnable {
17174 public void run() {
17175 setPressed(false);
17176 }
17177 }
17178
17179 /**
17180 * Base class for derived classes that want to save and restore their own
17181 * state in {@link android.view.View#onSaveInstanceState()}.
17182 */
17183 public static class BaseSavedState extends AbsSavedState {
17184 /**
17185 * Constructor used when reading from a parcel. Reads the state of the superclass.
17186 *
17187 * @param source
17188 */
17189 public BaseSavedState(Parcel source) {
17190 super(source);
17191 }
17192
17193 /**
17194 * Constructor called by derived classes when creating their SavedState objects
17195 *
17196 * @param superState The state of the superclass of this view
17197 */
17198 public BaseSavedState(Parcelable superState) {
17199 super(superState);
17200 }
17201
17202 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17203 new Parcelable.Creator<BaseSavedState>() {
17204 public BaseSavedState createFromParcel(Parcel in) {
17205 return new BaseSavedState(in);
17206 }
17207
17208 public BaseSavedState[] newArray(int size) {
17209 return new BaseSavedState[size];
17210 }
17211 };
17212 }
17213
17214 /**
17215 * A set of information given to a view when it is attached to its parent
17216 * window.
17217 */
17218 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017219 interface Callbacks {
17220 void playSoundEffect(int effectId);
17221 boolean performHapticFeedback(int effectId, boolean always);
17222 }
17223
17224 /**
17225 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17226 * to a Handler. This class contains the target (View) to invalidate and
17227 * the coordinates of the dirty rectangle.
17228 *
17229 * For performance purposes, this class also implements a pool of up to
17230 * POOL_LIMIT objects that get reused. This reduces memory allocations
17231 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017232 */
Romain Guyd928d682009-03-31 17:52:16 -070017233 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017234 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017235 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17236 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017237 public InvalidateInfo newInstance() {
17238 return new InvalidateInfo();
17239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017240
Romain Guyd928d682009-03-31 17:52:16 -070017241 public void onAcquired(InvalidateInfo element) {
17242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017243
Romain Guyd928d682009-03-31 17:52:16 -070017244 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017245 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017246 }
17247 }, POOL_LIMIT)
17248 );
17249
17250 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017251 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017252
17253 View target;
17254
17255 int left;
17256 int top;
17257 int right;
17258 int bottom;
17259
Romain Guyd928d682009-03-31 17:52:16 -070017260 public void setNextPoolable(InvalidateInfo element) {
17261 mNext = element;
17262 }
17263
17264 public InvalidateInfo getNextPoolable() {
17265 return mNext;
17266 }
17267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017269 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017270 }
17271
17272 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017273 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017274 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017275
17276 public boolean isPooled() {
17277 return mIsPooled;
17278 }
17279
17280 public void setPooled(boolean isPooled) {
17281 mIsPooled = isPooled;
17282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017283 }
17284
17285 final IWindowSession mSession;
17286
17287 final IWindow mWindow;
17288
17289 final IBinder mWindowToken;
17290
17291 final Callbacks mRootCallbacks;
17292
Romain Guy59a12ca2011-06-09 17:48:21 -070017293 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017295 /**
17296 * The top view of the hierarchy.
17297 */
17298 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017300 IBinder mPanelParentWindowToken;
17301 Surface mSurface;
17302
Romain Guyb051e892010-09-28 19:09:36 -070017303 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017304 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017305 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017306
Romain Guy7e4e5612012-03-05 14:37:29 -080017307 boolean mScreenOn;
17308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017309 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017310 * Scale factor used by the compatibility mode
17311 */
17312 float mApplicationScale;
17313
17314 /**
17315 * Indicates whether the application is in compatibility mode
17316 */
17317 boolean mScalingRequired;
17318
17319 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017320 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017321 */
17322 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017323
Dianne Hackborn63042d62011-01-26 18:56:29 -080017324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325 * Left position of this view's window
17326 */
17327 int mWindowLeft;
17328
17329 /**
17330 * Top position of this view's window
17331 */
17332 int mWindowTop;
17333
17334 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -070017335 * Left actual position of this view's window.
17336 *
17337 * TODO: This is a workaround for 6623031. Remove when fixed.
17338 */
17339 int mActualWindowLeft;
17340
17341 /**
17342 * Actual top position of this view's window.
17343 *
17344 * TODO: This is a workaround for 6623031. Remove when fixed.
17345 */
17346 int mActualWindowTop;
17347
17348 /**
Adam Powell26153a32010-11-08 15:22:27 -080017349 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017350 */
Adam Powell26153a32010-11-08 15:22:27 -080017351 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017352
17353 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017354 * For windows that are full-screen but using insets to layout inside
17355 * of the screen decorations, these are the current insets for the
17356 * content of the window.
17357 */
17358 final Rect mContentInsets = new Rect();
17359
17360 /**
17361 * For windows that are full-screen but using insets to layout inside
17362 * of the screen decorations, these are the current insets for the
17363 * actual visible parts of the window.
17364 */
17365 final Rect mVisibleInsets = new Rect();
17366
17367 /**
17368 * The internal insets given by this window. This value is
17369 * supplied by the client (through
17370 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17371 * be given to the window manager when changed to be used in laying
17372 * out windows behind it.
17373 */
17374 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17375 = new ViewTreeObserver.InternalInsetsInfo();
17376
17377 /**
17378 * All views in the window's hierarchy that serve as scroll containers,
17379 * used to determine if the window can be resized or must be panned
17380 * to adjust for a soft input area.
17381 */
17382 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17383
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017384 final KeyEvent.DispatcherState mKeyDispatchState
17385 = new KeyEvent.DispatcherState();
17386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017387 /**
17388 * Indicates whether the view's window currently has the focus.
17389 */
17390 boolean mHasWindowFocus;
17391
17392 /**
17393 * The current visibility of the window.
17394 */
17395 int mWindowVisibility;
17396
17397 /**
17398 * Indicates the time at which drawing started to occur.
17399 */
17400 long mDrawingTime;
17401
17402 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017403 * Indicates whether or not ignoring the DIRTY_MASK flags.
17404 */
17405 boolean mIgnoreDirtyState;
17406
17407 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017408 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17409 * to avoid clearing that flag prematurely.
17410 */
17411 boolean mSetIgnoreDirtyState = false;
17412
17413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017414 * Indicates whether the view's window is currently in touch mode.
17415 */
17416 boolean mInTouchMode;
17417
17418 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017419 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017420 * the next time it performs a traversal
17421 */
17422 boolean mRecomputeGlobalAttributes;
17423
17424 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017425 * Always report new attributes at next traversal.
17426 */
17427 boolean mForceReportNewAttributes;
17428
17429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017430 * Set during a traveral if any views want to keep the screen on.
17431 */
17432 boolean mKeepScreenOn;
17433
17434 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017435 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17436 */
17437 int mSystemUiVisibility;
17438
17439 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017440 * Hack to force certain system UI visibility flags to be cleared.
17441 */
17442 int mDisabledSystemUiVisibility;
17443
17444 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017445 * Last global system UI visibility reported by the window manager.
17446 */
17447 int mGlobalSystemUiVisibility;
17448
17449 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017450 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17451 * attached.
17452 */
17453 boolean mHasSystemUiListeners;
17454
17455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017456 * Set if the visibility of any views has changed.
17457 */
17458 boolean mViewVisibilityChanged;
17459
17460 /**
17461 * Set to true if a view has been scrolled.
17462 */
17463 boolean mViewScrollChanged;
17464
17465 /**
17466 * Global to the view hierarchy used as a temporary for dealing with
17467 * x/y points in the transparent region computations.
17468 */
17469 final int[] mTransparentLocation = new int[2];
17470
17471 /**
17472 * Global to the view hierarchy used as a temporary for dealing with
17473 * x/y points in the ViewGroup.invalidateChild implementation.
17474 */
17475 final int[] mInvalidateChildLocation = new int[2];
17476
Chet Haasec3aa3612010-06-17 08:50:37 -070017477
17478 /**
17479 * Global to the view hierarchy used as a temporary for dealing with
17480 * x/y location when view is transformed.
17481 */
17482 final float[] mTmpTransformLocation = new float[2];
17483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017484 /**
17485 * The view tree observer used to dispatch global events like
17486 * layout, pre-draw, touch mode change, etc.
17487 */
17488 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17489
17490 /**
17491 * A Canvas used by the view hierarchy to perform bitmap caching.
17492 */
17493 Canvas mCanvas;
17494
17495 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017496 * The view root impl.
17497 */
17498 final ViewRootImpl mViewRootImpl;
17499
17500 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017501 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017502 * handler can be used to pump events in the UI events queue.
17503 */
17504 final Handler mHandler;
17505
17506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017507 * Temporary for use in computing invalidate rectangles while
17508 * calling up the hierarchy.
17509 */
17510 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017511
17512 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017513 * Temporary for use in computing hit areas with transformed views
17514 */
17515 final RectF mTmpTransformRect = new RectF();
17516
17517 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017518 * Temporary list for use in collecting focusable descendents of a view.
17519 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017520 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017522 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017523 * The id of the window for accessibility purposes.
17524 */
17525 int mAccessibilityWindowId = View.NO_ID;
17526
17527 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017528 * Whether to ingore not exposed for accessibility Views when
17529 * reporting the view tree to accessibility services.
17530 */
17531 boolean mIncludeNotImportantViews;
17532
17533 /**
17534 * The drawable for highlighting accessibility focus.
17535 */
17536 Drawable mAccessibilityFocusDrawable;
17537
17538 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017539 * Show where the margins, bounds and layout bounds are for each view.
17540 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017541 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017542
17543 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017544 * Point used to compute visible regions.
17545 */
17546 final Point mPoint = new Point();
17547
17548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017549 * Creates a new set of attachment information with the specified
17550 * events handler and thread.
17551 *
17552 * @param handler the events handler the view must use
17553 */
17554 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017555 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017556 mSession = session;
17557 mWindow = window;
17558 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017559 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017560 mHandler = handler;
17561 mRootCallbacks = effectPlayer;
17562 }
17563 }
17564
17565 /**
17566 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17567 * is supported. This avoids keeping too many unused fields in most
17568 * instances of View.</p>
17569 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017570 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017571
Mike Cleronf116bf82009-09-27 19:14:12 -070017572 /**
17573 * Scrollbars are not visible
17574 */
17575 public static final int OFF = 0;
17576
17577 /**
17578 * Scrollbars are visible
17579 */
17580 public static final int ON = 1;
17581
17582 /**
17583 * Scrollbars are fading away
17584 */
17585 public static final int FADING = 2;
17586
17587 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017589 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017590 public int scrollBarDefaultDelayBeforeFade;
17591 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017592
17593 public int scrollBarSize;
17594 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017595 public float[] interpolatorValues;
17596 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017597
17598 public final Paint paint;
17599 public final Matrix matrix;
17600 public Shader shader;
17601
Mike Cleronf116bf82009-09-27 19:14:12 -070017602 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17603
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017604 private static final float[] OPAQUE = { 255 };
17605 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017606
Mike Cleronf116bf82009-09-27 19:14:12 -070017607 /**
17608 * When fading should start. This time moves into the future every time
17609 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17610 */
17611 public long fadeStartTime;
17612
17613
17614 /**
17615 * The current state of the scrollbars: ON, OFF, or FADING
17616 */
17617 public int state = OFF;
17618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017619 private int mLastColor;
17620
Mike Cleronf116bf82009-09-27 19:14:12 -070017621 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017622 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17623 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017624 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17625 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017626
17627 paint = new Paint();
17628 matrix = new Matrix();
17629 // use use a height of 1, and then wack the matrix each time we
17630 // actually use it.
17631 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017633 paint.setShader(shader);
17634 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017635 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017636 }
Romain Guy8506ab42009-06-11 17:35:47 -070017637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017638 public void setFadeColor(int color) {
17639 if (color != 0 && color != mLastColor) {
17640 mLastColor = color;
17641 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017642
Romain Guye55e1a72009-08-27 10:42:26 -070017643 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17644 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017646 paint.setShader(shader);
17647 // Restore the default transfer mode (src_over)
17648 paint.setXfermode(null);
17649 }
17650 }
Joe Malin32736f02011-01-19 16:14:20 -080017651
Mike Cleronf116bf82009-09-27 19:14:12 -070017652 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017653 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017654 if (now >= fadeStartTime) {
17655
17656 // the animation fades the scrollbars out by changing
17657 // the opacity (alpha) from fully opaque to fully
17658 // transparent
17659 int nextFrame = (int) now;
17660 int framesCount = 0;
17661
17662 Interpolator interpolator = scrollBarInterpolator;
17663
17664 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017665 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017666
17667 // End transparent
17668 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017669 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017670
17671 state = FADING;
17672
17673 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017674 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017675 }
17676 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017677 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017678
Svetoslav Ganova0156172011-06-26 17:55:44 -070017679 /**
17680 * Resuable callback for sending
17681 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17682 */
17683 private class SendViewScrolledAccessibilityEvent implements Runnable {
17684 public volatile boolean mIsPending;
17685
17686 public void run() {
17687 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17688 mIsPending = false;
17689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017690 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017691
17692 /**
17693 * <p>
17694 * This class represents a delegate that can be registered in a {@link View}
17695 * to enhance accessibility support via composition rather via inheritance.
17696 * It is specifically targeted to widget developers that extend basic View
17697 * classes i.e. classes in package android.view, that would like their
17698 * applications to be backwards compatible.
17699 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017700 * <div class="special reference">
17701 * <h3>Developer Guides</h3>
17702 * <p>For more information about making applications accessible, read the
17703 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17704 * developer guide.</p>
17705 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017706 * <p>
17707 * A scenario in which a developer would like to use an accessibility delegate
17708 * is overriding a method introduced in a later API version then the minimal API
17709 * version supported by the application. For example, the method
17710 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17711 * in API version 4 when the accessibility APIs were first introduced. If a
17712 * developer would like his application to run on API version 4 devices (assuming
17713 * all other APIs used by the application are version 4 or lower) and take advantage
17714 * of this method, instead of overriding the method which would break the application's
17715 * backwards compatibility, he can override the corresponding method in this
17716 * delegate and register the delegate in the target View if the API version of
17717 * the system is high enough i.e. the API version is same or higher to the API
17718 * version that introduced
17719 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17720 * </p>
17721 * <p>
17722 * Here is an example implementation:
17723 * </p>
17724 * <code><pre><p>
17725 * if (Build.VERSION.SDK_INT >= 14) {
17726 * // If the API version is equal of higher than the version in
17727 * // which onInitializeAccessibilityNodeInfo was introduced we
17728 * // register a delegate with a customized implementation.
17729 * View view = findViewById(R.id.view_id);
17730 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17731 * public void onInitializeAccessibilityNodeInfo(View host,
17732 * AccessibilityNodeInfo info) {
17733 * // Let the default implementation populate the info.
17734 * super.onInitializeAccessibilityNodeInfo(host, info);
17735 * // Set some other information.
17736 * info.setEnabled(host.isEnabled());
17737 * }
17738 * });
17739 * }
17740 * </code></pre></p>
17741 * <p>
17742 * This delegate contains methods that correspond to the accessibility methods
17743 * in View. If a delegate has been specified the implementation in View hands
17744 * off handling to the corresponding method in this delegate. The default
17745 * implementation the delegate methods behaves exactly as the corresponding
17746 * method in View for the case of no accessibility delegate been set. Hence,
17747 * to customize the behavior of a View method, clients can override only the
17748 * corresponding delegate method without altering the behavior of the rest
17749 * accessibility related methods of the host view.
17750 * </p>
17751 */
17752 public static class AccessibilityDelegate {
17753
17754 /**
17755 * Sends an accessibility event of the given type. If accessibility is not
17756 * enabled this method has no effect.
17757 * <p>
17758 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17759 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17760 * been set.
17761 * </p>
17762 *
17763 * @param host The View hosting the delegate.
17764 * @param eventType The type of the event to send.
17765 *
17766 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17767 */
17768 public void sendAccessibilityEvent(View host, int eventType) {
17769 host.sendAccessibilityEventInternal(eventType);
17770 }
17771
17772 /**
alanv8eeefef2012-05-07 16:57:53 -070017773 * Performs the specified accessibility action on the view. For
17774 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17775 * <p>
17776 * The default implementation behaves as
17777 * {@link View#performAccessibilityAction(int, Bundle)
17778 * View#performAccessibilityAction(int, Bundle)} for the case of
17779 * no accessibility delegate been set.
17780 * </p>
17781 *
17782 * @param action The action to perform.
17783 * @return Whether the action was performed.
17784 *
17785 * @see View#performAccessibilityAction(int, Bundle)
17786 * View#performAccessibilityAction(int, Bundle)
17787 */
17788 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17789 return host.performAccessibilityActionInternal(action, args);
17790 }
17791
17792 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017793 * Sends an accessibility event. This method behaves exactly as
17794 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17795 * empty {@link AccessibilityEvent} and does not perform a check whether
17796 * accessibility is enabled.
17797 * <p>
17798 * The default implementation behaves as
17799 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17800 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17801 * the case of no accessibility delegate been set.
17802 * </p>
17803 *
17804 * @param host The View hosting the delegate.
17805 * @param event The event to send.
17806 *
17807 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17808 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17809 */
17810 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17811 host.sendAccessibilityEventUncheckedInternal(event);
17812 }
17813
17814 /**
17815 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17816 * to its children for adding their text content to the event.
17817 * <p>
17818 * The default implementation behaves as
17819 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17820 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17821 * the case of no accessibility delegate been set.
17822 * </p>
17823 *
17824 * @param host The View hosting the delegate.
17825 * @param event The event.
17826 * @return True if the event population was completed.
17827 *
17828 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17829 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17830 */
17831 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17832 return host.dispatchPopulateAccessibilityEventInternal(event);
17833 }
17834
17835 /**
17836 * Gives a chance to the host View to populate the accessibility event with its
17837 * text content.
17838 * <p>
17839 * The default implementation behaves as
17840 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17841 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17842 * the case of no accessibility delegate been set.
17843 * </p>
17844 *
17845 * @param host The View hosting the delegate.
17846 * @param event The accessibility event which to populate.
17847 *
17848 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17849 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17850 */
17851 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17852 host.onPopulateAccessibilityEventInternal(event);
17853 }
17854
17855 /**
17856 * Initializes an {@link AccessibilityEvent} with information about the
17857 * the host View which is the event source.
17858 * <p>
17859 * The default implementation behaves as
17860 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17861 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17862 * the case of no accessibility delegate been set.
17863 * </p>
17864 *
17865 * @param host The View hosting the delegate.
17866 * @param event The event to initialize.
17867 *
17868 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17869 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17870 */
17871 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17872 host.onInitializeAccessibilityEventInternal(event);
17873 }
17874
17875 /**
17876 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17877 * <p>
17878 * The default implementation behaves as
17879 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17880 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17881 * the case of no accessibility delegate been set.
17882 * </p>
17883 *
17884 * @param host The View hosting the delegate.
17885 * @param info The instance to initialize.
17886 *
17887 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17888 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17889 */
17890 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17891 host.onInitializeAccessibilityNodeInfoInternal(info);
17892 }
17893
17894 /**
17895 * Called when a child of the host View has requested sending an
17896 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17897 * to augment the event.
17898 * <p>
17899 * The default implementation behaves as
17900 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17901 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17902 * the case of no accessibility delegate been set.
17903 * </p>
17904 *
17905 * @param host The View hosting the delegate.
17906 * @param child The child which requests sending the event.
17907 * @param event The event to be sent.
17908 * @return True if the event should be sent
17909 *
17910 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17911 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17912 */
17913 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17914 AccessibilityEvent event) {
17915 return host.onRequestSendAccessibilityEventInternal(child, event);
17916 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017917
17918 /**
17919 * Gets the provider for managing a virtual view hierarchy rooted at this View
17920 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17921 * that explore the window content.
17922 * <p>
17923 * The default implementation behaves as
17924 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17925 * the case of no accessibility delegate been set.
17926 * </p>
17927 *
17928 * @return The provider.
17929 *
17930 * @see AccessibilityNodeProvider
17931 */
17932 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17933 return null;
17934 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017936}